phy-uniphier-usb3ss.c 8.8 KB

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