blob: 837d5e0447acbf90b5397cc49988553310dadb5e [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>
22#include <linux/pci.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#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>
Sean Crossbb389192013-09-26 11:24:47 +080029
30#include "pcie-designware.h"
31
32#define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
33
34struct imx6_pcie {
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030035 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050036 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010037 struct clk *pcie_bus;
38 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050039 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010040 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080041 struct pcie_port pp;
42 struct regmap *iomuxc_gpr;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050043 bool is_imx6sx;
Sean Crossbb389192013-09-26 11:24:47 +080044 void __iomem *mem_base;
Justin Waters28e3abe2016-01-15 10:24:35 -050045 u32 tx_deemph_gen1;
46 u32 tx_deemph_gen2_3p5db;
47 u32 tx_deemph_gen2_6db;
48 u32 tx_swing_full;
49 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050050 int link_gen;
Sean Crossbb389192013-09-26 11:24:47 +080051};
52
Marek Vasutfa33a6d2013-12-12 22:50:02 +010053/* PCIe Root Complex registers (memory-mapped) */
54#define PCIE_RC_LCR 0x7c
55#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
56#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
57#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
58
Bjorn Helgaas2393f792015-06-12 17:27:43 -050059#define PCIE_RC_LCSR 0x80
60
Sean Crossbb389192013-09-26 11:24:47 +080061/* PCIe Port Logic registers (memory-mapped) */
62#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020063#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
64#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
65#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080066#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
67#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010068#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
69#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080070
71#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
72#define PCIE_PHY_CTRL_DATA_LOC 0
73#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
74#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
75#define PCIE_PHY_CTRL_WR_LOC 18
76#define PCIE_PHY_CTRL_RD_LOC 19
77
78#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
79#define PCIE_PHY_STAT_ACK_LOC 16
80
Marek Vasutfa33a6d2013-12-12 22:50:02 +010081#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
82#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
83
Sean Crossbb389192013-09-26 11:24:47 +080084/* PHY registers (not memory-mapped) */
85#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -030086#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +080087
88#define PHY_RX_OVRD_IN_LO 0x1005
89#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
90#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
91
92static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
93{
94 u32 val;
95 u32 max_iterations = 10;
96 u32 wait_counter = 0;
97
98 do {
99 val = readl(dbi_base + PCIE_PHY_STAT);
100 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
101 wait_counter++;
102
103 if (val == exp_val)
104 return 0;
105
106 udelay(1);
107 } while (wait_counter < max_iterations);
108
109 return -ETIMEDOUT;
110}
111
112static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
113{
114 u32 val;
115 int ret;
116
117 val = addr << PCIE_PHY_CTRL_DATA_LOC;
118 writel(val, dbi_base + PCIE_PHY_CTRL);
119
120 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
121 writel(val, dbi_base + PCIE_PHY_CTRL);
122
123 ret = pcie_phy_poll_ack(dbi_base, 1);
124 if (ret)
125 return ret;
126
127 val = addr << PCIE_PHY_CTRL_DATA_LOC;
128 writel(val, dbi_base + PCIE_PHY_CTRL);
129
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500130 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800131}
132
133/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800134static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800135{
136 u32 val, phy_ctl;
137 int ret;
138
139 ret = pcie_phy_wait_ack(dbi_base, addr);
140 if (ret)
141 return ret;
142
143 /* assert Read signal */
144 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
145 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
146
147 ret = pcie_phy_poll_ack(dbi_base, 1);
148 if (ret)
149 return ret;
150
151 val = readl(dbi_base + PCIE_PHY_STAT);
152 *data = val & 0xffff;
153
154 /* deassert Read signal */
155 writel(0x00, dbi_base + PCIE_PHY_CTRL);
156
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500157 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800158}
159
160static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
161{
162 u32 var;
163 int ret;
164
165 /* write addr */
166 /* cap addr */
167 ret = pcie_phy_wait_ack(dbi_base, addr);
168 if (ret)
169 return ret;
170
171 var = data << PCIE_PHY_CTRL_DATA_LOC;
172 writel(var, dbi_base + PCIE_PHY_CTRL);
173
174 /* capture data */
175 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
176 writel(var, dbi_base + PCIE_PHY_CTRL);
177
178 ret = pcie_phy_poll_ack(dbi_base, 1);
179 if (ret)
180 return ret;
181
182 /* deassert cap data */
183 var = data << PCIE_PHY_CTRL_DATA_LOC;
184 writel(var, dbi_base + PCIE_PHY_CTRL);
185
186 /* wait for ack de-assertion */
187 ret = pcie_phy_poll_ack(dbi_base, 0);
188 if (ret)
189 return ret;
190
191 /* assert wr signal */
192 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
193 writel(var, dbi_base + PCIE_PHY_CTRL);
194
195 /* wait for ack */
196 ret = pcie_phy_poll_ack(dbi_base, 1);
197 if (ret)
198 return ret;
199
200 /* deassert wr signal */
201 var = data << PCIE_PHY_CTRL_DATA_LOC;
202 writel(var, dbi_base + PCIE_PHY_CTRL);
203
204 /* wait for ack de-assertion */
205 ret = pcie_phy_poll_ack(dbi_base, 0);
206 if (ret)
207 return ret;
208
209 writel(0x0, dbi_base + PCIE_PHY_CTRL);
210
211 return 0;
212}
213
Lucas Stach53eeb482016-01-15 19:56:47 +0100214static void imx6_pcie_reset_phy(struct pcie_port *pp)
215{
216 u32 tmp;
217
218 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
219 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
220 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
221 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
222
223 usleep_range(2000, 3000);
224
225 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
226 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
227 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
228 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
229}
230
Sean Crossbb389192013-09-26 11:24:47 +0800231/* Added for PCI abort handling */
232static int imx6q_pcie_abort_handler(unsigned long addr,
233 unsigned int fsr, struct pt_regs *regs)
234{
Sean Crossbb389192013-09-26 11:24:47 +0800235 return 0;
236}
237
238static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
239{
240 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200241 u32 val, gpr1, gpr12;
242
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500243 if (imx6_pcie->is_imx6sx) {
244 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
245 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
246 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
247 /* Force PCIe PHY reset */
248 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
249 IMX6SX_GPR5_PCIE_BTNRST_RESET,
250 IMX6SX_GPR5_PCIE_BTNRST_RESET);
251 return 0;
252 }
253
Lucas Stach3e3e4062014-07-31 20:16:05 +0200254 /*
255 * If the bootloader already enabled the link we need some special
256 * handling to get the core back into a state where it is safe to
257 * touch it for configuration. As there is no dedicated reset signal
258 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
259 * state before completely disabling LTSSM, which is a prerequisite
260 * for core configuration.
261 *
262 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
263 * indication that the bootloader activated the link.
264 */
265 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
266 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
267
268 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
269 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
270 val = readl(pp->dbi_base + PCIE_PL_PFLR);
271 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
272 val |= PCIE_PL_PFLR_FORCE_LINK;
273 writel(val, pp->dbi_base + PCIE_PL_PFLR);
274
275 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
276 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
277 }
Sean Crossbb389192013-09-26 11:24:47 +0800278
279 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
280 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800281 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
282 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
283
Sean Crossbb389192013-09-26 11:24:47 +0800284 return 0;
285}
286
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100287static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
288{
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500289 struct pcie_port *pp = &imx6_pcie->pp;
290 int ret;
291
292 if (imx6_pcie->is_imx6sx) {
293 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
294 if (ret) {
295 dev_err(pp->dev, "unable to enable pcie_axi clock\n");
296 return ret;
297 }
298
299 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
300 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
301 return ret;
302 }
303
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100304 /* power up core phy and enable ref clock */
305 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
306 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
307 /*
308 * the async reset input need ref clock to sync internally,
309 * when the ref clock comes after reset, internal synced
310 * reset time is too short, cannot meet the requirement.
311 * add one ~10us delay here.
312 */
313 udelay(10);
314 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
315 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
316 return 0;
317}
318
Sean Crossbb389192013-09-26 11:24:47 +0800319static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
320{
321 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
322 int ret;
323
Lucas Stach57526132014-03-28 17:52:55 +0100324 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800325 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100326 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
327 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800328 }
329
Lucas Stach57526132014-03-28 17:52:55 +0100330 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800331 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100332 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
333 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800334 }
335
Lucas Stach57526132014-03-28 17:52:55 +0100336 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800337 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100338 dev_err(pp->dev, "unable to enable pcie clock\n");
339 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800340 }
341
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100342 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
343 if (ret) {
344 dev_err(pp->dev, "unable to enable pcie ref clock\n");
345 goto err_ref_clk;
346 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700347
Richard Zhua2fa6f62014-10-27 13:17:32 +0800348 /* allow the clocks to stabilize */
349 usleep_range(200, 500);
350
Richard Zhubc9ef772013-12-12 22:50:03 +0100351 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300352 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500353 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
354 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100355 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500356 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
357 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100358 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500359
360 if (imx6_pcie->is_imx6sx)
361 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
362 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
363
Sean Crossbb389192013-09-26 11:24:47 +0800364 return 0;
365
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100366err_ref_clk:
367 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100368err_pcie:
369 clk_disable_unprepare(imx6_pcie->pcie_bus);
370err_pcie_bus:
371 clk_disable_unprepare(imx6_pcie->pcie_phy);
372err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800373 return ret;
374
375}
376
377static void imx6_pcie_init_phy(struct pcie_port *pp)
378{
379 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
380
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500381 if (imx6_pcie->is_imx6sx) {
382 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
383 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
384 IMX6SX_GPR12_PCIE_RX_EQ_2);
385 }
386
Sean Crossbb389192013-09-26 11:24:47 +0800387 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
388 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
389
390 /* configure constant input signal to the pcie ctrl and phy */
391 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
392 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
393 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
394 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
395
396 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500397 IMX6Q_GPR8_TX_DEEMPH_GEN1,
398 imx6_pcie->tx_deemph_gen1 << 0);
Sean Crossbb389192013-09-26 11:24:47 +0800399 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500400 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
401 imx6_pcie->tx_deemph_gen2_3p5db << 6);
Sean Crossbb389192013-09-26 11:24:47 +0800402 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500403 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
404 imx6_pcie->tx_deemph_gen2_6db << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800405 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500406 IMX6Q_GPR8_TX_SWING_FULL,
407 imx6_pcie->tx_swing_full << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800408 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500409 IMX6Q_GPR8_TX_SWING_LOW,
410 imx6_pcie->tx_swing_low << 25);
Sean Crossbb389192013-09-26 11:24:47 +0800411}
412
Marek Vasut66a60f92013-12-12 22:50:01 +0100413static int imx6_pcie_wait_for_link(struct pcie_port *pp)
414{
Joao Pinto886bc5c2016-03-10 14:44:35 -0600415 /* check if the link is up or not */
416 if (!dw_pcie_wait_for_link(pp))
417 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100418
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500419 dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
420 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
421 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600422 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100423}
424
Troy Kiskya0427462015-06-12 14:30:16 -0500425static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
426{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500427 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500428 unsigned int retries;
429
430 for (retries = 0; retries < 200; retries++) {
431 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
432 /* Test if the speed change finished. */
433 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
434 return 0;
435 usleep_range(100, 1000);
436 }
437
438 dev_err(pp->dev, "Speed change timeout\n");
439 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800440}
441
Lucas Stachd1dc9742014-03-28 17:52:59 +0100442static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
443{
444 struct pcie_port *pp = arg;
445
446 return dw_handle_msi_irq(pp);
447}
448
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500449static int imx6_pcie_establish_link(struct pcie_port *pp)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100450{
451 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500452 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500453 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100454
455 /*
456 * Force Gen1 operation when starting the link. In case the link is
457 * started in Gen2 mode, there is a possibility the devices on the
458 * bus will not be detected at all. This happens with PCIe switches.
459 */
460 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
461 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
462 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
463 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
464
465 /* Start LTSSM. */
466 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
467 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
468
469 ret = imx6_pcie_wait_for_link(pp);
Lucas Stach54a47a82016-01-25 16:49:53 -0600470 if (ret) {
471 dev_info(pp->dev, "Link never came up\n");
472 goto err_reset_phy;
473 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100474
Tim Harveya5fcec42016-04-19 19:52:44 -0500475 if (imx6_pcie->link_gen == 2) {
476 /* Allow Gen2 mode after the link is up. */
477 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
478 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
479 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
480 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
481 } else {
482 dev_info(pp->dev, "Link: Gen2 disabled\n");
483 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100484
485 /*
486 * Start Directed Speed Change so the best possible speed both link
487 * partners support can be negotiated.
488 */
489 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
490 tmp |= PORT_LOGIC_SPEED_CHANGE;
491 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
492
Troy Kiskya0427462015-06-12 14:30:16 -0500493 ret = imx6_pcie_wait_for_speed_change(pp);
494 if (ret) {
495 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600496 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100497 }
498
499 /* Make sure link training is finished as well! */
Troy Kiskya0427462015-06-12 14:30:16 -0500500 ret = imx6_pcie_wait_for_link(pp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100501 if (ret) {
502 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600503 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100504 }
505
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500506 tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500507 dev_info(pp->dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500508 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600509
510err_reset_phy:
511 dev_dbg(pp->dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
512 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
513 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
514 imx6_pcie_reset_phy(pp);
515
516 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100517}
518
Sean Crossbb389192013-09-26 11:24:47 +0800519static void imx6_pcie_host_init(struct pcie_port *pp)
520{
Sean Crossbb389192013-09-26 11:24:47 +0800521 imx6_pcie_assert_core_reset(pp);
522
523 imx6_pcie_init_phy(pp);
524
525 imx6_pcie_deassert_core_reset(pp);
526
527 dw_pcie_setup_rc(pp);
528
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500529 imx6_pcie_establish_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100530
531 if (IS_ENABLED(CONFIG_PCI_MSI))
532 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800533}
534
535static int imx6_pcie_link_up(struct pcie_port *pp)
536{
Lucas Stach4d107d32016-01-25 16:50:02 -0600537 return readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) &
538 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800539}
540
541static struct pcie_host_ops imx6_pcie_host_ops = {
542 .link_up = imx6_pcie_link_up,
543 .host_init = imx6_pcie_host_init,
544};
545
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530546static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800547 struct platform_device *pdev)
548{
549 int ret;
550
Lucas Stachd1dc9742014-03-28 17:52:59 +0100551 if (IS_ENABLED(CONFIG_PCI_MSI)) {
552 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
553 if (pp->msi_irq <= 0) {
554 dev_err(&pdev->dev, "failed to get MSI irq\n");
555 return -ENODEV;
556 }
557
558 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900559 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200560 IRQF_SHARED | IRQF_NO_THREAD,
561 "mx6-pcie-msi", pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100562 if (ret) {
563 dev_err(&pdev->dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300564 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100565 }
566 }
567
Sean Crossbb389192013-09-26 11:24:47 +0800568 pp->root_bus_nr = -1;
569 pp->ops = &imx6_pcie_host_ops;
570
Sean Crossbb389192013-09-26 11:24:47 +0800571 ret = dw_pcie_host_init(pp);
572 if (ret) {
573 dev_err(&pdev->dev, "failed to initialize host\n");
574 return ret;
575 }
576
577 return 0;
578}
579
580static int __init imx6_pcie_probe(struct platform_device *pdev)
581{
582 struct imx6_pcie *imx6_pcie;
583 struct pcie_port *pp;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300584 struct device_node *np = pdev->dev.of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800585 struct resource *dbi_base;
Justin Waters28e3abe2016-01-15 10:24:35 -0500586 struct device_node *node = pdev->dev.of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800587 int ret;
588
589 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
590 if (!imx6_pcie)
591 return -ENOMEM;
592
593 pp = &imx6_pcie->pp;
594 pp->dev = &pdev->dev;
595
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500596 imx6_pcie->is_imx6sx = of_device_is_compatible(pp->dev->of_node,
597 "fsl,imx6sx-pcie");
598
Sean Crossbb389192013-09-26 11:24:47 +0800599 /* Added for PCI abort handling */
600 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
601 "imprecise external abort");
602
603 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800604 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200605 if (IS_ERR(pp->dbi_base))
606 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800607
608 /* Fetch GPIOs */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300609 imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500610 imx6_pcie->gpio_active_high = of_property_read_bool(np,
611 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300612 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
613 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500614 imx6_pcie->gpio_active_high ?
615 GPIOF_OUT_INIT_HIGH :
616 GPIOF_OUT_INIT_LOW,
617 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300618 if (ret) {
619 dev_err(&pdev->dev, "unable to get reset gpio\n");
620 return ret;
621 }
622 }
Sean Crossbb389192013-09-26 11:24:47 +0800623
Sean Crossbb389192013-09-26 11:24:47 +0800624 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100625 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
626 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800627 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100628 "pcie_phy clock source missing or invalid\n");
629 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800630 }
631
Lucas Stach57526132014-03-28 17:52:55 +0100632 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
633 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800634 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100635 "pcie_bus clock source missing or invalid\n");
636 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800637 }
638
Lucas Stach57526132014-03-28 17:52:55 +0100639 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
640 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800641 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100642 "pcie clock source missing or invalid\n");
643 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800644 }
645
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500646 if (imx6_pcie->is_imx6sx) {
647 imx6_pcie->pcie_inbound_axi = devm_clk_get(&pdev->dev,
648 "pcie_inbound_axi");
649 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
650 dev_err(&pdev->dev,
651 "pcie_incbound_axi clock missing or invalid\n");
652 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
653 }
654 }
655
Sean Crossbb389192013-09-26 11:24:47 +0800656 /* Grab GPR config register range */
657 imx6_pcie->iomuxc_gpr =
658 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
659 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
660 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200661 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800662 }
663
Justin Waters28e3abe2016-01-15 10:24:35 -0500664 /* Grab PCIe PHY Tx Settings */
665 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
666 &imx6_pcie->tx_deemph_gen1))
667 imx6_pcie->tx_deemph_gen1 = 0;
668
669 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
670 &imx6_pcie->tx_deemph_gen2_3p5db))
671 imx6_pcie->tx_deemph_gen2_3p5db = 0;
672
673 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
674 &imx6_pcie->tx_deemph_gen2_6db))
675 imx6_pcie->tx_deemph_gen2_6db = 20;
676
677 if (of_property_read_u32(node, "fsl,tx-swing-full",
678 &imx6_pcie->tx_swing_full))
679 imx6_pcie->tx_swing_full = 127;
680
681 if (of_property_read_u32(node, "fsl,tx-swing-low",
682 &imx6_pcie->tx_swing_low))
683 imx6_pcie->tx_swing_low = 127;
684
Tim Harveya5fcec42016-04-19 19:52:44 -0500685 /* Limit link speed */
686 ret = of_property_read_u32(pp->dev->of_node, "fsl,max-link-speed",
687 &imx6_pcie->link_gen);
688 if (ret)
689 imx6_pcie->link_gen = 1;
690
Sean Crossbb389192013-09-26 11:24:47 +0800691 ret = imx6_add_pcie_port(pp, pdev);
692 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200693 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800694
695 platform_set_drvdata(pdev, imx6_pcie);
696 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800697}
698
Lucas Stach3e3e4062014-07-31 20:16:05 +0200699static void imx6_pcie_shutdown(struct platform_device *pdev)
700{
701 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
702
703 /* bring down link, so bootloader gets clean state in case of reboot */
704 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
705}
706
Sean Crossbb389192013-09-26 11:24:47 +0800707static const struct of_device_id imx6_pcie_of_match[] = {
708 { .compatible = "fsl,imx6q-pcie", },
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500709 { .compatible = "fsl,imx6sx-pcie", },
Sean Crossbb389192013-09-26 11:24:47 +0800710 {},
711};
712MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
713
714static struct platform_driver imx6_pcie_driver = {
715 .driver = {
716 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530717 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800718 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200719 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800720};
721
722/* Freescale PCIe driver does not allow module unload */
723
724static int __init imx6_pcie_init(void)
725{
726 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
727}
Lucas Stach61da50d2014-09-05 09:36:48 -0600728module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800729
730MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
731MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
732MODULE_LICENSE("GPL v2");