phy-histb-combphy.c 6.9 KB

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