1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Freescale i.MX6 SoCs
5 * Copyright (C) 2013 Kosagi
6 * https://www.kosagi.com
11 #include <linux/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
18 #include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_device.h>
22 #include <linux/of_address.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/resource.h>
28 #include <linux/signal.h>
29 #include <linux/types.h>
30 #include <linux/interrupt.h>
31 #include <linux/reset.h>
32 #include <linux/pm_domain.h>
33 #include <linux/pm_runtime.h>
35 #include "pcie-designware.h"
37 #define IMX8MQ_GPR_PCIE_REF_USE_PAD BIT(9)
38 #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN BIT(10)
39 #define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE BIT(11)
40 #define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE GENMASK(11, 8)
41 #define IMX8MQ_PCIE2_BASE_ADDR 0x33c00000
43 #define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
45 enum imx6_pcie_variants {
53 #define IMX6_PCIE_FLAG_IMX6_PHY BIT(0)
54 #define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE BIT(1)
55 #define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND BIT(2)
57 struct imx6_pcie_drvdata {
58 enum imx6_pcie_variants variant;
66 bool gpio_active_high;
69 struct clk *pcie_inbound_axi;
72 struct regmap *iomuxc_gpr;
74 struct reset_control *pciephy_reset;
75 struct reset_control *apps_reset;
76 struct reset_control *turnoff_reset;
78 u32 tx_deemph_gen2_3p5db;
79 u32 tx_deemph_gen2_6db;
83 struct regulator *vpcie;
84 void __iomem *phy_base;
86 /* power domain for pcie */
87 struct device *pd_pcie;
88 /* power domain for pcie phy */
89 struct device *pd_pcie_phy;
90 const struct imx6_pcie_drvdata *drvdata;
93 /* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
94 #define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
95 #define PHY_PLL_LOCK_WAIT_TIMEOUT (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
97 /* PCIe Root Complex registers (memory-mapped) */
98 #define PCIE_RC_IMX6_MSI_CAP 0x50
99 #define PCIE_RC_LCR 0x7c
100 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
101 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
102 #define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
104 #define PCIE_RC_LCSR 0x80
106 /* PCIe Port Logic registers (memory-mapped) */
107 #define PL_OFFSET 0x700
109 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
110 #define PCIE_PHY_CTRL_DATA(x) FIELD_PREP(GENMASK(15, 0), (x))
111 #define PCIE_PHY_CTRL_CAP_ADR BIT(16)
112 #define PCIE_PHY_CTRL_CAP_DAT BIT(17)
113 #define PCIE_PHY_CTRL_WR BIT(18)
114 #define PCIE_PHY_CTRL_RD BIT(19)
116 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
117 #define PCIE_PHY_STAT_ACK BIT(16)
119 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
121 /* PHY registers (not memory-mapped) */
122 #define PCIE_PHY_ATEOVRD 0x10
123 #define PCIE_PHY_ATEOVRD_EN BIT(2)
124 #define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
125 #define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
127 #define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
128 #define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
129 #define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
130 #define PCIE_PHY_MPLL_MULTIPLIER_OVRD BIT(9)
132 #define PCIE_PHY_RX_ASIC_OUT 0x100D
133 #define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
135 /* iMX7 PCIe PHY registers */
136 #define PCIE_PHY_CMN_REG4 0x14
137 /* These are probably the bits that *aren't* DCC_FB_EN */
138 #define PCIE_PHY_CMN_REG4_DCC_FB_EN 0x29
140 #define PCIE_PHY_CMN_REG15 0x54
141 #define PCIE_PHY_CMN_REG15_DLY_4 BIT(2)
142 #define PCIE_PHY_CMN_REG15_PLL_PD BIT(5)
143 #define PCIE_PHY_CMN_REG15_OVRD_PLL_PD BIT(7)
145 #define PCIE_PHY_CMN_REG24 0x90
146 #define PCIE_PHY_CMN_REG24_RX_EQ BIT(6)
147 #define PCIE_PHY_CMN_REG24_RX_EQ_SEL BIT(3)
149 #define PCIE_PHY_CMN_REG26 0x98
150 #define PCIE_PHY_CMN_REG26_ATT_MODE 0xBC
152 #define PHY_RX_OVRD_IN_LO 0x1005
153 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN BIT(5)
154 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN BIT(3)
156 static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
158 struct dw_pcie *pci = imx6_pcie->pci;
160 u32 max_iterations = 10;
161 u32 wait_counter = 0;
164 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
172 } while (wait_counter < max_iterations);
177 static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
179 struct dw_pcie *pci = imx6_pcie->pci;
183 val = PCIE_PHY_CTRL_DATA(addr);
184 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
186 val |= PCIE_PHY_CTRL_CAP_ADR;
187 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
189 ret = pcie_phy_poll_ack(imx6_pcie, true);
193 val = PCIE_PHY_CTRL_DATA(addr);
194 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
196 return pcie_phy_poll_ack(imx6_pcie, false);
199 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
200 static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
202 struct dw_pcie *pci = imx6_pcie->pci;
206 ret = pcie_phy_wait_ack(imx6_pcie, addr);
210 /* assert Read signal */
211 phy_ctl = PCIE_PHY_CTRL_RD;
212 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
214 ret = pcie_phy_poll_ack(imx6_pcie, true);
218 *data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
220 /* deassert Read signal */
221 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
223 return pcie_phy_poll_ack(imx6_pcie, false);
226 static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
228 struct dw_pcie *pci = imx6_pcie->pci;
234 ret = pcie_phy_wait_ack(imx6_pcie, addr);
238 var = PCIE_PHY_CTRL_DATA(data);
239 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
242 var |= PCIE_PHY_CTRL_CAP_DAT;
243 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
245 ret = pcie_phy_poll_ack(imx6_pcie, true);
249 /* deassert cap data */
250 var = PCIE_PHY_CTRL_DATA(data);
251 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
253 /* wait for ack de-assertion */
254 ret = pcie_phy_poll_ack(imx6_pcie, false);
258 /* assert wr signal */
259 var = PCIE_PHY_CTRL_WR;
260 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
263 ret = pcie_phy_poll_ack(imx6_pcie, true);
267 /* deassert wr signal */
268 var = PCIE_PHY_CTRL_DATA(data);
269 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
271 /* wait for ack de-assertion */
272 ret = pcie_phy_poll_ack(imx6_pcie, false);
276 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
281 static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
285 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
288 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
289 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
290 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
291 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
293 usleep_range(2000, 3000);
295 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
296 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
297 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
298 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
302 /* Added for PCI abort handling */
303 static int imx6q_pcie_abort_handler(unsigned long addr,
304 unsigned int fsr, struct pt_regs *regs)
306 unsigned long pc = instruction_pointer(regs);
307 unsigned long instr = *(unsigned long *)pc;
308 int reg = (instr >> 12) & 15;
311 * If the instruction being executed was a read,
312 * make it look like it read all-ones.
314 if ((instr & 0x0c100000) == 0x04100000) {
317 if (instr & 0x00400000)
322 regs->uregs[reg] = val;
327 if ((instr & 0x0e100090) == 0x00100090) {
328 regs->uregs[reg] = -1;
337 static int imx6_pcie_attach_pd(struct device *dev)
339 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
340 struct device_link *link;
342 /* Do nothing when in a single power domain */
346 imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
347 if (IS_ERR(imx6_pcie->pd_pcie))
348 return PTR_ERR(imx6_pcie->pd_pcie);
349 /* Do nothing when power domain missing */
350 if (!imx6_pcie->pd_pcie)
352 link = device_link_add(dev, imx6_pcie->pd_pcie,
357 dev_err(dev, "Failed to add device_link to pcie pd.\n");
361 imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
362 if (IS_ERR(imx6_pcie->pd_pcie_phy))
363 return PTR_ERR(imx6_pcie->pd_pcie_phy);
365 link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
370 dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
377 static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
379 struct device *dev = imx6_pcie->pci->dev;
381 switch (imx6_pcie->drvdata->variant) {
384 reset_control_assert(imx6_pcie->pciephy_reset);
385 reset_control_assert(imx6_pcie->apps_reset);
388 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
389 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
390 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
391 /* Force PCIe PHY reset */
392 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
393 IMX6SX_GPR5_PCIE_BTNRST_RESET,
394 IMX6SX_GPR5_PCIE_BTNRST_RESET);
397 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
398 IMX6Q_GPR1_PCIE_SW_RST,
399 IMX6Q_GPR1_PCIE_SW_RST);
402 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
403 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
404 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
405 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
409 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
410 int ret = regulator_disable(imx6_pcie->vpcie);
413 dev_err(dev, "failed to disable vpcie regulator: %d\n",
418 static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
420 WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ);
421 return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
424 static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
426 struct dw_pcie *pci = imx6_pcie->pci;
427 struct device *dev = pci->dev;
431 switch (imx6_pcie->drvdata->variant) {
433 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
435 dev_err(dev, "unable to enable pcie_axi clock\n");
439 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
440 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
444 /* power up core phy and enable ref clock */
445 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
446 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
448 * the async reset input need ref clock to sync internally,
449 * when the ref clock comes after reset, internal synced
450 * reset time is too short, cannot meet the requirement.
451 * add one ~10us delay here.
453 usleep_range(10, 100);
454 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
455 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
460 ret = clk_prepare_enable(imx6_pcie->pcie_aux);
462 dev_err(dev, "unable to enable pcie_aux clock\n");
466 offset = imx6_pcie_grp_offset(imx6_pcie);
468 * Set the over ride low and enabled
469 * make sure that REF_CLK is turned on.
471 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
472 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
474 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
475 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
476 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
483 static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
486 struct device *dev = imx6_pcie->pci->dev;
488 if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
490 val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
491 PHY_PLL_LOCK_WAIT_USLEEP_MAX,
492 PHY_PLL_LOCK_WAIT_TIMEOUT))
493 dev_err(dev, "PCIe PLL lock timeout\n");
496 static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
498 struct dw_pcie *pci = imx6_pcie->pci;
499 struct device *dev = pci->dev;
502 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
503 ret = regulator_enable(imx6_pcie->vpcie);
505 dev_err(dev, "failed to enable vpcie regulator: %d\n",
511 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
513 dev_err(dev, "unable to enable pcie_phy clock\n");
517 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
519 dev_err(dev, "unable to enable pcie_bus clock\n");
523 ret = clk_prepare_enable(imx6_pcie->pcie);
525 dev_err(dev, "unable to enable pcie clock\n");
529 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
531 dev_err(dev, "unable to enable pcie ref clock\n");
535 /* allow the clocks to stabilize */
536 usleep_range(200, 500);
538 /* Some boards don't have PCIe reset GPIO. */
539 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
540 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
541 imx6_pcie->gpio_active_high);
543 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
544 !imx6_pcie->gpio_active_high);
547 switch (imx6_pcie->drvdata->variant) {
549 reset_control_deassert(imx6_pcie->pciephy_reset);
552 reset_control_deassert(imx6_pcie->pciephy_reset);
554 /* Workaround for ERR010728, failure of PCI-e PLL VCO to
555 * oscillate, especially when cold. This turns off "Duty-cycle
556 * Corrector" and other mysterious undocumented things.
558 if (likely(imx6_pcie->phy_base)) {
559 /* De-assert DCC_FB_EN */
560 writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
561 imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
562 /* Assert RX_EQS and RX_EQS_SEL */
563 writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
564 | PCIE_PHY_CMN_REG24_RX_EQ,
565 imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
566 /* Assert ATT_MODE */
567 writel(PCIE_PHY_CMN_REG26_ATT_MODE,
568 imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
570 dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
573 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
576 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
577 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
580 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
581 IMX6Q_GPR1_PCIE_SW_RST, 0);
583 usleep_range(200, 500);
585 case IMX6Q: /* Nothing to do */
592 clk_disable_unprepare(imx6_pcie->pcie);
594 clk_disable_unprepare(imx6_pcie->pcie_bus);
596 clk_disable_unprepare(imx6_pcie->pcie_phy);
598 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
599 ret = regulator_disable(imx6_pcie->vpcie);
601 dev_err(dev, "failed to disable vpcie regulator: %d\n",
606 static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
608 unsigned int mask, val;
610 if (imx6_pcie->drvdata->variant == IMX8MQ &&
611 imx6_pcie->controller_id == 1) {
612 mask = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE;
613 val = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
614 PCI_EXP_TYPE_ROOT_PORT);
616 mask = IMX6Q_GPR12_DEVICE_TYPE;
617 val = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE,
618 PCI_EXP_TYPE_ROOT_PORT);
621 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val);
624 static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
626 switch (imx6_pcie->drvdata->variant) {
629 * TODO: Currently this code assumes external
630 * oscillator is being used
632 regmap_update_bits(imx6_pcie->iomuxc_gpr,
633 imx6_pcie_grp_offset(imx6_pcie),
634 IMX8MQ_GPR_PCIE_REF_USE_PAD,
635 IMX8MQ_GPR_PCIE_REF_USE_PAD);
638 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
639 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
642 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
643 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
644 IMX6SX_GPR12_PCIE_RX_EQ_2);
647 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
648 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
650 /* configure constant input signal to the pcie ctrl and phy */
651 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
652 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
654 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
655 IMX6Q_GPR8_TX_DEEMPH_GEN1,
656 imx6_pcie->tx_deemph_gen1 << 0);
657 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
658 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
659 imx6_pcie->tx_deemph_gen2_3p5db << 6);
660 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
661 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
662 imx6_pcie->tx_deemph_gen2_6db << 12);
663 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
664 IMX6Q_GPR8_TX_SWING_FULL,
665 imx6_pcie->tx_swing_full << 18);
666 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
667 IMX6Q_GPR8_TX_SWING_LOW,
668 imx6_pcie->tx_swing_low << 25);
672 imx6_pcie_configure_type(imx6_pcie);
675 static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
677 unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
681 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
687 * The default settings of the MPLL are for a 125MHz input
688 * clock, so no need to reconfigure anything in that case.
700 dev_err(imx6_pcie->pci->dev,
701 "Unsupported PHY reference clock rate %lu\n", phy_rate);
705 pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
706 val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
707 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
708 val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
709 val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
710 pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
712 pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
713 val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
714 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
715 val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
716 val |= PCIE_PHY_ATEOVRD_EN;
717 pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
722 static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
724 struct dw_pcie *pci = imx6_pcie->pci;
725 struct device *dev = pci->dev;
727 unsigned int retries;
729 for (retries = 0; retries < 200; retries++) {
730 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
731 /* Test if the speed change finished. */
732 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
734 usleep_range(100, 1000);
737 dev_err(dev, "Speed change timeout\n");
741 static void imx6_pcie_ltssm_enable(struct device *dev)
743 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
745 switch (imx6_pcie->drvdata->variant) {
749 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
750 IMX6Q_GPR12_PCIE_CTL_2,
751 IMX6Q_GPR12_PCIE_CTL_2);
755 reset_control_deassert(imx6_pcie->apps_reset);
760 static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
762 struct dw_pcie *pci = imx6_pcie->pci;
763 struct device *dev = pci->dev;
768 * Force Gen1 operation when starting the link. In case the link is
769 * started in Gen2 mode, there is a possibility the devices on the
770 * bus will not be detected at all. This happens with PCIe switches.
772 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
773 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
774 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
775 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
778 imx6_pcie_ltssm_enable(dev);
780 ret = dw_pcie_wait_for_link(pci);
784 if (imx6_pcie->link_gen == 2) {
785 /* Allow Gen2 mode after the link is up. */
786 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
787 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
788 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
789 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
792 * Start Directed Speed Change so the best possible
793 * speed both link partners support can be negotiated.
795 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
796 tmp |= PORT_LOGIC_SPEED_CHANGE;
797 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
799 if (imx6_pcie->drvdata->flags &
800 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
802 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
803 * from i.MX6 family when no link speed transition
804 * occurs and we go Gen1 -> yep, Gen1. The difference
805 * is that, in such case, it will not be cleared by HW
806 * which will cause the following code to report false
810 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
812 dev_err(dev, "Failed to bring link up!\n");
817 /* Make sure link training is finished as well! */
818 ret = dw_pcie_wait_for_link(pci);
820 dev_err(dev, "Failed to bring link up!\n");
824 dev_info(dev, "Link: Gen2 disabled\n");
827 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
828 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
832 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
833 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0),
834 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1));
835 imx6_pcie_reset_phy(imx6_pcie);
839 static int imx6_pcie_host_init(struct pcie_port *pp)
841 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
842 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
844 imx6_pcie_assert_core_reset(imx6_pcie);
845 imx6_pcie_init_phy(imx6_pcie);
846 imx6_pcie_deassert_core_reset(imx6_pcie);
847 imx6_setup_phy_mpll(imx6_pcie);
848 dw_pcie_setup_rc(pp);
849 imx6_pcie_establish_link(imx6_pcie);
851 if (IS_ENABLED(CONFIG_PCI_MSI))
852 dw_pcie_msi_init(pp);
857 static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
858 .host_init = imx6_pcie_host_init,
861 static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
862 struct platform_device *pdev)
864 struct dw_pcie *pci = imx6_pcie->pci;
865 struct pcie_port *pp = &pci->pp;
866 struct device *dev = &pdev->dev;
869 if (IS_ENABLED(CONFIG_PCI_MSI)) {
870 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
875 pp->ops = &imx6_pcie_host_ops;
877 ret = dw_pcie_host_init(pp);
879 dev_err(dev, "failed to initialize host\n");
886 static const struct dw_pcie_ops dw_pcie_ops = {
887 /* No special ops needed, but pcie-designware still expects this struct */
890 #ifdef CONFIG_PM_SLEEP
891 static void imx6_pcie_ltssm_disable(struct device *dev)
893 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
895 switch (imx6_pcie->drvdata->variant) {
898 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
899 IMX6Q_GPR12_PCIE_CTL_2, 0);
902 reset_control_assert(imx6_pcie->apps_reset);
905 dev_err(dev, "ltssm_disable not supported\n");
909 static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
911 struct device *dev = imx6_pcie->pci->dev;
913 /* Some variants have a turnoff reset in DT */
914 if (imx6_pcie->turnoff_reset) {
915 reset_control_assert(imx6_pcie->turnoff_reset);
916 reset_control_deassert(imx6_pcie->turnoff_reset);
917 goto pm_turnoff_sleep;
920 /* Others poke directly at IOMUXC registers */
921 switch (imx6_pcie->drvdata->variant) {
923 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
924 IMX6SX_GPR12_PCIE_PM_TURN_OFF,
925 IMX6SX_GPR12_PCIE_PM_TURN_OFF);
926 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
927 IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
930 dev_err(dev, "PME_Turn_Off not implemented\n");
935 * Components with an upstream port must respond to
936 * PME_Turn_Off with PME_TO_Ack but we can't check.
938 * The standard recommends a 1-10ms timeout after which to
939 * proceed anyway as if acks were received.
942 usleep_range(1000, 10000);
945 static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
947 clk_disable_unprepare(imx6_pcie->pcie);
948 clk_disable_unprepare(imx6_pcie->pcie_phy);
949 clk_disable_unprepare(imx6_pcie->pcie_bus);
951 switch (imx6_pcie->drvdata->variant) {
953 clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
956 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
957 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
958 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
961 clk_disable_unprepare(imx6_pcie->pcie_aux);
968 static int imx6_pcie_suspend_noirq(struct device *dev)
970 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
972 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
975 imx6_pcie_pm_turnoff(imx6_pcie);
976 imx6_pcie_clk_disable(imx6_pcie);
977 imx6_pcie_ltssm_disable(dev);
982 static int imx6_pcie_resume_noirq(struct device *dev)
985 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
986 struct pcie_port *pp = &imx6_pcie->pci->pp;
988 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
991 imx6_pcie_assert_core_reset(imx6_pcie);
992 imx6_pcie_init_phy(imx6_pcie);
993 imx6_pcie_deassert_core_reset(imx6_pcie);
994 dw_pcie_setup_rc(pp);
996 ret = imx6_pcie_establish_link(imx6_pcie);
998 dev_info(dev, "pcie link is down after resume.\n");
1004 static const struct dev_pm_ops imx6_pcie_pm_ops = {
1005 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
1006 imx6_pcie_resume_noirq)
1009 static int imx6_pcie_probe(struct platform_device *pdev)
1011 struct device *dev = &pdev->dev;
1012 struct dw_pcie *pci;
1013 struct imx6_pcie *imx6_pcie;
1014 struct device_node *np;
1015 struct resource *dbi_base;
1016 struct device_node *node = dev->of_node;
1020 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
1024 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1029 pci->ops = &dw_pcie_ops;
1031 imx6_pcie->pci = pci;
1032 imx6_pcie->drvdata = of_device_get_match_data(dev);
1034 /* Find the PHY if one is defined, only imx7d uses it */
1035 np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
1037 struct resource res;
1039 ret = of_address_to_resource(np, 0, &res);
1041 dev_err(dev, "Unable to map PCIe PHY\n");
1044 imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1045 if (IS_ERR(imx6_pcie->phy_base)) {
1046 dev_err(dev, "Unable to map PCIe PHY\n");
1047 return PTR_ERR(imx6_pcie->phy_base);
1051 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1052 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
1053 if (IS_ERR(pci->dbi_base))
1054 return PTR_ERR(pci->dbi_base);
1057 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1058 imx6_pcie->gpio_active_high = of_property_read_bool(node,
1059 "reset-gpio-active-high");
1060 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
1061 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
1062 imx6_pcie->gpio_active_high ?
1063 GPIOF_OUT_INIT_HIGH :
1067 dev_err(dev, "unable to get reset gpio\n");
1070 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1071 return imx6_pcie->reset_gpio;
1075 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
1076 if (IS_ERR(imx6_pcie->pcie_phy)) {
1077 dev_err(dev, "pcie_phy clock source missing or invalid\n");
1078 return PTR_ERR(imx6_pcie->pcie_phy);
1081 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
1082 if (IS_ERR(imx6_pcie->pcie_bus)) {
1083 dev_err(dev, "pcie_bus clock source missing or invalid\n");
1084 return PTR_ERR(imx6_pcie->pcie_bus);
1087 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
1088 if (IS_ERR(imx6_pcie->pcie)) {
1089 dev_err(dev, "pcie clock source missing or invalid\n");
1090 return PTR_ERR(imx6_pcie->pcie);
1093 switch (imx6_pcie->drvdata->variant) {
1095 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
1096 "pcie_inbound_axi");
1097 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
1098 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
1099 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
1103 imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
1104 if (IS_ERR(imx6_pcie->pcie_aux)) {
1105 dev_err(dev, "pcie_aux clock source missing or invalid\n");
1106 return PTR_ERR(imx6_pcie->pcie_aux);
1110 if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1111 imx6_pcie->controller_id = 1;
1113 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
1115 if (IS_ERR(imx6_pcie->pciephy_reset)) {
1116 dev_err(dev, "Failed to get PCIEPHY reset control\n");
1117 return PTR_ERR(imx6_pcie->pciephy_reset);
1120 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1122 if (IS_ERR(imx6_pcie->apps_reset)) {
1123 dev_err(dev, "Failed to get PCIE APPS reset control\n");
1124 return PTR_ERR(imx6_pcie->apps_reset);
1131 /* Grab turnoff reset */
1132 imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1133 if (IS_ERR(imx6_pcie->turnoff_reset)) {
1134 dev_err(dev, "Failed to get TURNOFF reset control\n");
1135 return PTR_ERR(imx6_pcie->turnoff_reset);
1138 /* Grab GPR config register range */
1139 imx6_pcie->iomuxc_gpr =
1140 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
1141 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
1142 dev_err(dev, "unable to find iomuxc registers\n");
1143 return PTR_ERR(imx6_pcie->iomuxc_gpr);
1146 /* Grab PCIe PHY Tx Settings */
1147 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1148 &imx6_pcie->tx_deemph_gen1))
1149 imx6_pcie->tx_deemph_gen1 = 0;
1151 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1152 &imx6_pcie->tx_deemph_gen2_3p5db))
1153 imx6_pcie->tx_deemph_gen2_3p5db = 0;
1155 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1156 &imx6_pcie->tx_deemph_gen2_6db))
1157 imx6_pcie->tx_deemph_gen2_6db = 20;
1159 if (of_property_read_u32(node, "fsl,tx-swing-full",
1160 &imx6_pcie->tx_swing_full))
1161 imx6_pcie->tx_swing_full = 127;
1163 if (of_property_read_u32(node, "fsl,tx-swing-low",
1164 &imx6_pcie->tx_swing_low))
1165 imx6_pcie->tx_swing_low = 127;
1167 /* Limit link speed */
1168 ret = of_property_read_u32(node, "fsl,max-link-speed",
1169 &imx6_pcie->link_gen);
1171 imx6_pcie->link_gen = 1;
1173 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1174 if (IS_ERR(imx6_pcie->vpcie)) {
1175 if (PTR_ERR(imx6_pcie->vpcie) != -ENODEV)
1176 return PTR_ERR(imx6_pcie->vpcie);
1177 imx6_pcie->vpcie = NULL;
1180 platform_set_drvdata(pdev, imx6_pcie);
1182 ret = imx6_pcie_attach_pd(dev);
1186 ret = imx6_add_pcie_port(imx6_pcie, pdev);
1190 if (pci_msi_enabled()) {
1191 val = dw_pcie_readw_dbi(pci, PCIE_RC_IMX6_MSI_CAP +
1193 val |= PCI_MSI_FLAGS_ENABLE;
1194 dw_pcie_writew_dbi(pci, PCIE_RC_IMX6_MSI_CAP + PCI_MSI_FLAGS,
1201 static void imx6_pcie_shutdown(struct platform_device *pdev)
1203 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1205 /* bring down link, so bootloader gets clean state in case of reboot */
1206 imx6_pcie_assert_core_reset(imx6_pcie);
1209 static const struct imx6_pcie_drvdata drvdata[] = {
1212 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1213 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1214 .dbi_length = 0x200,
1218 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1219 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1220 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1224 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1225 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
1229 .flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
1236 static const struct of_device_id imx6_pcie_of_match[] = {
1237 { .compatible = "fsl,imx6q-pcie", .data = &drvdata[IMX6Q], },
1238 { .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1239 { .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1240 { .compatible = "fsl,imx7d-pcie", .data = &drvdata[IMX7D], },
1241 { .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], } ,
1245 static struct platform_driver imx6_pcie_driver = {
1247 .name = "imx6q-pcie",
1248 .of_match_table = imx6_pcie_of_match,
1249 .suppress_bind_attrs = true,
1250 .pm = &imx6_pcie_pm_ops,
1251 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1253 .probe = imx6_pcie_probe,
1254 .shutdown = imx6_pcie_shutdown,
1257 static void imx6_pcie_quirk(struct pci_dev *dev)
1259 struct pci_bus *bus = dev->bus;
1260 struct pcie_port *pp = bus->sysdata;
1262 /* Bus parent is the PCI bridge, its parent is this platform driver */
1263 if (!bus->dev.parent || !bus->dev.parent->parent)
1266 /* Make sure we only quirk devices associated with this driver */
1267 if (bus->dev.parent->parent->driver != &imx6_pcie_driver.driver)
1270 if (pci_is_root_bus(bus)) {
1271 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1272 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1275 * Limit config length to avoid the kernel reading beyond
1276 * the register set and causing an abort on i.MX 6Quad
1278 if (imx6_pcie->drvdata->dbi_length) {
1279 dev->cfg_size = imx6_pcie->drvdata->dbi_length;
1280 dev_info(&dev->dev, "Limiting cfg_size to %d\n",
1285 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_SYNOPSYS, 0xabcd,
1286 PCI_CLASS_BRIDGE_PCI, 8, imx6_pcie_quirk);
1288 static int __init imx6_pcie_init(void)
1292 * Since probe() can be deferred we need to make sure that
1293 * hook_fault_code is not called after __init memory is freed
1294 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1295 * we can install the handler here without risking it
1296 * accessing some uninitialized driver state.
1298 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1299 "external abort on non-linefetch");
1302 return platform_driver_register(&imx6_pcie_driver);
1304 device_initcall(imx6_pcie_init);