blob: 66c3b84a84d782ae03a1d019677e59751750ea58 [file] [log] [blame]
Sean Crossbb389192013-09-26 11:24:47 +08001/*
2 * PCIe host controller driver for Freescale i.MX6 SoCs
3 *
4 * Copyright (C) 2013 Kosagi
5 * http://www.kosagi.com
6 *
7 * Author: Sean Cross <xobs@kosagi.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/gpio.h>
17#include <linux/kernel.h>
18#include <linux/mfd/syscon.h>
19#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20#include <linux/module.h>
21#include <linux/of_gpio.h>
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050022#include <linux/of_device.h>
Sean Crossbb389192013-09-26 11:24:47 +080023#include <linux/pci.h>
24#include <linux/platform_device.h>
25#include <linux/regmap.h>
26#include <linux/resource.h>
27#include <linux/signal.h>
28#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010029#include <linux/interrupt.h>
Sean Crossbb389192013-09-26 11:24:47 +080030
31#include "pcie-designware.h"
32
33#define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
34
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 Smirnove6f1fef2016-05-02 14:08:21 -050039};
40
Sean Crossbb389192013-09-26 11:24:47 +080041struct imx6_pcie {
Bjorn Helgaas916bf1c2016-10-06 13:35:17 -050042 struct pcie_port pp; /* pp.dbi_base is DT 0th resource */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030043 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050044 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010045 struct clk *pcie_bus;
46 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050047 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010048 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080049 struct regmap *iomuxc_gpr;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050050 enum imx6_pcie_variants variant;
Justin Waters28e3abe2016-01-15 10:24:35 -050051 u32 tx_deemph_gen1;
52 u32 tx_deemph_gen2_3p5db;
53 u32 tx_deemph_gen2_6db;
54 u32 tx_swing_full;
55 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050056 int link_gen;
Sean Crossbb389192013-09-26 11:24:47 +080057};
58
Marek Vasutfa33a6d2013-12-12 22:50:02 +010059/* PCIe Root Complex registers (memory-mapped) */
60#define PCIE_RC_LCR 0x7c
61#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
62#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
63#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
64
Bjorn Helgaas2393f792015-06-12 17:27:43 -050065#define PCIE_RC_LCSR 0x80
66
Sean Crossbb389192013-09-26 11:24:47 +080067/* PCIe Port Logic registers (memory-mapped) */
68#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020069#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
70#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
71#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080072#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
73#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010074#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
75#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080076
77#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
78#define PCIE_PHY_CTRL_DATA_LOC 0
79#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
80#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
81#define PCIE_PHY_CTRL_WR_LOC 18
82#define PCIE_PHY_CTRL_RD_LOC 19
83
84#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
85#define PCIE_PHY_STAT_ACK_LOC 16
86
Marek Vasutfa33a6d2013-12-12 22:50:02 +010087#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
88#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
89
Sean Crossbb389192013-09-26 11:24:47 +080090/* PHY registers (not memory-mapped) */
91#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -030092#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +080093
94#define PHY_RX_OVRD_IN_LO 0x1005
95#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
96#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
97
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -050098static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
Sean Crossbb389192013-09-26 11:24:47 +080099{
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500100 struct pcie_port *pp = &imx6_pcie->pp;
Sean Crossbb389192013-09-26 11:24:47 +0800101 u32 val;
102 u32 max_iterations = 10;
103 u32 wait_counter = 0;
104
105 do {
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500106 val = dw_pcie_readl_rc(pp, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800107 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
108 wait_counter++;
109
110 if (val == exp_val)
111 return 0;
112
113 udelay(1);
114 } while (wait_counter < max_iterations);
115
116 return -ETIMEDOUT;
117}
118
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500119static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800120{
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500121 struct pcie_port *pp = &imx6_pcie->pp;
Sean Crossbb389192013-09-26 11:24:47 +0800122 u32 val;
123 int ret;
124
125 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500126 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800127
128 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500129 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800130
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500131 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800132 if (ret)
133 return ret;
134
135 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500136 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800137
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500138 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800139}
140
141/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500142static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800143{
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500144 struct pcie_port *pp = &imx6_pcie->pp;
Sean Crossbb389192013-09-26 11:24:47 +0800145 u32 val, phy_ctl;
146 int ret;
147
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500148 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800149 if (ret)
150 return ret;
151
152 /* assert Read signal */
153 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500154 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800155
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500156 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800157 if (ret)
158 return ret;
159
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500160 val = dw_pcie_readl_rc(pp, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800161 *data = val & 0xffff;
162
163 /* deassert Read signal */
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500164 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800165
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500166 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800167}
168
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500169static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
Sean Crossbb389192013-09-26 11:24:47 +0800170{
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500171 struct pcie_port *pp = &imx6_pcie->pp;
Sean Crossbb389192013-09-26 11:24:47 +0800172 u32 var;
173 int ret;
174
175 /* write addr */
176 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500177 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800178 if (ret)
179 return ret;
180
181 var = data << PCIE_PHY_CTRL_DATA_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500182 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800183
184 /* capture data */
185 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500186 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800187
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500188 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800189 if (ret)
190 return ret;
191
192 /* deassert cap data */
193 var = data << PCIE_PHY_CTRL_DATA_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500194 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800195
196 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500197 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800198 if (ret)
199 return ret;
200
201 /* assert wr signal */
202 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500203 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800204
205 /* wait for ack */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500206 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800207 if (ret)
208 return ret;
209
210 /* deassert wr signal */
211 var = data << PCIE_PHY_CTRL_DATA_LOC;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500212 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800213
214 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500215 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800216 if (ret)
217 return ret;
218
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500219 dw_pcie_writel_rc(pp, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800220
221 return 0;
222}
223
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500224static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100225{
226 u32 tmp;
227
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500228 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100229 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
230 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500231 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100232
233 usleep_range(2000, 3000);
234
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500235 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100236 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
237 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500238 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100239}
240
Sean Crossbb389192013-09-26 11:24:47 +0800241/* Added for PCI abort handling */
242static int imx6q_pcie_abort_handler(unsigned long addr,
243 unsigned int fsr, struct pt_regs *regs)
244{
Sean Crossbb389192013-09-26 11:24:47 +0800245 return 0;
246}
247
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500248static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800249{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500250 struct pcie_port *pp = &imx6_pcie->pp;
Lucas Stach3e3e4062014-07-31 20:16:05 +0200251 u32 val, gpr1, gpr12;
252
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500253 switch (imx6_pcie->variant) {
254 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500255 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
256 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
257 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
258 /* Force PCIe PHY reset */
259 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
260 IMX6SX_GPR5_PCIE_BTNRST_RESET,
261 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500262 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500263 case IMX6QP:
264 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
265 IMX6Q_GPR1_PCIE_SW_RST,
266 IMX6Q_GPR1_PCIE_SW_RST);
267 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500268 case IMX6Q:
269 /*
270 * If the bootloader already enabled the link we need some
271 * special handling to get the core back into a state where
272 * it is safe to touch it for configuration. As there is
273 * no dedicated reset signal wired up for MX6QDL, we need
274 * to manually force LTSSM into "detect" state before
275 * completely disabling LTSSM, which is a prerequisite for
276 * core configuration.
277 *
278 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
279 * have a strong indication that the bootloader activated
280 * the link.
281 */
282 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
283 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
284
285 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
286 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500287 val = dw_pcie_readl_rc(pp, PCIE_PL_PFLR);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500288 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
289 val |= PCIE_PL_PFLR_FORCE_LINK;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500290 dw_pcie_writel_rc(pp, PCIE_PL_PFLR, val);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500291
292 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
293 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
294 }
295
296 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
297 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
298 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
299 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
300 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500301 }
Sean Crossbb389192013-09-26 11:24:47 +0800302}
303
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100304static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
305{
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500306 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500307 struct device *dev = pp->dev;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500308 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500309
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500310 switch (imx6_pcie->variant) {
311 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500312 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
313 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500314 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500315 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500316 }
317
318 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
319 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500320 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500321 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500322 case IMX6Q:
323 /* power up core phy and enable ref clock */
324 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
325 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
326 /*
327 * the async reset input need ref clock to sync internally,
328 * when the ref clock comes after reset, internal synced
329 * reset time is too short, cannot meet the requirement.
330 * add one ~10us delay here.
331 */
332 udelay(10);
333 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
334 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
335 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500336 }
337
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500338 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100339}
340
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500341static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800342{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500343 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500344 struct device *dev = pp->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800345 int ret;
346
Lucas Stach57526132014-03-28 17:52:55 +0100347 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800348 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500349 dev_err(dev, "unable to enable pcie_phy clock\n");
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500350 return;
Sean Crossbb389192013-09-26 11:24:47 +0800351 }
352
Lucas Stach57526132014-03-28 17:52:55 +0100353 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800354 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500355 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100356 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800357 }
358
Lucas Stach57526132014-03-28 17:52:55 +0100359 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800360 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500361 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100362 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800363 }
364
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100365 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
366 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500367 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100368 goto err_ref_clk;
369 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700370
Richard Zhua2fa6f62014-10-27 13:17:32 +0800371 /* allow the clocks to stabilize */
372 usleep_range(200, 500);
373
Richard Zhubc9ef772013-12-12 22:50:03 +0100374 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300375 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500376 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
377 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100378 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500379 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
380 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100381 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500382
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500383 switch (imx6_pcie->variant) {
384 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500385 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
386 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500387 break;
388 case IMX6QP:
389 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
390 IMX6Q_GPR1_PCIE_SW_RST, 0);
391
392 usleep_range(200, 500);
393 break;
394 case IMX6Q: /* Nothing to do */
395 break;
396 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500397
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500398 return;
Sean Crossbb389192013-09-26 11:24:47 +0800399
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100400err_ref_clk:
401 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100402err_pcie:
403 clk_disable_unprepare(imx6_pcie->pcie_bus);
404err_pcie_bus:
405 clk_disable_unprepare(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800406}
407
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500408static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800409{
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500410 if (imx6_pcie->variant == IMX6SX)
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500411 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
412 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
413 IMX6SX_GPR12_PCIE_RX_EQ_2);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500414
Sean Crossbb389192013-09-26 11:24:47 +0800415 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
416 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
417
418 /* configure constant input signal to the pcie ctrl and phy */
419 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
420 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
421 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
422 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
423
424 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500425 IMX6Q_GPR8_TX_DEEMPH_GEN1,
426 imx6_pcie->tx_deemph_gen1 << 0);
Sean Crossbb389192013-09-26 11:24:47 +0800427 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500428 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
429 imx6_pcie->tx_deemph_gen2_3p5db << 6);
Sean Crossbb389192013-09-26 11:24:47 +0800430 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500431 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
432 imx6_pcie->tx_deemph_gen2_6db << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800433 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500434 IMX6Q_GPR8_TX_SWING_FULL,
435 imx6_pcie->tx_swing_full << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800436 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500437 IMX6Q_GPR8_TX_SWING_LOW,
438 imx6_pcie->tx_swing_low << 25);
Sean Crossbb389192013-09-26 11:24:47 +0800439}
440
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500441static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
Marek Vasut66a60f92013-12-12 22:50:01 +0100442{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500443 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500444 struct device *dev = pp->dev;
445
Joao Pinto886bc5c2016-03-10 14:44:35 -0600446 /* check if the link is up or not */
447 if (!dw_pcie_wait_for_link(pp))
448 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100449
Bjorn Helgaas13957652016-10-06 13:35:18 -0500450 dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500451 dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R0),
452 dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600453 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100454}
455
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500456static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500457{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500458 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500459 struct device *dev = pp->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500460 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500461 unsigned int retries;
462
463 for (retries = 0; retries < 200; retries++) {
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500464 tmp = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500465 /* Test if the speed change finished. */
466 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
467 return 0;
468 usleep_range(100, 1000);
469 }
470
Bjorn Helgaas13957652016-10-06 13:35:18 -0500471 dev_err(dev, "Speed change timeout\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500472 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800473}
474
Lucas Stachd1dc9742014-03-28 17:52:59 +0100475static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
476{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500477 struct imx6_pcie *imx6_pcie = arg;
478 struct pcie_port *pp = &imx6_pcie->pp;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100479
480 return dw_handle_msi_irq(pp);
481}
482
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500483static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100484{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500485 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500486 struct device *dev = pp->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500487 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500488 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100489
490 /*
491 * Force Gen1 operation when starting the link. In case the link is
492 * started in Gen2 mode, there is a possibility the devices on the
493 * bus will not be detected at all. This happens with PCIe switches.
494 */
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500495 tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100496 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
497 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500498 dw_pcie_writel_rc(pp, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100499
500 /* Start LTSSM. */
501 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
502 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
503
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500504 ret = imx6_pcie_wait_for_link(imx6_pcie);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200505 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600506 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100507
Tim Harveya5fcec42016-04-19 19:52:44 -0500508 if (imx6_pcie->link_gen == 2) {
509 /* Allow Gen2 mode after the link is up. */
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500510 tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500511 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
512 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500513 dw_pcie_writel_rc(pp, PCIE_RC_LCR, tmp);
Tim Harveya5fcec42016-04-19 19:52:44 -0500514 } else {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500515 dev_info(dev, "Link: Gen2 disabled\n");
Tim Harveya5fcec42016-04-19 19:52:44 -0500516 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100517
518 /*
519 * Start Directed Speed Change so the best possible speed both link
520 * partners support can be negotiated.
521 */
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500522 tmp = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100523 tmp |= PORT_LOGIC_SPEED_CHANGE;
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500524 dw_pcie_writel_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100525
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500526 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
Troy Kiskya0427462015-06-12 14:30:16 -0500527 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500528 dev_err(dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600529 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100530 }
531
532 /* Make sure link training is finished as well! */
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500533 ret = imx6_pcie_wait_for_link(imx6_pcie);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100534 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500535 dev_err(dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600536 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100537 }
538
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500539 tmp = dw_pcie_readl_rc(pp, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500540 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500541 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600542
543err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500544 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500545 dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R0),
546 dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1));
547 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600548 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100549}
550
Sean Crossbb389192013-09-26 11:24:47 +0800551static void imx6_pcie_host_init(struct pcie_port *pp)
552{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500553 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800554
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500555 imx6_pcie_assert_core_reset(imx6_pcie);
556 imx6_pcie_init_phy(imx6_pcie);
557 imx6_pcie_deassert_core_reset(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800558 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500559 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100560
561 if (IS_ENABLED(CONFIG_PCI_MSI))
562 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800563}
564
565static int imx6_pcie_link_up(struct pcie_port *pp)
566{
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500567 return dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1) &
Lucas Stach4d107d32016-01-25 16:50:02 -0600568 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800569}
570
571static struct pcie_host_ops imx6_pcie_host_ops = {
572 .link_up = imx6_pcie_link_up,
573 .host_init = imx6_pcie_host_init,
574};
575
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500576static int __init imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
577 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800578{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500579 struct pcie_port *pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500580 struct device *dev = pp->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800581 int ret;
582
Lucas Stachd1dc9742014-03-28 17:52:59 +0100583 if (IS_ENABLED(CONFIG_PCI_MSI)) {
584 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
585 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500586 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100587 return -ENODEV;
588 }
589
Bjorn Helgaas13957652016-10-06 13:35:18 -0500590 ret = devm_request_irq(dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900591 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200592 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500593 "mx6-pcie-msi", imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100594 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500595 dev_err(dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300596 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100597 }
598 }
599
Sean Crossbb389192013-09-26 11:24:47 +0800600 pp->root_bus_nr = -1;
601 pp->ops = &imx6_pcie_host_ops;
602
Sean Crossbb389192013-09-26 11:24:47 +0800603 ret = dw_pcie_host_init(pp);
604 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500605 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800606 return ret;
607 }
608
609 return 0;
610}
611
612static int __init imx6_pcie_probe(struct platform_device *pdev)
613{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500614 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800615 struct imx6_pcie *imx6_pcie;
616 struct pcie_port *pp;
Sean Crossbb389192013-09-26 11:24:47 +0800617 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500618 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800619 int ret;
620
Bjorn Helgaas13957652016-10-06 13:35:18 -0500621 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800622 if (!imx6_pcie)
623 return -ENOMEM;
624
625 pp = &imx6_pcie->pp;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500626 pp->dev = dev;
Sean Crossbb389192013-09-26 11:24:47 +0800627
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500628 imx6_pcie->variant =
Bjorn Helgaas13957652016-10-06 13:35:18 -0500629 (enum imx6_pcie_variants)of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500630
Sean Crossbb389192013-09-26 11:24:47 +0800631 /* Added for PCI abort handling */
632 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
633 "imprecise external abort");
634
635 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500636 pp->dbi_base = devm_ioremap_resource(dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200637 if (IS_ERR(pp->dbi_base))
638 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800639
640 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500641 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
642 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500643 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300644 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500645 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500646 imx6_pcie->gpio_active_high ?
647 GPIOF_OUT_INIT_HIGH :
648 GPIOF_OUT_INIT_LOW,
649 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300650 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500651 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300652 return ret;
653 }
654 }
Sean Crossbb389192013-09-26 11:24:47 +0800655
Sean Crossbb389192013-09-26 11:24:47 +0800656 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -0500657 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +0100658 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500659 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100660 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800661 }
662
Bjorn Helgaas13957652016-10-06 13:35:18 -0500663 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +0100664 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500665 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100666 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800667 }
668
Bjorn Helgaas13957652016-10-06 13:35:18 -0500669 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +0100670 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500671 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100672 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800673 }
674
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500675 if (imx6_pcie->variant == IMX6SX) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500676 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500677 "pcie_inbound_axi");
678 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500679 dev_err(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500680 "pcie_incbound_axi clock missing or invalid\n");
681 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
682 }
683 }
684
Sean Crossbb389192013-09-26 11:24:47 +0800685 /* Grab GPR config register range */
686 imx6_pcie->iomuxc_gpr =
687 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
688 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500689 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200690 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800691 }
692
Justin Waters28e3abe2016-01-15 10:24:35 -0500693 /* Grab PCIe PHY Tx Settings */
694 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
695 &imx6_pcie->tx_deemph_gen1))
696 imx6_pcie->tx_deemph_gen1 = 0;
697
698 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
699 &imx6_pcie->tx_deemph_gen2_3p5db))
700 imx6_pcie->tx_deemph_gen2_3p5db = 0;
701
702 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
703 &imx6_pcie->tx_deemph_gen2_6db))
704 imx6_pcie->tx_deemph_gen2_6db = 20;
705
706 if (of_property_read_u32(node, "fsl,tx-swing-full",
707 &imx6_pcie->tx_swing_full))
708 imx6_pcie->tx_swing_full = 127;
709
710 if (of_property_read_u32(node, "fsl,tx-swing-low",
711 &imx6_pcie->tx_swing_low))
712 imx6_pcie->tx_swing_low = 127;
713
Tim Harveya5fcec42016-04-19 19:52:44 -0500714 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500715 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -0500716 &imx6_pcie->link_gen);
717 if (ret)
718 imx6_pcie->link_gen = 1;
719
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500720 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +0800721 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200722 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800723
724 platform_set_drvdata(pdev, imx6_pcie);
725 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800726}
727
Lucas Stach3e3e4062014-07-31 20:16:05 +0200728static void imx6_pcie_shutdown(struct platform_device *pdev)
729{
730 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
731
732 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500733 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200734}
735
Sean Crossbb389192013-09-26 11:24:47 +0800736static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500737 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
738 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500739 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Sean Crossbb389192013-09-26 11:24:47 +0800740 {},
741};
Sean Crossbb389192013-09-26 11:24:47 +0800742
743static struct platform_driver imx6_pcie_driver = {
744 .driver = {
745 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530746 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800747 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200748 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800749};
750
Sean Crossbb389192013-09-26 11:24:47 +0800751static int __init imx6_pcie_init(void)
752{
753 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
754}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -0400755device_initcall(imx6_pcie_init);