1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author: Chris Zhong <zyw@rock-chips.com>
5 * Kever Yang <kever.yang@rock-chips.com>
6 *
7 * The ROCKCHIP Type-C PHY has two PLL clocks. The first PLL clock
8 * is used for USB3, the second PLL clock is used for DP. This Type-C PHY has
9 * 3 working modes: USB3 only mode, DP only mode, and USB3+DP mode.
10 * At USB3 only mode, both PLL clocks need to be initialized, this allows the
11 * PHY to switch mode between USB3 and USB3+DP, without disconnecting the USB
12 * device.
13 * In The DP only mode, only the DP PLL needs to be powered on, and the 4 lanes
14 * are all used for DP.
15 *
16 * This driver gets extcon cable state and property, then decides which mode to
17 * select:
18 *
19 * 1. USB3 only mode:
20 * EXTCON_USB or EXTCON_USB_HOST state is true, and
21 * EXTCON_PROP_USB_SS property is true.
22 * EXTCON_DISP_DP state is false.
23 *
24 * 2. DP only mode:
25 * EXTCON_DISP_DP state is true, and
26 * EXTCON_PROP_USB_SS property is false.
27 * If EXTCON_USB_HOST state is true, it is DP + USB2 mode, since the USB2 phy
28 * is a separate phy, so this case is still DP only mode.
29 *
30 * 3. USB3+DP mode:
31 * EXTCON_USB_HOST and EXTCON_DISP_DP are both true, and
32 * EXTCON_PROP_USB_SS property is true.
33 *
34 * This Type-C PHY driver supports normal and flip orientation. The orientation
35 * is reported by the EXTCON_PROP_USB_TYPEC_POLARITY property: true is flip
36 * orientation, false is normal orientation.
37 */
38
39 #include <linux/clk.h>
40 #include <linux/clk-provider.h>
41 #include <linux/delay.h>
42 #include <linux/extcon.h>
43 #include <linux/io.h>
44 #include <linux/iopoll.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/mutex.h>
48 #include <linux/of.h>
49 #include <linux/of_address.h>
50 #include <linux/of_platform.h>
51 #include <linux/platform_device.h>
52 #include <linux/regmap.h>
53 #include <linux/reset.h>
54
55 #include <linux/mfd/syscon.h>
56 #include <linux/phy/phy.h>
57 #include <linux/phy/phy-rockchip-typec.h>
58
59 #define CMN_SSM_BANDGAP (0x21 << 2)
60 #define CMN_SSM_BIAS (0x22 << 2)
61 #define CMN_PLLSM0_PLLEN (0x29 << 2)
62 #define CMN_PLLSM0_PLLPRE (0x2a << 2)
63 #define CMN_PLLSM0_PLLVREF (0x2b << 2)
64 #define CMN_PLLSM0_PLLLOCK (0x2c << 2)
65 #define CMN_PLLSM1_PLLEN (0x31 << 2)
66 #define CMN_PLLSM1_PLLPRE (0x32 << 2)
67 #define CMN_PLLSM1_PLLVREF (0x33 << 2)
68 #define CMN_PLLSM1_PLLLOCK (0x34 << 2)
69 #define CMN_PLLSM1_USER_DEF_CTRL (0x37 << 2)
70 #define CMN_ICAL_OVRD (0xc1 << 2)
71 #define CMN_PLL0_VCOCAL_OVRD (0x83 << 2)
72 #define CMN_PLL0_VCOCAL_INIT (0x84 << 2)
73 #define CMN_PLL0_VCOCAL_ITER (0x85 << 2)
74 #define CMN_PLL0_LOCK_REFCNT_START (0x90 << 2)
75 #define CMN_PLL0_LOCK_PLLCNT_START (0x92 << 2)
76 #define CMN_PLL0_LOCK_PLLCNT_THR (0x93 << 2)
77 #define CMN_PLL0_INTDIV (0x94 << 2)
78 #define CMN_PLL0_FRACDIV (0x95 << 2)
79 #define CMN_PLL0_HIGH_THR (0x96 << 2)
80 #define CMN_PLL0_DSM_DIAG (0x97 << 2)
81 #define CMN_PLL0_SS_CTRL1 (0x98 << 2)
82 #define CMN_PLL0_SS_CTRL2 (0x99 << 2)
83 #define CMN_PLL1_VCOCAL_START (0xa1 << 2)
84 #define CMN_PLL1_VCOCAL_OVRD (0xa3 << 2)
85 #define CMN_PLL1_VCOCAL_INIT (0xa4 << 2)
86 #define CMN_PLL1_VCOCAL_ITER (0xa5 << 2)
87 #define CMN_PLL1_LOCK_REFCNT_START (0xb0 << 2)
88 #define CMN_PLL1_LOCK_PLLCNT_START (0xb2 << 2)
89 #define CMN_PLL1_LOCK_PLLCNT_THR (0xb3 << 2)
90 #define CMN_PLL1_INTDIV (0xb4 << 2)
91 #define CMN_PLL1_FRACDIV (0xb5 << 2)
92 #define CMN_PLL1_HIGH_THR (0xb6 << 2)
93 #define CMN_PLL1_DSM_DIAG (0xb7 << 2)
94 #define CMN_PLL1_SS_CTRL1 (0xb8 << 2)
95 #define CMN_PLL1_SS_CTRL2 (0xb9 << 2)
96 #define CMN_RXCAL_OVRD (0xd1 << 2)
97
98 #define CMN_TXPUCAL_CTRL (0xe0 << 2)
99 #define CMN_TXPUCAL_OVRD (0xe1 << 2)
100 #define CMN_TXPDCAL_CTRL (0xf0 << 2)
101 #define CMN_TXPDCAL_OVRD (0xf1 << 2)
102
103 /* For CMN_TXPUCAL_CTRL, CMN_TXPDCAL_CTRL */
104 #define CMN_TXPXCAL_START BIT(15)
105 #define CMN_TXPXCAL_DONE BIT(14)
106 #define CMN_TXPXCAL_NO_RESPONSE BIT(13)
107 #define CMN_TXPXCAL_CURRENT_RESPONSE BIT(12)
108
109 #define CMN_TXPU_ADJ_CTRL (0x108 << 2)
110 #define CMN_TXPD_ADJ_CTRL (0x10c << 2)
111
112 /*
113 * For CMN_TXPUCAL_CTRL, CMN_TXPDCAL_CTRL,
114 * CMN_TXPU_ADJ_CTRL, CMN_TXPDCAL_CTRL
115 *
116 * NOTE: some of these registers are documented to be 2's complement
117 * signed numbers, but then documented to be always positive. Weird.
118 * In such a case, using CMN_CALIB_CODE_POS() avoids the unnecessary
119 * sign extension.
120 */
121 #define CMN_CALIB_CODE_WIDTH 7
122 #define CMN_CALIB_CODE_OFFSET 0
123 #define CMN_CALIB_CODE_MASK GENMASK(CMN_CALIB_CODE_WIDTH, 0)
124 #define CMN_CALIB_CODE(x) sign_extend32((x) >> CMN_CALIB_CODE_OFFSET, CMN_CALIB_CODE_WIDTH)
125
126 #define CMN_CALIB_CODE_POS_MASK GENMASK(CMN_CALIB_CODE_WIDTH - 1, 0)
127 #define CMN_CALIB_CODE_POS(x) (((x) >> CMN_CALIB_CODE_OFFSET) & CMN_CALIB_CODE_POS_MASK)
128
129 #define CMN_DIAG_PLL0_FBH_OVRD (0x1c0 << 2)
130 #define CMN_DIAG_PLL0_FBL_OVRD (0x1c1 << 2)
131 #define CMN_DIAG_PLL0_OVRD (0x1c2 << 2)
132 #define CMN_DIAG_PLL0_V2I_TUNE (0x1c5 << 2)
133 #define CMN_DIAG_PLL0_CP_TUNE (0x1c6 << 2)
134 #define CMN_DIAG_PLL0_LF_PROG (0x1c7 << 2)
135 #define CMN_DIAG_PLL1_FBH_OVRD (0x1d0 << 2)
136 #define CMN_DIAG_PLL1_FBL_OVRD (0x1d1 << 2)
137 #define CMN_DIAG_PLL1_OVRD (0x1d2 << 2)
138 #define CMN_DIAG_PLL1_V2I_TUNE (0x1d5 << 2)
139 #define CMN_DIAG_PLL1_CP_TUNE (0x1d6 << 2)
140 #define CMN_DIAG_PLL1_LF_PROG (0x1d7 << 2)
141 #define CMN_DIAG_PLL1_PTATIS_TUNE1 (0x1d8 << 2)
142 #define CMN_DIAG_PLL1_PTATIS_TUNE2 (0x1d9 << 2)
143 #define CMN_DIAG_PLL1_INCLK_CTRL (0x1da << 2)
144 #define CMN_DIAG_HSCLK_SEL (0x1e0 << 2)
145
146 #define XCVR_PSM_RCTRL(n) ((0x4001 | ((n) << 9)) << 2)
147 #define XCVR_PSM_CAL_TMR(n) ((0x4002 | ((n) << 9)) << 2)
148 #define XCVR_PSM_A0IN_TMR(n) ((0x4003 | ((n) << 9)) << 2)
149 #define TX_TXCC_CAL_SCLR_MULT(n) ((0x4047 | ((n) << 9)) << 2)
150 #define TX_TXCC_CPOST_MULT_00(n) ((0x404c | ((n) << 9)) << 2)
151 #define TX_TXCC_CPOST_MULT_01(n) ((0x404d | ((n) << 9)) << 2)
152 #define TX_TXCC_CPOST_MULT_10(n) ((0x404e | ((n) << 9)) << 2)
153 #define TX_TXCC_CPOST_MULT_11(n) ((0x404f | ((n) << 9)) << 2)
154 #define TX_TXCC_MGNFS_MULT_000(n) ((0x4050 | ((n) << 9)) << 2)
155 #define TX_TXCC_MGNFS_MULT_001(n) ((0x4051 | ((n) << 9)) << 2)
156 #define TX_TXCC_MGNFS_MULT_010(n) ((0x4052 | ((n) << 9)) << 2)
157 #define TX_TXCC_MGNFS_MULT_011(n) ((0x4053 | ((n) << 9)) << 2)
158 #define TX_TXCC_MGNFS_MULT_100(n) ((0x4054 | ((n) << 9)) << 2)
159 #define TX_TXCC_MGNFS_MULT_101(n) ((0x4055 | ((n) << 9)) << 2)
160 #define TX_TXCC_MGNFS_MULT_110(n) ((0x4056 | ((n) << 9)) << 2)
161 #define TX_TXCC_MGNFS_MULT_111(n) ((0x4057 | ((n) << 9)) << 2)
162 #define TX_TXCC_MGNLS_MULT_000(n) ((0x4058 | ((n) << 9)) << 2)
163 #define TX_TXCC_MGNLS_MULT_001(n) ((0x4059 | ((n) << 9)) << 2)
164 #define TX_TXCC_MGNLS_MULT_010(n) ((0x405a | ((n) << 9)) << 2)
165 #define TX_TXCC_MGNLS_MULT_011(n) ((0x405b | ((n) << 9)) << 2)
166 #define TX_TXCC_MGNLS_MULT_100(n) ((0x405c | ((n) << 9)) << 2)
167 #define TX_TXCC_MGNLS_MULT_101(n) ((0x405d | ((n) << 9)) << 2)
168 #define TX_TXCC_MGNLS_MULT_110(n) ((0x405e | ((n) << 9)) << 2)
169 #define TX_TXCC_MGNLS_MULT_111(n) ((0x405f | ((n) << 9)) << 2)
170
171 #define XCVR_DIAG_PLLDRC_CTRL(n) ((0x40e0 | ((n) << 9)) << 2)
172 #define XCVR_DIAG_BIDI_CTRL(n) ((0x40e8 | ((n) << 9)) << 2)
173 #define XCVR_DIAG_LANE_FCM_EN_MGN(n) ((0x40f2 | ((n) << 9)) << 2)
174 #define TX_PSC_A0(n) ((0x4100 | ((n) << 9)) << 2)
175 #define TX_PSC_A1(n) ((0x4101 | ((n) << 9)) << 2)
176 #define TX_PSC_A2(n) ((0x4102 | ((n) << 9)) << 2)
177 #define TX_PSC_A3(n) ((0x4103 | ((n) << 9)) << 2)
178 #define TX_RCVDET_CTRL(n) ((0x4120 | ((n) << 9)) << 2)
179 #define TX_RCVDET_EN_TMR(n) ((0x4122 | ((n) << 9)) << 2)
180 #define TX_RCVDET_ST_TMR(n) ((0x4123 | ((n) << 9)) << 2)
181 #define TX_DIAG_TX_DRV(n) ((0x41e1 | ((n) << 9)) << 2)
182 #define TX_DIAG_BGREF_PREDRV_DELAY (0x41e7 << 2)
183
184 /* Use this for "n" in macros like "_MULT_XXX" to target the aux channel */
185 #define AUX_CH_LANE 8
186
187 #define TX_ANA_CTRL_REG_1 (0x5020 << 2)
188
189 #define TXDA_DP_AUX_EN BIT(15)
190 #define AUXDA_SE_EN BIT(14)
191 #define TXDA_CAL_LATCH_EN BIT(13)
192 #define AUXDA_POLARITY BIT(12)
193 #define TXDA_DRV_POWER_ISOLATION_EN BIT(11)
194 #define TXDA_DRV_POWER_EN_PH_2_N BIT(10)
195 #define TXDA_DRV_POWER_EN_PH_1_N BIT(9)
196 #define TXDA_BGREF_EN BIT(8)
197 #define TXDA_DRV_LDO_EN BIT(7)
198 #define TXDA_DECAP_EN_DEL BIT(6)
199 #define TXDA_DECAP_EN BIT(5)
200 #define TXDA_UPHY_SUPPLY_EN_DEL BIT(4)
201 #define TXDA_UPHY_SUPPLY_EN BIT(3)
202 #define TXDA_LOW_LEAKAGE_EN BIT(2)
203 #define TXDA_DRV_IDLE_LOWI_EN BIT(1)
204 #define TXDA_DRV_CMN_MODE_EN BIT(0)
205
206 #define TX_ANA_CTRL_REG_2 (0x5021 << 2)
207
208 #define AUXDA_DEBOUNCING_CLK BIT(15)
209 #define TXDA_LPBK_RECOVERED_CLK_EN BIT(14)
210 #define TXDA_LPBK_ISI_GEN_EN BIT(13)
211 #define TXDA_LPBK_SERIAL_EN BIT(12)
212 #define TXDA_LPBK_LINE_EN BIT(11)
213 #define TXDA_DRV_LDO_REDC_SINKIQ BIT(10)
214 #define XCVR_DECAP_EN_DEL BIT(9)
215 #define XCVR_DECAP_EN BIT(8)
216 #define TXDA_MPHY_ENABLE_HS_NT BIT(7)
217 #define TXDA_MPHY_SA_MODE BIT(6)
218 #define TXDA_DRV_LDO_RBYR_FB_EN BIT(5)
219 #define TXDA_DRV_RST_PULL_DOWN BIT(4)
220 #define TXDA_DRV_LDO_BG_FB_EN BIT(3)
221 #define TXDA_DRV_LDO_BG_REF_EN BIT(2)
222 #define TXDA_DRV_PREDRV_EN_DEL BIT(1)
223 #define TXDA_DRV_PREDRV_EN BIT(0)
224
225 #define TXDA_COEFF_CALC_CTRL (0x5022 << 2)
226
227 #define TX_HIGH_Z BIT(6)
228 #define TX_VMARGIN_OFFSET 3
229 #define TX_VMARGIN_MASK 0x7
230 #define LOW_POWER_SWING_EN BIT(2)
231 #define TX_FCM_DRV_MAIN_EN BIT(1)
232 #define TX_FCM_FULL_MARGIN BIT(0)
233
234 #define TX_DIG_CTRL_REG_2 (0x5024 << 2)
235
236 #define TX_HIGH_Z_TM_EN BIT(15)
237 #define TX_RESCAL_CODE_OFFSET 0
238 #define TX_RESCAL_CODE_MASK 0x3f
239
240 #define TXDA_CYA_AUXDA_CYA (0x5025 << 2)
241 #define TX_ANA_CTRL_REG_3 (0x5026 << 2)
242 #define TX_ANA_CTRL_REG_4 (0x5027 << 2)
243 #define TX_ANA_CTRL_REG_5 (0x5029 << 2)
244
245 #define RX_PSC_A0(n) ((0x8000 | ((n) << 9)) << 2)
246 #define RX_PSC_A1(n) ((0x8001 | ((n) << 9)) << 2)
247 #define RX_PSC_A2(n) ((0x8002 | ((n) << 9)) << 2)
248 #define RX_PSC_A3(n) ((0x8003 | ((n) << 9)) << 2)
249 #define RX_PSC_CAL(n) ((0x8006 | ((n) << 9)) << 2)
250 #define RX_PSC_RDY(n) ((0x8007 | ((n) << 9)) << 2)
251 #define RX_IQPI_ILL_CAL_OVRD (0x8023 << 2)
252 #define RX_EPI_ILL_CAL_OVRD (0x8033 << 2)
253 #define RX_SDCAL0_OVRD (0x8041 << 2)
254 #define RX_SDCAL1_OVRD (0x8049 << 2)
255 #define RX_SLC_INIT (0x806d << 2)
256 #define RX_SLC_RUN (0x806e << 2)
257 #define RX_CDRLF_CNFG2 (0x8081 << 2)
258 #define RX_SIGDET_HL_FILT_TMR(n) ((0x8090 | ((n) << 9)) << 2)
259 #define RX_SLC_IOP0_OVRD (0x8101 << 2)
260 #define RX_SLC_IOP1_OVRD (0x8105 << 2)
261 #define RX_SLC_QOP0_OVRD (0x8109 << 2)
262 #define RX_SLC_QOP1_OVRD (0x810d << 2)
263 #define RX_SLC_EOP0_OVRD (0x8111 << 2)
264 #define RX_SLC_EOP1_OVRD (0x8115 << 2)
265 #define RX_SLC_ION0_OVRD (0x8119 << 2)
266 #define RX_SLC_ION1_OVRD (0x811d << 2)
267 #define RX_SLC_QON0_OVRD (0x8121 << 2)
268 #define RX_SLC_QON1_OVRD (0x8125 << 2)
269 #define RX_SLC_EON0_OVRD (0x8129 << 2)
270 #define RX_SLC_EON1_OVRD (0x812d << 2)
271 #define RX_SLC_IEP0_OVRD (0x8131 << 2)
272 #define RX_SLC_IEP1_OVRD (0x8135 << 2)
273 #define RX_SLC_QEP0_OVRD (0x8139 << 2)
274 #define RX_SLC_QEP1_OVRD (0x813d << 2)
275 #define RX_SLC_EEP0_OVRD (0x8141 << 2)
276 #define RX_SLC_EEP1_OVRD (0x8145 << 2)
277 #define RX_SLC_IEN0_OVRD (0x8149 << 2)
278 #define RX_SLC_IEN1_OVRD (0x814d << 2)
279 #define RX_SLC_QEN0_OVRD (0x8151 << 2)
280 #define RX_SLC_QEN1_OVRD (0x8155 << 2)
281 #define RX_SLC_EEN0_OVRD (0x8159 << 2)
282 #define RX_SLC_EEN1_OVRD (0x815d << 2)
283 #define RX_REE_CTRL_DATA_MASK(n) ((0x81bb | ((n) << 9)) << 2)
284 #define RX_DIAG_SIGDET_TUNE(n) ((0x81dc | ((n) << 9)) << 2)
285 #define RX_DIAG_SC2C_DELAY (0x81e1 << 2)
286
287 #define PHY_PMA_LANE_CFG (0xc000 << 2)
288 #define PMA_LANE3_DP_LANE_SEL(x) (((x)&0x3) << 14)
289 #define PMA_LANE3_INTERFACE_SEL(x) (((x)&0x1) << 12)
290 #define PMA_LANE2_DP_LANE_SEL(x) (((x)&0x3) << 10)
291 #define PMA_LANE2_INTERFACE_SEL(x) (((x)&0x1) << 8)
292 #define PMA_LANE1_DP_LANE_SEL(x) (((x)&0x3) << 6)
293 #define PMA_LANE1_INTERFACE_SEL(x) (((x)&0x1) << 4)
294 #define PMA_LANE0_DP_LANE_SEL(x) (((x)&0x3) << 2)
295 #define PMA_LANE0_INTERFACE_SEL(x) (((x)&0x1) << 0)
296 #define PIPE_CMN_CTRL1 (0xc001 << 2)
297 #define PIPE_CMN_CTRL2 (0xc002 << 2)
298 #define PIPE_COM_LOCK_CFG1 (0xc003 << 2)
299 #define PIPE_COM_LOCK_CFG2 (0xc004 << 2)
300 #define PIPE_RCV_DET_INH (0xc005 << 2)
301 #define PHY_DP_MODE_CTL (0xc008 << 2)
302 #define PHY_DP_LANE_DISABLE GENMASK(15, 12)
303 #define PHY_DP_LANE_3_DISABLE BIT(15)
304 #define PHY_DP_LANE_2_DISABLE BIT(14)
305 #define PHY_DP_LANE_1_DISABLE BIT(13)
306 #define PHY_DP_LANE_0_DISABLE BIT(12)
307 #define PHY_DP_POWER_STATE_ACK_MASK GENMASK(7, 4)
308 #define PHY_DP_POWER_STATE_ACK_SHIFT 4
309 #define PHY_DP_POWER_STATE_MASK GENMASK(3, 0)
310 #define PHY_DP_CLK_CTL (0xc009 << 2)
311 #define DP_PLL_CLOCK_ENABLE_ACK BIT(3)
312 #define DP_PLL_CLOCK_ENABLE_MASK BIT(2)
313 #define DP_PLL_CLOCK_DISABLE 0
314 #define DP_PLL_READY BIT(1)
315 #define DP_PLL_ENABLE_MASK BIT(0)
316 #define DP_PLL_ENABLE BIT(0)
317 #define DP_PLL_DISABLE 0
318 #define DP_CLK_CTL (0xc009 << 2)
319 #define STS (0xc00F << 2)
320 #define PHY_ISO_CMN_CTRL (0xc010 << 2)
321 #define PHY_DP_TX_CTL (0xc408 << 2)
322 #define PMA_CMN_CTRL1 (0xc800 << 2)
323 #define PHY_PMA_ISO_CMN_CTRL (0xc810 << 2)
324 #define PHY_ISOLATION_CTRL (0xc81f << 2)
325 #define PHY_PMA_ISO_XCVR_CTRL(n) ((0xcc11 | ((n) << 6)) << 2)
326 #define PHY_PMA_ISO_LINK_MODE(n) ((0xcc12 | ((n) << 6)) << 2)
327 #define PHY_PMA_ISO_PWRST_CTRL(n) ((0xcc13 | ((n) << 6)) << 2)
328 #define PHY_PMA_ISO_TX_DATA_LO(n) ((0xcc14 | ((n) << 6)) << 2)
329 #define PHY_PMA_ISO_TX_DATA_HI(n) ((0xcc15 | ((n) << 6)) << 2)
330 #define PHY_PMA_ISO_RX_DATA_LO(n) ((0xcc16 | ((n) << 6)) << 2)
331 #define PHY_PMA_ISO_RX_DATA_HI(n) ((0xcc17 | ((n) << 6)) << 2)
332 #define TX_BIST_CTRL(n) ((0x4140 | ((n) << 9)) << 2)
333 #define TX_BIST_UDDWR(n) ((0x4141 | ((n) << 9)) << 2)
334
335 /*
336 * Selects which PLL clock will be driven on the analog high speed
337 * clock 0: PLL 0 div 1
338 * clock 1: PLL 1 div 2
339 */
340 #define CLK_PLL1_DIV1 0x20
341 #define CLK_PLL1_DIV2 0x30
342 #define CLK_PLL_MASK 0x33
343
344 #define CMN_READY BIT(0)
345
346 #define DP_PLL_CLOCK_ENABLE_ACK BIT(3)
347 #define DP_PLL_CLOCK_ENABLE BIT(2)
348 #define DP_PLL_ENABLE_ACK BIT(1)
349 #define DP_PLL_ENABLE BIT(0)
350 #define DP_PLL_DATA_RATE_RBR ((2 << 12) | (4 << 8))
351 #define DP_PLL_DATA_RATE_HBR ((2 << 12) | (4 << 8))
352 #define DP_PLL_DATA_RATE_HBR2 ((1 << 12) | (2 << 8))
353 #define DP_PLL_DATA_RATE_MASK 0xff00
354
355 #define DP_MODE_MASK 0xf
356 #define DP_MODE_ENTER_A0 BIT(0)
357 #define DP_MODE_ENTER_A2 BIT(2)
358 #define DP_MODE_ENTER_A3 BIT(3)
359 #define DP_MODE_A0_ACK BIT(4)
360 #define DP_MODE_A2_ACK BIT(6)
361 #define DP_MODE_A3_ACK BIT(7)
362 #define DP_LINK_RESET_DEASSERTED BIT(8)
363
364 #define PHY_MODE_SET_TIMEOUT 100000
365
366 #define PIN_ASSIGN_C_E 0x51d9
367 #define PIN_ASSIGN_D_F 0x5100
368
369 #define MODE_DISCONNECT 0
370 #define MODE_UFP_USB BIT(0)
371 #define MODE_DFP_USB BIT(1)
372 #define MODE_DFP_DP BIT(2)
373
374 #define DP_DEFAULT_RATE 162000
375
376 #define POWER_ON_TRIES 5
377
378 struct usb3phy_reg {
379 u32 offset;
380 u32 enable_bit;
381 u32 write_enable;
382 };
383
384 /**
385 * struct rockchip_usb3phy_port_cfg - usb3-phy port configuration.
386 * @reg: the base address for usb3-phy config.
387 * @typec_conn_dir: the register of type-c connector direction.
388 * @usb3tousb2_en: the register of type-c force usb2 to usb2 enable.
389 * @external_psm: the register of type-c phy external psm clock.
390 * @pipe_status: the register of type-c phy pipe status.
391 * @usb3_host_disable: the register of type-c usb3 host disable.
392 * @usb3_host_port: the register of type-c usb3 host port.
393 * @uphy_dp_sel: the register of type-c phy DP select control.
394 */
395 struct rockchip_usb3phy_port_cfg {
396 unsigned int reg;
397 struct usb3phy_reg typec_conn_dir;
398 struct usb3phy_reg usb3tousb2_en;
399 struct usb3phy_reg external_psm;
400 struct usb3phy_reg pipe_status;
401 struct usb3phy_reg usb3_host_disable;
402 struct usb3phy_reg usb3_host_port;
403 struct usb3phy_reg uphy_dp_sel;
404 };
405
406 struct phy_config {
407 int swing;
408 int pe;
409 };
410
411 struct rockchip_typec_phy {
412 struct device *dev;
413 void __iomem *base;
414 struct extcon_dev *extcon;
415 struct regmap *grf_regs;
416 struct clk *clk_core;
417 struct clk *clk_ref;
418 struct reset_control *uphy_rst;
419 struct reset_control *pipe_rst;
420 struct reset_control *tcphy_rst;
421 const struct rockchip_usb3phy_port_cfg *port_cfgs;
422 /* mutex to protect access to individual PHYs */
423 struct mutex lock;
424
425 bool flip;
426 u8 mode;
427 struct phy_config config[3][4];
428 };
429
430 struct phy_reg {
431 u16 value;
432 u32 addr;
433 };
434
435 static struct phy_reg usb3_pll_cfg[] = {
436 {0xf0, CMN_PLL0_VCOCAL_INIT}, {0x18, CMN_PLL0_VCOCAL_ITER}, {0xd0, CMN_PLL0_INTDIV},
437 {0x4a4a, CMN_PLL0_FRACDIV}, {0x34, CMN_PLL0_HIGH_THR}, {0x1ee, CMN_PLL0_SS_CTRL1},
438 {0x7f03, CMN_PLL0_SS_CTRL2}, {0x20, CMN_PLL0_DSM_DIAG}, {0, CMN_DIAG_PLL0_OVRD},
439 {0, CMN_DIAG_PLL0_FBH_OVRD}, {0, CMN_DIAG_PLL0_FBL_OVRD}, {0x7, CMN_DIAG_PLL0_V2I_TUNE},
440 {0x45, CMN_DIAG_PLL0_CP_TUNE}, {0x8, CMN_DIAG_PLL0_LF_PROG},
441 };
442
443 static const struct phy_reg dp_pll_rbr_cfg[] = {
444 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b9, CMN_PLL1_VCOCAL_START},
445 {0x0087, CMN_PLL1_INTDIV}, {0x0000, CMN_PLL1_FRACDIV}, {0x0022, CMN_PLL1_HIGH_THR},
446 {0x8000, CMN_PLL1_SS_CTRL1}, {0x0000, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
447 {0x0000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
448 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0006, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
449 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0100, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0007, CMN_DIAG_PLL1_PTATIS_TUNE2},
450 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
451 };
452
453 static const struct phy_reg dp_pll_rbr_ssc_cfg[] = {
454 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b9, CMN_PLL1_VCOCAL_START},
455 {0x0086, CMN_PLL1_INTDIV}, {0xf915, CMN_PLL1_FRACDIV}, {0x0022, CMN_PLL1_HIGH_THR},
456 {0x0140, CMN_PLL1_SS_CTRL1}, {0x7f03, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
457 {0x0000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
458 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0006, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
459 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0100, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0007, CMN_DIAG_PLL1_PTATIS_TUNE2},
460 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
461 };
462
463 static const struct phy_reg dp_pll_hbr_cfg[] = {
464 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b4, CMN_PLL1_VCOCAL_START},
465 {0x00e1, CMN_PLL1_INTDIV}, {0x0000, CMN_PLL1_FRACDIV}, {0x0005, CMN_PLL1_HIGH_THR},
466 {0x8000, CMN_PLL1_SS_CTRL1}, {0x0000, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
467 {0x1000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
468 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0007, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
469 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2},
470 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
471 };
472
473 static const struct phy_reg dp_pll_hbr_ssc_cfg[] = {
474 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b4, CMN_PLL1_VCOCAL_START},
475 {0x00e0, CMN_PLL1_INTDIV}, {0xf479, CMN_PLL1_FRACDIV}, {0x0038, CMN_PLL1_HIGH_THR},
476 {0x0204, CMN_PLL1_SS_CTRL1}, {0x7f03, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
477 {0x1000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
478 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0007, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
479 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2},
480 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
481 };
482
483 static const struct phy_reg dp_pll_hbr2_cfg[] = {
484 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b4, CMN_PLL1_VCOCAL_START},
485 {0x00e1, CMN_PLL1_INTDIV}, {0x0000, CMN_PLL1_FRACDIV}, {0x0005, CMN_PLL1_HIGH_THR},
486 {0x8000, CMN_PLL1_SS_CTRL1}, {0x0000, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
487 {0x1000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
488 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0007, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
489 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2},
490 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
491 };
492
493 static const struct phy_reg dp_pll_hbr2_ssc_cfg[] = {
494 {0x00f0, CMN_PLL1_VCOCAL_INIT}, {0x0018, CMN_PLL1_VCOCAL_ITER}, {0x30b4, CMN_PLL1_VCOCAL_START},
495 {0x00e0, CMN_PLL1_INTDIV}, {0xf479, CMN_PLL1_FRACDIV}, {0x0038, CMN_PLL1_HIGH_THR},
496 {0x0204, CMN_PLL1_SS_CTRL1}, {0x7f03, CMN_PLL1_SS_CTRL2}, {0x0020, CMN_PLL1_DSM_DIAG},
497 {0x1000, CMN_PLLSM1_USER_DEF_CTRL}, {0x0000, CMN_DIAG_PLL1_OVRD}, {0x0000, CMN_DIAG_PLL1_FBH_OVRD},
498 {0x0000, CMN_DIAG_PLL1_FBL_OVRD}, {0x0007, CMN_DIAG_PLL1_V2I_TUNE}, {0x0045, CMN_DIAG_PLL1_CP_TUNE},
499 {0x0008, CMN_DIAG_PLL1_LF_PROG}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE1}, {0x0001, CMN_DIAG_PLL1_PTATIS_TUNE2},
500 {0x0001, CMN_DIAG_PLL1_INCLK_CTRL},
501 };
502
503 static const struct rockchip_usb3phy_port_cfg rk3399_usb3phy_port_cfgs[] = {
504 {
505 .reg = 0xff7c0000,
506 .typec_conn_dir = {0xe580, 0, 16},
507 .usb3tousb2_en = {0xe580, 3, 19},
508 .external_psm = {0xe588, 14, 30},
509 .pipe_status = {0xe5c0, 0, 0},
510 .usb3_host_disable = {0x2434, 0, 16},
511 .usb3_host_port = {0x2434, 12, 28},
512 .uphy_dp_sel = {0x6268, 19, 19},
513 },
514 {
515 .reg = 0xff800000,
516 .typec_conn_dir = {0xe58c, 0, 16},
517 .usb3tousb2_en = {0xe58c, 3, 19},
518 .external_psm = {0xe594, 14, 30},
519 .pipe_status = {0xe5c0, 16, 16},
520 .usb3_host_disable = {0x2444, 0, 16},
521 .usb3_host_port = {0x2444, 12, 28},
522 .uphy_dp_sel = {0x6268, 3, 19},
523 },
524 {}
525 };
526
527 /* default phy config */
528 static const struct phy_config tcphy_default_config[3][4] = {
529 {{.swing = 0x2a, .pe = 0x00},
530 {.swing = 0x1f, .pe = 0x15},
531 {.swing = 0x14, .pe = 0x22},
532 {.swing = 0x02, .pe = 0x2b}},
533
534 {{.swing = 0x21, .pe = 0x00}, {.swing = 0x12, .pe = 0x15}, {.swing = 0x02, .pe = 0x22}, {.swing = 0, .pe = 0}},
535
536 {{.swing = 0x15, .pe = 0x00}, {.swing = 0x00, .pe = 0x15}, {.swing = 0, .pe = 0}, {.swing = 0, .pe = 0}},
537 };
538
539 enum phy_dp_power_state {
540 PHY_DP_POWER_STATE_DISABLED = -1,
541 PHY_DP_POWER_STATE_A0,
542 PHY_DP_POWER_STATE_A1,
543 PHY_DP_POWER_STATE_A2,
544 PHY_DP_POWER_STATE_A3,
545 };
546
tcphy_dp_set_power_state(struct rockchip_typec_phy * tcphy,enum phy_dp_power_state state)547 static int tcphy_dp_set_power_state(struct rockchip_typec_phy *tcphy, enum phy_dp_power_state state)
548 {
549 u32 ack, reg, sts = BIT(state);
550 int ret;
551
552 /*
553 * Power state changes must not be requested until after the cmn_ready
554 * signal has gone active.
555 */
556 reg = readl(tcphy->base + PMA_CMN_CTRL1);
557 if (!(reg & CMN_READY)) {
558 dev_err(tcphy->dev, "cmn_ready in the inactive state\n");
559 return -EINVAL;
560 }
561
562 reg = readl(tcphy->base + PHY_DP_MODE_CTL);
563 reg &= ~PHY_DP_POWER_STATE_MASK;
564 reg |= sts;
565 writel(reg, tcphy->base + PHY_DP_MODE_CTL);
566
567 ret = readl_poll_timeout(tcphy->base + PHY_DP_MODE_CTL, ack,
568 (((ack & PHY_DP_POWER_STATE_ACK_MASK) >> PHY_DP_POWER_STATE_ACK_SHIFT) == sts), 0xa,
569 PHY_MODE_SET_TIMEOUT);
570 if (ret < 0) {
571 dev_err(tcphy->dev, "failed to enter power state %d\n", state);
572 return ret;
573 }
574
575 return 0;
576 }
577
578 enum {
579 PHY_DP_LANE_0,
580 PHY_DP_LANE_1,
581 PHY_DP_LANE_2,
582 PHY_DP_LANE_3,
583 };
584
585 enum {
586 PMA_IF_PIPE_PCS,
587 PMA_IF_PHY_DP,
588 };
589
590 /*
591 * For the TypeC PHY, the 4 lanes are mapping to the USB TypeC receptacle pins
592 * as follows
593 * -------------------------------------------------------------------
594 * PHY Lanes/Module Pins TypeC Receptacle Pins
595 * -------------------------------------------------------------------
596 * Lane0 (tx_p/m_ln_0) TX1+/TX1- (pins A2/A3)
597 * Lane1 (tx_rx_p/m_ln_1) RX1+/RX1- (pins B11/B10)
598 * Lane2 (tx_rx_p/m_ln_2) RX2+/RX2- (pins A11/A10)
599 * Lane3 (tx_p/m_ln_3) TX2+/TX2- (pins B2/B3)
600 * -------------------------------------------------------------------
601 *
602 * USB and DP lanes mapping to TypeC PHY lanes for each of pin assignment
603 * options (normal connector orientation) described in the VESA DisplayPort
604 * Alt Mode on USB TypeC Standard as follows:
605 *
606 * ----------------------------------------------------------------------
607 * PHY Lanes A B C D E F
608 * ----------------------------------------------------------------------
609 * 0 ML1 SSTX ML2 SSTX ML2 SSTX
610 * 1 ML3 SSRX ML3 SSRX ML3 SSRX
611 * 2 ML2 ML1 ML0 ML0 ML0 ML0
612 * 3 ML0 ML0 ML1 ML1 ML1 ML1
613 * ----------------------------------------------------------------------
614 */
tcphy_set_lane_mapping(struct rockchip_typec_phy * tcphy,u8 mode)615 static void tcphy_set_lane_mapping(struct rockchip_typec_phy *tcphy, u8 mode)
616 {
617 /*
618 * The PHY_PMA_LANE_CFG register is used to select whether a PMA lane
619 * is mapped for USB or PHY DP. The PHY_PMA_LANE_CFG register is
620 * configured based on a normal connector orientation. Logic in the
621 * PHY automatically handles the flipped connector case based on the
622 * setting of orientation of TypeC PHY.
623 */
624 if (mode == MODE_DFP_DP) {
625 /* This maps to VESA DP Alt Mode pin assignments C and E. */
626 writel(PMA_LANE3_DP_LANE_SEL(PHY_DP_LANE_1) | PMA_LANE3_INTERFACE_SEL(PMA_IF_PHY_DP) |
627 PMA_LANE2_DP_LANE_SEL(PHY_DP_LANE_0) | PMA_LANE2_INTERFACE_SEL(PMA_IF_PHY_DP) |
628 PMA_LANE1_DP_LANE_SEL(PHY_DP_LANE_3) | PMA_LANE1_INTERFACE_SEL(PMA_IF_PHY_DP) |
629 PMA_LANE0_DP_LANE_SEL(PHY_DP_LANE_2) | PMA_LANE0_INTERFACE_SEL(PMA_IF_PHY_DP),
630 tcphy->base + PHY_PMA_LANE_CFG);
631 } else {
632 /* This maps to VESA DP Alt Mode pin assignments D and F. */
633 writel(PMA_LANE3_DP_LANE_SEL(PHY_DP_LANE_1) | PMA_LANE3_INTERFACE_SEL(PMA_IF_PHY_DP) |
634 PMA_LANE2_DP_LANE_SEL(PHY_DP_LANE_0) | PMA_LANE2_INTERFACE_SEL(PMA_IF_PHY_DP) |
635 PMA_LANE1_INTERFACE_SEL(PMA_IF_PIPE_PCS) | PMA_LANE0_INTERFACE_SEL(PMA_IF_PIPE_PCS),
636 tcphy->base + PHY_PMA_LANE_CFG);
637 }
638 }
639
tcphy_cfg_24m(struct rockchip_typec_phy * tcphy)640 static void tcphy_cfg_24m(struct rockchip_typec_phy *tcphy)
641 {
642 u32 i, rdata;
643
644 /*
645 * cmn_ref_clk_sel = 3, select the 24Mhz for clk parent
646 * cmn_psm_clk_dig_div = 2, set the clk division to 2
647 */
648 writel(0x830, tcphy->base + PMA_CMN_CTRL1);
649 for (i = 0; i < 0x4; i++) {
650 /*
651 * The following PHY configuration assumes a 24 MHz reference
652 * clock.
653 */
654 writel(0x90, tcphy->base + XCVR_DIAG_LANE_FCM_EN_MGN(i));
655 writel(0x960, tcphy->base + TX_RCVDET_EN_TMR(i));
656 writel(0x30, tcphy->base + TX_RCVDET_ST_TMR(i));
657 }
658
659 rdata = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
660 rdata &= ~CLK_PLL_MASK;
661 rdata |= CLK_PLL1_DIV2;
662 writel(rdata, tcphy->base + CMN_DIAG_HSCLK_SEL);
663 }
664
tcphy_cfg_usb3_pll(struct rockchip_typec_phy * tcphy)665 static void tcphy_cfg_usb3_pll(struct rockchip_typec_phy *tcphy)
666 {
667 u32 i;
668
669 /* load the configuration of PLL0 */
670 for (i = 0; i < ARRAY_SIZE(usb3_pll_cfg); i++) {
671 writel(usb3_pll_cfg[i].value, tcphy->base + usb3_pll_cfg[i].addr);
672 }
673 }
674
tcphy_cfg_dp_pll(struct rockchip_typec_phy * tcphy,int link_rate)675 static void tcphy_cfg_dp_pll(struct rockchip_typec_phy *tcphy, int link_rate)
676 {
677 const struct phy_reg *phy_cfg;
678 u32 clk_ctrl;
679 u32 i, cfg_size, hsclk_sel;
680
681 hsclk_sel = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
682 hsclk_sel &= ~CLK_PLL_MASK;
683
684 switch (link_rate) {
685 case 0x83d60:
686 clk_ctrl = DP_PLL_DATA_RATE_HBR2;
687 hsclk_sel |= CLK_PLL1_DIV1;
688 phy_cfg = dp_pll_hbr2_cfg;
689 cfg_size = ARRAY_SIZE(dp_pll_hbr2_cfg);
690 break;
691 case 0x41eb0:
692 clk_ctrl = DP_PLL_DATA_RATE_HBR;
693 hsclk_sel |= CLK_PLL1_DIV2;
694 phy_cfg = dp_pll_hbr_cfg;
695 cfg_size = ARRAY_SIZE(dp_pll_hbr_cfg);
696 break;
697 case 0x278d0:
698 default:
699 clk_ctrl = DP_PLL_DATA_RATE_RBR;
700 hsclk_sel |= CLK_PLL1_DIV2;
701 phy_cfg = dp_pll_rbr_cfg;
702 cfg_size = ARRAY_SIZE(dp_pll_rbr_cfg);
703 break;
704 }
705
706 clk_ctrl |= DP_PLL_CLOCK_ENABLE | DP_PLL_ENABLE;
707 writel(clk_ctrl, tcphy->base + PHY_DP_CLK_CTL);
708 writel(hsclk_sel, tcphy->base + CMN_DIAG_HSCLK_SEL);
709
710 /* load the configuration of PLL1 */
711 for (i = 0; i < cfg_size; i++) {
712 writel(phy_cfg[i].value, tcphy->base + phy_cfg[i].addr);
713 }
714 }
715
tcphy_tx_usb3_cfg_lane(struct rockchip_typec_phy * tcphy,u32 lane)716 static void tcphy_tx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
717 {
718 writel(0x7799, tcphy->base + TX_PSC_A0(lane));
719 writel(0x7798, tcphy->base + TX_PSC_A1(lane));
720 writel(0x5098, tcphy->base + TX_PSC_A2(lane));
721 writel(0x5098, tcphy->base + TX_PSC_A3(lane));
722 writel(0, tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
723 writel(0xbf, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
724 }
725
tcphy_rx_usb3_cfg_lane(struct rockchip_typec_phy * tcphy,u32 lane)726 static void tcphy_rx_usb3_cfg_lane(struct rockchip_typec_phy *tcphy, u32 lane)
727 {
728 writel(0xa6fd, tcphy->base + RX_PSC_A0(lane));
729 writel(0xa6fd, tcphy->base + RX_PSC_A1(lane));
730 writel(0xa410, tcphy->base + RX_PSC_A2(lane));
731 writel(0x2410, tcphy->base + RX_PSC_A3(lane));
732 writel(0x23ff, tcphy->base + RX_PSC_CAL(lane));
733 writel(0x13, tcphy->base + RX_SIGDET_HL_FILT_TMR(lane));
734 writel(0x03e7, tcphy->base + RX_REE_CTRL_DATA_MASK(lane));
735 writel(0x1004, tcphy->base + RX_DIAG_SIGDET_TUNE(lane));
736 writel(0x2010, tcphy->base + RX_PSC_RDY(lane));
737 writel(0xfb, tcphy->base + XCVR_DIAG_BIDI_CTRL(lane));
738 }
739
tcphy_dp_cfg_lane(struct rockchip_typec_phy * tcphy,int link_rate,u8 swing,u8 pre_emp,u32 lane)740 static void tcphy_dp_cfg_lane(struct rockchip_typec_phy *tcphy, int link_rate, u8 swing, u8 pre_emp, u32 lane)
741 {
742 u16 val;
743
744 writel(0xbefc, tcphy->base + XCVR_PSM_RCTRL(lane));
745 writel(0x6799, tcphy->base + TX_PSC_A0(lane));
746 writel(0x6798, tcphy->base + TX_PSC_A1(lane));
747 writel(0x98, tcphy->base + TX_PSC_A2(lane));
748 writel(0x98, tcphy->base + TX_PSC_A3(lane));
749
750 writel(tcphy->config[swing][pre_emp].swing, tcphy->base + TX_TXCC_MGNFS_MULT_000(lane));
751 writel(tcphy->config[swing][pre_emp].pe, tcphy->base + TX_TXCC_CPOST_MULT_00(lane));
752
753 if (swing == 0x2 && pre_emp == 0 && link_rate != 0x83d60) {
754 writel(0x700, tcphy->base + TX_DIAG_TX_DRV(lane));
755 writel(0x13c, tcphy->base + TX_TXCC_CAL_SCLR_MULT(lane));
756 } else {
757 writel(0x128, tcphy->base + TX_TXCC_CAL_SCLR_MULT(lane));
758 writel(0x0400, tcphy->base + TX_DIAG_TX_DRV(lane));
759 }
760
761 val = readl(tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
762 val = val & 0x8fff;
763 switch (link_rate) {
764 case 0x83d60:
765 val |= (0x5 << 0xc);
766 break;
767 case 0x278d0:
768 case 0x41eb0:
769 default:
770 val |= (0x6 << 0xc);
771 break;
772 }
773 writel(val, tcphy->base + XCVR_DIAG_PLLDRC_CTRL(lane));
774 }
775
tcphy_dp_set_phy_config(struct phy * phy,int link_rate,int lane_count,u8 swing,u8 pre_emp)776 int tcphy_dp_set_phy_config(struct phy *phy, int link_rate, int lane_count, u8 swing, u8 pre_emp)
777 {
778 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
779 u8 i;
780
781 if (!phy->power_count) {
782 return -EPERM;
783 }
784
785 if (tcphy->mode == MODE_DFP_DP) {
786 for (i = 0x0; i < 0x4; i++) {
787 tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, i);
788 }
789 } else {
790 if (tcphy->flip) {
791 tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 0x0);
792 tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 0x1);
793 } else {
794 tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 0x2);
795 tcphy_dp_cfg_lane(tcphy, link_rate, swing, pre_emp, 0x3);
796 }
797 }
798
799 return 0;
800 }
801 EXPORT_SYMBOL(tcphy_dp_set_phy_config);
802
tcphy_dp_set_lane_count(struct phy * phy,u8 lane_count)803 int tcphy_dp_set_lane_count(struct phy *phy, u8 lane_count)
804 {
805 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
806 u32 reg;
807
808 if (!phy->power_count) {
809 return -EPERM;
810 }
811
812 /*
813 * In cases where fewer than the configured number of DP lanes are
814 * being used. PHY_DP_MODE_CTL[15:12] must be set to disable and
815 * power-down the unused PHY DP lanes (and their mapped PMA lanes).
816 * Set the bit ([15:12]) associated with each DP PHY lane(s) to be
817 * disabled.
818 */
819 reg = readl(tcphy->base + PHY_DP_MODE_CTL);
820 reg |= PHY_DP_LANE_DISABLE;
821
822 switch (lane_count) {
823 case 0x4:
824 reg &= ~(PHY_DP_LANE_3_DISABLE | PHY_DP_LANE_2_DISABLE | PHY_DP_LANE_1_DISABLE | PHY_DP_LANE_0_DISABLE);
825 break;
826 case 0x2:
827 reg &= ~(PHY_DP_LANE_1_DISABLE | PHY_DP_LANE_0_DISABLE);
828 break;
829 case 0x1:
830 reg &= ~PHY_DP_LANE_0_DISABLE;
831 break;
832 default:
833 return -EINVAL;
834 }
835
836 writel(reg, tcphy->base + PHY_DP_MODE_CTL);
837
838 return 0;
839 }
840 EXPORT_SYMBOL(tcphy_dp_set_lane_count);
841
tcphy_dp_set_link_rate(struct phy * phy,int link_rate,bool ssc_on)842 int tcphy_dp_set_link_rate(struct phy *phy, int link_rate, bool ssc_on)
843 {
844 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
845 const struct phy_reg *phy_cfg;
846 u32 cmn_diag_hsclk_sel, phy_dp_clk_ctl, reg;
847 u32 i, cfg_size;
848 int ret;
849
850 if (!phy->power_count) {
851 return -EPERM;
852 }
853
854 /* Place the PHY lanes in the A3 power state. */
855 ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A3);
856 if (ret) {
857 dev_err(tcphy->dev, "failed to enter A3 state: %d\n", ret);
858 return ret;
859 }
860
861 /* Gate the PLL clocks from PMA */
862 reg = readl(tcphy->base + PHY_DP_CLK_CTL);
863 reg &= ~DP_PLL_CLOCK_ENABLE_MASK;
864 reg |= DP_PLL_CLOCK_DISABLE;
865 writel(reg, tcphy->base + PHY_DP_CLK_CTL);
866
867 ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg, !(reg & DP_PLL_CLOCK_ENABLE_ACK), 0xA,
868 PHY_MODE_SET_TIMEOUT);
869 if (ret) {
870 dev_err(tcphy->dev, "wait DP PLL clock disabled timeout\n");
871 return ret;
872 }
873
874 /* Disable the PLL */
875 reg = readl(tcphy->base + PHY_DP_CLK_CTL);
876 reg &= ~DP_PLL_ENABLE_MASK;
877 reg |= DP_PLL_DISABLE;
878 writel(reg, tcphy->base + PHY_DP_CLK_CTL);
879
880 ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg, !(reg & DP_PLL_READY), 0xA, PHY_MODE_SET_TIMEOUT);
881 if (ret) {
882 dev_err(tcphy->dev, "wait DP PLL not ready timeout\n");
883 return ret;
884 }
885
886 /* Re-configure PHY registers for the new data rate */
887 cmn_diag_hsclk_sel = readl(tcphy->base + CMN_DIAG_HSCLK_SEL);
888 cmn_diag_hsclk_sel &= ~(GENMASK(0x5, 0x4) | GENMASK(0x1, 0x0));
889
890 phy_dp_clk_ctl = readl(tcphy->base + PHY_DP_CLK_CTL);
891 phy_dp_clk_ctl &= ~(GENMASK(0xf, 0xc) | GENMASK(0xb, 0x8));
892
893 switch (link_rate) {
894 case 0x278d0:
895 cmn_diag_hsclk_sel |= (0x3 << 0x4) | (0 << 0);
896 phy_dp_clk_ctl |= (0x2 << 0xc) | (0x4 << 0x8);
897
898 phy_cfg = ssc_on ? dp_pll_rbr_ssc_cfg : dp_pll_rbr_cfg;
899 cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_rbr_ssc_cfg) : ARRAY_SIZE(dp_pll_rbr_cfg);
900 break;
901 case 0x41eb0:
902 cmn_diag_hsclk_sel |= (0x3 << 0x4) | (0 << 0);
903 phy_dp_clk_ctl |= (0x2 << 0xc) | (0x4 << 0x8);
904
905 phy_cfg = ssc_on ? dp_pll_hbr_ssc_cfg : dp_pll_hbr_cfg;
906 cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_hbr_ssc_cfg) : ARRAY_SIZE(dp_pll_hbr_cfg);
907 break;
908 case 0x83d60:
909 cmn_diag_hsclk_sel |= (0x2 << 0x4) | (0 << 0);
910 phy_dp_clk_ctl |= (0x1 << 0xc) | (0x2 << 0x8);
911
912 phy_cfg = ssc_on ? dp_pll_hbr2_ssc_cfg : dp_pll_hbr2_cfg;
913 cfg_size = ssc_on ? ARRAY_SIZE(dp_pll_hbr2_ssc_cfg) : ARRAY_SIZE(dp_pll_hbr2_cfg);
914 break;
915 default:
916 return -EINVAL;
917 }
918
919 writel(cmn_diag_hsclk_sel, tcphy->base + CMN_DIAG_HSCLK_SEL);
920 writel(phy_dp_clk_ctl, tcphy->base + PHY_DP_CLK_CTL);
921
922 /* load the configuration of PLL1 */
923 for (i = 0; i < cfg_size; i++) {
924 writel(phy_cfg[i].value, tcphy->base + phy_cfg[i].addr);
925 }
926
927 /* Enable the PLL */
928 reg = readl(tcphy->base + PHY_DP_CLK_CTL);
929 reg &= ~DP_PLL_ENABLE_MASK;
930 reg |= DP_PLL_ENABLE;
931 writel(reg, tcphy->base + PHY_DP_CLK_CTL);
932
933 ret = readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg, reg & DP_PLL_READY, 0xa, PHY_MODE_SET_TIMEOUT);
934 if (ret < 0) {
935 dev_err(tcphy->dev, "wait DP PLL ready timeout\n");
936 return ret;
937 }
938
939 /* Enable PMA PLL clocks */
940 reg = readl(tcphy->base + PHY_DP_CLK_CTL);
941 reg &= ~DP_PLL_CLOCK_ENABLE_MASK;
942 reg |= DP_PLL_CLOCK_ENABLE;
943 writel(reg, tcphy->base + PHY_DP_CLK_CTL);
944
945 ret =
946 readl_poll_timeout(tcphy->base + PHY_DP_CLK_CTL, reg, reg & DP_PLL_CLOCK_ENABLE_ACK, 0xa, PHY_MODE_SET_TIMEOUT);
947 if (ret) {
948 dev_err(tcphy->dev, "wait DP PLL clock enabled timeout\n");
949 return ret;
950 }
951
952 /* The PMA must go through the A2 power state upon a data rate change */
953 ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A2);
954 if (ret) {
955 dev_err(tcphy->dev, "failed to enter A2 state: %d\n", ret);
956 return ret;
957 }
958
959 /* change the PHY power state to A0 */
960 ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A0);
961 if (ret) {
962 dev_err(tcphy->dev, "failed to enter A0 state: %d\n", ret);
963 return ret;
964 }
965
966 return 0;
967 }
968 EXPORT_SYMBOL(tcphy_dp_set_link_rate);
969
property_enable(struct rockchip_typec_phy * tcphy,const struct usb3phy_reg * reg,bool en)970 static inline int property_enable(struct rockchip_typec_phy *tcphy, const struct usb3phy_reg *reg, bool en)
971 {
972 u32 mask = 1 << reg->write_enable;
973 u32 val = en << reg->enable_bit;
974
975 return regmap_write(tcphy->grf_regs, reg->offset, val | mask);
976 }
977
tcphy_dp_aux_set_flip(struct rockchip_typec_phy * tcphy)978 static void tcphy_dp_aux_set_flip(struct rockchip_typec_phy *tcphy)
979 {
980 u16 tx_ana_ctrl_reg_1;
981
982 /*
983 * Select the polarity of the xcvr:
984 * 1, Reverses the polarity (If TYPEC, Pulls ups aux_p and pull
985 * down aux_m)
986 * 0, Normal polarity (if TYPEC, pulls up aux_m and pulls down
987 * aux_p)
988 */
989 tx_ana_ctrl_reg_1 = readl(tcphy->base + TX_ANA_CTRL_REG_1);
990 if (!tcphy->flip) {
991 tx_ana_ctrl_reg_1 |= AUXDA_POLARITY;
992 } else {
993 tx_ana_ctrl_reg_1 &= ~AUXDA_POLARITY;
994 }
995 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
996 }
997
tcphy_dp_aux_calibration(struct rockchip_typec_phy * tcphy)998 static void tcphy_dp_aux_calibration(struct rockchip_typec_phy *tcphy)
999 {
1000 u16 val;
1001 u16 tx_ana_ctrl_reg_1;
1002 u16 tx_ana_ctrl_reg_2;
1003 s32 pu_calib_code, pd_calib_code;
1004 s32 pu_adj, pd_adj;
1005 u16 calib;
1006
1007 /*
1008 * Calculate calibration code as per docs: use an average of the
1009 * pull down and pull up. Then add in adjustments.
1010 */
1011 val = readl(tcphy->base + CMN_TXPUCAL_CTRL);
1012 pu_calib_code = CMN_CALIB_CODE_POS(val);
1013 val = readl(tcphy->base + CMN_TXPDCAL_CTRL);
1014 pd_calib_code = CMN_CALIB_CODE_POS(val);
1015 val = readl(tcphy->base + CMN_TXPU_ADJ_CTRL);
1016 pu_adj = CMN_CALIB_CODE(val);
1017 val = readl(tcphy->base + CMN_TXPD_ADJ_CTRL);
1018 pd_adj = CMN_CALIB_CODE(val);
1019 calib = (pu_calib_code + pd_calib_code) / 0x2 + pu_adj + pd_adj;
1020
1021 /* disable txda_cal_latch_en for rewrite the calibration values */
1022 tx_ana_ctrl_reg_1 = readl(tcphy->base + TX_ANA_CTRL_REG_1);
1023 tx_ana_ctrl_reg_1 &= ~TXDA_CAL_LATCH_EN;
1024 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1025
1026 /* write the calibration, then delay 10 ms as sample in docs */
1027 val = readl(tcphy->base + TX_DIG_CTRL_REG_2);
1028 val &= ~(TX_RESCAL_CODE_MASK << TX_RESCAL_CODE_OFFSET);
1029 val |= calib << TX_RESCAL_CODE_OFFSET;
1030 writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
1031 usleep_range(0x2710, 0x2742);
1032
1033 /*
1034 * Enable signal for latch that sample and holds calibration values.
1035 * Activate this signal for 1 clock cycle to sample new calibration
1036 * values.
1037 */
1038 tx_ana_ctrl_reg_1 |= TXDA_CAL_LATCH_EN;
1039 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1040 usleep_range(0x96, 0xc8);
1041
1042 /* set TX Voltage Level and TX Deemphasis to 0 */
1043 writel(0, tcphy->base + PHY_DP_TX_CTL);
1044
1045 /* re-enable decap */
1046 tx_ana_ctrl_reg_2 = XCVR_DECAP_EN;
1047 writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1048 udelay(1);
1049 tx_ana_ctrl_reg_2 |= XCVR_DECAP_EN_DEL;
1050 writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1051
1052 writel(0, tcphy->base + TX_ANA_CTRL_REG_3);
1053
1054 tx_ana_ctrl_reg_1 |= TXDA_UPHY_SUPPLY_EN;
1055 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1056 udelay(1);
1057 tx_ana_ctrl_reg_1 |= TXDA_UPHY_SUPPLY_EN_DEL;
1058 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1059
1060 writel(0, tcphy->base + TX_ANA_CTRL_REG_5);
1061
1062 /*
1063 * Programs txda_drv_ldo_prog[15:0], Sets driver LDO
1064 * voltage 16'h1001 for DP-AUX-TX and RX
1065 */
1066 writel(0x1001, tcphy->base + TX_ANA_CTRL_REG_4);
1067
1068 /* re-enables Bandgap reference for LDO */
1069 tx_ana_ctrl_reg_1 |= TXDA_DRV_LDO_EN;
1070 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1071 udelay(0x5);
1072 tx_ana_ctrl_reg_1 |= TXDA_BGREF_EN;
1073 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1074
1075 /*
1076 * re-enables the transmitter pre-driver, driver data selection MUX,
1077 * and receiver detect circuits.
1078 */
1079 tx_ana_ctrl_reg_2 |= TXDA_DRV_PREDRV_EN;
1080 writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1081 udelay(1);
1082 tx_ana_ctrl_reg_2 |= TXDA_DRV_PREDRV_EN_DEL;
1083 writel(tx_ana_ctrl_reg_2, tcphy->base + TX_ANA_CTRL_REG_2);
1084
1085 /*
1086 * Do all the undocumented magic:
1087 * - Turn on TXDA_DP_AUX_EN, whatever that is, even though sample
1088 * never shows this going on.
1089 * - Turn on TXDA_DECAP_EN (and TXDA_DECAP_EN_DEL) even though
1090 * docs say for aux it's always 0.
1091 * - Turn off the LDO and BGREF, which we just spent time turning
1092 * on above (???).
1093 *
1094 * Without this magic, things seem worse.
1095 */
1096 tx_ana_ctrl_reg_1 |= TXDA_DP_AUX_EN;
1097 tx_ana_ctrl_reg_1 |= TXDA_DECAP_EN;
1098 tx_ana_ctrl_reg_1 &= ~TXDA_DRV_LDO_EN;
1099 tx_ana_ctrl_reg_1 &= ~TXDA_BGREF_EN;
1100 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1101 udelay(1);
1102 tx_ana_ctrl_reg_1 |= TXDA_DECAP_EN_DEL;
1103 writel(tx_ana_ctrl_reg_1, tcphy->base + TX_ANA_CTRL_REG_1);
1104
1105 /*
1106 * Undo the work we did to set the LDO voltage.
1107 * This doesn't seem to help nor hurt, but it kinda goes with the
1108 * undocumented magic above.
1109 */
1110 writel(0, tcphy->base + TX_ANA_CTRL_REG_4);
1111
1112 /* Don't set voltage swing to 400 mV peak to peak (differential) */
1113 writel(0, tcphy->base + TXDA_COEFF_CALC_CTRL);
1114
1115 /* Init TXDA_CYA_AUXDA_CYA for unknown magic reasons */
1116 writel(0, tcphy->base + TXDA_CYA_AUXDA_CYA);
1117
1118 /*
1119 * More undocumented magic, presumably the goal of which is to
1120 * make the "auxda_source_aux_oen" be ignored and instead to decide
1121 * about "high impedance state" based on what software puts in the
1122 * register TXDA_COEFF_CALC_CTRL (see TX_HIGH_Z). Since we only
1123 * program that register once and we don't set the bit TX_HIGH_Z,
1124 * presumably the goal here is that we should never put the analog
1125 * driver in high impedance state.
1126 */
1127 val = readl(tcphy->base + TX_DIG_CTRL_REG_2);
1128 val |= TX_HIGH_Z_TM_EN;
1129 writel(val, tcphy->base + TX_DIG_CTRL_REG_2);
1130 }
1131
tcphy_cfg_usb3_to_usb2_only(struct rockchip_typec_phy * tcphy,bool value)1132 static int tcphy_cfg_usb3_to_usb2_only(struct rockchip_typec_phy *tcphy, bool value)
1133 {
1134 const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1135
1136 property_enable(tcphy, &cfg->usb3tousb2_en, value);
1137 property_enable(tcphy, &cfg->usb3_host_disable, value);
1138 property_enable(tcphy, &cfg->usb3_host_port, !value);
1139
1140 return 0;
1141 }
1142
tcphy_phy_init(struct rockchip_typec_phy * tcphy,u8 mode)1143 static int tcphy_phy_init(struct rockchip_typec_phy *tcphy, u8 mode)
1144 {
1145 const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1146 int ret, i;
1147 u32 val;
1148
1149 ret = clk_prepare_enable(tcphy->clk_core);
1150 if (ret) {
1151 dev_err(tcphy->dev, "Failed to prepare_enable core clock\n");
1152 return ret;
1153 }
1154
1155 ret = clk_prepare_enable(tcphy->clk_ref);
1156 if (ret) {
1157 dev_err(tcphy->dev, "Failed to prepare_enable ref clock\n");
1158 goto err_clk_core;
1159 }
1160
1161 reset_control_deassert(tcphy->tcphy_rst);
1162
1163 property_enable(tcphy, &cfg->typec_conn_dir, tcphy->flip);
1164 tcphy_dp_aux_set_flip(tcphy);
1165
1166 tcphy_cfg_24m(tcphy);
1167 tcphy_set_lane_mapping(tcphy, mode);
1168
1169 if (mode == MODE_DFP_DP) {
1170 tcphy_cfg_usb3_to_usb2_only(tcphy, true);
1171 tcphy_cfg_dp_pll(tcphy, DP_DEFAULT_RATE);
1172 for (i = 0; i < 0x4; i++) {
1173 tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, i);
1174 }
1175 } else {
1176 tcphy_cfg_usb3_pll(tcphy);
1177 tcphy_cfg_dp_pll(tcphy, DP_DEFAULT_RATE);
1178 if (tcphy->flip) {
1179 tcphy_tx_usb3_cfg_lane(tcphy, 0x3);
1180 tcphy_rx_usb3_cfg_lane(tcphy, 0x2);
1181 tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 0);
1182 tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 1);
1183 } else {
1184 tcphy_tx_usb3_cfg_lane(tcphy, 0);
1185 tcphy_rx_usb3_cfg_lane(tcphy, 1);
1186 tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 0x2);
1187 tcphy_dp_cfg_lane(tcphy, DP_DEFAULT_RATE, 0, 0, 0x3);
1188 }
1189 }
1190
1191 val = readl(tcphy->base + PHY_DP_MODE_CTL);
1192 val &= ~DP_MODE_MASK;
1193 val |= DP_MODE_ENTER_A2 | DP_LINK_RESET_DEASSERTED;
1194 writel(val, tcphy->base + PHY_DP_MODE_CTL);
1195
1196 reset_control_deassert(tcphy->uphy_rst);
1197
1198 ret = readx_poll_timeout(readl, tcphy->base + PMA_CMN_CTRL1, val, val & CMN_READY, 0xA, PHY_MODE_SET_TIMEOUT);
1199 if (ret < 0) {
1200 dev_err(tcphy->dev, "wait pma ready timeout\n");
1201 ret = -ETIMEDOUT;
1202 goto err_wait_pma;
1203 }
1204
1205 reset_control_deassert(tcphy->pipe_rst);
1206
1207 return 0;
1208
1209 err_wait_pma:
1210 reset_control_assert(tcphy->uphy_rst);
1211 reset_control_assert(tcphy->tcphy_rst);
1212 clk_disable_unprepare(tcphy->clk_ref);
1213 err_clk_core:
1214 clk_disable_unprepare(tcphy->clk_core);
1215 return ret;
1216 }
1217
tcphy_phy_deinit(struct rockchip_typec_phy * tcphy)1218 static void tcphy_phy_deinit(struct rockchip_typec_phy *tcphy)
1219 {
1220 reset_control_assert(tcphy->tcphy_rst);
1221 reset_control_assert(tcphy->uphy_rst);
1222 reset_control_assert(tcphy->pipe_rst);
1223 clk_disable_unprepare(tcphy->clk_core);
1224 clk_disable_unprepare(tcphy->clk_ref);
1225 }
1226
tcphy_get_mode(struct rockchip_typec_phy * tcphy)1227 static int tcphy_get_mode(struct rockchip_typec_phy *tcphy)
1228 {
1229 struct extcon_dev *edev = tcphy->extcon;
1230 union extcon_property_value property;
1231 unsigned int id;
1232 bool ufp, dp;
1233 u8 mode;
1234 int ret;
1235
1236 if (!edev) {
1237 return MODE_DFP_USB;
1238 }
1239
1240 ufp = extcon_get_state(edev, EXTCON_USB);
1241 dp = extcon_get_state(edev, EXTCON_DISP_DP);
1242
1243 mode = MODE_DFP_USB;
1244 id = EXTCON_USB_HOST;
1245
1246 if (ufp) {
1247 mode = MODE_UFP_USB;
1248 id = EXTCON_USB;
1249 } else if (dp) {
1250 mode = MODE_DFP_DP;
1251 id = EXTCON_DISP_DP;
1252
1253 ret = extcon_get_property(edev, id, EXTCON_PROP_USB_SS, &property);
1254 if (ret) {
1255 dev_err(tcphy->dev, "get superspeed property failed\n");
1256 return ret;
1257 }
1258
1259 if (property.intval) {
1260 mode |= MODE_DFP_USB;
1261 }
1262 }
1263
1264 ret = extcon_get_property(edev, id, EXTCON_PROP_USB_TYPEC_POLARITY, &property);
1265 if (ret) {
1266 dev_err(tcphy->dev, "get polarity property failed\n");
1267 return ret;
1268 }
1269
1270 tcphy->flip = property.intval ? 1 : 0;
1271
1272 return mode;
1273 }
1274
_rockchip_usb3_phy_power_on(struct rockchip_typec_phy * tcphy)1275 static int _rockchip_usb3_phy_power_on(struct rockchip_typec_phy *tcphy)
1276 {
1277 const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1278 const struct usb3phy_reg *reg = &cfg->pipe_status;
1279 int timeout, new_mode, ret = 0;
1280 u32 val;
1281
1282 mutex_lock(&tcphy->lock);
1283
1284 new_mode = tcphy_get_mode(tcphy);
1285 if (new_mode < 0) {
1286 ret = new_mode;
1287 goto unlock_ret;
1288 }
1289
1290 /* DP-only mode; fall back to USB2 */
1291 if (!(new_mode & (MODE_DFP_USB | MODE_UFP_USB))) {
1292 tcphy_cfg_usb3_to_usb2_only(tcphy, true);
1293 goto unlock_ret;
1294 }
1295
1296 if (tcphy->mode == new_mode) {
1297 goto unlock_ret;
1298 }
1299
1300 if (tcphy->mode == MODE_DISCONNECT) {
1301 ret = tcphy_phy_init(tcphy, new_mode);
1302 if (ret) {
1303 goto unlock_ret;
1304 }
1305 }
1306
1307 /* wait TCPHY for pipe ready */
1308 for (timeout = 0; timeout < 0x64; timeout++) {
1309 regmap_read(tcphy->grf_regs, reg->offset, &val);
1310 if (!(val & BIT(reg->enable_bit))) {
1311 tcphy->mode |= new_mode & (MODE_DFP_USB | MODE_UFP_USB);
1312
1313 /* enable usb3 host */
1314 tcphy_cfg_usb3_to_usb2_only(tcphy, false);
1315 goto unlock_ret;
1316 }
1317 usleep_range(0xa, 0x14);
1318 }
1319
1320 if (tcphy->mode == MODE_DISCONNECT) {
1321 tcphy_phy_deinit(tcphy);
1322 }
1323
1324 ret = -ETIMEDOUT;
1325
1326 unlock_ret:
1327 mutex_unlock(&tcphy->lock);
1328 return ret;
1329 }
1330
rockchip_usb3_phy_power_on(struct phy * phy)1331 static int rockchip_usb3_phy_power_on(struct phy *phy)
1332 {
1333 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1334 int ret;
1335 int tries;
1336
1337 for (tries = 0; tries < POWER_ON_TRIES; tries++) {
1338 ret = _rockchip_usb3_phy_power_on(tcphy);
1339 if (!ret) {
1340 break;
1341 }
1342 }
1343
1344 if (tries && !ret) {
1345 dev_info(tcphy->dev, "Needed %d loops to turn on\n", tries);
1346 }
1347
1348 return ret;
1349 }
1350
rockchip_usb3_phy_power_off(struct phy * phy)1351 static int rockchip_usb3_phy_power_off(struct phy *phy)
1352 {
1353 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1354
1355 mutex_lock(&tcphy->lock);
1356 tcphy_cfg_usb3_to_usb2_only(tcphy, false);
1357
1358 if (tcphy->mode == MODE_DISCONNECT) {
1359 goto unlock;
1360 }
1361
1362 tcphy->mode &= ~(MODE_UFP_USB | MODE_DFP_USB);
1363 if (tcphy->mode == MODE_DISCONNECT) {
1364 tcphy_phy_deinit(tcphy);
1365 }
1366
1367 unlock:
1368 mutex_unlock(&tcphy->lock);
1369 return 0;
1370 }
1371
1372 static const struct phy_ops rockchip_usb3_phy_ops = {
1373 .power_on = rockchip_usb3_phy_power_on,
1374 .power_off = rockchip_usb3_phy_power_off,
1375 .owner = THIS_MODULE,
1376 };
1377
rockchip_dp_phy_power_on(struct phy * phy)1378 static int rockchip_dp_phy_power_on(struct phy *phy)
1379 {
1380 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1381 const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1382 int new_mode, ret = 0;
1383 u32 val;
1384
1385 mutex_lock(&tcphy->lock);
1386
1387 new_mode = tcphy_get_mode(tcphy);
1388 if (new_mode < 0) {
1389 ret = new_mode;
1390 goto unlock_ret;
1391 }
1392
1393 if (!(new_mode & MODE_DFP_DP)) {
1394 ret = -ENODEV;
1395 goto unlock_ret;
1396 }
1397
1398 if (tcphy->mode == new_mode) {
1399 goto unlock_ret;
1400 }
1401
1402 /*
1403 * If the PHY has been power on, but the mode is not DP only mode,
1404 * re-init the PHY for setting all of 4 lanes to DP.
1405 */
1406 if (new_mode == MODE_DFP_DP && tcphy->mode != MODE_DISCONNECT) {
1407 tcphy_phy_deinit(tcphy);
1408 ret = tcphy_phy_init(tcphy, new_mode);
1409 } else if (tcphy->mode == MODE_DISCONNECT) {
1410 ret = tcphy_phy_init(tcphy, new_mode);
1411 }
1412 if (ret) {
1413 goto unlock_ret;
1414 }
1415
1416 property_enable(tcphy, &cfg->uphy_dp_sel, 1);
1417
1418 ret =
1419 readx_poll_timeout(readl, tcphy->base + PHY_DP_MODE_CTL, val, val & DP_MODE_A2_ACK, 0x3E8, \
1420 PHY_MODE_SET_TIMEOUT);
1421 if (ret < 0) {
1422 dev_err(tcphy->dev, "failed to wait TCPHY enter A2\n");
1423 goto power_on_finish;
1424 }
1425
1426 tcphy_dp_aux_calibration(tcphy);
1427
1428 /* enter A0 mode */
1429 ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A0);
1430 if (ret) {
1431 dev_err(tcphy->dev, "failed to enter A0 power state\n");
1432 goto power_on_finish;
1433 }
1434
1435 tcphy->mode |= MODE_DFP_DP;
1436
1437 power_on_finish:
1438 if (tcphy->mode == MODE_DISCONNECT) {
1439 tcphy_phy_deinit(tcphy);
1440 }
1441 unlock_ret:
1442 mutex_unlock(&tcphy->lock);
1443 return ret;
1444 }
1445
rockchip_dp_phy_power_off(struct phy * phy)1446 static int rockchip_dp_phy_power_off(struct phy *phy)
1447 {
1448 struct rockchip_typec_phy *tcphy = phy_get_drvdata(phy);
1449 int ret;
1450
1451 mutex_lock(&tcphy->lock);
1452
1453 if (tcphy->mode == MODE_DISCONNECT) {
1454 goto unlock;
1455 }
1456
1457 tcphy->mode &= ~MODE_DFP_DP;
1458
1459 ret = tcphy_dp_set_power_state(tcphy, PHY_DP_POWER_STATE_A2);
1460 if (ret) {
1461 dev_err(tcphy->dev, "failed to enter A2 power state\n");
1462 goto unlock;
1463 }
1464
1465 if (tcphy->mode == MODE_DISCONNECT) {
1466 tcphy_phy_deinit(tcphy);
1467 }
1468
1469 unlock:
1470 mutex_unlock(&tcphy->lock);
1471 return 0;
1472 }
1473
1474 static const struct phy_ops rockchip_dp_phy_ops = {
1475 .power_on = rockchip_dp_phy_power_on,
1476 .power_off = rockchip_dp_phy_power_off,
1477 .owner = THIS_MODULE,
1478 };
1479
tcphy_parse_dt(struct rockchip_typec_phy * tcphy,struct device * dev)1480 static int tcphy_parse_dt(struct rockchip_typec_phy *tcphy, struct device *dev)
1481 {
1482 int ret;
1483
1484 tcphy->grf_regs = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
1485 if (IS_ERR(tcphy->grf_regs)) {
1486 dev_err(dev, "could not find grf dt node\n");
1487 return PTR_ERR(tcphy->grf_regs);
1488 }
1489
1490 tcphy->clk_core = devm_clk_get(dev, "tcpdcore");
1491 if (IS_ERR(tcphy->clk_core)) {
1492 dev_err(dev, "could not get uphy core clock\n");
1493 return PTR_ERR(tcphy->clk_core);
1494 }
1495
1496 tcphy->clk_ref = devm_clk_get(dev, "tcpdphy-ref");
1497 if (IS_ERR(tcphy->clk_ref)) {
1498 dev_err(dev, "could not get uphy ref clock\n");
1499 return PTR_ERR(tcphy->clk_ref);
1500 }
1501
1502 tcphy->uphy_rst = devm_reset_control_get(dev, "uphy");
1503 if (IS_ERR(tcphy->uphy_rst)) {
1504 dev_err(dev, "no uphy_rst reset control found\n");
1505 return PTR_ERR(tcphy->uphy_rst);
1506 }
1507
1508 tcphy->pipe_rst = devm_reset_control_get(dev, "uphy-pipe");
1509 if (IS_ERR(tcphy->pipe_rst)) {
1510 dev_err(dev, "no pipe_rst reset control found\n");
1511 return PTR_ERR(tcphy->pipe_rst);
1512 }
1513
1514 tcphy->tcphy_rst = devm_reset_control_get(dev, "uphy-tcphy");
1515 if (IS_ERR(tcphy->tcphy_rst)) {
1516 dev_err(dev, "no tcphy_rst reset control found\n");
1517 return PTR_ERR(tcphy->tcphy_rst);
1518 }
1519
1520 /*
1521 * check if phy_config pass from dts, if no,
1522 * use default phy config value.
1523 */
1524 ret = of_property_read_u32_array(dev->of_node, "rockchip,phy-config", (u32 *)tcphy->config,
1525 sizeof(tcphy->config) / sizeof(u32));
1526 if (ret) {
1527 memcpy(tcphy->config, tcphy_default_config, sizeof(tcphy->config));
1528 }
1529
1530 return 0;
1531 }
1532
typec_phy_pre_init(struct rockchip_typec_phy * tcphy)1533 static void typec_phy_pre_init(struct rockchip_typec_phy *tcphy)
1534 {
1535 const struct rockchip_usb3phy_port_cfg *cfg = tcphy->port_cfgs;
1536
1537 reset_control_assert(tcphy->tcphy_rst);
1538 reset_control_assert(tcphy->uphy_rst);
1539 reset_control_assert(tcphy->pipe_rst);
1540
1541 /* select external psm clock */
1542 property_enable(tcphy, &cfg->external_psm, 1);
1543 property_enable(tcphy, &cfg->usb3tousb2_en, 0);
1544
1545 tcphy->mode = MODE_DISCONNECT;
1546 }
1547
rockchip_typec_phy_probe(struct platform_device * pdev)1548 static int rockchip_typec_phy_probe(struct platform_device *pdev)
1549 {
1550 struct device *dev = &pdev->dev;
1551 struct device_node *np = dev->of_node;
1552 struct device_node *child_np;
1553 struct rockchip_typec_phy *tcphy;
1554 struct phy_provider *phy_provider;
1555 struct resource *res;
1556 const struct rockchip_usb3phy_port_cfg *phy_cfgs;
1557 const struct of_device_id *match;
1558 int index, ret;
1559
1560 tcphy = devm_kzalloc(dev, sizeof(*tcphy), GFP_KERNEL);
1561 if (!tcphy) {
1562 return -ENOMEM;
1563 }
1564
1565 match = of_match_device(dev->driver->of_match_table, dev);
1566 if (!match || !match->data) {
1567 dev_err(dev, "phy configs are not assigned!\n");
1568 return -EINVAL;
1569 }
1570
1571 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1572 tcphy->base = devm_ioremap_resource(dev, res);
1573 if (IS_ERR(tcphy->base)) {
1574 return PTR_ERR(tcphy->base);
1575 }
1576
1577 phy_cfgs = match->data;
1578 /* find out a proper config which can be matched with dt. */
1579 index = 0;
1580 while (phy_cfgs[index].reg) {
1581 if (phy_cfgs[index].reg == res->start) {
1582 tcphy->port_cfgs = &phy_cfgs[index];
1583 break;
1584 }
1585
1586 ++index;
1587 }
1588
1589 if (!tcphy->port_cfgs) {
1590 dev_err(dev, "no phy-config can be matched with %pOFn node\n", np);
1591 return -EINVAL;
1592 }
1593
1594 ret = tcphy_parse_dt(tcphy, dev);
1595 if (ret) {
1596 return ret;
1597 }
1598
1599 tcphy->dev = dev;
1600 platform_set_drvdata(pdev, tcphy);
1601 mutex_init(&tcphy->lock);
1602
1603 typec_phy_pre_init(tcphy);
1604
1605 tcphy->extcon = extcon_get_edev_by_phandle(dev, 0);
1606 if (IS_ERR(tcphy->extcon)) {
1607 if (PTR_ERR(tcphy->extcon) == -ENODEV) {
1608 tcphy->extcon = NULL;
1609 } else {
1610 if (PTR_ERR(tcphy->extcon) != -EPROBE_DEFER) {
1611 dev_err(dev, "Invalid or missing extcon\n");
1612 }
1613 return PTR_ERR(tcphy->extcon);
1614 }
1615 }
1616
1617 pm_runtime_enable(dev);
1618
1619 for_each_available_child_of_node(np, child_np)
1620 {
1621 struct phy *phy;
1622
1623 if (of_node_name_eq(child_np, "dp-port")) {
1624 phy = devm_phy_create(dev, child_np, &rockchip_dp_phy_ops);
1625 } else if (of_node_name_eq(child_np, "usb3-port")) {
1626 phy = devm_phy_create(dev, child_np, &rockchip_usb3_phy_ops);
1627 } else {
1628 continue;
1629 }
1630
1631 if (IS_ERR(phy)) {
1632 dev_err(dev, "failed to create phy: %pOFn\n", child_np);
1633 pm_runtime_disable(dev);
1634 return PTR_ERR(phy);
1635 }
1636
1637 phy_set_drvdata(phy, tcphy);
1638 }
1639
1640 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1641 if (IS_ERR(phy_provider)) {
1642 dev_err(dev, "Failed to register phy provider\n");
1643 pm_runtime_disable(dev);
1644 return PTR_ERR(phy_provider);
1645 }
1646
1647 return 0;
1648 }
1649
rockchip_typec_phy_remove(struct platform_device * pdev)1650 static int rockchip_typec_phy_remove(struct platform_device *pdev)
1651 {
1652 pm_runtime_disable(&pdev->dev);
1653
1654 return 0;
1655 }
1656
1657 static const struct of_device_id rockchip_typec_phy_dt_ids[] = {
1658 {.compatible = "rockchip,rk3399-typec-phy", .data = &rk3399_usb3phy_port_cfgs}, {}};
1659
1660 MODULE_DEVICE_TABLE(of, rockchip_typec_phy_dt_ids);
1661
1662 static struct platform_driver rockchip_typec_phy_driver = {
1663 .probe = rockchip_typec_phy_probe,
1664 .remove = rockchip_typec_phy_remove,
1665 .driver =
1666 {
1667 .name = "rockchip-typec-phy",
1668 .of_match_table = rockchip_typec_phy_dt_ids,
1669 },
1670 };
1671
1672 module_platform_driver(rockchip_typec_phy_driver);
1673
1674 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
1675 MODULE_AUTHOR("Kever Yang <kever.yang@rock-chips.com>");
1676 MODULE_DESCRIPTION("Rockchip USB TYPE-C PHY driver");
1677 MODULE_LICENSE("GPL v2");
1678