phy-uniphier-usb3hs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-uniphier-usb3hs.c - HS-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/nvmem-consumer.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/reset.h>
  23. #include <linux/slab.h>
  24. #define HSPHY_CFG0 0x0
  25. #define HSPHY_CFG0_HS_I_MASK GENMASK(31, 28)
  26. #define HSPHY_CFG0_HSDISC_MASK GENMASK(27, 26)
  27. #define HSPHY_CFG0_SWING_MASK GENMASK(17, 16)
  28. #define HSPHY_CFG0_SEL_T_MASK GENMASK(15, 12)
  29. #define HSPHY_CFG0_RTERM_MASK GENMASK(7, 6)
  30. #define HSPHY_CFG0_TRIMMASK (HSPHY_CFG0_HS_I_MASK \
  31. | HSPHY_CFG0_SEL_T_MASK \
  32. | HSPHY_CFG0_RTERM_MASK)
  33. #define HSPHY_CFG1 0x4
  34. #define HSPHY_CFG1_DAT_EN BIT(29)
  35. #define HSPHY_CFG1_ADR_EN BIT(28)
  36. #define HSPHY_CFG1_ADR_MASK GENMASK(27, 16)
  37. #define HSPHY_CFG1_DAT_MASK GENMASK(23, 16)
  38. #define PHY_F(regno, msb, lsb) { (regno), (msb), (lsb) }
  39. #define LS_SLEW PHY_F(10, 6, 6) /* LS mode slew rate */
  40. #define FS_LS_DRV PHY_F(10, 5, 5) /* FS/LS slew rate */
  41. #define MAX_PHY_PARAMS 2
  42. struct uniphier_u3hsphy_param {
  43. struct {
  44. int reg_no;
  45. int msb;
  46. int lsb;
  47. } field;
  48. u8 value;
  49. };
  50. struct uniphier_u3hsphy_trim_param {
  51. unsigned int rterm;
  52. unsigned int sel_t;
  53. unsigned int hs_i;
  54. };
  55. #define trim_param_is_valid(p) ((p)->rterm || (p)->sel_t || (p)->hs_i)
  56. struct uniphier_u3hsphy_priv {
  57. struct device *dev;
  58. void __iomem *base;
  59. struct clk *clk, *clk_parent, *clk_ext;
  60. struct reset_control *rst, *rst_parent;
  61. struct regulator *vbus;
  62. const struct uniphier_u3hsphy_soc_data *data;
  63. };
  64. struct uniphier_u3hsphy_soc_data {
  65. int nparams;
  66. const struct uniphier_u3hsphy_param param[MAX_PHY_PARAMS];
  67. u32 config0;
  68. u32 config1;
  69. void (*trim_func)(struct uniphier_u3hsphy_priv *priv, u32 *pconfig,
  70. struct uniphier_u3hsphy_trim_param *pt);
  71. };
  72. static void uniphier_u3hsphy_trim_ld20(struct uniphier_u3hsphy_priv *priv,
  73. u32 *pconfig,
  74. struct uniphier_u3hsphy_trim_param *pt)
  75. {
  76. *pconfig &= ~HSPHY_CFG0_RTERM_MASK;
  77. *pconfig |= FIELD_PREP(HSPHY_CFG0_RTERM_MASK, pt->rterm);
  78. *pconfig &= ~HSPHY_CFG0_SEL_T_MASK;
  79. *pconfig |= FIELD_PREP(HSPHY_CFG0_SEL_T_MASK, pt->sel_t);
  80. *pconfig &= ~HSPHY_CFG0_HS_I_MASK;
  81. *pconfig |= FIELD_PREP(HSPHY_CFG0_HS_I_MASK, pt->hs_i);
  82. }
  83. static int uniphier_u3hsphy_get_nvparam(struct uniphier_u3hsphy_priv *priv,
  84. const char *name, unsigned int *val)
  85. {
  86. struct nvmem_cell *cell;
  87. u8 *buf;
  88. cell = devm_nvmem_cell_get(priv->dev, name);
  89. if (IS_ERR(cell))
  90. return PTR_ERR(cell);
  91. buf = nvmem_cell_read(cell, NULL);
  92. if (IS_ERR(buf))
  93. return PTR_ERR(buf);
  94. *val = *buf;
  95. kfree(buf);
  96. return 0;
  97. }
  98. static int uniphier_u3hsphy_get_nvparams(struct uniphier_u3hsphy_priv *priv,
  99. struct uniphier_u3hsphy_trim_param *pt)
  100. {
  101. int ret;
  102. ret = uniphier_u3hsphy_get_nvparam(priv, "rterm", &pt->rterm);
  103. if (ret)
  104. return ret;
  105. ret = uniphier_u3hsphy_get_nvparam(priv, "sel_t", &pt->sel_t);
  106. if (ret)
  107. return ret;
  108. ret = uniphier_u3hsphy_get_nvparam(priv, "hs_i", &pt->hs_i);
  109. if (ret)
  110. return ret;
  111. return 0;
  112. }
  113. static int uniphier_u3hsphy_update_config(struct uniphier_u3hsphy_priv *priv,
  114. u32 *pconfig)
  115. {
  116. struct uniphier_u3hsphy_trim_param trim;
  117. int ret, trimmed = 0;
  118. if (priv->data->trim_func) {
  119. ret = uniphier_u3hsphy_get_nvparams(priv, &trim);
  120. if (ret == -EPROBE_DEFER)
  121. return ret;
  122. /*
  123. * call trim_func only when trimming parameters that aren't
  124. * all-zero can be acquired. All-zero parameters mean nothing
  125. * has been written to nvmem.
  126. */
  127. if (!ret && trim_param_is_valid(&trim)) {
  128. priv->data->trim_func(priv, pconfig, &trim);
  129. trimmed = 1;
  130. } else {
  131. dev_dbg(priv->dev, "can't get parameter from nvmem\n");
  132. }
  133. }
  134. /* use default parameters without trimming values */
  135. if (!trimmed) {
  136. *pconfig &= ~HSPHY_CFG0_HSDISC_MASK;
  137. *pconfig |= FIELD_PREP(HSPHY_CFG0_HSDISC_MASK, 3);
  138. }
  139. return 0;
  140. }
  141. static void uniphier_u3hsphy_set_param(struct uniphier_u3hsphy_priv *priv,
  142. const struct uniphier_u3hsphy_param *p)
  143. {
  144. u32 val;
  145. u32 field_mask = GENMASK(p->field.msb, p->field.lsb);
  146. u8 data;
  147. val = readl(priv->base + HSPHY_CFG1);
  148. val &= ~HSPHY_CFG1_ADR_MASK;
  149. val |= FIELD_PREP(HSPHY_CFG1_ADR_MASK, p->field.reg_no)
  150. | HSPHY_CFG1_ADR_EN;
  151. writel(val, priv->base + HSPHY_CFG1);
  152. val = readl(priv->base + HSPHY_CFG1);
  153. val &= ~HSPHY_CFG1_ADR_EN;
  154. writel(val, priv->base + HSPHY_CFG1);
  155. val = readl(priv->base + HSPHY_CFG1);
  156. val &= ~FIELD_PREP(HSPHY_CFG1_DAT_MASK, field_mask);
  157. data = field_mask & (p->value << p->field.lsb);
  158. val |= FIELD_PREP(HSPHY_CFG1_DAT_MASK, data) | HSPHY_CFG1_DAT_EN;
  159. writel(val, priv->base + HSPHY_CFG1);
  160. val = readl(priv->base + HSPHY_CFG1);
  161. val &= ~HSPHY_CFG1_DAT_EN;
  162. writel(val, priv->base + HSPHY_CFG1);
  163. }
  164. static int uniphier_u3hsphy_power_on(struct phy *phy)
  165. {
  166. struct uniphier_u3hsphy_priv *priv = phy_get_drvdata(phy);
  167. int ret;
  168. ret = clk_prepare_enable(priv->clk_ext);
  169. if (ret)
  170. return ret;
  171. ret = clk_prepare_enable(priv->clk);
  172. if (ret)
  173. goto out_clk_ext_disable;
  174. ret = reset_control_deassert(priv->rst);
  175. if (ret)
  176. goto out_clk_disable;
  177. if (priv->vbus) {
  178. ret = regulator_enable(priv->vbus);
  179. if (ret)
  180. goto out_rst_assert;
  181. }
  182. return 0;
  183. out_rst_assert:
  184. reset_control_assert(priv->rst);
  185. out_clk_disable:
  186. clk_disable_unprepare(priv->clk);
  187. out_clk_ext_disable:
  188. clk_disable_unprepare(priv->clk_ext);
  189. return ret;
  190. }
  191. static int uniphier_u3hsphy_power_off(struct phy *phy)
  192. {
  193. struct uniphier_u3hsphy_priv *priv = phy_get_drvdata(phy);
  194. if (priv->vbus)
  195. regulator_disable(priv->vbus);
  196. reset_control_assert(priv->rst);
  197. clk_disable_unprepare(priv->clk);
  198. clk_disable_unprepare(priv->clk_ext);
  199. return 0;
  200. }
  201. static int uniphier_u3hsphy_init(struct phy *phy)
  202. {
  203. struct uniphier_u3hsphy_priv *priv = phy_get_drvdata(phy);
  204. u32 config0, config1;
  205. int i, ret;
  206. ret = clk_prepare_enable(priv->clk_parent);
  207. if (ret)
  208. return ret;
  209. ret = reset_control_deassert(priv->rst_parent);
  210. if (ret)
  211. goto out_clk_disable;
  212. if (!priv->data->config0 && !priv->data->config1)
  213. return 0;
  214. config0 = priv->data->config0;
  215. config1 = priv->data->config1;
  216. ret = uniphier_u3hsphy_update_config(priv, &config0);
  217. if (ret)
  218. goto out_rst_assert;
  219. writel(config0, priv->base + HSPHY_CFG0);
  220. writel(config1, priv->base + HSPHY_CFG1);
  221. for (i = 0; i < priv->data->nparams; i++)
  222. uniphier_u3hsphy_set_param(priv, &priv->data->param[i]);
  223. return 0;
  224. out_rst_assert:
  225. reset_control_assert(priv->rst_parent);
  226. out_clk_disable:
  227. clk_disable_unprepare(priv->clk_parent);
  228. return ret;
  229. }
  230. static int uniphier_u3hsphy_exit(struct phy *phy)
  231. {
  232. struct uniphier_u3hsphy_priv *priv = phy_get_drvdata(phy);
  233. reset_control_assert(priv->rst_parent);
  234. clk_disable_unprepare(priv->clk_parent);
  235. return 0;
  236. }
  237. static const struct phy_ops uniphier_u3hsphy_ops = {
  238. .init = uniphier_u3hsphy_init,
  239. .exit = uniphier_u3hsphy_exit,
  240. .power_on = uniphier_u3hsphy_power_on,
  241. .power_off = uniphier_u3hsphy_power_off,
  242. .owner = THIS_MODULE,
  243. };
  244. static int uniphier_u3hsphy_probe(struct platform_device *pdev)
  245. {
  246. struct device *dev = &pdev->dev;
  247. struct uniphier_u3hsphy_priv *priv;
  248. struct phy_provider *phy_provider;
  249. struct resource *res;
  250. struct phy *phy;
  251. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  252. if (!priv)
  253. return -ENOMEM;
  254. priv->dev = dev;
  255. priv->data = of_device_get_match_data(dev);
  256. if (WARN_ON(!priv->data ||
  257. priv->data->nparams > MAX_PHY_PARAMS))
  258. return -EINVAL;
  259. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. priv->base = devm_ioremap_resource(dev, res);
  261. if (IS_ERR(priv->base))
  262. return PTR_ERR(priv->base);
  263. priv->clk = devm_clk_get(dev, "phy");
  264. if (IS_ERR(priv->clk))
  265. return PTR_ERR(priv->clk);
  266. priv->clk_parent = devm_clk_get(dev, "link");
  267. if (IS_ERR(priv->clk_parent))
  268. return PTR_ERR(priv->clk_parent);
  269. priv->clk_ext = devm_clk_get(dev, "phy-ext");
  270. if (IS_ERR(priv->clk_ext)) {
  271. if (PTR_ERR(priv->clk_ext) == -ENOENT)
  272. priv->clk_ext = NULL;
  273. else
  274. return PTR_ERR(priv->clk_ext);
  275. }
  276. priv->rst = devm_reset_control_get_shared(dev, "phy");
  277. if (IS_ERR(priv->rst))
  278. return PTR_ERR(priv->rst);
  279. priv->rst_parent = devm_reset_control_get_shared(dev, "link");
  280. if (IS_ERR(priv->rst_parent))
  281. return PTR_ERR(priv->rst_parent);
  282. priv->vbus = devm_regulator_get_optional(dev, "vbus");
  283. if (IS_ERR(priv->vbus)) {
  284. if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
  285. return PTR_ERR(priv->vbus);
  286. priv->vbus = NULL;
  287. }
  288. phy = devm_phy_create(dev, dev->of_node, &uniphier_u3hsphy_ops);
  289. if (IS_ERR(phy))
  290. return PTR_ERR(phy);
  291. phy_set_drvdata(phy, priv);
  292. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  293. return PTR_ERR_OR_ZERO(phy_provider);
  294. }
  295. static const struct uniphier_u3hsphy_soc_data uniphier_pxs2_data = {
  296. .nparams = 0,
  297. };
  298. static const struct uniphier_u3hsphy_soc_data uniphier_ld20_data = {
  299. .nparams = 2,
  300. .param = {
  301. { LS_SLEW, 1 },
  302. { FS_LS_DRV, 1 },
  303. },
  304. .trim_func = uniphier_u3hsphy_trim_ld20,
  305. .config0 = 0x92316680,
  306. .config1 = 0x00000106,
  307. };
  308. static const struct uniphier_u3hsphy_soc_data uniphier_pxs3_data = {
  309. .nparams = 0,
  310. .trim_func = uniphier_u3hsphy_trim_ld20,
  311. .config0 = 0x92316680,
  312. .config1 = 0x00000106,
  313. };
  314. static const struct of_device_id uniphier_u3hsphy_match[] = {
  315. {
  316. .compatible = "socionext,uniphier-pxs2-usb3-hsphy",
  317. .data = &uniphier_pxs2_data,
  318. },
  319. {
  320. .compatible = "socionext,uniphier-ld20-usb3-hsphy",
  321. .data = &uniphier_ld20_data,
  322. },
  323. {
  324. .compatible = "socionext,uniphier-pxs3-usb3-hsphy",
  325. .data = &uniphier_pxs3_data,
  326. },
  327. { /* sentinel */ }
  328. };
  329. MODULE_DEVICE_TABLE(of, uniphier_u3hsphy_match);
  330. static struct platform_driver uniphier_u3hsphy_driver = {
  331. .probe = uniphier_u3hsphy_probe,
  332. .driver = {
  333. .name = "uniphier-usb3-hsphy",
  334. .of_match_table = uniphier_u3hsphy_match,
  335. },
  336. };
  337. module_platform_driver(uniphier_u3hsphy_driver);
  338. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  339. MODULE_DESCRIPTION("UniPhier HS-PHY driver for USB3 controller");
  340. MODULE_LICENSE("GPL v2");