1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Copyright (c) 2013-2015, The Linux Foundation. All rights reserved. 3 */ 4 5 #ifndef UFS_QCOM_H_ 6 #define UFS_QCOM_H_ 7 8 #include <linux/msi.h> 9 #include <linux/reset-controller.h> 10 #include <linux/reset.h> 11 #include <soc/qcom/ice.h> 12 #include <ufs/ufshcd.h> 13 14 #define MPHY_TX_FSM_STATE 0x41 15 #define TX_FSM_HIBERN8 0x1 16 #define HBRN8_POLL_TOUT_MS 100 17 #define DEFAULT_CLK_RATE_HZ 1000000 18 #define MAX_SUPP_MAC 64 19 #define MAX_ESI_VEC 32 20 21 #define UFS_HW_VER_MAJOR_MASK GENMASK(31, 28) 22 #define UFS_HW_VER_MINOR_MASK GENMASK(27, 16) 23 #define UFS_HW_VER_STEP_MASK GENMASK(15, 0) 24 #define UFS_DEV_VER_MAJOR_MASK GENMASK(7, 4) 25 26 #define UFS_QCOM_LIMIT_HS_RATE PA_HS_MODE_B 27 28 /* bit and mask definitions for PA_VS_CLK_CFG_REG attribute */ 29 #define PA_VS_CLK_CFG_REG 0x9004 30 #define PA_VS_CLK_CFG_REG_MASK GENMASK(8, 0) 31 32 /* bit and mask definitions for DL_VS_CLK_CFG attribute */ 33 #define DL_VS_CLK_CFG 0xA00B 34 #define DL_VS_CLK_CFG_MASK GENMASK(9, 0) 35 #define DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN BIT(9) 36 37 #define UFS_QCOM_EOM_VOLTAGE_STEPS_MAX 127 38 #define UFS_QCOM_EOM_TIMING_STEPS_MAX 63 39 #define UFS_QCOM_EOM_TARGET_TEST_COUNT_MIN 8 40 #define UFS_QCOM_EOM_TARGET_TEST_COUNT_G6 0x3F 41 42 #define SW_RX_FOM_EOM_COORDS 23 43 #define SW_RX_FOM_EOM_COORDS_WEIGHT (127 / SW_RX_FOM_EOM_COORDS) 44 45 struct ufs_eom_coord { 46 int t_step; 47 int v_step; 48 u8 eye_mask; 49 }; 50 51 static const struct ufs_eom_coord sw_rx_fom_eom_coords_g6[SW_RX_FOM_EOM_COORDS] = { 52 [0] = { -2, -15, UFS_EOM_EYE_MASK_M }, 53 [1] = { 0, -15, UFS_EOM_EYE_MASK_M }, 54 [2] = { 2, -15, UFS_EOM_EYE_MASK_M }, 55 [3] = { -4, -10, UFS_EOM_EYE_MASK_M }, 56 [4] = { -2, -10, UFS_EOM_EYE_MASK_M }, 57 [5] = { 0, -10, UFS_EOM_EYE_MASK_M }, 58 [6] = { 2, -10, UFS_EOM_EYE_MASK_M }, 59 [7] = { 4, -10, UFS_EOM_EYE_MASK_M }, 60 [8] = { -6, 0, UFS_EOM_EYE_MASK_M }, 61 [9] = { -4, 0, UFS_EOM_EYE_MASK_M }, 62 [10] = { -2, 0, UFS_EOM_EYE_MASK_M }, 63 [11] = { 0, 0, UFS_EOM_EYE_MASK_M }, 64 [12] = { 2, 0, UFS_EOM_EYE_MASK_M }, 65 [13] = { 4, 0, UFS_EOM_EYE_MASK_M }, 66 [14] = { 6, 0, UFS_EOM_EYE_MASK_M }, 67 [15] = { -4, 10, UFS_EOM_EYE_MASK_M }, 68 [16] = { -2, 10, UFS_EOM_EYE_MASK_M }, 69 [17] = { 0, 10, UFS_EOM_EYE_MASK_M }, 70 [18] = { 2, 10, UFS_EOM_EYE_MASK_M }, 71 [19] = { 4, 10, UFS_EOM_EYE_MASK_M }, 72 [20] = { -2, 15, UFS_EOM_EYE_MASK_M }, 73 [21] = { 0, 15, UFS_EOM_EYE_MASK_M }, 74 [22] = { 2, 15, UFS_EOM_EYE_MASK_M }, 75 }; 76 77 /* Qualcomm MCQ Configuration */ 78 #define UFS_QCOM_MCQCAP_QCFGPTR 224 /* 0xE0 in hex */ 79 #define UFS_QCOM_MCQ_CONFIG_OFFSET (UFS_QCOM_MCQCAP_QCFGPTR * 0x200) /* 0x1C000 */ 80 81 /* Doorbell offsets within MCQ region (relative to MCQ_CONFIG_BASE) */ 82 #define UFS_QCOM_MCQ_SQD_OFFSET 0x5000 83 #define UFS_QCOM_MCQ_CQD_OFFSET 0x5080 84 #define UFS_QCOM_MCQ_SQIS_OFFSET 0x5040 85 #define UFS_QCOM_MCQ_CQIS_OFFSET 0x50C0 86 #define UFS_QCOM_MCQ_STRIDE 0x100 87 88 /* Calculated doorbell address offsets (relative to mmio_base) */ 89 #define UFS_QCOM_SQD_ADDR_OFFSET (UFS_QCOM_MCQ_CONFIG_OFFSET + UFS_QCOM_MCQ_SQD_OFFSET) 90 #define UFS_QCOM_CQD_ADDR_OFFSET (UFS_QCOM_MCQ_CONFIG_OFFSET + UFS_QCOM_MCQ_CQD_OFFSET) 91 #define UFS_QCOM_SQIS_ADDR_OFFSET (UFS_QCOM_MCQ_CONFIG_OFFSET + UFS_QCOM_MCQ_SQIS_OFFSET) 92 #define UFS_QCOM_CQIS_ADDR_OFFSET (UFS_QCOM_MCQ_CONFIG_OFFSET + UFS_QCOM_MCQ_CQIS_OFFSET) 93 #define REG_UFS_MCQ_STRIDE UFS_QCOM_MCQ_STRIDE 94 95 /* MCQ Vendor specific address offsets (relative to MCQ_CONFIG_BASE) */ 96 #define UFS_MEM_VS_BASE 0x4000 97 #define UFS_MEM_CQIS_VS 0x4008 98 99 /* QCOM UFS host controller vendor specific registers */ 100 enum { 101 REG_UFS_SYS1CLK_1US = 0xC0, 102 REG_UFS_TX_SYMBOL_CLK_NS_US = 0xC4, 103 REG_UFS_LOCAL_PORT_ID_REG = 0xC8, 104 REG_UFS_PA_ERR_CODE = 0xCC, 105 /* On older UFS revisions, this register is called "RETRY_TIMER_REG" */ 106 REG_UFS_PARAM0 = 0xD0, 107 /* On older UFS revisions, this register is called "REG_UFS_PA_LINK_STARTUP_TIMER" */ 108 REG_UFS_CFG0 = 0xD8, 109 REG_UFS_CFG1 = 0xDC, 110 REG_UFS_CFG2 = 0xE0, 111 REG_UFS_HW_VERSION = 0xE4, 112 113 UFS_TEST_BUS = 0xE8, 114 UFS_TEST_BUS_CTRL_0 = 0xEC, 115 UFS_TEST_BUS_CTRL_1 = 0xF0, 116 UFS_TEST_BUS_CTRL_2 = 0xF4, 117 UFS_UNIPRO_CFG = 0xF8, 118 119 /* 120 * QCOM UFS host controller vendor specific registers 121 * added in HW Version 3.0.0 122 */ 123 UFS_AH8_CFG = 0xFC, 124 125 UFS_RD_REG_MCQ = 0xD00, 126 UFS_MEM_ICE_CFG = 0x2600, 127 REG_UFS_MEM_ICE_CONFIG = 0x260C, 128 REG_UFS_MEM_ICE_NUM_CORE = 0x2664, 129 130 REG_UFS_CFG3 = 0x271C, 131 132 REG_UFS_DEBUG_SPARE_CFG = 0x284C, 133 }; 134 135 /* QCOM UFS host controller vendor specific debug registers */ 136 enum { 137 UFS_DBG_RD_REG_UAWM = 0x100, 138 UFS_DBG_RD_REG_UARM = 0x200, 139 UFS_DBG_RD_REG_TXUC = 0x300, 140 UFS_DBG_RD_REG_RXUC = 0x400, 141 UFS_DBG_RD_REG_DFC = 0x500, 142 UFS_DBG_RD_REG_TRLUT = 0x600, 143 UFS_DBG_RD_REG_TMRLUT = 0x700, 144 UFS_UFS_DBG_RD_REG_OCSC = 0x800, 145 146 UFS_UFS_DBG_RD_DESC_RAM = 0x1500, 147 UFS_UFS_DBG_RD_PRDT_RAM = 0x1700, 148 UFS_UFS_DBG_RD_RESP_RAM = 0x1800, 149 UFS_UFS_DBG_RD_EDTL_RAM = 0x1900, 150 }; 151 152 /* QCOM UFS HC vendor specific Hibern8 count registers */ 153 enum { 154 REG_UFS_HW_H8_ENTER_CNT = 0x2700, 155 REG_UFS_SW_H8_ENTER_CNT = 0x2704, 156 REG_UFS_SW_AFTER_HW_H8_ENTER_CNT = 0x2708, 157 REG_UFS_HW_H8_EXIT_CNT = 0x270C, 158 REG_UFS_SW_H8_EXIT_CNT = 0x2710, 159 }; 160 161 #define UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(x) (0x000 + x) 162 #define UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(x) (0x400 + x) 163 164 /* bit definitions for REG_UFS_CFG0 register */ 165 #define QUNIPRO_G4_SEL BIT(5) 166 167 /* bit definitions for REG_UFS_CFG1 register */ 168 #define QUNIPRO_SEL BIT(0) 169 #define UFS_PHY_SOFT_RESET BIT(1) 170 #define UTP_DBG_RAMS_EN BIT(17) 171 #define TEST_BUS_EN BIT(18) 172 #define TEST_BUS_SEL GENMASK(22, 19) 173 #define UFS_REG_TEST_BUS_EN BIT(30) 174 175 /* bit definitions for REG_UFS_CFG2 register */ 176 #define UAWM_HW_CGC_EN BIT(0) 177 #define UARM_HW_CGC_EN BIT(1) 178 #define TXUC_HW_CGC_EN BIT(2) 179 #define RXUC_HW_CGC_EN BIT(3) 180 #define DFC_HW_CGC_EN BIT(4) 181 #define TRLUT_HW_CGC_EN BIT(5) 182 #define TMRLUT_HW_CGC_EN BIT(6) 183 #define OCSC_HW_CGC_EN BIT(7) 184 185 /* bit definitions for REG_UFS_CFG3 register */ 186 #define ESI_VEC_MASK GENMASK(22, 12) 187 188 /* bit definitions for REG_UFS_PARAM0 */ 189 #define MAX_HS_GEAR_MASK GENMASK(6, 4) 190 #define UFS_QCOM_MAX_GEAR(x) FIELD_GET(MAX_HS_GEAR_MASK, (x)) 191 192 /* bit definition for UFS_UFS_TEST_BUS_CTRL_n */ 193 #define TEST_BUS_SUB_SEL_MASK GENMASK(4, 0) /* All XXX_SEL fields are 5 bits wide */ 194 195 /* bit definition for UFS Shared ICE config */ 196 #define UFS_QCOM_CAP_ICE_CONFIG BIT(0) 197 198 #define REG_UFS_CFG2_CGC_EN_ALL (UAWM_HW_CGC_EN | UARM_HW_CGC_EN |\ 199 TXUC_HW_CGC_EN | RXUC_HW_CGC_EN |\ 200 DFC_HW_CGC_EN | TRLUT_HW_CGC_EN |\ 201 TMRLUT_HW_CGC_EN | OCSC_HW_CGC_EN) 202 203 /* QUniPro Vendor specific attributes */ 204 #define PA_TX_HSG1_SYNC_LENGTH 0x1552 205 #define PA_VS_CONFIG_REG1 0x9000 206 #define DME_VS_CORE_CLK_CTRL 0xD002 207 #define TX_HS_EQUALIZER 0x0037 208 209 /* bit and mask definitions for DME_VS_CORE_CLK_CTRL attribute */ 210 #define CLK_1US_CYCLES_MASK_V4 GENMASK(27, 16) 211 #define CLK_1US_CYCLES_MASK GENMASK(7, 0) 212 #define DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT BIT(8) 213 #define PA_VS_CORE_CLK_40NS_CYCLES 0x9007 214 #define PA_VS_CORE_CLK_40NS_CYCLES_MASK GENMASK(6, 0) 215 216 217 /* QCOM UFS host controller core clk frequencies */ 218 #define UNIPRO_CORE_CLK_FREQ_37_5_MHZ 38 219 #define UNIPRO_CORE_CLK_FREQ_75_MHZ 75 220 #define UNIPRO_CORE_CLK_FREQ_100_MHZ 100 221 #define UNIPRO_CORE_CLK_FREQ_150_MHZ 150 222 #define UNIPRO_CORE_CLK_FREQ_300_MHZ 300 223 #define UNIPRO_CORE_CLK_FREQ_201_5_MHZ 202 224 #define UNIPRO_CORE_CLK_FREQ_403_MHZ 403 225 226 /* TX_HSG1_SYNC_LENGTH attr value */ 227 #define PA_TX_HSG1_SYNC_LENGTH_VAL 0x4A 228 229 /* 230 * Some ufs device vendors need a different TSync length. 231 * Enable this quirk to give an additional TX_HS_SYNC_LENGTH. 232 */ 233 #define UFS_DEVICE_QUIRK_PA_TX_HSG1_SYNC_LENGTH BIT(16) 234 235 /* 236 * Some ufs device vendors need a different Deemphasis setting. 237 * Enable this quirk to tune TX Deemphasis parameters. 238 */ 239 #define UFS_DEVICE_QUIRK_PA_TX_DEEMPHASIS_TUNING BIT(17) 240 241 /* ICE allocator type to share AES engines among TX stream and RX stream */ 242 #define ICE_ALLOCATOR_TYPE 2 243 244 /* 245 * Number of cores allocated for RX stream when Read data block received and 246 * Write data block is not in progress 247 */ 248 #define NUM_RX_R1W0 28 249 250 /* 251 * Number of cores allocated for TX stream when Device asked to send write 252 * data block and Read data block is not in progress 253 */ 254 #define NUM_TX_R0W1 28 255 256 /* 257 * Number of cores allocated for RX stream when Read data block received and 258 * Write data block is in progress 259 * OR 260 * Device asked to send write data block and Read data block is in progress 261 */ 262 #define NUM_RX_R1W1 15 263 264 /* 265 * Number of cores allocated for TX stream (UFS write) when Read data block 266 * received and Write data block is in progress 267 * OR 268 * Device asked to send write data block and Read data block is in progress 269 */ 270 #define NUM_TX_R1W1 13 271 272 /* bit definitions for UFS_AH8_CFG register */ 273 #define CC_UFS_SYS_CLK_REQ_EN BIT(2) 274 #define CC_UFS_ICE_CORE_CLK_REQ_EN BIT(3) 275 #define CC_UFS_UNIPRO_CORE_CLK_REQ_EN BIT(4) 276 #define CC_UFS_AUXCLK_REQ_EN BIT(5) 277 278 #define UFS_HW_CLK_CTRL_EN (CC_UFS_SYS_CLK_REQ_EN |\ 279 CC_UFS_ICE_CORE_CLK_REQ_EN |\ 280 CC_UFS_UNIPRO_CORE_CLK_REQ_EN |\ 281 CC_UFS_AUXCLK_REQ_EN) 282 283 static inline void 284 ufs_qcom_get_controller_revision(struct ufs_hba *hba, 285 u8 *major, u16 *minor, u16 *step) 286 { 287 u32 ver = ufshcd_readl(hba, REG_UFS_HW_VERSION); 288 289 *major = FIELD_GET(UFS_HW_VER_MAJOR_MASK, ver); 290 *minor = FIELD_GET(UFS_HW_VER_MINOR_MASK, ver); 291 *step = FIELD_GET(UFS_HW_VER_STEP_MASK, ver); 292 }; 293 294 static inline void ufs_qcom_assert_reset(struct ufs_hba *hba) 295 { 296 ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, UFS_PHY_SOFT_RESET, REG_UFS_CFG1); 297 298 /* 299 * Dummy read to ensure the write takes effect before doing any sort 300 * of delay 301 */ 302 ufshcd_readl(hba, REG_UFS_CFG1); 303 } 304 305 static inline void ufs_qcom_deassert_reset(struct ufs_hba *hba) 306 { 307 ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, 0, REG_UFS_CFG1); 308 309 /* 310 * Dummy read to ensure the write takes effect before doing any sort 311 * of delay 312 */ 313 ufshcd_readl(hba, REG_UFS_CFG1); 314 } 315 316 /* Host controller hardware version: major.minor.step */ 317 struct ufs_hw_version { 318 u16 step; 319 u16 minor; 320 u8 major; 321 }; 322 323 struct ufs_qcom_testbus { 324 u8 select_major; 325 u8 select_minor; 326 }; 327 328 struct gpio_desc; 329 330 struct ufs_qcom_host { 331 struct phy *generic_phy; 332 struct ufs_hba *hba; 333 struct ufs_pa_layer_attr dev_req_params; 334 struct clk *tx_lane0_sync_clk; 335 struct clk *rx_lane0_sync_clk; 336 struct clk *rx_lane1_sync_clk; 337 bool is_lane_clks_enabled; 338 339 struct icc_path *icc_ddr; 340 struct icc_path *icc_cpu; 341 342 #ifdef CONFIG_SCSI_UFS_CRYPTO 343 struct qcom_ice *ice; 344 #endif 345 u32 caps; 346 void __iomem *dev_ref_clk_ctrl_mmio; 347 bool is_dev_ref_clk_enabled; 348 struct ufs_hw_version hw_ver; 349 350 u32 dev_ref_clk_en_mask; 351 352 struct ufs_qcom_testbus testbus; 353 354 /* Reset control of HCI */ 355 struct reset_control *core_reset; 356 struct reset_controller_dev rcdev; 357 358 struct gpio_desc *device_reset; 359 360 struct ufs_host_params host_params; 361 u32 phy_gear; 362 363 bool esi_enabled; 364 struct msi_msg esi_msg; 365 366 u32 saved_tx_eq_g1_setting; 367 u32 boot_spare_cfg; 368 }; 369 370 struct ufs_qcom_drvdata { 371 enum ufshcd_quirks quirks; 372 bool no_phy_retention; 373 const struct ufs_hba_variant_ops *vops; 374 }; 375 376 static inline u32 377 ufs_qcom_get_debug_reg_offset(struct ufs_qcom_host *host, u32 reg) 378 { 379 if (host->hw_ver.major <= 0x02) 380 return UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(reg); 381 382 return UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(reg); 383 }; 384 385 #define ufs_qcom_is_link_off(hba) ufshcd_is_link_off(hba) 386 #define ufs_qcom_is_link_active(hba) ufshcd_is_link_active(hba) 387 #define ufs_qcom_is_link_hibern8(hba) ufshcd_is_link_hibern8(hba) 388 #define ceil(freq, div) ((freq) % (div) == 0 ? ((freq)/(div)) : ((freq)/(div) + 1)) 389 390 int ufs_qcom_testbus_config(struct ufs_qcom_host *host); 391 392 #endif /* UFS_QCOM_H_ */ 393