Bhupesh Sharma | 068f06c | 2023-08-11 11:44:00 +0530 | [diff] [blame] | 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 <reset.h> |
| 9 | #include <linux/bitfield.h> |
| 10 | |
| 11 | #define MAX_UFS_QCOM_HOSTS 1 |
| 12 | #define MAX_U32 (~(u32)0) |
| 13 | #define MPHY_TX_FSM_STATE 0x41 |
| 14 | #define TX_FSM_HIBERN8 0x1 |
| 15 | #define HBRN8_POLL_TOUT_MS 100 |
| 16 | #define DEFAULT_CLK_RATE_HZ 1000000 |
| 17 | #define BUS_VECTOR_NAME_LEN 32 |
| 18 | #define MAX_SUPP_MAC 64 |
| 19 | |
| 20 | #define UFS_HW_VER_MAJOR_MASK GENMASK(31, 28) |
| 21 | #define UFS_HW_VER_MINOR_MASK GENMASK(27, 16) |
| 22 | #define UFS_HW_VER_STEP_MASK GENMASK(15, 0) |
| 23 | |
| 24 | /* vendor specific pre-defined parameters */ |
| 25 | #define SLOW 1 |
| 26 | #define FAST 2 |
| 27 | |
| 28 | #define UFS_QCOM_LIMIT_HS_RATE PA_HS_MODE_B |
| 29 | |
| 30 | /* QCOM UFS host controller vendor specific registers */ |
| 31 | enum { |
| 32 | REG_UFS_SYS1CLK_1US = 0xC0, |
| 33 | REG_UFS_TX_SYMBOL_CLK_NS_US = 0xC4, |
| 34 | REG_UFS_LOCAL_PORT_ID_REG = 0xC8, |
| 35 | REG_UFS_PA_ERR_CODE = 0xCC, |
| 36 | /* On older UFS revisions, this register is called "RETRY_TIMER_REG" */ |
| 37 | REG_UFS_PARAM0 = 0xD0, |
| 38 | /* On older UFS revisions, this register is called "REG_UFS_PA_LINK_STARTUP_TIMER" */ |
| 39 | REG_UFS_CFG0 = 0xD8, |
| 40 | REG_UFS_CFG1 = 0xDC, |
| 41 | REG_UFS_CFG2 = 0xE0, |
| 42 | REG_UFS_HW_VERSION = 0xE4, |
| 43 | |
| 44 | UFS_TEST_BUS = 0xE8, |
| 45 | UFS_TEST_BUS_CTRL_0 = 0xEC, |
| 46 | UFS_TEST_BUS_CTRL_1 = 0xF0, |
| 47 | UFS_TEST_BUS_CTRL_2 = 0xF4, |
| 48 | UFS_UNIPRO_CFG = 0xF8, |
| 49 | |
| 50 | /* |
| 51 | * QCOM UFS host controller vendor specific registers |
| 52 | * added in HW Version 3.0.0 |
| 53 | */ |
| 54 | UFS_AH8_CFG = 0xFC, |
| 55 | |
| 56 | REG_UFS_CFG3 = 0x271C, |
| 57 | }; |
| 58 | |
| 59 | /* QCOM UFS host controller vendor specific debug registers */ |
| 60 | enum { |
| 61 | UFS_DBG_RD_REG_UAWM = 0x100, |
| 62 | UFS_DBG_RD_REG_UARM = 0x200, |
| 63 | UFS_DBG_RD_REG_TXUC = 0x300, |
| 64 | UFS_DBG_RD_REG_RXUC = 0x400, |
| 65 | UFS_DBG_RD_REG_DFC = 0x500, |
| 66 | UFS_DBG_RD_REG_TRLUT = 0x600, |
| 67 | UFS_DBG_RD_REG_TMRLUT = 0x700, |
| 68 | UFS_UFS_DBG_RD_REG_OCSC = 0x800, |
| 69 | |
| 70 | UFS_UFS_DBG_RD_DESC_RAM = 0x1500, |
| 71 | UFS_UFS_DBG_RD_PRDT_RAM = 0x1700, |
| 72 | UFS_UFS_DBG_RD_RESP_RAM = 0x1800, |
| 73 | UFS_UFS_DBG_RD_EDTL_RAM = 0x1900, |
| 74 | }; |
| 75 | |
| 76 | enum { |
| 77 | UFS_MEM_CQIS_VS = 0x8, |
| 78 | }; |
| 79 | |
| 80 | #define UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(x) (0x000 + x) |
| 81 | #define UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(x) (0x400 + x) |
| 82 | |
| 83 | /* bit definitions for REG_UFS_CFG0 register */ |
| 84 | #define QUNIPRO_G4_SEL BIT(5) |
| 85 | |
| 86 | /* bit definitions for REG_UFS_CFG1 register */ |
| 87 | #define QUNIPRO_SEL BIT(0) |
| 88 | #define UFS_PHY_SOFT_RESET BIT(1) |
| 89 | #define UTP_DBG_RAMS_EN BIT(17) |
| 90 | #define TEST_BUS_EN BIT(18) |
| 91 | #define TEST_BUS_SEL GENMASK(22, 19) |
| 92 | #define UFS_REG_TEST_BUS_EN BIT(30) |
| 93 | |
| 94 | #define UFS_PHY_RESET_ENABLE 1 |
| 95 | #define UFS_PHY_RESET_DISABLE 0 |
| 96 | |
| 97 | /* bit definitions for REG_UFS_CFG2 register */ |
| 98 | #define UAWM_HW_CGC_EN BIT(0) |
| 99 | #define UARM_HW_CGC_EN BIT(1) |
| 100 | #define TXUC_HW_CGC_EN BIT(2) |
| 101 | #define RXUC_HW_CGC_EN BIT(3) |
| 102 | #define DFC_HW_CGC_EN BIT(4) |
| 103 | #define TRLUT_HW_CGC_EN BIT(5) |
| 104 | #define TMRLUT_HW_CGC_EN BIT(6) |
| 105 | #define OCSC_HW_CGC_EN BIT(7) |
| 106 | |
| 107 | /* bit definitions for REG_UFS_PARAM0 */ |
| 108 | #define MAX_HS_GEAR_MASK GENMASK(6, 4) |
| 109 | #define UFS_QCOM_MAX_GEAR(x) FIELD_GET(MAX_HS_GEAR_MASK, (x)) |
| 110 | |
| 111 | /* bit definition for UFS_UFS_TEST_BUS_CTRL_n */ |
| 112 | #define TEST_BUS_SUB_SEL_MASK GENMASK(4, 0) /* All XXX_SEL fields are 5 bits wide */ |
| 113 | |
| 114 | #define REG_UFS_CFG2_CGC_EN_ALL (UAWM_HW_CGC_EN | UARM_HW_CGC_EN |\ |
| 115 | TXUC_HW_CGC_EN | RXUC_HW_CGC_EN |\ |
| 116 | DFC_HW_CGC_EN | TRLUT_HW_CGC_EN |\ |
| 117 | TMRLUT_HW_CGC_EN | OCSC_HW_CGC_EN) |
| 118 | |
| 119 | /* bit offset */ |
| 120 | #define OFFSET_CLK_NS_REG 0xa |
| 121 | |
| 122 | /* bit masks */ |
| 123 | #define MASK_TX_SYMBOL_CLK_1US_REG GENMASK(9, 0) |
| 124 | #define MASK_CLK_NS_REG GENMASK(23, 10) |
| 125 | |
| 126 | /* QUniPro Vendor specific attributes */ |
| 127 | #define PA_VS_CONFIG_REG1 0x9000 |
| 128 | #define DME_VS_CORE_CLK_CTRL 0xD002 |
| 129 | /* bit and mask definitions for DME_VS_CORE_CLK_CTRL attribute */ |
| 130 | #define DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT BIT(8) |
| 131 | #define DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK 0xFF |
| 132 | |
| 133 | static inline void |
| 134 | ufs_qcom_get_controller_revision(struct ufs_hba *hba, |
| 135 | u8 *major, u16 *minor, u16 *step) |
| 136 | { |
| 137 | u32 ver = ufshcd_readl(hba, REG_UFS_HW_VERSION); |
| 138 | |
| 139 | *major = FIELD_GET(UFS_HW_VER_MAJOR_MASK, ver); |
| 140 | *minor = FIELD_GET(UFS_HW_VER_MINOR_MASK, ver); |
| 141 | *step = FIELD_GET(UFS_HW_VER_STEP_MASK, ver); |
| 142 | }; |
| 143 | |
| 144 | static inline void ufs_qcom_assert_reset(struct ufs_hba *hba) |
| 145 | { |
| 146 | ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, FIELD_PREP(UFS_PHY_SOFT_RESET, UFS_PHY_RESET_ENABLE), |
| 147 | REG_UFS_CFG1); |
| 148 | |
| 149 | /* |
| 150 | * Make sure assertion of ufs phy reset is written to |
| 151 | * register before returning |
| 152 | */ |
| 153 | mb(); |
| 154 | } |
| 155 | |
| 156 | static inline void ufs_qcom_deassert_reset(struct ufs_hba *hba) |
| 157 | { |
| 158 | ufshcd_rmwl(hba, UFS_PHY_SOFT_RESET, FIELD_PREP(UFS_PHY_SOFT_RESET, UFS_PHY_RESET_DISABLE), |
| 159 | REG_UFS_CFG1); |
| 160 | |
| 161 | /* |
| 162 | * Make sure de-assertion of ufs phy reset is written to |
| 163 | * register before returning |
| 164 | */ |
| 165 | mb(); |
| 166 | } |
| 167 | |
| 168 | /* Host controller hardware version: major.minor.step */ |
| 169 | struct ufs_hw_version { |
| 170 | u16 step; |
| 171 | u16 minor; |
| 172 | u8 major; |
| 173 | }; |
| 174 | |
| 175 | struct ufs_qcom_testbus { |
| 176 | u8 select_major; |
| 177 | u8 select_minor; |
| 178 | }; |
| 179 | |
| 180 | struct gpio_desc; |
| 181 | |
| 182 | struct ufs_qcom_priv { |
| 183 | /* |
| 184 | * Set this capability if host controller supports the QUniPro mode |
| 185 | * and if driver wants the Host controller to operate in QUniPro mode. |
| 186 | * Note: By default this capability will be kept enabled if host |
| 187 | * controller supports the QUniPro mode. |
| 188 | */ |
| 189 | #define UFS_QCOM_CAP_QUNIPRO 0x1 |
| 190 | |
| 191 | /* |
| 192 | * Set this capability if host controller can retain the secure |
| 193 | * configuration even after UFS controller core power collapse. |
| 194 | */ |
| 195 | #define UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE 0x2 |
| 196 | u32 caps; |
| 197 | |
| 198 | struct phy *generic_phy; |
| 199 | struct ufs_hba *hba; |
| 200 | struct ufs_pa_layer_attr dev_req_params; |
| 201 | |
| 202 | struct clk *core_clk; |
| 203 | struct clk *bus_aggr_clk; |
| 204 | struct clk *iface_clk; |
| 205 | struct clk *core_clk_unipro; |
| 206 | struct clk *ref_clk; |
| 207 | bool is_core_clks_enabled; |
| 208 | |
| 209 | struct clk *rx_l0_sync_clk; |
| 210 | struct clk *tx_l0_sync_clk; |
| 211 | struct clk *rx_l1_sync_clk; |
| 212 | struct clk *tx_l1_sync_clk; |
| 213 | bool is_lane_clks_enabled; |
| 214 | |
| 215 | struct ufs_hw_version hw_ver; |
| 216 | |
| 217 | /* Reset control of HCI */ |
| 218 | struct reset_ctl core_reset; |
| 219 | |
| 220 | u32 hs_gear; |
| 221 | |
| 222 | int esi_base; |
| 223 | bool esi_enabled; |
| 224 | |
| 225 | void __iomem *dev_ref_clk_ctrl_mmio; |
| 226 | bool is_dev_ref_clk_enabled; |
| 227 | |
| 228 | u32 dev_ref_clk_en_mask; |
| 229 | }; |
| 230 | |
| 231 | static inline u32 |
| 232 | ufs_qcom_get_debug_reg_offset(struct ufs_qcom_priv *host, u32 reg) |
| 233 | { |
| 234 | if (host->hw_ver.major <= 0x02) |
| 235 | return UFS_CNTLR_2_x_x_VEN_REGS_OFFSET(reg); |
| 236 | |
| 237 | return UFS_CNTLR_3_x_x_VEN_REGS_OFFSET(reg); |
| 238 | }; |
| 239 | |
| 240 | #define ufs_qcom_is_link_off(hba) ufshcd_is_link_off(hba) |
| 241 | #define ufs_qcom_is_link_active(hba) ufshcd_is_link_active(hba) |
| 242 | #define ufs_qcom_is_link_hibern8(hba) ufshcd_is_link_hibern8(hba) |
| 243 | |
| 244 | int ufs_qcom_testbus_config(struct ufs_qcom_priv *host); |
| 245 | |
| 246 | static inline bool ufs_qcom_cap_qunipro(struct ufs_qcom_priv *host) |
| 247 | { |
| 248 | return host->caps & UFS_QCOM_CAP_QUNIPRO; |
| 249 | } |
| 250 | |
| 251 | /* ufs-qcom-ice.c */ |
| 252 | |
| 253 | #ifdef CONFIG_SCSI_UFS_CRYPTO |
| 254 | int ufs_qcom_ice_init(struct ufs_qcom_priv *host); |
| 255 | int ufs_qcom_ice_enable(struct ufs_qcom_priv *host); |
| 256 | int ufs_qcom_ice_resume(struct ufs_qcom_priv *host); |
| 257 | int ufs_qcom_ice_program_key(struct ufs_hba *hba, |
| 258 | const union ufs_crypto_cfg_entry *cfg, int slot); |
| 259 | #else |
| 260 | static inline int ufs_qcom_ice_init(struct ufs_qcom_priv *host) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | static inline int ufs_qcom_ice_enable(struct ufs_qcom_priv *host) |
| 265 | { |
| 266 | return 0; |
| 267 | } |
| 268 | static inline int ufs_qcom_ice_resume(struct ufs_qcom_priv *host) |
| 269 | { |
| 270 | return 0; |
| 271 | } |
| 272 | #define ufs_qcom_ice_program_key NULL |
| 273 | #endif /* !CONFIG_SCSI_UFS_CRYPTO */ |
| 274 | |
| 275 | #endif /* UFS_QCOM_H_ */ |