phy-fsl-imx8m-pcie.c 7.3 KB

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