phy-histb-combphy.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * COMBPHY driver for HiSilicon STB SoCs
  4. *
  5. * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
  6. *
  7. * Authors: Jianguo Sun <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/regmap.h>
  18. #include <linux/reset.h>
  19. #include <dt-bindings/phy/phy.h>
  20. #define COMBPHY_MODE_PCIE 0
  21. #define COMBPHY_MODE_USB3 1
  22. #define COMBPHY_MODE_SATA 2
  23. #define COMBPHY_CFG_REG 0x0
  24. #define COMBPHY_BYPASS_CODEC BIT(31)
  25. #define COMBPHY_TEST_WRITE BIT(24)
  26. #define COMBPHY_TEST_DATA_SHIFT 20
  27. #define COMBPHY_TEST_DATA_MASK GENMASK(23, 20)
  28. #define COMBPHY_TEST_ADDR_SHIFT 12
  29. #define COMBPHY_TEST_ADDR_MASK GENMASK(16, 12)
  30. #define COMBPHY_CLKREF_OUT_OEN BIT(0)
  31. struct histb_combphy_mode {
  32. int fixed;
  33. int select;
  34. u32 reg;
  35. u32 shift;
  36. u32 mask;
  37. };
  38. struct histb_combphy_priv {
  39. void __iomem *mmio;
  40. struct regmap *syscon;
  41. struct reset_control *por_rst;
  42. struct clk *ref_clk;
  43. struct phy *phy;
  44. struct histb_combphy_mode mode;
  45. };
  46. static void nano_register_write(struct histb_combphy_priv *priv,
  47. u32 addr, u32 data)
  48. {
  49. void __iomem *reg = priv->mmio + COMBPHY_CFG_REG;
  50. u32 val;
  51. /* Set up address and data for the write */
  52. val = readl(reg);
  53. val &= ~COMBPHY_TEST_ADDR_MASK;
  54. val |= addr << COMBPHY_TEST_ADDR_SHIFT;
  55. val &= ~COMBPHY_TEST_DATA_MASK;
  56. val |= data << COMBPHY_TEST_DATA_SHIFT;
  57. writel(val, reg);
  58. /* Flip strobe control to trigger the write */
  59. val &= ~COMBPHY_TEST_WRITE;
  60. writel(val, reg);
  61. val |= COMBPHY_TEST_WRITE;
  62. writel(val, reg);
  63. }
  64. static int is_mode_fixed(struct histb_combphy_mode *mode)
  65. {
  66. return (mode->fixed != PHY_NONE) ? true : false;
  67. }
  68. static int histb_combphy_set_mode(struct histb_combphy_priv *priv)
  69. {
  70. struct histb_combphy_mode *mode = &priv->mode;
  71. struct regmap *syscon = priv->syscon;
  72. u32 hw_sel;
  73. if (is_mode_fixed(mode))
  74. return 0;
  75. switch (mode->select) {
  76. case PHY_TYPE_SATA:
  77. hw_sel = COMBPHY_MODE_SATA;
  78. break;
  79. case PHY_TYPE_PCIE:
  80. hw_sel = COMBPHY_MODE_PCIE;
  81. break;
  82. case PHY_TYPE_USB3:
  83. hw_sel = COMBPHY_MODE_USB3;
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. return regmap_update_bits(syscon, mode->reg, mode->mask,
  89. hw_sel << mode->shift);
  90. }
  91. static int histb_combphy_init(struct phy *phy)
  92. {
  93. struct histb_combphy_priv *priv = phy_get_drvdata(phy);
  94. u32 val;
  95. int ret;
  96. ret = histb_combphy_set_mode(priv);
  97. if (ret)
  98. return ret;
  99. /* Clear bypass bit to enable encoding/decoding */
  100. val = readl(priv->mmio + COMBPHY_CFG_REG);
  101. val &= ~COMBPHY_BYPASS_CODEC;
  102. writel(val, priv->mmio + COMBPHY_CFG_REG);
  103. ret = clk_prepare_enable(priv->ref_clk);
  104. if (ret)
  105. return ret;
  106. reset_control_deassert(priv->por_rst);
  107. /* Enable EP clock */
  108. val = readl(priv->mmio + COMBPHY_CFG_REG);
  109. val |= COMBPHY_CLKREF_OUT_OEN;
  110. writel(val, priv->mmio + COMBPHY_CFG_REG);
  111. /* Need to wait for EP clock stable */
  112. mdelay(5);
  113. /* Configure nano phy registers as suggested by vendor */
  114. nano_register_write(priv, 0x1, 0x8);
  115. nano_register_write(priv, 0xc, 0x9);
  116. nano_register_write(priv, 0x1a, 0x4);
  117. return 0;
  118. }
  119. static int histb_combphy_exit(struct phy *phy)
  120. {
  121. struct histb_combphy_priv *priv = phy_get_drvdata(phy);
  122. u32 val;
  123. /* Disable EP clock */
  124. val = readl(priv->mmio + COMBPHY_CFG_REG);
  125. val &= ~COMBPHY_CLKREF_OUT_OEN;
  126. writel(val, priv->mmio + COMBPHY_CFG_REG);
  127. reset_control_assert(priv->por_rst);
  128. clk_disable_unprepare(priv->ref_clk);
  129. return 0;
  130. }
  131. static const struct phy_ops histb_combphy_ops = {
  132. .init = histb_combphy_init,
  133. .exit = histb_combphy_exit,
  134. .owner = THIS_MODULE,
  135. };
  136. static struct phy *histb_combphy_xlate(struct device *dev,
  137. struct of_phandle_args *args)
  138. {
  139. struct histb_combphy_priv *priv = dev_get_drvdata(dev);
  140. struct histb_combphy_mode *mode = &priv->mode;
  141. if (args->args_count < 1) {
  142. dev_err(dev, "invalid number of arguments\n");
  143. return ERR_PTR(-EINVAL);
  144. }
  145. mode->select = args->args[0];
  146. if (mode->select < PHY_TYPE_SATA || mode->select > PHY_TYPE_USB3) {
  147. dev_err(dev, "invalid phy mode select argument\n");
  148. return ERR_PTR(-EINVAL);
  149. }
  150. if (is_mode_fixed(mode) && mode->select != mode->fixed) {
  151. dev_err(dev, "mode select %d mismatch fixed phy mode %d\n",
  152. mode->select, mode->fixed);
  153. return ERR_PTR(-EINVAL);
  154. }
  155. return priv->phy;
  156. }
  157. static int histb_combphy_probe(struct platform_device *pdev)
  158. {
  159. struct phy_provider *phy_provider;
  160. struct device *dev = &pdev->dev;
  161. struct histb_combphy_priv *priv;
  162. struct device_node *np = dev->of_node;
  163. struct histb_combphy_mode *mode;
  164. u32 vals[3];
  165. int ret;
  166. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  167. if (!priv)
  168. return -ENOMEM;
  169. priv->mmio = devm_platform_ioremap_resource(pdev, 0);
  170. if (IS_ERR(priv->mmio)) {
  171. ret = PTR_ERR(priv->mmio);
  172. return ret;
  173. }
  174. priv->syscon = syscon_node_to_regmap(np->parent);
  175. if (IS_ERR(priv->syscon)) {
  176. dev_err(dev, "failed to find peri_ctrl syscon regmap\n");
  177. return PTR_ERR(priv->syscon);
  178. }
  179. mode = &priv->mode;
  180. mode->fixed = PHY_NONE;
  181. ret = of_property_read_u32(np, "hisilicon,fixed-mode", &mode->fixed);
  182. if (ret == 0)
  183. dev_dbg(dev, "found fixed phy mode %d\n", mode->fixed);
  184. ret = of_property_read_u32_array(np, "hisilicon,mode-select-bits",
  185. vals, ARRAY_SIZE(vals));
  186. if (ret == 0) {
  187. if (is_mode_fixed(mode)) {
  188. dev_err(dev, "found select bits for fixed mode phy\n");
  189. return -EINVAL;
  190. }
  191. mode->reg = vals[0];
  192. mode->shift = vals[1];
  193. mode->mask = vals[2];
  194. dev_dbg(dev, "found mode select bits\n");
  195. } else {
  196. if (!is_mode_fixed(mode)) {
  197. dev_err(dev, "no valid select bits found for non-fixed phy\n");
  198. return -ENODEV;
  199. }
  200. }
  201. priv->ref_clk = devm_clk_get(dev, NULL);
  202. if (IS_ERR(priv->ref_clk)) {
  203. dev_err(dev, "failed to find ref clock\n");
  204. return PTR_ERR(priv->ref_clk);
  205. }
  206. priv->por_rst = devm_reset_control_get(dev, NULL);
  207. if (IS_ERR(priv->por_rst)) {
  208. dev_err(dev, "failed to get poweron reset\n");
  209. return PTR_ERR(priv->por_rst);
  210. }
  211. priv->phy = devm_phy_create(dev, NULL, &histb_combphy_ops);
  212. if (IS_ERR(priv->phy)) {
  213. dev_err(dev, "failed to create combphy\n");
  214. return PTR_ERR(priv->phy);
  215. }
  216. dev_set_drvdata(dev, priv);
  217. phy_set_drvdata(priv->phy, priv);
  218. phy_provider = devm_of_phy_provider_register(dev, histb_combphy_xlate);
  219. return PTR_ERR_OR_ZERO(phy_provider);
  220. }
  221. static const struct of_device_id histb_combphy_of_match[] = {
  222. { .compatible = "hisilicon,hi3798cv200-combphy" },
  223. { },
  224. };
  225. MODULE_DEVICE_TABLE(of, histb_combphy_of_match);
  226. static struct platform_driver histb_combphy_driver = {
  227. .probe = histb_combphy_probe,
  228. .driver = {
  229. .name = "combphy",
  230. .of_match_table = histb_combphy_of_match,
  231. },
  232. };
  233. module_platform_driver(histb_combphy_driver);
  234. MODULE_DESCRIPTION("HiSilicon STB COMBPHY driver");
  235. MODULE_LICENSE("GPL v2");