1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright 2021 NXP 4 */ 5 6 #include <linux/bitfield.h> 7 #include <linux/clk.h> 8 #include <linux/delay.h> 9 #include <linux/io.h> 10 #include <linux/iopoll.h> 11 #include <linux/mfd/syscon.h> 12 #include <linux/mfd/syscon/imx7-iomuxc-gpr.h> 13 #include <linux/module.h> 14 #include <linux/phy/phy.h> 15 #include <linux/platform_device.h> 16 #include <linux/regmap.h> 17 #include <linux/reset.h> 18 19 #include <dt-bindings/phy/phy-imx8-pcie.h> 20 21 #define IMX8MM_PCIE_PHY_CMN_REG061 0x184 22 #define ANA_PLL_CLK_OUT_TO_EXT_IO_EN BIT(0) 23 #define IMX8MM_PCIE_PHY_CMN_REG062 0x188 24 #define ANA_PLL_CLK_OUT_TO_EXT_IO_SEL BIT(3) 25 #define IMX8MM_PCIE_PHY_CMN_REG063 0x18C 26 #define AUX_PLL_REFCLK_SEL_SYS_PLL GENMASK(7, 6) 27 #define IMX8MM_PCIE_PHY_CMN_REG064 0x190 28 #define ANA_AUX_RX_TX_SEL_TX BIT(7) 29 #define ANA_AUX_RX_TERM_GND_EN BIT(3) 30 #define ANA_AUX_TX_TERM BIT(2) 31 #define IMX8MM_PCIE_PHY_CMN_REG065 0x194 32 #define ANA_AUX_RX_TERM (BIT(7) | BIT(4)) 33 #define ANA_AUX_TX_LVL GENMASK(3, 0) 34 #define IMX8MM_PCIE_PHY_CMN_REG75 0x1D4 35 #define PCIE_PHY_CMN_REG75_PLL_DONE 0x3 36 #define PCIE_PHY_TRSV_REG5 0x414 37 #define PCIE_PHY_TRSV_REG5_GEN1_DEEMP 0x2D 38 #define PCIE_PHY_TRSV_REG6 0x418 39 #define PCIE_PHY_TRSV_REG6_GEN2_DEEMP 0xF 40 41 #define IMX8MM_GPR_PCIE_REF_CLK_SEL GENMASK(25, 24) 42 #define IMX8MM_GPR_PCIE_REF_CLK_PLL FIELD_PREP(IMX8MM_GPR_PCIE_REF_CLK_SEL, 0x3) 43 #define IMX8MM_GPR_PCIE_REF_CLK_EXT FIELD_PREP(IMX8MM_GPR_PCIE_REF_CLK_SEL, 0x2) 44 #define IMX8MM_GPR_PCIE_AUX_EN BIT(19) 45 #define IMX8MM_GPR_PCIE_CMN_RST BIT(18) 46 #define IMX8MM_GPR_PCIE_POWER_OFF BIT(17) 47 #define IMX8MM_GPR_PCIE_SSC_EN BIT(16) 48 #define IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE BIT(9) 49 50 struct imx8_pcie_phy { 51 void __iomem *base; 52 struct clk *clk; 53 struct phy *phy; 54 struct regmap *iomuxc_gpr; 55 struct reset_control *reset; 56 u32 refclk_pad_mode; 57 u32 tx_deemph_gen1; 58 u32 tx_deemph_gen2; 59 bool clkreq_unused; 60 }; 61 62 static int imx8_pcie_phy_init(struct phy *phy) 63 { 64 int ret; 65 u32 val, pad_mode; 66 struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy); 67 68 reset_control_assert(imx8_phy->reset); 69 70 pad_mode = imx8_phy->refclk_pad_mode; 71 /* Set AUX_EN_OVERRIDE 1'b0, when the CLKREQ# isn't hooked */ 72 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 73 IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE, 74 imx8_phy->clkreq_unused ? 75 0 : IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE); 76 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 77 IMX8MM_GPR_PCIE_AUX_EN, 78 IMX8MM_GPR_PCIE_AUX_EN); 79 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 80 IMX8MM_GPR_PCIE_POWER_OFF, 0); 81 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 82 IMX8MM_GPR_PCIE_SSC_EN, 0); 83 84 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 85 IMX8MM_GPR_PCIE_REF_CLK_SEL, 86 pad_mode == IMX8_PCIE_REFCLK_PAD_INPUT ? 87 IMX8MM_GPR_PCIE_REF_CLK_EXT : 88 IMX8MM_GPR_PCIE_REF_CLK_PLL); 89 usleep_range(100, 200); 90 91 /* Do the PHY common block reset */ 92 regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14, 93 IMX8MM_GPR_PCIE_CMN_RST, 94 IMX8MM_GPR_PCIE_CMN_RST); 95 usleep_range(200, 500); 96 97 if (pad_mode == IMX8_PCIE_REFCLK_PAD_INPUT) { 98 /* Configure the pad as input */ 99 val = readl(imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061); 100 writel(val & ~ANA_PLL_CLK_OUT_TO_EXT_IO_EN, 101 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061); 102 } else if (pad_mode == IMX8_PCIE_REFCLK_PAD_OUTPUT) { 103 /* Configure the PHY to output the refclock via pad */ 104 writel(ANA_PLL_CLK_OUT_TO_EXT_IO_EN, 105 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061); 106 writel(ANA_PLL_CLK_OUT_TO_EXT_IO_SEL, 107 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG062); 108 writel(AUX_PLL_REFCLK_SEL_SYS_PLL, 109 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG063); 110 val = ANA_AUX_RX_TX_SEL_TX | ANA_AUX_TX_TERM; 111 writel(val | ANA_AUX_RX_TERM_GND_EN, 112 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG064); 113 writel(ANA_AUX_RX_TERM | ANA_AUX_TX_LVL, 114 imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG065); 115 } 116 117 /* Tune PHY de-emphasis setting to pass PCIe compliance. */ 118 if (imx8_phy->tx_deemph_gen1) 119 writel(imx8_phy->tx_deemph_gen1, 120 imx8_phy->base + PCIE_PHY_TRSV_REG5); 121 if (imx8_phy->tx_deemph_gen2) 122 writel(imx8_phy->tx_deemph_gen2, 123 imx8_phy->base + PCIE_PHY_TRSV_REG6); 124 125 reset_control_deassert(imx8_phy->reset); 126 127 /* Polling to check the phy is ready or not. */ 128 ret = readl_poll_timeout(imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG75, 129 val, val == PCIE_PHY_CMN_REG75_PLL_DONE, 130 10, 20000); 131 return ret; 132 } 133 134 static int imx8_pcie_phy_power_on(struct phy *phy) 135 { 136 struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy); 137 138 return clk_prepare_enable(imx8_phy->clk); 139 } 140 141 static int imx8_pcie_phy_power_off(struct phy *phy) 142 { 143 struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy); 144 145 clk_disable_unprepare(imx8_phy->clk); 146 147 return 0; 148 } 149 150 static const struct phy_ops imx8_pcie_phy_ops = { 151 .init = imx8_pcie_phy_init, 152 .power_on = imx8_pcie_phy_power_on, 153 .power_off = imx8_pcie_phy_power_off, 154 .owner = THIS_MODULE, 155 }; 156 157 static int imx8_pcie_phy_probe(struct platform_device *pdev) 158 { 159 struct phy_provider *phy_provider; 160 struct device *dev = &pdev->dev; 161 struct device_node *np = dev->of_node; 162 struct imx8_pcie_phy *imx8_phy; 163 struct resource *res; 164 165 imx8_phy = devm_kzalloc(dev, sizeof(*imx8_phy), GFP_KERNEL); 166 if (!imx8_phy) 167 return -ENOMEM; 168 169 /* get PHY refclk pad mode */ 170 of_property_read_u32(np, "fsl,refclk-pad-mode", 171 &imx8_phy->refclk_pad_mode); 172 173 if (of_property_read_u32(np, "fsl,tx-deemph-gen1", 174 &imx8_phy->tx_deemph_gen1)) 175 imx8_phy->tx_deemph_gen1 = 0; 176 177 if (of_property_read_u32(np, "fsl,tx-deemph-gen2", 178 &imx8_phy->tx_deemph_gen2)) 179 imx8_phy->tx_deemph_gen2 = 0; 180 181 if (of_property_read_bool(np, "fsl,clkreq-unsupported")) 182 imx8_phy->clkreq_unused = true; 183 else 184 imx8_phy->clkreq_unused = false; 185 186 imx8_phy->clk = devm_clk_get(dev, "ref"); 187 if (IS_ERR(imx8_phy->clk)) { 188 dev_err(dev, "failed to get imx pcie phy clock\n"); 189 return PTR_ERR(imx8_phy->clk); 190 } 191 192 /* Grab GPR config register range */ 193 imx8_phy->iomuxc_gpr = 194 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr"); 195 if (IS_ERR(imx8_phy->iomuxc_gpr)) { 196 dev_err(dev, "unable to find iomuxc registers\n"); 197 return PTR_ERR(imx8_phy->iomuxc_gpr); 198 } 199 200 imx8_phy->reset = devm_reset_control_get_exclusive(dev, "pciephy"); 201 if (IS_ERR(imx8_phy->reset)) { 202 dev_err(dev, "Failed to get PCIEPHY reset control\n"); 203 return PTR_ERR(imx8_phy->reset); 204 } 205 206 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 207 imx8_phy->base = devm_ioremap_resource(dev, res); 208 if (IS_ERR(imx8_phy->base)) 209 return PTR_ERR(imx8_phy->base); 210 211 imx8_phy->phy = devm_phy_create(dev, NULL, &imx8_pcie_phy_ops); 212 if (IS_ERR(imx8_phy->phy)) 213 return PTR_ERR(imx8_phy->phy); 214 215 phy_set_drvdata(imx8_phy->phy, imx8_phy); 216 217 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 218 219 return PTR_ERR_OR_ZERO(phy_provider); 220 } 221 222 static const struct of_device_id imx8_pcie_phy_of_match[] = { 223 {.compatible = "fsl,imx8mm-pcie-phy",}, 224 { }, 225 }; 226 MODULE_DEVICE_TABLE(of, imx8_pcie_phy_of_match); 227 228 static struct platform_driver imx8_pcie_phy_driver = { 229 .probe = imx8_pcie_phy_probe, 230 .driver = { 231 .name = "imx8-pcie-phy", 232 .of_match_table = imx8_pcie_phy_of_match, 233 } 234 }; 235 module_platform_driver(imx8_pcie_phy_driver); 236 237 MODULE_DESCRIPTION("FSL IMX8 PCIE PHY driver"); 238 MODULE_LICENSE("GPL v2"); 239