blob: 21e03c6567da9d710c5966a770b8263b1a7cea03 [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;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050053 enum imx6_pcie_variants variant;
Justin Waters28e3abe2016-01-15 10:24:35 -050054 u32 tx_deemph_gen1;
55 u32 tx_deemph_gen2_3p5db;
56 u32 tx_deemph_gen2_6db;
57 u32 tx_swing_full;
58 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050059 int link_gen;
Quentin Schulzc26ebe92017-06-08 10:07:42 +020060 struct regulator *vpcie;
Sean Crossbb389192013-09-26 11:24:47 +080061};
62
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070063/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
64#define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000
65#define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50
66#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
67
Marek Vasutfa33a6d2013-12-12 22:50:02 +010068/* PCIe Root Complex registers (memory-mapped) */
69#define PCIE_RC_LCR 0x7c
70#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
71#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
72#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
73
Bjorn Helgaas2393f792015-06-12 17:27:43 -050074#define PCIE_RC_LCSR 0x80
75
Sean Crossbb389192013-09-26 11:24:47 +080076/* PCIe Port Logic registers (memory-mapped) */
77#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020078#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
79#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
80#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080081#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
82#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010083#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
84#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080085
86#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
87#define PCIE_PHY_CTRL_DATA_LOC 0
88#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
89#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
90#define PCIE_PHY_CTRL_WR_LOC 18
91#define PCIE_PHY_CTRL_RD_LOC 19
92
93#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
94#define PCIE_PHY_STAT_ACK_LOC 16
95
Marek Vasutfa33a6d2013-12-12 22:50:02 +010096#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
97#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
98
Sean Crossbb389192013-09-26 11:24:47 +080099/* PHY registers (not memory-mapped) */
Lucas Stachf18f42d2018-07-31 12:21:49 +0200100#define PCIE_PHY_ATEOVRD 0x10
101#define PCIE_PHY_ATEOVRD_EN (0x1 << 2)
102#define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
103#define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
104
105#define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
106#define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
107#define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
108#define PCIE_PHY_MPLL_MULTIPLIER_OVRD (0x1 << 9)
109
Sean Crossbb389192013-09-26 11:24:47 +0800110#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -0300111#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +0800112
113#define PHY_RX_OVRD_IN_LO 0x1005
114#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
115#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
116
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500117static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
Sean Crossbb389192013-09-26 11:24:47 +0800118{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530119 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800120 u32 val;
121 u32 max_iterations = 10;
122 u32 wait_counter = 0;
123
124 do {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530125 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800126 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
127 wait_counter++;
128
129 if (val == exp_val)
130 return 0;
131
132 udelay(1);
133 } while (wait_counter < max_iterations);
134
135 return -ETIMEDOUT;
136}
137
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500138static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800139{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530140 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800141 u32 val;
142 int ret;
143
144 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530145 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800146
147 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530148 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800149
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500150 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800151 if (ret)
152 return ret;
153
154 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530155 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800156
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500157 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800158}
159
160/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500161static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800162{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530163 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800164 u32 val, phy_ctl;
165 int ret;
166
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500167 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800168 if (ret)
169 return ret;
170
171 /* assert Read signal */
172 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530173 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800174
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500175 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800176 if (ret)
177 return ret;
178
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530179 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800180 *data = val & 0xffff;
181
182 /* deassert Read signal */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530183 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800184
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500185 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800186}
187
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500188static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int 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;
Sean Crossbb389192013-09-26 11:24:47 +0800191 u32 var;
192 int ret;
193
194 /* write addr */
195 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500196 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800197 if (ret)
198 return ret;
199
200 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530201 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800202
203 /* capture data */
204 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530205 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800206
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500207 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800208 if (ret)
209 return ret;
210
211 /* deassert cap data */
212 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530213 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800214
215 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500216 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800217 if (ret)
218 return ret;
219
220 /* assert wr signal */
221 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530222 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800223
224 /* wait for ack */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500225 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800226 if (ret)
227 return ret;
228
229 /* deassert wr signal */
230 var = data << PCIE_PHY_CTRL_DATA_LOC;
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
233 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500234 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800235 if (ret)
236 return ret;
237
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530238 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800239
240 return 0;
241}
242
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500243static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100244{
245 u32 tmp;
246
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500247 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100248 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
249 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500250 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100251
252 usleep_range(2000, 3000);
253
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500254 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100255 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
256 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500257 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100258}
259
Sean Crossbb389192013-09-26 11:24:47 +0800260/* Added for PCI abort handling */
261static int imx6q_pcie_abort_handler(unsigned long addr,
262 unsigned int fsr, struct pt_regs *regs)
263{
Lucas Stach415b6182017-05-22 17:06:30 -0500264 unsigned long pc = instruction_pointer(regs);
265 unsigned long instr = *(unsigned long *)pc;
266 int reg = (instr >> 12) & 15;
267
268 /*
269 * If the instruction being executed was a read,
270 * make it look like it read all-ones.
271 */
272 if ((instr & 0x0c100000) == 0x04100000) {
273 unsigned long val;
274
275 if (instr & 0x00400000)
276 val = 255;
277 else
278 val = -1;
279
280 regs->uregs[reg] = val;
281 regs->ARM_pc += 4;
282 return 0;
283 }
284
285 if ((instr & 0x0e100090) == 0x00100090) {
286 regs->uregs[reg] = -1;
287 regs->ARM_pc += 4;
288 return 0;
289 }
290
291 return 1;
Sean Crossbb389192013-09-26 11:24:47 +0800292}
293
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500294static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800295{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200296 struct device *dev = imx6_pcie->pci->dev;
297
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500298 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700299 case IMX7D:
300 reset_control_assert(imx6_pcie->pciephy_reset);
301 reset_control_assert(imx6_pcie->apps_reset);
302 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500303 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500304 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
305 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
306 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
307 /* Force PCIe PHY reset */
308 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
309 IMX6SX_GPR5_PCIE_BTNRST_RESET,
310 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500311 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500312 case IMX6QP:
313 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
314 IMX6Q_GPR1_PCIE_SW_RST,
315 IMX6Q_GPR1_PCIE_SW_RST);
316 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500317 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500318 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
319 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
320 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
321 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
322 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500323 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200324
325 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
326 int ret = regulator_disable(imx6_pcie->vpcie);
327
328 if (ret)
329 dev_err(dev, "failed to disable vpcie regulator: %d\n",
330 ret);
331 }
Sean Crossbb389192013-09-26 11:24:47 +0800332}
333
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100334static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
335{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530336 struct dw_pcie *pci = imx6_pcie->pci;
337 struct device *dev = pci->dev;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500338 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500339
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500340 switch (imx6_pcie->variant) {
341 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500342 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
343 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500344 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500345 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500346 }
347
348 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
349 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500350 break;
Fabio Estevamc27fd682018-05-09 14:01:48 -0300351 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500352 case IMX6Q:
353 /* power up core phy and enable ref clock */
354 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
355 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
356 /*
357 * the async reset input need ref clock to sync internally,
358 * when the ref clock comes after reset, internal synced
359 * reset time is too short, cannot meet the requirement.
360 * add one ~10us delay here.
361 */
362 udelay(10);
363 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
364 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
365 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700366 case IMX7D:
367 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500368 }
369
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500370 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100371}
372
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700373static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
374{
375 u32 val;
376 unsigned int retries;
377 struct device *dev = imx6_pcie->pci->dev;
378
379 for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) {
380 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val);
381
382 if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED)
383 return;
384
385 usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN,
386 PHY_PLL_LOCK_WAIT_USLEEP_MAX);
387 }
388
389 dev_err(dev, "PCIe PLL lock timeout\n");
390}
391
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500392static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800393{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530394 struct dw_pcie *pci = imx6_pcie->pci;
395 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800396 int ret;
397
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200398 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
399 ret = regulator_enable(imx6_pcie->vpcie);
400 if (ret) {
401 dev_err(dev, "failed to enable vpcie regulator: %d\n",
402 ret);
403 return;
404 }
405 }
406
Lucas Stach57526132014-03-28 17:52:55 +0100407 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800408 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500409 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200410 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800411 }
412
Lucas Stach57526132014-03-28 17:52:55 +0100413 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800414 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500415 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100416 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800417 }
418
Lucas Stach57526132014-03-28 17:52:55 +0100419 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800420 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500421 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100422 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800423 }
424
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100425 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
426 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500427 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100428 goto err_ref_clk;
429 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700430
Richard Zhua2fa6f62014-10-27 13:17:32 +0800431 /* allow the clocks to stabilize */
432 usleep_range(200, 500);
433
Richard Zhubc9ef772013-12-12 22:50:03 +0100434 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300435 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500436 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
437 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100438 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500439 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
440 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100441 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500442
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500443 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700444 case IMX7D:
445 reset_control_deassert(imx6_pcie->pciephy_reset);
446 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
447 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500448 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500449 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
450 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500451 break;
452 case IMX6QP:
453 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
454 IMX6Q_GPR1_PCIE_SW_RST, 0);
455
456 usleep_range(200, 500);
457 break;
458 case IMX6Q: /* Nothing to do */
459 break;
460 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500461
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500462 return;
Sean Crossbb389192013-09-26 11:24:47 +0800463
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100464err_ref_clk:
465 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100466err_pcie:
467 clk_disable_unprepare(imx6_pcie->pcie_bus);
468err_pcie_bus:
469 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200470err_pcie_phy:
471 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
472 ret = regulator_disable(imx6_pcie->vpcie);
473 if (ret)
474 dev_err(dev, "failed to disable vpcie regulator: %d\n",
475 ret);
476 }
Sean Crossbb389192013-09-26 11:24:47 +0800477}
478
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500479static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800480{
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700481 switch (imx6_pcie->variant) {
482 case IMX7D:
483 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
484 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
485 break;
486 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500487 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
488 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
489 IMX6SX_GPR12_PCIE_RX_EQ_2);
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700490 /* FALLTHROUGH */
491 default:
492 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
493 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500494
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700495 /* configure constant input signal to the pcie ctrl and phy */
496 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
497 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800498
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700499 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
500 IMX6Q_GPR8_TX_DEEMPH_GEN1,
501 imx6_pcie->tx_deemph_gen1 << 0);
502 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
503 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
504 imx6_pcie->tx_deemph_gen2_3p5db << 6);
505 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
506 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
507 imx6_pcie->tx_deemph_gen2_6db << 12);
508 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
509 IMX6Q_GPR8_TX_SWING_FULL,
510 imx6_pcie->tx_swing_full << 18);
511 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
512 IMX6Q_GPR8_TX_SWING_LOW,
513 imx6_pcie->tx_swing_low << 25);
514 break;
515 }
516
Sean Crossbb389192013-09-26 11:24:47 +0800517 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
518 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800519}
520
Lucas Stachf18f42d2018-07-31 12:21:49 +0200521static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
522{
523 unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
524 int mult, div;
525 u32 val;
526
527 switch (phy_rate) {
528 case 125000000:
529 /*
530 * The default settings of the MPLL are for a 125MHz input
531 * clock, so no need to reconfigure anything in that case.
532 */
533 return 0;
534 case 100000000:
535 mult = 25;
536 div = 0;
537 break;
538 case 200000000:
539 mult = 25;
540 div = 1;
541 break;
542 default:
543 dev_err(imx6_pcie->pci->dev,
544 "Unsupported PHY reference clock rate %lu\n", phy_rate);
545 return -EINVAL;
546 }
547
548 pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
549 val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
550 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
551 val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
552 val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
553 pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
554
555 pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
556 val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
557 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
558 val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
559 val |= PCIE_PHY_ATEOVRD_EN;
560 pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
561
562 return 0;
563}
564
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500565static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
Marek Vasut66a60f92013-12-12 22:50:01 +0100566{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530567 struct dw_pcie *pci = imx6_pcie->pci;
568 struct device *dev = pci->dev;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500569
Joao Pinto886bc5c2016-03-10 14:44:35 -0600570 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530571 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600572 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100573
Bjorn Helgaas13957652016-10-06 13:35:18 -0500574 dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530575 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
576 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600577 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100578}
579
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500580static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500581{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530582 struct dw_pcie *pci = imx6_pcie->pci;
583 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500584 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500585 unsigned int retries;
586
587 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530588 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500589 /* Test if the speed change finished. */
590 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
591 return 0;
592 usleep_range(100, 1000);
593 }
594
Bjorn Helgaas13957652016-10-06 13:35:18 -0500595 dev_err(dev, "Speed change timeout\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500596 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800597}
598
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500599static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100600{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530601 struct dw_pcie *pci = imx6_pcie->pci;
602 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500603 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500604 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100605
606 /*
607 * Force Gen1 operation when starting the link. In case the link is
608 * started in Gen2 mode, there is a possibility the devices on the
609 * bus will not be detected at all. This happens with PCIe switches.
610 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530611 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100612 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
613 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530614 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100615
616 /* Start LTSSM. */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700617 if (imx6_pcie->variant == IMX7D)
618 reset_control_deassert(imx6_pcie->apps_reset);
619 else
620 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
621 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100622
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500623 ret = imx6_pcie_wait_for_link(imx6_pcie);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200624 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600625 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100626
Tim Harveya5fcec42016-04-19 19:52:44 -0500627 if (imx6_pcie->link_gen == 2) {
628 /* Allow Gen2 mode after the link is up. */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530629 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500630 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
631 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530632 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100633
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700634 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700635 * Start Directed Speed Change so the best possible
636 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700637 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700638 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
639 tmp |= PORT_LOGIC_SPEED_CHANGE;
640 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700641
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700642 if (imx6_pcie->variant != IMX7D) {
643 /*
644 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
645 * from i.MX6 family when no link speed transition
646 * occurs and we go Gen1 -> yep, Gen1. The difference
647 * is that, in such case, it will not be cleared by HW
648 * which will cause the following code to report false
649 * failure.
650 */
651
652 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
653 if (ret) {
654 dev_err(dev, "Failed to bring link up!\n");
655 goto err_reset_phy;
656 }
657 }
658
659 /* Make sure link training is finished as well! */
660 ret = imx6_pcie_wait_for_link(imx6_pcie);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700661 if (ret) {
662 dev_err(dev, "Failed to bring link up!\n");
663 goto err_reset_phy;
664 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700665 } else {
666 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100667 }
668
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530669 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500670 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500671 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600672
673err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500674 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530675 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
676 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500677 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600678 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100679}
680
Bjorn Andersson4a301762017-07-15 23:39:45 -0700681static int imx6_pcie_host_init(struct pcie_port *pp)
Sean Crossbb389192013-09-26 11:24:47 +0800682{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530683 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
684 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800685
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500686 imx6_pcie_assert_core_reset(imx6_pcie);
687 imx6_pcie_init_phy(imx6_pcie);
688 imx6_pcie_deassert_core_reset(imx6_pcie);
Lucas Stachf18f42d2018-07-31 12:21:49 +0200689 imx6_setup_phy_mpll(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800690 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500691 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100692
693 if (IS_ENABLED(CONFIG_PCI_MSI))
694 dw_pcie_msi_init(pp);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700695
696 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800697}
698
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530699static int imx6_pcie_link_up(struct dw_pcie *pci)
Sean Crossbb389192013-09-26 11:24:47 +0800700{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530701 return dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1) &
Lucas Stach4d107d32016-01-25 16:50:02 -0600702 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800703}
704
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800705static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800706 .host_init = imx6_pcie_host_init,
707};
708
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700709static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
710 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800711{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530712 struct dw_pcie *pci = imx6_pcie->pci;
713 struct pcie_port *pp = &pci->pp;
714 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800715 int ret;
716
Lucas Stachd1dc9742014-03-28 17:52:59 +0100717 if (IS_ENABLED(CONFIG_PCI_MSI)) {
718 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
719 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500720 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100721 return -ENODEV;
722 }
Lucas Stachd1dc9742014-03-28 17:52:59 +0100723 }
724
Sean Crossbb389192013-09-26 11:24:47 +0800725 pp->ops = &imx6_pcie_host_ops;
726
Sean Crossbb389192013-09-26 11:24:47 +0800727 ret = dw_pcie_host_init(pp);
728 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500729 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800730 return ret;
731 }
732
733 return 0;
734}
735
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530736static const struct dw_pcie_ops dw_pcie_ops = {
737 .link_up = imx6_pcie_link_up,
738};
739
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700740static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800741{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500742 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530743 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +0800744 struct imx6_pcie *imx6_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800745 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500746 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800747 int ret;
748
Bjorn Helgaas13957652016-10-06 13:35:18 -0500749 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800750 if (!imx6_pcie)
751 return -ENOMEM;
752
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530753 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
754 if (!pci)
755 return -ENOMEM;
756
757 pci->dev = dev;
758 pci->ops = &dw_pcie_ops;
Sean Crossbb389192013-09-26 11:24:47 +0800759
Guenter Roeckc0464062017-02-25 02:08:12 -0800760 imx6_pcie->pci = pci;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500761 imx6_pcie->variant =
Bjorn Helgaas13957652016-10-06 13:35:18 -0500762 (enum imx6_pcie_variants)of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500763
Sean Crossbb389192013-09-26 11:24:47 +0800764 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530765 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
766 if (IS_ERR(pci->dbi_base))
767 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800768
769 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500770 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
771 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500772 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300773 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500774 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500775 imx6_pcie->gpio_active_high ?
776 GPIOF_OUT_INIT_HIGH :
777 GPIOF_OUT_INIT_LOW,
778 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300779 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500780 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300781 return ret;
782 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700783 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
784 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300785 }
Sean Crossbb389192013-09-26 11:24:47 +0800786
Sean Crossbb389192013-09-26 11:24:47 +0800787 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -0500788 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +0100789 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500790 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100791 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800792 }
793
Bjorn Helgaas13957652016-10-06 13:35:18 -0500794 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +0100795 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500796 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100797 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800798 }
799
Bjorn Helgaas13957652016-10-06 13:35:18 -0500800 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +0100801 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500802 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100803 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800804 }
805
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700806 switch (imx6_pcie->variant) {
807 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500808 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500809 "pcie_inbound_axi");
810 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Andrey Smirnov21b72452017-02-07 07:50:25 -0800811 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500812 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
813 }
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700814 break;
815 case IMX7D:
Philipp Zabel7c180582017-07-19 17:25:56 +0200816 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
817 "pciephy");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700818 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100819 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700820 return PTR_ERR(imx6_pcie->pciephy_reset);
821 }
822
Philipp Zabel7c180582017-07-19 17:25:56 +0200823 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
824 "apps");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700825 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100826 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700827 return PTR_ERR(imx6_pcie->apps_reset);
828 }
829 break;
830 default:
831 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500832 }
833
Sean Crossbb389192013-09-26 11:24:47 +0800834 /* Grab GPR config register range */
835 imx6_pcie->iomuxc_gpr =
836 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
837 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500838 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200839 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800840 }
841
Justin Waters28e3abe2016-01-15 10:24:35 -0500842 /* Grab PCIe PHY Tx Settings */
843 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
844 &imx6_pcie->tx_deemph_gen1))
845 imx6_pcie->tx_deemph_gen1 = 0;
846
847 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
848 &imx6_pcie->tx_deemph_gen2_3p5db))
849 imx6_pcie->tx_deemph_gen2_3p5db = 0;
850
851 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
852 &imx6_pcie->tx_deemph_gen2_6db))
853 imx6_pcie->tx_deemph_gen2_6db = 20;
854
855 if (of_property_read_u32(node, "fsl,tx-swing-full",
856 &imx6_pcie->tx_swing_full))
857 imx6_pcie->tx_swing_full = 127;
858
859 if (of_property_read_u32(node, "fsl,tx-swing-low",
860 &imx6_pcie->tx_swing_low))
861 imx6_pcie->tx_swing_low = 127;
862
Tim Harveya5fcec42016-04-19 19:52:44 -0500863 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500864 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -0500865 &imx6_pcie->link_gen);
866 if (ret)
867 imx6_pcie->link_gen = 1;
868
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200869 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
870 if (IS_ERR(imx6_pcie->vpcie)) {
871 if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
872 return -EPROBE_DEFER;
873 imx6_pcie->vpcie = NULL;
874 }
875
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530876 platform_set_drvdata(pdev, imx6_pcie);
877
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500878 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +0800879 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200880 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800881
Sean Crossbb389192013-09-26 11:24:47 +0800882 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800883}
884
Lucas Stach3e3e4062014-07-31 20:16:05 +0200885static void imx6_pcie_shutdown(struct platform_device *pdev)
886{
887 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
888
889 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500890 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200891}
892
Sean Crossbb389192013-09-26 11:24:47 +0800893static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500894 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
895 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500896 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700897 { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, },
Sean Crossbb389192013-09-26 11:24:47 +0800898 {},
899};
Sean Crossbb389192013-09-26 11:24:47 +0800900
901static struct platform_driver imx6_pcie_driver = {
902 .driver = {
903 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530904 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -0500905 .suppress_bind_attrs = true,
Sean Crossbb389192013-09-26 11:24:47 +0800906 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700907 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +0200908 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800909};
910
Sean Crossbb389192013-09-26 11:24:47 +0800911static int __init imx6_pcie_init(void)
912{
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700913 /*
914 * Since probe() can be deferred we need to make sure that
915 * hook_fault_code is not called after __init memory is freed
916 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
917 * we can install the handler here without risking it
918 * accessing some uninitialized driver state.
919 */
Lucas Stach415b6182017-05-22 17:06:30 -0500920 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
921 "external abort on non-linefetch");
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700922
923 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +0800924}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -0400925device_initcall(imx6_pcie_init);