1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Freescale i.MX6 SoCs
5 * Copyright (C) 2013 Kosagi
6 * http://www.kosagi.com
8 * Author: Sean Cross <xobs@kosagi.com>
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>
17 #include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
18 #include <linux/module.h>
19 #include <linux/of_gpio.h>
20 #include <linux/of_device.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/resource.h>
26 #include <linux/signal.h>
27 #include <linux/types.h>
28 #include <linux/interrupt.h>
29 #include <linux/reset.h>
31 #include "pcie-designware.h"
33 #define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
35 enum imx6_pcie_variants
{
45 bool gpio_active_high
;
48 struct clk
*pcie_inbound_axi
;
50 struct regmap
*iomuxc_gpr
;
51 struct reset_control
*pciephy_reset
;
52 struct reset_control
*apps_reset
;
53 enum imx6_pcie_variants variant
;
55 u32 tx_deemph_gen2_3p5db
;
56 u32 tx_deemph_gen2_6db
;
60 struct regulator
*vpcie
;
63 /* 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
68 /* PCIe Root Complex registers (memory-mapped) */
69 #define PCIE_RC_IMX6_MSI_CAP 0x50
70 #define PCIE_RC_LCR 0x7c
71 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
72 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
73 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
75 #define PCIE_RC_LCSR 0x80
77 /* PCIe Port Logic registers (memory-mapped) */
78 #define PL_OFFSET 0x700
79 #define PCIE_PL_PFLR (PL_OFFSET + 0x08)
80 #define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
81 #define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
82 #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
83 #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
85 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
86 #define PCIE_PHY_CTRL_DATA_LOC 0
87 #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
88 #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
89 #define PCIE_PHY_CTRL_WR_LOC 18
90 #define PCIE_PHY_CTRL_RD_LOC 19
92 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
93 #define PCIE_PHY_STAT_ACK_LOC 16
95 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
96 #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
98 /* PHY registers (not memory-mapped) */
99 #define PCIE_PHY_RX_ASIC_OUT 0x100D
100 #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
102 #define PHY_RX_OVRD_IN_LO 0x1005
103 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
104 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
106 static int pcie_phy_poll_ack(struct imx6_pcie
*imx6_pcie
, int exp_val
)
108 struct dw_pcie
*pci
= imx6_pcie
->pci
;
110 u32 max_iterations
= 10;
111 u32 wait_counter
= 0;
114 val
= dw_pcie_readl_dbi(pci
, PCIE_PHY_STAT
);
115 val
= (val
>> PCIE_PHY_STAT_ACK_LOC
) & 0x1;
122 } while (wait_counter
< max_iterations
);
127 static int pcie_phy_wait_ack(struct imx6_pcie
*imx6_pcie
, int addr
)
129 struct dw_pcie
*pci
= imx6_pcie
->pci
;
133 val
= addr
<< PCIE_PHY_CTRL_DATA_LOC
;
134 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, val
);
136 val
|= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC
);
137 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, val
);
139 ret
= pcie_phy_poll_ack(imx6_pcie
, 1);
143 val
= addr
<< PCIE_PHY_CTRL_DATA_LOC
;
144 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, val
);
146 return pcie_phy_poll_ack(imx6_pcie
, 0);
149 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
150 static int pcie_phy_read(struct imx6_pcie
*imx6_pcie
, int addr
, int *data
)
152 struct dw_pcie
*pci
= imx6_pcie
->pci
;
156 ret
= pcie_phy_wait_ack(imx6_pcie
, addr
);
160 /* assert Read signal */
161 phy_ctl
= 0x1 << PCIE_PHY_CTRL_RD_LOC
;
162 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, phy_ctl
);
164 ret
= pcie_phy_poll_ack(imx6_pcie
, 1);
168 val
= dw_pcie_readl_dbi(pci
, PCIE_PHY_STAT
);
169 *data
= val
& 0xffff;
171 /* deassert Read signal */
172 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, 0x00);
174 return pcie_phy_poll_ack(imx6_pcie
, 0);
177 static int pcie_phy_write(struct imx6_pcie
*imx6_pcie
, int addr
, int data
)
179 struct dw_pcie
*pci
= imx6_pcie
->pci
;
185 ret
= pcie_phy_wait_ack(imx6_pcie
, addr
);
189 var
= data
<< PCIE_PHY_CTRL_DATA_LOC
;
190 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, var
);
193 var
|= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC
);
194 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, var
);
196 ret
= pcie_phy_poll_ack(imx6_pcie
, 1);
200 /* deassert cap data */
201 var
= data
<< PCIE_PHY_CTRL_DATA_LOC
;
202 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, var
);
204 /* wait for ack de-assertion */
205 ret
= pcie_phy_poll_ack(imx6_pcie
, 0);
209 /* assert wr signal */
210 var
= 0x1 << PCIE_PHY_CTRL_WR_LOC
;
211 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, var
);
214 ret
= pcie_phy_poll_ack(imx6_pcie
, 1);
218 /* deassert wr signal */
219 var
= data
<< PCIE_PHY_CTRL_DATA_LOC
;
220 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, var
);
222 /* wait for ack de-assertion */
223 ret
= pcie_phy_poll_ack(imx6_pcie
, 0);
227 dw_pcie_writel_dbi(pci
, PCIE_PHY_CTRL
, 0x0);
232 static void imx6_pcie_reset_phy(struct imx6_pcie
*imx6_pcie
)
236 pcie_phy_read(imx6_pcie
, PHY_RX_OVRD_IN_LO
, &tmp
);
237 tmp
|= (PHY_RX_OVRD_IN_LO_RX_DATA_EN
|
238 PHY_RX_OVRD_IN_LO_RX_PLL_EN
);
239 pcie_phy_write(imx6_pcie
, PHY_RX_OVRD_IN_LO
, tmp
);
241 usleep_range(2000, 3000);
243 pcie_phy_read(imx6_pcie
, PHY_RX_OVRD_IN_LO
, &tmp
);
244 tmp
&= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN
|
245 PHY_RX_OVRD_IN_LO_RX_PLL_EN
);
246 pcie_phy_write(imx6_pcie
, PHY_RX_OVRD_IN_LO
, tmp
);
249 /* Added for PCI abort handling */
250 static int imx6q_pcie_abort_handler(unsigned long addr
,
251 unsigned int fsr
, struct pt_regs
*regs
)
253 unsigned long pc
= instruction_pointer(regs
);
254 unsigned long instr
= *(unsigned long *)pc
;
255 int reg
= (instr
>> 12) & 15;
258 * If the instruction being executed was a read,
259 * make it look like it read all-ones.
261 if ((instr
& 0x0c100000) == 0x04100000) {
264 if (instr
& 0x00400000)
269 regs
->uregs
[reg
] = val
;
274 if ((instr
& 0x0e100090) == 0x00100090) {
275 regs
->uregs
[reg
] = -1;
283 static void imx6_pcie_assert_core_reset(struct imx6_pcie
*imx6_pcie
)
285 struct device
*dev
= imx6_pcie
->pci
->dev
;
287 switch (imx6_pcie
->variant
) {
289 reset_control_assert(imx6_pcie
->pciephy_reset
);
290 reset_control_assert(imx6_pcie
->apps_reset
);
293 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
294 IMX6SX_GPR12_PCIE_TEST_POWERDOWN
,
295 IMX6SX_GPR12_PCIE_TEST_POWERDOWN
);
296 /* Force PCIe PHY reset */
297 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR5
,
298 IMX6SX_GPR5_PCIE_BTNRST_RESET
,
299 IMX6SX_GPR5_PCIE_BTNRST_RESET
);
302 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
303 IMX6Q_GPR1_PCIE_SW_RST
,
304 IMX6Q_GPR1_PCIE_SW_RST
);
307 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
308 IMX6Q_GPR1_PCIE_TEST_PD
, 1 << 18);
309 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
310 IMX6Q_GPR1_PCIE_REF_CLK_EN
, 0 << 16);
314 if (imx6_pcie
->vpcie
&& regulator_is_enabled(imx6_pcie
->vpcie
) > 0) {
315 int ret
= regulator_disable(imx6_pcie
->vpcie
);
318 dev_err(dev
, "failed to disable vpcie regulator: %d\n",
323 static int imx6_pcie_enable_ref_clk(struct imx6_pcie
*imx6_pcie
)
325 struct dw_pcie
*pci
= imx6_pcie
->pci
;
326 struct device
*dev
= pci
->dev
;
329 switch (imx6_pcie
->variant
) {
331 ret
= clk_prepare_enable(imx6_pcie
->pcie_inbound_axi
);
333 dev_err(dev
, "unable to enable pcie_axi clock\n");
337 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
338 IMX6SX_GPR12_PCIE_TEST_POWERDOWN
, 0);
340 case IMX6QP
: /* FALLTHROUGH */
342 /* power up core phy and enable ref clock */
343 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
344 IMX6Q_GPR1_PCIE_TEST_PD
, 0 << 18);
346 * the async reset input need ref clock to sync internally,
347 * when the ref clock comes after reset, internal synced
348 * reset time is too short, cannot meet the requirement.
349 * add one ~10us delay here.
352 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
353 IMX6Q_GPR1_PCIE_REF_CLK_EN
, 1 << 16);
362 static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie
*imx6_pcie
)
365 unsigned int retries
;
366 struct device
*dev
= imx6_pcie
->pci
->dev
;
368 for (retries
= 0; retries
< PHY_PLL_LOCK_WAIT_MAX_RETRIES
; retries
++) {
369 regmap_read(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR22
, &val
);
371 if (val
& IMX7D_GPR22_PCIE_PHY_PLL_LOCKED
)
374 usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN
,
375 PHY_PLL_LOCK_WAIT_USLEEP_MAX
);
378 dev_err(dev
, "PCIe PLL lock timeout\n");
381 static void imx6_pcie_deassert_core_reset(struct imx6_pcie
*imx6_pcie
)
383 struct dw_pcie
*pci
= imx6_pcie
->pci
;
384 struct device
*dev
= pci
->dev
;
387 if (imx6_pcie
->vpcie
&& !regulator_is_enabled(imx6_pcie
->vpcie
)) {
388 ret
= regulator_enable(imx6_pcie
->vpcie
);
390 dev_err(dev
, "failed to enable vpcie regulator: %d\n",
396 ret
= clk_prepare_enable(imx6_pcie
->pcie_phy
);
398 dev_err(dev
, "unable to enable pcie_phy clock\n");
402 ret
= clk_prepare_enable(imx6_pcie
->pcie_bus
);
404 dev_err(dev
, "unable to enable pcie_bus clock\n");
408 ret
= clk_prepare_enable(imx6_pcie
->pcie
);
410 dev_err(dev
, "unable to enable pcie clock\n");
414 ret
= imx6_pcie_enable_ref_clk(imx6_pcie
);
416 dev_err(dev
, "unable to enable pcie ref clock\n");
420 /* allow the clocks to stabilize */
421 usleep_range(200, 500);
423 /* Some boards don't have PCIe reset GPIO. */
424 if (gpio_is_valid(imx6_pcie
->reset_gpio
)) {
425 gpio_set_value_cansleep(imx6_pcie
->reset_gpio
,
426 imx6_pcie
->gpio_active_high
);
428 gpio_set_value_cansleep(imx6_pcie
->reset_gpio
,
429 !imx6_pcie
->gpio_active_high
);
432 switch (imx6_pcie
->variant
) {
434 reset_control_deassert(imx6_pcie
->pciephy_reset
);
435 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie
);
438 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR5
,
439 IMX6SX_GPR5_PCIE_BTNRST_RESET
, 0);
442 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR1
,
443 IMX6Q_GPR1_PCIE_SW_RST
, 0);
445 usleep_range(200, 500);
447 case IMX6Q
: /* Nothing to do */
454 clk_disable_unprepare(imx6_pcie
->pcie
);
456 clk_disable_unprepare(imx6_pcie
->pcie_bus
);
458 clk_disable_unprepare(imx6_pcie
->pcie_phy
);
460 if (imx6_pcie
->vpcie
&& regulator_is_enabled(imx6_pcie
->vpcie
) > 0) {
461 ret
= regulator_disable(imx6_pcie
->vpcie
);
463 dev_err(dev
, "failed to disable vpcie regulator: %d\n",
468 static void imx6_pcie_init_phy(struct imx6_pcie
*imx6_pcie
)
470 switch (imx6_pcie
->variant
) {
472 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
473 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL
, 0);
476 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
477 IMX6SX_GPR12_PCIE_RX_EQ_MASK
,
478 IMX6SX_GPR12_PCIE_RX_EQ_2
);
481 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
482 IMX6Q_GPR12_PCIE_CTL_2
, 0 << 10);
484 /* configure constant input signal to the pcie ctrl and phy */
485 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
486 IMX6Q_GPR12_LOS_LEVEL
, 9 << 4);
488 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR8
,
489 IMX6Q_GPR8_TX_DEEMPH_GEN1
,
490 imx6_pcie
->tx_deemph_gen1
<< 0);
491 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR8
,
492 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB
,
493 imx6_pcie
->tx_deemph_gen2_3p5db
<< 6);
494 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR8
,
495 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB
,
496 imx6_pcie
->tx_deemph_gen2_6db
<< 12);
497 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR8
,
498 IMX6Q_GPR8_TX_SWING_FULL
,
499 imx6_pcie
->tx_swing_full
<< 18);
500 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR8
,
501 IMX6Q_GPR8_TX_SWING_LOW
,
502 imx6_pcie
->tx_swing_low
<< 25);
506 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
507 IMX6Q_GPR12_DEVICE_TYPE
, PCI_EXP_TYPE_ROOT_PORT
<< 12);
510 static int imx6_pcie_wait_for_link(struct imx6_pcie
*imx6_pcie
)
512 struct dw_pcie
*pci
= imx6_pcie
->pci
;
513 struct device
*dev
= pci
->dev
;
515 /* check if the link is up or not */
516 if (!dw_pcie_wait_for_link(pci
))
519 dev_dbg(dev
, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
520 dw_pcie_readl_dbi(pci
, PCIE_PHY_DEBUG_R0
),
521 dw_pcie_readl_dbi(pci
, PCIE_PHY_DEBUG_R1
));
525 static int imx6_pcie_wait_for_speed_change(struct imx6_pcie
*imx6_pcie
)
527 struct dw_pcie
*pci
= imx6_pcie
->pci
;
528 struct device
*dev
= pci
->dev
;
530 unsigned int retries
;
532 for (retries
= 0; retries
< 200; retries
++) {
533 tmp
= dw_pcie_readl_dbi(pci
, PCIE_LINK_WIDTH_SPEED_CONTROL
);
534 /* Test if the speed change finished. */
535 if (!(tmp
& PORT_LOGIC_SPEED_CHANGE
))
537 usleep_range(100, 1000);
540 dev_err(dev
, "Speed change timeout\n");
544 static int imx6_pcie_establish_link(struct imx6_pcie
*imx6_pcie
)
546 struct dw_pcie
*pci
= imx6_pcie
->pci
;
547 struct device
*dev
= pci
->dev
;
552 * Force Gen1 operation when starting the link. In case the link is
553 * started in Gen2 mode, there is a possibility the devices on the
554 * bus will not be detected at all. This happens with PCIe switches.
556 tmp
= dw_pcie_readl_dbi(pci
, PCIE_RC_LCR
);
557 tmp
&= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK
;
558 tmp
|= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1
;
559 dw_pcie_writel_dbi(pci
, PCIE_RC_LCR
, tmp
);
562 if (imx6_pcie
->variant
== IMX7D
)
563 reset_control_deassert(imx6_pcie
->apps_reset
);
565 regmap_update_bits(imx6_pcie
->iomuxc_gpr
, IOMUXC_GPR12
,
566 IMX6Q_GPR12_PCIE_CTL_2
, 1 << 10);
568 ret
= imx6_pcie_wait_for_link(imx6_pcie
);
572 if (imx6_pcie
->link_gen
== 2) {
573 /* Allow Gen2 mode after the link is up. */
574 tmp
= dw_pcie_readl_dbi(pci
, PCIE_RC_LCR
);
575 tmp
&= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK
;
576 tmp
|= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2
;
577 dw_pcie_writel_dbi(pci
, PCIE_RC_LCR
, tmp
);
580 * Start Directed Speed Change so the best possible
581 * speed both link partners support can be negotiated.
583 tmp
= dw_pcie_readl_dbi(pci
, PCIE_LINK_WIDTH_SPEED_CONTROL
);
584 tmp
|= PORT_LOGIC_SPEED_CHANGE
;
585 dw_pcie_writel_dbi(pci
, PCIE_LINK_WIDTH_SPEED_CONTROL
, tmp
);
587 if (imx6_pcie
->variant
!= IMX7D
) {
589 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
590 * from i.MX6 family when no link speed transition
591 * occurs and we go Gen1 -> yep, Gen1. The difference
592 * is that, in such case, it will not be cleared by HW
593 * which will cause the following code to report false
597 ret
= imx6_pcie_wait_for_speed_change(imx6_pcie
);
599 dev_err(dev
, "Failed to bring link up!\n");
604 /* Make sure link training is finished as well! */
605 ret
= imx6_pcie_wait_for_link(imx6_pcie
);
607 dev_err(dev
, "Failed to bring link up!\n");
611 dev_info(dev
, "Link: Gen2 disabled\n");
614 tmp
= dw_pcie_readl_dbi(pci
, PCIE_RC_LCSR
);
615 dev_info(dev
, "Link up, Gen%i\n", (tmp
>> 16) & 0xf);
619 dev_dbg(dev
, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
620 dw_pcie_readl_dbi(pci
, PCIE_PHY_DEBUG_R0
),
621 dw_pcie_readl_dbi(pci
, PCIE_PHY_DEBUG_R1
));
622 imx6_pcie_reset_phy(imx6_pcie
);
626 static int imx6_pcie_host_init(struct pcie_port
*pp
)
628 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
629 struct imx6_pcie
*imx6_pcie
= to_imx6_pcie(pci
);
631 imx6_pcie_assert_core_reset(imx6_pcie
);
632 imx6_pcie_init_phy(imx6_pcie
);
633 imx6_pcie_deassert_core_reset(imx6_pcie
);
634 dw_pcie_setup_rc(pp
);
635 imx6_pcie_establish_link(imx6_pcie
);
637 if (IS_ENABLED(CONFIG_PCI_MSI
))
638 dw_pcie_msi_init(pp
);
643 static const struct dw_pcie_host_ops imx6_pcie_host_ops
= {
644 .host_init
= imx6_pcie_host_init
,
647 static int imx6_add_pcie_port(struct imx6_pcie
*imx6_pcie
,
648 struct platform_device
*pdev
)
650 struct dw_pcie
*pci
= imx6_pcie
->pci
;
651 struct pcie_port
*pp
= &pci
->pp
;
652 struct device
*dev
= &pdev
->dev
;
655 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
656 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
657 if (pp
->msi_irq
<= 0) {
658 dev_err(dev
, "failed to get MSI irq\n");
663 pp
->ops
= &imx6_pcie_host_ops
;
665 ret
= dw_pcie_host_init(pp
);
667 dev_err(dev
, "failed to initialize host\n");
674 static const struct dw_pcie_ops dw_pcie_ops
= {
675 /* No special ops needed, but pcie-designware still expects this struct */
678 static int imx6_pcie_probe(struct platform_device
*pdev
)
680 struct device
*dev
= &pdev
->dev
;
682 struct imx6_pcie
*imx6_pcie
;
683 struct resource
*dbi_base
;
684 struct device_node
*node
= dev
->of_node
;
688 imx6_pcie
= devm_kzalloc(dev
, sizeof(*imx6_pcie
), GFP_KERNEL
);
692 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
697 pci
->ops
= &dw_pcie_ops
;
699 imx6_pcie
->pci
= pci
;
701 (enum imx6_pcie_variants
)of_device_get_match_data(dev
);
703 dbi_base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
704 pci
->dbi_base
= devm_ioremap_resource(dev
, dbi_base
);
705 if (IS_ERR(pci
->dbi_base
))
706 return PTR_ERR(pci
->dbi_base
);
709 imx6_pcie
->reset_gpio
= of_get_named_gpio(node
, "reset-gpio", 0);
710 imx6_pcie
->gpio_active_high
= of_property_read_bool(node
,
711 "reset-gpio-active-high");
712 if (gpio_is_valid(imx6_pcie
->reset_gpio
)) {
713 ret
= devm_gpio_request_one(dev
, imx6_pcie
->reset_gpio
,
714 imx6_pcie
->gpio_active_high
?
715 GPIOF_OUT_INIT_HIGH
:
719 dev_err(dev
, "unable to get reset gpio\n");
722 } else if (imx6_pcie
->reset_gpio
== -EPROBE_DEFER
) {
723 return imx6_pcie
->reset_gpio
;
727 imx6_pcie
->pcie_phy
= devm_clk_get(dev
, "pcie_phy");
728 if (IS_ERR(imx6_pcie
->pcie_phy
)) {
729 dev_err(dev
, "pcie_phy clock source missing or invalid\n");
730 return PTR_ERR(imx6_pcie
->pcie_phy
);
733 imx6_pcie
->pcie_bus
= devm_clk_get(dev
, "pcie_bus");
734 if (IS_ERR(imx6_pcie
->pcie_bus
)) {
735 dev_err(dev
, "pcie_bus clock source missing or invalid\n");
736 return PTR_ERR(imx6_pcie
->pcie_bus
);
739 imx6_pcie
->pcie
= devm_clk_get(dev
, "pcie");
740 if (IS_ERR(imx6_pcie
->pcie
)) {
741 dev_err(dev
, "pcie clock source missing or invalid\n");
742 return PTR_ERR(imx6_pcie
->pcie
);
745 switch (imx6_pcie
->variant
) {
747 imx6_pcie
->pcie_inbound_axi
= devm_clk_get(dev
,
749 if (IS_ERR(imx6_pcie
->pcie_inbound_axi
)) {
750 dev_err(dev
, "pcie_inbound_axi clock missing or invalid\n");
751 return PTR_ERR(imx6_pcie
->pcie_inbound_axi
);
755 imx6_pcie
->pciephy_reset
= devm_reset_control_get_exclusive(dev
,
757 if (IS_ERR(imx6_pcie
->pciephy_reset
)) {
758 dev_err(dev
, "Failed to get PCIEPHY reset control\n");
759 return PTR_ERR(imx6_pcie
->pciephy_reset
);
762 imx6_pcie
->apps_reset
= devm_reset_control_get_exclusive(dev
,
764 if (IS_ERR(imx6_pcie
->apps_reset
)) {
765 dev_err(dev
, "Failed to get PCIE APPS reset control\n");
766 return PTR_ERR(imx6_pcie
->apps_reset
);
773 /* Grab GPR config register range */
774 imx6_pcie
->iomuxc_gpr
=
775 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
776 if (IS_ERR(imx6_pcie
->iomuxc_gpr
)) {
777 dev_err(dev
, "unable to find iomuxc registers\n");
778 return PTR_ERR(imx6_pcie
->iomuxc_gpr
);
781 /* Grab PCIe PHY Tx Settings */
782 if (of_property_read_u32(node
, "fsl,tx-deemph-gen1",
783 &imx6_pcie
->tx_deemph_gen1
))
784 imx6_pcie
->tx_deemph_gen1
= 0;
786 if (of_property_read_u32(node
, "fsl,tx-deemph-gen2-3p5db",
787 &imx6_pcie
->tx_deemph_gen2_3p5db
))
788 imx6_pcie
->tx_deemph_gen2_3p5db
= 0;
790 if (of_property_read_u32(node
, "fsl,tx-deemph-gen2-6db",
791 &imx6_pcie
->tx_deemph_gen2_6db
))
792 imx6_pcie
->tx_deemph_gen2_6db
= 20;
794 if (of_property_read_u32(node
, "fsl,tx-swing-full",
795 &imx6_pcie
->tx_swing_full
))
796 imx6_pcie
->tx_swing_full
= 127;
798 if (of_property_read_u32(node
, "fsl,tx-swing-low",
799 &imx6_pcie
->tx_swing_low
))
800 imx6_pcie
->tx_swing_low
= 127;
802 /* Limit link speed */
803 ret
= of_property_read_u32(node
, "fsl,max-link-speed",
804 &imx6_pcie
->link_gen
);
806 imx6_pcie
->link_gen
= 1;
808 imx6_pcie
->vpcie
= devm_regulator_get_optional(&pdev
->dev
, "vpcie");
809 if (IS_ERR(imx6_pcie
->vpcie
)) {
810 if (PTR_ERR(imx6_pcie
->vpcie
) != -ENODEV
)
811 return PTR_ERR(imx6_pcie
->vpcie
);
812 imx6_pcie
->vpcie
= NULL
;
815 platform_set_drvdata(pdev
, imx6_pcie
);
817 ret
= imx6_add_pcie_port(imx6_pcie
, pdev
);
821 if (pci_msi_enabled()) {
822 val
= dw_pcie_readw_dbi(pci
, PCIE_RC_IMX6_MSI_CAP
+
824 val
|= PCI_MSI_FLAGS_ENABLE
;
825 dw_pcie_writew_dbi(pci
, PCIE_RC_IMX6_MSI_CAP
+ PCI_MSI_FLAGS
,
832 static void imx6_pcie_shutdown(struct platform_device
*pdev
)
834 struct imx6_pcie
*imx6_pcie
= platform_get_drvdata(pdev
);
836 /* bring down link, so bootloader gets clean state in case of reboot */
837 imx6_pcie_assert_core_reset(imx6_pcie
);
840 static const struct of_device_id imx6_pcie_of_match
[] = {
841 { .compatible
= "fsl,imx6q-pcie", .data
= (void *)IMX6Q
, },
842 { .compatible
= "fsl,imx6sx-pcie", .data
= (void *)IMX6SX
, },
843 { .compatible
= "fsl,imx6qp-pcie", .data
= (void *)IMX6QP
, },
844 { .compatible
= "fsl,imx7d-pcie", .data
= (void *)IMX7D
, },
848 static struct platform_driver imx6_pcie_driver
= {
850 .name
= "imx6q-pcie",
851 .of_match_table
= imx6_pcie_of_match
,
852 .suppress_bind_attrs
= true,
854 .probe
= imx6_pcie_probe
,
855 .shutdown
= imx6_pcie_shutdown
,
858 static int __init
imx6_pcie_init(void)
861 * Since probe() can be deferred we need to make sure that
862 * hook_fault_code is not called after __init memory is freed
863 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
864 * we can install the handler here without risking it
865 * accessing some uninitialized driver state.
867 hook_fault_code(8, imx6q_pcie_abort_handler
, SIGBUS
, 0,
868 "external abort on non-linefetch");
870 return platform_driver_register(&imx6_pcie_driver
);
872 device_initcall(imx6_pcie_init
);