blob: bd02760257e596d0f910fea19ac64fbf8ea900c7 [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
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/kernel.h>
15#include <linux/mfd/syscon.h>
16#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070017#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
Sean Crossbb389192013-09-26 11:24:47 +080018#include <linux/module.h>
19#include <linux/of_gpio.h>
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050020#include <linux/of_device.h>
Sean Crossbb389192013-09-26 11:24:47 +080021#include <linux/pci.h>
22#include <linux/platform_device.h>
23#include <linux/regmap.h>
Quentin Schulzc26ebe92017-06-08 10:07:42 +020024#include <linux/regulator/consumer.h>
Sean Crossbb389192013-09-26 11:24:47 +080025#include <linux/resource.h>
26#include <linux/signal.h>
27#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010028#include <linux/interrupt.h>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070029#include <linux/reset.h>
Sean Crossbb389192013-09-26 11:24:47 +080030
31#include "pcie-designware.h"
32
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053033#define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
Sean Crossbb389192013-09-26 11:24:47 +080034
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050035enum imx6_pcie_variants {
36 IMX6Q,
Andrey Smirnov4d31c612016-05-02 14:09:10 -050037 IMX6SX,
38 IMX6QP,
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070039 IMX7D,
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050040};
41
Sean Crossbb389192013-09-26 11:24:47 +080042struct imx6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053043 struct dw_pcie *pci;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030044 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050045 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010046 struct clk *pcie_bus;
47 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050048 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010049 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080050 struct regmap *iomuxc_gpr;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070051 struct reset_control *pciephy_reset;
52 struct reset_control *apps_reset;
Leonard Crestezf4e833b2018-07-19 17:02:10 +030053 struct reset_control *turnoff_reset;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050054 enum imx6_pcie_variants variant;
Justin Waters28e3abe2016-01-15 10:24:35 -050055 u32 tx_deemph_gen1;
56 u32 tx_deemph_gen2_3p5db;
57 u32 tx_deemph_gen2_6db;
58 u32 tx_swing_full;
59 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050060 int link_gen;
Quentin Schulzc26ebe92017-06-08 10:07:42 +020061 struct regulator *vpcie;
Sean Crossbb389192013-09-26 11:24:47 +080062};
63
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070064/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
65#define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000
66#define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50
67#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
68
Marek Vasutfa33a6d2013-12-12 22:50:02 +010069/* PCIe Root Complex registers (memory-mapped) */
Richard Zhu75cb8d22018-12-21 04:33:38 +000070#define PCIE_RC_IMX6_MSI_CAP 0x50
Marek Vasutfa33a6d2013-12-12 22:50:02 +010071#define PCIE_RC_LCR 0x7c
72#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
73#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
74#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
75
Bjorn Helgaas2393f792015-06-12 17:27:43 -050076#define PCIE_RC_LCSR 0x80
77
Sean Crossbb389192013-09-26 11:24:47 +080078/* PCIe Port Logic registers (memory-mapped) */
79#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020080#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
81#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
82#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080083#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
84#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010085#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
86#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080087
88#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
89#define PCIE_PHY_CTRL_DATA_LOC 0
90#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
91#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
92#define PCIE_PHY_CTRL_WR_LOC 18
93#define PCIE_PHY_CTRL_RD_LOC 19
94
95#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
96#define PCIE_PHY_STAT_ACK_LOC 16
97
Marek Vasutfa33a6d2013-12-12 22:50:02 +010098#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
99#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
100
Sean Crossbb389192013-09-26 11:24:47 +0800101/* PHY registers (not memory-mapped) */
Lucas Stachf18f42d2018-07-31 12:21:49 +0200102#define PCIE_PHY_ATEOVRD 0x10
103#define PCIE_PHY_ATEOVRD_EN (0x1 << 2)
104#define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
105#define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
106
107#define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
108#define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
109#define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
110#define PCIE_PHY_MPLL_MULTIPLIER_OVRD (0x1 << 9)
111
Sean Crossbb389192013-09-26 11:24:47 +0800112#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -0300113#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +0800114
115#define PHY_RX_OVRD_IN_LO 0x1005
116#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
117#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
118
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500119static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
Sean Crossbb389192013-09-26 11:24:47 +0800120{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530121 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800122 u32 val;
123 u32 max_iterations = 10;
124 u32 wait_counter = 0;
125
126 do {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530127 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800128 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
129 wait_counter++;
130
131 if (val == exp_val)
132 return 0;
133
134 udelay(1);
135 } while (wait_counter < max_iterations);
136
137 return -ETIMEDOUT;
138}
139
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500140static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800141{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530142 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800143 u32 val;
144 int ret;
145
146 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530147 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800148
149 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530150 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800151
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500152 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800153 if (ret)
154 return ret;
155
156 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530157 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800158
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500159 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800160}
161
162/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500163static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800164{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530165 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800166 u32 val, phy_ctl;
167 int ret;
168
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500169 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800170 if (ret)
171 return ret;
172
173 /* assert Read signal */
174 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530175 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800176
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500177 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800178 if (ret)
179 return ret;
180
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530181 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800182 *data = val & 0xffff;
183
184 /* deassert Read signal */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530185 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800186
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500187 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800188}
189
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500190static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
Sean Crossbb389192013-09-26 11:24:47 +0800191{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530192 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800193 u32 var;
194 int ret;
195
196 /* write addr */
197 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500198 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800199 if (ret)
200 return ret;
201
202 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530203 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800204
205 /* capture data */
206 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530207 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800208
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500209 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800210 if (ret)
211 return ret;
212
213 /* deassert cap data */
214 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530215 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800216
217 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500218 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800219 if (ret)
220 return ret;
221
222 /* assert wr signal */
223 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530224 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800225
226 /* wait for ack */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500227 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800228 if (ret)
229 return ret;
230
231 /* deassert wr signal */
232 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530233 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800234
235 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500236 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800237 if (ret)
238 return ret;
239
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530240 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800241
242 return 0;
243}
244
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500245static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100246{
247 u32 tmp;
248
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500249 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100250 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
251 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500252 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100253
254 usleep_range(2000, 3000);
255
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500256 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100257 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
258 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500259 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100260}
261
Sean Crossbb389192013-09-26 11:24:47 +0800262/* Added for PCI abort handling */
263static int imx6q_pcie_abort_handler(unsigned long addr,
264 unsigned int fsr, struct pt_regs *regs)
265{
Lucas Stach415b6182017-05-22 17:06:30 -0500266 unsigned long pc = instruction_pointer(regs);
267 unsigned long instr = *(unsigned long *)pc;
268 int reg = (instr >> 12) & 15;
269
270 /*
271 * If the instruction being executed was a read,
272 * make it look like it read all-ones.
273 */
274 if ((instr & 0x0c100000) == 0x04100000) {
275 unsigned long val;
276
277 if (instr & 0x00400000)
278 val = 255;
279 else
280 val = -1;
281
282 regs->uregs[reg] = val;
283 regs->ARM_pc += 4;
284 return 0;
285 }
286
287 if ((instr & 0x0e100090) == 0x00100090) {
288 regs->uregs[reg] = -1;
289 regs->ARM_pc += 4;
290 return 0;
291 }
292
293 return 1;
Sean Crossbb389192013-09-26 11:24:47 +0800294}
295
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500296static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800297{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200298 struct device *dev = imx6_pcie->pci->dev;
299
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500300 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700301 case IMX7D:
302 reset_control_assert(imx6_pcie->pciephy_reset);
303 reset_control_assert(imx6_pcie->apps_reset);
304 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500305 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500306 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
307 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
308 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
309 /* Force PCIe PHY reset */
310 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
311 IMX6SX_GPR5_PCIE_BTNRST_RESET,
312 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500313 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500314 case IMX6QP:
315 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
316 IMX6Q_GPR1_PCIE_SW_RST,
317 IMX6Q_GPR1_PCIE_SW_RST);
318 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500319 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500320 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
321 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
322 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
323 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
324 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500325 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200326
327 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
328 int ret = regulator_disable(imx6_pcie->vpcie);
329
330 if (ret)
331 dev_err(dev, "failed to disable vpcie regulator: %d\n",
332 ret);
333 }
Sean Crossbb389192013-09-26 11:24:47 +0800334}
335
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100336static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
337{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530338 struct dw_pcie *pci = imx6_pcie->pci;
339 struct device *dev = pci->dev;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500340 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500341
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500342 switch (imx6_pcie->variant) {
343 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500344 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
345 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500346 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500347 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500348 }
349
350 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
351 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500352 break;
Fabio Estevamc27fd682018-05-09 14:01:48 -0300353 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500354 case IMX6Q:
355 /* power up core phy and enable ref clock */
356 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
357 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
358 /*
359 * the async reset input need ref clock to sync internally,
360 * when the ref clock comes after reset, internal synced
361 * reset time is too short, cannot meet the requirement.
362 * add one ~10us delay here.
363 */
364 udelay(10);
365 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
366 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
367 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700368 case IMX7D:
369 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500370 }
371
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500372 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100373}
374
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700375static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
376{
377 u32 val;
378 unsigned int retries;
379 struct device *dev = imx6_pcie->pci->dev;
380
381 for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) {
382 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val);
383
384 if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED)
385 return;
386
387 usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN,
388 PHY_PLL_LOCK_WAIT_USLEEP_MAX);
389 }
390
391 dev_err(dev, "PCIe PLL lock timeout\n");
392}
393
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500394static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800395{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530396 struct dw_pcie *pci = imx6_pcie->pci;
397 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800398 int ret;
399
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200400 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
401 ret = regulator_enable(imx6_pcie->vpcie);
402 if (ret) {
403 dev_err(dev, "failed to enable vpcie regulator: %d\n",
404 ret);
405 return;
406 }
407 }
408
Lucas Stach57526132014-03-28 17:52:55 +0100409 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800410 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500411 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200412 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800413 }
414
Lucas Stach57526132014-03-28 17:52:55 +0100415 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800416 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500417 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100418 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800419 }
420
Lucas Stach57526132014-03-28 17:52:55 +0100421 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800422 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500423 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100424 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800425 }
426
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100427 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
428 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500429 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100430 goto err_ref_clk;
431 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700432
Richard Zhua2fa6f62014-10-27 13:17:32 +0800433 /* allow the clocks to stabilize */
434 usleep_range(200, 500);
435
Richard Zhubc9ef772013-12-12 22:50:03 +0100436 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300437 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500438 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
439 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100440 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500441 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
442 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100443 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500444
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500445 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700446 case IMX7D:
447 reset_control_deassert(imx6_pcie->pciephy_reset);
448 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
449 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500450 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500451 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
452 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500453 break;
454 case IMX6QP:
455 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
456 IMX6Q_GPR1_PCIE_SW_RST, 0);
457
458 usleep_range(200, 500);
459 break;
460 case IMX6Q: /* Nothing to do */
461 break;
462 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500463
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500464 return;
Sean Crossbb389192013-09-26 11:24:47 +0800465
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100466err_ref_clk:
467 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100468err_pcie:
469 clk_disable_unprepare(imx6_pcie->pcie_bus);
470err_pcie_bus:
471 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200472err_pcie_phy:
473 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
474 ret = regulator_disable(imx6_pcie->vpcie);
475 if (ret)
476 dev_err(dev, "failed to disable vpcie regulator: %d\n",
477 ret);
478 }
Sean Crossbb389192013-09-26 11:24:47 +0800479}
480
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500481static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800482{
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700483 switch (imx6_pcie->variant) {
484 case IMX7D:
485 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
486 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
487 break;
488 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500489 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
490 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
491 IMX6SX_GPR12_PCIE_RX_EQ_2);
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700492 /* FALLTHROUGH */
493 default:
494 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
495 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500496
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700497 /* configure constant input signal to the pcie ctrl and phy */
498 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
499 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800500
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700501 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
502 IMX6Q_GPR8_TX_DEEMPH_GEN1,
503 imx6_pcie->tx_deemph_gen1 << 0);
504 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
505 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
506 imx6_pcie->tx_deemph_gen2_3p5db << 6);
507 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
508 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
509 imx6_pcie->tx_deemph_gen2_6db << 12);
510 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
511 IMX6Q_GPR8_TX_SWING_FULL,
512 imx6_pcie->tx_swing_full << 18);
513 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
514 IMX6Q_GPR8_TX_SWING_LOW,
515 imx6_pcie->tx_swing_low << 25);
516 break;
517 }
518
Sean Crossbb389192013-09-26 11:24:47 +0800519 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
520 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800521}
522
Lucas Stachf18f42d2018-07-31 12:21:49 +0200523static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
524{
525 unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
526 int mult, div;
527 u32 val;
528
529 switch (phy_rate) {
530 case 125000000:
531 /*
532 * The default settings of the MPLL are for a 125MHz input
533 * clock, so no need to reconfigure anything in that case.
534 */
535 return 0;
536 case 100000000:
537 mult = 25;
538 div = 0;
539 break;
540 case 200000000:
541 mult = 25;
542 div = 1;
543 break;
544 default:
545 dev_err(imx6_pcie->pci->dev,
546 "Unsupported PHY reference clock rate %lu\n", phy_rate);
547 return -EINVAL;
548 }
549
550 pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
551 val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
552 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
553 val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
554 val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
555 pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
556
557 pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
558 val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
559 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
560 val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
561 val |= PCIE_PHY_ATEOVRD_EN;
562 pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
563
564 return 0;
565}
566
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500567static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
Marek Vasut66a60f92013-12-12 22:50:01 +0100568{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530569 struct dw_pcie *pci = imx6_pcie->pci;
570 struct device *dev = pci->dev;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500571
Joao Pinto886bc5c2016-03-10 14:44:35 -0600572 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530573 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600574 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100575
Bjorn Helgaas13957652016-10-06 13:35:18 -0500576 dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530577 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
578 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600579 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100580}
581
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500582static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500583{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530584 struct dw_pcie *pci = imx6_pcie->pci;
585 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500586 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500587 unsigned int retries;
588
589 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530590 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500591 /* Test if the speed change finished. */
592 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
593 return 0;
594 usleep_range(100, 1000);
595 }
596
Bjorn Helgaas13957652016-10-06 13:35:18 -0500597 dev_err(dev, "Speed change timeout\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500598 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800599}
600
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300601static void imx6_pcie_ltssm_enable(struct device *dev)
602{
603 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
604
605 switch (imx6_pcie->variant) {
606 case IMX6Q:
607 case IMX6SX:
608 case IMX6QP:
609 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
610 IMX6Q_GPR12_PCIE_CTL_2,
611 IMX6Q_GPR12_PCIE_CTL_2);
612 break;
613 case IMX7D:
614 reset_control_deassert(imx6_pcie->apps_reset);
615 break;
616 }
617}
618
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500619static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100620{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530621 struct dw_pcie *pci = imx6_pcie->pci;
622 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500623 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500624 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100625
626 /*
627 * Force Gen1 operation when starting the link. In case the link is
628 * started in Gen2 mode, there is a possibility the devices on the
629 * bus will not be detected at all. This happens with PCIe switches.
630 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530631 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100632 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
633 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530634 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100635
636 /* Start LTSSM. */
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300637 imx6_pcie_ltssm_enable(dev);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100638
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500639 ret = imx6_pcie_wait_for_link(imx6_pcie);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200640 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600641 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100642
Tim Harveya5fcec42016-04-19 19:52:44 -0500643 if (imx6_pcie->link_gen == 2) {
644 /* Allow Gen2 mode after the link is up. */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530645 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500646 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
647 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530648 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100649
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700650 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700651 * Start Directed Speed Change so the best possible
652 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700653 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700654 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
655 tmp |= PORT_LOGIC_SPEED_CHANGE;
656 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700657
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700658 if (imx6_pcie->variant != IMX7D) {
659 /*
660 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
661 * from i.MX6 family when no link speed transition
662 * occurs and we go Gen1 -> yep, Gen1. The difference
663 * is that, in such case, it will not be cleared by HW
664 * which will cause the following code to report false
665 * failure.
666 */
667
668 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
669 if (ret) {
670 dev_err(dev, "Failed to bring link up!\n");
671 goto err_reset_phy;
672 }
673 }
674
675 /* Make sure link training is finished as well! */
676 ret = imx6_pcie_wait_for_link(imx6_pcie);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700677 if (ret) {
678 dev_err(dev, "Failed to bring link up!\n");
679 goto err_reset_phy;
680 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700681 } else {
682 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100683 }
684
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530685 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500686 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500687 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600688
689err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500690 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530691 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
692 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500693 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600694 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100695}
696
Bjorn Andersson4a301762017-07-15 23:39:45 -0700697static int imx6_pcie_host_init(struct pcie_port *pp)
Sean Crossbb389192013-09-26 11:24:47 +0800698{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530699 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
700 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800701
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500702 imx6_pcie_assert_core_reset(imx6_pcie);
703 imx6_pcie_init_phy(imx6_pcie);
704 imx6_pcie_deassert_core_reset(imx6_pcie);
Lucas Stachf18f42d2018-07-31 12:21:49 +0200705 imx6_setup_phy_mpll(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800706 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500707 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100708
709 if (IS_ENABLED(CONFIG_PCI_MSI))
710 dw_pcie_msi_init(pp);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700711
712 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800713}
714
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530715static int imx6_pcie_link_up(struct dw_pcie *pci)
Sean Crossbb389192013-09-26 11:24:47 +0800716{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530717 return dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1) &
Lucas Stach4d107d32016-01-25 16:50:02 -0600718 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800719}
720
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800721static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800722 .host_init = imx6_pcie_host_init,
723};
724
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700725static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
726 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800727{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530728 struct dw_pcie *pci = imx6_pcie->pci;
729 struct pcie_port *pp = &pci->pp;
730 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800731 int ret;
732
Lucas Stachd1dc9742014-03-28 17:52:59 +0100733 if (IS_ENABLED(CONFIG_PCI_MSI)) {
734 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
735 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500736 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100737 return -ENODEV;
738 }
Lucas Stachd1dc9742014-03-28 17:52:59 +0100739 }
740
Sean Crossbb389192013-09-26 11:24:47 +0800741 pp->ops = &imx6_pcie_host_ops;
742
Sean Crossbb389192013-09-26 11:24:47 +0800743 ret = dw_pcie_host_init(pp);
744 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500745 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800746 return ret;
747 }
748
749 return 0;
750}
751
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530752static const struct dw_pcie_ops dw_pcie_ops = {
753 .link_up = imx6_pcie_link_up,
754};
755
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300756#ifdef CONFIG_PM_SLEEP
757static void imx6_pcie_ltssm_disable(struct device *dev)
758{
759 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
760
761 switch (imx6_pcie->variant) {
762 case IMX6SX:
763 case IMX6QP:
764 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
765 IMX6Q_GPR12_PCIE_CTL_2, 0);
766 break;
767 case IMX7D:
768 reset_control_assert(imx6_pcie->apps_reset);
769 break;
770 default:
771 dev_err(dev, "ltssm_disable not supported\n");
772 }
773}
774
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300775static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
776{
777 reset_control_assert(imx6_pcie->turnoff_reset);
778 reset_control_deassert(imx6_pcie->turnoff_reset);
779
780 /*
781 * Components with an upstream port must respond to
782 * PME_Turn_Off with PME_TO_Ack but we can't check.
783 *
784 * The standard recommends a 1-10ms timeout after which to
785 * proceed anyway as if acks were received.
786 */
787 usleep_range(1000, 10000);
788}
789
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300790static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
791{
792 clk_disable_unprepare(imx6_pcie->pcie);
793 clk_disable_unprepare(imx6_pcie->pcie_phy);
794 clk_disable_unprepare(imx6_pcie->pcie_bus);
795
796 if (imx6_pcie->variant == IMX7D) {
797 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
798 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
799 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
800 }
801}
802
803static int imx6_pcie_suspend_noirq(struct device *dev)
804{
805 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
806
807 if (imx6_pcie->variant != IMX7D)
808 return 0;
809
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300810 imx6_pcie_pm_turnoff(imx6_pcie);
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300811 imx6_pcie_clk_disable(imx6_pcie);
812 imx6_pcie_ltssm_disable(dev);
813
814 return 0;
815}
816
817static int imx6_pcie_resume_noirq(struct device *dev)
818{
819 int ret;
820 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
821 struct pcie_port *pp = &imx6_pcie->pci->pp;
822
823 if (imx6_pcie->variant != IMX7D)
824 return 0;
825
826 imx6_pcie_assert_core_reset(imx6_pcie);
827 imx6_pcie_init_phy(imx6_pcie);
828 imx6_pcie_deassert_core_reset(imx6_pcie);
829 dw_pcie_setup_rc(pp);
830
831 ret = imx6_pcie_establish_link(imx6_pcie);
832 if (ret < 0)
833 dev_info(dev, "pcie link is down after resume.\n");
834
835 return 0;
836}
837#endif
838
839static const struct dev_pm_ops imx6_pcie_pm_ops = {
840 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
841 imx6_pcie_resume_noirq)
842};
843
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700844static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800845{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500846 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530847 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +0800848 struct imx6_pcie *imx6_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800849 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500850 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800851 int ret;
Richard Zhu75cb8d22018-12-21 04:33:38 +0000852 u16 val;
Sean Crossbb389192013-09-26 11:24:47 +0800853
Bjorn Helgaas13957652016-10-06 13:35:18 -0500854 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800855 if (!imx6_pcie)
856 return -ENOMEM;
857
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530858 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
859 if (!pci)
860 return -ENOMEM;
861
862 pci->dev = dev;
863 pci->ops = &dw_pcie_ops;
Sean Crossbb389192013-09-26 11:24:47 +0800864
Guenter Roeckc0464062017-02-25 02:08:12 -0800865 imx6_pcie->pci = pci;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500866 imx6_pcie->variant =
Bjorn Helgaas13957652016-10-06 13:35:18 -0500867 (enum imx6_pcie_variants)of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500868
Sean Crossbb389192013-09-26 11:24:47 +0800869 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530870 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
871 if (IS_ERR(pci->dbi_base))
872 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800873
874 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500875 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
876 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500877 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300878 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500879 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500880 imx6_pcie->gpio_active_high ?
881 GPIOF_OUT_INIT_HIGH :
882 GPIOF_OUT_INIT_LOW,
883 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300884 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500885 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300886 return ret;
887 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700888 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
889 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300890 }
Sean Crossbb389192013-09-26 11:24:47 +0800891
Sean Crossbb389192013-09-26 11:24:47 +0800892 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -0500893 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +0100894 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500895 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100896 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800897 }
898
Bjorn Helgaas13957652016-10-06 13:35:18 -0500899 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +0100900 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500901 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100902 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800903 }
904
Bjorn Helgaas13957652016-10-06 13:35:18 -0500905 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +0100906 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500907 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100908 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800909 }
910
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700911 switch (imx6_pcie->variant) {
912 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500913 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500914 "pcie_inbound_axi");
915 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Andrey Smirnov21b72452017-02-07 07:50:25 -0800916 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500917 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
918 }
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700919 break;
920 case IMX7D:
Philipp Zabel7c180582017-07-19 17:25:56 +0200921 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
922 "pciephy");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700923 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100924 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700925 return PTR_ERR(imx6_pcie->pciephy_reset);
926 }
927
Philipp Zabel7c180582017-07-19 17:25:56 +0200928 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
929 "apps");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700930 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100931 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700932 return PTR_ERR(imx6_pcie->apps_reset);
933 }
934 break;
935 default:
936 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500937 }
938
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300939 /* Grab turnoff reset */
940 imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
941 if (IS_ERR(imx6_pcie->turnoff_reset)) {
942 dev_err(dev, "Failed to get TURNOFF reset control\n");
943 return PTR_ERR(imx6_pcie->turnoff_reset);
944 }
945
Sean Crossbb389192013-09-26 11:24:47 +0800946 /* Grab GPR config register range */
947 imx6_pcie->iomuxc_gpr =
948 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
949 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500950 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200951 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800952 }
953
Justin Waters28e3abe2016-01-15 10:24:35 -0500954 /* Grab PCIe PHY Tx Settings */
955 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
956 &imx6_pcie->tx_deemph_gen1))
957 imx6_pcie->tx_deemph_gen1 = 0;
958
959 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
960 &imx6_pcie->tx_deemph_gen2_3p5db))
961 imx6_pcie->tx_deemph_gen2_3p5db = 0;
962
963 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
964 &imx6_pcie->tx_deemph_gen2_6db))
965 imx6_pcie->tx_deemph_gen2_6db = 20;
966
967 if (of_property_read_u32(node, "fsl,tx-swing-full",
968 &imx6_pcie->tx_swing_full))
969 imx6_pcie->tx_swing_full = 127;
970
971 if (of_property_read_u32(node, "fsl,tx-swing-low",
972 &imx6_pcie->tx_swing_low))
973 imx6_pcie->tx_swing_low = 127;
974
Tim Harveya5fcec42016-04-19 19:52:44 -0500975 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500976 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -0500977 &imx6_pcie->link_gen);
978 if (ret)
979 imx6_pcie->link_gen = 1;
980
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200981 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
982 if (IS_ERR(imx6_pcie->vpcie)) {
983 if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
984 return -EPROBE_DEFER;
985 imx6_pcie->vpcie = NULL;
986 }
987
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530988 platform_set_drvdata(pdev, imx6_pcie);
989
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500990 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +0800991 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200992 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800993
Richard Zhu75cb8d22018-12-21 04:33:38 +0000994 if (pci_msi_enabled()) {
995 val = dw_pcie_readw_dbi(pci, PCIE_RC_IMX6_MSI_CAP +
996 PCI_MSI_FLAGS);
997 val |= PCI_MSI_FLAGS_ENABLE;
998 dw_pcie_writew_dbi(pci, PCIE_RC_IMX6_MSI_CAP + PCI_MSI_FLAGS,
999 val);
1000 }
1001
Sean Crossbb389192013-09-26 11:24:47 +08001002 return 0;
Sean Crossbb389192013-09-26 11:24:47 +08001003}
1004
Lucas Stach3e3e4062014-07-31 20:16:05 +02001005static void imx6_pcie_shutdown(struct platform_device *pdev)
1006{
1007 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1008
1009 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -05001010 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +02001011}
1012
Sean Crossbb389192013-09-26 11:24:47 +08001013static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -05001014 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
1015 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -05001016 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001017 { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, },
Sean Crossbb389192013-09-26 11:24:47 +08001018 {},
1019};
Sean Crossbb389192013-09-26 11:24:47 +08001020
1021static struct platform_driver imx6_pcie_driver = {
1022 .driver = {
1023 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +05301024 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -05001025 .suppress_bind_attrs = true,
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +03001026 .pm = &imx6_pcie_pm_ops,
Sean Crossbb389192013-09-26 11:24:47 +08001027 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001028 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +02001029 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +08001030};
1031
Sean Crossbb389192013-09-26 11:24:47 +08001032static int __init imx6_pcie_init(void)
1033{
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001034 /*
1035 * Since probe() can be deferred we need to make sure that
1036 * hook_fault_code is not called after __init memory is freed
1037 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1038 * we can install the handler here without risking it
1039 * accessing some uninitialized driver state.
1040 */
Lucas Stach415b6182017-05-22 17:06:30 -05001041 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1042 "external abort on non-linefetch");
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001043
1044 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +08001045}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -04001046device_initcall(imx6_pcie_init);