1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/clk-provider.h>
8 #include <linux/delay.h>
9 #include <linux/err.h>
10 #include <linux/io.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/phy/phy.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/reset.h>
20 #include <linux/slab.h>
21 
22 #include <ufs/unipro.h>
23 #include "phy-qcom-qmp.h"
24 #include "phy-qcom-qmp-pcs-ufs-v2.h"
25 #include "phy-qcom-qmp-pcs-ufs-v3.h"
26 #include "phy-qcom-qmp-pcs-ufs-v4.h"
27 #include "phy-qcom-qmp-pcs-ufs-v5.h"
28 #include "phy-qcom-qmp-pcs-ufs-v6.h"
29 
30 #include "phy-qcom-qmp-qserdes-txrx-ufs-v6.h"
31 
32 /* QPHY_SW_RESET bit */
33 #define SW_RESET				BIT(0)
34 /* QPHY_POWER_DOWN_CONTROL */
35 #define SW_PWRDN				BIT(0)
36 /* QPHY_START_CONTROL bits */
37 #define SERDES_START				BIT(0)
38 #define PCS_START				BIT(1)
39 /* QPHY_PCS_READY_STATUS bit */
40 #define PCS_READY				BIT(0)
41 
42 #define PHY_INIT_COMPLETE_TIMEOUT		10000
43 
44 struct qmp_phy_init_tbl {
45 	unsigned int offset;
46 	unsigned int val;
47 	/*
48 	 * mask of lanes for which this register is written
49 	 * for cases when second lane needs different values
50 	 */
51 	u8 lane_mask;
52 };
53 
54 #define QMP_PHY_INIT_CFG(o, v)		\
55 	{				\
56 		.offset = o,		\
57 		.val = v,		\
58 		.lane_mask = 0xff,	\
59 	}
60 
61 #define QMP_PHY_INIT_CFG_LANE(o, v, l)	\
62 	{				\
63 		.offset = o,		\
64 		.val = v,		\
65 		.lane_mask = l,		\
66 	}
67 
68 /* set of registers with offsets different per-PHY */
69 enum qphy_reg_layout {
70 	/* PCS registers */
71 	QPHY_SW_RESET,
72 	QPHY_START_CTRL,
73 	QPHY_PCS_READY_STATUS,
74 	QPHY_PCS_POWER_DOWN_CONTROL,
75 	/* Keep last to ensure regs_layout arrays are properly initialized */
76 	QPHY_LAYOUT_SIZE
77 };
78 
79 static const unsigned int ufsphy_v2_regs_layout[QPHY_LAYOUT_SIZE] = {
80 	[QPHY_START_CTRL]		= QPHY_V2_PCS_UFS_PHY_START,
81 	[QPHY_PCS_READY_STATUS]		= QPHY_V2_PCS_UFS_READY_STATUS,
82 	[QPHY_PCS_POWER_DOWN_CONTROL]	= QPHY_V2_PCS_UFS_POWER_DOWN_CONTROL,
83 };
84 
85 static const unsigned int ufsphy_v3_regs_layout[QPHY_LAYOUT_SIZE] = {
86 	[QPHY_START_CTRL]		= QPHY_V3_PCS_UFS_PHY_START,
87 	[QPHY_PCS_READY_STATUS]		= QPHY_V3_PCS_UFS_READY_STATUS,
88 	[QPHY_PCS_POWER_DOWN_CONTROL]	= QPHY_V3_PCS_UFS_POWER_DOWN_CONTROL,
89 };
90 
91 static const unsigned int ufsphy_v4_regs_layout[QPHY_LAYOUT_SIZE] = {
92 	[QPHY_START_CTRL]		= QPHY_V4_PCS_UFS_PHY_START,
93 	[QPHY_PCS_READY_STATUS]		= QPHY_V4_PCS_UFS_READY_STATUS,
94 	[QPHY_SW_RESET]			= QPHY_V4_PCS_UFS_SW_RESET,
95 	[QPHY_PCS_POWER_DOWN_CONTROL]	= QPHY_V4_PCS_UFS_POWER_DOWN_CONTROL,
96 };
97 
98 static const unsigned int ufsphy_v5_regs_layout[QPHY_LAYOUT_SIZE] = {
99 	[QPHY_START_CTRL]		= QPHY_V5_PCS_UFS_PHY_START,
100 	[QPHY_PCS_READY_STATUS]		= QPHY_V5_PCS_UFS_READY_STATUS,
101 	[QPHY_SW_RESET]			= QPHY_V5_PCS_UFS_SW_RESET,
102 	[QPHY_PCS_POWER_DOWN_CONTROL]	= QPHY_V5_PCS_UFS_POWER_DOWN_CONTROL,
103 };
104 
105 static const unsigned int ufsphy_v6_regs_layout[QPHY_LAYOUT_SIZE] = {
106 	[QPHY_START_CTRL]		= QPHY_V6_PCS_UFS_PHY_START,
107 	[QPHY_PCS_READY_STATUS]		= QPHY_V6_PCS_UFS_READY_STATUS,
108 	[QPHY_SW_RESET]			= QPHY_V6_PCS_UFS_SW_RESET,
109 	[QPHY_PCS_POWER_DOWN_CONTROL]	= QPHY_V6_PCS_UFS_POWER_DOWN_CONTROL,
110 };
111 
112 static const struct qmp_phy_init_tbl msm8996_ufsphy_serdes[] = {
113 	QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x0e),
114 	QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0xd7),
115 	QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
116 	QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x06),
117 	QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
118 	QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
119 	QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x05),
120 	QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
121 	QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV_MODE1, 0x0a),
122 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x01),
123 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0x10),
124 	QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x20),
125 	QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
126 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
127 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0xff),
128 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x3f),
129 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x54),
130 	QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x05),
131 	QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
132 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x00),
133 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x00),
134 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x00),
135 	QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
136 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
137 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
138 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
139 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
140 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE1_MODE0, 0x28),
141 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE2_MODE0, 0x02),
142 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0xff),
143 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x0c),
144 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
145 	QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE1, 0x98),
146 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE1, 0x00),
147 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE1, 0x00),
148 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE1, 0x00),
149 	QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE1, 0x0b),
150 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE1, 0x16),
151 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE1, 0x28),
152 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE1, 0x80),
153 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE1, 0x00),
154 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE1_MODE1, 0xd6),
155 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE2_MODE1, 0x00),
156 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE1, 0x32),
157 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE1, 0x0f),
158 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE1, 0x00),
159 };
160 
161 static const struct qmp_phy_init_tbl msm8996_ufsphy_tx[] = {
162 	QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
163 	QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x02),
164 };
165 
166 static const struct qmp_phy_init_tbl msm8996_ufsphy_rx[] = {
167 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x24),
168 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x02),
169 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_INTERFACE_MODE, 0x00),
170 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x18),
171 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_FASTLOCK_FO_GAIN, 0x0B),
172 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_TERM_BW, 0x5b),
173 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN1_LSB, 0xff),
174 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN1_MSB, 0x3f),
175 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN2_LSB, 0xff),
176 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN2_MSB, 0x0f),
177 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0E),
178 };
179 
180 static const struct qmp_phy_init_tbl sm6115_ufsphy_serdes[] = {
181 	QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x0e),
182 	QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x14),
183 	QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
184 	QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x02),
185 	QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
186 	QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
187 	QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
188 	QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
189 	QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV_MODE1, 0x0a),
190 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x01),
191 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0x00),
192 	QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x20),
193 	QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
194 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
195 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0xff),
196 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x3f),
197 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x04),
198 	QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x05),
199 	QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
200 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x00),
201 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x00),
202 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x00),
203 	QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
204 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
205 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
206 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
207 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
208 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE1_MODE0, 0x28),
209 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE2_MODE0, 0x02),
210 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0xff),
211 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x0c),
212 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
213 	QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE1, 0x98),
214 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE1, 0x00),
215 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE1, 0x00),
216 	QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE1, 0x00),
217 	QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE1, 0x0b),
218 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE1, 0x16),
219 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE1, 0x28),
220 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE1, 0x80),
221 	QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE1, 0x00),
222 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE1_MODE1, 0xd6),
223 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE2_MODE1, 0x00),
224 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE1, 0x32),
225 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE1, 0x0f),
226 	QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE1, 0x00),
227 	QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
228 	QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
229 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_INITVAL1, 0xff),
230 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_INITVAL2, 0x00),
231 };
232 
233 static const struct qmp_phy_init_tbl sm6115_ufsphy_hs_b_serdes[] = {
234 	QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x44),
235 };
236 
237 static const struct qmp_phy_init_tbl sm6115_ufsphy_tx[] = {
238 	QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
239 	QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
240 };
241 
242 static const struct qmp_phy_init_tbl sm6115_ufsphy_rx[] = {
243 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x24),
244 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x0F),
245 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_INTERFACE_MODE, 0x40),
246 	QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x1E),
247 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_FASTLOCK_FO_GAIN, 0x0B),
248 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_TERM_BW, 0x5B),
249 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN1_LSB, 0xFF),
250 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN1_MSB, 0x3F),
251 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN2_LSB, 0xFF),
252 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_GAIN2_MSB, 0x3F),
253 	QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0D),
254 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SVS_SO_GAIN_HALF, 0x04),
255 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SVS_SO_GAIN_QUARTER, 0x04),
256 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SVS_SO_GAIN, 0x04),
257 	QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x5B),
258 };
259 
260 static const struct qmp_phy_init_tbl sm6115_ufsphy_pcs[] = {
261 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_RX_PWM_GEAR_BAND, 0x15),
262 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_RX_SIGDET_CTRL2, 0x6d),
263 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0f),
264 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_TX_SMALL_AMP_DRV_LVL, 0x02),
265 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_RX_MIN_STALL_NOCONFIG_TIME_CAP, 0x28),
266 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_RX_SYM_RESYNC_CTRL, 0x03),
267 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_TX_LARGE_AMP_POST_EMP_LVL, 0x12),
268 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_TX_SMALL_AMP_POST_EMP_LVL, 0x0f),
269 	QMP_PHY_INIT_CFG(QPHY_V2_PCS_UFS_RX_MIN_HIBERN8_TIME, 0x9a), /* 8 us */
270 };
271 
272 static const struct qmp_phy_init_tbl sdm845_ufsphy_serdes[] = {
273 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
274 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x04),
275 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_BG_TIMER, 0x0a),
276 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
277 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
278 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0xd5),
279 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL, 0x20),
280 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
281 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x00),
282 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x01),
283 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_CTRL, 0x00),
284 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
285 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x04),
286 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x05),
287 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_INITVAL1, 0xff),
288 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_INITVAL2, 0x00),
289 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
290 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
291 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
292 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
293 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
294 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
295 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xda),
296 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
297 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0xff),
298 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x0c),
299 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE1, 0x98),
300 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE1, 0x06),
301 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE1, 0x16),
302 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE1, 0x36),
303 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE1, 0x3f),
304 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE1, 0x00),
305 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE1, 0xc1),
306 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE1, 0x00),
307 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE1, 0x32),
308 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE1, 0x0f),
309 };
310 
311 static const struct qmp_phy_init_tbl sdm845_ufsphy_hs_b_serdes[] = {
312 	QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x44),
313 };
314 
315 static const struct qmp_phy_init_tbl sdm845_ufsphy_tx[] = {
316 	QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x06),
317 	QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x04),
318 	QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x07),
319 };
320 
321 static const struct qmp_phy_init_tbl sdm845_ufsphy_rx[] = {
322 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_LVL, 0x24),
323 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x0f),
324 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
325 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_INTERFACE_MODE, 0x40),
326 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
327 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_TERM_BW, 0x5b),
328 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
329 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
330 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1b),
331 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_HALF, 0x04),
332 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_QUARTER, 0x04),
333 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN, 0x04),
334 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
335 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x81),
336 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
337 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x59),
338 };
339 
340 static const struct qmp_phy_init_tbl sdm845_ufsphy_pcs[] = {
341 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SIGDET_CTRL2, 0x6e),
342 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0a),
343 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_SMALL_AMP_DRV_LVL, 0x02),
344 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SYM_RESYNC_CTRL, 0x03),
345 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_MID_TERM_CTRL1, 0x43),
346 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SIGDET_CTRL1, 0x0f),
347 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_MIN_HIBERN8_TIME, 0x9a),
348 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_MULTI_LANE_CTRL1, 0x02),
349 };
350 
351 static const struct qmp_phy_init_tbl sm7150_ufsphy_rx[] = {
352 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_LVL, 0x24),
353 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x0f),
354 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
355 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_INTERFACE_MODE, 0x40),
356 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
357 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_TERM_BW, 0x5b),
358 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
359 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
360 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1b),
361 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_HALF, 0x04),
362 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_QUARTER, 0x04),
363 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN, 0x04),
364 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x5b),
365 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x81),
366 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
367 	QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x59),
368 };
369 
370 static const struct qmp_phy_init_tbl sm7150_ufsphy_pcs[] = {
371 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SIGDET_CTRL2, 0x6f),
372 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0f),
373 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_SMALL_AMP_DRV_LVL, 0x02),
374 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SYM_RESYNC_CTRL, 0x03),
375 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_TX_MID_TERM_CTRL1, 0x43),
376 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_SIGDET_CTRL1, 0x0f),
377 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_RX_MIN_HIBERN8_TIME, 0xff),
378 	QMP_PHY_INIT_CFG(QPHY_V3_PCS_UFS_MULTI_LANE_CTRL1, 0x02),
379 };
380 
381 static const struct qmp_phy_init_tbl sm8150_ufsphy_serdes[] = {
382 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_SYSCLK_EN_SEL, 0xd9),
383 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_HSCLK_SEL, 0x11),
384 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_HSCLK_HS_SWITCH_SEL, 0x00),
385 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP_EN, 0x01),
386 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_MAP, 0x02),
387 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_IVCO, 0x0f),
388 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_INITVAL2, 0x00),
389 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_HSCLK_SEL, 0x11),
390 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_DEC_START_MODE0, 0x82),
391 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_CP_CTRL_MODE0, 0x06),
392 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_RCTRL_MODE0, 0x16),
393 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_CCTRL_MODE0, 0x36),
394 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP1_MODE0, 0xff),
395 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP2_MODE0, 0x0c),
396 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE1_MODE0, 0xac),
397 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE2_MODE0, 0x1e),
398 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_DEC_START_MODE1, 0x98),
399 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_CP_CTRL_MODE1, 0x06),
400 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_RCTRL_MODE1, 0x16),
401 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_CCTRL_MODE1, 0x36),
402 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP1_MODE1, 0x32),
403 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP2_MODE1, 0x0f),
404 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE1_MODE1, 0xdd),
405 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE2_MODE1, 0x23),
406 };
407 
408 static const struct qmp_phy_init_tbl sm8150_ufsphy_hs_b_serdes[] = {
409 	QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_MAP, 0x06),
410 };
411 
412 static const struct qmp_phy_init_tbl sm8150_ufsphy_tx[] = {
413 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_1_DIVIDER_BAND0_1, 0x06),
414 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_2_DIVIDER_BAND0_1, 0x03),
415 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_3_DIVIDER_BAND0_1, 0x01),
416 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_4_DIVIDER_BAND0_1, 0x00),
417 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_LANE_MODE_1, 0x05),
418 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_TRAN_DRVR_EMP_EN, 0x0c),
419 };
420 
421 static const struct qmp_phy_init_tbl sm8150_ufsphy_hs_g4_tx[] = {
422 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_LANE_MODE_1, 0x75),
423 };
424 
425 static const struct qmp_phy_init_tbl sm8150_ufsphy_rx[] = {
426 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_LVL, 0x24),
427 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_CNTRL, 0x0f),
428 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
429 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_BAND, 0x18),
430 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FASTLOCK_FO_GAIN, 0x0a),
431 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
432 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CONTROLS, 0xf1),
433 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
434 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CTRL2, 0x80),
435 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FO_GAIN, 0x0c),
436 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_GAIN, 0x04),
437 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_TERM_BW, 0x1b),
438 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
439 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
440 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1d),
441 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x00),
442 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_MEASURE_TIME, 0x10),
443 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_LOW, 0xc0),
444 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_HIGH, 0x00),
445 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_LOW, 0x36),
446 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH, 0x36),
447 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH2, 0xf6),
448 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH3, 0x3b),
449 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH4, 0x3d),
450 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_LOW, 0xe0),
451 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH, 0xc8),
452 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH2, 0xc8),
453 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH3, 0x3b),
454 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH4, 0xb1),
455 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_LOW, 0xe0),
456 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH, 0xc8),
457 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH2, 0xc8),
458 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH3, 0x3b),
459 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH4, 0xb1),
460 };
461 
462 static const struct qmp_phy_init_tbl sm8150_ufsphy_hs_g4_rx[] = {
463 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x5a),
464 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CTRL2, 0x81),
465 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FO_GAIN, 0x0e),
466 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_TERM_BW, 0x6f),
467 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_MEASURE_TIME, 0x20),
468 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_LOW, 0x80),
469 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_HIGH, 0x01),
470 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_LOW, 0x3f),
471 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH, 0xff),
472 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH2, 0xff),
473 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH3, 0x7f),
474 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH4, 0x6c),
475 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_LOW, 0x6d),
476 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH, 0x6d),
477 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH2, 0xed),
478 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH4, 0x3c),
479 };
480 
481 static const struct qmp_phy_init_tbl sm8150_ufsphy_pcs[] = {
482 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_RX_SIGDET_CTRL2, 0x6d),
483 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0a),
484 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_TX_SMALL_AMP_DRV_LVL, 0x02),
485 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_TX_MID_TERM_CTRL1, 0x43),
486 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_DEBUG_BUS_CLKSEL, 0x1f),
487 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_RX_MIN_HIBERN8_TIME, 0xff),
488 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_MULTI_LANE_CTRL1, 0x02),
489 };
490 
491 static const struct qmp_phy_init_tbl sm8150_ufsphy_hs_g4_pcs[] = {
492 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x10),
493 	QMP_PHY_INIT_CFG(QPHY_V4_PCS_UFS_BIST_FIXED_PAT_CTRL, 0x0a),
494 };
495 
496 static const struct qmp_phy_init_tbl sm8250_ufsphy_hs_g4_tx[] = {
497 	QMP_PHY_INIT_CFG(QSERDES_V4_TX_LANE_MODE_1, 0xe5),
498 };
499 
500 static const struct qmp_phy_init_tbl sm8250_ufsphy_hs_g4_rx[] = {
501 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x5a),
502 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CTRL2, 0x81),
503 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FO_GAIN, 0x0e),
504 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_TERM_BW, 0x6f),
505 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL1, 0x04),
506 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL2, 0x00),
507 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL3, 0x09),
508 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL4, 0x07),
509 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x17),
510 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_MEASURE_TIME, 0x20),
511 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_LOW, 0x80),
512 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_HIGH, 0x01),
513 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_LOW, 0x3f),
514 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH, 0xff),
515 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH2, 0xff),
516 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH3, 0x7f),
517 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH4, 0x2c),
518 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_LOW, 0x6d),
519 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH, 0x6d),
520 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH2, 0xed),
521 	QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH4, 0x3c),
522 };
523 
524 static const struct qmp_phy_init_tbl sm8350_ufsphy_serdes[] = {
525 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_SYSCLK_EN_SEL, 0xd9),
526 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_HSCLK_SEL, 0x11),
527 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_HSCLK_HS_SWITCH_SEL, 0x00),
528 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_LOCK_CMP_EN, 0x42),
529 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_VCO_TUNE_MAP, 0x02),
530 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_PLL_IVCO, 0x0f),
531 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_VCO_TUNE_INITVAL2, 0x00),
532 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_BIN_VCOCAL_HSCLK_SEL, 0x11),
533 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_DEC_START_MODE0, 0x82),
534 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_CP_CTRL_MODE0, 0x14),
535 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_PLL_RCTRL_MODE0, 0x18),
536 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_PLL_CCTRL_MODE0, 0x18),
537 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_LOCK_CMP1_MODE0, 0xff),
538 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_LOCK_CMP2_MODE0, 0x19),
539 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_BIN_VCOCAL_CMP_CODE1_MODE0, 0xac),
540 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_BIN_VCOCAL_CMP_CODE2_MODE0, 0x1e),
541 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_DEC_START_MODE1, 0x98),
542 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_CP_CTRL_MODE1, 0x14),
543 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_PLL_RCTRL_MODE1, 0x18),
544 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_PLL_CCTRL_MODE1, 0x18),
545 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_LOCK_CMP1_MODE1, 0x65),
546 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_LOCK_CMP2_MODE1, 0x1e),
547 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_BIN_VCOCAL_CMP_CODE1_MODE1, 0xdd),
548 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_BIN_VCOCAL_CMP_CODE2_MODE1, 0x23),
549 };
550 
551 static const struct qmp_phy_init_tbl sm8350_ufsphy_hs_b_serdes[] = {
552 	QMP_PHY_INIT_CFG(QSERDES_V5_COM_VCO_TUNE_MAP, 0x06),
553 };
554 
555 static const struct qmp_phy_init_tbl sm8350_ufsphy_tx[] = {
556 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_PWM_GEAR_1_DIVIDER_BAND0_1, 0x06),
557 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_PWM_GEAR_2_DIVIDER_BAND0_1, 0x03),
558 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_PWM_GEAR_3_DIVIDER_BAND0_1, 0x01),
559 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_PWM_GEAR_4_DIVIDER_BAND0_1, 0x00),
560 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_LANE_MODE_1, 0xf5),
561 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_LANE_MODE_3, 0x3f),
562 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_RES_CODE_LANE_OFFSET_TX, 0x09),
563 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_RES_CODE_LANE_OFFSET_RX, 0x09),
564 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_TRAN_DRVR_EMP_EN, 0x0c),
565 };
566 
567 static const struct qmp_phy_init_tbl sm8350_ufsphy_rx[] = {
568 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_SIGDET_LVL, 0x24),
569 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_SIGDET_CNTRL, 0x0f),
570 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
571 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_BAND, 0x18),
572 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_FASTLOCK_FO_GAIN, 0x0a),
573 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x5a),
574 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_PI_CONTROLS, 0xf1),
575 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
576 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_PI_CTRL2, 0x80),
577 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_FO_GAIN, 0x0e),
578 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_SO_GAIN, 0x04),
579 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_TERM_BW, 0x1b),
580 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL1, 0x04),
581 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
582 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
583 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1a),
584 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x17),
585 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x00),
586 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_MEASURE_TIME, 0x10),
587 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_TSETTLE_LOW, 0xc0),
588 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_TSETTLE_HIGH, 0x00),
589 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_LOW, 0x6d),
590 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH, 0x6d),
591 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH2, 0xed),
592 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH3, 0x3b),
593 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH4, 0x3c),
594 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_LOW, 0xe0),
595 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH, 0xc8),
596 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH2, 0xc8),
597 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH3, 0x3b),
598 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH4, 0xb7),
599 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_10_LOW, 0xe0),
600 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_10_HIGH, 0xc8),
601 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_10_HIGH2, 0xc8),
602 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_10_HIGH3, 0x3b),
603 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_10_HIGH4, 0xb7),
604 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_DCC_CTRL1, 0x0c),
605 };
606 
607 static const struct qmp_phy_init_tbl sm8350_ufsphy_pcs[] = {
608 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_RX_SIGDET_CTRL2, 0x6d),
609 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0a),
610 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_TX_SMALL_AMP_DRV_LVL, 0x02),
611 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_TX_MID_TERM_CTRL1, 0x43),
612 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_DEBUG_BUS_CLKSEL, 0x1f),
613 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_RX_MIN_HIBERN8_TIME, 0xff),
614 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_RX_SIGDET_CTRL1, 0x0e),
615 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_MULTI_LANE_CTRL1, 0x02),
616 };
617 
618 static const struct qmp_phy_init_tbl sm8350_ufsphy_g4_tx[] = {
619 	QMP_PHY_INIT_CFG(QSERDES_V5_TX_LANE_MODE_1, 0xe5),
620 };
621 
622 static const struct qmp_phy_init_tbl sm8350_ufsphy_g4_rx[] = {
623 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_UCDR_PI_CTRL2, 0x81),
624 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_TERM_BW, 0x6f),
625 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL2, 0x00),
626 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4a),
627 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_EQU_ADAPTOR_CNTRL4, 0x0a),
628 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_MEASURE_TIME, 0x20),
629 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_TSETTLE_LOW, 0x80),
630 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_IDAC_TSETTLE_HIGH, 0x01),
631 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_LOW, 0xbf),
632 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH, 0xbf),
633 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH2, 0x7f),
634 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH3, 0x7f),
635 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_00_HIGH4, 0x2d),
636 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_LOW, 0x6d),
637 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH, 0x6d),
638 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH2, 0xed),
639 	QMP_PHY_INIT_CFG(QSERDES_V5_RX_RX_MODE_01_HIGH4, 0x3c),
640 };
641 
642 static const struct qmp_phy_init_tbl sm8350_ufsphy_g4_pcs[] = {
643 	QMP_PHY_INIT_CFG(QPHY_V5_PCS_UFS_BIST_FIXED_PAT_CTRL, 0x0a),
644 };
645 
646 static const struct qmp_phy_init_tbl sm8550_ufsphy_serdes[] = {
647 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_SYSCLK_EN_SEL, 0xd9),
648 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_CMN_CONFIG_1, 0x16),
649 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_HSCLK_SEL_1, 0x11),
650 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_HSCLK_HS_SWITCH_SEL_1, 0x00),
651 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_LOCK_CMP_EN, 0x01),
652 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_VCO_TUNE_MAP, 0x04),
653 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_PLL_IVCO, 0x0f),
654 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_VCO_TUNE_INITVAL2, 0x00),
655 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_DEC_START_MODE0, 0x41),
656 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_CP_CTRL_MODE0, 0x0a),
657 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_PLL_RCTRL_MODE0, 0x18),
658 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_PLL_CCTRL_MODE0, 0x14),
659 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_LOCK_CMP1_MODE0, 0x7f),
660 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_LOCK_CMP2_MODE0, 0x06),
661 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_DEC_START_MODE0, 0x4c),
662 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_CP_CTRL_MODE0, 0x0a),
663 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_PLL_RCTRL_MODE0, 0x18),
664 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_PLL_CCTRL_MODE0, 0x14),
665 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_LOCK_CMP1_MODE0, 0x99),
666 	QMP_PHY_INIT_CFG(QSERDES_V6_COM_LOCK_CMP2_MODE0, 0x07),
667 };
668 
669 static const struct qmp_phy_init_tbl sm8550_ufsphy_tx[] = {
670 	QMP_PHY_INIT_CFG(QSERDES_V6_TX_LANE_MODE_1, 0x05),
671 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_TX_RES_CODE_LANE_OFFSET_TX, 0x07),
672 };
673 
674 static const struct qmp_phy_init_tbl sm8550_ufsphy_rx[] = {
675 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_UCDR_FASTLOCK_FO_GAIN_RATE2, 0x0c),
676 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_UCDR_FASTLOCK_FO_GAIN_RATE4, 0x0f),
677 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_VGA_CAL_MAN_VAL, 0x0e),
678 
679 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE_0_1_B0, 0xc2),
680 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE_0_1_B1, 0xc2),
681 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE_0_1_B3, 0x1a),
682 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE_0_1_B6, 0x60),
683 
684 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE2_B3, 0x9e),
685 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE2_B6, 0x60),
686 
687 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE3_B3, 0x9e),
688 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE3_B4, 0x0e),
689 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE3_B5, 0x36),
690 	QMP_PHY_INIT_CFG(QSERDES_UFS_V6_RX_MODE_RATE3_B8, 0x02),
691 };
692 
693 static const struct qmp_phy_init_tbl sm8550_ufsphy_pcs[] = {
694 	QMP_PHY_INIT_CFG(QPHY_V6_PCS_UFS_RX_SIGDET_CTRL2, 0x69),
695 	QMP_PHY_INIT_CFG(QPHY_V6_PCS_UFS_TX_LARGE_AMP_DRV_LVL, 0x0f),
696 	QMP_PHY_INIT_CFG(QPHY_V6_PCS_UFS_TX_MID_TERM_CTRL1, 0x43),
697 	QMP_PHY_INIT_CFG(QPHY_V6_PCS_UFS_PLL_CNTL, 0x2b),
698 	QMP_PHY_INIT_CFG(QPHY_V6_PCS_UFS_MULTI_LANE_CTRL1, 0x02),
699 };
700 
701 struct qmp_ufs_offsets {
702 	u16 serdes;
703 	u16 pcs;
704 	u16 tx;
705 	u16 rx;
706 	u16 tx2;
707 	u16 rx2;
708 };
709 
710 struct qmp_phy_cfg_tbls {
711 	/* Init sequence for PHY blocks - serdes, tx, rx, pcs */
712 	const struct qmp_phy_init_tbl *serdes;
713 	int serdes_num;
714 	const struct qmp_phy_init_tbl *tx;
715 	int tx_num;
716 	const struct qmp_phy_init_tbl *rx;
717 	int rx_num;
718 	const struct qmp_phy_init_tbl *pcs;
719 	int pcs_num;
720 };
721 
722 /* struct qmp_phy_cfg - per-PHY initialization config */
723 struct qmp_phy_cfg {
724 	int lanes;
725 
726 	const struct qmp_ufs_offsets *offsets;
727 
728 	/* Main init sequence for PHY blocks - serdes, tx, rx, pcs */
729 	const struct qmp_phy_cfg_tbls tbls;
730 	/* Additional sequence for HS Series B */
731 	const struct qmp_phy_cfg_tbls tbls_hs_b;
732 	/* Additional sequence for HS G4 */
733 	const struct qmp_phy_cfg_tbls tbls_hs_g4;
734 
735 	/* clock ids to be requested */
736 	const char * const *clk_list;
737 	int num_clks;
738 	/* regulators to be requested */
739 	const char * const *vreg_list;
740 	int num_vregs;
741 
742 	/* array of registers with different offsets */
743 	const unsigned int *regs;
744 
745 	/* true, if PCS block has no separate SW_RESET register */
746 	bool no_pcs_sw_reset;
747 };
748 
749 struct qmp_ufs {
750 	struct device *dev;
751 
752 	const struct qmp_phy_cfg *cfg;
753 
754 	void __iomem *serdes;
755 	void __iomem *pcs;
756 	void __iomem *pcs_misc;
757 	void __iomem *tx;
758 	void __iomem *rx;
759 	void __iomem *tx2;
760 	void __iomem *rx2;
761 
762 	struct clk_bulk_data *clks;
763 	struct regulator_bulk_data *vregs;
764 	struct reset_control *ufs_reset;
765 
766 	struct phy *phy;
767 	u32 mode;
768 	u32 submode;
769 };
770 
771 static inline void qphy_setbits(void __iomem *base, u32 offset, u32 val)
772 {
773 	u32 reg;
774 
775 	reg = readl(base + offset);
776 	reg |= val;
777 	writel(reg, base + offset);
778 
779 	/* ensure that above write is through */
780 	readl(base + offset);
781 }
782 
783 static inline void qphy_clrbits(void __iomem *base, u32 offset, u32 val)
784 {
785 	u32 reg;
786 
787 	reg = readl(base + offset);
788 	reg &= ~val;
789 	writel(reg, base + offset);
790 
791 	/* ensure that above write is through */
792 	readl(base + offset);
793 }
794 
795 /* list of clocks required by phy */
796 static const char * const msm8996_ufs_phy_clk_l[] = {
797 	"ref",
798 };
799 
800 /* the primary usb3 phy on sm8250 doesn't have a ref clock */
801 static const char * const sm8450_ufs_phy_clk_l[] = {
802 	"qref", "ref", "ref_aux",
803 };
804 
805 static const char * const sdm845_ufs_phy_clk_l[] = {
806 	"ref", "ref_aux",
807 };
808 
809 /* list of regulators */
810 static const char * const qmp_phy_vreg_l[] = {
811 	"vdda-phy", "vdda-pll",
812 };
813 
814 static const struct qmp_ufs_offsets qmp_ufs_offsets = {
815 	.serdes		= 0,
816 	.pcs		= 0xc00,
817 	.tx		= 0x400,
818 	.rx		= 0x600,
819 	.tx2		= 0x800,
820 	.rx2		= 0xa00,
821 };
822 
823 static const struct qmp_ufs_offsets qmp_ufs_offsets_v6 = {
824 	.serdes		= 0,
825 	.pcs		= 0x0400,
826 	.tx		= 0x1000,
827 	.rx		= 0x1200,
828 	.tx2		= 0x1800,
829 	.rx2		= 0x1a00,
830 };
831 
832 static const struct qmp_phy_cfg msm8996_ufsphy_cfg = {
833 	.lanes			= 1,
834 
835 	.offsets		= &qmp_ufs_offsets,
836 
837 	.tbls = {
838 		.serdes		= msm8996_ufsphy_serdes,
839 		.serdes_num	= ARRAY_SIZE(msm8996_ufsphy_serdes),
840 		.tx		= msm8996_ufsphy_tx,
841 		.tx_num		= ARRAY_SIZE(msm8996_ufsphy_tx),
842 		.rx		= msm8996_ufsphy_rx,
843 		.rx_num		= ARRAY_SIZE(msm8996_ufsphy_rx),
844 	},
845 
846 	.clk_list		= msm8996_ufs_phy_clk_l,
847 	.num_clks		= ARRAY_SIZE(msm8996_ufs_phy_clk_l),
848 
849 	.vreg_list		= qmp_phy_vreg_l,
850 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
851 
852 	.regs			= ufsphy_v2_regs_layout,
853 
854 	.no_pcs_sw_reset	= true,
855 };
856 
857 static const struct qmp_phy_cfg sa8775p_ufsphy_cfg = {
858 	.lanes			= 2,
859 
860 	.offsets		= &qmp_ufs_offsets,
861 
862 	.tbls = {
863 		.serdes		= sm8350_ufsphy_serdes,
864 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_serdes),
865 		.tx		= sm8350_ufsphy_tx,
866 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_tx),
867 		.rx		= sm8350_ufsphy_rx,
868 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_rx),
869 		.pcs		= sm8350_ufsphy_pcs,
870 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_pcs),
871 	},
872 	.tbls_hs_b = {
873 		.serdes		= sm8350_ufsphy_hs_b_serdes,
874 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_hs_b_serdes),
875 	},
876 	.tbls_hs_g4 = {
877 		.tx		= sm8350_ufsphy_g4_tx,
878 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_tx),
879 		.rx		= sm8350_ufsphy_g4_rx,
880 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_rx),
881 		.pcs		= sm8350_ufsphy_g4_pcs,
882 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_g4_pcs),
883 	},
884 	.clk_list		= sm8450_ufs_phy_clk_l,
885 	.num_clks		= ARRAY_SIZE(sm8450_ufs_phy_clk_l),
886 	.vreg_list		= qmp_phy_vreg_l,
887 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
888 	.regs			= ufsphy_v5_regs_layout,
889 };
890 
891 static const struct qmp_phy_cfg sc8280xp_ufsphy_cfg = {
892 	.lanes			= 2,
893 
894 	.offsets		= &qmp_ufs_offsets,
895 
896 	.tbls = {
897 		.serdes		= sm8350_ufsphy_serdes,
898 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_serdes),
899 		.tx		= sm8350_ufsphy_tx,
900 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_tx),
901 		.rx		= sm8350_ufsphy_rx,
902 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_rx),
903 		.pcs		= sm8350_ufsphy_pcs,
904 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_pcs),
905 	},
906 	.tbls_hs_b = {
907 		.serdes		= sm8350_ufsphy_hs_b_serdes,
908 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_hs_b_serdes),
909 	},
910 	.tbls_hs_g4 = {
911 		.tx		= sm8350_ufsphy_g4_tx,
912 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_tx),
913 		.rx		= sm8350_ufsphy_g4_rx,
914 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_rx),
915 		.pcs		= sm8350_ufsphy_g4_pcs,
916 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_g4_pcs),
917 	},
918 	.clk_list		= sdm845_ufs_phy_clk_l,
919 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
920 	.vreg_list		= qmp_phy_vreg_l,
921 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
922 	.regs			= ufsphy_v5_regs_layout,
923 };
924 
925 static const struct qmp_phy_cfg sdm845_ufsphy_cfg = {
926 	.lanes			= 2,
927 
928 	.offsets		= &qmp_ufs_offsets,
929 
930 	.tbls = {
931 		.serdes		= sdm845_ufsphy_serdes,
932 		.serdes_num	= ARRAY_SIZE(sdm845_ufsphy_serdes),
933 		.tx		= sdm845_ufsphy_tx,
934 		.tx_num		= ARRAY_SIZE(sdm845_ufsphy_tx),
935 		.rx		= sdm845_ufsphy_rx,
936 		.rx_num		= ARRAY_SIZE(sdm845_ufsphy_rx),
937 		.pcs		= sdm845_ufsphy_pcs,
938 		.pcs_num	= ARRAY_SIZE(sdm845_ufsphy_pcs),
939 	},
940 	.tbls_hs_b = {
941 		.serdes		= sdm845_ufsphy_hs_b_serdes,
942 		.serdes_num	= ARRAY_SIZE(sdm845_ufsphy_hs_b_serdes),
943 	},
944 	.clk_list		= sdm845_ufs_phy_clk_l,
945 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
946 	.vreg_list		= qmp_phy_vreg_l,
947 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
948 	.regs			= ufsphy_v3_regs_layout,
949 
950 	.no_pcs_sw_reset	= true,
951 };
952 
953 static const struct qmp_phy_cfg sm6115_ufsphy_cfg = {
954 	.lanes			= 1,
955 
956 	.offsets		= &qmp_ufs_offsets,
957 
958 	.tbls = {
959 		.serdes		= sm6115_ufsphy_serdes,
960 		.serdes_num	= ARRAY_SIZE(sm6115_ufsphy_serdes),
961 		.tx		= sm6115_ufsphy_tx,
962 		.tx_num		= ARRAY_SIZE(sm6115_ufsphy_tx),
963 		.rx		= sm6115_ufsphy_rx,
964 		.rx_num		= ARRAY_SIZE(sm6115_ufsphy_rx),
965 		.pcs		= sm6115_ufsphy_pcs,
966 		.pcs_num	= ARRAY_SIZE(sm6115_ufsphy_pcs),
967 	},
968 	.tbls_hs_b = {
969 		.serdes		= sm6115_ufsphy_hs_b_serdes,
970 		.serdes_num	= ARRAY_SIZE(sm6115_ufsphy_hs_b_serdes),
971 	},
972 	.clk_list		= sdm845_ufs_phy_clk_l,
973 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
974 	.vreg_list		= qmp_phy_vreg_l,
975 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
976 	.regs			= ufsphy_v2_regs_layout,
977 
978 	.no_pcs_sw_reset	= true,
979 };
980 
981 static const struct qmp_phy_cfg sm7150_ufsphy_cfg = {
982 	.lanes			= 1,
983 
984 	.offsets		= &qmp_ufs_offsets,
985 
986 	.tbls = {
987 		.serdes		= sdm845_ufsphy_serdes,
988 		.serdes_num	= ARRAY_SIZE(sdm845_ufsphy_serdes),
989 		.tx		= sdm845_ufsphy_tx,
990 		.tx_num		= ARRAY_SIZE(sdm845_ufsphy_tx),
991 		.rx		= sm7150_ufsphy_rx,
992 		.rx_num		= ARRAY_SIZE(sm7150_ufsphy_rx),
993 		.pcs		= sm7150_ufsphy_pcs,
994 		.pcs_num	= ARRAY_SIZE(sm7150_ufsphy_pcs),
995 	},
996 	.tbls_hs_b = {
997 		.serdes		= sdm845_ufsphy_hs_b_serdes,
998 		.serdes_num	= ARRAY_SIZE(sdm845_ufsphy_hs_b_serdes),
999 	},
1000 	.clk_list		= sdm845_ufs_phy_clk_l,
1001 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1002 	.vreg_list		= qmp_phy_vreg_l,
1003 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1004 	.regs			= ufsphy_v3_regs_layout,
1005 
1006 	.no_pcs_sw_reset	= true,
1007 };
1008 
1009 static const struct qmp_phy_cfg sm8150_ufsphy_cfg = {
1010 	.lanes			= 2,
1011 
1012 	.tbls = {
1013 		.serdes		= sm8150_ufsphy_serdes,
1014 		.serdes_num	= ARRAY_SIZE(sm8150_ufsphy_serdes),
1015 		.tx		= sm8150_ufsphy_tx,
1016 		.tx_num		= ARRAY_SIZE(sm8150_ufsphy_tx),
1017 		.rx		= sm8150_ufsphy_rx,
1018 		.rx_num		= ARRAY_SIZE(sm8150_ufsphy_rx),
1019 		.pcs		= sm8150_ufsphy_pcs,
1020 		.pcs_num	= ARRAY_SIZE(sm8150_ufsphy_pcs),
1021 	},
1022 	.tbls_hs_b = {
1023 		.serdes		= sm8150_ufsphy_hs_b_serdes,
1024 		.serdes_num	= ARRAY_SIZE(sm8150_ufsphy_hs_b_serdes),
1025 	},
1026 	.tbls_hs_g4 = {
1027 		.tx		= sm8150_ufsphy_hs_g4_tx,
1028 		.tx_num		= ARRAY_SIZE(sm8150_ufsphy_hs_g4_tx),
1029 		.rx		= sm8150_ufsphy_hs_g4_rx,
1030 		.rx_num		= ARRAY_SIZE(sm8150_ufsphy_hs_g4_rx),
1031 		.pcs		= sm8150_ufsphy_hs_g4_pcs,
1032 		.pcs_num	= ARRAY_SIZE(sm8150_ufsphy_hs_g4_pcs),
1033 	},
1034 	.clk_list		= sdm845_ufs_phy_clk_l,
1035 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1036 	.vreg_list		= qmp_phy_vreg_l,
1037 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1038 	.regs			= ufsphy_v4_regs_layout,
1039 };
1040 
1041 static const struct qmp_phy_cfg sm8250_ufsphy_cfg = {
1042 	.lanes			= 2,
1043 
1044 	.offsets		= &qmp_ufs_offsets,
1045 
1046 	.tbls = {
1047 		.serdes		= sm8150_ufsphy_serdes,
1048 		.serdes_num	= ARRAY_SIZE(sm8150_ufsphy_serdes),
1049 		.tx		= sm8150_ufsphy_tx,
1050 		.tx_num		= ARRAY_SIZE(sm8150_ufsphy_tx),
1051 		.rx		= sm8150_ufsphy_rx,
1052 		.rx_num		= ARRAY_SIZE(sm8150_ufsphy_rx),
1053 		.pcs		= sm8150_ufsphy_pcs,
1054 		.pcs_num	= ARRAY_SIZE(sm8150_ufsphy_pcs),
1055 	},
1056 	.tbls_hs_b = {
1057 		.serdes		= sm8150_ufsphy_hs_b_serdes,
1058 		.serdes_num	= ARRAY_SIZE(sm8150_ufsphy_hs_b_serdes),
1059 	},
1060 	.tbls_hs_g4 = {
1061 		.tx		= sm8250_ufsphy_hs_g4_tx,
1062 		.tx_num		= ARRAY_SIZE(sm8250_ufsphy_hs_g4_tx),
1063 		.rx		= sm8250_ufsphy_hs_g4_rx,
1064 		.rx_num		= ARRAY_SIZE(sm8250_ufsphy_hs_g4_rx),
1065 		.pcs		= sm8150_ufsphy_hs_g4_pcs,
1066 		.pcs_num	= ARRAY_SIZE(sm8150_ufsphy_hs_g4_pcs),
1067 	},
1068 	.clk_list		= sdm845_ufs_phy_clk_l,
1069 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1070 	.vreg_list		= qmp_phy_vreg_l,
1071 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1072 	.regs			= ufsphy_v4_regs_layout,
1073 };
1074 
1075 static const struct qmp_phy_cfg sm8350_ufsphy_cfg = {
1076 	.lanes			= 2,
1077 
1078 	.offsets		= &qmp_ufs_offsets,
1079 
1080 	.tbls = {
1081 		.serdes		= sm8350_ufsphy_serdes,
1082 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_serdes),
1083 		.tx		= sm8350_ufsphy_tx,
1084 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_tx),
1085 		.rx		= sm8350_ufsphy_rx,
1086 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_rx),
1087 		.pcs		= sm8350_ufsphy_pcs,
1088 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_pcs),
1089 	},
1090 	.tbls_hs_b = {
1091 		.serdes		= sm8350_ufsphy_hs_b_serdes,
1092 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_hs_b_serdes),
1093 	},
1094 	.tbls_hs_g4 = {
1095 		.tx		= sm8350_ufsphy_g4_tx,
1096 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_tx),
1097 		.rx		= sm8350_ufsphy_g4_rx,
1098 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_rx),
1099 		.pcs		= sm8350_ufsphy_g4_pcs,
1100 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_g4_pcs),
1101 	},
1102 	.clk_list		= sdm845_ufs_phy_clk_l,
1103 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1104 	.vreg_list		= qmp_phy_vreg_l,
1105 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1106 	.regs			= ufsphy_v5_regs_layout,
1107 };
1108 
1109 static const struct qmp_phy_cfg sm8450_ufsphy_cfg = {
1110 	.lanes			= 2,
1111 
1112 	.offsets		= &qmp_ufs_offsets,
1113 
1114 	.tbls = {
1115 		.serdes		= sm8350_ufsphy_serdes,
1116 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_serdes),
1117 		.tx		= sm8350_ufsphy_tx,
1118 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_tx),
1119 		.rx		= sm8350_ufsphy_rx,
1120 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_rx),
1121 		.pcs		= sm8350_ufsphy_pcs,
1122 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_pcs),
1123 	},
1124 	.tbls_hs_b = {
1125 		.serdes		= sm8350_ufsphy_hs_b_serdes,
1126 		.serdes_num	= ARRAY_SIZE(sm8350_ufsphy_hs_b_serdes),
1127 	},
1128 	.tbls_hs_g4 = {
1129 		.tx		= sm8350_ufsphy_g4_tx,
1130 		.tx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_tx),
1131 		.rx		= sm8350_ufsphy_g4_rx,
1132 		.rx_num		= ARRAY_SIZE(sm8350_ufsphy_g4_rx),
1133 		.pcs		= sm8350_ufsphy_g4_pcs,
1134 		.pcs_num	= ARRAY_SIZE(sm8350_ufsphy_g4_pcs),
1135 	},
1136 	.clk_list		= sm8450_ufs_phy_clk_l,
1137 	.num_clks		= ARRAY_SIZE(sm8450_ufs_phy_clk_l),
1138 	.vreg_list		= qmp_phy_vreg_l,
1139 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1140 	.regs			= ufsphy_v5_regs_layout,
1141 };
1142 
1143 static const struct qmp_phy_cfg sm8550_ufsphy_cfg = {
1144 	.lanes			= 2,
1145 
1146 	.offsets		= &qmp_ufs_offsets_v6,
1147 
1148 	.tbls = {
1149 		.serdes		= sm8550_ufsphy_serdes,
1150 		.serdes_num	= ARRAY_SIZE(sm8550_ufsphy_serdes),
1151 		.tx		= sm8550_ufsphy_tx,
1152 		.tx_num		= ARRAY_SIZE(sm8550_ufsphy_tx),
1153 		.rx		= sm8550_ufsphy_rx,
1154 		.rx_num		= ARRAY_SIZE(sm8550_ufsphy_rx),
1155 		.pcs		= sm8550_ufsphy_pcs,
1156 		.pcs_num	= ARRAY_SIZE(sm8550_ufsphy_pcs),
1157 	},
1158 	.clk_list		= sdm845_ufs_phy_clk_l,
1159 	.num_clks		= ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1160 	.vreg_list		= qmp_phy_vreg_l,
1161 	.num_vregs		= ARRAY_SIZE(qmp_phy_vreg_l),
1162 	.regs			= ufsphy_v6_regs_layout,
1163 };
1164 
1165 static void qmp_ufs_configure_lane(void __iomem *base,
1166 					const struct qmp_phy_init_tbl tbl[],
1167 					int num,
1168 					u8 lane_mask)
1169 {
1170 	int i;
1171 	const struct qmp_phy_init_tbl *t = tbl;
1172 
1173 	if (!t)
1174 		return;
1175 
1176 	for (i = 0; i < num; i++, t++) {
1177 		if (!(t->lane_mask & lane_mask))
1178 			continue;
1179 
1180 		writel(t->val, base + t->offset);
1181 	}
1182 }
1183 
1184 static void qmp_ufs_configure(void __iomem *base,
1185 				   const struct qmp_phy_init_tbl tbl[],
1186 				   int num)
1187 {
1188 	qmp_ufs_configure_lane(base, tbl, num, 0xff);
1189 }
1190 
1191 static void qmp_ufs_serdes_init(struct qmp_ufs *qmp, const struct qmp_phy_cfg_tbls *tbls)
1192 {
1193 	void __iomem *serdes = qmp->serdes;
1194 
1195 	qmp_ufs_configure(serdes, tbls->serdes, tbls->serdes_num);
1196 }
1197 
1198 static void qmp_ufs_lanes_init(struct qmp_ufs *qmp, const struct qmp_phy_cfg_tbls *tbls)
1199 {
1200 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1201 	void __iomem *tx = qmp->tx;
1202 	void __iomem *rx = qmp->rx;
1203 
1204 	qmp_ufs_configure_lane(tx, tbls->tx, tbls->tx_num, 1);
1205 	qmp_ufs_configure_lane(rx, tbls->rx, tbls->rx_num, 1);
1206 
1207 	if (cfg->lanes >= 2) {
1208 		qmp_ufs_configure_lane(qmp->tx2, tbls->tx, tbls->tx_num, 2);
1209 		qmp_ufs_configure_lane(qmp->rx2, tbls->rx, tbls->rx_num, 2);
1210 	}
1211 }
1212 
1213 static void qmp_ufs_pcs_init(struct qmp_ufs *qmp, const struct qmp_phy_cfg_tbls *tbls)
1214 {
1215 	void __iomem *pcs = qmp->pcs;
1216 
1217 	qmp_ufs_configure(pcs, tbls->pcs, tbls->pcs_num);
1218 }
1219 
1220 static void qmp_ufs_init_registers(struct qmp_ufs *qmp, const struct qmp_phy_cfg *cfg)
1221 {
1222 	qmp_ufs_serdes_init(qmp, &cfg->tbls);
1223 	if (qmp->mode == PHY_MODE_UFS_HS_B)
1224 		qmp_ufs_serdes_init(qmp, &cfg->tbls_hs_b);
1225 	qmp_ufs_lanes_init(qmp, &cfg->tbls);
1226 	if (qmp->submode == UFS_HS_G4)
1227 		qmp_ufs_lanes_init(qmp, &cfg->tbls_hs_g4);
1228 	qmp_ufs_pcs_init(qmp, &cfg->tbls);
1229 	if (qmp->submode == UFS_HS_G4)
1230 		qmp_ufs_pcs_init(qmp, &cfg->tbls_hs_g4);
1231 }
1232 
1233 static int qmp_ufs_com_init(struct qmp_ufs *qmp)
1234 {
1235 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1236 	void __iomem *pcs = qmp->pcs;
1237 	int ret;
1238 
1239 	ret = regulator_bulk_enable(cfg->num_vregs, qmp->vregs);
1240 	if (ret) {
1241 		dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
1242 		return ret;
1243 	}
1244 
1245 	ret = clk_bulk_prepare_enable(cfg->num_clks, qmp->clks);
1246 	if (ret)
1247 		goto err_disable_regulators;
1248 
1249 	qphy_setbits(pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL], SW_PWRDN);
1250 
1251 	return 0;
1252 
1253 err_disable_regulators:
1254 	regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
1255 
1256 	return ret;
1257 }
1258 
1259 static int qmp_ufs_com_exit(struct qmp_ufs *qmp)
1260 {
1261 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1262 
1263 	reset_control_assert(qmp->ufs_reset);
1264 
1265 	clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
1266 
1267 	regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
1268 
1269 	return 0;
1270 }
1271 
1272 static int qmp_ufs_init(struct phy *phy)
1273 {
1274 	struct qmp_ufs *qmp = phy_get_drvdata(phy);
1275 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1276 	int ret;
1277 	dev_vdbg(qmp->dev, "Initializing QMP phy\n");
1278 
1279 	if (cfg->no_pcs_sw_reset) {
1280 		/*
1281 		 * Get UFS reset, which is delayed until now to avoid a
1282 		 * circular dependency where UFS needs its PHY, but the PHY
1283 		 * needs this UFS reset.
1284 		 */
1285 		if (!qmp->ufs_reset) {
1286 			qmp->ufs_reset =
1287 				devm_reset_control_get_exclusive(qmp->dev,
1288 								 "ufsphy");
1289 
1290 			if (IS_ERR(qmp->ufs_reset)) {
1291 				ret = PTR_ERR(qmp->ufs_reset);
1292 				dev_err(qmp->dev,
1293 					"failed to get UFS reset: %d\n",
1294 					ret);
1295 
1296 				qmp->ufs_reset = NULL;
1297 				return ret;
1298 			}
1299 		}
1300 
1301 		ret = reset_control_assert(qmp->ufs_reset);
1302 		if (ret)
1303 			return ret;
1304 	}
1305 
1306 	ret = qmp_ufs_com_init(qmp);
1307 	if (ret)
1308 		return ret;
1309 
1310 	return 0;
1311 }
1312 
1313 static int qmp_ufs_power_on(struct phy *phy)
1314 {
1315 	struct qmp_ufs *qmp = phy_get_drvdata(phy);
1316 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1317 	void __iomem *pcs = qmp->pcs;
1318 	void __iomem *status;
1319 	unsigned int val;
1320 	int ret;
1321 
1322 	qmp_ufs_init_registers(qmp, cfg);
1323 
1324 	ret = reset_control_deassert(qmp->ufs_reset);
1325 	if (ret)
1326 		return ret;
1327 
1328 	/* Pull PHY out of reset state */
1329 	if (!cfg->no_pcs_sw_reset)
1330 		qphy_clrbits(pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
1331 
1332 	/* start SerDes */
1333 	qphy_setbits(pcs, cfg->regs[QPHY_START_CTRL], SERDES_START);
1334 
1335 	status = pcs + cfg->regs[QPHY_PCS_READY_STATUS];
1336 	ret = readl_poll_timeout(status, val, (val & PCS_READY), 200,
1337 				 PHY_INIT_COMPLETE_TIMEOUT);
1338 	if (ret) {
1339 		dev_err(qmp->dev, "phy initialization timed-out\n");
1340 		return ret;
1341 	}
1342 
1343 	return 0;
1344 }
1345 
1346 static int qmp_ufs_power_off(struct phy *phy)
1347 {
1348 	struct qmp_ufs *qmp = phy_get_drvdata(phy);
1349 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1350 
1351 	/* PHY reset */
1352 	if (!cfg->no_pcs_sw_reset)
1353 		qphy_setbits(qmp->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
1354 
1355 	/* stop SerDes */
1356 	qphy_clrbits(qmp->pcs, cfg->regs[QPHY_START_CTRL], SERDES_START);
1357 
1358 	/* Put PHY into POWER DOWN state: active low */
1359 	qphy_clrbits(qmp->pcs, cfg->regs[QPHY_PCS_POWER_DOWN_CONTROL],
1360 			SW_PWRDN);
1361 
1362 	return 0;
1363 }
1364 
1365 static int qmp_ufs_exit(struct phy *phy)
1366 {
1367 	struct qmp_ufs *qmp = phy_get_drvdata(phy);
1368 
1369 	qmp_ufs_com_exit(qmp);
1370 
1371 	return 0;
1372 }
1373 
1374 static int qmp_ufs_enable(struct phy *phy)
1375 {
1376 	int ret;
1377 
1378 	ret = qmp_ufs_init(phy);
1379 	if (ret)
1380 		return ret;
1381 
1382 	ret = qmp_ufs_power_on(phy);
1383 	if (ret)
1384 		qmp_ufs_exit(phy);
1385 
1386 	return ret;
1387 }
1388 
1389 static int qmp_ufs_disable(struct phy *phy)
1390 {
1391 	int ret;
1392 
1393 	ret = qmp_ufs_power_off(phy);
1394 	if (ret)
1395 		return ret;
1396 	return qmp_ufs_exit(phy);
1397 }
1398 
1399 static int qmp_ufs_set_mode(struct phy *phy, enum phy_mode mode, int submode)
1400 {
1401 	struct qmp_ufs *qmp = phy_get_drvdata(phy);
1402 
1403 	qmp->mode = mode;
1404 	qmp->submode = submode;
1405 
1406 	return 0;
1407 }
1408 
1409 static const struct phy_ops qcom_qmp_ufs_phy_ops = {
1410 	.power_on	= qmp_ufs_enable,
1411 	.power_off	= qmp_ufs_disable,
1412 	.set_mode	= qmp_ufs_set_mode,
1413 	.owner		= THIS_MODULE,
1414 };
1415 
1416 static int qmp_ufs_vreg_init(struct qmp_ufs *qmp)
1417 {
1418 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1419 	struct device *dev = qmp->dev;
1420 	int num = cfg->num_vregs;
1421 	int i;
1422 
1423 	qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
1424 	if (!qmp->vregs)
1425 		return -ENOMEM;
1426 
1427 	for (i = 0; i < num; i++)
1428 		qmp->vregs[i].supply = cfg->vreg_list[i];
1429 
1430 	return devm_regulator_bulk_get(dev, num, qmp->vregs);
1431 }
1432 
1433 static int qmp_ufs_clk_init(struct qmp_ufs *qmp)
1434 {
1435 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1436 	struct device *dev = qmp->dev;
1437 	int num = cfg->num_clks;
1438 	int i;
1439 
1440 	qmp->clks = devm_kcalloc(dev, num, sizeof(*qmp->clks), GFP_KERNEL);
1441 	if (!qmp->clks)
1442 		return -ENOMEM;
1443 
1444 	for (i = 0; i < num; i++)
1445 		qmp->clks[i].id = cfg->clk_list[i];
1446 
1447 	return devm_clk_bulk_get(dev, num, qmp->clks);
1448 }
1449 
1450 static void qmp_ufs_clk_release_provider(void *res)
1451 {
1452 	of_clk_del_provider(res);
1453 }
1454 
1455 #define UFS_SYMBOL_CLOCKS 3
1456 
1457 static int qmp_ufs_register_clocks(struct qmp_ufs *qmp, struct device_node *np)
1458 {
1459 	struct clk_hw_onecell_data *clk_data;
1460 	struct clk_hw *hw;
1461 	char name[64];
1462 	int ret;
1463 
1464 	clk_data = devm_kzalloc(qmp->dev,
1465 				struct_size(clk_data, hws, UFS_SYMBOL_CLOCKS),
1466 				GFP_KERNEL);
1467 	if (!clk_data)
1468 		return -ENOMEM;
1469 
1470 	clk_data->num = UFS_SYMBOL_CLOCKS;
1471 
1472 	snprintf(name, sizeof(name), "%s::rx_symbol_0", dev_name(qmp->dev));
1473 	hw = devm_clk_hw_register_fixed_rate(qmp->dev, name, NULL, 0, 0);
1474 	if (IS_ERR(hw))
1475 		return PTR_ERR(hw);
1476 
1477 	clk_data->hws[0] = hw;
1478 
1479 	snprintf(name, sizeof(name), "%s::rx_symbol_1", dev_name(qmp->dev));
1480 	hw = devm_clk_hw_register_fixed_rate(qmp->dev, name, NULL, 0, 0);
1481 	if (IS_ERR(hw))
1482 		return PTR_ERR(hw);
1483 
1484 	clk_data->hws[1] = hw;
1485 
1486 	snprintf(name, sizeof(name), "%s::tx_symbol_0", dev_name(qmp->dev));
1487 	hw = devm_clk_hw_register_fixed_rate(qmp->dev, name, NULL, 0, 0);
1488 	if (IS_ERR(hw))
1489 		return PTR_ERR(hw);
1490 
1491 	clk_data->hws[2] = hw;
1492 
1493 	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
1494 	if (ret)
1495 		return ret;
1496 
1497 	/*
1498 	 * Roll a devm action because the clock provider can be a child node.
1499 	 */
1500 	return devm_add_action_or_reset(qmp->dev, qmp_ufs_clk_release_provider, np);
1501 }
1502 
1503 static int qmp_ufs_parse_dt_legacy(struct qmp_ufs *qmp, struct device_node *np)
1504 {
1505 	struct platform_device *pdev = to_platform_device(qmp->dev);
1506 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1507 	struct device *dev = qmp->dev;
1508 
1509 	qmp->serdes = devm_platform_ioremap_resource(pdev, 0);
1510 	if (IS_ERR(qmp->serdes))
1511 		return PTR_ERR(qmp->serdes);
1512 
1513 	/*
1514 	 * Get memory resources for the PHY:
1515 	 * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
1516 	 * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5
1517 	 * For single lane PHYs: pcs_misc (optional) -> 3.
1518 	 */
1519 	qmp->tx = devm_of_iomap(dev, np, 0, NULL);
1520 	if (IS_ERR(qmp->tx))
1521 		return PTR_ERR(qmp->tx);
1522 
1523 	qmp->rx = devm_of_iomap(dev, np, 1, NULL);
1524 	if (IS_ERR(qmp->rx))
1525 		return PTR_ERR(qmp->rx);
1526 
1527 	qmp->pcs = devm_of_iomap(dev, np, 2, NULL);
1528 	if (IS_ERR(qmp->pcs))
1529 		return PTR_ERR(qmp->pcs);
1530 
1531 	if (cfg->lanes >= 2) {
1532 		qmp->tx2 = devm_of_iomap(dev, np, 3, NULL);
1533 		if (IS_ERR(qmp->tx2))
1534 			return PTR_ERR(qmp->tx2);
1535 
1536 		qmp->rx2 = devm_of_iomap(dev, np, 4, NULL);
1537 		if (IS_ERR(qmp->rx2))
1538 			return PTR_ERR(qmp->rx2);
1539 
1540 		qmp->pcs_misc = devm_of_iomap(dev, np, 5, NULL);
1541 	} else {
1542 		qmp->pcs_misc = devm_of_iomap(dev, np, 3, NULL);
1543 	}
1544 
1545 	if (IS_ERR(qmp->pcs_misc))
1546 		dev_vdbg(dev, "PHY pcs_misc-reg not used\n");
1547 
1548 	return 0;
1549 }
1550 
1551 static int qmp_ufs_parse_dt(struct qmp_ufs *qmp)
1552 {
1553 	struct platform_device *pdev = to_platform_device(qmp->dev);
1554 	const struct qmp_phy_cfg *cfg = qmp->cfg;
1555 	const struct qmp_ufs_offsets *offs = cfg->offsets;
1556 	void __iomem *base;
1557 
1558 	if (!offs)
1559 		return -EINVAL;
1560 
1561 	base = devm_platform_ioremap_resource(pdev, 0);
1562 	if (IS_ERR(base))
1563 		return PTR_ERR(base);
1564 
1565 	qmp->serdes = base + offs->serdes;
1566 	qmp->pcs = base + offs->pcs;
1567 	qmp->tx = base + offs->tx;
1568 	qmp->rx = base + offs->rx;
1569 
1570 	if (cfg->lanes >= 2) {
1571 		qmp->tx2 = base + offs->tx2;
1572 		qmp->rx2 = base + offs->rx2;
1573 	}
1574 
1575 	return 0;
1576 }
1577 
1578 static int qmp_ufs_probe(struct platform_device *pdev)
1579 {
1580 	struct device *dev = &pdev->dev;
1581 	struct phy_provider *phy_provider;
1582 	struct device_node *np;
1583 	struct qmp_ufs *qmp;
1584 	int ret;
1585 
1586 	qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
1587 	if (!qmp)
1588 		return -ENOMEM;
1589 
1590 	qmp->dev = dev;
1591 
1592 	qmp->cfg = of_device_get_match_data(dev);
1593 	if (!qmp->cfg)
1594 		return -EINVAL;
1595 
1596 	ret = qmp_ufs_clk_init(qmp);
1597 	if (ret)
1598 		return ret;
1599 
1600 	ret = qmp_ufs_vreg_init(qmp);
1601 	if (ret)
1602 		return ret;
1603 
1604 	/* Check for legacy binding with child node. */
1605 	np = of_get_next_available_child(dev->of_node, NULL);
1606 	if (np) {
1607 		ret = qmp_ufs_parse_dt_legacy(qmp, np);
1608 	} else {
1609 		np = of_node_get(dev->of_node);
1610 		ret = qmp_ufs_parse_dt(qmp);
1611 	}
1612 	if (ret)
1613 		goto err_node_put;
1614 
1615 	ret = qmp_ufs_register_clocks(qmp, np);
1616 	if (ret)
1617 		goto err_node_put;
1618 
1619 	qmp->phy = devm_phy_create(dev, np, &qcom_qmp_ufs_phy_ops);
1620 	if (IS_ERR(qmp->phy)) {
1621 		ret = PTR_ERR(qmp->phy);
1622 		dev_err(dev, "failed to create PHY: %d\n", ret);
1623 		goto err_node_put;
1624 	}
1625 
1626 	phy_set_drvdata(qmp->phy, qmp);
1627 
1628 	of_node_put(np);
1629 
1630 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1631 
1632 	return PTR_ERR_OR_ZERO(phy_provider);
1633 
1634 err_node_put:
1635 	of_node_put(np);
1636 	return ret;
1637 }
1638 
1639 static const struct of_device_id qmp_ufs_of_match_table[] = {
1640 	{
1641 		.compatible = "qcom,msm8996-qmp-ufs-phy",
1642 		.data = &msm8996_ufsphy_cfg,
1643 	}, {
1644 		.compatible = "qcom,msm8998-qmp-ufs-phy",
1645 		.data = &sdm845_ufsphy_cfg,
1646 	}, {
1647 		.compatible = "qcom,sa8775p-qmp-ufs-phy",
1648 		.data = &sa8775p_ufsphy_cfg,
1649 	}, {
1650 		.compatible = "qcom,sc8180x-qmp-ufs-phy",
1651 		.data = &sm8150_ufsphy_cfg,
1652 	}, {
1653 		.compatible = "qcom,sc8280xp-qmp-ufs-phy",
1654 		.data = &sc8280xp_ufsphy_cfg,
1655 	}, {
1656 		.compatible = "qcom,sdm845-qmp-ufs-phy",
1657 		.data = &sdm845_ufsphy_cfg,
1658 	}, {
1659 		.compatible = "qcom,sm6115-qmp-ufs-phy",
1660 		.data = &sm6115_ufsphy_cfg,
1661 	}, {
1662 		.compatible = "qcom,sm6125-qmp-ufs-phy",
1663 		.data = &sm6115_ufsphy_cfg,
1664 	}, {
1665 		.compatible = "qcom,sm6350-qmp-ufs-phy",
1666 		.data = &sdm845_ufsphy_cfg,
1667 	}, {
1668 		.compatible = "qcom,sm7150-qmp-ufs-phy",
1669 		.data = &sm7150_ufsphy_cfg,
1670 	}, {
1671 		.compatible = "qcom,sm8150-qmp-ufs-phy",
1672 		.data = &sm8150_ufsphy_cfg,
1673 	}, {
1674 		.compatible = "qcom,sm8250-qmp-ufs-phy",
1675 		.data = &sm8250_ufsphy_cfg,
1676 	}, {
1677 		.compatible = "qcom,sm8350-qmp-ufs-phy",
1678 		.data = &sm8350_ufsphy_cfg,
1679 	}, {
1680 		.compatible = "qcom,sm8450-qmp-ufs-phy",
1681 		.data = &sm8450_ufsphy_cfg,
1682 	}, {
1683 		.compatible = "qcom,sm8550-qmp-ufs-phy",
1684 		.data = &sm8550_ufsphy_cfg,
1685 	},
1686 	{ },
1687 };
1688 MODULE_DEVICE_TABLE(of, qmp_ufs_of_match_table);
1689 
1690 static struct platform_driver qmp_ufs_driver = {
1691 	.probe		= qmp_ufs_probe,
1692 	.driver = {
1693 		.name	= "qcom-qmp-ufs-phy",
1694 		.of_match_table = qmp_ufs_of_match_table,
1695 	},
1696 };
1697 
1698 module_platform_driver(qmp_ufs_driver);
1699 
1700 MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
1701 MODULE_DESCRIPTION("Qualcomm QMP UFS PHY driver");
1702 MODULE_LICENSE("GPL v2");
1703