ubifs: Fix memory leak from c->sup_node
[linux-2.6-microblaze.git] / drivers / phy / qualcomm / phy-qcom-qmp.c
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_device.h>
16 #include <linux/of_address.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22
23 #include <dt-bindings/phy/phy.h>
24
25 #include "phy-qcom-qmp.h"
26
27 /* QPHY_SW_RESET bit */
28 #define SW_RESET                                BIT(0)
29 /* QPHY_POWER_DOWN_CONTROL */
30 #define SW_PWRDN                                BIT(0)
31 #define REFCLK_DRV_DSBL                         BIT(1)
32 /* QPHY_START_CONTROL bits */
33 #define SERDES_START                            BIT(0)
34 #define PCS_START                               BIT(1)
35 #define PLL_READY_GATE_EN                       BIT(3)
36 /* QPHY_PCS_STATUS bit */
37 #define PHYSTATUS                               BIT(6)
38 /* QPHY_PCS_READY_STATUS & QPHY_COM_PCS_READY_STATUS bit */
39 #define PCS_READY                               BIT(0)
40
41 /* QPHY_V3_DP_COM_RESET_OVRD_CTRL register bits */
42 /* DP PHY soft reset */
43 #define SW_DPPHY_RESET                          BIT(0)
44 /* mux to select DP PHY reset control, 0:HW control, 1: software reset */
45 #define SW_DPPHY_RESET_MUX                      BIT(1)
46 /* USB3 PHY soft reset */
47 #define SW_USB3PHY_RESET                        BIT(2)
48 /* mux to select USB3 PHY reset control, 0:HW control, 1: software reset */
49 #define SW_USB3PHY_RESET_MUX                    BIT(3)
50
51 /* QPHY_V3_DP_COM_PHY_MODE_CTRL register bits */
52 #define USB3_MODE                               BIT(0) /* enables USB3 mode */
53 #define DP_MODE                                 BIT(1) /* enables DP mode */
54
55 /* QPHY_PCS_AUTONOMOUS_MODE_CTRL register bits */
56 #define ARCVR_DTCT_EN                           BIT(0)
57 #define ALFPS_DTCT_EN                           BIT(1)
58 #define ARCVR_DTCT_EVENT_SEL                    BIT(4)
59
60 /* QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR register bits */
61 #define IRQ_CLEAR                               BIT(0)
62
63 /* QPHY_PCS_LFPS_RXTERM_IRQ_STATUS register bits */
64 #define RCVR_DETECT                             BIT(0)
65
66 /* QPHY_V3_PCS_MISC_CLAMP_ENABLE register bits */
67 #define CLAMP_EN                                BIT(0) /* enables i/o clamp_n */
68
69 #define PHY_INIT_COMPLETE_TIMEOUT               10000
70 #define POWER_DOWN_DELAY_US_MIN                 10
71 #define POWER_DOWN_DELAY_US_MAX                 11
72
73 #define MAX_PROP_NAME                           32
74
75 /* Define the assumed distance between lanes for underspecified device trees. */
76 #define QMP_PHY_LEGACY_LANE_STRIDE              0x400
77
78 struct qmp_phy_init_tbl {
79         unsigned int offset;
80         unsigned int val;
81         /*
82          * register part of layout ?
83          * if yes, then offset gives index in the reg-layout
84          */
85         int in_layout;
86 };
87
88 #define QMP_PHY_INIT_CFG(o, v)          \
89         {                               \
90                 .offset = o,            \
91                 .val = v,               \
92         }
93
94 #define QMP_PHY_INIT_CFG_L(o, v)        \
95         {                               \
96                 .offset = o,            \
97                 .val = v,               \
98                 .in_layout = 1,         \
99         }
100
101 /* set of registers with offsets different per-PHY */
102 enum qphy_reg_layout {
103         /* Common block control registers */
104         QPHY_COM_SW_RESET,
105         QPHY_COM_POWER_DOWN_CONTROL,
106         QPHY_COM_START_CONTROL,
107         QPHY_COM_PCS_READY_STATUS,
108         /* PCS registers */
109         QPHY_PLL_LOCK_CHK_DLY_TIME,
110         QPHY_FLL_CNTRL1,
111         QPHY_FLL_CNTRL2,
112         QPHY_FLL_CNT_VAL_L,
113         QPHY_FLL_CNT_VAL_H_TOL,
114         QPHY_FLL_MAN_CODE,
115         QPHY_SW_RESET,
116         QPHY_START_CTRL,
117         QPHY_PCS_READY_STATUS,
118         QPHY_PCS_STATUS,
119         QPHY_PCS_AUTONOMOUS_MODE_CTRL,
120         QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR,
121         QPHY_PCS_LFPS_RXTERM_IRQ_STATUS,
122 };
123
124 static const unsigned int pciephy_regs_layout[] = {
125         [QPHY_COM_SW_RESET]             = 0x400,
126         [QPHY_COM_POWER_DOWN_CONTROL]   = 0x404,
127         [QPHY_COM_START_CONTROL]        = 0x408,
128         [QPHY_COM_PCS_READY_STATUS]     = 0x448,
129         [QPHY_PLL_LOCK_CHK_DLY_TIME]    = 0xa8,
130         [QPHY_FLL_CNTRL1]               = 0xc4,
131         [QPHY_FLL_CNTRL2]               = 0xc8,
132         [QPHY_FLL_CNT_VAL_L]            = 0xcc,
133         [QPHY_FLL_CNT_VAL_H_TOL]        = 0xd0,
134         [QPHY_FLL_MAN_CODE]             = 0xd4,
135         [QPHY_SW_RESET]                 = 0x00,
136         [QPHY_START_CTRL]               = 0x08,
137         [QPHY_PCS_STATUS]               = 0x174,
138 };
139
140 static const unsigned int usb3phy_regs_layout[] = {
141         [QPHY_FLL_CNTRL1]               = 0xc0,
142         [QPHY_FLL_CNTRL2]               = 0xc4,
143         [QPHY_FLL_CNT_VAL_L]            = 0xc8,
144         [QPHY_FLL_CNT_VAL_H_TOL]        = 0xcc,
145         [QPHY_FLL_MAN_CODE]             = 0xd0,
146         [QPHY_SW_RESET]                 = 0x00,
147         [QPHY_START_CTRL]               = 0x08,
148         [QPHY_PCS_STATUS]               = 0x17c,
149         [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = 0x0d4,
150         [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR]  = 0x0d8,
151         [QPHY_PCS_LFPS_RXTERM_IRQ_STATUS] = 0x178,
152 };
153
154 static const unsigned int qmp_v3_usb3phy_regs_layout[] = {
155         [QPHY_SW_RESET]                 = 0x00,
156         [QPHY_START_CTRL]               = 0x08,
157         [QPHY_PCS_STATUS]               = 0x174,
158         [QPHY_PCS_AUTONOMOUS_MODE_CTRL] = 0x0d8,
159         [QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR]  = 0x0dc,
160         [QPHY_PCS_LFPS_RXTERM_IRQ_STATUS] = 0x170,
161 };
162
163 static const unsigned int sdm845_ufsphy_regs_layout[] = {
164         [QPHY_START_CTRL]               = 0x00,
165         [QPHY_PCS_READY_STATUS]         = 0x160,
166 };
167
168 static const unsigned int sm8150_ufsphy_regs_layout[] = {
169         [QPHY_START_CTRL]               = 0x00,
170         [QPHY_PCS_READY_STATUS]         = 0x180,
171 };
172
173 static const struct qmp_phy_init_tbl msm8996_pcie_serdes_tbl[] = {
174         QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x1c),
175         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
176         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
177         QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
178         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x42),
179         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
180         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0xff),
181         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x1f),
182         QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x01),
183         QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
184         QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
185         QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0x0a),
186         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x09),
187         QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
188         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
189         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
190         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
191         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
192         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x1a),
193         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x0a),
194         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
195         QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x02),
196         QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
197         QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x04),
198         QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
199         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
200         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
201         QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
202         QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
203         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
204         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
205         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
206         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x02),
207         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
208         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
209         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
210         QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x15),
211         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
212         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
213         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
214         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
215         QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
216         QMP_PHY_INIT_CFG(QSERDES_COM_RESCODE_DIV_NUM, 0x40),
217 };
218
219 static const struct qmp_phy_init_tbl msm8996_pcie_tx_tbl[] = {
220         QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
221         QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
222 };
223
224 static const struct qmp_phy_init_tbl msm8996_pcie_rx_tbl[] = {
225         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
226         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x01),
227         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x00),
228         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
229         QMP_PHY_INIT_CFG(QSERDES_RX_RX_BAND, 0x18),
230         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
231         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x04),
232         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
233         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
234         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x19),
235 };
236
237 static const struct qmp_phy_init_tbl msm8996_pcie_pcs_tbl[] = {
238         QMP_PHY_INIT_CFG(QPHY_RX_IDLE_DTCT_CNTRL, 0x4c),
239         QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x00),
240         QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x01),
241
242         QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x05),
243
244         QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x05),
245         QMP_PHY_INIT_CFG(QPHY_POWER_DOWN_CONTROL, 0x02),
246         QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG4, 0x00),
247         QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG1, 0xa3),
248         QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0x0e),
249 };
250
251 static const struct qmp_phy_init_tbl msm8998_pcie_serdes_tbl[] = {
252         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x14),
253         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
254         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x0f),
255         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
256         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x01),
257         QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL, 0x20),
258         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
259         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
260         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
261         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_TIMER1, 0xff),
262         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_TIMER2, 0x3f),
263         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
264         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
265         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
266         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_EP_DIV, 0x19),
267         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_ENABLE1, 0x90),
268         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
269         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x03),
270         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0x55),
271         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0x55),
272         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
273         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x0d),
274         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x04),
275         QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x00),
276         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x08),
277         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
278         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x34),
279         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
280         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x33),
281         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
282         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_BUF_ENABLE, 0x07),
283         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x04),
284         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
285         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
286         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BG_TIMER, 0x09),
287         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
288         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x40),
289         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
290         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x02),
291         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
292         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x7e),
293         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x15),
294 };
295
296 static const struct qmp_phy_init_tbl msm8998_pcie_tx_tbl[] = {
297         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x02),
298         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
299         QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
300         QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x06),
301 };
302
303 static const struct qmp_phy_init_tbl msm8998_pcie_rx_tbl[] = {
304         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
305         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_ENABLES, 0x1c),
306         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
307         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0a),
308         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
309         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1a),
310         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
311         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN, 0x04),
312         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN_HALF, 0x04),
313         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x00),
314         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
315         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_INTERFACE_MODE, 0x40),
316         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x71),
317         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x40),
318 };
319
320 static const struct qmp_phy_init_tbl msm8998_pcie_pcs_tbl[] = {
321         QMP_PHY_INIT_CFG(QPHY_V3_PCS_ENDPOINT_REFCLK_DRIVE, 0x04),
322         QMP_PHY_INIT_CFG(QPHY_V3_PCS_OSC_DTCT_ACTIONS, 0x00),
323         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x01),
324         QMP_PHY_INIT_CFG(QPHY_V3_PCS_L1SS_WAKEUP_DLY_TIME_AUXCLK_MSB, 0x00),
325         QMP_PHY_INIT_CFG(QPHY_V3_PCS_L1SS_WAKEUP_DLY_TIME_AUXCLK_LSB, 0x20),
326         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LP_WAKEUP_DLY_TIME_AUXCLK_MSB, 0x00),
327         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LP_WAKEUP_DLY_TIME_AUXCLK, 0x01),
328         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PLL_LOCK_CHK_DLY_TIME, 0x73),
329         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0x99),
330         QMP_PHY_INIT_CFG(QPHY_V3_PCS_SIGDET_CNTRL, 0x03),
331 };
332
333 static const struct qmp_phy_init_tbl msm8996_usb3_serdes_tbl[] = {
334         QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0x14),
335         QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
336         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x30),
337         QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x06),
338         QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x01),
339         QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x00),
340         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0x0f),
341         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0x0f),
342         QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x04),
343         /* PLL and Loop filter settings */
344         QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
345         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
346         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
347         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x03),
348         QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0x0b),
349         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
350         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
351         QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
352         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0x00),
353         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0x15),
354         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0x34),
355         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x00),
356         QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x00),
357         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_CFG, 0x00),
358         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x00),
359         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0x0a),
360         /* SSC settings */
361         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x01),
362         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
363         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x01),
364         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x00),
365         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x00),
366         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0xde),
367         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x07),
368 };
369
370 static const struct qmp_phy_init_tbl msm8996_usb3_tx_tbl[] = {
371         QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
372         QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
373         QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x06),
374 };
375
376 static const struct qmp_phy_init_tbl msm8996_usb3_rx_tbl[] = {
377         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
378         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x04),
379         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x02),
380         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4c),
381         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xbb),
382         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
383         QMP_PHY_INIT_CFG(QSERDES_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
384         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x03),
385         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_LVL, 0x18),
386         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
387 };
388
389 static const struct qmp_phy_init_tbl msm8996_usb3_pcs_tbl[] = {
390         /* FLL settings */
391         QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL2, 0x03),
392         QMP_PHY_INIT_CFG_L(QPHY_FLL_CNTRL1, 0x02),
393         QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_L, 0x09),
394         QMP_PHY_INIT_CFG_L(QPHY_FLL_CNT_VAL_H_TOL, 0x42),
395         QMP_PHY_INIT_CFG_L(QPHY_FLL_MAN_CODE, 0x85),
396
397         /* Lock Det settings */
398         QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG1, 0xd1),
399         QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG2, 0x1f),
400         QMP_PHY_INIT_CFG(QPHY_LOCK_DETECT_CONFIG3, 0x47),
401         QMP_PHY_INIT_CFG(QPHY_POWER_STATE_CONFIG2, 0x08),
402 };
403
404 static const struct qmp_phy_init_tbl ipq8074_pcie_serdes_tbl[] = {
405         QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x18),
406         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_ENABLE1, 0x10),
407         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TRIM, 0xf),
408         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP_EN, 0x1),
409         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_MAP, 0x0),
410         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER1, 0x1f),
411         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_TIMER2, 0x3f),
412         QMP_PHY_INIT_CFG(QSERDES_COM_CMN_CONFIG, 0x6),
413         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_IVCO, 0xf),
414         QMP_PHY_INIT_CFG(QSERDES_COM_HSCLK_SEL, 0x0),
415         QMP_PHY_INIT_CFG(QSERDES_COM_SVS_MODE_CLK_SEL, 0x1),
416         QMP_PHY_INIT_CFG(QSERDES_COM_CORE_CLK_EN, 0x20),
417         QMP_PHY_INIT_CFG(QSERDES_COM_CORECLK_DIV, 0xa),
418         QMP_PHY_INIT_CFG(QSERDES_COM_RESETSM_CNTRL, 0x20),
419         QMP_PHY_INIT_CFG(QSERDES_COM_BG_TIMER, 0xa),
420         QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_EN_SEL, 0xa),
421         QMP_PHY_INIT_CFG(QSERDES_COM_DEC_START_MODE0, 0x82),
422         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START3_MODE0, 0x3),
423         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START2_MODE0, 0x55),
424         QMP_PHY_INIT_CFG(QSERDES_COM_DIV_FRAC_START1_MODE0, 0x55),
425         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP3_MODE0, 0x0),
426         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP2_MODE0, 0xD),
427         QMP_PHY_INIT_CFG(QSERDES_COM_LOCK_CMP1_MODE0, 0xD04),
428         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_SELECT, 0x33),
429         QMP_PHY_INIT_CFG(QSERDES_COM_SYS_CLK_CTRL, 0x2),
430         QMP_PHY_INIT_CFG(QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1f),
431         QMP_PHY_INIT_CFG(QSERDES_COM_CP_CTRL_MODE0, 0xb),
432         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_RCTRL_MODE0, 0x16),
433         QMP_PHY_INIT_CFG(QSERDES_COM_PLL_CCTRL_MODE0, 0x28),
434         QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN1_MODE0, 0x0),
435         QMP_PHY_INIT_CFG(QSERDES_COM_INTEGLOOP_GAIN0_MODE0, 0x80),
436         QMP_PHY_INIT_CFG(QSERDES_COM_BIAS_EN_CTRL_BY_PSM, 0x1),
437         QMP_PHY_INIT_CFG(QSERDES_COM_VCO_TUNE_CTRL, 0xa),
438         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_EN_CENTER, 0x1),
439         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER1, 0x31),
440         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_PER2, 0x1),
441         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER1, 0x2),
442         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_ADJ_PER2, 0x0),
443         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE1, 0x2f),
444         QMP_PHY_INIT_CFG(QSERDES_COM_SSC_STEP_SIZE2, 0x19),
445         QMP_PHY_INIT_CFG(QSERDES_COM_CLK_EP_DIV, 0x19),
446         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_CNTRL, 0x7),
447 };
448
449 static const struct qmp_phy_init_tbl ipq8074_pcie_tx_tbl[] = {
450         QMP_PHY_INIT_CFG(QSERDES_TX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN, 0x45),
451         QMP_PHY_INIT_CFG(QSERDES_TX_LANE_MODE, 0x6),
452         QMP_PHY_INIT_CFG(QSERDES_TX_RES_CODE_LANE_OFFSET, 0x2),
453         QMP_PHY_INIT_CFG(QSERDES_TX_RCV_DETECT_LVL_2, 0x12),
454 };
455
456 static const struct qmp_phy_init_tbl ipq8074_pcie_rx_tbl[] = {
457         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_ENABLES, 0x1c),
458         QMP_PHY_INIT_CFG(QSERDES_RX_SIGDET_DEGLITCH_CNTRL, 0x14),
459         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL2, 0x1),
460         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL3, 0x0),
461         QMP_PHY_INIT_CFG(QSERDES_RX_RX_EQU_ADAPTOR_CNTRL4, 0xdb),
462         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
463         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN, 0x4),
464         QMP_PHY_INIT_CFG(QSERDES_RX_UCDR_SO_GAIN_HALF, 0x4),
465 };
466
467 static const struct qmp_phy_init_tbl ipq8074_pcie_pcs_tbl[] = {
468         QMP_PHY_INIT_CFG(QPHY_ENDPOINT_REFCLK_DRIVE, 0x4),
469         QMP_PHY_INIT_CFG(QPHY_OSC_DTCT_ACTIONS, 0x0),
470         QMP_PHY_INIT_CFG(QPHY_PWRUP_RESET_DLY_TIME_AUXCLK, 0x40),
471         QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_MSB, 0x0),
472         QMP_PHY_INIT_CFG(QPHY_L1SS_WAKEUP_DLY_TIME_AUXCLK_LSB, 0x40),
473         QMP_PHY_INIT_CFG(QPHY_PLL_LOCK_CHK_DLY_TIME_AUXCLK_LSB, 0x0),
474         QMP_PHY_INIT_CFG(QPHY_LP_WAKEUP_DLY_TIME_AUXCLK, 0x40),
475         QMP_PHY_INIT_CFG_L(QPHY_PLL_LOCK_CHK_DLY_TIME, 0x73),
476         QMP_PHY_INIT_CFG(QPHY_RX_SIGDET_LVL, 0x99),
477         QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M6DB_V0, 0x15),
478         QMP_PHY_INIT_CFG(QPHY_TXDEEMPH_M3P5DB_V0, 0xe),
479         QMP_PHY_INIT_CFG_L(QPHY_SW_RESET, 0x0),
480         QMP_PHY_INIT_CFG_L(QPHY_START_CTRL, 0x3),
481 };
482
483 static const struct qmp_phy_init_tbl qmp_v3_usb3_serdes_tbl[] = {
484         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
485         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x14),
486         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x08),
487         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
488         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
489         QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL2, 0x08),
490         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x16),
491         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
492         QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x80),
493         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
494         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0xab),
495         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0xea),
496         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x02),
497         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
498         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
499         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
500         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
501         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
502         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
503         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
504         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
505         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
506         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x34),
507         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x15),
508         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x04),
509         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
510         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_CFG, 0x00),
511         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
512         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_BUF_ENABLE, 0x0a),
513         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
514         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x31),
515         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
516         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x00),
517         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
518         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x85),
519         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x07),
520 };
521
522 static const struct qmp_phy_init_tbl qmp_v3_usb3_tx_tbl[] = {
523         QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
524         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
525         QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x16),
526         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x09),
527         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x06),
528 };
529
530 static const struct qmp_phy_init_tbl qmp_v3_usb3_rx_tbl[] = {
531         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
532         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0f),
533         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
534         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
535         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
536         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
537         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
538         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x16),
539         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
540 };
541
542 static const struct qmp_phy_init_tbl qmp_v3_usb3_pcs_tbl[] = {
543         /* FLL settings */
544         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
545         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
546         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
547         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x40),
548         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
549
550         /* Lock Det settings */
551         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
552         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
553         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
554         QMP_PHY_INIT_CFG(QPHY_V3_PCS_POWER_STATE_CONFIG2, 0x1b),
555
556         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0xba),
557         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
558         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V1, 0x9f),
559         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V2, 0xb7),
560         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V3, 0x4e),
561         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V4, 0x65),
562         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_LS, 0x6b),
563         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x15),
564         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0d),
565         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V1, 0x15),
566         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V1, 0x0d),
567         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V2, 0x15),
568         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V2, 0x0d),
569         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V3, 0x15),
570         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V3, 0x1d),
571         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V4, 0x15),
572         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V4, 0x0d),
573         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_LS, 0x15),
574         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_LS, 0x0d),
575
576         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RATE_SLEW_CNTRL, 0x02),
577         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
578         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
579         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
580         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
581         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
582         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
583         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
584         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
585         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
586         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
587 };
588
589 static const struct qmp_phy_init_tbl qmp_v3_usb3_uniphy_serdes_tbl[] = {
590         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
591         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x14),
592         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x04),
593         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
594         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
595         QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL2, 0x08),
596         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
597         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
598         QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x80),
599         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
600         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0xab),
601         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0xea),
602         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x02),
603         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
604         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
605         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
606         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
607         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
608         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
609         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
610         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
611         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
612         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x34),
613         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x15),
614         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x04),
615         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
616         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_CFG, 0x00),
617         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
618         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_BUF_ENABLE, 0x0a),
619         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
620         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x31),
621         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
622         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x00),
623         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
624         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x85),
625         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x07),
626 };
627
628 static const struct qmp_phy_init_tbl qmp_v3_usb3_uniphy_tx_tbl[] = {
629         QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
630         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
631         QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0xc6),
632         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x06),
633         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x06),
634 };
635
636 static const struct qmp_phy_init_tbl qmp_v3_usb3_uniphy_rx_tbl[] = {
637         QMP_PHY_INIT_CFG(QSERDES_V3_RX_VGA_CAL_CNTRL2, 0x0c),
638         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x50),
639         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
640         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0e),
641         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
642         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
643         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x77),
644         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
645         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x03),
646         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1c),
647         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
648 };
649
650 static const struct qmp_phy_init_tbl qmp_v3_usb3_uniphy_pcs_tbl[] = {
651         /* FLL settings */
652         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
653         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
654         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
655         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x40),
656         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
657
658         /* Lock Det settings */
659         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
660         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
661         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
662         QMP_PHY_INIT_CFG(QPHY_V3_PCS_POWER_STATE_CONFIG2, 0x1b),
663
664         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0xba),
665         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
666         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V1, 0x9f),
667         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V2, 0xb5),
668         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V3, 0x4c),
669         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V4, 0x64),
670         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_LS, 0x6a),
671         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x15),
672         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0d),
673         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V1, 0x15),
674         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V1, 0x0d),
675         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V2, 0x15),
676         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V2, 0x0d),
677         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V3, 0x15),
678         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V3, 0x1d),
679         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V4, 0x15),
680         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V4, 0x0d),
681         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_LS, 0x15),
682         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_LS, 0x0d),
683
684         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RATE_SLEW_CNTRL, 0x02),
685         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
686         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
687         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
688         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
689         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
690         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
691         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
692         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
693         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
694         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
695
696         QMP_PHY_INIT_CFG(QPHY_V3_PCS_REFGEN_REQ_CONFIG1, 0x21),
697         QMP_PHY_INIT_CFG(QPHY_V3_PCS_REFGEN_REQ_CONFIG2, 0x60),
698 };
699
700 static const struct qmp_phy_init_tbl sdm845_ufsphy_serdes_tbl[] = {
701         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x02),
702         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x04),
703         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BG_TIMER, 0x0a),
704         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
705         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
706         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0xd5),
707         QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL, 0x20),
708         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
709         QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x00),
710         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x01),
711         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_CTRL, 0x00),
712         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
713         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x04),
714         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x05),
715         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_INITVAL1, 0xff),
716         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_INITVAL2, 0x00),
717         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
718         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
719         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
720         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
721         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
722         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
723         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xda),
724         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
725         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0xff),
726         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x0c),
727         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE1, 0x98),
728         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE1, 0x06),
729         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE1, 0x16),
730         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE1, 0x36),
731         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE1, 0x3f),
732         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE1, 0x00),
733         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE1, 0xc1),
734         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE1, 0x00),
735         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE1, 0x32),
736         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE1, 0x0f),
737
738         /* Rate B */
739         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x44),
740 };
741
742 static const struct qmp_phy_init_tbl sdm845_ufsphy_tx_tbl[] = {
743         QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x06),
744         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x04),
745         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_RX, 0x07),
746 };
747
748 static const struct qmp_phy_init_tbl sdm845_ufsphy_rx_tbl[] = {
749         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_LVL, 0x24),
750         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x0f),
751         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
752         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_INTERFACE_MODE, 0x40),
753         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
754         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_TERM_BW, 0x5b),
755         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
756         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
757         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1b),
758         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_HALF, 0x04),
759         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN_QUARTER, 0x04),
760         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SVS_SO_GAIN, 0x04),
761         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
762         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x81),
763         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
764         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x59),
765 };
766
767 static const struct qmp_phy_init_tbl sdm845_ufsphy_pcs_tbl[] = {
768         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_CTRL2, 0x6e),
769         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TX_LARGE_AMP_DRV_LVL, 0x0a),
770         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TX_SMALL_AMP_DRV_LVL, 0x02),
771         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SYM_RESYNC_CTRL, 0x03),
772         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TX_MID_TERM_CTRL1, 0x43),
773         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_CTRL1, 0x0f),
774         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_MIN_HIBERN8_TIME, 0x9a),
775         QMP_PHY_INIT_CFG(QPHY_V3_PCS_MULTI_LANE_CTRL1, 0x02),
776 };
777
778 static const struct qmp_phy_init_tbl msm8998_usb3_serdes_tbl[] = {
779         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CLK_SELECT, 0x30),
780         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BIAS_EN_CLKBUFLR_EN, 0x04),
781         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYSCLK_EN_SEL, 0x14),
782         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SYS_CLK_CTRL, 0x06),
783         QMP_PHY_INIT_CFG(QSERDES_V3_COM_RESETSM_CNTRL2, 0x08),
784         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_CONFIG, 0x06),
785         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SVS_MODE_CLK_SEL, 0x01),
786         QMP_PHY_INIT_CFG(QSERDES_V3_COM_HSCLK_SEL, 0x80),
787         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DEC_START_MODE0, 0x82),
788         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START1_MODE0, 0xab),
789         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START2_MODE0, 0xea),
790         QMP_PHY_INIT_CFG(QSERDES_V3_COM_DIV_FRAC_START3_MODE0, 0x02),
791         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CP_CTRL_MODE0, 0x06),
792         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_RCTRL_MODE0, 0x16),
793         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_CCTRL_MODE0, 0x36),
794         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN1_MODE0, 0x00),
795         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_GAIN0_MODE0, 0x3f),
796         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE2_MODE0, 0x01),
797         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE1_MODE0, 0xc9),
798         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORECLK_DIV_MODE0, 0x0a),
799         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP3_MODE0, 0x00),
800         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP2_MODE0, 0x34),
801         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP1_MODE0, 0x15),
802         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_EN, 0x04),
803         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CORE_CLK_EN, 0x00),
804         QMP_PHY_INIT_CFG(QSERDES_V3_COM_LOCK_CMP_CFG, 0x00),
805         QMP_PHY_INIT_CFG(QSERDES_V3_COM_VCO_TUNE_MAP, 0x00),
806         QMP_PHY_INIT_CFG(QSERDES_V3_COM_BG_TIMER, 0x0a),
807         QMP_PHY_INIT_CFG(QSERDES_V3_COM_PLL_IVCO, 0x07),
808         QMP_PHY_INIT_CFG(QSERDES_V3_COM_INTEGLOOP_INITVAL, 0x80),
809         QMP_PHY_INIT_CFG(QSERDES_V3_COM_CMN_MODE, 0x01),
810         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_EN_CENTER, 0x01),
811         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER1, 0x31),
812         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_PER2, 0x01),
813         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER1, 0x00),
814         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_ADJ_PER2, 0x00),
815         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE1, 0x85),
816         QMP_PHY_INIT_CFG(QSERDES_V3_COM_SSC_STEP_SIZE2, 0x07),
817 };
818
819 static const struct qmp_phy_init_tbl msm8998_usb3_tx_tbl[] = {
820         QMP_PHY_INIT_CFG(QSERDES_V3_TX_HIGHZ_DRVR_EN, 0x10),
821         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RCV_DETECT_LVL_2, 0x12),
822         QMP_PHY_INIT_CFG(QSERDES_V3_TX_LANE_MODE_1, 0x16),
823         QMP_PHY_INIT_CFG(QSERDES_V3_TX_RES_CODE_LANE_OFFSET_TX, 0x00),
824 };
825
826 static const struct qmp_phy_init_tbl msm8998_usb3_rx_tbl[] = {
827         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_FO_GAIN, 0x0b),
828         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL2, 0x0f),
829         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL3, 0x4e),
830         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQU_ADAPTOR_CNTRL4, 0x18),
831         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_EQ_OFFSET_ADAPTOR_CNTRL1, 0x07),
832         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x80),
833         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_CNTRL, 0x43),
834         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_DEGLITCH_CNTRL, 0x1c),
835         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x75),
836         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_LOW, 0x00),
837         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FASTLOCK_COUNT_HIGH, 0x00),
838         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_PI_CONTROLS, 0x80),
839         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_FO_GAIN, 0x0a),
840         QMP_PHY_INIT_CFG(QSERDES_V3_RX_UCDR_SO_GAIN, 0x06),
841         QMP_PHY_INIT_CFG(QSERDES_V3_RX_SIGDET_ENABLES, 0x00),
842         QMP_PHY_INIT_CFG(QSERDES_V3_RX_VGA_CAL_CNTRL2, 0x03),
843         QMP_PHY_INIT_CFG(QSERDES_V3_RX_RX_MODE_00, 0x05),
844 };
845
846 static const struct qmp_phy_init_tbl msm8998_usb3_pcs_tbl[] = {
847         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL2, 0x83),
848         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_L, 0x09),
849         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNT_VAL_H_TOL, 0xa2),
850         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_MAN_CODE, 0x40),
851         QMP_PHY_INIT_CFG(QPHY_V3_PCS_FLL_CNTRL1, 0x02),
852         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG1, 0xd1),
853         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG2, 0x1f),
854         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LOCK_DETECT_CONFIG3, 0x47),
855         QMP_PHY_INIT_CFG(QPHY_V3_PCS_POWER_STATE_CONFIG2, 0x1b),
856         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V0, 0x9f),
857         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V1, 0x9f),
858         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V2, 0xb7),
859         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V3, 0x4e),
860         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_V4, 0x65),
861         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXMGN_LS, 0x6b),
862         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V0, 0x15),
863         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V0, 0x0d),
864         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TX_LARGE_AMP_DRV_LVL, 0x15),
865         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V1, 0x0d),
866         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V2, 0x15),
867         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V2, 0x0d),
868         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V3, 0x15),
869         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V3, 0x0d),
870         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_V4, 0x15),
871         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_V4, 0x0d),
872         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M6DB_LS, 0x15),
873         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TXDEEMPH_M3P5DB_LS, 0x0d),
874         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RATE_SLEW_CNTRL, 0x02),
875         QMP_PHY_INIT_CFG(QPHY_V3_PCS_PWRUP_RESET_DLY_TIME_AUXCLK, 0x04),
876         QMP_PHY_INIT_CFG(QPHY_V3_PCS_TSYNC_RSYNC_TIME, 0x44),
877         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_L, 0xe7),
878         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_P1U2_H, 0x03),
879         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_L, 0x40),
880         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RCVR_DTCT_DLY_U3_H, 0x00),
881         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RX_SIGDET_LVL, 0x8a),
882         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_WAIT_TIME, 0x75),
883         QMP_PHY_INIT_CFG(QPHY_V3_PCS_LFPS_TX_ECSTART_EQTLOCK, 0x86),
884         QMP_PHY_INIT_CFG(QPHY_V3_PCS_RXEQTRAINING_RUN_TIME, 0x13),
885 };
886
887 static const struct qmp_phy_init_tbl sm8150_ufsphy_serdes_tbl[] = {
888         QMP_PHY_INIT_CFG(QPHY_POWER_DOWN_CONTROL, 0x01),
889         QMP_PHY_INIT_CFG(QSERDES_V4_COM_SYSCLK_EN_SEL, 0xd9),
890         QMP_PHY_INIT_CFG(QSERDES_V4_COM_HSCLK_SEL, 0x11),
891         QMP_PHY_INIT_CFG(QSERDES_V4_COM_HSCLK_HS_SWITCH_SEL, 0x00),
892         QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP_EN, 0x01),
893         QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_MAP, 0x02),
894         QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_IVCO, 0x0f),
895         QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_INITVAL2, 0x00),
896         QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_HSCLK_SEL, 0x11),
897         QMP_PHY_INIT_CFG(QSERDES_V4_COM_DEC_START_MODE0, 0x82),
898         QMP_PHY_INIT_CFG(QSERDES_V4_COM_CP_CTRL_MODE0, 0x06),
899         QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_RCTRL_MODE0, 0x16),
900         QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_CCTRL_MODE0, 0x36),
901         QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP1_MODE0, 0xff),
902         QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP2_MODE0, 0x0c),
903         QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE1_MODE0, 0xac),
904         QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE2_MODE0, 0x1e),
905         QMP_PHY_INIT_CFG(QSERDES_V4_COM_DEC_START_MODE1, 0x98),
906         QMP_PHY_INIT_CFG(QSERDES_V4_COM_CP_CTRL_MODE1, 0x06),
907         QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_RCTRL_MODE1, 0x16),
908         QMP_PHY_INIT_CFG(QSERDES_V4_COM_PLL_CCTRL_MODE1, 0x36),
909         QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP1_MODE1, 0x32),
910         QMP_PHY_INIT_CFG(QSERDES_V4_COM_LOCK_CMP2_MODE1, 0x0f),
911         QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE1_MODE1, 0xdd),
912         QMP_PHY_INIT_CFG(QSERDES_V4_COM_BIN_VCOCAL_CMP_CODE2_MODE1, 0x23),
913
914         /* Rate B */
915         QMP_PHY_INIT_CFG(QSERDES_V4_COM_VCO_TUNE_MAP, 0x06),
916 };
917
918 static const struct qmp_phy_init_tbl sm8150_ufsphy_tx_tbl[] = {
919         QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_1_DIVIDER_BAND0_1, 0x06),
920         QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_2_DIVIDER_BAND0_1, 0x03),
921         QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_3_DIVIDER_BAND0_1, 0x01),
922         QMP_PHY_INIT_CFG(QSERDES_V4_TX_PWM_GEAR_4_DIVIDER_BAND0_1, 0x00),
923         QMP_PHY_INIT_CFG(QSERDES_V4_TX_LANE_MODE_1, 0x05),
924         QMP_PHY_INIT_CFG(QSERDES_V4_TX_TRAN_DRVR_EMP_EN, 0x0c),
925 };
926
927 static const struct qmp_phy_init_tbl sm8150_ufsphy_rx_tbl[] = {
928         QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_LVL, 0x24),
929         QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_CNTRL, 0x0f),
930         QMP_PHY_INIT_CFG(QSERDES_V4_RX_SIGDET_DEGLITCH_CNTRL, 0x1e),
931         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_BAND, 0x18),
932         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FASTLOCK_FO_GAIN, 0x0a),
933         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_SATURATION_AND_ENABLE, 0x4b),
934         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CONTROLS, 0xf1),
935         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FASTLOCK_COUNT_LOW, 0x80),
936         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_PI_CTRL2, 0x80),
937         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_FO_GAIN, 0x0c),
938         QMP_PHY_INIT_CFG(QSERDES_V4_RX_UCDR_SO_GAIN, 0x04),
939         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_TERM_BW, 0x1b),
940         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL2, 0x06),
941         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL3, 0x04),
942         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_EQU_ADAPTOR_CNTRL4, 0x1d),
943         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_OFFSET_ADAPTOR_CNTRL2, 0x00),
944         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_MEASURE_TIME, 0x10),
945         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_LOW, 0xc0),
946         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_IDAC_TSETTLE_HIGH, 0x00),
947         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_LOW, 0x36),
948         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH, 0x36),
949         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH2, 0xf6),
950         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH3, 0x3b),
951         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_00_HIGH4, 0x3d),
952         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_LOW, 0xe0),
953         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH, 0xc8),
954         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH2, 0xc8),
955         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH3, 0x3b),
956         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_01_HIGH4, 0xb1),
957         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_LOW, 0xe0),
958         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH, 0xc8),
959         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH2, 0xc8),
960         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH3, 0x3b),
961         QMP_PHY_INIT_CFG(QSERDES_V4_RX_RX_MODE_10_HIGH4, 0xb1),
962
963 };
964
965 static const struct qmp_phy_init_tbl sm8150_ufsphy_pcs_tbl[] = {
966         QMP_PHY_INIT_CFG(QPHY_V4_RX_SIGDET_CTRL2, 0x6d),
967         QMP_PHY_INIT_CFG(QPHY_V4_TX_LARGE_AMP_DRV_LVL, 0x0a),
968         QMP_PHY_INIT_CFG(QPHY_V4_TX_SMALL_AMP_DRV_LVL, 0x02),
969         QMP_PHY_INIT_CFG(QPHY_V4_TX_MID_TERM_CTRL1, 0x43),
970         QMP_PHY_INIT_CFG(QPHY_V4_DEBUG_BUS_CLKSEL, 0x1f),
971         QMP_PHY_INIT_CFG(QPHY_V4_RX_MIN_HIBERN8_TIME, 0xff),
972         QMP_PHY_INIT_CFG(QPHY_V4_MULTI_LANE_CTRL1, 0x02),
973 };
974
975 /* struct qmp_phy_cfg - per-PHY initialization config */
976 struct qmp_phy_cfg {
977         /* phy-type - PCIE/UFS/USB */
978         unsigned int type;
979         /* number of lanes provided by phy */
980         int nlanes;
981
982         /* Init sequence for PHY blocks - serdes, tx, rx, pcs */
983         const struct qmp_phy_init_tbl *serdes_tbl;
984         int serdes_tbl_num;
985         const struct qmp_phy_init_tbl *tx_tbl;
986         int tx_tbl_num;
987         const struct qmp_phy_init_tbl *rx_tbl;
988         int rx_tbl_num;
989         const struct qmp_phy_init_tbl *pcs_tbl;
990         int pcs_tbl_num;
991
992         /* clock ids to be requested */
993         const char * const *clk_list;
994         int num_clks;
995         /* resets to be requested */
996         const char * const *reset_list;
997         int num_resets;
998         /* regulators to be requested */
999         const char * const *vreg_list;
1000         int num_vregs;
1001
1002         /* array of registers with different offsets */
1003         const unsigned int *regs;
1004
1005         unsigned int start_ctrl;
1006         unsigned int pwrdn_ctrl;
1007         unsigned int mask_com_pcs_ready;
1008
1009         /* true, if PHY has a separate PHY_COM control block */
1010         bool has_phy_com_ctrl;
1011         /* true, if PHY has a reset for individual lanes */
1012         bool has_lane_rst;
1013         /* true, if PHY needs delay after POWER_DOWN */
1014         bool has_pwrdn_delay;
1015         /* power_down delay in usec */
1016         int pwrdn_delay_min;
1017         int pwrdn_delay_max;
1018
1019         /* true, if PHY has a separate DP_COM control block */
1020         bool has_phy_dp_com_ctrl;
1021         /* true, if PHY has secondary tx/rx lanes to be configured */
1022         bool is_dual_lane_phy;
1023
1024         /* true, if PCS block has no separate SW_RESET register */
1025         bool no_pcs_sw_reset;
1026 };
1027
1028 /**
1029  * struct qmp_phy - per-lane phy descriptor
1030  *
1031  * @phy: generic phy
1032  * @tx: iomapped memory space for lane's tx
1033  * @rx: iomapped memory space for lane's rx
1034  * @pcs: iomapped memory space for lane's pcs
1035  * @tx2: iomapped memory space for second lane's tx (in dual lane PHYs)
1036  * @rx2: iomapped memory space for second lane's rx (in dual lane PHYs)
1037  * @pcs_misc: iomapped memory space for lane's pcs_misc
1038  * @pipe_clk: pipe lock
1039  * @index: lane index
1040  * @qmp: QMP phy to which this lane belongs
1041  * @lane_rst: lane's reset controller
1042  */
1043 struct qmp_phy {
1044         struct phy *phy;
1045         void __iomem *tx;
1046         void __iomem *rx;
1047         void __iomem *pcs;
1048         void __iomem *tx2;
1049         void __iomem *rx2;
1050         void __iomem *pcs_misc;
1051         struct clk *pipe_clk;
1052         unsigned int index;
1053         struct qcom_qmp *qmp;
1054         struct reset_control *lane_rst;
1055 };
1056
1057 /**
1058  * struct qcom_qmp - structure holding QMP phy block attributes
1059  *
1060  * @dev: device
1061  * @serdes: iomapped memory space for phy's serdes
1062  * @dp_com: iomapped memory space for phy's dp_com control block
1063  *
1064  * @clks: array of clocks required by phy
1065  * @resets: array of resets required by phy
1066  * @vregs: regulator supplies bulk data
1067  *
1068  * @cfg: phy specific configuration
1069  * @phys: array of per-lane phy descriptors
1070  * @phy_mutex: mutex lock for PHY common block initialization
1071  * @init_count: phy common block initialization count
1072  * @phy_initialized: indicate if PHY has been initialized
1073  * @mode: current PHY mode
1074  * @ufs_reset: optional UFS PHY reset handle
1075  */
1076 struct qcom_qmp {
1077         struct device *dev;
1078         void __iomem *serdes;
1079         void __iomem *dp_com;
1080
1081         struct clk_bulk_data *clks;
1082         struct reset_control **resets;
1083         struct regulator_bulk_data *vregs;
1084
1085         const struct qmp_phy_cfg *cfg;
1086         struct qmp_phy **phys;
1087
1088         struct mutex phy_mutex;
1089         int init_count;
1090         bool phy_initialized;
1091         enum phy_mode mode;
1092
1093         struct reset_control *ufs_reset;
1094 };
1095
1096 static inline void qphy_setbits(void __iomem *base, u32 offset, u32 val)
1097 {
1098         u32 reg;
1099
1100         reg = readl(base + offset);
1101         reg |= val;
1102         writel(reg, base + offset);
1103
1104         /* ensure that above write is through */
1105         readl(base + offset);
1106 }
1107
1108 static inline void qphy_clrbits(void __iomem *base, u32 offset, u32 val)
1109 {
1110         u32 reg;
1111
1112         reg = readl(base + offset);
1113         reg &= ~val;
1114         writel(reg, base + offset);
1115
1116         /* ensure that above write is through */
1117         readl(base + offset);
1118 }
1119
1120 /* list of clocks required by phy */
1121 static const char * const msm8996_phy_clk_l[] = {
1122         "aux", "cfg_ahb", "ref",
1123 };
1124
1125 static const char * const qmp_v3_phy_clk_l[] = {
1126         "aux", "cfg_ahb", "ref", "com_aux",
1127 };
1128
1129 static const char * const sdm845_ufs_phy_clk_l[] = {
1130         "ref", "ref_aux",
1131 };
1132
1133 /* list of resets */
1134 static const char * const msm8996_pciephy_reset_l[] = {
1135         "phy", "common", "cfg",
1136 };
1137
1138 static const char * const msm8996_usb3phy_reset_l[] = {
1139         "phy", "common",
1140 };
1141
1142 /* list of regulators */
1143 static const char * const qmp_phy_vreg_l[] = {
1144         "vdda-phy", "vdda-pll",
1145 };
1146
1147 static const struct qmp_phy_cfg msm8996_pciephy_cfg = {
1148         .type                   = PHY_TYPE_PCIE,
1149         .nlanes                 = 3,
1150
1151         .serdes_tbl             = msm8996_pcie_serdes_tbl,
1152         .serdes_tbl_num         = ARRAY_SIZE(msm8996_pcie_serdes_tbl),
1153         .tx_tbl                 = msm8996_pcie_tx_tbl,
1154         .tx_tbl_num             = ARRAY_SIZE(msm8996_pcie_tx_tbl),
1155         .rx_tbl                 = msm8996_pcie_rx_tbl,
1156         .rx_tbl_num             = ARRAY_SIZE(msm8996_pcie_rx_tbl),
1157         .pcs_tbl                = msm8996_pcie_pcs_tbl,
1158         .pcs_tbl_num            = ARRAY_SIZE(msm8996_pcie_pcs_tbl),
1159         .clk_list               = msm8996_phy_clk_l,
1160         .num_clks               = ARRAY_SIZE(msm8996_phy_clk_l),
1161         .reset_list             = msm8996_pciephy_reset_l,
1162         .num_resets             = ARRAY_SIZE(msm8996_pciephy_reset_l),
1163         .vreg_list              = qmp_phy_vreg_l,
1164         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1165         .regs                   = pciephy_regs_layout,
1166
1167         .start_ctrl             = PCS_START | PLL_READY_GATE_EN,
1168         .pwrdn_ctrl             = SW_PWRDN | REFCLK_DRV_DSBL,
1169         .mask_com_pcs_ready     = PCS_READY,
1170
1171         .has_phy_com_ctrl       = true,
1172         .has_lane_rst           = true,
1173         .has_pwrdn_delay        = true,
1174         .pwrdn_delay_min        = POWER_DOWN_DELAY_US_MIN,
1175         .pwrdn_delay_max        = POWER_DOWN_DELAY_US_MAX,
1176 };
1177
1178 static const struct qmp_phy_cfg msm8996_usb3phy_cfg = {
1179         .type                   = PHY_TYPE_USB3,
1180         .nlanes                 = 1,
1181
1182         .serdes_tbl             = msm8996_usb3_serdes_tbl,
1183         .serdes_tbl_num         = ARRAY_SIZE(msm8996_usb3_serdes_tbl),
1184         .tx_tbl                 = msm8996_usb3_tx_tbl,
1185         .tx_tbl_num             = ARRAY_SIZE(msm8996_usb3_tx_tbl),
1186         .rx_tbl                 = msm8996_usb3_rx_tbl,
1187         .rx_tbl_num             = ARRAY_SIZE(msm8996_usb3_rx_tbl),
1188         .pcs_tbl                = msm8996_usb3_pcs_tbl,
1189         .pcs_tbl_num            = ARRAY_SIZE(msm8996_usb3_pcs_tbl),
1190         .clk_list               = msm8996_phy_clk_l,
1191         .num_clks               = ARRAY_SIZE(msm8996_phy_clk_l),
1192         .reset_list             = msm8996_usb3phy_reset_l,
1193         .num_resets             = ARRAY_SIZE(msm8996_usb3phy_reset_l),
1194         .vreg_list              = qmp_phy_vreg_l,
1195         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1196         .regs                   = usb3phy_regs_layout,
1197
1198         .start_ctrl             = SERDES_START | PCS_START,
1199         .pwrdn_ctrl             = SW_PWRDN,
1200 };
1201
1202 /* list of resets */
1203 static const char * const ipq8074_pciephy_reset_l[] = {
1204         "phy", "common",
1205 };
1206
1207 static const struct qmp_phy_cfg ipq8074_pciephy_cfg = {
1208         .type                   = PHY_TYPE_PCIE,
1209         .nlanes                 = 1,
1210
1211         .serdes_tbl             = ipq8074_pcie_serdes_tbl,
1212         .serdes_tbl_num         = ARRAY_SIZE(ipq8074_pcie_serdes_tbl),
1213         .tx_tbl                 = ipq8074_pcie_tx_tbl,
1214         .tx_tbl_num             = ARRAY_SIZE(ipq8074_pcie_tx_tbl),
1215         .rx_tbl                 = ipq8074_pcie_rx_tbl,
1216         .rx_tbl_num             = ARRAY_SIZE(ipq8074_pcie_rx_tbl),
1217         .pcs_tbl                = ipq8074_pcie_pcs_tbl,
1218         .pcs_tbl_num            = ARRAY_SIZE(ipq8074_pcie_pcs_tbl),
1219         .clk_list               = NULL,
1220         .num_clks               = 0,
1221         .reset_list             = ipq8074_pciephy_reset_l,
1222         .num_resets             = ARRAY_SIZE(ipq8074_pciephy_reset_l),
1223         .vreg_list              = NULL,
1224         .num_vregs              = 0,
1225         .regs                   = pciephy_regs_layout,
1226
1227         .start_ctrl             = SERDES_START | PCS_START,
1228         .pwrdn_ctrl             = SW_PWRDN | REFCLK_DRV_DSBL,
1229
1230         .has_phy_com_ctrl       = false,
1231         .has_lane_rst           = false,
1232         .has_pwrdn_delay        = true,
1233         .pwrdn_delay_min        = 995,          /* us */
1234         .pwrdn_delay_max        = 1005,         /* us */
1235 };
1236
1237 static const struct qmp_phy_cfg qmp_v3_usb3phy_cfg = {
1238         .type                   = PHY_TYPE_USB3,
1239         .nlanes                 = 1,
1240
1241         .serdes_tbl             = qmp_v3_usb3_serdes_tbl,
1242         .serdes_tbl_num         = ARRAY_SIZE(qmp_v3_usb3_serdes_tbl),
1243         .tx_tbl                 = qmp_v3_usb3_tx_tbl,
1244         .tx_tbl_num             = ARRAY_SIZE(qmp_v3_usb3_tx_tbl),
1245         .rx_tbl                 = qmp_v3_usb3_rx_tbl,
1246         .rx_tbl_num             = ARRAY_SIZE(qmp_v3_usb3_rx_tbl),
1247         .pcs_tbl                = qmp_v3_usb3_pcs_tbl,
1248         .pcs_tbl_num            = ARRAY_SIZE(qmp_v3_usb3_pcs_tbl),
1249         .clk_list               = qmp_v3_phy_clk_l,
1250         .num_clks               = ARRAY_SIZE(qmp_v3_phy_clk_l),
1251         .reset_list             = msm8996_usb3phy_reset_l,
1252         .num_resets             = ARRAY_SIZE(msm8996_usb3phy_reset_l),
1253         .vreg_list              = qmp_phy_vreg_l,
1254         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1255         .regs                   = qmp_v3_usb3phy_regs_layout,
1256
1257         .start_ctrl             = SERDES_START | PCS_START,
1258         .pwrdn_ctrl             = SW_PWRDN,
1259
1260         .has_pwrdn_delay        = true,
1261         .pwrdn_delay_min        = POWER_DOWN_DELAY_US_MIN,
1262         .pwrdn_delay_max        = POWER_DOWN_DELAY_US_MAX,
1263
1264         .has_phy_dp_com_ctrl    = true,
1265         .is_dual_lane_phy       = true,
1266 };
1267
1268 static const struct qmp_phy_cfg qmp_v3_usb3_uniphy_cfg = {
1269         .type                   = PHY_TYPE_USB3,
1270         .nlanes                 = 1,
1271
1272         .serdes_tbl             = qmp_v3_usb3_uniphy_serdes_tbl,
1273         .serdes_tbl_num         = ARRAY_SIZE(qmp_v3_usb3_uniphy_serdes_tbl),
1274         .tx_tbl                 = qmp_v3_usb3_uniphy_tx_tbl,
1275         .tx_tbl_num             = ARRAY_SIZE(qmp_v3_usb3_uniphy_tx_tbl),
1276         .rx_tbl                 = qmp_v3_usb3_uniphy_rx_tbl,
1277         .rx_tbl_num             = ARRAY_SIZE(qmp_v3_usb3_uniphy_rx_tbl),
1278         .pcs_tbl                = qmp_v3_usb3_uniphy_pcs_tbl,
1279         .pcs_tbl_num            = ARRAY_SIZE(qmp_v3_usb3_uniphy_pcs_tbl),
1280         .clk_list               = qmp_v3_phy_clk_l,
1281         .num_clks               = ARRAY_SIZE(qmp_v3_phy_clk_l),
1282         .reset_list             = msm8996_usb3phy_reset_l,
1283         .num_resets             = ARRAY_SIZE(msm8996_usb3phy_reset_l),
1284         .vreg_list              = qmp_phy_vreg_l,
1285         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1286         .regs                   = qmp_v3_usb3phy_regs_layout,
1287
1288         .start_ctrl             = SERDES_START | PCS_START,
1289         .pwrdn_ctrl             = SW_PWRDN,
1290
1291         .has_pwrdn_delay        = true,
1292         .pwrdn_delay_min        = POWER_DOWN_DELAY_US_MIN,
1293         .pwrdn_delay_max        = POWER_DOWN_DELAY_US_MAX,
1294 };
1295
1296 static const struct qmp_phy_cfg sdm845_ufsphy_cfg = {
1297         .type                   = PHY_TYPE_UFS,
1298         .nlanes                 = 2,
1299
1300         .serdes_tbl             = sdm845_ufsphy_serdes_tbl,
1301         .serdes_tbl_num         = ARRAY_SIZE(sdm845_ufsphy_serdes_tbl),
1302         .tx_tbl                 = sdm845_ufsphy_tx_tbl,
1303         .tx_tbl_num             = ARRAY_SIZE(sdm845_ufsphy_tx_tbl),
1304         .rx_tbl                 = sdm845_ufsphy_rx_tbl,
1305         .rx_tbl_num             = ARRAY_SIZE(sdm845_ufsphy_rx_tbl),
1306         .pcs_tbl                = sdm845_ufsphy_pcs_tbl,
1307         .pcs_tbl_num            = ARRAY_SIZE(sdm845_ufsphy_pcs_tbl),
1308         .clk_list               = sdm845_ufs_phy_clk_l,
1309         .num_clks               = ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1310         .vreg_list              = qmp_phy_vreg_l,
1311         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1312         .regs                   = sdm845_ufsphy_regs_layout,
1313
1314         .start_ctrl             = SERDES_START,
1315         .pwrdn_ctrl             = SW_PWRDN,
1316
1317         .is_dual_lane_phy       = true,
1318         .no_pcs_sw_reset        = true,
1319 };
1320
1321 static const struct qmp_phy_cfg msm8998_pciephy_cfg = {
1322         .type                   = PHY_TYPE_PCIE,
1323         .nlanes                 = 1,
1324
1325         .serdes_tbl             = msm8998_pcie_serdes_tbl,
1326         .serdes_tbl_num         = ARRAY_SIZE(msm8998_pcie_serdes_tbl),
1327         .tx_tbl                 = msm8998_pcie_tx_tbl,
1328         .tx_tbl_num             = ARRAY_SIZE(msm8998_pcie_tx_tbl),
1329         .rx_tbl                 = msm8998_pcie_rx_tbl,
1330         .rx_tbl_num             = ARRAY_SIZE(msm8998_pcie_rx_tbl),
1331         .pcs_tbl                = msm8998_pcie_pcs_tbl,
1332         .pcs_tbl_num            = ARRAY_SIZE(msm8998_pcie_pcs_tbl),
1333         .clk_list               = msm8996_phy_clk_l,
1334         .num_clks               = ARRAY_SIZE(msm8996_phy_clk_l),
1335         .reset_list             = ipq8074_pciephy_reset_l,
1336         .num_resets             = ARRAY_SIZE(ipq8074_pciephy_reset_l),
1337         .vreg_list              = qmp_phy_vreg_l,
1338         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1339         .regs                   = pciephy_regs_layout,
1340
1341         .start_ctrl             = SERDES_START | PCS_START,
1342         .pwrdn_ctrl             = SW_PWRDN | REFCLK_DRV_DSBL,
1343 };
1344
1345 static const struct qmp_phy_cfg msm8998_usb3phy_cfg = {
1346         .type                   = PHY_TYPE_USB3,
1347         .nlanes                 = 1,
1348
1349         .serdes_tbl             = msm8998_usb3_serdes_tbl,
1350         .serdes_tbl_num         = ARRAY_SIZE(msm8998_usb3_serdes_tbl),
1351         .tx_tbl                 = msm8998_usb3_tx_tbl,
1352         .tx_tbl_num             = ARRAY_SIZE(msm8998_usb3_tx_tbl),
1353         .rx_tbl                 = msm8998_usb3_rx_tbl,
1354         .rx_tbl_num             = ARRAY_SIZE(msm8998_usb3_rx_tbl),
1355         .pcs_tbl                = msm8998_usb3_pcs_tbl,
1356         .pcs_tbl_num            = ARRAY_SIZE(msm8998_usb3_pcs_tbl),
1357         .clk_list               = msm8996_phy_clk_l,
1358         .num_clks               = ARRAY_SIZE(msm8996_phy_clk_l),
1359         .reset_list             = msm8996_usb3phy_reset_l,
1360         .num_resets             = ARRAY_SIZE(msm8996_usb3phy_reset_l),
1361         .vreg_list              = qmp_phy_vreg_l,
1362         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1363         .regs                   = qmp_v3_usb3phy_regs_layout,
1364
1365         .start_ctrl             = SERDES_START | PCS_START,
1366         .pwrdn_ctrl             = SW_PWRDN,
1367
1368         .is_dual_lane_phy       = true,
1369 };
1370
1371 static const struct qmp_phy_cfg sm8150_ufsphy_cfg = {
1372         .type                   = PHY_TYPE_UFS,
1373         .nlanes                 = 2,
1374
1375         .serdes_tbl             = sm8150_ufsphy_serdes_tbl,
1376         .serdes_tbl_num         = ARRAY_SIZE(sm8150_ufsphy_serdes_tbl),
1377         .tx_tbl                 = sm8150_ufsphy_tx_tbl,
1378         .tx_tbl_num             = ARRAY_SIZE(sm8150_ufsphy_tx_tbl),
1379         .rx_tbl                 = sm8150_ufsphy_rx_tbl,
1380         .rx_tbl_num             = ARRAY_SIZE(sm8150_ufsphy_rx_tbl),
1381         .pcs_tbl                = sm8150_ufsphy_pcs_tbl,
1382         .pcs_tbl_num            = ARRAY_SIZE(sm8150_ufsphy_pcs_tbl),
1383         .clk_list               = sdm845_ufs_phy_clk_l,
1384         .num_clks               = ARRAY_SIZE(sdm845_ufs_phy_clk_l),
1385         .vreg_list              = qmp_phy_vreg_l,
1386         .num_vregs              = ARRAY_SIZE(qmp_phy_vreg_l),
1387         .regs                   = sm8150_ufsphy_regs_layout,
1388
1389         .start_ctrl             = SERDES_START,
1390         .pwrdn_ctrl             = SW_PWRDN,
1391
1392         .is_dual_lane_phy       = true,
1393         .no_pcs_sw_reset        = true,
1394 };
1395
1396 static void qcom_qmp_phy_configure(void __iomem *base,
1397                                    const unsigned int *regs,
1398                                    const struct qmp_phy_init_tbl tbl[],
1399                                    int num)
1400 {
1401         int i;
1402         const struct qmp_phy_init_tbl *t = tbl;
1403
1404         if (!t)
1405                 return;
1406
1407         for (i = 0; i < num; i++, t++) {
1408                 if (t->in_layout)
1409                         writel(t->val, base + regs[t->offset]);
1410                 else
1411                         writel(t->val, base + t->offset);
1412         }
1413 }
1414
1415 static int qcom_qmp_phy_com_init(struct qmp_phy *qphy)
1416 {
1417         struct qcom_qmp *qmp = qphy->qmp;
1418         const struct qmp_phy_cfg *cfg = qmp->cfg;
1419         void __iomem *serdes = qmp->serdes;
1420         void __iomem *pcs = qphy->pcs;
1421         void __iomem *dp_com = qmp->dp_com;
1422         int ret, i;
1423
1424         mutex_lock(&qmp->phy_mutex);
1425         if (qmp->init_count++) {
1426                 mutex_unlock(&qmp->phy_mutex);
1427                 return 0;
1428         }
1429
1430         /* turn on regulator supplies */
1431         ret = regulator_bulk_enable(cfg->num_vregs, qmp->vregs);
1432         if (ret) {
1433                 dev_err(qmp->dev, "failed to enable regulators, err=%d\n", ret);
1434                 goto err_reg_enable;
1435         }
1436
1437         for (i = 0; i < cfg->num_resets; i++) {
1438                 ret = reset_control_assert(qmp->resets[i]);
1439                 if (ret) {
1440                         dev_err(qmp->dev, "%s reset assert failed\n",
1441                                 cfg->reset_list[i]);
1442                         goto err_rst_assert;
1443                 }
1444         }
1445
1446         for (i = cfg->num_resets - 1; i >= 0; i--) {
1447                 ret = reset_control_deassert(qmp->resets[i]);
1448                 if (ret) {
1449                         dev_err(qmp->dev, "%s reset deassert failed\n",
1450                                 qmp->cfg->reset_list[i]);
1451                         goto err_rst;
1452                 }
1453         }
1454
1455         ret = clk_bulk_prepare_enable(cfg->num_clks, qmp->clks);
1456         if (ret) {
1457                 dev_err(qmp->dev, "failed to enable clks, err=%d\n", ret);
1458                 goto err_rst;
1459         }
1460
1461         if (cfg->has_phy_dp_com_ctrl) {
1462                 qphy_setbits(dp_com, QPHY_V3_DP_COM_POWER_DOWN_CTRL,
1463                              SW_PWRDN);
1464                 /* override hardware control for reset of qmp phy */
1465                 qphy_setbits(dp_com, QPHY_V3_DP_COM_RESET_OVRD_CTRL,
1466                              SW_DPPHY_RESET_MUX | SW_DPPHY_RESET |
1467                              SW_USB3PHY_RESET_MUX | SW_USB3PHY_RESET);
1468
1469                 qphy_setbits(dp_com, QPHY_V3_DP_COM_PHY_MODE_CTRL,
1470                              USB3_MODE | DP_MODE);
1471
1472                 /* bring both QMP USB and QMP DP PHYs PCS block out of reset */
1473                 qphy_clrbits(dp_com, QPHY_V3_DP_COM_RESET_OVRD_CTRL,
1474                              SW_DPPHY_RESET_MUX | SW_DPPHY_RESET |
1475                              SW_USB3PHY_RESET_MUX | SW_USB3PHY_RESET);
1476         }
1477
1478         if (cfg->has_phy_com_ctrl)
1479                 qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
1480                              SW_PWRDN);
1481         else
1482                 qphy_setbits(pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
1483
1484         /* Serdes configuration */
1485         qcom_qmp_phy_configure(serdes, cfg->regs, cfg->serdes_tbl,
1486                                cfg->serdes_tbl_num);
1487
1488         if (cfg->has_phy_com_ctrl) {
1489                 void __iomem *status;
1490                 unsigned int mask, val;
1491
1492                 qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET], SW_RESET);
1493                 qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
1494                              SERDES_START | PCS_START);
1495
1496                 status = serdes + cfg->regs[QPHY_COM_PCS_READY_STATUS];
1497                 mask = cfg->mask_com_pcs_ready;
1498
1499                 ret = readl_poll_timeout(status, val, (val & mask), 10,
1500                                          PHY_INIT_COMPLETE_TIMEOUT);
1501                 if (ret) {
1502                         dev_err(qmp->dev,
1503                                 "phy common block init timed-out\n");
1504                         goto err_com_init;
1505                 }
1506         }
1507
1508         mutex_unlock(&qmp->phy_mutex);
1509
1510         return 0;
1511
1512 err_com_init:
1513         clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
1514 err_rst:
1515         while (++i < cfg->num_resets)
1516                 reset_control_assert(qmp->resets[i]);
1517 err_rst_assert:
1518         regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
1519 err_reg_enable:
1520         mutex_unlock(&qmp->phy_mutex);
1521
1522         return ret;
1523 }
1524
1525 static int qcom_qmp_phy_com_exit(struct qcom_qmp *qmp)
1526 {
1527         const struct qmp_phy_cfg *cfg = qmp->cfg;
1528         void __iomem *serdes = qmp->serdes;
1529         int i = cfg->num_resets;
1530
1531         mutex_lock(&qmp->phy_mutex);
1532         if (--qmp->init_count) {
1533                 mutex_unlock(&qmp->phy_mutex);
1534                 return 0;
1535         }
1536
1537         reset_control_assert(qmp->ufs_reset);
1538         if (cfg->has_phy_com_ctrl) {
1539                 qphy_setbits(serdes, cfg->regs[QPHY_COM_START_CONTROL],
1540                              SERDES_START | PCS_START);
1541                 qphy_clrbits(serdes, cfg->regs[QPHY_COM_SW_RESET],
1542                              SW_RESET);
1543                 qphy_setbits(serdes, cfg->regs[QPHY_COM_POWER_DOWN_CONTROL],
1544                              SW_PWRDN);
1545         }
1546
1547         while (--i >= 0)
1548                 reset_control_assert(qmp->resets[i]);
1549
1550         clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
1551
1552         regulator_bulk_disable(cfg->num_vregs, qmp->vregs);
1553
1554         mutex_unlock(&qmp->phy_mutex);
1555
1556         return 0;
1557 }
1558
1559 static int qcom_qmp_phy_enable(struct phy *phy)
1560 {
1561         struct qmp_phy *qphy = phy_get_drvdata(phy);
1562         struct qcom_qmp *qmp = qphy->qmp;
1563         const struct qmp_phy_cfg *cfg = qmp->cfg;
1564         void __iomem *tx = qphy->tx;
1565         void __iomem *rx = qphy->rx;
1566         void __iomem *pcs = qphy->pcs;
1567         void __iomem *dp_com = qmp->dp_com;
1568         void __iomem *status;
1569         unsigned int mask, val, ready;
1570         int ret;
1571
1572         dev_vdbg(qmp->dev, "Initializing QMP phy\n");
1573
1574         if (cfg->no_pcs_sw_reset) {
1575                 /*
1576                  * Get UFS reset, which is delayed until now to avoid a
1577                  * circular dependency where UFS needs its PHY, but the PHY
1578                  * needs this UFS reset.
1579                  */
1580                 if (!qmp->ufs_reset) {
1581                         qmp->ufs_reset =
1582                                 devm_reset_control_get_exclusive(qmp->dev,
1583                                                                  "ufsphy");
1584
1585                         if (IS_ERR(qmp->ufs_reset)) {
1586                                 ret = PTR_ERR(qmp->ufs_reset);
1587                                 dev_err(qmp->dev,
1588                                         "failed to get UFS reset: %d\n",
1589                                         ret);
1590
1591                                 qmp->ufs_reset = NULL;
1592                                 return ret;
1593                         }
1594                 }
1595
1596                 ret = reset_control_assert(qmp->ufs_reset);
1597                 if (ret)
1598                         goto err_lane_rst;
1599         }
1600
1601         ret = qcom_qmp_phy_com_init(qphy);
1602         if (ret)
1603                 return ret;
1604
1605         if (cfg->has_lane_rst) {
1606                 ret = reset_control_deassert(qphy->lane_rst);
1607                 if (ret) {
1608                         dev_err(qmp->dev, "lane%d reset deassert failed\n",
1609                                 qphy->index);
1610                         goto err_lane_rst;
1611                 }
1612         }
1613
1614         ret = clk_prepare_enable(qphy->pipe_clk);
1615         if (ret) {
1616                 dev_err(qmp->dev, "pipe_clk enable failed err=%d\n", ret);
1617                 goto err_clk_enable;
1618         }
1619
1620         /* Tx, Rx, and PCS configurations */
1621         qcom_qmp_phy_configure(tx, cfg->regs, cfg->tx_tbl, cfg->tx_tbl_num);
1622         /* Configuration for other LANE for USB-DP combo PHY */
1623         if (cfg->is_dual_lane_phy)
1624                 qcom_qmp_phy_configure(qphy->tx2, cfg->regs,
1625                                        cfg->tx_tbl, cfg->tx_tbl_num);
1626
1627         qcom_qmp_phy_configure(rx, cfg->regs, cfg->rx_tbl, cfg->rx_tbl_num);
1628         if (cfg->is_dual_lane_phy)
1629                 qcom_qmp_phy_configure(qphy->rx2, cfg->regs,
1630                                        cfg->rx_tbl, cfg->rx_tbl_num);
1631
1632         qcom_qmp_phy_configure(pcs, cfg->regs, cfg->pcs_tbl, cfg->pcs_tbl_num);
1633         ret = reset_control_deassert(qmp->ufs_reset);
1634         if (ret)
1635                 goto err_lane_rst;
1636
1637         /*
1638          * Pull out PHY from POWER DOWN state.
1639          * This is active low enable signal to power-down PHY.
1640          */
1641         if(cfg->type == PHY_TYPE_PCIE)
1642                 qphy_setbits(pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
1643
1644         if (cfg->has_pwrdn_delay)
1645                 usleep_range(cfg->pwrdn_delay_min, cfg->pwrdn_delay_max);
1646
1647         /* Pull PHY out of reset state */
1648         if (!cfg->no_pcs_sw_reset)
1649                 qphy_clrbits(pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
1650
1651         if (cfg->has_phy_dp_com_ctrl)
1652                 qphy_clrbits(dp_com, QPHY_V3_DP_COM_SW_RESET, SW_RESET);
1653
1654         /* start SerDes and Phy-Coding-Sublayer */
1655         qphy_setbits(pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
1656
1657         if (cfg->type == PHY_TYPE_UFS) {
1658                 status = pcs + cfg->regs[QPHY_PCS_READY_STATUS];
1659                 mask = PCS_READY;
1660                 ready = PCS_READY;
1661         } else {
1662                 status = pcs + cfg->regs[QPHY_PCS_STATUS];
1663                 mask = PHYSTATUS;
1664                 ready = 0;
1665         }
1666
1667         ret = readl_poll_timeout(status, val, (val & mask) == ready, 10,
1668                                  PHY_INIT_COMPLETE_TIMEOUT);
1669         if (ret) {
1670                 dev_err(qmp->dev, "phy initialization timed-out\n");
1671                 goto err_pcs_ready;
1672         }
1673         qmp->phy_initialized = true;
1674         return 0;
1675
1676 err_pcs_ready:
1677         reset_control_assert(qmp->ufs_reset);
1678         clk_disable_unprepare(qphy->pipe_clk);
1679 err_clk_enable:
1680         if (cfg->has_lane_rst)
1681                 reset_control_assert(qphy->lane_rst);
1682 err_lane_rst:
1683         qcom_qmp_phy_com_exit(qmp);
1684
1685         return ret;
1686 }
1687
1688 static int qcom_qmp_phy_disable(struct phy *phy)
1689 {
1690         struct qmp_phy *qphy = phy_get_drvdata(phy);
1691         struct qcom_qmp *qmp = qphy->qmp;
1692         const struct qmp_phy_cfg *cfg = qmp->cfg;
1693
1694         clk_disable_unprepare(qphy->pipe_clk);
1695
1696         /* PHY reset */
1697         if (!cfg->no_pcs_sw_reset)
1698                 qphy_setbits(qphy->pcs, cfg->regs[QPHY_SW_RESET], SW_RESET);
1699
1700         /* stop SerDes and Phy-Coding-Sublayer */
1701         qphy_clrbits(qphy->pcs, cfg->regs[QPHY_START_CTRL], cfg->start_ctrl);
1702
1703         /* Put PHY into POWER DOWN state: active low */
1704         qphy_clrbits(qphy->pcs, QPHY_POWER_DOWN_CONTROL, cfg->pwrdn_ctrl);
1705
1706         if (cfg->has_lane_rst)
1707                 reset_control_assert(qphy->lane_rst);
1708
1709         qcom_qmp_phy_com_exit(qmp);
1710
1711         qmp->phy_initialized = false;
1712
1713         return 0;
1714 }
1715
1716 static int qcom_qmp_phy_set_mode(struct phy *phy,
1717                                  enum phy_mode mode, int submode)
1718 {
1719         struct qmp_phy *qphy = phy_get_drvdata(phy);
1720         struct qcom_qmp *qmp = qphy->qmp;
1721
1722         qmp->mode = mode;
1723
1724         return 0;
1725 }
1726
1727 static void qcom_qmp_phy_enable_autonomous_mode(struct qmp_phy *qphy)
1728 {
1729         struct qcom_qmp *qmp = qphy->qmp;
1730         const struct qmp_phy_cfg *cfg = qmp->cfg;
1731         void __iomem *pcs = qphy->pcs;
1732         void __iomem *pcs_misc = qphy->pcs_misc;
1733         u32 intr_mask;
1734
1735         if (qmp->mode == PHY_MODE_USB_HOST_SS ||
1736             qmp->mode == PHY_MODE_USB_DEVICE_SS)
1737                 intr_mask = ARCVR_DTCT_EN | ALFPS_DTCT_EN;
1738         else
1739                 intr_mask = ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL;
1740
1741         /* Clear any pending interrupts status */
1742         qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
1743         /* Writing 1 followed by 0 clears the interrupt */
1744         qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
1745
1746         qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
1747                      ARCVR_DTCT_EN | ALFPS_DTCT_EN | ARCVR_DTCT_EVENT_SEL);
1748
1749         /* Enable required PHY autonomous mode interrupts */
1750         qphy_setbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL], intr_mask);
1751
1752         /* Enable i/o clamp_n for autonomous mode */
1753         if (pcs_misc)
1754                 qphy_clrbits(pcs_misc, QPHY_V3_PCS_MISC_CLAMP_ENABLE, CLAMP_EN);
1755 }
1756
1757 static void qcom_qmp_phy_disable_autonomous_mode(struct qmp_phy *qphy)
1758 {
1759         struct qcom_qmp *qmp = qphy->qmp;
1760         const struct qmp_phy_cfg *cfg = qmp->cfg;
1761         void __iomem *pcs = qphy->pcs;
1762         void __iomem *pcs_misc = qphy->pcs_misc;
1763
1764         /* Disable i/o clamp_n on resume for normal mode */
1765         if (pcs_misc)
1766                 qphy_setbits(pcs_misc, QPHY_V3_PCS_MISC_CLAMP_ENABLE, CLAMP_EN);
1767
1768         qphy_clrbits(pcs, cfg->regs[QPHY_PCS_AUTONOMOUS_MODE_CTRL],
1769                      ARCVR_DTCT_EN | ARCVR_DTCT_EVENT_SEL | ALFPS_DTCT_EN);
1770
1771         qphy_setbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
1772         /* Writing 1 followed by 0 clears the interrupt */
1773         qphy_clrbits(pcs, cfg->regs[QPHY_PCS_LFPS_RXTERM_IRQ_CLEAR], IRQ_CLEAR);
1774 }
1775
1776 static int __maybe_unused qcom_qmp_phy_runtime_suspend(struct device *dev)
1777 {
1778         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1779         struct qmp_phy *qphy = qmp->phys[0];
1780         const struct qmp_phy_cfg *cfg = qmp->cfg;
1781
1782         dev_vdbg(dev, "Suspending QMP phy, mode:%d\n", qmp->mode);
1783
1784         /* Supported only for USB3 PHY */
1785         if (cfg->type != PHY_TYPE_USB3)
1786                 return 0;
1787
1788         if (!qmp->phy_initialized) {
1789                 dev_vdbg(dev, "PHY not initialized, bailing out\n");
1790                 return 0;
1791         }
1792
1793         qcom_qmp_phy_enable_autonomous_mode(qphy);
1794
1795         clk_disable_unprepare(qphy->pipe_clk);
1796         clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
1797
1798         return 0;
1799 }
1800
1801 static int __maybe_unused qcom_qmp_phy_runtime_resume(struct device *dev)
1802 {
1803         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1804         struct qmp_phy *qphy = qmp->phys[0];
1805         const struct qmp_phy_cfg *cfg = qmp->cfg;
1806         int ret = 0;
1807
1808         dev_vdbg(dev, "Resuming QMP phy, mode:%d\n", qmp->mode);
1809
1810         /* Supported only for USB3 PHY */
1811         if (cfg->type != PHY_TYPE_USB3)
1812                 return 0;
1813
1814         if (!qmp->phy_initialized) {
1815                 dev_vdbg(dev, "PHY not initialized, bailing out\n");
1816                 return 0;
1817         }
1818
1819         ret = clk_bulk_prepare_enable(cfg->num_clks, qmp->clks);
1820         if (ret) {
1821                 dev_err(qmp->dev, "failed to enable clks, err=%d\n", ret);
1822                 return ret;
1823         }
1824
1825         ret = clk_prepare_enable(qphy->pipe_clk);
1826         if (ret) {
1827                 dev_err(dev, "pipe_clk enable failed, err=%d\n", ret);
1828                 clk_bulk_disable_unprepare(cfg->num_clks, qmp->clks);
1829                 return ret;
1830         }
1831
1832         qcom_qmp_phy_disable_autonomous_mode(qphy);
1833
1834         return 0;
1835 }
1836
1837 static int qcom_qmp_phy_vreg_init(struct device *dev)
1838 {
1839         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1840         int num = qmp->cfg->num_vregs;
1841         int i;
1842
1843         qmp->vregs = devm_kcalloc(dev, num, sizeof(*qmp->vregs), GFP_KERNEL);
1844         if (!qmp->vregs)
1845                 return -ENOMEM;
1846
1847         for (i = 0; i < num; i++)
1848                 qmp->vregs[i].supply = qmp->cfg->vreg_list[i];
1849
1850         return devm_regulator_bulk_get(dev, num, qmp->vregs);
1851 }
1852
1853 static int qcom_qmp_phy_reset_init(struct device *dev)
1854 {
1855         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1856         int i;
1857
1858         qmp->resets = devm_kcalloc(dev, qmp->cfg->num_resets,
1859                                    sizeof(*qmp->resets), GFP_KERNEL);
1860         if (!qmp->resets)
1861                 return -ENOMEM;
1862
1863         for (i = 0; i < qmp->cfg->num_resets; i++) {
1864                 struct reset_control *rst;
1865                 const char *name = qmp->cfg->reset_list[i];
1866
1867                 rst = devm_reset_control_get(dev, name);
1868                 if (IS_ERR(rst)) {
1869                         dev_err(dev, "failed to get %s reset\n", name);
1870                         return PTR_ERR(rst);
1871                 }
1872                 qmp->resets[i] = rst;
1873         }
1874
1875         return 0;
1876 }
1877
1878 static int qcom_qmp_phy_clk_init(struct device *dev)
1879 {
1880         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1881         int num = qmp->cfg->num_clks;
1882         int i;
1883
1884         qmp->clks = devm_kcalloc(dev, num, sizeof(*qmp->clks), GFP_KERNEL);
1885         if (!qmp->clks)
1886                 return -ENOMEM;
1887
1888         for (i = 0; i < num; i++)
1889                 qmp->clks[i].id = qmp->cfg->clk_list[i];
1890
1891         return devm_clk_bulk_get(dev, num, qmp->clks);
1892 }
1893
1894 static void phy_pipe_clk_release_provider(void *res)
1895 {
1896         of_clk_del_provider(res);
1897 }
1898
1899 /*
1900  * Register a fixed rate pipe clock.
1901  *
1902  * The <s>_pipe_clksrc generated by PHY goes to the GCC that gate
1903  * controls it. The <s>_pipe_clk coming out of the GCC is requested
1904  * by the PHY driver for its operations.
1905  * We register the <s>_pipe_clksrc here. The gcc driver takes care
1906  * of assigning this <s>_pipe_clksrc as parent to <s>_pipe_clk.
1907  * Below picture shows this relationship.
1908  *
1909  *         +---------------+
1910  *         |   PHY block   |<<---------------------------------------+
1911  *         |               |                                         |
1912  *         |   +-------+   |                   +-----+               |
1913  *   I/P---^-->|  PLL  |---^--->pipe_clksrc--->| GCC |--->pipe_clk---+
1914  *    clk  |   +-------+   |                   +-----+
1915  *         +---------------+
1916  */
1917 static int phy_pipe_clk_register(struct qcom_qmp *qmp, struct device_node *np)
1918 {
1919         struct clk_fixed_rate *fixed;
1920         struct clk_init_data init = { };
1921         int ret;
1922
1923         if ((qmp->cfg->type != PHY_TYPE_USB3) &&
1924             (qmp->cfg->type != PHY_TYPE_PCIE)) {
1925                 /* not all phys register pipe clocks, so return success */
1926                 return 0;
1927         }
1928
1929         ret = of_property_read_string(np, "clock-output-names", &init.name);
1930         if (ret) {
1931                 dev_err(qmp->dev, "%pOFn: No clock-output-names\n", np);
1932                 return ret;
1933         }
1934
1935         fixed = devm_kzalloc(qmp->dev, sizeof(*fixed), GFP_KERNEL);
1936         if (!fixed)
1937                 return -ENOMEM;
1938
1939         init.ops = &clk_fixed_rate_ops;
1940
1941         /* controllers using QMP phys use 125MHz pipe clock interface */
1942         fixed->fixed_rate = 125000000;
1943         fixed->hw.init = &init;
1944
1945         ret = devm_clk_hw_register(qmp->dev, &fixed->hw);
1946         if (ret)
1947                 return ret;
1948
1949         ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &fixed->hw);
1950         if (ret)
1951                 return ret;
1952
1953         /*
1954          * Roll a devm action because the clock provider is the child node, but
1955          * the child node is not actually a device.
1956          */
1957         ret = devm_add_action(qmp->dev, phy_pipe_clk_release_provider, np);
1958         if (ret)
1959                 phy_pipe_clk_release_provider(np);
1960
1961         return ret;
1962 }
1963
1964 static const struct phy_ops qcom_qmp_phy_gen_ops = {
1965         .init           = qcom_qmp_phy_enable,
1966         .exit           = qcom_qmp_phy_disable,
1967         .set_mode       = qcom_qmp_phy_set_mode,
1968         .owner          = THIS_MODULE,
1969 };
1970
1971 static const struct phy_ops qcom_qmp_ufs_ops = {
1972         .power_on       = qcom_qmp_phy_enable,
1973         .power_off      = qcom_qmp_phy_disable,
1974         .set_mode       = qcom_qmp_phy_set_mode,
1975         .owner          = THIS_MODULE,
1976 };
1977
1978 static
1979 int qcom_qmp_phy_create(struct device *dev, struct device_node *np, int id)
1980 {
1981         struct qcom_qmp *qmp = dev_get_drvdata(dev);
1982         struct phy *generic_phy;
1983         struct qmp_phy *qphy;
1984         const struct phy_ops *ops = &qcom_qmp_phy_gen_ops;
1985         char prop_name[MAX_PROP_NAME];
1986         int ret;
1987
1988         qphy = devm_kzalloc(dev, sizeof(*qphy), GFP_KERNEL);
1989         if (!qphy)
1990                 return -ENOMEM;
1991
1992         /*
1993          * Get memory resources for each phy lane:
1994          * Resources are indexed as: tx -> 0; rx -> 1; pcs -> 2.
1995          * For dual lane PHYs: tx2 -> 3, rx2 -> 4, pcs_misc (optional) -> 5
1996          * For single lane PHYs: pcs_misc (optional) -> 3.
1997          */
1998         qphy->tx = of_iomap(np, 0);
1999         if (!qphy->tx)
2000                 return -ENOMEM;
2001
2002         qphy->rx = of_iomap(np, 1);
2003         if (!qphy->rx)
2004                 return -ENOMEM;
2005
2006         qphy->pcs = of_iomap(np, 2);
2007         if (!qphy->pcs)
2008                 return -ENOMEM;
2009
2010         /*
2011          * If this is a dual-lane PHY, then there should be registers for the
2012          * second lane. Some old device trees did not specify this, so fall
2013          * back to old legacy behavior of assuming they can be reached at an
2014          * offset from the first lane.
2015          */
2016         if (qmp->cfg->is_dual_lane_phy) {
2017                 qphy->tx2 = of_iomap(np, 3);
2018                 qphy->rx2 = of_iomap(np, 4);
2019                 if (!qphy->tx2 || !qphy->rx2) {
2020                         dev_warn(dev,
2021                                  "Underspecified device tree, falling back to legacy register regions\n");
2022
2023                         /* In the old version, pcs_misc is at index 3. */
2024                         qphy->pcs_misc = qphy->tx2;
2025                         qphy->tx2 = qphy->tx + QMP_PHY_LEGACY_LANE_STRIDE;
2026                         qphy->rx2 = qphy->rx + QMP_PHY_LEGACY_LANE_STRIDE;
2027
2028                 } else {
2029                         qphy->pcs_misc = of_iomap(np, 5);
2030                 }
2031
2032         } else {
2033                 qphy->pcs_misc = of_iomap(np, 3);
2034         }
2035
2036         if (!qphy->pcs_misc)
2037                 dev_vdbg(dev, "PHY pcs_misc-reg not used\n");
2038
2039         /*
2040          * Get PHY's Pipe clock, if any. USB3 and PCIe are PIPE3
2041          * based phys, so they essentially have pipe clock. So,
2042          * we return error in case phy is USB3 or PIPE type.
2043          * Otherwise, we initialize pipe clock to NULL for
2044          * all phys that don't need this.
2045          */
2046         snprintf(prop_name, sizeof(prop_name), "pipe%d", id);
2047         qphy->pipe_clk = of_clk_get_by_name(np, prop_name);
2048         if (IS_ERR(qphy->pipe_clk)) {
2049                 if (qmp->cfg->type == PHY_TYPE_PCIE ||
2050                     qmp->cfg->type == PHY_TYPE_USB3) {
2051                         ret = PTR_ERR(qphy->pipe_clk);
2052                         if (ret != -EPROBE_DEFER)
2053                                 dev_err(dev,
2054                                         "failed to get lane%d pipe_clk, %d\n",
2055                                         id, ret);
2056                         return ret;
2057                 }
2058                 qphy->pipe_clk = NULL;
2059         }
2060
2061         /* Get lane reset, if any */
2062         if (qmp->cfg->has_lane_rst) {
2063                 snprintf(prop_name, sizeof(prop_name), "lane%d", id);
2064                 qphy->lane_rst = of_reset_control_get(np, prop_name);
2065                 if (IS_ERR(qphy->lane_rst)) {
2066                         dev_err(dev, "failed to get lane%d reset\n", id);
2067                         return PTR_ERR(qphy->lane_rst);
2068                 }
2069         }
2070
2071         if (qmp->cfg->type == PHY_TYPE_UFS)
2072                 ops = &qcom_qmp_ufs_ops;
2073
2074         generic_phy = devm_phy_create(dev, np, ops);
2075         if (IS_ERR(generic_phy)) {
2076                 ret = PTR_ERR(generic_phy);
2077                 dev_err(dev, "failed to create qphy %d\n", ret);
2078                 return ret;
2079         }
2080
2081         qphy->phy = generic_phy;
2082         qphy->index = id;
2083         qphy->qmp = qmp;
2084         qmp->phys[id] = qphy;
2085         phy_set_drvdata(generic_phy, qphy);
2086
2087         return 0;
2088 }
2089
2090 static const struct of_device_id qcom_qmp_phy_of_match_table[] = {
2091         {
2092                 .compatible = "qcom,msm8996-qmp-pcie-phy",
2093                 .data = &msm8996_pciephy_cfg,
2094         }, {
2095                 .compatible = "qcom,msm8996-qmp-usb3-phy",
2096                 .data = &msm8996_usb3phy_cfg,
2097         }, {
2098                 .compatible = "qcom,msm8998-qmp-pcie-phy",
2099                 .data = &msm8998_pciephy_cfg,
2100         }, {
2101                 .compatible = "qcom,msm8998-qmp-ufs-phy",
2102                 .data = &sdm845_ufsphy_cfg,
2103         }, {
2104                 .compatible = "qcom,ipq8074-qmp-pcie-phy",
2105                 .data = &ipq8074_pciephy_cfg,
2106         }, {
2107                 .compatible = "qcom,sdm845-qmp-usb3-phy",
2108                 .data = &qmp_v3_usb3phy_cfg,
2109         }, {
2110                 .compatible = "qcom,sdm845-qmp-usb3-uni-phy",
2111                 .data = &qmp_v3_usb3_uniphy_cfg,
2112         }, {
2113                 .compatible = "qcom,sdm845-qmp-ufs-phy",
2114                 .data = &sdm845_ufsphy_cfg,
2115         }, {
2116                 .compatible = "qcom,msm8998-qmp-usb3-phy",
2117                 .data = &msm8998_usb3phy_cfg,
2118         }, {
2119                 .compatible = "qcom,sm8150-qmp-ufs-phy",
2120                 .data = &sm8150_ufsphy_cfg,
2121         },
2122         { },
2123 };
2124 MODULE_DEVICE_TABLE(of, qcom_qmp_phy_of_match_table);
2125
2126 static const struct dev_pm_ops qcom_qmp_phy_pm_ops = {
2127         SET_RUNTIME_PM_OPS(qcom_qmp_phy_runtime_suspend,
2128                            qcom_qmp_phy_runtime_resume, NULL)
2129 };
2130
2131 static int qcom_qmp_phy_probe(struct platform_device *pdev)
2132 {
2133         struct qcom_qmp *qmp;
2134         struct device *dev = &pdev->dev;
2135         struct resource *res;
2136         struct device_node *child;
2137         struct phy_provider *phy_provider;
2138         void __iomem *base;
2139         int num, id;
2140         int ret;
2141
2142         qmp = devm_kzalloc(dev, sizeof(*qmp), GFP_KERNEL);
2143         if (!qmp)
2144                 return -ENOMEM;
2145
2146         qmp->dev = dev;
2147         dev_set_drvdata(dev, qmp);
2148
2149         /* Get the specific init parameters of QMP phy */
2150         qmp->cfg = of_device_get_match_data(dev);
2151         if (!qmp->cfg)
2152                 return -EINVAL;
2153
2154         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2155         base = devm_ioremap_resource(dev, res);
2156         if (IS_ERR(base))
2157                 return PTR_ERR(base);
2158
2159         /* per PHY serdes; usually located at base address */
2160         qmp->serdes = base;
2161
2162         /* per PHY dp_com; if PHY has dp_com control block */
2163         if (qmp->cfg->has_phy_dp_com_ctrl) {
2164                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2165                                                    "dp_com");
2166                 base = devm_ioremap_resource(dev, res);
2167                 if (IS_ERR(base))
2168                         return PTR_ERR(base);
2169
2170                 qmp->dp_com = base;
2171         }
2172
2173         mutex_init(&qmp->phy_mutex);
2174
2175         ret = qcom_qmp_phy_clk_init(dev);
2176         if (ret)
2177                 return ret;
2178
2179         ret = qcom_qmp_phy_reset_init(dev);
2180         if (ret)
2181                 return ret;
2182
2183         ret = qcom_qmp_phy_vreg_init(dev);
2184         if (ret) {
2185                 if (ret != -EPROBE_DEFER)
2186                         dev_err(dev, "failed to get regulator supplies: %d\n",
2187                                 ret);
2188                 return ret;
2189         }
2190
2191         num = of_get_available_child_count(dev->of_node);
2192         /* do we have a rogue child node ? */
2193         if (num > qmp->cfg->nlanes)
2194                 return -EINVAL;
2195
2196         qmp->phys = devm_kcalloc(dev, num, sizeof(*qmp->phys), GFP_KERNEL);
2197         if (!qmp->phys)
2198                 return -ENOMEM;
2199
2200         id = 0;
2201         pm_runtime_set_active(dev);
2202         pm_runtime_enable(dev);
2203         /*
2204          * Prevent runtime pm from being ON by default. Users can enable
2205          * it using power/control in sysfs.
2206          */
2207         pm_runtime_forbid(dev);
2208
2209         for_each_available_child_of_node(dev->of_node, child) {
2210                 /* Create per-lane phy */
2211                 ret = qcom_qmp_phy_create(dev, child, id);
2212                 if (ret) {
2213                         dev_err(dev, "failed to create lane%d phy, %d\n",
2214                                 id, ret);
2215                         goto err_node_put;
2216                 }
2217
2218                 /*
2219                  * Register the pipe clock provided by phy.
2220                  * See function description to see details of this pipe clock.
2221                  */
2222                 ret = phy_pipe_clk_register(qmp, child);
2223                 if (ret) {
2224                         dev_err(qmp->dev,
2225                                 "failed to register pipe clock source\n");
2226                         goto err_node_put;
2227                 }
2228                 id++;
2229         }
2230
2231         phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
2232         if (!IS_ERR(phy_provider))
2233                 dev_info(dev, "Registered Qcom-QMP phy\n");
2234         else
2235                 pm_runtime_disable(dev);
2236
2237         return PTR_ERR_OR_ZERO(phy_provider);
2238
2239 err_node_put:
2240         pm_runtime_disable(dev);
2241         of_node_put(child);
2242         return ret;
2243 }
2244
2245 static struct platform_driver qcom_qmp_phy_driver = {
2246         .probe          = qcom_qmp_phy_probe,
2247         .driver = {
2248                 .name   = "qcom-qmp-phy",
2249                 .pm     = &qcom_qmp_phy_pm_ops,
2250                 .of_match_table = qcom_qmp_phy_of_match_table,
2251         },
2252 };
2253
2254 module_platform_driver(qcom_qmp_phy_driver);
2255
2256 MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
2257 MODULE_DESCRIPTION("Qualcomm QMP PHY driver");
2258 MODULE_LICENSE("GPL v2");