phy-samsung-ufs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <essuuj@gmail.com>
  7. * Author: Alim Akhtar <alim.akhtar@samsung.com>
  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/module.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/soc/samsung/exynos-pmu.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. int samsung_ufs_phy_wait_for_lock_acq(struct phy *phy, u8 lane)
  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. for_each_phy_lane(ufs_phy, i) {
  88. if (ufs_phy->ufs_phy_state == CFG_PRE_INIT &&
  89. ufs_phy->drvdata->wait_for_cal) {
  90. err = ufs_phy->drvdata->wait_for_cal(phy, i);
  91. if (err)
  92. goto out;
  93. }
  94. if (ufs_phy->ufs_phy_state == CFG_POST_PWR_HS &&
  95. ufs_phy->drvdata->wait_for_cdr) {
  96. err = ufs_phy->drvdata->wait_for_cdr(phy, i);
  97. if (err)
  98. goto out;
  99. }
  100. }
  101. /**
  102. * In Samsung ufshci, PHY need to be calibrated at different
  103. * stages / state mainly before Linkstartup, after Linkstartup,
  104. * before power mode change and after power mode change.
  105. * Below state machine to make sure to calibrate PHY in each
  106. * state. Here after configuring PHY in a given state, will
  107. * change the state to next state so that next state phy
  108. * calibration value can be programed
  109. */
  110. out:
  111. switch (ufs_phy->ufs_phy_state) {
  112. case CFG_PRE_INIT:
  113. ufs_phy->ufs_phy_state = CFG_POST_INIT;
  114. break;
  115. case CFG_POST_INIT:
  116. ufs_phy->ufs_phy_state = CFG_PRE_PWR_HS;
  117. break;
  118. case CFG_PRE_PWR_HS:
  119. ufs_phy->ufs_phy_state = CFG_POST_PWR_HS;
  120. break;
  121. case CFG_POST_PWR_HS:
  122. /* Change back to INIT state */
  123. ufs_phy->ufs_phy_state = CFG_PRE_INIT;
  124. break;
  125. default:
  126. dev_err(ufs_phy->dev, "wrong state for phy calibration\n");
  127. }
  128. return err;
  129. }
  130. static int samsung_ufs_phy_clks_init(struct samsung_ufs_phy *phy)
  131. {
  132. int i;
  133. const struct samsung_ufs_phy_drvdata *drvdata = phy->drvdata;
  134. int num_clks = drvdata->num_clks;
  135. phy->clks = devm_kcalloc(phy->dev, num_clks, sizeof(*phy->clks),
  136. GFP_KERNEL);
  137. if (!phy->clks)
  138. return -ENOMEM;
  139. for (i = 0; i < num_clks; i++)
  140. phy->clks[i].id = drvdata->clk_list[i];
  141. return devm_clk_bulk_get(phy->dev, num_clks, phy->clks);
  142. }
  143. static int samsung_ufs_phy_init(struct phy *phy)
  144. {
  145. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  146. ss_phy->lane_cnt = phy->attrs.bus_width;
  147. ss_phy->ufs_phy_state = CFG_PRE_INIT;
  148. return 0;
  149. }
  150. static int samsung_ufs_phy_power_on(struct phy *phy)
  151. {
  152. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  153. int ret;
  154. samsung_ufs_phy_ctrl_isol(ss_phy, false);
  155. ret = clk_bulk_prepare_enable(ss_phy->drvdata->num_clks, ss_phy->clks);
  156. if (ret) {
  157. dev_err(ss_phy->dev, "failed to enable ufs phy clocks\n");
  158. return ret;
  159. }
  160. if (ss_phy->ufs_phy_state == CFG_PRE_INIT) {
  161. ret = samsung_ufs_phy_calibrate(phy);
  162. if (ret)
  163. dev_err(ss_phy->dev, "ufs phy calibration failed\n");
  164. }
  165. return ret;
  166. }
  167. static int samsung_ufs_phy_power_off(struct phy *phy)
  168. {
  169. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  170. clk_bulk_disable_unprepare(ss_phy->drvdata->num_clks, ss_phy->clks);
  171. samsung_ufs_phy_ctrl_isol(ss_phy, true);
  172. return 0;
  173. }
  174. static int samsung_ufs_phy_set_mode(struct phy *generic_phy,
  175. enum phy_mode mode, int submode)
  176. {
  177. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(generic_phy);
  178. ss_phy->mode = PHY_MODE_INVALID;
  179. if (mode > 0)
  180. ss_phy->mode = mode;
  181. return 0;
  182. }
  183. static int samsung_ufs_phy_exit(struct phy *phy)
  184. {
  185. struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
  186. ss_phy->ufs_phy_state = CFG_TAG_MAX;
  187. return 0;
  188. }
  189. static const struct phy_ops samsung_ufs_phy_ops = {
  190. .init = samsung_ufs_phy_init,
  191. .exit = samsung_ufs_phy_exit,
  192. .power_on = samsung_ufs_phy_power_on,
  193. .power_off = samsung_ufs_phy_power_off,
  194. .calibrate = samsung_ufs_phy_calibrate,
  195. .set_mode = samsung_ufs_phy_set_mode,
  196. .owner = THIS_MODULE,
  197. };
  198. static const struct of_device_id samsung_ufs_phy_match[];
  199. static int samsung_ufs_phy_probe(struct platform_device *pdev)
  200. {
  201. struct device *dev = &pdev->dev;
  202. const struct of_device_id *match;
  203. struct samsung_ufs_phy *phy;
  204. struct phy *gen_phy;
  205. struct phy_provider *phy_provider;
  206. const struct samsung_ufs_phy_drvdata *drvdata;
  207. u32 isol_offset;
  208. int err = 0;
  209. match = of_match_node(samsung_ufs_phy_match, dev->of_node);
  210. if (!match) {
  211. err = -EINVAL;
  212. dev_err(dev, "failed to get match_node\n");
  213. goto out;
  214. }
  215. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  216. if (!phy) {
  217. err = -ENOMEM;
  218. goto out;
  219. }
  220. phy->reg_pma = devm_platform_ioremap_resource_byname(pdev, "phy-pma");
  221. if (IS_ERR(phy->reg_pma)) {
  222. err = PTR_ERR(phy->reg_pma);
  223. goto out;
  224. }
  225. phy->reg_pmu = exynos_get_pmu_regmap_by_phandle(dev->of_node,
  226. "samsung,pmu-syscon");
  227. if (IS_ERR(phy->reg_pmu)) {
  228. err = PTR_ERR(phy->reg_pmu);
  229. dev_err(dev, "failed syscon remap for pmu\n");
  230. goto out;
  231. }
  232. gen_phy = devm_phy_create(dev, NULL, &samsung_ufs_phy_ops);
  233. if (IS_ERR(gen_phy)) {
  234. err = PTR_ERR(gen_phy);
  235. dev_err(dev, "failed to create PHY for ufs-phy\n");
  236. goto out;
  237. }
  238. drvdata = match->data;
  239. phy->dev = dev;
  240. phy->drvdata = drvdata;
  241. phy->cfgs = drvdata->cfgs;
  242. memcpy(&phy->isol, &drvdata->isol, sizeof(phy->isol));
  243. if (!of_property_read_u32_index(dev->of_node, "samsung,pmu-syscon", 1,
  244. &isol_offset))
  245. phy->isol.offset = isol_offset;
  246. phy->lane_cnt = PHY_DEF_LANE_CNT;
  247. err = samsung_ufs_phy_clks_init(phy);
  248. if (err) {
  249. dev_err(dev, "failed to get phy clocks\n");
  250. goto out;
  251. }
  252. phy_set_drvdata(gen_phy, phy);
  253. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  254. if (IS_ERR(phy_provider)) {
  255. err = PTR_ERR(phy_provider);
  256. dev_err(dev, "failed to register phy-provider\n");
  257. goto out;
  258. }
  259. out:
  260. return err;
  261. }
  262. static const struct of_device_id samsung_ufs_phy_match[] = {
  263. {
  264. .compatible = "google,gs101-ufs-phy",
  265. .data = &tensor_gs101_ufs_phy,
  266. }, {
  267. .compatible = "samsung,exynos7-ufs-phy",
  268. .data = &exynos7_ufs_phy,
  269. }, {
  270. .compatible = "samsung,exynosautov9-ufs-phy",
  271. .data = &exynosautov9_ufs_phy,
  272. }, {
  273. .compatible = "tesla,fsd-ufs-phy",
  274. .data = &fsd_ufs_phy,
  275. },
  276. {},
  277. };
  278. MODULE_DEVICE_TABLE(of, samsung_ufs_phy_match);
  279. static struct platform_driver samsung_ufs_phy_driver = {
  280. .probe = samsung_ufs_phy_probe,
  281. .driver = {
  282. .name = "samsung-ufs-phy",
  283. .of_match_table = samsung_ufs_phy_match,
  284. },
  285. };
  286. module_platform_driver(samsung_ufs_phy_driver);
  287. MODULE_DESCRIPTION("Samsung SoC UFS PHY Driver");
  288. MODULE_AUTHOR("Seungwon Jeon <essuuj@gmail.com>");
  289. MODULE_AUTHOR("Alim Akhtar <alim.akhtar@samsung.com>");
  290. MODULE_LICENSE("GPL v2");