phy-meson-axg-pcie.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Amlogic AXG PCIE PHY driver
  4. *
  5. * Copyright (C) 2020 Remi Pommarel <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/phy/phy.h>
  9. #include <linux/regmap.h>
  10. #include <linux/reset.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/bitfield.h>
  13. #include <dt-bindings/phy/phy.h>
  14. #define MESON_PCIE_REG0 0x00
  15. #define MESON_PCIE_COMMON_CLK BIT(4)
  16. #define MESON_PCIE_PORT_SEL GENMASK(3, 2)
  17. #define MESON_PCIE_CLK BIT(1)
  18. #define MESON_PCIE_POWERDOWN BIT(0)
  19. #define MESON_PCIE_TWO_X1 FIELD_PREP(MESON_PCIE_PORT_SEL, 0x3)
  20. #define MESON_PCIE_COMMON_REF_CLK FIELD_PREP(MESON_PCIE_COMMON_CLK, 0x1)
  21. #define MESON_PCIE_PHY_INIT (MESON_PCIE_TWO_X1 | \
  22. MESON_PCIE_COMMON_REF_CLK)
  23. #define MESON_PCIE_RESET_DELAY 500
  24. struct phy_axg_pcie_priv {
  25. struct phy *phy;
  26. struct phy *analog;
  27. struct regmap *regmap;
  28. struct reset_control *reset;
  29. };
  30. static const struct regmap_config phy_axg_pcie_regmap_conf = {
  31. .reg_bits = 8,
  32. .val_bits = 32,
  33. .reg_stride = 4,
  34. .max_register = MESON_PCIE_REG0,
  35. };
  36. static int phy_axg_pcie_power_on(struct phy *phy)
  37. {
  38. struct phy_axg_pcie_priv *priv = phy_get_drvdata(phy);
  39. int ret;
  40. ret = phy_power_on(priv->analog);
  41. if (ret != 0)
  42. return ret;
  43. regmap_update_bits(priv->regmap, MESON_PCIE_REG0,
  44. MESON_PCIE_POWERDOWN, 0);
  45. return 0;
  46. }
  47. static int phy_axg_pcie_power_off(struct phy *phy)
  48. {
  49. struct phy_axg_pcie_priv *priv = phy_get_drvdata(phy);
  50. int ret;
  51. ret = phy_power_off(priv->analog);
  52. if (ret != 0)
  53. return ret;
  54. regmap_update_bits(priv->regmap, MESON_PCIE_REG0,
  55. MESON_PCIE_POWERDOWN, 1);
  56. return 0;
  57. }
  58. static int phy_axg_pcie_init(struct phy *phy)
  59. {
  60. struct phy_axg_pcie_priv *priv = phy_get_drvdata(phy);
  61. int ret;
  62. ret = phy_init(priv->analog);
  63. if (ret != 0)
  64. return ret;
  65. regmap_write(priv->regmap, MESON_PCIE_REG0, MESON_PCIE_PHY_INIT);
  66. return reset_control_reset(priv->reset);
  67. }
  68. static int phy_axg_pcie_exit(struct phy *phy)
  69. {
  70. struct phy_axg_pcie_priv *priv = phy_get_drvdata(phy);
  71. int ret;
  72. ret = phy_exit(priv->analog);
  73. if (ret != 0)
  74. return ret;
  75. return reset_control_reset(priv->reset);
  76. }
  77. static int phy_axg_pcie_reset(struct phy *phy)
  78. {
  79. struct phy_axg_pcie_priv *priv = phy_get_drvdata(phy);
  80. int ret = 0;
  81. ret = phy_reset(priv->analog);
  82. if (ret != 0)
  83. goto out;
  84. ret = reset_control_assert(priv->reset);
  85. if (ret != 0)
  86. goto out;
  87. udelay(MESON_PCIE_RESET_DELAY);
  88. ret = reset_control_deassert(priv->reset);
  89. if (ret != 0)
  90. goto out;
  91. udelay(MESON_PCIE_RESET_DELAY);
  92. out:
  93. return ret;
  94. }
  95. static const struct phy_ops phy_axg_pcie_ops = {
  96. .init = phy_axg_pcie_init,
  97. .exit = phy_axg_pcie_exit,
  98. .power_on = phy_axg_pcie_power_on,
  99. .power_off = phy_axg_pcie_power_off,
  100. .reset = phy_axg_pcie_reset,
  101. .owner = THIS_MODULE,
  102. };
  103. static int phy_axg_pcie_probe(struct platform_device *pdev)
  104. {
  105. struct phy_provider *pphy;
  106. struct device *dev = &pdev->dev;
  107. struct phy_axg_pcie_priv *priv;
  108. struct device_node *np = dev->of_node;
  109. void __iomem *base;
  110. int ret;
  111. priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
  112. if (!priv)
  113. return -ENOMEM;
  114. priv->phy = devm_phy_create(dev, np, &phy_axg_pcie_ops);
  115. if (IS_ERR(priv->phy)) {
  116. ret = PTR_ERR(priv->phy);
  117. if (ret != -EPROBE_DEFER)
  118. dev_err(dev, "failed to create PHY\n");
  119. return ret;
  120. }
  121. base = devm_platform_ioremap_resource(pdev, 0);
  122. if (IS_ERR(base))
  123. return PTR_ERR(base);
  124. priv->regmap = devm_regmap_init_mmio(dev, base,
  125. &phy_axg_pcie_regmap_conf);
  126. if (IS_ERR(priv->regmap))
  127. return PTR_ERR(priv->regmap);
  128. priv->reset = devm_reset_control_array_get_exclusive(dev);
  129. if (IS_ERR(priv->reset))
  130. return PTR_ERR(priv->reset);
  131. priv->analog = devm_phy_get(dev, "analog");
  132. if (IS_ERR(priv->analog))
  133. return PTR_ERR(priv->analog);
  134. phy_set_drvdata(priv->phy, priv);
  135. dev_set_drvdata(dev, priv);
  136. pphy = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  137. return PTR_ERR_OR_ZERO(pphy);
  138. }
  139. static const struct of_device_id phy_axg_pcie_of_match[] = {
  140. {
  141. .compatible = "amlogic,axg-pcie-phy",
  142. },
  143. { },
  144. };
  145. MODULE_DEVICE_TABLE(of, phy_axg_pcie_of_match);
  146. static struct platform_driver phy_axg_pcie_driver = {
  147. .probe = phy_axg_pcie_probe,
  148. .driver = {
  149. .name = "phy-axg-pcie",
  150. .of_match_table = phy_axg_pcie_of_match,
  151. },
  152. };
  153. module_platform_driver(phy_axg_pcie_driver);
  154. MODULE_AUTHOR("Remi Pommarel <[email protected]>");
  155. MODULE_DESCRIPTION("Amlogic AXG PCIE PHY driver");
  156. MODULE_LICENSE("GPL v2");