dw_ufs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <drivers/dw_ufs.h>
  11. #include <drivers/ufs.h>
  12. #include <lib/mmio.h>
  13. static int dwufs_phy_init(ufs_params_t *params)
  14. {
  15. uintptr_t base;
  16. unsigned int fsm0, fsm1;
  17. unsigned int data;
  18. int result;
  19. assert((params != NULL) && (params->reg_base != 0));
  20. base = params->reg_base;
  21. /* Unipro VS_MPHY disable */
  22. ufshc_dme_set(VS_MPHY_DISABLE_OFFSET, 0, VS_MPHY_DISABLE_MPHYDIS);
  23. ufshc_dme_set(PA_HS_SERIES_OFFSET, 0, 2);
  24. /* MPHY CBRATESEL */
  25. ufshc_dme_set(0x8114, 0, 1);
  26. /* MPHY CBOVRCTRL2 */
  27. ufshc_dme_set(0x8121, 0, 0x2d);
  28. /* MPHY CBOVRCTRL3 */
  29. ufshc_dme_set(0x8122, 0, 0x1);
  30. ufshc_dme_set(VS_MPHY_CFG_UPDT_OFFSET, 0, 1);
  31. /* MPHY RXOVRCTRL4 rx0 */
  32. ufshc_dme_set(0x800d, 4, 0x58);
  33. /* MPHY RXOVRCTRL4 rx1 */
  34. ufshc_dme_set(0x800d, 5, 0x58);
  35. /* MPHY RXOVRCTRL5 rx0 */
  36. ufshc_dme_set(0x800e, 4, 0xb);
  37. /* MPHY RXOVRCTRL5 rx1 */
  38. ufshc_dme_set(0x800e, 5, 0xb);
  39. /* MPHY RXSQCONTROL rx0 */
  40. ufshc_dme_set(0x8009, 4, 0x1);
  41. /* MPHY RXSQCONTROL rx1 */
  42. ufshc_dme_set(0x8009, 5, 0x1);
  43. ufshc_dme_set(VS_MPHY_CFG_UPDT_OFFSET, 0, 1);
  44. ufshc_dme_set(0x8113, 0, 0x1);
  45. ufshc_dme_set(VS_MPHY_CFG_UPDT_OFFSET, 0, 1);
  46. ufshc_dme_set(RX_HS_G3_SYNC_LENGTH_CAP_OFFSET, 4, 0x4a);
  47. ufshc_dme_set(RX_HS_G3_SYNC_LENGTH_CAP_OFFSET, 5, 0x4a);
  48. ufshc_dme_set(RX_HS_G2_SYNC_LENGTH_CAP_OFFSET, 4, 0x4a);
  49. ufshc_dme_set(RX_HS_G2_SYNC_LENGTH_CAP_OFFSET, 5, 0x4a);
  50. ufshc_dme_set(RX_MIN_ACTIVATETIME_CAP_OFFSET, 4, 0x7);
  51. ufshc_dme_set(RX_MIN_ACTIVATETIME_CAP_OFFSET, 5, 0x7);
  52. ufshc_dme_set(TX_HIBERN8TIME_CAP_OFFSET, 0, 0x5);
  53. ufshc_dme_set(TX_HIBERN8TIME_CAP_OFFSET, 1, 0x5);
  54. ufshc_dme_set(VS_MPHY_CFG_UPDT_OFFSET, 0, 1);
  55. result = ufshc_dme_get(VS_MPHY_DISABLE_OFFSET, 0, &data);
  56. assert((result == 0) && (data == VS_MPHY_DISABLE_MPHYDIS));
  57. /* enable Unipro VS MPHY */
  58. ufshc_dme_set(VS_MPHY_DISABLE_OFFSET, 0, 0);
  59. while (1) {
  60. result = ufshc_dme_get(TX_FSM_STATE_OFFSET, 0, &fsm0);
  61. assert(result == 0);
  62. result = ufshc_dme_get(TX_FSM_STATE_OFFSET, 1, &fsm1);
  63. assert(result == 0);
  64. if ((fsm0 == TX_FSM_STATE_HIBERN8) &&
  65. (fsm1 == TX_FSM_STATE_HIBERN8))
  66. break;
  67. }
  68. mmio_write_32(base + HCLKDIV, 0xE4);
  69. mmio_clrbits_32(base + AHIT, 0x3FF);
  70. ufshc_dme_set(PA_LOCAL_TX_LCC_ENABLE_OFFSET, 0, 0);
  71. ufshc_dme_set(VS_MK2_EXTN_SUPPORT_OFFSET, 0, 0);
  72. result = ufshc_dme_get(VS_MK2_EXTN_SUPPORT_OFFSET, 0, &data);
  73. assert((result == 0) && (data == 0));
  74. ufshc_dme_set(DL_AFC0_CREDIT_THRESHOLD_OFFSET, 0, 0);
  75. ufshc_dme_set(DL_TC0_OUT_ACK_THRESHOLD_OFFSET, 0, 0);
  76. ufshc_dme_set(DL_TC0_TX_FC_THRESHOLD_OFFSET, 0, 9);
  77. (void)result;
  78. return 0;
  79. }
  80. static int dwufs_phy_set_pwr_mode(ufs_params_t *params)
  81. {
  82. int result;
  83. unsigned int data, tx_lanes, rx_lanes;
  84. uintptr_t base;
  85. unsigned int flags;
  86. assert((params != NULL) && (params->reg_base != 0));
  87. base = params->reg_base;
  88. flags = params->flags;
  89. if ((flags & UFS_FLAGS_VENDOR_SKHYNIX) != 0U) {
  90. NOTICE("ufs: H**** device must set VS_DebugSaveConfigTime 0x10\n");
  91. /* VS_DebugSaveConfigTime */
  92. result = ufshc_dme_set(0xd0a0, 0x0, 0x10);
  93. assert(result == 0);
  94. /* sync length */
  95. result = ufshc_dme_set(0x1556, 0x0, 0x48);
  96. assert(result == 0);
  97. }
  98. result = ufshc_dme_get(PA_TACTIVATE_OFFSET, 0, &data);
  99. assert(result == 0);
  100. if (data < 7) {
  101. result = ufshc_dme_set(PA_TACTIVATE_OFFSET, 0, 7);
  102. assert(result == 0);
  103. }
  104. result = ufshc_dme_get(PA_CONNECTED_TX_DATA_LANES_OFFSET, 0, &tx_lanes);
  105. assert(result == 0);
  106. result = ufshc_dme_get(PA_CONNECTED_RX_DATA_LANES_OFFSET, 0, &rx_lanes);
  107. assert(result == 0);
  108. result = ufshc_dme_set(PA_TX_SKIP_OFFSET, 0, 0);
  109. assert(result == 0);
  110. result = ufshc_dme_set(PA_TX_GEAR_OFFSET, 0, 3);
  111. assert(result == 0);
  112. result = ufshc_dme_set(PA_RX_GEAR_OFFSET, 0, 3);
  113. assert(result == 0);
  114. result = ufshc_dme_set(PA_HS_SERIES_OFFSET, 0, 2);
  115. assert(result == 0);
  116. result = ufshc_dme_set(PA_TX_TERMINATION_OFFSET, 0, 1);
  117. assert(result == 0);
  118. result = ufshc_dme_set(PA_RX_TERMINATION_OFFSET, 0, 1);
  119. assert(result == 0);
  120. result = ufshc_dme_set(PA_SCRAMBLING_OFFSET, 0, 0);
  121. assert(result == 0);
  122. result = ufshc_dme_set(PA_ACTIVE_TX_DATA_LANES_OFFSET, 0, tx_lanes);
  123. assert(result == 0);
  124. result = ufshc_dme_set(PA_ACTIVE_RX_DATA_LANES_OFFSET, 0, rx_lanes);
  125. assert(result == 0);
  126. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA0_OFFSET, 0, 8191);
  127. assert(result == 0);
  128. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA1_OFFSET, 0, 65535);
  129. assert(result == 0);
  130. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA2_OFFSET, 0, 32767);
  131. assert(result == 0);
  132. result = ufshc_dme_set(DME_FC0_PROTECTION_TIMEOUT_OFFSET, 0, 8191);
  133. assert(result == 0);
  134. result = ufshc_dme_set(DME_TC0_REPLAY_TIMEOUT_OFFSET, 0, 65535);
  135. assert(result == 0);
  136. result = ufshc_dme_set(DME_AFC0_REQ_TIMEOUT_OFFSET, 0, 32767);
  137. assert(result == 0);
  138. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA3_OFFSET, 0, 8191);
  139. assert(result == 0);
  140. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA4_OFFSET, 0, 65535);
  141. assert(result == 0);
  142. result = ufshc_dme_set(PA_PWR_MODE_USER_DATA5_OFFSET, 0, 32767);
  143. assert(result == 0);
  144. result = ufshc_dme_set(DME_FC1_PROTECTION_TIMEOUT_OFFSET, 0, 8191);
  145. assert(result == 0);
  146. result = ufshc_dme_set(DME_TC1_REPLAY_TIMEOUT_OFFSET, 0, 65535);
  147. assert(result == 0);
  148. result = ufshc_dme_set(DME_AFC1_REQ_TIMEOUT_OFFSET, 0, 32767);
  149. assert(result == 0);
  150. result = ufshc_dme_set(PA_PWR_MODE_OFFSET, 0, 0x11);
  151. assert(result == 0);
  152. do {
  153. data = mmio_read_32(base + IS);
  154. } while ((data & UFS_INT_UPMS) == 0);
  155. mmio_write_32(base + IS, UFS_INT_UPMS);
  156. data = mmio_read_32(base + HCS);
  157. if ((data & HCS_UPMCRS_MASK) == HCS_PWR_LOCAL)
  158. INFO("ufs: change power mode success\n");
  159. else
  160. WARN("ufs: HCS.UPMCRS error, HCS:0x%x\n", data);
  161. (void)result;
  162. return 0;
  163. }
  164. static const ufs_ops_t dw_ufs_ops = {
  165. .phy_init = dwufs_phy_init,
  166. .phy_set_pwr_mode = dwufs_phy_set_pwr_mode,
  167. };
  168. int dw_ufs_init(dw_ufs_params_t *params)
  169. {
  170. ufs_params_t ufs_params;
  171. memset(&ufs_params, 0, sizeof(ufs_params));
  172. ufs_params.reg_base = params->reg_base;
  173. ufs_params.desc_base = params->desc_base;
  174. ufs_params.desc_size = params->desc_size;
  175. ufs_params.flags = params->flags;
  176. ufs_init(&dw_ufs_ops, &ufs_params);
  177. return 0;
  178. }