phy-samsung-ufs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UFS PHY driver for Samsung SoC
  4. *
  5. * Copyright (C) 2020 Samsung Electronics Co., Ltd.
  6. * Author: Seungwon Jeon <[email protected]>
  7. * Author: Alim Akhtar <[email protected]>
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/of.h>
  14. #include <linux/io.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/module.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include "phy-samsung-ufs.h"
  22. #define for_each_phy_lane(phy, i) \
  23. for (i = 0; i < (phy)->lane_cnt; i++)
  24. #define for_each_phy_cfg(cfg) \
  25. for (; (cfg)->id; (cfg)++)
  26. #define PHY_DEF_LANE_CNT 1
  27. static void samsung_ufs_phy_config(struct samsung_ufs_phy *phy,
  28. const struct samsung_ufs_phy_cfg *cfg,
  29. u8 lane)
  30. {
  31. enum {LANE_0, LANE_1}; /* lane index */
  32. switch (lane) {
  33. case LANE_0:
  34. writel(cfg->val, (phy)->reg_pma + cfg->off_0);
  35. break;
  36. case LANE_1:
  37. if (cfg->id == PHY_TRSV_BLK)
  38. writel(cfg->val, (phy)->reg_pma + cfg->off_1);
  39. break;
  40. }
  41. }
  42. static int samsung_ufs_phy_wait_for_lock_acq(struct phy *phy)
  43. {
  44. struct samsung_ufs_phy *ufs_phy = get_samsung_ufs_phy(phy);
  45. const unsigned int timeout_us = 100000;
  46. const unsigned int sleep_us = 10;
  47. u32 val;
  48. int err;
  49. err = readl_poll_timeout(
  50. ufs_phy->reg_pma + PHY_APB_ADDR(PHY_PLL_LOCK_STATUS),
  51. val, (val & PHY_PLL_LOCK_BIT), sleep_us, timeout_us);
  52. if (err) {
  53. dev_err(ufs_phy->dev,
  54. "failed to get phy pll lock acquisition %d\n", err);
  55. goto out;
  56. }
  57. err = readl_poll_timeout(
  58. ufs_phy->reg_pma +
  59. PHY_APB_ADDR(ufs_phy->drvdata->cdr_lock_status_offset),
  60. val, (val & PHY_CDR_LOCK_BIT), sleep_us, timeout_us);
  61. if (err)
  62. dev_err(ufs_phy->dev,
  63. "failed to get phy cdr lock acquisition %d\n", err);
  64. out:
  65. return err;
  66. }
  67. static int samsung_ufs_phy_calibrate(struct phy *phy)
  68. {
  69. struct samsung_ufs_phy *ufs_phy = get_samsung_ufs_phy(phy);
  70. const struct samsung_ufs_phy_cfg * const *cfgs = ufs_phy->cfgs;
  71. const struct samsung_ufs_phy_cfg *cfg;
  72. int err = 0;
  73. int i;
  74. if (unlikely(ufs_phy->ufs_phy_state < CFG_PRE_INIT ||
  75. ufs_phy->ufs_phy_state >= CFG_TAG_MAX)) {
  76. dev_err(ufs_phy->dev, "invalid phy config index %d\n", ufs_phy->ufs_phy_state);
  77. return -EINVAL;
  78. }
  79. cfg = cfgs[ufs_phy->ufs_phy_state];
  80. if (!cfg)
  81. goto out;
  82. for_each_phy_cfg(cfg) {
  83. for_each_phy_lane(ufs_phy, i) {
  84. samsung_ufs_phy_config(ufs_phy, cfg, i);
  85. }
  86. }
  87. if (ufs_phy->ufs_phy_state == CFG_POST_PWR_HS)
  88. err = samsung_ufs_phy_wait_for_lock_acq(phy);
  89. /**
  90. * In Samsung ufshci, PHY need to be calibrated at different
  91. * stages / state mainly before Linkstartup, after Linkstartup,
  92. * before power mode change and after power mode change.
  93. * Below state machine to make sure to calibrate PHY in each
  94. * state. Here after configuring PHY in a given state, will
  95. * change the state to next state so that next state phy
  96. * calibration value can be programed
  97. */
  98. out:
  99. switch (ufs_phy->ufs_phy_state) {
  100. case CFG_PRE_INIT:
  101. ufs_phy->ufs_phy_state = CFG_POST_INIT;
  102. break;
  103. case CFG_POST_INIT:
  104. ufs_phy->ufs_phy_state = CFG_PRE_PWR_HS;
  105. break;
  106. case CFG_PRE_PWR_HS:
  107. ufs_phy->ufs_phy_state = CFG_POST_PWR_HS;
  108. break;
  109. case CFG_POST_PWR_HS:
  110. /* Change back to INIT state */
  111. ufs_phy->ufs_phy_state = CFG_PRE_INIT;
  112. break;
  113. default:
  114. dev_err(ufs_phy->dev, "wrong state for phy calibration\n");
  115. }
  116. return err;
  117. }
  118. static int samsung_ufs_phy_clks_init(struct samsung_ufs_phy *phy)
  119. {
  120. int i;
  121. const struct samsung_ufs_phy_drvdata *drvdata = phy->drvdata;
  122. int num_clks = drvdata->num_clks;
  123. phy->clks = devm_kcalloc(phy->dev, num_clks, sizeof(*phy->clks),
  124. GFP_KERNEL);
  125. if (!phy->clks)
  126. return -ENOMEM;
  127. for (i = 0; i < num_clks; i++)
  128. phy->clks[i].id = drvdata->clk_list[i];
  129. return devm_clk_bulk_get(phy->dev, num_clks, phy->clks);
  130. }
  131. static int samsung_ufs_phy_init(struct phy *phy)
  132. {
  133. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  134. ss_phy->lane_cnt = phy->attrs.bus_width;
  135. ss_phy->ufs_phy_state = CFG_PRE_INIT;
  136. return 0;
  137. }
  138. static int samsung_ufs_phy_power_on(struct phy *phy)
  139. {
  140. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  141. int ret;
  142. samsung_ufs_phy_ctrl_isol(ss_phy, false);
  143. ret = clk_bulk_prepare_enable(ss_phy->drvdata->num_clks, ss_phy->clks);
  144. if (ret) {
  145. dev_err(ss_phy->dev, "failed to enable ufs phy clocks\n");
  146. return ret;
  147. }
  148. if (ss_phy->ufs_phy_state == CFG_PRE_INIT) {
  149. ret = samsung_ufs_phy_calibrate(phy);
  150. if (ret)
  151. dev_err(ss_phy->dev, "ufs phy calibration failed\n");
  152. }
  153. return ret;
  154. }
  155. static int samsung_ufs_phy_power_off(struct phy *phy)
  156. {
  157. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  158. clk_bulk_disable_unprepare(ss_phy->drvdata->num_clks, ss_phy->clks);
  159. samsung_ufs_phy_ctrl_isol(ss_phy, true);
  160. return 0;
  161. }
  162. static int samsung_ufs_phy_set_mode(struct phy *generic_phy,
  163. enum phy_mode mode, int submode)
  164. {
  165. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(generic_phy);
  166. ss_phy->mode = PHY_MODE_INVALID;
  167. if (mode > 0)
  168. ss_phy->mode = mode;
  169. return 0;
  170. }
  171. static int samsung_ufs_phy_exit(struct phy *phy)
  172. {
  173. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  174. ss_phy->ufs_phy_state = CFG_TAG_MAX;
  175. return 0;
  176. }
  177. static const struct phy_ops samsung_ufs_phy_ops = {
  178. .init = samsung_ufs_phy_init,
  179. .exit = samsung_ufs_phy_exit,
  180. .power_on = samsung_ufs_phy_power_on,
  181. .power_off = samsung_ufs_phy_power_off,
  182. .calibrate = samsung_ufs_phy_calibrate,
  183. .set_mode = samsung_ufs_phy_set_mode,
  184. .owner = THIS_MODULE,
  185. };
  186. static const struct of_device_id samsung_ufs_phy_match[];
  187. static int samsung_ufs_phy_probe(struct platform_device *pdev)
  188. {
  189. struct device *dev = &pdev->dev;
  190. const struct of_device_id *match;
  191. struct samsung_ufs_phy *phy;
  192. struct phy *gen_phy;
  193. struct phy_provider *phy_provider;
  194. const struct samsung_ufs_phy_drvdata *drvdata;
  195. u32 isol_offset;
  196. int err = 0;
  197. match = of_match_node(samsung_ufs_phy_match, dev->of_node);
  198. if (!match) {
  199. err = -EINVAL;
  200. dev_err(dev, "failed to get match_node\n");
  201. goto out;
  202. }
  203. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  204. if (!phy) {
  205. err = -ENOMEM;
  206. goto out;
  207. }
  208. phy->reg_pma = devm_platform_ioremap_resource_byname(pdev, "phy-pma");
  209. if (IS_ERR(phy->reg_pma)) {
  210. err = PTR_ERR(phy->reg_pma);
  211. goto out;
  212. }
  213. phy->reg_pmu = syscon_regmap_lookup_by_phandle(
  214. dev->of_node, "samsung,pmu-syscon");
  215. if (IS_ERR(phy->reg_pmu)) {
  216. err = PTR_ERR(phy->reg_pmu);
  217. dev_err(dev, "failed syscon remap for pmu\n");
  218. goto out;
  219. }
  220. gen_phy = devm_phy_create(dev, NULL, &samsung_ufs_phy_ops);
  221. if (IS_ERR(gen_phy)) {
  222. err = PTR_ERR(gen_phy);
  223. dev_err(dev, "failed to create PHY for ufs-phy\n");
  224. goto out;
  225. }
  226. drvdata = match->data;
  227. phy->dev = dev;
  228. phy->drvdata = drvdata;
  229. phy->cfgs = drvdata->cfgs;
  230. memcpy(&phy->isol, &drvdata->isol, sizeof(phy->isol));
  231. if (!of_property_read_u32_index(dev->of_node, "samsung,pmu-syscon", 1,
  232. &isol_offset))
  233. phy->isol.offset = isol_offset;
  234. phy->lane_cnt = PHY_DEF_LANE_CNT;
  235. err = samsung_ufs_phy_clks_init(phy);
  236. if (err) {
  237. dev_err(dev, "failed to get phy clocks\n");
  238. goto out;
  239. }
  240. phy_set_drvdata(gen_phy, phy);
  241. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  242. if (IS_ERR(phy_provider)) {
  243. err = PTR_ERR(phy_provider);
  244. dev_err(dev, "failed to register phy-provider\n");
  245. goto out;
  246. }
  247. out:
  248. return err;
  249. }
  250. static const struct of_device_id samsung_ufs_phy_match[] = {
  251. {
  252. .compatible = "samsung,exynos7-ufs-phy",
  253. .data = &exynos7_ufs_phy,
  254. }, {
  255. .compatible = "samsung,exynosautov9-ufs-phy",
  256. .data = &exynosautov9_ufs_phy,
  257. }, {
  258. .compatible = "tesla,fsd-ufs-phy",
  259. .data = &fsd_ufs_phy,
  260. },
  261. {},
  262. };
  263. MODULE_DEVICE_TABLE(of, samsung_ufs_phy_match);
  264. static struct platform_driver samsung_ufs_phy_driver = {
  265. .probe = samsung_ufs_phy_probe,
  266. .driver = {
  267. .name = "samsung-ufs-phy",
  268. .of_match_table = samsung_ufs_phy_match,
  269. },
  270. };
  271. module_platform_driver(samsung_ufs_phy_driver);
  272. MODULE_DESCRIPTION("Samsung SoC UFS PHY Driver");
  273. MODULE_AUTHOR("Seungwon Jeon <[email protected]>");
  274. MODULE_AUTHOR("Alim Akhtar <[email protected]>");
  275. MODULE_LICENSE("GPL v2");