ufs-sprd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UNISOC UFS Host Controller driver
  4. *
  5. * Copyright (C) 2022 Unisoc, Inc.
  6. * Author: Zhe Wang <[email protected]>
  7. */
  8. #include <linux/arm-smccc.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/reset.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <ufs/ufshcd.h>
  16. #include "ufshcd-pltfrm.h"
  17. #include "ufs-sprd.h"
  18. static const struct of_device_id ufs_sprd_of_match[];
  19. static struct ufs_sprd_priv *ufs_sprd_get_priv_data(struct ufs_hba *hba)
  20. {
  21. struct ufs_sprd_host *host = ufshcd_get_variant(hba);
  22. WARN_ON(!host->priv);
  23. return host->priv;
  24. }
  25. static void ufs_sprd_regmap_update(struct ufs_sprd_priv *priv, unsigned int index,
  26. unsigned int reg, unsigned int bits, unsigned int val)
  27. {
  28. regmap_update_bits(priv->sysci[index].regmap, reg, bits, val);
  29. }
  30. static void ufs_sprd_regmap_read(struct ufs_sprd_priv *priv, unsigned int index,
  31. unsigned int reg, unsigned int *val)
  32. {
  33. regmap_read(priv->sysci[index].regmap, reg, val);
  34. }
  35. static void ufs_sprd_get_unipro_ver(struct ufs_hba *hba)
  36. {
  37. struct ufs_sprd_host *host = ufshcd_get_variant(hba);
  38. if (ufshcd_dme_get(hba, UIC_ARG_MIB(PA_LOCALVERINFO), &host->unipro_ver))
  39. host->unipro_ver = 0;
  40. }
  41. static void ufs_sprd_ctrl_uic_compl(struct ufs_hba *hba, bool enable)
  42. {
  43. u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
  44. if (enable == true)
  45. set |= UIC_COMMAND_COMPL;
  46. else
  47. set &= ~UIC_COMMAND_COMPL;
  48. ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
  49. }
  50. static int ufs_sprd_get_reset_ctrl(struct device *dev, struct ufs_sprd_rst *rci)
  51. {
  52. rci->rc = devm_reset_control_get(dev, rci->name);
  53. if (IS_ERR(rci->rc)) {
  54. dev_err(dev, "failed to get reset ctrl:%s\n", rci->name);
  55. return PTR_ERR(rci->rc);
  56. }
  57. return 0;
  58. }
  59. static int ufs_sprd_get_syscon_reg(struct device *dev, struct ufs_sprd_syscon *sysci)
  60. {
  61. sysci->regmap = syscon_regmap_lookup_by_phandle(dev->of_node, sysci->name);
  62. if (IS_ERR(sysci->regmap)) {
  63. dev_err(dev, "failed to get ufs syscon:%s\n", sysci->name);
  64. return PTR_ERR(sysci->regmap);
  65. }
  66. return 0;
  67. }
  68. static int ufs_sprd_get_vreg(struct device *dev, struct ufs_sprd_vreg *vregi)
  69. {
  70. vregi->vreg = devm_regulator_get(dev, vregi->name);
  71. if (IS_ERR(vregi->vreg)) {
  72. dev_err(dev, "failed to get vreg:%s\n", vregi->name);
  73. return PTR_ERR(vregi->vreg);
  74. }
  75. return 0;
  76. }
  77. static int ufs_sprd_parse_dt(struct device *dev, struct ufs_hba *hba, struct ufs_sprd_host *host)
  78. {
  79. u32 i;
  80. struct ufs_sprd_priv *priv = host->priv;
  81. int ret = 0;
  82. /* Parse UFS reset ctrl info */
  83. for (i = 0; i < SPRD_UFS_RST_MAX; i++) {
  84. if (!priv->rci[i].name)
  85. continue;
  86. ret = ufs_sprd_get_reset_ctrl(dev, &priv->rci[i]);
  87. if (ret)
  88. goto out;
  89. }
  90. /* Parse UFS syscon reg info */
  91. for (i = 0; i < SPRD_UFS_SYSCON_MAX; i++) {
  92. if (!priv->sysci[i].name)
  93. continue;
  94. ret = ufs_sprd_get_syscon_reg(dev, &priv->sysci[i]);
  95. if (ret)
  96. goto out;
  97. }
  98. /* Parse UFS vreg info */
  99. for (i = 0; i < SPRD_UFS_VREG_MAX; i++) {
  100. if (!priv->vregi[i].name)
  101. continue;
  102. ret = ufs_sprd_get_vreg(dev, &priv->vregi[i]);
  103. if (ret)
  104. goto out;
  105. }
  106. out:
  107. return ret;
  108. }
  109. static int ufs_sprd_common_init(struct ufs_hba *hba)
  110. {
  111. struct device *dev = hba->dev;
  112. struct ufs_sprd_host *host;
  113. struct platform_device __maybe_unused *pdev = to_platform_device(dev);
  114. const struct of_device_id *of_id;
  115. int ret = 0;
  116. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  117. if (!host)
  118. return -ENOMEM;
  119. of_id = of_match_node(ufs_sprd_of_match, pdev->dev.of_node);
  120. if (of_id->data != NULL)
  121. host->priv = container_of(of_id->data, struct ufs_sprd_priv,
  122. ufs_hba_sprd_vops);
  123. host->hba = hba;
  124. ufshcd_set_variant(hba, host);
  125. hba->caps |= UFSHCD_CAP_CLK_GATING |
  126. UFSHCD_CAP_CRYPTO |
  127. UFSHCD_CAP_WB_EN;
  128. hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS;
  129. ret = ufs_sprd_parse_dt(dev, hba, host);
  130. return ret;
  131. }
  132. static int sprd_ufs_pwr_change_notify(struct ufs_hba *hba,
  133. enum ufs_notify_change_status status,
  134. struct ufs_pa_layer_attr *dev_max_params,
  135. struct ufs_pa_layer_attr *dev_req_params)
  136. {
  137. struct ufs_sprd_host *host = ufshcd_get_variant(hba);
  138. if (status == PRE_CHANGE) {
  139. memcpy(dev_req_params, dev_max_params,
  140. sizeof(struct ufs_pa_layer_attr));
  141. if (host->unipro_ver >= UFS_UNIPRO_VER_1_8)
  142. ufshcd_dme_configure_adapt(hba, dev_req_params->gear_tx,
  143. PA_INITIAL_ADAPT);
  144. }
  145. return 0;
  146. }
  147. static int ufs_sprd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  148. enum ufs_notify_change_status status)
  149. {
  150. unsigned long flags;
  151. if (status == PRE_CHANGE) {
  152. if (ufshcd_is_auto_hibern8_supported(hba)) {
  153. spin_lock_irqsave(hba->host->host_lock, flags);
  154. ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER);
  155. spin_unlock_irqrestore(hba->host->host_lock, flags);
  156. }
  157. }
  158. return 0;
  159. }
  160. static void ufs_sprd_n6_host_reset(struct ufs_hba *hba)
  161. {
  162. struct ufs_sprd_priv *priv = ufs_sprd_get_priv_data(hba);
  163. dev_info(hba->dev, "ufs host reset!\n");
  164. reset_control_assert(priv->rci[SPRD_UFSHCI_SOFT_RST].rc);
  165. usleep_range(1000, 1100);
  166. reset_control_deassert(priv->rci[SPRD_UFSHCI_SOFT_RST].rc);
  167. }
  168. static int ufs_sprd_n6_device_reset(struct ufs_hba *hba)
  169. {
  170. struct ufs_sprd_priv *priv = ufs_sprd_get_priv_data(hba);
  171. dev_info(hba->dev, "ufs device reset!\n");
  172. reset_control_assert(priv->rci[SPRD_UFS_DEV_RST].rc);
  173. usleep_range(1000, 1100);
  174. reset_control_deassert(priv->rci[SPRD_UFS_DEV_RST].rc);
  175. return 0;
  176. }
  177. static void ufs_sprd_n6_key_acc_enable(struct ufs_hba *hba)
  178. {
  179. u32 val;
  180. u32 retry = 10;
  181. struct arm_smccc_res res;
  182. check_hce:
  183. /* Key access only can be enabled under HCE enable */
  184. val = ufshcd_readl(hba, REG_CONTROLLER_ENABLE);
  185. if (!(val & CONTROLLER_ENABLE)) {
  186. ufs_sprd_n6_host_reset(hba);
  187. val |= CONTROLLER_ENABLE;
  188. ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
  189. usleep_range(1000, 1100);
  190. if (retry) {
  191. retry--;
  192. goto check_hce;
  193. }
  194. goto disable_crypto;
  195. }
  196. arm_smccc_smc(SPRD_SIP_SVC_STORAGE_UFS_CRYPTO_ENABLE,
  197. 0, 0, 0, 0, 0, 0, 0, &res);
  198. if (!res.a0)
  199. return;
  200. disable_crypto:
  201. dev_err(hba->dev, "key reg access enable fail, disable crypto\n");
  202. hba->caps &= ~UFSHCD_CAP_CRYPTO;
  203. }
  204. static int ufs_sprd_n6_init(struct ufs_hba *hba)
  205. {
  206. struct ufs_sprd_priv *priv;
  207. int ret = 0;
  208. ret = ufs_sprd_common_init(hba);
  209. if (ret != 0)
  210. return ret;
  211. priv = ufs_sprd_get_priv_data(hba);
  212. ret = regulator_enable(priv->vregi[SPRD_UFS_VDD_MPHY].vreg);
  213. if (ret)
  214. return -ENODEV;
  215. if (hba->caps & UFSHCD_CAP_CRYPTO)
  216. ufs_sprd_n6_key_acc_enable(hba);
  217. return 0;
  218. }
  219. static int ufs_sprd_n6_phy_init(struct ufs_hba *hba)
  220. {
  221. int ret = 0;
  222. uint32_t val = 0;
  223. uint32_t retry = 10;
  224. uint32_t offset;
  225. struct ufs_sprd_priv *priv = ufs_sprd_get_priv_data(hba);
  226. ufshcd_dme_set(hba, UIC_ARG_MIB(CBREFCLKCTRL2), 0x90);
  227. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCRCTRL), 0x01);
  228. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RXSQCONTROL,
  229. UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), 0x01);
  230. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RXSQCONTROL,
  231. UIC_ARG_MPHY_RX_GEN_SEL_INDEX(1)), 0x01);
  232. ufshcd_dme_set(hba, UIC_ARG_MIB(VS_MPHYCFGUPDT), 0x01);
  233. ufshcd_dme_set(hba, UIC_ARG_MIB(CBRATESEL), 0x01);
  234. do {
  235. /* phy_sram_init_done */
  236. ufs_sprd_regmap_read(priv, SPRD_UFS_ANLG, 0xc, &val);
  237. if ((val & 0x1) == 0x1) {
  238. for (offset = 0x40; offset < 0x42; offset++) {
  239. /* Lane afe calibration */
  240. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCREGADDRLSB), 0x1c);
  241. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCREGADDRMSB), offset);
  242. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCREGWRLSB), 0x04);
  243. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCREGWRMSB), 0x00);
  244. ufshcd_dme_set(hba, UIC_ARG_MIB(CBCREGRDWRSEL), 0x01);
  245. ufshcd_dme_set(hba, UIC_ARG_MIB(VS_MPHYCFGUPDT), 0x01);
  246. }
  247. goto update_phy;
  248. }
  249. udelay(1000);
  250. retry--;
  251. } while (retry > 0);
  252. ret = -ETIMEDOUT;
  253. goto out;
  254. update_phy:
  255. /* phy_sram_ext_ld_done */
  256. ufs_sprd_regmap_update(priv, SPRD_UFS_ANLG, 0xc, 0x2, 0);
  257. ufshcd_dme_set(hba, UIC_ARG_MIB(VS_MPHYCFGUPDT), 0x01);
  258. ufshcd_dme_set(hba, UIC_ARG_MIB(VS_MPHYDISABLE), 0x0);
  259. out:
  260. return ret;
  261. }
  262. static int sprd_ufs_n6_hce_enable_notify(struct ufs_hba *hba,
  263. enum ufs_notify_change_status status)
  264. {
  265. int err = 0;
  266. struct ufs_sprd_priv *priv = ufs_sprd_get_priv_data(hba);
  267. if (status == PRE_CHANGE) {
  268. /* phy_sram_ext_ld_done */
  269. ufs_sprd_regmap_update(priv, SPRD_UFS_ANLG, 0xc, 0x2, 0x2);
  270. /* phy_sram_bypass */
  271. ufs_sprd_regmap_update(priv, SPRD_UFS_ANLG, 0xc, 0x4, 0x4);
  272. ufs_sprd_n6_host_reset(hba);
  273. if (hba->caps & UFSHCD_CAP_CRYPTO)
  274. ufs_sprd_n6_key_acc_enable(hba);
  275. }
  276. if (status == POST_CHANGE) {
  277. err = ufs_sprd_n6_phy_init(hba);
  278. if (err) {
  279. dev_err(hba->dev, "Phy setup failed (%d)\n", err);
  280. goto out;
  281. }
  282. ufs_sprd_get_unipro_ver(hba);
  283. }
  284. out:
  285. return err;
  286. }
  287. static void sprd_ufs_n6_h8_notify(struct ufs_hba *hba,
  288. enum uic_cmd_dme cmd,
  289. enum ufs_notify_change_status status)
  290. {
  291. struct ufs_sprd_priv *priv = ufs_sprd_get_priv_data(hba);
  292. if (status == PRE_CHANGE) {
  293. if (cmd == UIC_CMD_DME_HIBER_ENTER)
  294. /*
  295. * Disable UIC COMPL INTR to prevent access to UFSHCI after
  296. * checking HCS.UPMCRS
  297. */
  298. ufs_sprd_ctrl_uic_compl(hba, false);
  299. if (cmd == UIC_CMD_DME_HIBER_EXIT) {
  300. ufs_sprd_regmap_update(priv, SPRD_UFS_AON_APB, APB_UFSDEV_REG,
  301. APB_UFSDEV_REFCLK_EN, APB_UFSDEV_REFCLK_EN);
  302. ufs_sprd_regmap_update(priv, SPRD_UFS_AON_APB, APB_USB31PLL_CTRL,
  303. APB_USB31PLLV_REF2MPHY, APB_USB31PLLV_REF2MPHY);
  304. }
  305. }
  306. if (status == POST_CHANGE) {
  307. if (cmd == UIC_CMD_DME_HIBER_EXIT)
  308. ufs_sprd_ctrl_uic_compl(hba, true);
  309. if (cmd == UIC_CMD_DME_HIBER_ENTER) {
  310. ufs_sprd_regmap_update(priv, SPRD_UFS_AON_APB, APB_UFSDEV_REG,
  311. APB_UFSDEV_REFCLK_EN, 0);
  312. ufs_sprd_regmap_update(priv, SPRD_UFS_AON_APB, APB_USB31PLL_CTRL,
  313. APB_USB31PLLV_REF2MPHY, 0);
  314. }
  315. }
  316. }
  317. static struct ufs_sprd_priv n6_ufs = {
  318. .rci[SPRD_UFSHCI_SOFT_RST] = { .name = "controller", },
  319. .rci[SPRD_UFS_DEV_RST] = { .name = "device", },
  320. .sysci[SPRD_UFS_ANLG] = { .name = "sprd,ufs-anlg-syscon", },
  321. .sysci[SPRD_UFS_AON_APB] = { .name = "sprd,aon-apb-syscon", },
  322. .vregi[SPRD_UFS_VDD_MPHY] = { .name = "vdd-mphy", },
  323. .ufs_hba_sprd_vops = {
  324. .name = "sprd,ums9620-ufs",
  325. .init = ufs_sprd_n6_init,
  326. .hce_enable_notify = sprd_ufs_n6_hce_enable_notify,
  327. .pwr_change_notify = sprd_ufs_pwr_change_notify,
  328. .hibern8_notify = sprd_ufs_n6_h8_notify,
  329. .device_reset = ufs_sprd_n6_device_reset,
  330. .suspend = ufs_sprd_suspend,
  331. },
  332. };
  333. static const struct of_device_id __maybe_unused ufs_sprd_of_match[] = {
  334. { .compatible = "sprd,ums9620-ufs", .data = &n6_ufs.ufs_hba_sprd_vops},
  335. {},
  336. };
  337. MODULE_DEVICE_TABLE(of, ufs_sprd_of_match);
  338. static int ufs_sprd_probe(struct platform_device *pdev)
  339. {
  340. int err;
  341. struct device *dev = &pdev->dev;
  342. const struct of_device_id *of_id;
  343. of_id = of_match_node(ufs_sprd_of_match, dev->of_node);
  344. err = ufshcd_pltfrm_init(pdev, of_id->data);
  345. if (err)
  346. dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
  347. return err;
  348. }
  349. static int ufs_sprd_remove(struct platform_device *pdev)
  350. {
  351. struct ufs_hba *hba = platform_get_drvdata(pdev);
  352. pm_runtime_get_sync(&(pdev)->dev);
  353. ufshcd_remove(hba);
  354. return 0;
  355. }
  356. static const struct dev_pm_ops ufs_sprd_pm_ops = {
  357. SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  358. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  359. .prepare = ufshcd_suspend_prepare,
  360. .complete = ufshcd_resume_complete,
  361. };
  362. static struct platform_driver ufs_sprd_pltform = {
  363. .probe = ufs_sprd_probe,
  364. .remove = ufs_sprd_remove,
  365. .shutdown = ufshcd_pltfrm_shutdown,
  366. .driver = {
  367. .name = "ufshcd-sprd",
  368. .pm = &ufs_sprd_pm_ops,
  369. .of_match_table = of_match_ptr(ufs_sprd_of_match),
  370. },
  371. };
  372. module_platform_driver(ufs_sprd_pltform);
  373. MODULE_AUTHOR("Zhe Wang <[email protected]>");
  374. MODULE_DESCRIPTION("Unisoc UFS Host Driver");
  375. MODULE_LICENSE("GPL v2");