phy-uniphier-pcie.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_CLKCTRL 0x0000
  21. #define PORT_SEL_MASK GENMASK(11, 9)
  22. #define PORT_SEL_1 FIELD_PREP(PORT_SEL_MASK, 1)
  23. #define PCL_PHY_TEST_I 0x2000
  24. #define TESTI_DAT_MASK GENMASK(13, 6)
  25. #define TESTI_ADR_MASK GENMASK(5, 1)
  26. #define TESTI_WR_EN BIT(0)
  27. #define TESTIO_PHY_SHIFT 16
  28. #define PCL_PHY_TEST_O 0x2004
  29. #define TESTO_DAT_MASK GENMASK(7, 0)
  30. #define PCL_PHY_RESET 0x200c
  31. #define PCL_PHY_RESET_N_MNMODE BIT(8) /* =1:manual */
  32. #define PCL_PHY_RESET_N BIT(0) /* =1:deasssert */
  33. /* SG */
  34. #define SG_USBPCIESEL 0x590
  35. #define SG_USBPCIESEL_PCIE BIT(0)
  36. /* SC */
  37. #define SC_US3SRCSEL 0x2244
  38. #define SC_US3SRCSEL_2LANE GENMASK(9, 8)
  39. #define PCL_PHY_R00 0
  40. #define RX_EQ_ADJ_EN BIT(3) /* enable for EQ adjustment */
  41. #define PCL_PHY_R06 6
  42. #define RX_EQ_ADJ GENMASK(5, 0) /* EQ adjustment value */
  43. #define RX_EQ_ADJ_VAL 0
  44. #define PCL_PHY_R26 26
  45. #define VCO_CTRL GENMASK(7, 4) /* Tx VCO adjustment value */
  46. #define VCO_CTRL_INIT_VAL 5
  47. #define PCL_PHY_R28 28
  48. #define VCOPLL_CLMP GENMASK(3, 2) /* Tx VCOPLL clamp mode */
  49. #define VCOPLL_CLMP_VAL 0
  50. struct uniphier_pciephy_priv {
  51. void __iomem *base;
  52. struct device *dev;
  53. struct clk *clk, *clk_gio;
  54. struct reset_control *rst, *rst_gio;
  55. const struct uniphier_pciephy_soc_data *data;
  56. };
  57. struct uniphier_pciephy_soc_data {
  58. bool is_legacy;
  59. bool is_dual_phy;
  60. void (*set_phymode)(struct regmap *regmap);
  61. };
  62. static void uniphier_pciephy_testio_write(struct uniphier_pciephy_priv *priv,
  63. int id, u32 data)
  64. {
  65. if (id)
  66. data <<= TESTIO_PHY_SHIFT;
  67. /* need to read TESTO twice after accessing TESTI */
  68. writel(data, priv->base + PCL_PHY_TEST_I);
  69. readl(priv->base + PCL_PHY_TEST_O);
  70. readl(priv->base + PCL_PHY_TEST_O);
  71. }
  72. static u32 uniphier_pciephy_testio_read(struct uniphier_pciephy_priv *priv, int id)
  73. {
  74. u32 val = readl(priv->base + PCL_PHY_TEST_O);
  75. if (id)
  76. val >>= TESTIO_PHY_SHIFT;
  77. return val & TESTO_DAT_MASK;
  78. }
  79. static void uniphier_pciephy_set_param(struct uniphier_pciephy_priv *priv,
  80. int id, u32 reg, u32 mask, u32 param)
  81. {
  82. u32 val;
  83. /* read previous data */
  84. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  85. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  86. uniphier_pciephy_testio_write(priv, id, val);
  87. val = uniphier_pciephy_testio_read(priv, id);
  88. /* update value */
  89. val &= ~mask;
  90. val |= mask & param;
  91. val = FIELD_PREP(TESTI_DAT_MASK, val);
  92. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  93. uniphier_pciephy_testio_write(priv, id, val);
  94. uniphier_pciephy_testio_write(priv, id, val | TESTI_WR_EN);
  95. uniphier_pciephy_testio_write(priv, id, val);
  96. /* read current data as dummy */
  97. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  98. val |= FIELD_PREP(TESTI_ADR_MASK, reg);
  99. uniphier_pciephy_testio_write(priv, id, val);
  100. uniphier_pciephy_testio_read(priv, id);
  101. }
  102. static void uniphier_pciephy_assert(struct uniphier_pciephy_priv *priv)
  103. {
  104. u32 val;
  105. val = readl(priv->base + PCL_PHY_RESET);
  106. val &= ~PCL_PHY_RESET_N;
  107. val |= PCL_PHY_RESET_N_MNMODE;
  108. writel(val, priv->base + PCL_PHY_RESET);
  109. }
  110. static void uniphier_pciephy_deassert(struct uniphier_pciephy_priv *priv)
  111. {
  112. u32 val;
  113. val = readl(priv->base + PCL_PHY_RESET);
  114. val |= PCL_PHY_RESET_N_MNMODE | PCL_PHY_RESET_N;
  115. writel(val, priv->base + PCL_PHY_RESET);
  116. }
  117. static int uniphier_pciephy_init(struct phy *phy)
  118. {
  119. struct uniphier_pciephy_priv *priv = phy_get_drvdata(phy);
  120. u32 val;
  121. int ret, id;
  122. ret = clk_prepare_enable(priv->clk);
  123. if (ret)
  124. return ret;
  125. ret = clk_prepare_enable(priv->clk_gio);
  126. if (ret)
  127. goto out_clk_disable;
  128. ret = reset_control_deassert(priv->rst);
  129. if (ret)
  130. goto out_clk_gio_disable;
  131. ret = reset_control_deassert(priv->rst_gio);
  132. if (ret)
  133. goto out_rst_assert;
  134. /* support only 1 port */
  135. val = readl(priv->base + PCL_PHY_CLKCTRL);
  136. val &= ~PORT_SEL_MASK;
  137. val |= PORT_SEL_1;
  138. writel(val, priv->base + PCL_PHY_CLKCTRL);
  139. /* legacy controller doesn't have phy_reset and parameters */
  140. if (priv->data->is_legacy)
  141. return 0;
  142. for (id = 0; id < (priv->data->is_dual_phy ? 2 : 1); id++) {
  143. uniphier_pciephy_set_param(priv, id, PCL_PHY_R00,
  144. RX_EQ_ADJ_EN, RX_EQ_ADJ_EN);
  145. uniphier_pciephy_set_param(priv, id, PCL_PHY_R06, RX_EQ_ADJ,
  146. FIELD_PREP(RX_EQ_ADJ, RX_EQ_ADJ_VAL));
  147. uniphier_pciephy_set_param(priv, id, PCL_PHY_R26, VCO_CTRL,
  148. FIELD_PREP(VCO_CTRL, VCO_CTRL_INIT_VAL));
  149. uniphier_pciephy_set_param(priv, id, PCL_PHY_R28, VCOPLL_CLMP,
  150. FIELD_PREP(VCOPLL_CLMP, VCOPLL_CLMP_VAL));
  151. }
  152. usleep_range(1, 10);
  153. uniphier_pciephy_deassert(priv);
  154. usleep_range(1, 10);
  155. return 0;
  156. out_rst_assert:
  157. reset_control_assert(priv->rst);
  158. out_clk_gio_disable:
  159. clk_disable_unprepare(priv->clk_gio);
  160. out_clk_disable:
  161. clk_disable_unprepare(priv->clk);
  162. return ret;
  163. }
  164. static int uniphier_pciephy_exit(struct phy *phy)
  165. {
  166. struct uniphier_pciephy_priv *priv = phy_get_drvdata(phy);
  167. if (!priv->data->is_legacy)
  168. uniphier_pciephy_assert(priv);
  169. reset_control_assert(priv->rst_gio);
  170. reset_control_assert(priv->rst);
  171. clk_disable_unprepare(priv->clk_gio);
  172. clk_disable_unprepare(priv->clk);
  173. return 0;
  174. }
  175. static const struct phy_ops uniphier_pciephy_ops = {
  176. .init = uniphier_pciephy_init,
  177. .exit = uniphier_pciephy_exit,
  178. .owner = THIS_MODULE,
  179. };
  180. static int uniphier_pciephy_probe(struct platform_device *pdev)
  181. {
  182. struct uniphier_pciephy_priv *priv;
  183. struct phy_provider *phy_provider;
  184. struct device *dev = &pdev->dev;
  185. struct regmap *regmap;
  186. struct phy *phy;
  187. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  188. if (!priv)
  189. return -ENOMEM;
  190. priv->data = of_device_get_match_data(dev);
  191. if (WARN_ON(!priv->data))
  192. return -EINVAL;
  193. priv->dev = dev;
  194. priv->base = devm_platform_ioremap_resource(pdev, 0);
  195. if (IS_ERR(priv->base))
  196. return PTR_ERR(priv->base);
  197. if (priv->data->is_legacy) {
  198. priv->clk_gio = devm_clk_get(dev, "gio");
  199. if (IS_ERR(priv->clk_gio))
  200. return PTR_ERR(priv->clk_gio);
  201. priv->rst_gio =
  202. devm_reset_control_get_shared(dev, "gio");
  203. if (IS_ERR(priv->rst_gio))
  204. return PTR_ERR(priv->rst_gio);
  205. priv->clk = devm_clk_get(dev, "link");
  206. if (IS_ERR(priv->clk))
  207. return PTR_ERR(priv->clk);
  208. priv->rst = devm_reset_control_get_shared(dev, "link");
  209. if (IS_ERR(priv->rst))
  210. return PTR_ERR(priv->rst);
  211. } else {
  212. priv->clk = devm_clk_get(dev, NULL);
  213. if (IS_ERR(priv->clk))
  214. return PTR_ERR(priv->clk);
  215. priv->rst = devm_reset_control_get_shared(dev, NULL);
  216. if (IS_ERR(priv->rst))
  217. return PTR_ERR(priv->rst);
  218. }
  219. phy = devm_phy_create(dev, dev->of_node, &uniphier_pciephy_ops);
  220. if (IS_ERR(phy))
  221. return PTR_ERR(phy);
  222. regmap = syscon_regmap_lookup_by_phandle(dev->of_node,
  223. "socionext,syscon");
  224. if (!IS_ERR(regmap) && priv->data->set_phymode)
  225. priv->data->set_phymode(regmap);
  226. phy_set_drvdata(phy, priv);
  227. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  228. return PTR_ERR_OR_ZERO(phy_provider);
  229. }
  230. static void uniphier_pciephy_ld20_setmode(struct regmap *regmap)
  231. {
  232. regmap_update_bits(regmap, SG_USBPCIESEL,
  233. SG_USBPCIESEL_PCIE, SG_USBPCIESEL_PCIE);
  234. }
  235. static void uniphier_pciephy_nx1_setmode(struct regmap *regmap)
  236. {
  237. regmap_update_bits(regmap, SC_US3SRCSEL,
  238. SC_US3SRCSEL_2LANE, SC_US3SRCSEL_2LANE);
  239. }
  240. static const struct uniphier_pciephy_soc_data uniphier_pro5_data = {
  241. .is_legacy = true,
  242. };
  243. static const struct uniphier_pciephy_soc_data uniphier_ld20_data = {
  244. .is_legacy = false,
  245. .is_dual_phy = false,
  246. .set_phymode = uniphier_pciephy_ld20_setmode,
  247. };
  248. static const struct uniphier_pciephy_soc_data uniphier_pxs3_data = {
  249. .is_legacy = false,
  250. .is_dual_phy = false,
  251. };
  252. static const struct uniphier_pciephy_soc_data uniphier_nx1_data = {
  253. .is_legacy = false,
  254. .is_dual_phy = true,
  255. .set_phymode = uniphier_pciephy_nx1_setmode,
  256. };
  257. static const struct of_device_id uniphier_pciephy_match[] = {
  258. {
  259. .compatible = "socionext,uniphier-pro5-pcie-phy",
  260. .data = &uniphier_pro5_data,
  261. },
  262. {
  263. .compatible = "socionext,uniphier-ld20-pcie-phy",
  264. .data = &uniphier_ld20_data,
  265. },
  266. {
  267. .compatible = "socionext,uniphier-pxs3-pcie-phy",
  268. .data = &uniphier_pxs3_data,
  269. },
  270. {
  271. .compatible = "socionext,uniphier-nx1-pcie-phy",
  272. .data = &uniphier_nx1_data,
  273. },
  274. { /* sentinel */ },
  275. };
  276. MODULE_DEVICE_TABLE(of, uniphier_pciephy_match);
  277. static struct platform_driver uniphier_pciephy_driver = {
  278. .probe = uniphier_pciephy_probe,
  279. .driver = {
  280. .name = "uniphier-pcie-phy",
  281. .of_match_table = uniphier_pciephy_match,
  282. },
  283. };
  284. module_platform_driver(uniphier_pciephy_driver);
  285. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  286. MODULE_DESCRIPTION("UniPhier PHY driver for PCIe controller");
  287. MODULE_LICENSE("GPL v2");