1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2013-2016, Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/clk.h> 8 #include <linux/cleanup.h> 9 #include <linux/delay.h> 10 #include <linux/devfreq.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/interconnect.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/phy/phy.h> 16 #include <linux/platform_device.h> 17 #include <linux/reset-controller.h> 18 #include <linux/time.h> 19 #include <linux/unaligned.h> 20 #include <linux/units.h> 21 22 #include <soc/qcom/ice.h> 23 24 #include <ufs/ufshcd.h> 25 #include <ufs/ufshci.h> 26 #include <ufs/ufs_quirks.h> 27 #include <ufs/unipro.h> 28 #include "ufshcd-pltfrm.h" 29 #include "ufs-qcom.h" 30 31 #define MCQ_QCFGPTR_MASK GENMASK(7, 0) 32 #define MCQ_QCFGPTR_UNIT 0x200 33 #define MCQ_SQATTR_OFFSET(c) \ 34 ((((c) >> 16) & MCQ_QCFGPTR_MASK) * MCQ_QCFGPTR_UNIT) 35 #define MCQ_QCFG_SIZE 0x40 36 37 /* De-emphasis for gear-5 */ 38 #define DEEMPHASIS_3_5_dB 0x04 39 #define NO_DEEMPHASIS 0x0 40 41 enum { 42 TSTBUS_UAWM, 43 TSTBUS_UARM, 44 TSTBUS_TXUC, 45 TSTBUS_RXUC, 46 TSTBUS_DFC, 47 TSTBUS_TRLUT, 48 TSTBUS_TMRLUT, 49 TSTBUS_OCSC, 50 TSTBUS_UTP_HCI, 51 TSTBUS_COMBINED, 52 TSTBUS_WRAPPER, 53 TSTBUS_UNIPRO, 54 TSTBUS_MAX, 55 }; 56 57 #define QCOM_UFS_MAX_GEAR 5 58 #define QCOM_UFS_MAX_LANE 2 59 60 enum { 61 MODE_MIN, 62 MODE_PWM, 63 MODE_HS_RA, 64 MODE_HS_RB, 65 MODE_MAX, 66 }; 67 68 static const struct __ufs_qcom_bw_table { 69 u32 mem_bw; 70 u32 cfg_bw; 71 } ufs_qcom_bw_table[MODE_MAX + 1][QCOM_UFS_MAX_GEAR + 1][QCOM_UFS_MAX_LANE + 1] = { 72 [MODE_MIN][0][0] = { 0, 0 }, /* Bandwidth values in KB/s */ 73 [MODE_PWM][UFS_PWM_G1][UFS_LANE_1] = { 922, 1000 }, 74 [MODE_PWM][UFS_PWM_G2][UFS_LANE_1] = { 1844, 1000 }, 75 [MODE_PWM][UFS_PWM_G3][UFS_LANE_1] = { 3688, 1000 }, 76 [MODE_PWM][UFS_PWM_G4][UFS_LANE_1] = { 7376, 1000 }, 77 [MODE_PWM][UFS_PWM_G5][UFS_LANE_1] = { 14752, 1000 }, 78 [MODE_PWM][UFS_PWM_G1][UFS_LANE_2] = { 1844, 1000 }, 79 [MODE_PWM][UFS_PWM_G2][UFS_LANE_2] = { 3688, 1000 }, 80 [MODE_PWM][UFS_PWM_G3][UFS_LANE_2] = { 7376, 1000 }, 81 [MODE_PWM][UFS_PWM_G4][UFS_LANE_2] = { 14752, 1000 }, 82 [MODE_PWM][UFS_PWM_G5][UFS_LANE_2] = { 29504, 1000 }, 83 [MODE_HS_RA][UFS_HS_G1][UFS_LANE_1] = { 127796, 1000 }, 84 [MODE_HS_RA][UFS_HS_G2][UFS_LANE_1] = { 255591, 1000 }, 85 [MODE_HS_RA][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 }, 86 [MODE_HS_RA][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 }, 87 [MODE_HS_RA][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 }, 88 [MODE_HS_RA][UFS_HS_G1][UFS_LANE_2] = { 255591, 1000 }, 89 [MODE_HS_RA][UFS_HS_G2][UFS_LANE_2] = { 511181, 1000 }, 90 [MODE_HS_RA][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 }, 91 [MODE_HS_RA][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 }, 92 [MODE_HS_RA][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 }, 93 [MODE_HS_RB][UFS_HS_G1][UFS_LANE_1] = { 149422, 1000 }, 94 [MODE_HS_RB][UFS_HS_G2][UFS_LANE_1] = { 298189, 1000 }, 95 [MODE_HS_RB][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 }, 96 [MODE_HS_RB][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 }, 97 [MODE_HS_RB][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 }, 98 [MODE_HS_RB][UFS_HS_G1][UFS_LANE_2] = { 298189, 1000 }, 99 [MODE_HS_RB][UFS_HS_G2][UFS_LANE_2] = { 596378, 1000 }, 100 [MODE_HS_RB][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 }, 101 [MODE_HS_RB][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 }, 102 [MODE_HS_RB][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 }, 103 [MODE_MAX][0][0] = { 7643136, 819200 }, 104 }; 105 106 static const struct { 107 int nminor; 108 char *prefix; 109 } testbus_info[TSTBUS_MAX] = { 110 [TSTBUS_UAWM] = {32, "TSTBUS_UAWM"}, 111 [TSTBUS_UARM] = {32, "TSTBUS_UARM"}, 112 [TSTBUS_TXUC] = {32, "TSTBUS_TXUC"}, 113 [TSTBUS_RXUC] = {32, "TSTBUS_RXUC"}, 114 [TSTBUS_DFC] = {32, "TSTBUS_DFC"}, 115 [TSTBUS_TRLUT] = {32, "TSTBUS_TRLUT"}, 116 [TSTBUS_TMRLUT] = {32, "TSTBUS_TMRLUT"}, 117 [TSTBUS_OCSC] = {32, "TSTBUS_OCSC"}, 118 [TSTBUS_UTP_HCI] = {32, "TSTBUS_UTP_HCI"}, 119 [TSTBUS_COMBINED] = {32, "TSTBUS_COMBINED"}, 120 [TSTBUS_WRAPPER] = {32, "TSTBUS_WRAPPER"}, 121 [TSTBUS_UNIPRO] = {256, "TSTBUS_UNIPRO"}, 122 }; 123 124 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host); 125 static unsigned long ufs_qcom_opp_freq_to_clk_freq(struct ufs_hba *hba, 126 unsigned long freq, char *name); 127 static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up, unsigned long freq); 128 129 static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd) 130 { 131 return container_of(rcd, struct ufs_qcom_host, rcdev); 132 } 133 134 #ifdef CONFIG_SCSI_UFS_CRYPTO 135 /** 136 * ufs_qcom_config_ice_allocator() - ICE core allocator configuration 137 * 138 * @host: pointer to qcom specific variant structure. 139 */ 140 static void ufs_qcom_config_ice_allocator(struct ufs_qcom_host *host) 141 { 142 struct ufs_hba *hba = host->hba; 143 static const uint8_t val[4] = { NUM_RX_R1W0, NUM_TX_R0W1, NUM_RX_R1W1, NUM_TX_R1W1 }; 144 u32 config; 145 146 if (!(host->caps & UFS_QCOM_CAP_ICE_CONFIG) || 147 !(host->hba->caps & UFSHCD_CAP_CRYPTO)) 148 return; 149 150 config = get_unaligned_le32(val); 151 152 ufshcd_writel(hba, ICE_ALLOCATOR_TYPE, REG_UFS_MEM_ICE_CONFIG); 153 ufshcd_writel(hba, config, REG_UFS_MEM_ICE_NUM_CORE); 154 } 155 156 static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host) 157 { 158 if (host->hba->caps & UFSHCD_CAP_CRYPTO) 159 qcom_ice_enable(host->ice); 160 } 161 162 static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops; /* forward decl */ 163 164 static int ufs_qcom_ice_init(struct ufs_qcom_host *host) 165 { 166 struct ufs_hba *hba = host->hba; 167 struct blk_crypto_profile *profile = &hba->crypto_profile; 168 struct device *dev = hba->dev; 169 struct qcom_ice *ice; 170 union ufs_crypto_capabilities caps; 171 union ufs_crypto_cap_entry cap; 172 int err; 173 int i; 174 175 ice = devm_of_qcom_ice_get(dev); 176 if (ice == ERR_PTR(-EOPNOTSUPP)) { 177 dev_warn(dev, "Disabling inline encryption support\n"); 178 ice = NULL; 179 } 180 181 if (IS_ERR_OR_NULL(ice)) 182 return PTR_ERR_OR_ZERO(ice); 183 184 host->ice = ice; 185 186 /* Initialize the blk_crypto_profile */ 187 188 caps.reg_val = cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP)); 189 190 /* The number of keyslots supported is (CFGC+1) */ 191 err = devm_blk_crypto_profile_init(dev, profile, caps.config_count + 1); 192 if (err) 193 return err; 194 195 profile->ll_ops = ufs_qcom_crypto_ops; 196 profile->max_dun_bytes_supported = 8; 197 profile->key_types_supported = qcom_ice_get_supported_key_type(ice); 198 profile->dev = dev; 199 200 /* 201 * Currently this driver only supports AES-256-XTS. All known versions 202 * of ICE support it, but to be safe make sure it is really declared in 203 * the crypto capability registers. The crypto capability registers 204 * also give the supported data unit size(s). 205 */ 206 for (i = 0; i < caps.num_crypto_cap; i++) { 207 cap.reg_val = cpu_to_le32(ufshcd_readl(hba, 208 REG_UFS_CRYPTOCAP + 209 i * sizeof(__le32))); 210 if (cap.algorithm_id == UFS_CRYPTO_ALG_AES_XTS && 211 cap.key_size == UFS_CRYPTO_KEY_SIZE_256) 212 profile->modes_supported[BLK_ENCRYPTION_MODE_AES_256_XTS] |= 213 cap.sdus_mask * 512; 214 } 215 216 hba->caps |= UFSHCD_CAP_CRYPTO; 217 hba->quirks |= UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE; 218 return 0; 219 } 220 221 static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 222 { 223 if (host->hba->caps & UFSHCD_CAP_CRYPTO) 224 return qcom_ice_resume(host->ice); 225 226 return 0; 227 } 228 229 static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host) 230 { 231 if (host->hba->caps & UFSHCD_CAP_CRYPTO) 232 return qcom_ice_suspend(host->ice); 233 234 return 0; 235 } 236 237 static int ufs_qcom_ice_keyslot_program(struct blk_crypto_profile *profile, 238 const struct blk_crypto_key *key, 239 unsigned int slot) 240 { 241 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 242 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 243 int err; 244 245 ufshcd_hold(hba); 246 err = qcom_ice_program_key(host->ice, slot, key); 247 ufshcd_release(hba); 248 return err; 249 } 250 251 static int ufs_qcom_ice_keyslot_evict(struct blk_crypto_profile *profile, 252 const struct blk_crypto_key *key, 253 unsigned int slot) 254 { 255 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 256 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 257 int err; 258 259 ufshcd_hold(hba); 260 err = qcom_ice_evict_key(host->ice, slot); 261 ufshcd_release(hba); 262 return err; 263 } 264 265 static int ufs_qcom_ice_derive_sw_secret(struct blk_crypto_profile *profile, 266 const u8 *eph_key, size_t eph_key_size, 267 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]) 268 { 269 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 270 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 271 272 return qcom_ice_derive_sw_secret(host->ice, eph_key, eph_key_size, 273 sw_secret); 274 } 275 276 static int ufs_qcom_ice_import_key(struct blk_crypto_profile *profile, 277 const u8 *raw_key, size_t raw_key_size, 278 u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE]) 279 { 280 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 281 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 282 283 return qcom_ice_import_key(host->ice, raw_key, raw_key_size, lt_key); 284 } 285 286 static int ufs_qcom_ice_generate_key(struct blk_crypto_profile *profile, 287 u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE]) 288 { 289 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 290 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 291 292 return qcom_ice_generate_key(host->ice, lt_key); 293 } 294 295 static int ufs_qcom_ice_prepare_key(struct blk_crypto_profile *profile, 296 const u8 *lt_key, size_t lt_key_size, 297 u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE]) 298 { 299 struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile); 300 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 301 302 return qcom_ice_prepare_key(host->ice, lt_key, lt_key_size, eph_key); 303 } 304 305 static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops = { 306 .keyslot_program = ufs_qcom_ice_keyslot_program, 307 .keyslot_evict = ufs_qcom_ice_keyslot_evict, 308 .derive_sw_secret = ufs_qcom_ice_derive_sw_secret, 309 .import_key = ufs_qcom_ice_import_key, 310 .generate_key = ufs_qcom_ice_generate_key, 311 .prepare_key = ufs_qcom_ice_prepare_key, 312 }; 313 314 #else 315 316 static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host) 317 { 318 } 319 320 static int ufs_qcom_ice_init(struct ufs_qcom_host *host) 321 { 322 return 0; 323 } 324 325 static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 326 { 327 return 0; 328 } 329 330 static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host) 331 { 332 return 0; 333 } 334 335 static void ufs_qcom_config_ice_allocator(struct ufs_qcom_host *host) 336 { 337 } 338 339 #endif 340 341 static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host) 342 { 343 if (!host->is_lane_clks_enabled) 344 return; 345 346 clk_bulk_disable_unprepare(host->num_clks, host->clks); 347 348 host->is_lane_clks_enabled = false; 349 } 350 351 static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host) 352 { 353 int err; 354 355 err = clk_bulk_prepare_enable(host->num_clks, host->clks); 356 if (err) 357 return err; 358 359 host->is_lane_clks_enabled = true; 360 361 return 0; 362 } 363 364 static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host) 365 { 366 int err; 367 struct device *dev = host->hba->dev; 368 369 if (has_acpi_companion(dev)) 370 return 0; 371 372 err = devm_clk_bulk_get_all(dev, &host->clks); 373 if (err <= 0) 374 return err; 375 376 host->num_clks = err; 377 378 return 0; 379 } 380 381 static int ufs_qcom_check_hibern8(struct ufs_hba *hba) 382 { 383 int err; 384 u32 tx_fsm_val; 385 unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS); 386 387 do { 388 err = ufshcd_dme_get(hba, 389 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 390 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 391 &tx_fsm_val); 392 if (err || tx_fsm_val == TX_FSM_HIBERN8) 393 break; 394 395 /* sleep for max. 200us */ 396 usleep_range(100, 200); 397 } while (time_before(jiffies, timeout)); 398 399 /* 400 * we might have scheduled out for long during polling so 401 * check the state again. 402 */ 403 if (time_after(jiffies, timeout)) 404 err = ufshcd_dme_get(hba, 405 UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, 406 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), 407 &tx_fsm_val); 408 409 if (err) { 410 dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n", 411 __func__, err); 412 } else if (tx_fsm_val != TX_FSM_HIBERN8) { 413 err = tx_fsm_val; 414 dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n", 415 __func__, err); 416 } 417 418 return err; 419 } 420 421 static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host) 422 { 423 ufshcd_rmwl(host->hba, QUNIPRO_SEL, QUNIPRO_SEL, REG_UFS_CFG1); 424 425 if (host->hw_ver.major >= 0x05) 426 ufshcd_rmwl(host->hba, QUNIPRO_G4_SEL, 0, REG_UFS_CFG0); 427 } 428 429 /* 430 * ufs_qcom_host_reset - reset host controller and PHY 431 */ 432 static int ufs_qcom_host_reset(struct ufs_hba *hba) 433 { 434 int ret; 435 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 436 bool reenable_intr; 437 438 if (!host->core_reset) 439 return 0; 440 441 reenable_intr = hba->is_irq_enabled; 442 ufshcd_disable_irq(hba); 443 444 ret = reset_control_assert(host->core_reset); 445 if (ret) { 446 dev_err(hba->dev, "%s: core_reset assert failed, err = %d\n", 447 __func__, ret); 448 return ret; 449 } 450 451 /* 452 * The hardware requirement for delay between assert/deassert 453 * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to 454 * ~125us (4/32768). To be on the safe side add 200us delay. 455 */ 456 usleep_range(200, 210); 457 458 ret = reset_control_deassert(host->core_reset); 459 if (ret) { 460 dev_err(hba->dev, "%s: core_reset deassert failed, err = %d\n", 461 __func__, ret); 462 return ret; 463 } 464 465 usleep_range(1000, 1100); 466 467 if (reenable_intr) 468 ufshcd_enable_irq(hba); 469 470 return 0; 471 } 472 473 static u32 ufs_qcom_get_hs_gear(struct ufs_hba *hba) 474 { 475 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 476 477 if (host->hw_ver.major >= 0x4) 478 return UFS_QCOM_MAX_GEAR(ufshcd_readl(hba, REG_UFS_PARAM0)); 479 480 /* Default is HS-G3 */ 481 return UFS_HS_G3; 482 } 483 484 static int ufs_qcom_power_up_sequence(struct ufs_hba *hba) 485 { 486 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 487 struct ufs_host_params *host_params = &host->host_params; 488 struct phy *phy = host->generic_phy; 489 enum phy_mode mode; 490 int ret; 491 492 /* 493 * HW ver 5 can only support up to HS-G5 Rate-A due to HW limitations. 494 * If the HS-G5 PHY gear is used, update host_params->hs_rate to Rate-A, 495 * so that the subsequent power mode change shall stick to Rate-A. 496 */ 497 if (host->hw_ver.major == 0x5) { 498 if (host->phy_gear == UFS_HS_G5) 499 host_params->hs_rate = PA_HS_MODE_A; 500 else 501 host_params->hs_rate = PA_HS_MODE_B; 502 } 503 504 mode = host_params->hs_rate == PA_HS_MODE_B ? PHY_MODE_UFS_HS_B : PHY_MODE_UFS_HS_A; 505 506 /* Reset UFS Host Controller and PHY */ 507 ret = ufs_qcom_host_reset(hba); 508 if (ret) 509 return ret; 510 511 if (phy->power_count) 512 phy_power_off(phy); 513 514 515 /* phy initialization - calibrate the phy */ 516 ret = phy_init(phy); 517 if (ret) { 518 dev_err(hba->dev, "%s: phy init failed, ret = %d\n", 519 __func__, ret); 520 return ret; 521 } 522 523 ret = phy_set_mode_ext(phy, mode, host->phy_gear); 524 if (ret) 525 goto out_disable_phy; 526 527 /* power on phy - start serdes and phy's power and clocks */ 528 ret = phy_power_on(phy); 529 if (ret) { 530 dev_err(hba->dev, "%s: phy power on failed, ret = %d\n", 531 __func__, ret); 532 goto out_disable_phy; 533 } 534 535 ufs_qcom_select_unipro_mode(host); 536 537 return 0; 538 539 out_disable_phy: 540 phy_exit(phy); 541 542 return ret; 543 } 544 545 /* 546 * The UTP controller has a number of internal clock gating cells (CGCs). 547 * Internal hardware sub-modules within the UTP controller control the CGCs. 548 * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved 549 * in a specific operation, UTP controller CGCs are by default disabled and 550 * this function enables them (after every UFS link startup) to save some power 551 * leakage. 552 */ 553 static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba) 554 { 555 int err; 556 557 /* Enable UTP internal clock gating */ 558 ufshcd_rmwl(hba, REG_UFS_CFG2_CGC_EN_ALL, REG_UFS_CFG2_CGC_EN_ALL, 559 REG_UFS_CFG2); 560 561 /* Ensure that HW clock gating is enabled before next operations */ 562 ufshcd_readl(hba, REG_UFS_CFG2); 563 564 /* Enable Unipro internal clock gating */ 565 err = ufshcd_dme_rmw(hba, DL_VS_CLK_CFG_MASK, 566 DL_VS_CLK_CFG_MASK, DL_VS_CLK_CFG); 567 if (err) 568 goto out; 569 570 err = ufshcd_dme_rmw(hba, PA_VS_CLK_CFG_REG_MASK, 571 PA_VS_CLK_CFG_REG_MASK, PA_VS_CLK_CFG_REG); 572 if (err) 573 goto out; 574 575 err = ufshcd_dme_rmw(hba, DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN, 576 DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN, 577 DME_VS_CORE_CLK_CTRL); 578 out: 579 if (err) 580 dev_err(hba->dev, "hw clk gating enabled failed\n"); 581 } 582 583 static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba, 584 enum ufs_notify_change_status status) 585 { 586 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 587 int err; 588 589 switch (status) { 590 case PRE_CHANGE: 591 err = ufs_qcom_power_up_sequence(hba); 592 if (err) 593 return err; 594 595 /* 596 * The PHY PLL output is the source of tx/rx lane symbol 597 * clocks, hence, enable the lane clocks only after PHY 598 * is initialized. 599 */ 600 err = ufs_qcom_enable_lane_clks(host); 601 break; 602 case POST_CHANGE: 603 /* check if UFS PHY moved from DISABLED to HIBERN8 */ 604 err = ufs_qcom_check_hibern8(hba); 605 ufs_qcom_enable_hw_clk_gating(hba); 606 ufs_qcom_ice_enable(host); 607 ufs_qcom_config_ice_allocator(host); 608 break; 609 default: 610 dev_err(hba->dev, "%s: invalid status %d\n", __func__, status); 611 err = -EINVAL; 612 break; 613 } 614 return err; 615 } 616 617 /** 618 * ufs_qcom_cfg_timers - Configure ufs qcom cfg timers 619 * 620 * @hba: host controller instance 621 * @is_pre_scale_up: flag to check if pre scale up condition. 622 * @freq: target opp freq 623 * Return: zero for success and non-zero in case of a failure. 624 */ 625 static int ufs_qcom_cfg_timers(struct ufs_hba *hba, bool is_pre_scale_up, unsigned long freq) 626 { 627 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 628 struct ufs_clk_info *clki; 629 unsigned long clk_freq = 0; 630 u32 core_clk_cycles_per_us; 631 632 /* 633 * UTP controller uses SYS1CLK_1US_REG register for Interrupt 634 * Aggregation logic. 635 * It is mandatory to write SYS1CLK_1US_REG register on UFS host 636 * controller V4.0.0 onwards. 637 */ 638 if (host->hw_ver.major < 4 && !ufshcd_is_intr_aggr_allowed(hba)) 639 return 0; 640 641 if (hba->use_pm_opp && freq != ULONG_MAX) { 642 clk_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk"); 643 if (clk_freq) 644 goto cfg_timers; 645 } 646 647 list_for_each_entry(clki, &hba->clk_list_head, list) { 648 if (!strcmp(clki->name, "core_clk")) { 649 if (freq == ULONG_MAX) { 650 clk_freq = clki->max_freq; 651 break; 652 } 653 654 if (is_pre_scale_up) 655 clk_freq = clki->max_freq; 656 else 657 clk_freq = clk_get_rate(clki->clk); 658 break; 659 } 660 661 } 662 663 cfg_timers: 664 /* If frequency is smaller than 1MHz, set to 1MHz */ 665 if (clk_freq < DEFAULT_CLK_RATE_HZ) 666 clk_freq = DEFAULT_CLK_RATE_HZ; 667 668 core_clk_cycles_per_us = clk_freq / USEC_PER_SEC; 669 if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) { 670 ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US); 671 /* 672 * make sure above write gets applied before we return from 673 * this function. 674 */ 675 ufshcd_readl(hba, REG_UFS_SYS1CLK_1US); 676 } 677 678 return 0; 679 } 680 681 static int ufs_qcom_link_startup_notify(struct ufs_hba *hba, 682 enum ufs_notify_change_status status) 683 { 684 int err = 0; 685 686 switch (status) { 687 case PRE_CHANGE: 688 if (ufs_qcom_cfg_timers(hba, false, ULONG_MAX)) { 689 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n", 690 __func__); 691 return -EINVAL; 692 } 693 694 err = ufs_qcom_set_core_clk_ctrl(hba, true, ULONG_MAX); 695 if (err) 696 dev_err(hba->dev, "cfg core clk ctrl failed\n"); 697 /* 698 * Some UFS devices (and may be host) have issues if LCC is 699 * enabled. So we are setting PA_Local_TX_LCC_Enable to 0 700 * before link startup which will make sure that both host 701 * and device TX LCC are disabled once link startup is 702 * completed. 703 */ 704 err = ufshcd_disable_host_tx_lcc(hba); 705 706 break; 707 default: 708 break; 709 } 710 711 return err; 712 } 713 714 static void ufs_qcom_device_reset_ctrl(struct ufs_hba *hba, bool asserted) 715 { 716 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 717 718 /* reset gpio is optional */ 719 if (!host->device_reset) 720 return; 721 722 gpiod_set_value_cansleep(host->device_reset, asserted); 723 } 724 725 static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op, 726 enum ufs_notify_change_status status) 727 { 728 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 729 struct phy *phy = host->generic_phy; 730 731 if (status == PRE_CHANGE) 732 return 0; 733 734 if (ufs_qcom_is_link_off(hba)) { 735 /* 736 * Disable the tx/rx lane symbol clocks before PHY is 737 * powered down as the PLL source should be disabled 738 * after downstream clocks are disabled. 739 */ 740 ufs_qcom_disable_lane_clks(host); 741 phy_power_off(phy); 742 743 /* reset the connected UFS device during power down */ 744 ufs_qcom_device_reset_ctrl(hba, true); 745 746 } else if (!ufs_qcom_is_link_active(hba)) { 747 ufs_qcom_disable_lane_clks(host); 748 } 749 750 return ufs_qcom_ice_suspend(host); 751 } 752 753 static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) 754 { 755 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 756 struct phy *phy = host->generic_phy; 757 int err; 758 759 if (ufs_qcom_is_link_off(hba)) { 760 err = phy_power_on(phy); 761 if (err) { 762 dev_err(hba->dev, "%s: failed PHY power on: %d\n", 763 __func__, err); 764 return err; 765 } 766 767 err = ufs_qcom_enable_lane_clks(host); 768 if (err) 769 return err; 770 771 } else if (!ufs_qcom_is_link_active(hba)) { 772 err = ufs_qcom_enable_lane_clks(host); 773 if (err) 774 return err; 775 } 776 777 return ufs_qcom_ice_resume(host); 778 } 779 780 static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable) 781 { 782 if (host->dev_ref_clk_ctrl_mmio && 783 (enable ^ host->is_dev_ref_clk_enabled)) { 784 u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio); 785 786 if (enable) 787 temp |= host->dev_ref_clk_en_mask; 788 else 789 temp &= ~host->dev_ref_clk_en_mask; 790 791 /* 792 * If we are here to disable this clock it might be immediately 793 * after entering into hibern8 in which case we need to make 794 * sure that device ref_clk is active for specific time after 795 * hibern8 enter. 796 */ 797 if (!enable) { 798 unsigned long gating_wait; 799 800 gating_wait = host->hba->dev_info.clk_gating_wait_us; 801 if (!gating_wait) { 802 udelay(1); 803 } else { 804 /* 805 * bRefClkGatingWaitTime defines the minimum 806 * time for which the reference clock is 807 * required by device during transition from 808 * HS-MODE to LS-MODE or HIBERN8 state. Give it 809 * more delay to be on the safe side. 810 */ 811 gating_wait += 10; 812 usleep_range(gating_wait, gating_wait + 10); 813 } 814 } 815 816 writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio); 817 818 /* 819 * Make sure the write to ref_clk reaches the destination and 820 * not stored in a Write Buffer (WB). 821 */ 822 readl(host->dev_ref_clk_ctrl_mmio); 823 824 /* 825 * If we call hibern8 exit after this, we need to make sure that 826 * device ref_clk is stable for at least 1us before the hibern8 827 * exit command. 828 */ 829 if (enable) 830 udelay(1); 831 832 host->is_dev_ref_clk_enabled = enable; 833 } 834 } 835 836 static int ufs_qcom_icc_set_bw(struct ufs_qcom_host *host, u32 mem_bw, u32 cfg_bw) 837 { 838 struct device *dev = host->hba->dev; 839 int ret; 840 841 ret = icc_set_bw(host->icc_ddr, 0, mem_bw); 842 if (ret < 0) { 843 dev_err(dev, "failed to set bandwidth request: %d\n", ret); 844 return ret; 845 } 846 847 ret = icc_set_bw(host->icc_cpu, 0, cfg_bw); 848 if (ret < 0) { 849 dev_err(dev, "failed to set bandwidth request: %d\n", ret); 850 return ret; 851 } 852 853 return 0; 854 } 855 856 static struct __ufs_qcom_bw_table ufs_qcom_get_bw_table(struct ufs_qcom_host *host) 857 { 858 struct ufs_pa_layer_attr *p = &host->dev_req_params; 859 int gear = max_t(u32, p->gear_rx, p->gear_tx); 860 int lane = max_t(u32, p->lane_rx, p->lane_tx); 861 862 if (WARN_ONCE(gear > QCOM_UFS_MAX_GEAR, 863 "ICC scaling for UFS Gear (%d) not supported. Using Gear (%d) bandwidth\n", 864 gear, QCOM_UFS_MAX_GEAR)) 865 gear = QCOM_UFS_MAX_GEAR; 866 867 if (WARN_ONCE(lane > QCOM_UFS_MAX_LANE, 868 "ICC scaling for UFS Lane (%d) not supported. Using Lane (%d) bandwidth\n", 869 lane, QCOM_UFS_MAX_LANE)) 870 lane = QCOM_UFS_MAX_LANE; 871 872 if (ufshcd_is_hs_mode(p)) { 873 if (p->hs_rate == PA_HS_MODE_B) 874 return ufs_qcom_bw_table[MODE_HS_RB][gear][lane]; 875 else 876 return ufs_qcom_bw_table[MODE_HS_RA][gear][lane]; 877 } else { 878 return ufs_qcom_bw_table[MODE_PWM][gear][lane]; 879 } 880 } 881 882 static int ufs_qcom_icc_update_bw(struct ufs_qcom_host *host) 883 { 884 struct __ufs_qcom_bw_table bw_table; 885 886 bw_table = ufs_qcom_get_bw_table(host); 887 888 return ufs_qcom_icc_set_bw(host, bw_table.mem_bw, bw_table.cfg_bw); 889 } 890 891 static void ufs_qcom_set_tx_hs_equalizer(struct ufs_hba *hba, u32 gear, u32 tx_lanes) 892 { 893 u32 equalizer_val; 894 int ret, i; 895 896 /* Determine the equalizer value based on the gear */ 897 equalizer_val = (gear == 5) ? DEEMPHASIS_3_5_dB : NO_DEEMPHASIS; 898 899 for (i = 0; i < tx_lanes; i++) { 900 ret = ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_HS_EQUALIZER, i), 901 equalizer_val); 902 if (ret) 903 dev_err(hba->dev, "%s: failed equalizer lane %d\n", 904 __func__, i); 905 } 906 } 907 908 static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba, 909 enum ufs_notify_change_status status, 910 const struct ufs_pa_layer_attr *dev_max_params, 911 struct ufs_pa_layer_attr *dev_req_params) 912 { 913 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 914 struct ufs_host_params *host_params = &host->host_params; 915 int ret = 0; 916 917 if (!dev_req_params) { 918 pr_err("%s: incoming dev_req_params is NULL\n", __func__); 919 return -EINVAL; 920 } 921 922 switch (status) { 923 case PRE_CHANGE: 924 ret = ufshcd_negotiate_pwr_params(host_params, dev_max_params, dev_req_params); 925 if (ret) { 926 dev_err(hba->dev, "%s: failed to determine capabilities\n", 927 __func__); 928 return ret; 929 } 930 931 /* 932 * During UFS driver probe, always update the PHY gear to match the negotiated 933 * gear, so that, if quirk UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is enabled, 934 * the second init can program the optimal PHY settings. This allows one to start 935 * the first init with either the minimum or the maximum support gear. 936 */ 937 if (hba->ufshcd_state == UFSHCD_STATE_RESET) { 938 /* 939 * Skip REINIT if the negotiated gear matches with the 940 * initial phy_gear. Otherwise, update the phy_gear to 941 * program the optimal gear setting during REINIT. 942 */ 943 if (host->phy_gear == dev_req_params->gear_tx) 944 hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH; 945 else 946 host->phy_gear = dev_req_params->gear_tx; 947 } 948 949 /* enable the device ref clock before changing to HS mode */ 950 if (!ufshcd_is_hs_mode(&hba->pwr_info) && 951 ufshcd_is_hs_mode(dev_req_params)) 952 ufs_qcom_dev_ref_clk_ctrl(host, true); 953 954 if (host->hw_ver.major >= 0x4) { 955 ufshcd_dme_configure_adapt(hba, 956 dev_req_params->gear_tx, 957 PA_INITIAL_ADAPT); 958 } 959 960 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TX_DEEMPHASIS_TUNING) 961 ufs_qcom_set_tx_hs_equalizer(hba, 962 dev_req_params->gear_tx, dev_req_params->lane_tx); 963 964 break; 965 case POST_CHANGE: 966 /* cache the power mode parameters to use internally */ 967 memcpy(&host->dev_req_params, 968 dev_req_params, sizeof(*dev_req_params)); 969 970 ufs_qcom_icc_update_bw(host); 971 972 /* disable the device ref clock if entered PWM mode */ 973 if (ufshcd_is_hs_mode(&hba->pwr_info) && 974 !ufshcd_is_hs_mode(dev_req_params)) 975 ufs_qcom_dev_ref_clk_ctrl(host, false); 976 break; 977 default: 978 ret = -EINVAL; 979 break; 980 } 981 982 return ret; 983 } 984 985 static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba) 986 { 987 int err; 988 u32 pa_vs_config_reg1; 989 990 err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1), 991 &pa_vs_config_reg1); 992 if (err) 993 return err; 994 995 /* Allow extension of MSB bits of PA_SaveConfigTime attribute */ 996 return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1), 997 (pa_vs_config_reg1 | (1 << 12))); 998 } 999 1000 static void ufs_qcom_override_pa_tx_hsg1_sync_len(struct ufs_hba *hba) 1001 { 1002 int err; 1003 1004 err = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TX_HSG1_SYNC_LENGTH), 1005 PA_TX_HSG1_SYNC_LENGTH_VAL); 1006 if (err) 1007 dev_err(hba->dev, "Failed (%d) set PA_TX_HSG1_SYNC_LENGTH\n", err); 1008 } 1009 1010 static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba) 1011 { 1012 int err = 0; 1013 1014 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME) 1015 err = ufs_qcom_quirk_host_pa_saveconfigtime(hba); 1016 1017 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TX_HSG1_SYNC_LENGTH) 1018 ufs_qcom_override_pa_tx_hsg1_sync_len(hba); 1019 1020 return err; 1021 } 1022 1023 /* UFS device-specific quirks */ 1024 static struct ufs_dev_quirk ufs_qcom_dev_fixups[] = { 1025 { .wmanufacturerid = UFS_VENDOR_SKHYNIX, 1026 .model = UFS_ANY_MODEL, 1027 .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM }, 1028 { .wmanufacturerid = UFS_VENDOR_TOSHIBA, 1029 .model = UFS_ANY_MODEL, 1030 .quirk = UFS_DEVICE_QUIRK_DELAY_AFTER_LPM }, 1031 { .wmanufacturerid = UFS_VENDOR_WDC, 1032 .model = UFS_ANY_MODEL, 1033 .quirk = UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE }, 1034 { .wmanufacturerid = UFS_VENDOR_SAMSUNG, 1035 .model = UFS_ANY_MODEL, 1036 .quirk = UFS_DEVICE_QUIRK_PA_TX_HSG1_SYNC_LENGTH | 1037 UFS_DEVICE_QUIRK_PA_TX_DEEMPHASIS_TUNING }, 1038 {} 1039 }; 1040 1041 static void ufs_qcom_fixup_dev_quirks(struct ufs_hba *hba) 1042 { 1043 ufshcd_fixup_dev_quirks(hba, ufs_qcom_dev_fixups); 1044 } 1045 1046 static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba) 1047 { 1048 return ufshci_version(2, 0); 1049 } 1050 1051 /** 1052 * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks 1053 * @hba: host controller instance 1054 * 1055 * QCOM UFS host controller might have some non standard behaviours (quirks) 1056 * than what is specified by UFSHCI specification. Advertise all such 1057 * quirks to standard UFS host controller driver so standard takes them into 1058 * account. 1059 */ 1060 static void ufs_qcom_advertise_quirks(struct ufs_hba *hba) 1061 { 1062 const struct ufs_qcom_drvdata *drvdata = of_device_get_match_data(hba->dev); 1063 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1064 1065 if (host->hw_ver.major == 0x2) 1066 hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION; 1067 1068 if (host->hw_ver.major > 0x3) 1069 hba->quirks |= UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH; 1070 1071 if (drvdata && drvdata->quirks) 1072 hba->quirks |= drvdata->quirks; 1073 } 1074 1075 static void ufs_qcom_set_phy_gear(struct ufs_qcom_host *host) 1076 { 1077 struct ufs_host_params *host_params = &host->host_params; 1078 u32 val, dev_major; 1079 1080 /* 1081 * Default to powering up the PHY to the max gear possible, which is 1082 * backwards compatible with lower gears but not optimal from 1083 * a power usage point of view. After device negotiation, if the 1084 * gear is lower a reinit will be performed to program the PHY 1085 * to the ideal gear for this combo of controller and device. 1086 */ 1087 host->phy_gear = host_params->hs_tx_gear; 1088 1089 if (host->hw_ver.major < 0x4) { 1090 /* 1091 * These controllers only have one PHY init sequence, 1092 * let's power up the PHY using that (the minimum supported 1093 * gear, UFS_HS_G2). 1094 */ 1095 host->phy_gear = UFS_HS_G2; 1096 } else if (host->hw_ver.major >= 0x5) { 1097 val = ufshcd_readl(host->hba, REG_UFS_DEBUG_SPARE_CFG); 1098 dev_major = FIELD_GET(UFS_DEV_VER_MAJOR_MASK, val); 1099 1100 /* 1101 * Since the UFS device version is populated, let's remove the 1102 * REINIT quirk as the negotiated gear won't change during boot. 1103 * So there is no need to do reinit. 1104 */ 1105 if (dev_major != 0x0) 1106 host->hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH; 1107 1108 /* 1109 * For UFS 3.1 device and older, power up the PHY using HS-G4 1110 * PHY gear to save power. 1111 */ 1112 if (dev_major > 0x0 && dev_major < 0x4) 1113 host->phy_gear = UFS_HS_G4; 1114 } 1115 } 1116 1117 static void ufs_qcom_set_host_params(struct ufs_hba *hba) 1118 { 1119 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1120 struct ufs_host_params *host_params = &host->host_params; 1121 1122 ufshcd_init_host_params(host_params); 1123 1124 /* This driver only supports symmetic gear setting i.e., hs_tx_gear == hs_rx_gear */ 1125 host_params->hs_tx_gear = host_params->hs_rx_gear = ufs_qcom_get_hs_gear(hba); 1126 } 1127 1128 static void ufs_qcom_set_host_caps(struct ufs_hba *hba) 1129 { 1130 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1131 1132 if (host->hw_ver.major >= 0x5) 1133 host->caps |= UFS_QCOM_CAP_ICE_CONFIG; 1134 } 1135 1136 static void ufs_qcom_set_caps(struct ufs_hba *hba) 1137 { 1138 hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING; 1139 hba->caps |= UFSHCD_CAP_CLK_SCALING | UFSHCD_CAP_WB_WITH_CLK_SCALING; 1140 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND; 1141 hba->caps |= UFSHCD_CAP_WB_EN; 1142 hba->caps |= UFSHCD_CAP_AGGR_POWER_COLLAPSE; 1143 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND; 1144 1145 ufs_qcom_set_host_caps(hba); 1146 } 1147 1148 /** 1149 * ufs_qcom_setup_clocks - enables/disable clocks 1150 * @hba: host controller instance 1151 * @on: If true, enable clocks else disable them. 1152 * @status: PRE_CHANGE or POST_CHANGE notify 1153 * 1154 * Return: 0 on success, non-zero on failure. 1155 */ 1156 static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on, 1157 enum ufs_notify_change_status status) 1158 { 1159 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1160 1161 /* 1162 * In case ufs_qcom_init() is not yet done, simply ignore. 1163 * This ufs_qcom_setup_clocks() shall be called from 1164 * ufs_qcom_init() after init is done. 1165 */ 1166 if (!host) 1167 return 0; 1168 1169 switch (status) { 1170 case PRE_CHANGE: 1171 if (on) { 1172 ufs_qcom_icc_update_bw(host); 1173 } else { 1174 if (!ufs_qcom_is_link_active(hba)) { 1175 /* disable device ref_clk */ 1176 ufs_qcom_dev_ref_clk_ctrl(host, false); 1177 } 1178 } 1179 break; 1180 case POST_CHANGE: 1181 if (on) { 1182 /* enable the device ref clock for HS mode*/ 1183 if (ufshcd_is_hs_mode(&hba->pwr_info)) 1184 ufs_qcom_dev_ref_clk_ctrl(host, true); 1185 } else { 1186 ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MIN][0][0].mem_bw, 1187 ufs_qcom_bw_table[MODE_MIN][0][0].cfg_bw); 1188 } 1189 break; 1190 } 1191 1192 return 0; 1193 } 1194 1195 static int 1196 ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id) 1197 { 1198 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev); 1199 1200 ufs_qcom_assert_reset(host->hba); 1201 /* provide 1ms delay to let the reset pulse propagate. */ 1202 usleep_range(1000, 1100); 1203 return 0; 1204 } 1205 1206 static int 1207 ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id) 1208 { 1209 struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev); 1210 1211 ufs_qcom_deassert_reset(host->hba); 1212 1213 /* 1214 * after reset deassertion, phy will need all ref clocks, 1215 * voltage, current to settle down before starting serdes. 1216 */ 1217 usleep_range(1000, 1100); 1218 return 0; 1219 } 1220 1221 static const struct reset_control_ops ufs_qcom_reset_ops = { 1222 .assert = ufs_qcom_reset_assert, 1223 .deassert = ufs_qcom_reset_deassert, 1224 }; 1225 1226 static int ufs_qcom_icc_init(struct ufs_qcom_host *host) 1227 { 1228 struct device *dev = host->hba->dev; 1229 int ret; 1230 1231 host->icc_ddr = devm_of_icc_get(dev, "ufs-ddr"); 1232 if (IS_ERR(host->icc_ddr)) 1233 return dev_err_probe(dev, PTR_ERR(host->icc_ddr), 1234 "failed to acquire interconnect path\n"); 1235 1236 host->icc_cpu = devm_of_icc_get(dev, "cpu-ufs"); 1237 if (IS_ERR(host->icc_cpu)) 1238 return dev_err_probe(dev, PTR_ERR(host->icc_cpu), 1239 "failed to acquire interconnect path\n"); 1240 1241 /* 1242 * Set Maximum bandwidth vote before initializing the UFS controller and 1243 * device. Ideally, a minimal interconnect vote would suffice for the 1244 * initialization, but a max vote would allow faster initialization. 1245 */ 1246 ret = ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MAX][0][0].mem_bw, 1247 ufs_qcom_bw_table[MODE_MAX][0][0].cfg_bw); 1248 if (ret < 0) 1249 return dev_err_probe(dev, ret, "failed to set bandwidth request\n"); 1250 1251 return 0; 1252 } 1253 1254 /** 1255 * ufs_qcom_init - bind phy with controller 1256 * @hba: host controller instance 1257 * 1258 * Binds PHY with controller and powers up PHY enabling clocks 1259 * and regulators. 1260 * 1261 * Return: -EPROBE_DEFER if binding fails, returns negative error 1262 * on phy power up failure and returns zero on success. 1263 */ 1264 static int ufs_qcom_init(struct ufs_hba *hba) 1265 { 1266 int err; 1267 struct device *dev = hba->dev; 1268 struct ufs_qcom_host *host; 1269 struct ufs_clk_info *clki; 1270 const struct ufs_qcom_drvdata *drvdata = of_device_get_match_data(hba->dev); 1271 1272 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); 1273 if (!host) 1274 return -ENOMEM; 1275 1276 /* Make a two way bind between the qcom host and the hba */ 1277 host->hba = hba; 1278 ufshcd_set_variant(hba, host); 1279 1280 /* Setup the optional reset control of HCI */ 1281 host->core_reset = devm_reset_control_get_optional(hba->dev, "rst"); 1282 if (IS_ERR(host->core_reset)) { 1283 err = dev_err_probe(dev, PTR_ERR(host->core_reset), 1284 "Failed to get reset control\n"); 1285 goto out_variant_clear; 1286 } 1287 1288 /* Fire up the reset controller. Failure here is non-fatal. */ 1289 host->rcdev.of_node = dev->of_node; 1290 host->rcdev.ops = &ufs_qcom_reset_ops; 1291 host->rcdev.owner = dev->driver->owner; 1292 host->rcdev.nr_resets = 1; 1293 err = devm_reset_controller_register(dev, &host->rcdev); 1294 if (err) 1295 dev_warn(dev, "Failed to register reset controller\n"); 1296 1297 if (!has_acpi_companion(dev)) { 1298 host->generic_phy = devm_phy_get(dev, "ufsphy"); 1299 if (IS_ERR(host->generic_phy)) { 1300 err = dev_err_probe(dev, PTR_ERR(host->generic_phy), "Failed to get PHY\n"); 1301 goto out_variant_clear; 1302 } 1303 } 1304 1305 err = ufs_qcom_icc_init(host); 1306 if (err) 1307 goto out_variant_clear; 1308 1309 host->device_reset = devm_gpiod_get_optional(dev, "reset", 1310 GPIOD_OUT_HIGH); 1311 if (IS_ERR(host->device_reset)) { 1312 err = dev_err_probe(dev, PTR_ERR(host->device_reset), 1313 "Failed to acquire device reset gpio\n"); 1314 goto out_variant_clear; 1315 } 1316 1317 ufs_qcom_get_controller_revision(hba, &host->hw_ver.major, 1318 &host->hw_ver.minor, &host->hw_ver.step); 1319 1320 host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1; 1321 host->dev_ref_clk_en_mask = BIT(26); 1322 1323 list_for_each_entry(clki, &hba->clk_list_head, list) { 1324 if (!strcmp(clki->name, "core_clk_unipro")) 1325 clki->keep_link_active = true; 1326 } 1327 1328 err = ufs_qcom_init_lane_clks(host); 1329 if (err) 1330 goto out_variant_clear; 1331 1332 ufs_qcom_set_caps(hba); 1333 ufs_qcom_advertise_quirks(hba); 1334 ufs_qcom_set_host_params(hba); 1335 ufs_qcom_set_phy_gear(host); 1336 1337 err = ufs_qcom_ice_init(host); 1338 if (err) 1339 goto out_variant_clear; 1340 1341 ufs_qcom_setup_clocks(hba, true, POST_CHANGE); 1342 1343 ufs_qcom_get_default_testbus_cfg(host); 1344 err = ufs_qcom_testbus_config(host); 1345 if (err) 1346 /* Failure is non-fatal */ 1347 dev_warn(dev, "%s: failed to configure the testbus %d\n", 1348 __func__, err); 1349 1350 if (drvdata && drvdata->no_phy_retention) 1351 hba->spm_lvl = UFS_PM_LVL_5; 1352 1353 return 0; 1354 1355 out_variant_clear: 1356 ufshcd_set_variant(hba, NULL); 1357 1358 return err; 1359 } 1360 1361 static void ufs_qcom_exit(struct ufs_hba *hba) 1362 { 1363 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1364 1365 ufs_qcom_disable_lane_clks(host); 1366 phy_power_off(host->generic_phy); 1367 phy_exit(host->generic_phy); 1368 } 1369 1370 /** 1371 * ufs_qcom_set_clk_40ns_cycles - Configure 40ns clk cycles 1372 * 1373 * @hba: host controller instance 1374 * @cycles_in_1us: No of cycles in 1us to be configured 1375 * 1376 * Returns error if dme get/set configuration for 40ns fails 1377 * and returns zero on success. 1378 */ 1379 static int ufs_qcom_set_clk_40ns_cycles(struct ufs_hba *hba, 1380 u32 cycles_in_1us) 1381 { 1382 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1383 u32 cycles_in_40ns; 1384 u32 reg; 1385 int err; 1386 1387 /* 1388 * UFS host controller V4.0.0 onwards needs to program 1389 * PA_VS_CORE_CLK_40NS_CYCLES attribute per programmed 1390 * frequency of unipro core clk of UFS host controller. 1391 */ 1392 if (host->hw_ver.major < 4) 1393 return 0; 1394 1395 /* 1396 * Generic formulae for cycles_in_40ns = (freq_unipro/25) is not 1397 * applicable for all frequencies. For ex: ceil(37.5 MHz/25) will 1398 * be 2 and ceil(403 MHZ/25) will be 17 whereas Hardware 1399 * specification expect to be 16. Hence use exact hardware spec 1400 * mandated value for cycles_in_40ns instead of calculating using 1401 * generic formulae. 1402 */ 1403 switch (cycles_in_1us) { 1404 case UNIPRO_CORE_CLK_FREQ_403_MHZ: 1405 cycles_in_40ns = 16; 1406 break; 1407 case UNIPRO_CORE_CLK_FREQ_300_MHZ: 1408 cycles_in_40ns = 12; 1409 break; 1410 case UNIPRO_CORE_CLK_FREQ_201_5_MHZ: 1411 cycles_in_40ns = 8; 1412 break; 1413 case UNIPRO_CORE_CLK_FREQ_150_MHZ: 1414 cycles_in_40ns = 6; 1415 break; 1416 case UNIPRO_CORE_CLK_FREQ_100_MHZ: 1417 cycles_in_40ns = 4; 1418 break; 1419 case UNIPRO_CORE_CLK_FREQ_75_MHZ: 1420 cycles_in_40ns = 3; 1421 break; 1422 case UNIPRO_CORE_CLK_FREQ_37_5_MHZ: 1423 cycles_in_40ns = 2; 1424 break; 1425 default: 1426 dev_err(hba->dev, "UNIPRO clk freq %u MHz not supported\n", 1427 cycles_in_1us); 1428 return -EINVAL; 1429 } 1430 1431 err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), ®); 1432 if (err) 1433 return err; 1434 1435 reg &= ~PA_VS_CORE_CLK_40NS_CYCLES_MASK; 1436 reg |= cycles_in_40ns; 1437 1438 return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), reg); 1439 } 1440 1441 static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up, unsigned long freq) 1442 { 1443 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1444 struct list_head *head = &hba->clk_list_head; 1445 struct ufs_clk_info *clki; 1446 u32 cycles_in_1us = 0; 1447 u32 core_clk_ctrl_reg; 1448 unsigned long clk_freq; 1449 int err; 1450 1451 if (hba->use_pm_opp && freq != ULONG_MAX) { 1452 clk_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk_unipro"); 1453 if (clk_freq) { 1454 cycles_in_1us = ceil(clk_freq, HZ_PER_MHZ); 1455 goto set_core_clk_ctrl; 1456 } 1457 } 1458 1459 list_for_each_entry(clki, head, list) { 1460 if (!IS_ERR_OR_NULL(clki->clk) && 1461 !strcmp(clki->name, "core_clk_unipro")) { 1462 if (!clki->max_freq) { 1463 cycles_in_1us = 150; /* default for backwards compatibility */ 1464 break; 1465 } 1466 1467 if (freq == ULONG_MAX) { 1468 cycles_in_1us = ceil(clki->max_freq, HZ_PER_MHZ); 1469 break; 1470 } 1471 1472 if (is_scale_up) 1473 cycles_in_1us = ceil(clki->max_freq, HZ_PER_MHZ); 1474 else 1475 cycles_in_1us = ceil(clk_get_rate(clki->clk), HZ_PER_MHZ); 1476 break; 1477 } 1478 } 1479 1480 set_core_clk_ctrl: 1481 err = ufshcd_dme_get(hba, 1482 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), 1483 &core_clk_ctrl_reg); 1484 if (err) 1485 return err; 1486 1487 /* Bit mask is different for UFS host controller V4.0.0 onwards */ 1488 if (host->hw_ver.major >= 4) { 1489 if (!FIELD_FIT(CLK_1US_CYCLES_MASK_V4, cycles_in_1us)) 1490 return -ERANGE; 1491 core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK_V4; 1492 core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK_V4, cycles_in_1us); 1493 } else { 1494 if (!FIELD_FIT(CLK_1US_CYCLES_MASK, cycles_in_1us)) 1495 return -ERANGE; 1496 core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK; 1497 core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK, cycles_in_1us); 1498 } 1499 1500 /* Clear CORE_CLK_DIV_EN */ 1501 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT; 1502 1503 err = ufshcd_dme_set(hba, 1504 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), 1505 core_clk_ctrl_reg); 1506 if (err) 1507 return err; 1508 1509 /* Configure unipro core clk 40ns attribute */ 1510 return ufs_qcom_set_clk_40ns_cycles(hba, cycles_in_1us); 1511 } 1512 1513 static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba, unsigned long freq) 1514 { 1515 int ret; 1516 1517 ret = ufs_qcom_cfg_timers(hba, true, freq); 1518 if (ret) { 1519 dev_err(hba->dev, "%s ufs cfg timer failed\n", __func__); 1520 return ret; 1521 } 1522 /* set unipro core clock attributes and clear clock divider */ 1523 return ufs_qcom_set_core_clk_ctrl(hba, true, freq); 1524 } 1525 1526 static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba) 1527 { 1528 return 0; 1529 } 1530 1531 static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba) 1532 { 1533 int err; 1534 u32 core_clk_ctrl_reg; 1535 1536 err = ufshcd_dme_get(hba, 1537 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), 1538 &core_clk_ctrl_reg); 1539 1540 /* make sure CORE_CLK_DIV_EN is cleared */ 1541 if (!err && 1542 (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) { 1543 core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT; 1544 err = ufshcd_dme_set(hba, 1545 UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), 1546 core_clk_ctrl_reg); 1547 } 1548 1549 return err; 1550 } 1551 1552 static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba, unsigned long freq) 1553 { 1554 int ret; 1555 1556 ret = ufs_qcom_cfg_timers(hba, false, freq); 1557 if (ret) { 1558 dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n", __func__); 1559 return ret; 1560 } 1561 /* set unipro core clock attributes and clear clock divider */ 1562 return ufs_qcom_set_core_clk_ctrl(hba, false, freq); 1563 } 1564 1565 static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba, bool scale_up, 1566 unsigned long target_freq, 1567 enum ufs_notify_change_status status) 1568 { 1569 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1570 int err; 1571 1572 /* check the host controller state before sending hibern8 cmd */ 1573 if (!ufshcd_is_hba_active(hba)) 1574 return 0; 1575 1576 if (status == PRE_CHANGE) { 1577 err = ufshcd_uic_hibern8_enter(hba); 1578 if (err) 1579 return err; 1580 if (scale_up) 1581 err = ufs_qcom_clk_scale_up_pre_change(hba, target_freq); 1582 else 1583 err = ufs_qcom_clk_scale_down_pre_change(hba); 1584 1585 if (err) { 1586 ufshcd_uic_hibern8_exit(hba); 1587 return err; 1588 } 1589 } else { 1590 if (scale_up) 1591 err = ufs_qcom_clk_scale_up_post_change(hba); 1592 else 1593 err = ufs_qcom_clk_scale_down_post_change(hba, target_freq); 1594 1595 1596 if (err) { 1597 ufshcd_uic_hibern8_exit(hba); 1598 return err; 1599 } 1600 1601 ufs_qcom_icc_update_bw(host); 1602 ufshcd_uic_hibern8_exit(hba); 1603 } 1604 1605 return 0; 1606 } 1607 1608 static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host) 1609 { 1610 ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN, 1611 UFS_REG_TEST_BUS_EN, REG_UFS_CFG1); 1612 ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1); 1613 } 1614 1615 static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host) 1616 { 1617 /* provide a legal default configuration */ 1618 host->testbus.select_major = TSTBUS_UNIPRO; 1619 host->testbus.select_minor = 37; 1620 } 1621 1622 static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host) 1623 { 1624 if (host->testbus.select_major >= TSTBUS_MAX) { 1625 dev_err(host->hba->dev, 1626 "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n", 1627 __func__, host->testbus.select_major); 1628 return false; 1629 } 1630 1631 return true; 1632 } 1633 1634 int ufs_qcom_testbus_config(struct ufs_qcom_host *host) 1635 { 1636 int reg; 1637 int offset; 1638 u32 mask = TEST_BUS_SUB_SEL_MASK; 1639 1640 if (!host) 1641 return -EINVAL; 1642 1643 if (!ufs_qcom_testbus_cfg_is_ok(host)) 1644 return -EPERM; 1645 1646 switch (host->testbus.select_major) { 1647 case TSTBUS_UAWM: 1648 reg = UFS_TEST_BUS_CTRL_0; 1649 offset = 24; 1650 break; 1651 case TSTBUS_UARM: 1652 reg = UFS_TEST_BUS_CTRL_0; 1653 offset = 16; 1654 break; 1655 case TSTBUS_TXUC: 1656 reg = UFS_TEST_BUS_CTRL_0; 1657 offset = 8; 1658 break; 1659 case TSTBUS_RXUC: 1660 reg = UFS_TEST_BUS_CTRL_0; 1661 offset = 0; 1662 break; 1663 case TSTBUS_DFC: 1664 reg = UFS_TEST_BUS_CTRL_1; 1665 offset = 24; 1666 break; 1667 case TSTBUS_TRLUT: 1668 reg = UFS_TEST_BUS_CTRL_1; 1669 offset = 16; 1670 break; 1671 case TSTBUS_TMRLUT: 1672 reg = UFS_TEST_BUS_CTRL_1; 1673 offset = 8; 1674 break; 1675 case TSTBUS_OCSC: 1676 reg = UFS_TEST_BUS_CTRL_1; 1677 offset = 0; 1678 break; 1679 case TSTBUS_WRAPPER: 1680 reg = UFS_TEST_BUS_CTRL_2; 1681 offset = 16; 1682 break; 1683 case TSTBUS_COMBINED: 1684 reg = UFS_TEST_BUS_CTRL_2; 1685 offset = 8; 1686 break; 1687 case TSTBUS_UTP_HCI: 1688 reg = UFS_TEST_BUS_CTRL_2; 1689 offset = 0; 1690 break; 1691 case TSTBUS_UNIPRO: 1692 reg = UFS_UNIPRO_CFG; 1693 offset = 20; 1694 mask = 0xFFF; 1695 break; 1696 /* 1697 * No need for a default case, since 1698 * ufs_qcom_testbus_cfg_is_ok() checks that the configuration 1699 * is legal 1700 */ 1701 } 1702 mask <<= offset; 1703 ufshcd_rmwl(host->hba, TEST_BUS_SEL, 1704 (u32)host->testbus.select_major << 19, 1705 REG_UFS_CFG1); 1706 ufshcd_rmwl(host->hba, mask, 1707 (u32)host->testbus.select_minor << offset, 1708 reg); 1709 ufs_qcom_enable_test_bus(host); 1710 1711 return 0; 1712 } 1713 1714 static void ufs_qcom_dump_testbus(struct ufs_hba *hba) 1715 { 1716 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1717 int i, j, nminor = 0, testbus_len = 0; 1718 u32 *testbus __free(kfree) = NULL; 1719 char *prefix; 1720 1721 testbus = kmalloc_array(256, sizeof(u32), GFP_KERNEL); 1722 if (!testbus) 1723 return; 1724 1725 for (j = 0; j < TSTBUS_MAX; j++) { 1726 nminor = testbus_info[j].nminor; 1727 prefix = testbus_info[j].prefix; 1728 host->testbus.select_major = j; 1729 testbus_len = nminor * sizeof(u32); 1730 for (i = 0; i < nminor; i++) { 1731 host->testbus.select_minor = i; 1732 ufs_qcom_testbus_config(host); 1733 testbus[i] = ufshcd_readl(hba, UFS_TEST_BUS); 1734 } 1735 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET, 1736 16, 4, testbus, testbus_len, false); 1737 } 1738 } 1739 1740 static int ufs_qcom_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, 1741 const char *prefix, enum ufshcd_res id) 1742 { 1743 u32 *regs __free(kfree) = NULL; 1744 size_t pos; 1745 1746 if (offset % 4 != 0 || len % 4 != 0) 1747 return -EINVAL; 1748 1749 regs = kzalloc(len, GFP_ATOMIC); 1750 if (!regs) 1751 return -ENOMEM; 1752 1753 for (pos = 0; pos < len; pos += 4) 1754 regs[pos / 4] = readl(hba->res[id].base + offset + pos); 1755 1756 print_hex_dump(KERN_ERR, prefix, 1757 len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE, 1758 16, 4, regs, len, false); 1759 1760 return 0; 1761 } 1762 1763 static void ufs_qcom_dump_mcq_hci_regs(struct ufs_hba *hba) 1764 { 1765 struct dump_info { 1766 size_t offset; 1767 size_t len; 1768 const char *prefix; 1769 enum ufshcd_res id; 1770 }; 1771 1772 struct dump_info mcq_dumps[] = { 1773 {0x0, 256 * 4, "MCQ HCI-0 ", RES_MCQ}, 1774 {0x400, 256 * 4, "MCQ HCI-1 ", RES_MCQ}, 1775 {0x0, 5 * 4, "MCQ VS-0 ", RES_MCQ_VS}, 1776 {0x0, 256 * 4, "MCQ SQD-0 ", RES_MCQ_SQD}, 1777 {0x400, 256 * 4, "MCQ SQD-1 ", RES_MCQ_SQD}, 1778 {0x800, 256 * 4, "MCQ SQD-2 ", RES_MCQ_SQD}, 1779 {0xc00, 256 * 4, "MCQ SQD-3 ", RES_MCQ_SQD}, 1780 {0x1000, 256 * 4, "MCQ SQD-4 ", RES_MCQ_SQD}, 1781 {0x1400, 256 * 4, "MCQ SQD-5 ", RES_MCQ_SQD}, 1782 {0x1800, 256 * 4, "MCQ SQD-6 ", RES_MCQ_SQD}, 1783 {0x1c00, 256 * 4, "MCQ SQD-7 ", RES_MCQ_SQD}, 1784 }; 1785 1786 for (int i = 0; i < ARRAY_SIZE(mcq_dumps); i++) { 1787 ufs_qcom_dump_regs(hba, mcq_dumps[i].offset, mcq_dumps[i].len, 1788 mcq_dumps[i].prefix, mcq_dumps[i].id); 1789 cond_resched(); 1790 } 1791 } 1792 1793 static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba) 1794 { 1795 u32 reg; 1796 struct ufs_qcom_host *host; 1797 1798 host = ufshcd_get_variant(hba); 1799 1800 dev_err(hba->dev, "HW_H8_ENTER_CNT=%d\n", ufshcd_readl(hba, REG_UFS_HW_H8_ENTER_CNT)); 1801 dev_err(hba->dev, "HW_H8_EXIT_CNT=%d\n", ufshcd_readl(hba, REG_UFS_HW_H8_EXIT_CNT)); 1802 1803 dev_err(hba->dev, "SW_H8_ENTER_CNT=%d\n", ufshcd_readl(hba, REG_UFS_SW_H8_ENTER_CNT)); 1804 dev_err(hba->dev, "SW_H8_EXIT_CNT=%d\n", ufshcd_readl(hba, REG_UFS_SW_H8_EXIT_CNT)); 1805 1806 dev_err(hba->dev, "SW_AFTER_HW_H8_ENTER_CNT=%d\n", 1807 ufshcd_readl(hba, REG_UFS_SW_AFTER_HW_H8_ENTER_CNT)); 1808 1809 ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4, 1810 "HCI Vendor Specific Registers "); 1811 1812 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC); 1813 ufshcd_dump_regs(hba, reg, 44 * 4, "UFS_UFS_DBG_RD_REG_OCSC "); 1814 1815 reg = ufshcd_readl(hba, REG_UFS_CFG1); 1816 reg |= UTP_DBG_RAMS_EN; 1817 ufshcd_writel(hba, reg, REG_UFS_CFG1); 1818 1819 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM); 1820 ufshcd_dump_regs(hba, reg, 32 * 4, "UFS_UFS_DBG_RD_EDTL_RAM "); 1821 1822 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM); 1823 ufshcd_dump_regs(hba, reg, 128 * 4, "UFS_UFS_DBG_RD_DESC_RAM "); 1824 1825 reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM); 1826 ufshcd_dump_regs(hba, reg, 64 * 4, "UFS_UFS_DBG_RD_PRDT_RAM "); 1827 1828 /* clear bit 17 - UTP_DBG_RAMS_EN */ 1829 ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1); 1830 1831 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM); 1832 ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UAWM "); 1833 1834 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM); 1835 ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UARM "); 1836 1837 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC); 1838 ufshcd_dump_regs(hba, reg, 48 * 4, "UFS_DBG_RD_REG_TXUC "); 1839 1840 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC); 1841 ufshcd_dump_regs(hba, reg, 27 * 4, "UFS_DBG_RD_REG_RXUC "); 1842 1843 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC); 1844 ufshcd_dump_regs(hba, reg, 19 * 4, "UFS_DBG_RD_REG_DFC "); 1845 1846 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT); 1847 ufshcd_dump_regs(hba, reg, 34 * 4, "UFS_DBG_RD_REG_TRLUT "); 1848 1849 reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT); 1850 ufshcd_dump_regs(hba, reg, 9 * 4, "UFS_DBG_RD_REG_TMRLUT "); 1851 1852 if (hba->mcq_enabled) { 1853 reg = ufs_qcom_get_debug_reg_offset(host, UFS_RD_REG_MCQ); 1854 ufshcd_dump_regs(hba, reg, 64 * 4, "HCI MCQ Debug Registers "); 1855 } 1856 1857 /* ensure below dumps occur only in task context due to blocking calls. */ 1858 if (in_task()) { 1859 /* Dump MCQ Host Vendor Specific Registers */ 1860 if (hba->mcq_enabled) 1861 ufs_qcom_dump_mcq_hci_regs(hba); 1862 1863 /* voluntarily yield the CPU as we are dumping too much data */ 1864 ufshcd_dump_regs(hba, UFS_TEST_BUS, 4, "UFS_TEST_BUS "); 1865 cond_resched(); 1866 ufs_qcom_dump_testbus(hba); 1867 } 1868 } 1869 1870 /** 1871 * ufs_qcom_device_reset() - toggle the (optional) device reset line 1872 * @hba: per-adapter instance 1873 * 1874 * Toggles the (optional) reset line to reset the attached device. 1875 */ 1876 static int ufs_qcom_device_reset(struct ufs_hba *hba) 1877 { 1878 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 1879 1880 /* reset gpio is optional */ 1881 if (!host->device_reset) 1882 return -EOPNOTSUPP; 1883 1884 /* 1885 * The UFS device shall detect reset pulses of 1us, sleep for 10us to 1886 * be on the safe side. 1887 */ 1888 ufs_qcom_device_reset_ctrl(hba, true); 1889 usleep_range(10, 15); 1890 1891 ufs_qcom_device_reset_ctrl(hba, false); 1892 usleep_range(10, 15); 1893 1894 return 0; 1895 } 1896 1897 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) 1898 static void ufs_qcom_config_scaling_param(struct ufs_hba *hba, 1899 struct devfreq_dev_profile *p, 1900 struct devfreq_simple_ondemand_data *d) 1901 { 1902 p->polling_ms = 60; 1903 p->timer = DEVFREQ_TIMER_DELAYED; 1904 d->upthreshold = 70; 1905 d->downdifferential = 5; 1906 1907 hba->clk_scaling.suspend_on_no_request = true; 1908 } 1909 #else 1910 static void ufs_qcom_config_scaling_param(struct ufs_hba *hba, 1911 struct devfreq_dev_profile *p, 1912 struct devfreq_simple_ondemand_data *data) 1913 { 1914 } 1915 #endif 1916 1917 /* Resources */ 1918 static const struct ufshcd_res_info ufs_res_info[RES_MAX] = { 1919 {.name = "ufs_mem",}, 1920 {.name = "mcq",}, 1921 /* Submission Queue DAO */ 1922 {.name = "mcq_sqd",}, 1923 /* Submission Queue Interrupt Status */ 1924 {.name = "mcq_sqis",}, 1925 /* Completion Queue DAO */ 1926 {.name = "mcq_cqd",}, 1927 /* Completion Queue Interrupt Status */ 1928 {.name = "mcq_cqis",}, 1929 /* MCQ vendor specific */ 1930 {.name = "mcq_vs",}, 1931 }; 1932 1933 static int ufs_qcom_mcq_config_resource(struct ufs_hba *hba) 1934 { 1935 struct platform_device *pdev = to_platform_device(hba->dev); 1936 struct ufshcd_res_info *res; 1937 struct resource *res_mem, *res_mcq; 1938 int i, ret; 1939 1940 memcpy(hba->res, ufs_res_info, sizeof(ufs_res_info)); 1941 1942 for (i = 0; i < RES_MAX; i++) { 1943 res = &hba->res[i]; 1944 res->resource = platform_get_resource_byname(pdev, 1945 IORESOURCE_MEM, 1946 res->name); 1947 if (!res->resource) { 1948 dev_info(hba->dev, "Resource %s not provided\n", res->name); 1949 if (i == RES_UFS) 1950 return -ENODEV; 1951 continue; 1952 } else if (i == RES_UFS) { 1953 res_mem = res->resource; 1954 res->base = hba->mmio_base; 1955 continue; 1956 } 1957 1958 res->base = devm_ioremap_resource(hba->dev, res->resource); 1959 if (IS_ERR(res->base)) { 1960 dev_err(hba->dev, "Failed to map res %s, err=%d\n", 1961 res->name, (int)PTR_ERR(res->base)); 1962 ret = PTR_ERR(res->base); 1963 res->base = NULL; 1964 return ret; 1965 } 1966 } 1967 1968 /* MCQ resource provided in DT */ 1969 res = &hba->res[RES_MCQ]; 1970 /* Bail if MCQ resource is provided */ 1971 if (res->base) 1972 goto out; 1973 1974 /* Explicitly allocate MCQ resource from ufs_mem */ 1975 res_mcq = devm_kzalloc(hba->dev, sizeof(*res_mcq), GFP_KERNEL); 1976 if (!res_mcq) 1977 return -ENOMEM; 1978 1979 res_mcq->start = res_mem->start + 1980 MCQ_SQATTR_OFFSET(hba->mcq_capabilities); 1981 res_mcq->end = res_mcq->start + hba->nr_hw_queues * MCQ_QCFG_SIZE - 1; 1982 res_mcq->flags = res_mem->flags; 1983 res_mcq->name = "mcq"; 1984 1985 ret = insert_resource(&iomem_resource, res_mcq); 1986 if (ret) { 1987 dev_err(hba->dev, "Failed to insert MCQ resource, err=%d\n", 1988 ret); 1989 return ret; 1990 } 1991 1992 res->base = devm_ioremap_resource(hba->dev, res_mcq); 1993 if (IS_ERR(res->base)) { 1994 dev_err(hba->dev, "MCQ registers mapping failed, err=%d\n", 1995 (int)PTR_ERR(res->base)); 1996 ret = PTR_ERR(res->base); 1997 goto ioremap_err; 1998 } 1999 2000 out: 2001 hba->mcq_base = res->base; 2002 return 0; 2003 ioremap_err: 2004 res->base = NULL; 2005 remove_resource(res_mcq); 2006 return ret; 2007 } 2008 2009 static int ufs_qcom_op_runtime_config(struct ufs_hba *hba) 2010 { 2011 struct ufshcd_res_info *mem_res, *sqdao_res; 2012 struct ufshcd_mcq_opr_info_t *opr; 2013 int i; 2014 2015 mem_res = &hba->res[RES_UFS]; 2016 sqdao_res = &hba->res[RES_MCQ_SQD]; 2017 2018 if (!mem_res->base || !sqdao_res->base) 2019 return -EINVAL; 2020 2021 for (i = 0; i < OPR_MAX; i++) { 2022 opr = &hba->mcq_opr[i]; 2023 opr->offset = sqdao_res->resource->start - 2024 mem_res->resource->start + 0x40 * i; 2025 opr->stride = 0x100; 2026 opr->base = sqdao_res->base + 0x40 * i; 2027 } 2028 2029 return 0; 2030 } 2031 2032 static int ufs_qcom_get_hba_mac(struct ufs_hba *hba) 2033 { 2034 /* Qualcomm HC supports up to 64 */ 2035 return MAX_SUPP_MAC; 2036 } 2037 2038 static int ufs_qcom_get_outstanding_cqs(struct ufs_hba *hba, 2039 unsigned long *ocqs) 2040 { 2041 struct ufshcd_res_info *mcq_vs_res = &hba->res[RES_MCQ_VS]; 2042 2043 if (!mcq_vs_res->base) 2044 return -EINVAL; 2045 2046 *ocqs = readl(mcq_vs_res->base + UFS_MEM_CQIS_VS); 2047 2048 return 0; 2049 } 2050 2051 static void ufs_qcom_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg) 2052 { 2053 struct device *dev = msi_desc_to_dev(desc); 2054 struct ufs_hba *hba = dev_get_drvdata(dev); 2055 2056 ufshcd_mcq_config_esi(hba, msg); 2057 } 2058 2059 struct ufs_qcom_irq { 2060 unsigned int irq; 2061 unsigned int idx; 2062 struct ufs_hba *hba; 2063 }; 2064 2065 static irqreturn_t ufs_qcom_mcq_esi_handler(int irq, void *data) 2066 { 2067 struct ufs_qcom_irq *qi = data; 2068 struct ufs_hba *hba = qi->hba; 2069 struct ufs_hw_queue *hwq = &hba->uhq[qi->idx]; 2070 2071 ufshcd_mcq_write_cqis(hba, 0x1, qi->idx); 2072 ufshcd_mcq_poll_cqe_lock(hba, hwq); 2073 2074 return IRQ_HANDLED; 2075 } 2076 2077 static void ufs_qcom_irq_free(struct ufs_qcom_irq *uqi) 2078 { 2079 for (struct ufs_qcom_irq *q = uqi; q->irq; q++) 2080 devm_free_irq(q->hba->dev, q->irq, q->hba); 2081 2082 platform_device_msi_free_irqs_all(uqi->hba->dev); 2083 devm_kfree(uqi->hba->dev, uqi); 2084 } 2085 2086 DEFINE_FREE(ufs_qcom_irq, struct ufs_qcom_irq *, if (_T) ufs_qcom_irq_free(_T)) 2087 2088 static int ufs_qcom_config_esi(struct ufs_hba *hba) 2089 { 2090 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 2091 int nr_irqs, ret; 2092 2093 if (host->esi_enabled) 2094 return 0; 2095 2096 /* 2097 * 1. We only handle CQs as of now. 2098 * 2. Poll queues do not need ESI. 2099 */ 2100 nr_irqs = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]; 2101 2102 struct ufs_qcom_irq *qi __free(ufs_qcom_irq) = 2103 devm_kcalloc(hba->dev, nr_irqs, sizeof(*qi), GFP_KERNEL); 2104 if (!qi) 2105 return -ENOMEM; 2106 /* Preset so __free() has a pointer to hba in all error paths */ 2107 qi[0].hba = hba; 2108 2109 ret = platform_device_msi_init_and_alloc_irqs(hba->dev, nr_irqs, 2110 ufs_qcom_write_msi_msg); 2111 if (ret) { 2112 dev_err(hba->dev, "Failed to request Platform MSI %d\n", ret); 2113 return ret; 2114 } 2115 2116 for (int idx = 0; idx < nr_irqs; idx++) { 2117 qi[idx].irq = msi_get_virq(hba->dev, idx); 2118 qi[idx].idx = idx; 2119 qi[idx].hba = hba; 2120 2121 ret = devm_request_irq(hba->dev, qi[idx].irq, ufs_qcom_mcq_esi_handler, 2122 IRQF_SHARED, "qcom-mcq-esi", qi + idx); 2123 if (ret) { 2124 dev_err(hba->dev, "%s: Fail to request IRQ for %d, err = %d\n", 2125 __func__, qi[idx].irq, ret); 2126 qi[idx].irq = 0; 2127 return ret; 2128 } 2129 } 2130 2131 retain_and_null_ptr(qi); 2132 2133 if (host->hw_ver.major >= 6) { 2134 ufshcd_rmwl(hba, ESI_VEC_MASK, FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1), 2135 REG_UFS_CFG3); 2136 } 2137 ufshcd_mcq_enable_esi(hba); 2138 host->esi_enabled = true; 2139 return 0; 2140 } 2141 2142 static unsigned long ufs_qcom_opp_freq_to_clk_freq(struct ufs_hba *hba, 2143 unsigned long freq, char *name) 2144 { 2145 struct ufs_clk_info *clki; 2146 struct dev_pm_opp *opp; 2147 unsigned long clk_freq; 2148 int idx = 0; 2149 bool found = false; 2150 2151 opp = dev_pm_opp_find_freq_exact_indexed(hba->dev, freq, 0, true); 2152 if (IS_ERR(opp)) { 2153 dev_err(hba->dev, "Failed to find OPP for exact frequency %lu\n", freq); 2154 return 0; 2155 } 2156 2157 list_for_each_entry(clki, &hba->clk_list_head, list) { 2158 if (!strcmp(clki->name, name)) { 2159 found = true; 2160 break; 2161 } 2162 2163 idx++; 2164 } 2165 2166 if (!found) { 2167 dev_err(hba->dev, "Failed to find clock '%s' in clk list\n", name); 2168 dev_pm_opp_put(opp); 2169 return 0; 2170 } 2171 2172 clk_freq = dev_pm_opp_get_freq_indexed(opp, idx); 2173 2174 dev_pm_opp_put(opp); 2175 2176 return clk_freq; 2177 } 2178 2179 static u32 ufs_qcom_freq_to_gear_speed(struct ufs_hba *hba, unsigned long freq) 2180 { 2181 u32 gear = UFS_HS_DONT_CHANGE; 2182 unsigned long unipro_freq; 2183 2184 if (!hba->use_pm_opp) 2185 return gear; 2186 2187 unipro_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk_unipro"); 2188 switch (unipro_freq) { 2189 case 403000000: 2190 gear = UFS_HS_G5; 2191 break; 2192 case 300000000: 2193 gear = UFS_HS_G4; 2194 break; 2195 case 201500000: 2196 gear = UFS_HS_G3; 2197 break; 2198 case 150000000: 2199 case 100000000: 2200 gear = UFS_HS_G2; 2201 break; 2202 case 75000000: 2203 case 37500000: 2204 gear = UFS_HS_G1; 2205 break; 2206 default: 2207 dev_err(hba->dev, "%s: Unsupported clock freq : %lu\n", __func__, freq); 2208 return UFS_HS_DONT_CHANGE; 2209 } 2210 2211 return min_t(u32, gear, hba->max_pwr_info.info.gear_rx); 2212 } 2213 2214 /* 2215 * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations 2216 * 2217 * The variant operations configure the necessary controller and PHY 2218 * handshake during initialization. 2219 */ 2220 static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = { 2221 .name = "qcom", 2222 .init = ufs_qcom_init, 2223 .exit = ufs_qcom_exit, 2224 .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version, 2225 .clk_scale_notify = ufs_qcom_clk_scale_notify, 2226 .setup_clocks = ufs_qcom_setup_clocks, 2227 .hce_enable_notify = ufs_qcom_hce_enable_notify, 2228 .link_startup_notify = ufs_qcom_link_startup_notify, 2229 .pwr_change_notify = ufs_qcom_pwr_change_notify, 2230 .apply_dev_quirks = ufs_qcom_apply_dev_quirks, 2231 .fixup_dev_quirks = ufs_qcom_fixup_dev_quirks, 2232 .suspend = ufs_qcom_suspend, 2233 .resume = ufs_qcom_resume, 2234 .dbg_register_dump = ufs_qcom_dump_dbg_regs, 2235 .device_reset = ufs_qcom_device_reset, 2236 .config_scaling_param = ufs_qcom_config_scaling_param, 2237 .mcq_config_resource = ufs_qcom_mcq_config_resource, 2238 .get_hba_mac = ufs_qcom_get_hba_mac, 2239 .op_runtime_config = ufs_qcom_op_runtime_config, 2240 .get_outstanding_cqs = ufs_qcom_get_outstanding_cqs, 2241 .config_esi = ufs_qcom_config_esi, 2242 .freq_to_gear_speed = ufs_qcom_freq_to_gear_speed, 2243 }; 2244 2245 /** 2246 * ufs_qcom_probe - probe routine of the driver 2247 * @pdev: pointer to Platform device handle 2248 * 2249 * Return: zero for success and non-zero for failure. 2250 */ 2251 static int ufs_qcom_probe(struct platform_device *pdev) 2252 { 2253 int err; 2254 struct device *dev = &pdev->dev; 2255 2256 /* Perform generic probe */ 2257 err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops); 2258 if (err) 2259 return dev_err_probe(dev, err, "ufshcd_pltfrm_init() failed\n"); 2260 2261 return 0; 2262 } 2263 2264 /** 2265 * ufs_qcom_remove - set driver_data of the device to NULL 2266 * @pdev: pointer to platform device handle 2267 * 2268 * Always returns 0 2269 */ 2270 static void ufs_qcom_remove(struct platform_device *pdev) 2271 { 2272 struct ufs_hba *hba = platform_get_drvdata(pdev); 2273 struct ufs_qcom_host *host = ufshcd_get_variant(hba); 2274 2275 ufshcd_pltfrm_remove(pdev); 2276 if (host->esi_enabled) 2277 platform_device_msi_free_irqs_all(hba->dev); 2278 } 2279 2280 static const struct ufs_qcom_drvdata ufs_qcom_sm8550_drvdata = { 2281 .quirks = UFSHCD_QUIRK_BROKEN_LSDBS_CAP, 2282 .no_phy_retention = true, 2283 }; 2284 2285 static const struct of_device_id ufs_qcom_of_match[] __maybe_unused = { 2286 { .compatible = "qcom,ufshc" }, 2287 { .compatible = "qcom,sm8550-ufshc", .data = &ufs_qcom_sm8550_drvdata }, 2288 { .compatible = "qcom,sm8650-ufshc", .data = &ufs_qcom_sm8550_drvdata }, 2289 {}, 2290 }; 2291 MODULE_DEVICE_TABLE(of, ufs_qcom_of_match); 2292 2293 #ifdef CONFIG_ACPI 2294 static const struct acpi_device_id ufs_qcom_acpi_match[] = { 2295 { "QCOM24A5" }, 2296 { }, 2297 }; 2298 MODULE_DEVICE_TABLE(acpi, ufs_qcom_acpi_match); 2299 #endif 2300 2301 static const struct dev_pm_ops ufs_qcom_pm_ops = { 2302 SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL) 2303 .prepare = ufshcd_suspend_prepare, 2304 .complete = ufshcd_resume_complete, 2305 #ifdef CONFIG_PM_SLEEP 2306 .suspend = ufshcd_system_suspend, 2307 .resume = ufshcd_system_resume, 2308 .freeze = ufshcd_system_freeze, 2309 .restore = ufshcd_system_restore, 2310 .thaw = ufshcd_system_thaw, 2311 #endif 2312 }; 2313 2314 static struct platform_driver ufs_qcom_pltform = { 2315 .probe = ufs_qcom_probe, 2316 .remove = ufs_qcom_remove, 2317 .driver = { 2318 .name = "ufshcd-qcom", 2319 .pm = &ufs_qcom_pm_ops, 2320 .of_match_table = of_match_ptr(ufs_qcom_of_match), 2321 .acpi_match_table = ACPI_PTR(ufs_qcom_acpi_match), 2322 }, 2323 }; 2324 module_platform_driver(ufs_qcom_pltform); 2325 2326 MODULE_DESCRIPTION("Qualcomm UFS host controller driver"); 2327 MODULE_LICENSE("GPL v2"); 2328