Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright 2021 NXP
0004  */
0005 
0006 #include <linux/bitfield.h>
0007 #include <linux/clk.h>
0008 #include <linux/delay.h>
0009 #include <linux/io.h>
0010 #include <linux/iopoll.h>
0011 #include <linux/mfd/syscon.h>
0012 #include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
0013 #include <linux/module.h>
0014 #include <linux/phy/phy.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/regmap.h>
0017 #include <linux/reset.h>
0018 
0019 #include <dt-bindings/phy/phy-imx8-pcie.h>
0020 
0021 #define IMX8MM_PCIE_PHY_CMN_REG061  0x184
0022 #define  ANA_PLL_CLK_OUT_TO_EXT_IO_EN   BIT(0)
0023 #define IMX8MM_PCIE_PHY_CMN_REG062  0x188
0024 #define  ANA_PLL_CLK_OUT_TO_EXT_IO_SEL  BIT(3)
0025 #define IMX8MM_PCIE_PHY_CMN_REG063  0x18C
0026 #define  AUX_PLL_REFCLK_SEL_SYS_PLL GENMASK(7, 6)
0027 #define IMX8MM_PCIE_PHY_CMN_REG064  0x190
0028 #define  ANA_AUX_RX_TX_SEL_TX       BIT(7)
0029 #define  ANA_AUX_RX_TERM_GND_EN     BIT(3)
0030 #define  ANA_AUX_TX_TERM        BIT(2)
0031 #define IMX8MM_PCIE_PHY_CMN_REG065  0x194
0032 #define  ANA_AUX_RX_TERM        (BIT(7) | BIT(4))
0033 #define  ANA_AUX_TX_LVL         GENMASK(3, 0)
0034 #define IMX8MM_PCIE_PHY_CMN_REG75   0x1D4
0035 #define  PCIE_PHY_CMN_REG75_PLL_DONE    0x3
0036 #define PCIE_PHY_TRSV_REG5      0x414
0037 #define  PCIE_PHY_TRSV_REG5_GEN1_DEEMP  0x2D
0038 #define PCIE_PHY_TRSV_REG6      0x418
0039 #define  PCIE_PHY_TRSV_REG6_GEN2_DEEMP  0xF
0040 
0041 #define IMX8MM_GPR_PCIE_REF_CLK_SEL GENMASK(25, 24)
0042 #define IMX8MM_GPR_PCIE_REF_CLK_PLL FIELD_PREP(IMX8MM_GPR_PCIE_REF_CLK_SEL, 0x3)
0043 #define IMX8MM_GPR_PCIE_REF_CLK_EXT FIELD_PREP(IMX8MM_GPR_PCIE_REF_CLK_SEL, 0x2)
0044 #define IMX8MM_GPR_PCIE_AUX_EN      BIT(19)
0045 #define IMX8MM_GPR_PCIE_CMN_RST     BIT(18)
0046 #define IMX8MM_GPR_PCIE_POWER_OFF   BIT(17)
0047 #define IMX8MM_GPR_PCIE_SSC_EN      BIT(16)
0048 #define IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE BIT(9)
0049 
0050 struct imx8_pcie_phy {
0051     void __iomem        *base;
0052     struct clk      *clk;
0053     struct phy      *phy;
0054     struct regmap       *iomuxc_gpr;
0055     struct reset_control    *reset;
0056     u32         refclk_pad_mode;
0057     u32         tx_deemph_gen1;
0058     u32         tx_deemph_gen2;
0059     bool            clkreq_unused;
0060 };
0061 
0062 static int imx8_pcie_phy_init(struct phy *phy)
0063 {
0064     int ret;
0065     u32 val, pad_mode;
0066     struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy);
0067 
0068     reset_control_assert(imx8_phy->reset);
0069 
0070     pad_mode = imx8_phy->refclk_pad_mode;
0071     /* Set AUX_EN_OVERRIDE 1'b0, when the CLKREQ# isn't hooked */
0072     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0073                IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE,
0074                imx8_phy->clkreq_unused ?
0075                0 : IMX8MM_GPR_PCIE_AUX_EN_OVERRIDE);
0076     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0077                IMX8MM_GPR_PCIE_AUX_EN,
0078                IMX8MM_GPR_PCIE_AUX_EN);
0079     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0080                IMX8MM_GPR_PCIE_POWER_OFF, 0);
0081     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0082                IMX8MM_GPR_PCIE_SSC_EN, 0);
0083 
0084     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0085                IMX8MM_GPR_PCIE_REF_CLK_SEL,
0086                pad_mode == IMX8_PCIE_REFCLK_PAD_INPUT ?
0087                IMX8MM_GPR_PCIE_REF_CLK_EXT :
0088                IMX8MM_GPR_PCIE_REF_CLK_PLL);
0089     usleep_range(100, 200);
0090 
0091     /* Do the PHY common block reset */
0092     regmap_update_bits(imx8_phy->iomuxc_gpr, IOMUXC_GPR14,
0093                IMX8MM_GPR_PCIE_CMN_RST,
0094                IMX8MM_GPR_PCIE_CMN_RST);
0095     usleep_range(200, 500);
0096 
0097     if (pad_mode == IMX8_PCIE_REFCLK_PAD_INPUT ||
0098         pad_mode == IMX8_PCIE_REFCLK_PAD_UNUSED) {
0099         /* Configure the pad as input */
0100         val = readl(imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061);
0101         writel(val & ~ANA_PLL_CLK_OUT_TO_EXT_IO_EN,
0102                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061);
0103     } else {
0104         /* Configure the PHY to output the refclock via pad */
0105         writel(ANA_PLL_CLK_OUT_TO_EXT_IO_EN,
0106                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG061);
0107     }
0108 
0109     if (pad_mode == IMX8_PCIE_REFCLK_PAD_OUTPUT ||
0110         pad_mode == IMX8_PCIE_REFCLK_PAD_UNUSED) {
0111         /* Source clock from SoC internal PLL */
0112         writel(ANA_PLL_CLK_OUT_TO_EXT_IO_SEL,
0113                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG062);
0114         writel(AUX_PLL_REFCLK_SEL_SYS_PLL,
0115                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG063);
0116         val = ANA_AUX_RX_TX_SEL_TX | ANA_AUX_TX_TERM;
0117         writel(val | ANA_AUX_RX_TERM_GND_EN,
0118                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG064);
0119         writel(ANA_AUX_RX_TERM | ANA_AUX_TX_LVL,
0120                imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG065);
0121     }
0122 
0123     /* Tune PHY de-emphasis setting to pass PCIe compliance. */
0124     if (imx8_phy->tx_deemph_gen1)
0125         writel(imx8_phy->tx_deemph_gen1,
0126                imx8_phy->base + PCIE_PHY_TRSV_REG5);
0127     if (imx8_phy->tx_deemph_gen2)
0128         writel(imx8_phy->tx_deemph_gen2,
0129                imx8_phy->base + PCIE_PHY_TRSV_REG6);
0130 
0131     reset_control_deassert(imx8_phy->reset);
0132 
0133     /* Polling to check the phy is ready or not. */
0134     ret = readl_poll_timeout(imx8_phy->base + IMX8MM_PCIE_PHY_CMN_REG75,
0135                  val, val == PCIE_PHY_CMN_REG75_PLL_DONE,
0136                  10, 20000);
0137     return ret;
0138 }
0139 
0140 static int imx8_pcie_phy_power_on(struct phy *phy)
0141 {
0142     struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy);
0143 
0144     return clk_prepare_enable(imx8_phy->clk);
0145 }
0146 
0147 static int imx8_pcie_phy_power_off(struct phy *phy)
0148 {
0149     struct imx8_pcie_phy *imx8_phy = phy_get_drvdata(phy);
0150 
0151     clk_disable_unprepare(imx8_phy->clk);
0152 
0153     return 0;
0154 }
0155 
0156 static const struct phy_ops imx8_pcie_phy_ops = {
0157     .init       = imx8_pcie_phy_init,
0158     .power_on   = imx8_pcie_phy_power_on,
0159     .power_off  = imx8_pcie_phy_power_off,
0160     .owner      = THIS_MODULE,
0161 };
0162 
0163 static int imx8_pcie_phy_probe(struct platform_device *pdev)
0164 {
0165     struct phy_provider *phy_provider;
0166     struct device *dev = &pdev->dev;
0167     struct device_node *np = dev->of_node;
0168     struct imx8_pcie_phy *imx8_phy;
0169     struct resource *res;
0170 
0171     imx8_phy = devm_kzalloc(dev, sizeof(*imx8_phy), GFP_KERNEL);
0172     if (!imx8_phy)
0173         return -ENOMEM;
0174 
0175     /* get PHY refclk pad mode */
0176     of_property_read_u32(np, "fsl,refclk-pad-mode",
0177                  &imx8_phy->refclk_pad_mode);
0178 
0179     if (of_property_read_u32(np, "fsl,tx-deemph-gen1",
0180                  &imx8_phy->tx_deemph_gen1))
0181         imx8_phy->tx_deemph_gen1 = 0;
0182 
0183     if (of_property_read_u32(np, "fsl,tx-deemph-gen2",
0184                  &imx8_phy->tx_deemph_gen2))
0185         imx8_phy->tx_deemph_gen2 = 0;
0186 
0187     if (of_property_read_bool(np, "fsl,clkreq-unsupported"))
0188         imx8_phy->clkreq_unused = true;
0189     else
0190         imx8_phy->clkreq_unused = false;
0191 
0192     imx8_phy->clk = devm_clk_get(dev, "ref");
0193     if (IS_ERR(imx8_phy->clk)) {
0194         dev_err(dev, "failed to get imx pcie phy clock\n");
0195         return PTR_ERR(imx8_phy->clk);
0196     }
0197 
0198     /* Grab GPR config register range */
0199     imx8_phy->iomuxc_gpr =
0200          syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
0201     if (IS_ERR(imx8_phy->iomuxc_gpr)) {
0202         dev_err(dev, "unable to find iomuxc registers\n");
0203         return PTR_ERR(imx8_phy->iomuxc_gpr);
0204     }
0205 
0206     imx8_phy->reset = devm_reset_control_get_exclusive(dev, "pciephy");
0207     if (IS_ERR(imx8_phy->reset)) {
0208         dev_err(dev, "Failed to get PCIEPHY reset control\n");
0209         return PTR_ERR(imx8_phy->reset);
0210     }
0211 
0212     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0213     imx8_phy->base = devm_ioremap_resource(dev, res);
0214     if (IS_ERR(imx8_phy->base))
0215         return PTR_ERR(imx8_phy->base);
0216 
0217     imx8_phy->phy = devm_phy_create(dev, NULL, &imx8_pcie_phy_ops);
0218     if (IS_ERR(imx8_phy->phy))
0219         return PTR_ERR(imx8_phy->phy);
0220 
0221     phy_set_drvdata(imx8_phy->phy, imx8_phy);
0222 
0223     phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
0224 
0225     return PTR_ERR_OR_ZERO(phy_provider);
0226 }
0227 
0228 static const struct of_device_id imx8_pcie_phy_of_match[] = {
0229     {.compatible = "fsl,imx8mm-pcie-phy",},
0230     { },
0231 };
0232 MODULE_DEVICE_TABLE(of, imx8_pcie_phy_of_match);
0233 
0234 static struct platform_driver imx8_pcie_phy_driver = {
0235     .probe  = imx8_pcie_phy_probe,
0236     .driver = {
0237         .name   = "imx8-pcie-phy",
0238         .of_match_table = imx8_pcie_phy_of_match,
0239     }
0240 };
0241 module_platform_driver(imx8_pcie_phy_driver);
0242 
0243 MODULE_DESCRIPTION("FSL IMX8 PCIE PHY driver");
0244 MODULE_LICENSE("GPL v2");