phy-uniphier-pcie.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-uniphier-pcie.c - PHY driver for UniPhier PCIe controller
  4. * Copyright 2018, Socionext Inc.
  5. * Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of_device.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <linux/resource.h>
  19. /* PHY */
  20. #define PCL_PHY_TEST_I 0x2000
  21. #define PCL_PHY_TEST_O 0x2004
  22. #define TESTI_DAT_MASK GENMASK(13, 6)
  23. #define TESTI_ADR_MASK GENMASK(5, 1)
  24. #define TESTI_WR_EN BIT(0)
  25. #define PCL_PHY_RESET 0x200c
  26. #define PCL_PHY_RESET_N_MNMODE BIT(8) /* =1:manual */
  27. #define PCL_PHY_RESET_N BIT(0) /* =1:deasssert */
  28. /* SG */
  29. #define SG_USBPCIESEL 0x590
  30. #define SG_USBPCIESEL_PCIE BIT(0)
  31. #define PCL_PHY_R00 0
  32. #define RX_EQ_ADJ_EN BIT(3) /* enable for EQ adjustment */
  33. #define PCL_PHY_R06 6
  34. #define RX_EQ_ADJ GENMASK(5, 0) /* EQ adjustment value */
  35. #define RX_EQ_ADJ_VAL 0
  36. #define PCL_PHY_R26 26
  37. #define VCO_CTRL GENMASK(7, 4) /* Tx VCO adjustment value */
  38. #define VCO_CTRL_INIT_VAL 5
  39. struct uniphier_pciephy_priv {
  40. void __iomem *base;
  41. struct device *dev;
  42. struct clk *clk;
  43. struct reset_control *rst;
  44. const struct uniphier_pciephy_soc_data *data;
  45. };
  46. struct uniphier_pciephy_soc_data {
  47. bool has_syscon;
  48. };
  49. static void uniphier_pciephy_testio_write(struct uniphier_pciephy_priv *priv,
  50. u32 data)
  51. {
  52. /* need to read TESTO twice after accessing TESTI */
  53. writel(data, priv->base + PCL_PHY_TEST_I);
  54. readl(priv->base + PCL_PHY_TEST_O);
  55. readl(priv->base + PCL_PHY_TEST_O);
  56. }
  57. static void uniphier_pciephy_set_param(struct uniphier_pciephy_priv *priv,
  58. u32 reg, u32 mask, u32 param)
  59. {
  60. u32 val;
  61. /* read previous data */
  62. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  63. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  64. uniphier_pciephy_testio_write(priv, val);
  65. val = readl(priv->base + PCL_PHY_TEST_O);
  66. /* update value */
  67. val &= ~FIELD_PREP(TESTI_DAT_MASK, mask);
  68. val = FIELD_PREP(TESTI_DAT_MASK, mask & param);
  69. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  70. uniphier_pciephy_testio_write(priv, val);
  71. uniphier_pciephy_testio_write(priv, val | TESTI_WR_EN);
  72. uniphier_pciephy_testio_write(priv, val);
  73. /* read current data as dummy */
  74. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  75. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  76. uniphier_pciephy_testio_write(priv, val);
  77. readl(priv->base + PCL_PHY_TEST_O);
  78. }
  79. static void uniphier_pciephy_assert(struct uniphier_pciephy_priv *priv)
  80. {
  81. u32 val;
  82. val = readl(priv->base + PCL_PHY_RESET);
  83. val &= ~PCL_PHY_RESET_N;
  84. val |= PCL_PHY_RESET_N_MNMODE;
  85. writel(val, priv->base + PCL_PHY_RESET);
  86. }
  87. static void uniphier_pciephy_deassert(struct uniphier_pciephy_priv *priv)
  88. {
  89. u32 val;
  90. val = readl(priv->base + PCL_PHY_RESET);
  91. val |= PCL_PHY_RESET_N_MNMODE | PCL_PHY_RESET_N;
  92. writel(val, priv->base + PCL_PHY_RESET);
  93. }
  94. static int uniphier_pciephy_init(struct phy *phy)
  95. {
  96. struct uniphier_pciephy_priv *priv = phy_get_drvdata(phy);
  97. int ret;
  98. ret = clk_prepare_enable(priv->clk);
  99. if (ret)
  100. return ret;
  101. ret = reset_control_deassert(priv->rst);
  102. if (ret)
  103. goto out_clk_disable;
  104. uniphier_pciephy_set_param(priv, PCL_PHY_R00,
  105. RX_EQ_ADJ_EN, RX_EQ_ADJ_EN);
  106. uniphier_pciephy_set_param(priv, PCL_PHY_R06, RX_EQ_ADJ,
  107. FIELD_PREP(RX_EQ_ADJ, RX_EQ_ADJ_VAL));
  108. uniphier_pciephy_set_param(priv, PCL_PHY_R26, VCO_CTRL,
  109. FIELD_PREP(VCO_CTRL, VCO_CTRL_INIT_VAL));
  110. usleep_range(1, 10);
  111. uniphier_pciephy_deassert(priv);
  112. usleep_range(1, 10);
  113. return 0;
  114. out_clk_disable:
  115. clk_disable_unprepare(priv->clk);
  116. return ret;
  117. }
  118. static int uniphier_pciephy_exit(struct phy *phy)
  119. {
  120. struct uniphier_pciephy_priv *priv = phy_get_drvdata(phy);
  121. uniphier_pciephy_assert(priv);
  122. reset_control_assert(priv->rst);
  123. clk_disable_unprepare(priv->clk);
  124. return 0;
  125. }
  126. static const struct phy_ops uniphier_pciephy_ops = {
  127. .init = uniphier_pciephy_init,
  128. .exit = uniphier_pciephy_exit,
  129. .owner = THIS_MODULE,
  130. };
  131. static int uniphier_pciephy_probe(struct platform_device *pdev)
  132. {
  133. struct uniphier_pciephy_priv *priv;
  134. struct phy_provider *phy_provider;
  135. struct device *dev = &pdev->dev;
  136. struct regmap *regmap;
  137. struct resource *res;
  138. struct phy *phy;
  139. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  140. if (!priv)
  141. return -ENOMEM;
  142. priv->data = of_device_get_match_data(dev);
  143. if (WARN_ON(!priv->data))
  144. return -EINVAL;
  145. priv->dev = dev;
  146. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. priv->base = devm_ioremap_resource(dev, res);
  148. if (IS_ERR(priv->base))
  149. return PTR_ERR(priv->base);
  150. priv->clk = devm_clk_get(dev, NULL);
  151. if (IS_ERR(priv->clk))
  152. return PTR_ERR(priv->clk);
  153. priv->rst = devm_reset_control_get_shared(dev, NULL);
  154. if (IS_ERR(priv->rst))
  155. return PTR_ERR(priv->rst);
  156. phy = devm_phy_create(dev, dev->of_node, &uniphier_pciephy_ops);
  157. if (IS_ERR(phy))
  158. return PTR_ERR(phy);
  159. regmap = syscon_regmap_lookup_by_phandle(dev->of_node,
  160. "socionext,syscon");
  161. if (!IS_ERR(regmap) && priv->data->has_syscon)
  162. regmap_update_bits(regmap, SG_USBPCIESEL,
  163. SG_USBPCIESEL_PCIE, SG_USBPCIESEL_PCIE);
  164. phy_set_drvdata(phy, priv);
  165. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  166. return PTR_ERR_OR_ZERO(phy_provider);
  167. }
  168. static const struct uniphier_pciephy_soc_data uniphier_ld20_data = {
  169. .has_syscon = true,
  170. };
  171. static const struct uniphier_pciephy_soc_data uniphier_pxs3_data = {
  172. .has_syscon = false,
  173. };
  174. static const struct of_device_id uniphier_pciephy_match[] = {
  175. {
  176. .compatible = "socionext,uniphier-ld20-pcie-phy",
  177. .data = &uniphier_ld20_data,
  178. },
  179. {
  180. .compatible = "socionext,uniphier-pxs3-pcie-phy",
  181. .data = &uniphier_pxs3_data,
  182. },
  183. { /* sentinel */ },
  184. };
  185. MODULE_DEVICE_TABLE(of, uniphier_pciephy_match);
  186. static struct platform_driver uniphier_pciephy_driver = {
  187. .probe = uniphier_pciephy_probe,
  188. .driver = {
  189. .name = "uniphier-pcie-phy",
  190. .of_match_table = uniphier_pciephy_match,
  191. },
  192. };
  193. module_platform_driver(uniphier_pciephy_driver);
  194. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  195. MODULE_DESCRIPTION("UniPhier PHY driver for PCIe controller");
  196. MODULE_LICENSE("GPL v2");