ufs-rockchip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Rockchip UFS Host Controller driver
  4. *
  5. * Copyright (C) 2025 Rockchip Electronics Co., Ltd.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/gpio.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/pm_wakeup.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset.h>
  17. #include <ufs/ufshcd.h>
  18. #include <ufs/unipro.h>
  19. #include "ufshcd-pltfrm.h"
  20. #include "ufs-rockchip.h"
  21. static void ufs_rockchip_controller_reset(struct ufs_rockchip_host *host)
  22. {
  23. reset_control_assert(host->rst);
  24. udelay(1);
  25. reset_control_deassert(host->rst);
  26. }
  27. static int ufs_rockchip_hce_enable_notify(struct ufs_hba *hba,
  28. enum ufs_notify_change_status status)
  29. {
  30. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  31. int err = 0;
  32. if (status == POST_CHANGE) {
  33. err = ufshcd_dme_reset(hba);
  34. if (err)
  35. return err;
  36. err = ufshcd_dme_enable(hba);
  37. if (err)
  38. return err;
  39. return ufshcd_vops_phy_initialization(hba);
  40. }
  41. /* PRE_CHANGE */
  42. ufs_rockchip_controller_reset(host);
  43. return 0;
  44. }
  45. static void ufs_rockchip_set_pm_lvl(struct ufs_hba *hba)
  46. {
  47. hba->rpm_lvl = UFS_PM_LVL_5;
  48. hba->spm_lvl = UFS_PM_LVL_5;
  49. }
  50. static int ufs_rockchip_rk3576_phy_init(struct ufs_hba *hba)
  51. {
  52. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  53. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(PA_LOCAL_TX_LCC_ENABLE, 0x0), 0x0);
  54. /* enable the mphy DME_SET cfg */
  55. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(MPHY_CFG, 0x0), MPHY_CFG_ENABLE);
  56. for (int i = 0; i < 2; i++) {
  57. /* Configuration M - TX */
  58. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD, SEL_TX_LANE0 + i), 0x06);
  59. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD_EN, SEL_TX_LANE0 + i), 0x02);
  60. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_VALUE, SEL_TX_LANE0 + i), 0x44);
  61. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE1, SEL_TX_LANE0 + i), 0xe6);
  62. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE2, SEL_TX_LANE0 + i), 0x07);
  63. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_TASE_VALUE, SEL_TX_LANE0 + i), 0x93);
  64. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_BASE_NVALUE, SEL_TX_LANE0 + i), 0xc9);
  65. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_POWER_SAVING_CTRL, SEL_TX_LANE0 + i), 0x00);
  66. /* Configuration M - RX */
  67. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD, SEL_RX_LANE0 + i), 0x06);
  68. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD_EN, SEL_RX_LANE0 + i), 0x00);
  69. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE, SEL_RX_LANE0 + i), 0x58);
  70. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_PVALUE1, SEL_RX_LANE0 + i), 0x8c);
  71. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_PVALUE2, SEL_RX_LANE0 + i), 0x02);
  72. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_OPTION, SEL_RX_LANE0 + i), 0xf6);
  73. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_POWER_SAVING_CTRL, SEL_RX_LANE0 + i), 0x69);
  74. }
  75. /* disable the mphy DME_SET cfg */
  76. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(MPHY_CFG, 0x0), MPHY_CFG_DISABLE);
  77. ufs_sys_writel(host->mphy_base, 0x80, CMN_REG23);
  78. ufs_sys_writel(host->mphy_base, 0xB5, TRSV0_REG14);
  79. ufs_sys_writel(host->mphy_base, 0xB5, TRSV1_REG14);
  80. ufs_sys_writel(host->mphy_base, 0x03, TRSV0_REG15);
  81. ufs_sys_writel(host->mphy_base, 0x03, TRSV1_REG15);
  82. ufs_sys_writel(host->mphy_base, 0x38, TRSV0_REG08);
  83. ufs_sys_writel(host->mphy_base, 0x38, TRSV1_REG08);
  84. ufs_sys_writel(host->mphy_base, 0x50, TRSV0_REG29);
  85. ufs_sys_writel(host->mphy_base, 0x50, TRSV1_REG29);
  86. ufs_sys_writel(host->mphy_base, 0x80, TRSV0_REG2E);
  87. ufs_sys_writel(host->mphy_base, 0x80, TRSV1_REG2E);
  88. ufs_sys_writel(host->mphy_base, 0x18, TRSV0_REG3C);
  89. ufs_sys_writel(host->mphy_base, 0x18, TRSV1_REG3C);
  90. ufs_sys_writel(host->mphy_base, 0x03, TRSV0_REG16);
  91. ufs_sys_writel(host->mphy_base, 0x03, TRSV1_REG16);
  92. ufs_sys_writel(host->mphy_base, 0x20, TRSV0_REG17);
  93. ufs_sys_writel(host->mphy_base, 0x20, TRSV1_REG17);
  94. ufs_sys_writel(host->mphy_base, 0xC0, TRSV0_REG18);
  95. ufs_sys_writel(host->mphy_base, 0xC0, TRSV1_REG18);
  96. ufs_sys_writel(host->mphy_base, 0x03, CMN_REG25);
  97. ufs_sys_writel(host->mphy_base, 0x03, TRSV0_REG3D);
  98. ufs_sys_writel(host->mphy_base, 0x03, TRSV1_REG3D);
  99. ufs_sys_writel(host->mphy_base, 0xC0, CMN_REG23);
  100. udelay(1);
  101. ufs_sys_writel(host->mphy_base, 0x00, CMN_REG23);
  102. usleep_range(200, 250);
  103. /* start link up */
  104. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(MIB_T_DBG_CPORT_TX_ENDIAN, 0), 0x0);
  105. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(MIB_T_DBG_CPORT_RX_ENDIAN, 0), 0x0);
  106. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(N_DEVICEID, 0), 0x0);
  107. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(N_DEVICEID_VALID, 0), 0x1);
  108. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(T_PEERDEVICEID, 0), 0x1);
  109. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(T_CONNECTIONSTATE, 0), 0x1);
  110. return 0;
  111. }
  112. static int ufs_rockchip_common_init(struct ufs_hba *hba)
  113. {
  114. struct device *dev = hba->dev;
  115. struct platform_device *pdev = to_platform_device(dev);
  116. struct ufs_rockchip_host *host;
  117. int err;
  118. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  119. if (!host)
  120. return -ENOMEM;
  121. host->ufs_sys_ctrl = devm_platform_ioremap_resource_byname(pdev, "hci_grf");
  122. if (IS_ERR(host->ufs_sys_ctrl))
  123. return dev_err_probe(dev, PTR_ERR(host->ufs_sys_ctrl),
  124. "Failed to map HCI system control registers\n");
  125. host->ufs_phy_ctrl = devm_platform_ioremap_resource_byname(pdev, "mphy_grf");
  126. if (IS_ERR(host->ufs_phy_ctrl))
  127. return dev_err_probe(dev, PTR_ERR(host->ufs_phy_ctrl),
  128. "Failed to map mphy system control registers\n");
  129. host->mphy_base = devm_platform_ioremap_resource_byname(pdev, "mphy");
  130. if (IS_ERR(host->mphy_base))
  131. return dev_err_probe(dev, PTR_ERR(host->mphy_base),
  132. "Failed to map mphy base registers\n");
  133. host->rst = devm_reset_control_array_get_exclusive(dev);
  134. if (IS_ERR(host->rst))
  135. return dev_err_probe(dev, PTR_ERR(host->rst),
  136. "failed to get reset control\n");
  137. ufs_rockchip_controller_reset(host);
  138. host->ref_out_clk = devm_clk_get_enabled(dev, "ref_out");
  139. if (IS_ERR(host->ref_out_clk))
  140. return dev_err_probe(dev, PTR_ERR(host->ref_out_clk),
  141. "ref_out clock unavailable\n");
  142. host->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  143. if (IS_ERR(host->rst_gpio))
  144. return dev_err_probe(dev, PTR_ERR(host->rst_gpio),
  145. "failed to get reset gpio\n");
  146. err = devm_clk_bulk_get_all_enabled(dev, &host->clks);
  147. if (err < 0)
  148. return dev_err_probe(dev, err, "failed to enable clocks\n");
  149. host->hba = hba;
  150. ufshcd_set_variant(hba, host);
  151. return 0;
  152. }
  153. static int ufs_rockchip_rk3576_init(struct ufs_hba *hba)
  154. {
  155. struct device *dev = hba->dev;
  156. int ret;
  157. hba->quirks = UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING;
  158. /* Enable BKOPS when suspend */
  159. hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
  160. /* Enable putting device into deep sleep */
  161. hba->caps |= UFSHCD_CAP_DEEPSLEEP;
  162. /* Enable devfreq of UFS */
  163. hba->caps |= UFSHCD_CAP_CLK_SCALING;
  164. /* Enable WriteBooster */
  165. hba->caps |= UFSHCD_CAP_WB_EN;
  166. /* Set the default desired pm level in case no users set via sysfs */
  167. ufs_rockchip_set_pm_lvl(hba);
  168. ret = ufs_rockchip_common_init(hba);
  169. if (ret)
  170. return dev_err_probe(dev, ret, "ufs common init fail\n");
  171. return 0;
  172. }
  173. static int ufs_rockchip_device_reset(struct ufs_hba *hba)
  174. {
  175. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  176. gpiod_set_value_cansleep(host->rst_gpio, 1);
  177. usleep_range(20, 25);
  178. gpiod_set_value_cansleep(host->rst_gpio, 0);
  179. usleep_range(20, 25);
  180. return 0;
  181. }
  182. static const struct ufs_hba_variant_ops ufs_hba_rk3576_vops = {
  183. .name = "rk3576",
  184. .init = ufs_rockchip_rk3576_init,
  185. .device_reset = ufs_rockchip_device_reset,
  186. .hce_enable_notify = ufs_rockchip_hce_enable_notify,
  187. .phy_initialization = ufs_rockchip_rk3576_phy_init,
  188. };
  189. static const struct of_device_id ufs_rockchip_of_match[] = {
  190. { .compatible = "rockchip,rk3576-ufshc", .data = &ufs_hba_rk3576_vops },
  191. { },
  192. };
  193. MODULE_DEVICE_TABLE(of, ufs_rockchip_of_match);
  194. static int ufs_rockchip_probe(struct platform_device *pdev)
  195. {
  196. struct device *dev = &pdev->dev;
  197. const struct ufs_hba_variant_ops *vops;
  198. int err;
  199. vops = device_get_match_data(dev);
  200. if (!vops)
  201. return dev_err_probe(dev, -ENODATA, "ufs_hba_variant_ops not defined.\n");
  202. err = ufshcd_pltfrm_init(pdev, vops);
  203. if (err)
  204. return dev_err_probe(dev, err, "ufshcd_pltfrm_init failed\n");
  205. return 0;
  206. }
  207. static void ufs_rockchip_remove(struct platform_device *pdev)
  208. {
  209. ufshcd_pltfrm_remove(pdev);
  210. }
  211. #ifdef CONFIG_PM
  212. static int ufs_rockchip_runtime_suspend(struct device *dev)
  213. {
  214. struct ufs_hba *hba = dev_get_drvdata(dev);
  215. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  216. clk_disable_unprepare(host->ref_out_clk);
  217. /* Do not power down the genpd if rpm_lvl is less than level 5 */
  218. dev_pm_genpd_rpm_always_on(dev, hba->rpm_lvl < UFS_PM_LVL_5);
  219. return ufshcd_runtime_suspend(dev);
  220. }
  221. static int ufs_rockchip_runtime_resume(struct device *dev)
  222. {
  223. struct ufs_hba *hba = dev_get_drvdata(dev);
  224. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  225. int err;
  226. err = clk_prepare_enable(host->ref_out_clk);
  227. if (err) {
  228. dev_err(hba->dev, "failed to enable ref_out clock %d\n", err);
  229. return err;
  230. }
  231. ufs_rockchip_controller_reset(host);
  232. return ufshcd_runtime_resume(dev);
  233. }
  234. #endif
  235. #ifdef CONFIG_PM_SLEEP
  236. static int ufs_rockchip_system_suspend(struct device *dev)
  237. {
  238. struct ufs_hba *hba = dev_get_drvdata(dev);
  239. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  240. int err;
  241. /*
  242. * If spm_lvl is less than level 5, it means we need to keep the host
  243. * controller in powered-on state. So device_set_awake_path() is
  244. * calling pm core to notify the genpd provider to meet this requirement
  245. */
  246. if (hba->spm_lvl < UFS_PM_LVL_5)
  247. device_set_awake_path(dev);
  248. err = ufshcd_system_suspend(dev);
  249. if (err) {
  250. dev_err(hba->dev, "UFSHCD system suspend failed %d\n", err);
  251. return err;
  252. }
  253. clk_disable_unprepare(host->ref_out_clk);
  254. return 0;
  255. }
  256. static int ufs_rockchip_system_resume(struct device *dev)
  257. {
  258. struct ufs_hba *hba = dev_get_drvdata(dev);
  259. struct ufs_rockchip_host *host = ufshcd_get_variant(hba);
  260. int err;
  261. err = clk_prepare_enable(host->ref_out_clk);
  262. if (err) {
  263. dev_err(hba->dev, "failed to enable ref_out clock %d\n", err);
  264. return err;
  265. }
  266. return ufshcd_system_resume(dev);
  267. }
  268. #endif
  269. static const struct dev_pm_ops ufs_rockchip_pm_ops = {
  270. SET_SYSTEM_SLEEP_PM_OPS(ufs_rockchip_system_suspend, ufs_rockchip_system_resume)
  271. SET_RUNTIME_PM_OPS(ufs_rockchip_runtime_suspend, ufs_rockchip_runtime_resume, NULL)
  272. .prepare = ufshcd_suspend_prepare,
  273. .complete = ufshcd_resume_complete,
  274. };
  275. static struct platform_driver ufs_rockchip_pltform = {
  276. .probe = ufs_rockchip_probe,
  277. .remove = ufs_rockchip_remove,
  278. .driver = {
  279. .name = "ufshcd-rockchip",
  280. .pm = &ufs_rockchip_pm_ops,
  281. .of_match_table = ufs_rockchip_of_match,
  282. },
  283. };
  284. module_platform_driver(ufs_rockchip_pltform);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("Rockchip UFS Host Driver");