ufs-hisi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HiSilicon Hixxxx UFS Driver
  4. *
  5. * Copyright (c) 2016-2017 Linaro Ltd.
  6. * Copyright (c) 2016-2017 HiSilicon Technologies Co., Ltd.
  7. */
  8. #include <linux/time.h>
  9. #include <linux/delay.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. #include <ufs/ufshcd.h>
  17. #include "ufshcd-pltfrm.h"
  18. #include <ufs/unipro.h>
  19. #include "ufs-hisi.h"
  20. #include <ufs/ufshci.h>
  21. #include <ufs/ufs_quirks.h>
  22. static int ufs_hisi_check_hibern8(struct ufs_hba *hba)
  23. {
  24. int err = 0;
  25. u32 tx_fsm_val_0 = 0;
  26. u32 tx_fsm_val_1 = 0;
  27. unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
  28. do {
  29. err = ufshcd_dme_get(hba, UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 0),
  30. &tx_fsm_val_0);
  31. err |= ufshcd_dme_get(hba,
  32. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 1), &tx_fsm_val_1);
  33. if (err || (tx_fsm_val_0 == TX_FSM_HIBERN8 &&
  34. tx_fsm_val_1 == TX_FSM_HIBERN8))
  35. break;
  36. /* sleep for max. 200us */
  37. usleep_range(100, 200);
  38. } while (time_before(jiffies, timeout));
  39. /*
  40. * we might have scheduled out for long during polling so
  41. * check the state again.
  42. */
  43. if (time_after(jiffies, timeout)) {
  44. err = ufshcd_dme_get(hba, UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 0),
  45. &tx_fsm_val_0);
  46. err |= ufshcd_dme_get(hba,
  47. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 1), &tx_fsm_val_1);
  48. }
  49. if (err) {
  50. dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
  51. __func__, err);
  52. } else if (tx_fsm_val_0 != TX_FSM_HIBERN8 ||
  53. tx_fsm_val_1 != TX_FSM_HIBERN8) {
  54. err = -1;
  55. dev_err(hba->dev, "%s: invalid TX_FSM_STATE, lane0 = %d, lane1 = %d\n",
  56. __func__, tx_fsm_val_0, tx_fsm_val_1);
  57. }
  58. return err;
  59. }
  60. static void ufs_hisi_clk_init(struct ufs_hba *hba)
  61. {
  62. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  63. ufs_sys_ctrl_clr_bits(host, BIT_SYSCTRL_REF_CLOCK_EN, PHY_CLK_CTRL);
  64. if (ufs_sys_ctrl_readl(host, PHY_CLK_CTRL) & BIT_SYSCTRL_REF_CLOCK_EN)
  65. mdelay(1);
  66. /* use abb clk */
  67. ufs_sys_ctrl_clr_bits(host, BIT_UFS_REFCLK_SRC_SEl, UFS_SYSCTRL);
  68. ufs_sys_ctrl_clr_bits(host, BIT_UFS_REFCLK_ISO_EN, PHY_ISO_EN);
  69. /* open mphy ref clk */
  70. ufs_sys_ctrl_set_bits(host, BIT_SYSCTRL_REF_CLOCK_EN, PHY_CLK_CTRL);
  71. }
  72. static void ufs_hisi_soc_init(struct ufs_hba *hba)
  73. {
  74. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  75. u32 reg;
  76. if (!IS_ERR(host->rst))
  77. reset_control_assert(host->rst);
  78. /* HC_PSW powerup */
  79. ufs_sys_ctrl_set_bits(host, BIT_UFS_PSW_MTCMOS_EN, PSW_POWER_CTRL);
  80. udelay(10);
  81. /* notify PWR ready */
  82. ufs_sys_ctrl_set_bits(host, BIT_SYSCTRL_PWR_READY, HC_LP_CTRL);
  83. ufs_sys_ctrl_writel(host, MASK_UFS_DEVICE_RESET | 0,
  84. UFS_DEVICE_RESET_CTRL);
  85. reg = ufs_sys_ctrl_readl(host, PHY_CLK_CTRL);
  86. reg = (reg & ~MASK_SYSCTRL_CFG_CLOCK_FREQ) | UFS_FREQ_CFG_CLK;
  87. /* set cfg clk freq */
  88. ufs_sys_ctrl_writel(host, reg, PHY_CLK_CTRL);
  89. /* set ref clk freq */
  90. ufs_sys_ctrl_clr_bits(host, MASK_SYSCTRL_REF_CLOCK_SEL, PHY_CLK_CTRL);
  91. /* bypass ufs clk gate */
  92. ufs_sys_ctrl_set_bits(host, MASK_UFS_CLK_GATE_BYPASS,
  93. CLOCK_GATE_BYPASS);
  94. ufs_sys_ctrl_set_bits(host, MASK_UFS_SYSCRTL_BYPASS, UFS_SYSCTRL);
  95. /* open psw clk */
  96. ufs_sys_ctrl_set_bits(host, BIT_SYSCTRL_PSW_CLK_EN, PSW_CLK_CTRL);
  97. /* disable ufshc iso */
  98. ufs_sys_ctrl_clr_bits(host, BIT_UFS_PSW_ISO_CTRL, PSW_POWER_CTRL);
  99. /* disable phy iso */
  100. ufs_sys_ctrl_clr_bits(host, BIT_UFS_PHY_ISO_CTRL, PHY_ISO_EN);
  101. /* notice iso disable */
  102. ufs_sys_ctrl_clr_bits(host, BIT_SYSCTRL_LP_ISOL_EN, HC_LP_CTRL);
  103. /* disable lp_reset_n */
  104. ufs_sys_ctrl_set_bits(host, BIT_SYSCTRL_LP_RESET_N, RESET_CTRL_EN);
  105. mdelay(1);
  106. ufs_sys_ctrl_writel(host, MASK_UFS_DEVICE_RESET | BIT_UFS_DEVICE_RESET,
  107. UFS_DEVICE_RESET_CTRL);
  108. msleep(20);
  109. /*
  110. * enable the fix of linereset recovery,
  111. * and enable rx_reset/tx_rest beat
  112. * enable ref_clk_en override(bit5) &
  113. * override value = 1(bit4), with mask
  114. */
  115. ufs_sys_ctrl_writel(host, 0x03300330, UFS_DEVICE_RESET_CTRL);
  116. if (!IS_ERR(host->rst))
  117. reset_control_deassert(host->rst);
  118. }
  119. static int ufs_hisi_link_startup_pre_change(struct ufs_hba *hba)
  120. {
  121. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  122. int err;
  123. uint32_t value;
  124. uint32_t reg;
  125. /* Unipro VS_mphy_disable */
  126. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD0C1, 0x0), 0x1);
  127. /* PA_HSSeries */
  128. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x156A, 0x0), 0x2);
  129. /* MPHY CBRATESEL */
  130. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8114, 0x0), 0x1);
  131. /* MPHY CBOVRCTRL2 */
  132. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8121, 0x0), 0x2D);
  133. /* MPHY CBOVRCTRL3 */
  134. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8122, 0x0), 0x1);
  135. if (host->caps & UFS_HISI_CAP_PHY10nm) {
  136. /* MPHY CBOVRCTRL4 */
  137. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8127, 0x0), 0x98);
  138. /* MPHY CBOVRCTRL5 */
  139. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8128, 0x0), 0x1);
  140. }
  141. /* Unipro VS_MphyCfgUpdt */
  142. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD085, 0x0), 0x1);
  143. /* MPHY RXOVRCTRL4 rx0 */
  144. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x800D, 0x4), 0x58);
  145. /* MPHY RXOVRCTRL4 rx1 */
  146. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x800D, 0x5), 0x58);
  147. /* MPHY RXOVRCTRL5 rx0 */
  148. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x800E, 0x4), 0xB);
  149. /* MPHY RXOVRCTRL5 rx1 */
  150. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x800E, 0x5), 0xB);
  151. /* MPHY RXSQCONTROL rx0 */
  152. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8009, 0x4), 0x1);
  153. /* MPHY RXSQCONTROL rx1 */
  154. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8009, 0x5), 0x1);
  155. /* Unipro VS_MphyCfgUpdt */
  156. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD085, 0x0), 0x1);
  157. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8113, 0x0), 0x1);
  158. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD085, 0x0), 0x1);
  159. if (host->caps & UFS_HISI_CAP_PHY10nm) {
  160. /* RX_Hibern8Time_Capability*/
  161. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0092, 0x4), 0xA);
  162. /* RX_Hibern8Time_Capability*/
  163. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0092, 0x5), 0xA);
  164. /* RX_Min_ActivateTime */
  165. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008f, 0x4), 0xA);
  166. /* RX_Min_ActivateTime*/
  167. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008f, 0x5), 0xA);
  168. } else {
  169. /* Tactive RX */
  170. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008F, 0x4), 0x7);
  171. /* Tactive RX */
  172. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008F, 0x5), 0x7);
  173. }
  174. /* Gear3 Synclength */
  175. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0095, 0x4), 0x4F);
  176. /* Gear3 Synclength */
  177. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0095, 0x5), 0x4F);
  178. /* Gear2 Synclength */
  179. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0094, 0x4), 0x4F);
  180. /* Gear2 Synclength */
  181. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0094, 0x5), 0x4F);
  182. /* Gear1 Synclength */
  183. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008B, 0x4), 0x4F);
  184. /* Gear1 Synclength */
  185. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x008B, 0x5), 0x4F);
  186. /* Thibernate Tx */
  187. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x000F, 0x0), 0x5);
  188. /* Thibernate Tx */
  189. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x000F, 0x1), 0x5);
  190. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD085, 0x0), 0x1);
  191. /* Unipro VS_mphy_disable */
  192. ufshcd_dme_get(hba, UIC_ARG_MIB_SEL(0xD0C1, 0x0), &value);
  193. if (value != 0x1)
  194. dev_info(hba->dev,
  195. "Warring!!! Unipro VS_mphy_disable is 0x%x\n", value);
  196. /* Unipro VS_mphy_disable */
  197. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD0C1, 0x0), 0x0);
  198. err = ufs_hisi_check_hibern8(hba);
  199. if (err)
  200. dev_err(hba->dev, "ufs_hisi_check_hibern8 error\n");
  201. if (!(host->caps & UFS_HISI_CAP_PHY10nm))
  202. ufshcd_writel(hba, UFS_HCLKDIV_NORMAL_VALUE, UFS_REG_HCLKDIV);
  203. /* disable auto H8 */
  204. reg = ufshcd_readl(hba, REG_AUTO_HIBERNATE_IDLE_TIMER);
  205. reg = reg & (~UFS_AHIT_AH8ITV_MASK);
  206. ufshcd_writel(hba, reg, REG_AUTO_HIBERNATE_IDLE_TIMER);
  207. /* Unipro PA_Local_TX_LCC_Enable */
  208. ufshcd_disable_host_tx_lcc(hba);
  209. /* close Unipro VS_Mk2ExtnSupport */
  210. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xD0AB, 0x0), 0x0);
  211. ufshcd_dme_get(hba, UIC_ARG_MIB_SEL(0xD0AB, 0x0), &value);
  212. if (value != 0) {
  213. /* Ensure close success */
  214. dev_info(hba->dev, "WARN: close VS_Mk2ExtnSupport failed\n");
  215. }
  216. return err;
  217. }
  218. static int ufs_hisi_link_startup_post_change(struct ufs_hba *hba)
  219. {
  220. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  221. /* Unipro DL_AFC0CreditThreshold */
  222. ufshcd_dme_set(hba, UIC_ARG_MIB(0x2044), 0x0);
  223. /* Unipro DL_TC0OutAckThreshold */
  224. ufshcd_dme_set(hba, UIC_ARG_MIB(0x2045), 0x0);
  225. /* Unipro DL_TC0TXFCThreshold */
  226. ufshcd_dme_set(hba, UIC_ARG_MIB(0x2040), 0x9);
  227. /* not bypass ufs clk gate */
  228. ufs_sys_ctrl_clr_bits(host, MASK_UFS_CLK_GATE_BYPASS,
  229. CLOCK_GATE_BYPASS);
  230. ufs_sys_ctrl_clr_bits(host, MASK_UFS_SYSCRTL_BYPASS,
  231. UFS_SYSCTRL);
  232. /* select received symbol cnt */
  233. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd09a), 0x80000000);
  234. /* reset counter0 and enable */
  235. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd09c), 0x00000005);
  236. return 0;
  237. }
  238. static int ufs_hisi_link_startup_notify(struct ufs_hba *hba,
  239. enum ufs_notify_change_status status)
  240. {
  241. int err = 0;
  242. switch (status) {
  243. case PRE_CHANGE:
  244. err = ufs_hisi_link_startup_pre_change(hba);
  245. break;
  246. case POST_CHANGE:
  247. err = ufs_hisi_link_startup_post_change(hba);
  248. break;
  249. default:
  250. break;
  251. }
  252. return err;
  253. }
  254. static void ufs_hisi_set_dev_cap(struct ufs_host_params *host_params)
  255. {
  256. ufshcd_init_host_params(host_params);
  257. }
  258. static void ufs_hisi_pwr_change_pre_change(struct ufs_hba *hba)
  259. {
  260. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  261. if (host->caps & UFS_HISI_CAP_PHY10nm) {
  262. /*
  263. * Boston platform need to set SaveConfigTime to 0x13,
  264. * and change sync length to maximum value
  265. */
  266. /* VS_DebugSaveConfigTime */
  267. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0xD0A0), 0x13);
  268. /* g1 sync length */
  269. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0x1552), 0x4f);
  270. /* g2 sync length */
  271. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0x1554), 0x4f);
  272. /* g3 sync length */
  273. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0x1556), 0x4f);
  274. /* PA_Hibern8Time */
  275. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0x15a7), 0xA);
  276. /* PA_Tactivate */
  277. ufshcd_dme_set(hba, UIC_ARG_MIB((u32)0x15a8), 0xA);
  278. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xd085, 0x0), 0x01);
  279. }
  280. if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME) {
  281. pr_info("ufs flash device must set VS_DebugSaveConfigTime 0x10\n");
  282. /* VS_DebugSaveConfigTime */
  283. ufshcd_dme_set(hba, UIC_ARG_MIB(0xD0A0), 0x10);
  284. /* sync length */
  285. ufshcd_dme_set(hba, UIC_ARG_MIB(0x1556), 0x48);
  286. }
  287. /* update */
  288. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15A8), 0x1);
  289. /* PA_TxSkip */
  290. ufshcd_dme_set(hba, UIC_ARG_MIB(0x155c), 0x0);
  291. /*PA_PWRModeUserData0 = 8191, default is 0*/
  292. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b0), SZ_8K - 1);
  293. /*PA_PWRModeUserData1 = 65535, default is 0*/
  294. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b1), SZ_64K - 1);
  295. /*PA_PWRModeUserData2 = 32767, default is 0*/
  296. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b2), SZ_32K - 1);
  297. /*DME_FC0ProtectionTimeOutVal = 8191, default is 0*/
  298. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd041), SZ_8K - 1);
  299. /*DME_TC0ReplayTimeOutVal = 65535, default is 0*/
  300. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd042), SZ_64K - 1);
  301. /*DME_AFC0ReqTimeOutVal = 32767, default is 0*/
  302. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd043), SZ_32K - 1);
  303. /*PA_PWRModeUserData3 = 8191, default is 0*/
  304. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b3), SZ_8K - 1);
  305. /*PA_PWRModeUserData4 = 65535, default is 0*/
  306. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b4), SZ_64K - 1);
  307. /*PA_PWRModeUserData5 = 32767, default is 0*/
  308. ufshcd_dme_set(hba, UIC_ARG_MIB(0x15b5), SZ_32K - 1);
  309. /*DME_FC1ProtectionTimeOutVal = 8191, default is 0*/
  310. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd044), SZ_8K - 1);
  311. /*DME_TC1ReplayTimeOutVal = 65535, default is 0*/
  312. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd045), SZ_64K - 1);
  313. /*DME_AFC1ReqTimeOutVal = 32767, default is 0*/
  314. ufshcd_dme_set(hba, UIC_ARG_MIB(0xd046), SZ_32K - 1);
  315. }
  316. static int ufs_hisi_pwr_change_notify(struct ufs_hba *hba,
  317. enum ufs_notify_change_status status,
  318. struct ufs_pa_layer_attr *dev_max_params,
  319. struct ufs_pa_layer_attr *dev_req_params)
  320. {
  321. struct ufs_host_params host_params;
  322. int ret = 0;
  323. if (!dev_req_params) {
  324. dev_err(hba->dev,
  325. "%s: incoming dev_req_params is NULL\n", __func__);
  326. ret = -EINVAL;
  327. goto out;
  328. }
  329. switch (status) {
  330. case PRE_CHANGE:
  331. ufs_hisi_set_dev_cap(&host_params);
  332. ret = ufshcd_negotiate_pwr_params(&host_params, dev_max_params, dev_req_params);
  333. if (ret) {
  334. dev_err(hba->dev,
  335. "%s: failed to determine capabilities\n", __func__);
  336. goto out;
  337. }
  338. ufs_hisi_pwr_change_pre_change(hba);
  339. break;
  340. case POST_CHANGE:
  341. break;
  342. default:
  343. ret = -EINVAL;
  344. break;
  345. }
  346. out:
  347. return ret;
  348. }
  349. static int ufs_hisi_suspend_prepare(struct device *dev)
  350. {
  351. /* RPM and SPM are different. Refer ufs_hisi_suspend() */
  352. return __ufshcd_suspend_prepare(dev, false);
  353. }
  354. static int ufs_hisi_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  355. enum ufs_notify_change_status status)
  356. {
  357. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  358. if (status == PRE_CHANGE)
  359. return 0;
  360. if (pm_op == UFS_RUNTIME_PM)
  361. return 0;
  362. if (host->in_suspend) {
  363. WARN_ON(1);
  364. return 0;
  365. }
  366. ufs_sys_ctrl_clr_bits(host, BIT_SYSCTRL_REF_CLOCK_EN, PHY_CLK_CTRL);
  367. udelay(10);
  368. /* set ref_dig_clk override of PHY PCS to 0 */
  369. ufs_sys_ctrl_writel(host, 0x00100000, UFS_DEVICE_RESET_CTRL);
  370. host->in_suspend = true;
  371. return 0;
  372. }
  373. static int ufs_hisi_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  374. {
  375. struct ufs_hisi_host *host = ufshcd_get_variant(hba);
  376. if (!host->in_suspend)
  377. return 0;
  378. /* set ref_dig_clk override of PHY PCS to 1 */
  379. ufs_sys_ctrl_writel(host, 0x00100010, UFS_DEVICE_RESET_CTRL);
  380. udelay(10);
  381. ufs_sys_ctrl_set_bits(host, BIT_SYSCTRL_REF_CLOCK_EN, PHY_CLK_CTRL);
  382. host->in_suspend = false;
  383. return 0;
  384. }
  385. static int ufs_hisi_get_resource(struct ufs_hisi_host *host)
  386. {
  387. struct device *dev = host->hba->dev;
  388. struct platform_device *pdev = to_platform_device(dev);
  389. /* get resource of ufs sys ctrl */
  390. host->ufs_sys_ctrl = devm_platform_ioremap_resource(pdev, 1);
  391. return PTR_ERR_OR_ZERO(host->ufs_sys_ctrl);
  392. }
  393. static void ufs_hisi_set_pm_lvl(struct ufs_hba *hba)
  394. {
  395. hba->rpm_lvl = UFS_PM_LVL_1;
  396. hba->spm_lvl = UFS_PM_LVL_3;
  397. }
  398. /**
  399. * ufs_hisi_init_common
  400. * @hba: host controller instance
  401. */
  402. static int ufs_hisi_init_common(struct ufs_hba *hba)
  403. {
  404. int err = 0;
  405. struct device *dev = hba->dev;
  406. struct ufs_hisi_host *host;
  407. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  408. if (!host)
  409. return -ENOMEM;
  410. host->hba = hba;
  411. ufshcd_set_variant(hba, host);
  412. host->rst = devm_reset_control_get(dev, "rst");
  413. if (IS_ERR(host->rst)) {
  414. dev_err(dev, "%s: failed to get reset control\n", __func__);
  415. err = PTR_ERR(host->rst);
  416. goto error;
  417. }
  418. ufs_hisi_set_pm_lvl(hba);
  419. err = ufs_hisi_get_resource(host);
  420. if (err)
  421. goto error;
  422. return 0;
  423. error:
  424. ufshcd_set_variant(hba, NULL);
  425. return err;
  426. }
  427. static int ufs_hi3660_init(struct ufs_hba *hba)
  428. {
  429. int ret = 0;
  430. struct device *dev = hba->dev;
  431. ret = ufs_hisi_init_common(hba);
  432. if (ret) {
  433. dev_err(dev, "%s: ufs common init fail\n", __func__);
  434. return ret;
  435. }
  436. ufs_hisi_clk_init(hba);
  437. ufs_hisi_soc_init(hba);
  438. return 0;
  439. }
  440. static int ufs_hi3670_init(struct ufs_hba *hba)
  441. {
  442. int ret = 0;
  443. struct device *dev = hba->dev;
  444. struct ufs_hisi_host *host;
  445. ret = ufs_hisi_init_common(hba);
  446. if (ret) {
  447. dev_err(dev, "%s: ufs common init fail\n", __func__);
  448. return ret;
  449. }
  450. ufs_hisi_clk_init(hba);
  451. ufs_hisi_soc_init(hba);
  452. /* Add cap for 10nm PHY variant on HI3670 SoC */
  453. host = ufshcd_get_variant(hba);
  454. host->caps |= UFS_HISI_CAP_PHY10nm;
  455. return 0;
  456. }
  457. static const struct ufs_hba_variant_ops ufs_hba_hi3660_vops = {
  458. .name = "hi3660",
  459. .init = ufs_hi3660_init,
  460. .link_startup_notify = ufs_hisi_link_startup_notify,
  461. .pwr_change_notify = ufs_hisi_pwr_change_notify,
  462. .suspend = ufs_hisi_suspend,
  463. .resume = ufs_hisi_resume,
  464. };
  465. static const struct ufs_hba_variant_ops ufs_hba_hi3670_vops = {
  466. .name = "hi3670",
  467. .init = ufs_hi3670_init,
  468. .link_startup_notify = ufs_hisi_link_startup_notify,
  469. .pwr_change_notify = ufs_hisi_pwr_change_notify,
  470. .suspend = ufs_hisi_suspend,
  471. .resume = ufs_hisi_resume,
  472. };
  473. static const struct of_device_id ufs_hisi_of_match[] = {
  474. { .compatible = "hisilicon,hi3660-ufs", .data = &ufs_hba_hi3660_vops },
  475. { .compatible = "hisilicon,hi3670-ufs", .data = &ufs_hba_hi3670_vops },
  476. {},
  477. };
  478. MODULE_DEVICE_TABLE(of, ufs_hisi_of_match);
  479. static int ufs_hisi_probe(struct platform_device *pdev)
  480. {
  481. const struct of_device_id *of_id;
  482. of_id = of_match_node(ufs_hisi_of_match, pdev->dev.of_node);
  483. return ufshcd_pltfrm_init(pdev, of_id->data);
  484. }
  485. static void ufs_hisi_remove(struct platform_device *pdev)
  486. {
  487. ufshcd_pltfrm_remove(pdev);
  488. }
  489. static const struct dev_pm_ops ufs_hisi_pm_ops = {
  490. SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  491. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  492. .prepare = ufs_hisi_suspend_prepare,
  493. .complete = ufshcd_resume_complete,
  494. };
  495. static struct platform_driver ufs_hisi_pltform = {
  496. .probe = ufs_hisi_probe,
  497. .remove_new = ufs_hisi_remove,
  498. .driver = {
  499. .name = "ufshcd-hisi",
  500. .pm = &ufs_hisi_pm_ops,
  501. .of_match_table = ufs_hisi_of_match,
  502. },
  503. };
  504. module_platform_driver(ufs_hisi_pltform);
  505. MODULE_LICENSE("GPL");
  506. MODULE_ALIAS("platform:ufshcd-hisi");
  507. MODULE_DESCRIPTION("HiSilicon Hixxxx UFS Driver");