phy-uniphier-usb3ss.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-uniphier-usb3ss.c - SS-PHY driver for Socionext UniPhier USB3 controller
  4. * Copyright 2015-2018 Socionext Inc.
  5. * Author:
  6. * Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  7. * Contributors:
  8. * Motoya Tanigawa <tanigawa.motoya@socionext.com>
  9. * Masami Hiramatsu <masami.hiramatsu@linaro.org>
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/bitops.h>
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/reset.h>
  22. #define SSPHY_TESTI 0x0
  23. #define SSPHY_TESTO 0x4
  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 PHY_F(regno, msb, lsb) { (regno), (msb), (lsb) }
  28. #define CDR_CPD_TRIM PHY_F(7, 3, 0) /* RxPLL charge pump current */
  29. #define CDR_CPF_TRIM PHY_F(8, 3, 0) /* RxPLL charge pump current 2 */
  30. #define TX_PLL_TRIM PHY_F(9, 3, 0) /* TxPLL charge pump current */
  31. #define BGAP_TRIM PHY_F(11, 3, 0) /* Bandgap voltage */
  32. #define CDR_TRIM PHY_F(13, 6, 5) /* Clock Data Recovery setting */
  33. #define VCO_CTRL PHY_F(26, 7, 4) /* VCO control */
  34. #define VCOPLL_CTRL PHY_F(27, 2, 0) /* TxPLL VCO tuning */
  35. #define VCOPLL_CM PHY_F(28, 1, 0) /* TxPLL voltage */
  36. #define MAX_PHY_PARAMS 7
  37. struct uniphier_u3ssphy_param {
  38. struct {
  39. int reg_no;
  40. int msb;
  41. int lsb;
  42. } field;
  43. u8 value;
  44. };
  45. struct uniphier_u3ssphy_priv {
  46. struct device *dev;
  47. void __iomem *base;
  48. struct clk *clk, *clk_ext, *clk_parent, *clk_parent_gio;
  49. struct reset_control *rst, *rst_parent, *rst_parent_gio;
  50. struct regulator *vbus;
  51. const struct uniphier_u3ssphy_soc_data *data;
  52. };
  53. struct uniphier_u3ssphy_soc_data {
  54. bool is_legacy;
  55. int nparams;
  56. const struct uniphier_u3ssphy_param param[MAX_PHY_PARAMS];
  57. };
  58. static void uniphier_u3ssphy_testio_write(struct uniphier_u3ssphy_priv *priv,
  59. u32 data)
  60. {
  61. /* need to read TESTO twice after accessing TESTI */
  62. writel(data, priv->base + SSPHY_TESTI);
  63. readl(priv->base + SSPHY_TESTO);
  64. readl(priv->base + SSPHY_TESTO);
  65. }
  66. static void uniphier_u3ssphy_set_param(struct uniphier_u3ssphy_priv *priv,
  67. const struct uniphier_u3ssphy_param *p)
  68. {
  69. u32 val;
  70. u8 field_mask = GENMASK(p->field.msb, p->field.lsb);
  71. u8 data;
  72. /* read previous data */
  73. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  74. val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
  75. uniphier_u3ssphy_testio_write(priv, val);
  76. val = readl(priv->base + SSPHY_TESTO);
  77. /* update value */
  78. val &= ~FIELD_PREP(TESTI_DAT_MASK, field_mask);
  79. data = field_mask & (p->value << p->field.lsb);
  80. val = FIELD_PREP(TESTI_DAT_MASK, data);
  81. val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
  82. uniphier_u3ssphy_testio_write(priv, val);
  83. uniphier_u3ssphy_testio_write(priv, val | TESTI_WR_EN);
  84. uniphier_u3ssphy_testio_write(priv, val);
  85. /* read current data as dummy */
  86. val = FIELD_PREP(TESTI_DAT_MASK, 1);
  87. val |= FIELD_PREP(TESTI_ADR_MASK, p->field.reg_no);
  88. uniphier_u3ssphy_testio_write(priv, val);
  89. readl(priv->base + SSPHY_TESTO);
  90. }
  91. static int uniphier_u3ssphy_power_on(struct phy *phy)
  92. {
  93. struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
  94. int ret;
  95. ret = clk_prepare_enable(priv->clk_ext);
  96. if (ret)
  97. return ret;
  98. ret = clk_prepare_enable(priv->clk);
  99. if (ret)
  100. goto out_clk_ext_disable;
  101. ret = reset_control_deassert(priv->rst);
  102. if (ret)
  103. goto out_clk_disable;
  104. if (priv->vbus) {
  105. ret = regulator_enable(priv->vbus);
  106. if (ret)
  107. goto out_rst_assert;
  108. }
  109. return 0;
  110. out_rst_assert:
  111. reset_control_assert(priv->rst);
  112. out_clk_disable:
  113. clk_disable_unprepare(priv->clk);
  114. out_clk_ext_disable:
  115. clk_disable_unprepare(priv->clk_ext);
  116. return ret;
  117. }
  118. static int uniphier_u3ssphy_power_off(struct phy *phy)
  119. {
  120. struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
  121. if (priv->vbus)
  122. regulator_disable(priv->vbus);
  123. reset_control_assert(priv->rst);
  124. clk_disable_unprepare(priv->clk);
  125. clk_disable_unprepare(priv->clk_ext);
  126. return 0;
  127. }
  128. static int uniphier_u3ssphy_init(struct phy *phy)
  129. {
  130. struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
  131. int i, ret;
  132. ret = clk_prepare_enable(priv->clk_parent);
  133. if (ret)
  134. return ret;
  135. ret = clk_prepare_enable(priv->clk_parent_gio);
  136. if (ret)
  137. goto out_clk_disable;
  138. ret = reset_control_deassert(priv->rst_parent);
  139. if (ret)
  140. goto out_clk_gio_disable;
  141. ret = reset_control_deassert(priv->rst_parent_gio);
  142. if (ret)
  143. goto out_rst_assert;
  144. if (priv->data->is_legacy)
  145. return 0;
  146. for (i = 0; i < priv->data->nparams; i++)
  147. uniphier_u3ssphy_set_param(priv, &priv->data->param[i]);
  148. return 0;
  149. out_rst_assert:
  150. reset_control_assert(priv->rst_parent);
  151. out_clk_gio_disable:
  152. clk_disable_unprepare(priv->clk_parent_gio);
  153. out_clk_disable:
  154. clk_disable_unprepare(priv->clk_parent);
  155. return ret;
  156. }
  157. static int uniphier_u3ssphy_exit(struct phy *phy)
  158. {
  159. struct uniphier_u3ssphy_priv *priv = phy_get_drvdata(phy);
  160. reset_control_assert(priv->rst_parent_gio);
  161. reset_control_assert(priv->rst_parent);
  162. clk_disable_unprepare(priv->clk_parent_gio);
  163. clk_disable_unprepare(priv->clk_parent);
  164. return 0;
  165. }
  166. static const struct phy_ops uniphier_u3ssphy_ops = {
  167. .init = uniphier_u3ssphy_init,
  168. .exit = uniphier_u3ssphy_exit,
  169. .power_on = uniphier_u3ssphy_power_on,
  170. .power_off = uniphier_u3ssphy_power_off,
  171. .owner = THIS_MODULE,
  172. };
  173. static int uniphier_u3ssphy_probe(struct platform_device *pdev)
  174. {
  175. struct device *dev = &pdev->dev;
  176. struct uniphier_u3ssphy_priv *priv;
  177. struct phy_provider *phy_provider;
  178. struct resource *res;
  179. struct phy *phy;
  180. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  181. if (!priv)
  182. return -ENOMEM;
  183. priv->dev = dev;
  184. priv->data = of_device_get_match_data(dev);
  185. if (WARN_ON(!priv->data ||
  186. priv->data->nparams > MAX_PHY_PARAMS))
  187. return -EINVAL;
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. priv->base = devm_ioremap_resource(dev, res);
  190. if (IS_ERR(priv->base))
  191. return PTR_ERR(priv->base);
  192. if (!priv->data->is_legacy) {
  193. priv->clk = devm_clk_get(dev, "phy");
  194. if (IS_ERR(priv->clk))
  195. return PTR_ERR(priv->clk);
  196. priv->clk_ext = devm_clk_get(dev, "phy-ext");
  197. if (IS_ERR(priv->clk_ext)) {
  198. if (PTR_ERR(priv->clk_ext) == -ENOENT)
  199. priv->clk_ext = NULL;
  200. else
  201. return PTR_ERR(priv->clk_ext);
  202. }
  203. priv->rst = devm_reset_control_get_shared(dev, "phy");
  204. if (IS_ERR(priv->rst))
  205. return PTR_ERR(priv->rst);
  206. } else {
  207. priv->clk_parent_gio = devm_clk_get(dev, "gio");
  208. if (IS_ERR(priv->clk_parent_gio))
  209. return PTR_ERR(priv->clk_parent_gio);
  210. priv->rst_parent_gio =
  211. devm_reset_control_get_shared(dev, "gio");
  212. if (IS_ERR(priv->rst_parent_gio))
  213. return PTR_ERR(priv->rst_parent_gio);
  214. }
  215. priv->clk_parent = devm_clk_get(dev, "link");
  216. if (IS_ERR(priv->clk_parent))
  217. return PTR_ERR(priv->clk_parent);
  218. priv->rst_parent = devm_reset_control_get_shared(dev, "link");
  219. if (IS_ERR(priv->rst_parent))
  220. return PTR_ERR(priv->rst_parent);
  221. priv->vbus = devm_regulator_get_optional(dev, "vbus");
  222. if (IS_ERR(priv->vbus)) {
  223. if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
  224. return PTR_ERR(priv->vbus);
  225. priv->vbus = NULL;
  226. }
  227. phy = devm_phy_create(dev, dev->of_node, &uniphier_u3ssphy_ops);
  228. if (IS_ERR(phy))
  229. return PTR_ERR(phy);
  230. phy_set_drvdata(phy, priv);
  231. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  232. return PTR_ERR_OR_ZERO(phy_provider);
  233. }
  234. static const struct uniphier_u3ssphy_soc_data uniphier_pro4_data = {
  235. .is_legacy = true,
  236. };
  237. static const struct uniphier_u3ssphy_soc_data uniphier_pxs2_data = {
  238. .is_legacy = false,
  239. .nparams = 7,
  240. .param = {
  241. { CDR_CPD_TRIM, 10 },
  242. { CDR_CPF_TRIM, 3 },
  243. { TX_PLL_TRIM, 5 },
  244. { BGAP_TRIM, 9 },
  245. { CDR_TRIM, 2 },
  246. { VCOPLL_CTRL, 7 },
  247. { VCOPLL_CM, 1 },
  248. },
  249. };
  250. static const struct uniphier_u3ssphy_soc_data uniphier_ld20_data = {
  251. .is_legacy = false,
  252. .nparams = 3,
  253. .param = {
  254. { CDR_CPD_TRIM, 6 },
  255. { CDR_TRIM, 2 },
  256. { VCO_CTRL, 5 },
  257. },
  258. };
  259. static const struct of_device_id uniphier_u3ssphy_match[] = {
  260. {
  261. .compatible = "socionext,uniphier-pro4-usb3-ssphy",
  262. .data = &uniphier_pro4_data,
  263. },
  264. {
  265. .compatible = "socionext,uniphier-pxs2-usb3-ssphy",
  266. .data = &uniphier_pxs2_data,
  267. },
  268. {
  269. .compatible = "socionext,uniphier-ld20-usb3-ssphy",
  270. .data = &uniphier_ld20_data,
  271. },
  272. {
  273. .compatible = "socionext,uniphier-pxs3-usb3-ssphy",
  274. .data = &uniphier_ld20_data,
  275. },
  276. { /* sentinel */ }
  277. };
  278. MODULE_DEVICE_TABLE(of, uniphier_u3ssphy_match);
  279. static struct platform_driver uniphier_u3ssphy_driver = {
  280. .probe = uniphier_u3ssphy_probe,
  281. .driver = {
  282. .name = "uniphier-usb3-ssphy",
  283. .of_match_table = uniphier_u3ssphy_match,
  284. },
  285. };
  286. module_platform_driver(uniphier_u3ssphy_driver);
  287. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  288. MODULE_DESCRIPTION("UniPhier SS-PHY driver for USB3 controller");
  289. MODULE_LICENSE("GPL v2");