xref: /linux/drivers/phy/samsung/phy-exynos5-usbdrd.c (revision 1fd1dc41724319406b0aff221a352a400b0ddfc5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Samsung Exynos5 SoC series USB DRD PHY driver
4  *
5  * Phy provider for USB 3.0 DRD controller on Exynos5 SoC series
6  *
7  * Copyright (C) 2014 Samsung Electronics Co., Ltd.
8  * Author: Vivek Gautam <gautam.vivek@samsung.com>
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/iopoll.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/soc/samsung/exynos-regs-pmu.h>
26 #include <linux/usb/typec.h>
27 #include <linux/usb/typec_mux.h>
28 
29 /* Exynos USB PHY registers */
30 #define EXYNOS5_FSEL_9MHZ6		0x0
31 #define EXYNOS5_FSEL_10MHZ		0x1
32 #define EXYNOS5_FSEL_12MHZ		0x2
33 #define EXYNOS5_FSEL_19MHZ2		0x3
34 #define EXYNOS5_FSEL_20MHZ		0x4
35 #define EXYNOS5_FSEL_24MHZ		0x5
36 #define EXYNOS5_FSEL_26MHZ		0x6
37 #define EXYNOS5_FSEL_50MHZ		0x7
38 
39 /* USB 3.2 DRD 4nm PHY link controller registers */
40 #define EXYNOS2200_DRD_CLKRST			0x0c
41 #define EXYNOS2200_CLKRST_LINK_PCLK_SEL		BIT(1)
42 
43 #define EXYNOS2200_DRD_UTMI			0x10
44 
45 /* ExynosAutov920 bits */
46 #define UTMICTL_FORCE_UTMI_SUSPEND		BIT(13)
47 #define UTMICTL_FORCE_UTMI_SLEEP		BIT(12)
48 #define UTMICTL_FORCE_DPPULLDOWN		BIT(9)
49 #define UTMICTL_FORCE_DMPULLDOWN		BIT(8)
50 
51 #define EXYNOS2200_UTMI_FORCE_VBUSVALID		BIT(1)
52 #define EXYNOS2200_UTMI_FORCE_BVALID		BIT(0)
53 
54 #define EXYNOS2200_DRD_HSP_MISC			0x114
55 #define HSP_MISC_SET_REQ_IN2			BIT(4)
56 #define HSP_MISC_RES_TUNE			GENMASK(1, 0)
57 #define RES_TUNE_PHY1_PHY2			0x1
58 #define RES_TUNE_PHY1				0x2
59 #define RES_TUNE_PHY2				0x3
60 
61 /* Exynos5: USB 3.0 DRD PHY registers */
62 #define EXYNOS5_DRD_LINKSYSTEM			0x04
63 #define LINKSYSTEM_XHCI_VERSION_CONTROL		BIT(27)
64 #define LINKSYSTEM_FORCE_VBUSVALID		BIT(8)
65 #define LINKSYSTEM_FORCE_BVALID			BIT(7)
66 #define LINKSYSTEM_FLADJ			GENMASK(6, 1)
67 
68 #define EXYNOS5_DRD_PHYUTMI			0x08
69 #define PHYUTMI_UTMI_SUSPEND_COM_N		BIT(12)
70 #define PHYUTMI_UTMI_L1_SUSPEND_COM_N		BIT(11)
71 #define PHYUTMI_VBUSVLDEXTSEL			BIT(10)
72 #define PHYUTMI_VBUSVLDEXT			BIT(9)
73 #define PHYUTMI_TXBITSTUFFENH			BIT(8)
74 #define PHYUTMI_TXBITSTUFFEN			BIT(7)
75 #define PHYUTMI_OTGDISABLE			BIT(6)
76 #define PHYUTMI_IDPULLUP			BIT(5)
77 #define PHYUTMI_DRVVBUS				BIT(4)
78 #define PHYUTMI_DPPULLDOWN			BIT(3)
79 #define PHYUTMI_DMPULLDOWN			BIT(2)
80 #define PHYUTMI_FORCESUSPEND			BIT(1)
81 #define PHYUTMI_FORCESLEEP			BIT(0)
82 
83 #define EXYNOS5_DRD_PHYPIPE			0x0c
84 
85 #define EXYNOS5_DRD_PHYCLKRST			0x10
86 #define PHYCLKRST_EN_UTMISUSPEND		BIT(31)
87 #define PHYCLKRST_SSC_REFCLKSEL			GENMASK(30, 23)
88 #define PHYCLKRST_SSC_RANGE			GENMASK(22, 21)
89 #define PHYCLKRST_SSC_EN			BIT(20)
90 #define PHYCLKRST_REF_SSP_EN			BIT(19)
91 #define PHYCLKRST_REF_CLKDIV2			BIT(18)
92 #define PHYCLKRST_MPLL_MULTIPLIER		GENMASK(17, 11)
93 #define PHYCLKRST_MPLL_MULTIPLIER_100MHZ_REF	0x19
94 #define PHYCLKRST_MPLL_MULTIPLIER_50M_REF	0x32
95 #define PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF	0x68
96 #define PHYCLKRST_MPLL_MULTIPLIER_20MHZ_REF	0x7d
97 #define PHYCLKRST_MPLL_MULTIPLIER_19200KHZ_REF	0x02
98 #define PHYCLKRST_FSEL_PIPE			GENMASK(10, 8)
99 #define PHYCLKRST_FSEL_UTMI			GENMASK(7, 5)
100 #define PHYCLKRST_FSEL_PAD_100MHZ		0x27
101 #define PHYCLKRST_FSEL_PAD_24MHZ		0x2a
102 #define PHYCLKRST_FSEL_PAD_20MHZ		0x31
103 #define PHYCLKRST_FSEL_PAD_19_2MHZ		0x38
104 #define PHYCLKRST_RETENABLEN			BIT(4)
105 #define PHYCLKRST_REFCLKSEL			GENMASK(3, 2)
106 #define PHYCLKRST_REFCLKSEL_PAD_REFCLK		0x2
107 #define PHYCLKRST_REFCLKSEL_EXT_REFCLK		0x3
108 #define PHYCLKRST_PORTRESET			BIT(1)
109 #define PHYCLKRST_COMMONONN			BIT(0)
110 
111 #define EXYNOS5_DRD_PHYREG0			0x14
112 #define PHYREG0_SSC_REF_CLK_SEL			BIT(21)
113 #define PHYREG0_SSC_RANGE			BIT(20)
114 #define PHYREG0_CR_WRITE			BIT(19)
115 #define PHYREG0_CR_READ				BIT(18)
116 #define PHYREG0_CR_DATA_IN			GENMASK(17, 2)
117 #define PHYREG0_CR_CAP_DATA			BIT(1)
118 #define PHYREG0_CR_CAP_ADDR			BIT(0)
119 
120 #define EXYNOS5_DRD_PHYREG1			0x18
121 #define PHYREG0_CR_DATA_OUT			GENMASK(16, 1)
122 #define PHYREG1_CR_ACK				BIT(0)
123 
124 #define EXYNOS5_DRD_PHYPARAM0			0x1c
125 #define PHYPARAM0_REF_USE_PAD			BIT(31)
126 #define PHYPARAM0_REF_LOSLEVEL			GENMASK(30, 26)
127 #define PHYPARAM0_REF_LOSLEVEL_VAL		0x9
128 #define PHYPARAM0_TXVREFTUNE			GENMASK(25, 22)
129 #define PHYPARAM0_TXRISETUNE			GENMASK(21, 20)
130 #define PHYPARAM0_TXRESTUNE			GENMASK(19, 18)
131 #define PHYPARAM0_TXPREEMPPULSETUNE		BIT(17)
132 #define PHYPARAM0_TXPREEMPAMPTUNE		GENMASK(16, 15)
133 #define PHYPARAM0_TXHSXVTUNE			GENMASK(14, 13)
134 #define PHYPARAM0_TXFSLSTUNE			GENMASK(12, 9)
135 #define PHYPARAM0_SQRXTUNE			GENMASK(8, 6)
136 #define PHYPARAM0_OTGTUNE			GENMASK(5, 3)
137 #define PHYPARAM0_COMPDISTUNE			GENMASK(2, 0)
138 
139 #define EXYNOS5_DRD_PHYPARAM1			0x20
140 #define PHYPARAM1_PCS_TXDEEMPH			GENMASK(4, 0)
141 #define PHYPARAM1_PCS_TXDEEMPH_VAL		0x1c
142 
143 #define EXYNOS5_DRD_PHYTERM			0x24
144 
145 #define EXYNOS5_DRD_PHYTEST			0x28
146 #define PHYTEST_POWERDOWN_SSP			BIT(3)
147 #define PHYTEST_POWERDOWN_HSP			BIT(2)
148 
149 #define EXYNOS5_DRD_PHYADP			0x2c
150 
151 #define EXYNOS5_DRD_PHYUTMICLKSEL		0x30
152 #define PHYUTMICLKSEL_UTMI_CLKSEL		BIT(2)
153 
154 #define EXYNOS5_DRD_PHYRESUME			0x34
155 
156 #define EXYNOS5_DRD_LINKPORT			0x44
157 #define LINKPORT_HOST_U3_PORT_DISABLE		BIT(8)
158 #define LINKPORT_HOST_U2_PORT_DISABLE		BIT(7)
159 #define LINKPORT_HOST_PORT_OVCR_U3		BIT(5)
160 #define LINKPORT_HOST_PORT_OVCR_U2		BIT(4)
161 #define LINKPORT_HOST_PORT_OVCR_U3_SEL		BIT(3)
162 #define LINKPORT_HOST_PORT_OVCR_U2_SEL		BIT(2)
163 
164 /* USB 3.0 DRD PHY SS Function Control Reg; accessed by CR_PORT */
165 #define EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN		(0x15)
166 #define LOSLEVEL_OVRD_IN_LOS_BIAS_5420			(0x5 << 13)
167 #define LOSLEVEL_OVRD_IN_LOS_BIAS_DEFAULT		(0x0 << 13)
168 #define LOSLEVEL_OVRD_IN_EN				(0x1 << 10)
169 #define LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT		(0x9 << 0)
170 
171 #define EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN	(0x12)
172 #define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420		(0x5 << 13)
173 #define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_DEFAULT		(0x4 << 13)
174 
175 #define EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG		(0x1010)
176 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M		(0x4 << 4)
177 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M		(0x8 << 4)
178 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_25M_26M		(0x8 << 4)
179 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M	(0x20 << 4)
180 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_62M5		(0x20 << 4)
181 #define LANE0_TX_DEBUG_RXDET_MEAS_TIME_96M_100M		(0x40 << 4)
182 
183 /* Exynos7870: USB DRD PHY registers */
184 #define EXYNOS7870_DRD_PHYPCSVAL		0x3C
185 #define PHYPCSVAL_PCS_RX_LOS_MASK		GENMASK(9, 0)
186 
187 #define EXYNOS7870_DRD_PHYPARAM2		0x50
188 #define PHYPARAM2_TX_VBOOST_LVL		        GENMASK(6, 4)
189 #define PHYPARAM2_LOS_BIAS			GENMASK(2, 0)
190 
191 #define EXYNOS7870_DRD_HSPHYCTRL		0x54
192 #define HSPHYCTRL_PHYSWRSTALL			BIT(31)
193 #define HSPHYCTRL_SIDDQ				BIT(6)
194 #define HSPHYCTRL_PHYSWRST			BIT(0)
195 
196 #define EXYNOS7870_DRD_HSPHYPLLTUNE		0x70
197 #define HSPHYPLLTUNE_PLL_B_TUNE			BIT(6)
198 #define HSPHYPLLTUNE_PLL_I_TUNE			GENMASK(5, 4)
199 #define HSPHYPLLTUNE_PLL_P_TUNE			GENMASK(3, 0)
200 
201 /* Exynos850: USB DRD PHY registers */
202 #define EXYNOS850_DRD_LINKCTRL			0x04
203 #define LINKCTRL_FORCE_RXELECIDLE		BIT(18)
204 #define LINKCTRL_FORCE_PHYSTATUS		BIT(17)
205 #define LINKCTRL_FORCE_PIPE_EN			BIT(16)
206 #define LINKCTRL_FORCE_QACT			BIT(8)
207 #define LINKCTRL_BUS_FILTER_BYPASS		GENMASK(7, 4)
208 
209 #define EXYNOS850_DRD_LINKPORT			0x08
210 #define LINKPORT_HOST_NUM_U3			GENMASK(19, 16)
211 #define LINKPORT_HOST_NUM_U2			GENMASK(15, 12)
212 
213 #define EXYNOS850_DRD_CLKRST			0x20
214 /*
215  * On versions without SS ports (like E850), bit 3 is for the 2.0 phy (HS),
216  * while on versions with (like gs101), bits 2 and 3 are for the 3.0 phy (SS)
217  * and bits 12 & 13 for the 2.0 phy.
218  */
219 #define CLKRST_PHY20_SW_POR			BIT(13)
220 #define CLKRST_PHY20_SW_POR_SEL			BIT(12)
221 #define CLKRST_LINK_PCLK_SEL			BIT(7)
222 #define CLKRST_PHY_SW_RST			BIT(3)
223 #define CLKRST_PHY_RESET_SEL			BIT(2)
224 #define CLKRST_PORT_RST				BIT(1)
225 #define CLKRST_LINK_SW_RST			BIT(0)
226 
227 #define EXYNOS850_DRD_SSPPLLCTL			0x30
228 #define SSPPLLCTL_FSEL				GENMASK(2, 0)
229 
230 #define EXYNOS850_DRD_UTMI			0x50
231 #define UTMI_FORCE_VBUSVALID			BIT(5)
232 #define UTMI_FORCE_BVALID			BIT(4)
233 #define UTMI_DP_PULLDOWN			BIT(3)
234 #define UTMI_DM_PULLDOWN			BIT(2)
235 #define UTMI_FORCE_SUSPEND			BIT(1)
236 #define UTMI_FORCE_SLEEP			BIT(0)
237 
238 #define EXYNOS850_DRD_HSP			0x54
239 #define HSP_FSV_OUT_EN				BIT(24)
240 #define HSP_VBUSVLDEXTSEL			BIT(13)
241 #define HSP_VBUSVLDEXT				BIT(12)
242 #define HSP_EN_UTMISUSPEND			BIT(9)
243 #define HSP_COMMONONN				BIT(8)
244 
245 #define EXYNOS850_DRD_HSPPARACON		0x58
246 #define HSPPARACON_TXVREF			GENMASK(31, 28)
247 #define HSPPARACON_TXRISE			GENMASK(25, 24)
248 #define HSPPARACON_TXRES			GENMASK(22, 21)
249 #define HSPPARACON_TXPREEMPPULSE		BIT(20)
250 #define HSPPARACON_TXPREEMPAMP			GENMASK(19, 18)
251 #define HSPPARACON_TXHSXV			GENMASK(17, 16)
252 #define HSPPARACON_TXFSLS			GENMASK(15, 12)
253 #define HSPPARACON_SQRX				GENMASK(10, 8)
254 #define HSPPARACON_OTG				GENMASK(6, 4)
255 #define HSPPARACON_COMPDIS			GENMASK(2, 0)
256 
257 #define EXYNOS850_DRD_HSP_TEST			0x5c
258 #define HSP_TEST_SIDDQ				BIT(24)
259 
260 #define EXYNOSAUTOV920_DRD_HSP_CLKRST		0x100
261 #define HSPCLKRST_PHY20_SW_PORTRESET		BIT(3)
262 #define HSPCLKRST_PHY20_SW_POR			BIT(1)
263 #define HSPCLKRST_PHY20_SW_POR_SEL		BIT(0)
264 
265 #define EXYNOSAUTOV920_DRD_HSPCTL		0x104
266 #define HSPCTRL_VBUSVLDEXTSEL			BIT(13)
267 #define HSPCTRL_VBUSVLDEXT			BIT(12)
268 #define HSPCTRL_EN_UTMISUSPEND			BIT(9)
269 #define HSPCTRL_COMMONONN			BIT(8)
270 
271 #define EXYNOSAUTOV920_DRD_HSP_TEST		0x10c
272 
273 #define EXYNOSAUTOV920_DRD_HSPPLLTUNE		0x110
274 #define HSPPLLTUNE_FSEL				GENMASK(18, 16)
275 
276 /* ExynosAutov920 phy usb31drd port reg */
277 #define EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL	0x000
278 #define PHY_RST_CTRL_PIPE_LANE0_RESET_N_OVRD_EN	BIT(5)
279 #define PHY_RST_CTRL_PIPE_LANE0_RESET_N		BIT(4)
280 #define PHY_RST_CTRL_PHY_RESET_OVRD_EN		BIT(1)
281 #define PHY_RST_CTRL_PHY_RESET			BIT(0)
282 
283 #define EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0	0x0004
284 #define PHY_CR_PARA_CON0_PHY0_CR_PARA_ADDR		GENMASK(31, 16)
285 #define PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK		BIT(8)
286 #define PHY_CR_PARA_CON0_PHY0_CR_PARA_ACK		BIT(4)
287 #define PHY_CR_PARA_CON0_PHY0_CR_PARA_SEL		BIT(0)
288 
289 #define EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON1	0x0008
290 
291 #define EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON2	0x000c
292 #define PHY_CR_PARA_CON2_PHY0_CR_PARA_WR_EN		BIT(0)
293 #define PHY_CR_PARA_CON2_PHY0_CR_PARA_WR_DATA		GENMASK(31, 16)
294 
295 #define EXYNOSAUTOV920_USB31DRD_PHY_CONFIG0	0x100
296 #define PHY_CONFIG0_PHY0_PMA_PWR_STABLE		BIT(14)
297 #define PHY_CONFIG0_PHY0_PCS_PWR_STABLE		BIT(13)
298 #define PHY_CONFIG0_PHY0_ANA_PWR_EN		BIT(1)
299 
300 #define EXYNOSAUTOV920_USB31DRD_PHY_CONFIG7	0x11c
301 #define PHY_CONFIG7_PHY_TEST_POWERDOWN		BIT(24)
302 
303 #define EXYNOSAUTOV920_USB31DRD_PHY_CONFIG4	0x110
304 #define PHY_CONFIG4_PIPE_RX0_SRIS_MODE_EN	BIT(2)
305 
306 /* Exynos9 - GS101 */
307 #define EXYNOS850_DRD_SECPMACTL			0x48
308 #define SECPMACTL_PMA_ROPLL_REF_CLK_SEL		GENMASK(13, 12)
309 #define SECPMACTL_PMA_LCPLL_REF_CLK_SEL		GENMASK(11, 10)
310 #define SECPMACTL_PMA_REF_FREQ_SEL		GENMASK(9, 8)
311 #define SECPMACTL_PMA_LOW_PWR			BIT(4)
312 #define SECPMACTL_PMA_TRSV_SW_RST		BIT(3)
313 #define SECPMACTL_PMA_CMN_SW_RST		BIT(2)
314 #define SECPMACTL_PMA_INIT_SW_RST		BIT(1)
315 #define SECPMACTL_PMA_APB_SW_RST		BIT(0)
316 
317 /* PMA registers */
318 #define EXYNOS9_PMA_USBDP_CMN_REG0008		0x0020
319 #define CMN_REG0008_OVRD_AUX_EN			BIT(3)
320 #define CMN_REG0008_AUX_EN			BIT(2)
321 
322 #define EXYNOS9_PMA_USBDP_CMN_REG00B8		0x02e0
323 #define CMN_REG00B8_LANE_MUX_SEL_DP		GENMASK(3, 0)
324 #define CMN_REG00B8_LANE_MUX_SEL_DP_LANE3	BIT(3)
325 #define CMN_REG00B8_LANE_MUX_SEL_DP_LANE2	BIT(2)
326 #define CMN_REG00B8_LANE_MUX_SEL_DP_LANE1	BIT(1)
327 #define CMN_REG00B8_LANE_MUX_SEL_DP_LANE0	BIT(0)
328 
329 #define EXYNOS9_PMA_USBDP_CMN_REG01C0		0x0700
330 #define CMN_REG01C0_ANA_LCPLL_LOCK_DONE		BIT(7)
331 #define CMN_REG01C0_ANA_LCPLL_AFC_DONE		BIT(6)
332 
333 /* these have similar register layout, for lanes 0 and 2 */
334 #define EXYNOS9_PMA_USBDP_TRSV_REG03C3			0x0f0c
335 #define EXYNOS9_PMA_USBDP_TRSV_REG07C3			0x1f0c
336 #define TRSV_REG03C3_LN0_MON_RX_CDR_AFC_DONE		BIT(3)
337 #define TRSV_REG03C3_LN0_MON_RX_CDR_CAL_DONE		BIT(2)
338 #define TRSV_REG03C3_LN0_MON_RX_CDR_FLD_PLL_MODE_DONE	BIT(1)
339 #define TRSV_REG03C3_LN0_MON_RX_CDR_LOCK_DONE		BIT(0)
340 
341 /* TRSV_REG0413 and TRSV_REG0813 have similar register layout */
342 #define EXYNOS9_PMA_USBDP_TRSV_REG0413		0x104c
343 #define TRSV_REG0413_OVRD_LN1_TX_RXD_COMP_EN	BIT(7)
344 #define TRSV_REG0413_OVRD_LN1_TX_RXD_EN		BIT(5)
345 
346 #define EXYNOS9_PMA_USBDP_TRSV_REG0813		0x204c
347 #define TRSV_REG0813_OVRD_LN3_TX_RXD_COMP_EN	BIT(7)
348 #define TRSV_REG0813_OVRD_LN3_TX_RXD_EN		BIT(5)
349 
350 /* PCS registers */
351 #define EXYNOS9_PCS_NS_VEC_PS1_N1		0x010c
352 #define EXYNOS9_PCS_NS_VEC_PS2_N0		0x0110
353 #define EXYNOS9_PCS_NS_VEC_PS3_N0		0x0118
354 #define NS_VEC_NS_REQ				GENMASK(31, 24)
355 #define NS_VEC_ENABLE_TIMER			BIT(22)
356 #define NS_VEC_SEL_TIMEOUT			GENMASK(21, 20)
357 #define NS_VEC_INV_MASK				GENMASK(19, 16)
358 #define NS_VEC_COND_MASK			GENMASK(11, 8)
359 #define NS_VEC_EXP_COND				GENMASK(3, 0)
360 
361 #define EXYNOS9_PCS_OUT_VEC_2			0x014c
362 #define EXYNOS9_PCS_OUT_VEC_3			0x0150
363 #define PCS_OUT_VEC_B9_DYNAMIC			BIT(19)
364 #define PCS_OUT_VEC_B9_SEL_OUT			BIT(18)
365 #define PCS_OUT_VEC_B8_DYNAMIC			BIT(17)
366 #define PCS_OUT_VEC_B8_SEL_OUT			BIT(16)
367 #define PCS_OUT_VEC_B7_DYNAMIC			BIT(15)
368 #define PCS_OUT_VEC_B7_SEL_OUT			BIT(14)
369 #define PCS_OUT_VEC_B6_DYNAMIC			BIT(13)
370 #define PCS_OUT_VEC_B6_SEL_OUT			BIT(12)
371 #define PCS_OUT_VEC_B5_DYNAMIC			BIT(11)
372 #define PCS_OUT_VEC_B5_SEL_OUT			BIT(10)
373 #define PCS_OUT_VEC_B4_DYNAMIC			BIT(9)
374 #define PCS_OUT_VEC_B4_SEL_OUT			BIT(8)
375 #define PCS_OUT_VEC_B3_DYNAMIC			BIT(7)
376 #define PCS_OUT_VEC_B3_SEL_OUT			BIT(6)
377 #define PCS_OUT_VEC_B2_DYNAMIC			BIT(5)
378 #define PCS_OUT_VEC_B2_SEL_OUT			BIT(4)
379 #define PCS_OUT_VEC_B1_DYNAMIC			BIT(3)
380 #define PCS_OUT_VEC_B1_SEL_OUT			BIT(2)
381 #define PCS_OUT_VEC_B0_DYNAMIC			BIT(1)
382 #define PCS_OUT_VEC_B0_SEL_OUT			BIT(0)
383 
384 #define EXYNOS9_PCS_TIMEOUT_0			0x0170
385 
386 #define EXYNOS9_PCS_TIMEOUT_3			0x017c
387 
388 #define EXYNOS9_PCS_EBUF_PARAM			0x0304
389 #define EBUF_PARAM_SKP_REMOVE_TH_EMPTY_MODE	GENMASK(29, 24)
390 
391 #define EXYNOS9_PCS_BACK_END_MODE_VEC		0x030c
392 #define BACK_END_MODE_VEC_FORCE_EBUF_EMPTY_MODE	BIT(1)
393 #define BACK_END_MODE_VEC_DISABLE_DATA_MASK	BIT(0)
394 
395 #define EXYNOS9_PCS_RX_CONTROL			0x03f0
396 #define RX_CONTROL_EN_BLOCK_ALIGNER_TYPE_B	BIT(22)
397 
398 #define EXYNOS9_PCS_RX_CONTROL_DEBUG		0x03f4
399 #define RX_CONTROL_DEBUG_EN_TS_CHECK		BIT(5)
400 #define RX_CONTROL_DEBUG_NUM_COM_FOUND		GENMASK(3, 0)
401 
402 #define EXYNOS9_PCS_LOCAL_COEF			0x040c
403 #define LOCAL_COEF_PMA_CENTER_COEF		GENMASK(21, 16)
404 #define LOCAL_COEF_LF				GENMASK(13, 8)
405 #define LOCAL_COEF_FS				GENMASK(5, 0)
406 
407 #define EXYNOS9_PCS_HS_TX_COEF_MAP_0		0x0410
408 #define HS_TX_COEF_MAP_0_SSTX_DEEMP		GENMASK(17, 12)
409 #define HS_TX_COEF_MAP_0_SSTX_LEVEL		GENMASK(11, 6)
410 #define HS_TX_COEF_MAP_0_SSTX_PRE_SHOOT		GENMASK(5, 0)
411 
412 
413 #define KHZ	1000
414 #define MHZ	(KHZ * KHZ)
415 
416 #define PHY_TUNING_ENTRY_PHY(o, m, v) {	\
417 		.off = (o),		\
418 		.mask = (m),		\
419 		.val = (v),		\
420 		.region = PTR_PHY	\
421 	}
422 
423 #define PHY_TUNING_ENTRY_PCS(o, m, v) {	\
424 		.off = (o),		\
425 		.mask = (m),		\
426 		.val = (v),		\
427 		.region = PTR_PCS	\
428 	}
429 
430 #define PHY_TUNING_ENTRY_PMA(o, m, v) {	\
431 		.off = (o),		\
432 		.mask = (m),		\
433 		.val = (v),		\
434 		.region = PTR_PMA,	\
435 	}
436 
437 #define PHY_TUNING_ENTRY_LAST { .region = PTR_INVALID }
438 
439 #define for_each_phy_tune(tune) \
440 	for (; (tune)->region != PTR_INVALID; ++(tune))
441 
442 struct exynos5_usbdrd_phy_tuning {
443 	u32 off;
444 	u32 mask;
445 	u32 val;
446 	char region;
447 #define PTR_INVALID	0
448 #define PTR_PHY		1
449 #define PTR_PCS		2
450 #define PTR_PMA		3
451 };
452 
453 enum exynos5_usbdrd_phy_tuning_state {
454 	PTS_UTMI_POSTINIT,
455 	PTS_PIPE3_PREINIT,
456 	PTS_PIPE3_INIT,
457 	PTS_PIPE3_POSTINIT,
458 	PTS_PIPE3_POSTLOCK,
459 	PTS_MAX,
460 };
461 
462 enum exynos5_usbdrd_phy_id {
463 	EXYNOS5_DRDPHY_UTMI,
464 	EXYNOS5_DRDPHY_PIPE3,
465 	EXYNOS5_DRDPHYS_NUM,
466 };
467 
468 struct phy_usb_instance;
469 struct exynos5_usbdrd_phy;
470 
471 struct exynos5_usbdrd_phy_config {
472 	u32 id;
473 	void (*phy_isol)(struct phy_usb_instance *inst, bool isolate);
474 	void (*phy_init)(struct exynos5_usbdrd_phy *phy_drd);
475 	unsigned int (*set_refclk)(struct phy_usb_instance *inst);
476 };
477 
478 struct exynos5_usbdrd_phy_drvdata {
479 	const struct exynos5_usbdrd_phy_config *phy_cfg;
480 	const struct exynos5_usbdrd_phy_tuning **phy_tunes;
481 	const struct phy_ops *phy_ops;
482 	const char * const *clk_names;
483 	int n_clks;
484 	const char * const *core_clk_names;
485 	int n_core_clks;
486 	const char * const *regulator_names;
487 	int n_regulators;
488 	u32 pmu_offset_usbdrd0_phy;
489 	u32 pmu_offset_usbdrd0_phy_ss;
490 	u32 pmu_offset_usbdrd1_phy;
491 };
492 
493 /**
494  * struct exynos5_usbdrd_phy - driver data for USB 3.0 PHY
495  * @dev: pointer to device instance of this platform device
496  * @reg_phy: usb phy controller register memory base
497  * @reg_pcs: usb phy physical coding sublayer register memory base
498  * @reg_pma: usb phy physical media attachment register memory base
499  * @clks: clocks for register access
500  * @core_clks: core clocks for phy (ref, pipe3, utmi+, ITP, etc. as required)
501  * @drv_data: pointer to SoC level driver data structure
502  * @hs_phy: pointer to non-Samsung IP high-speed phy controller
503  * @phy_mutex: mutex protecting phy_init/exit & TCPC callbacks
504  * @phys: array for 'EXYNOS5_DRDPHYS_NUM' number of PHY
505  *	    instances each with its 'phy' and 'phy_cfg'.
506  * @extrefclk: frequency select settings when using 'separate
507  *	       reference clocks' for SS and HS operations
508  * @regulators: regulators for phy
509  * @sw: TypeC orientation switch handle
510  * @orientation: TypeC connector orientation - normal or flipped
511  */
512 struct exynos5_usbdrd_phy {
513 	struct device *dev;
514 	void __iomem *reg_phy;
515 	void __iomem *reg_pcs;
516 	void __iomem *reg_pma;
517 	struct clk_bulk_data *clks;
518 	struct clk_bulk_data *core_clks;
519 	const struct exynos5_usbdrd_phy_drvdata *drv_data;
520 	struct phy *hs_phy;
521 	struct mutex phy_mutex;
522 	struct phy_usb_instance {
523 		struct phy *phy;
524 		u32 index;
525 		struct regmap *reg_pmu;
526 		u32 pmu_offset;
527 		const struct exynos5_usbdrd_phy_config *phy_cfg;
528 	} phys[EXYNOS5_DRDPHYS_NUM];
529 	u32 extrefclk;
530 	struct regulator_bulk_data *regulators;
531 
532 	struct typec_switch_dev *sw;
533 	enum typec_orientation orientation;
534 };
535 
536 static inline
537 struct exynos5_usbdrd_phy *to_usbdrd_phy(struct phy_usb_instance *inst)
538 {
539 	return container_of((inst), struct exynos5_usbdrd_phy,
540 			    phys[(inst)->index]);
541 }
542 
543 /*
544  * exynos5_rate_to_clk() converts the supplied clock rate to the value that
545  * can be written to the phy register.
546  */
547 static unsigned int exynos5_rate_to_clk(unsigned long rate, u32 *reg)
548 {
549 	/* EXYNOS5_FSEL_MASK */
550 
551 	switch (rate) {
552 	case 9600 * KHZ:
553 		*reg = EXYNOS5_FSEL_9MHZ6;
554 		break;
555 	case 10 * MHZ:
556 		*reg = EXYNOS5_FSEL_10MHZ;
557 		break;
558 	case 12 * MHZ:
559 		*reg = EXYNOS5_FSEL_12MHZ;
560 		break;
561 	case 19200 * KHZ:
562 		*reg = EXYNOS5_FSEL_19MHZ2;
563 		break;
564 	case 20 * MHZ:
565 		*reg = EXYNOS5_FSEL_20MHZ;
566 		break;
567 	case 24 * MHZ:
568 		*reg = EXYNOS5_FSEL_24MHZ;
569 		break;
570 	case 26 * MHZ:
571 		*reg = EXYNOS5_FSEL_26MHZ;
572 		break;
573 	case 50 * MHZ:
574 		*reg = EXYNOS5_FSEL_50MHZ;
575 		break;
576 	default:
577 		return -EINVAL;
578 	}
579 
580 	return 0;
581 }
582 
583 static void exynos5_usbdrd_phy_isol(struct phy_usb_instance *inst,
584 				    bool isolate)
585 {
586 	unsigned int val;
587 
588 	if (!inst->reg_pmu)
589 		return;
590 
591 	val = isolate ? 0 : EXYNOS4_PHY_ENABLE;
592 
593 	regmap_update_bits(inst->reg_pmu, inst->pmu_offset,
594 			   EXYNOS4_PHY_ENABLE, val);
595 }
596 
597 /*
598  * Sets the pipe3 phy's clk as EXTREFCLK (XXTI) which is internal clock
599  * from clock core. Further sets multiplier values and spread spectrum
600  * clock settings for SuperSpeed operations.
601  */
602 static unsigned int
603 exynos5_usbdrd_pipe3_set_refclk(struct phy_usb_instance *inst)
604 {
605 	u32 reg;
606 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
607 
608 	/* restore any previous reference clock settings */
609 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
610 
611 	/* Use EXTREFCLK as ref clock */
612 	reg &= ~PHYCLKRST_REFCLKSEL;
613 	reg |= FIELD_PREP(PHYCLKRST_REFCLKSEL, PHYCLKRST_REFCLKSEL_EXT_REFCLK);
614 
615 	/* FSEL settings corresponding to reference clock */
616 	reg &= ~(PHYCLKRST_FSEL_PIPE |
617 		 PHYCLKRST_MPLL_MULTIPLIER |
618 		 PHYCLKRST_SSC_REFCLKSEL);
619 	switch (phy_drd->extrefclk) {
620 	case EXYNOS5_FSEL_50MHZ:
621 		reg |= (FIELD_PREP(PHYCLKRST_SSC_REFCLKSEL, 0x00) |
622 			FIELD_PREP(PHYCLKRST_MPLL_MULTIPLIER,
623 				   PHYCLKRST_MPLL_MULTIPLIER_50M_REF));
624 		break;
625 	case EXYNOS5_FSEL_24MHZ:
626 		reg |= (FIELD_PREP(PHYCLKRST_SSC_REFCLKSEL, 0x88) |
627 			FIELD_PREP(PHYCLKRST_MPLL_MULTIPLIER,
628 				   PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF));
629 		break;
630 	case EXYNOS5_FSEL_20MHZ:
631 		reg |= (FIELD_PREP(PHYCLKRST_SSC_REFCLKSEL, 0x00) |
632 			FIELD_PREP(PHYCLKRST_MPLL_MULTIPLIER,
633 				   PHYCLKRST_MPLL_MULTIPLIER_20MHZ_REF));
634 		break;
635 	case EXYNOS5_FSEL_19MHZ2:
636 		reg |= (FIELD_PREP(PHYCLKRST_SSC_REFCLKSEL, 0x88) |
637 			FIELD_PREP(PHYCLKRST_MPLL_MULTIPLIER,
638 				   PHYCLKRST_MPLL_MULTIPLIER_19200KHZ_REF));
639 		break;
640 	default:
641 		dev_dbg(phy_drd->dev, "unsupported ref clk\n");
642 		break;
643 	}
644 
645 	return reg;
646 }
647 
648 /*
649  * Sets the utmi phy's clk as EXTREFCLK (XXTI) which is internal clock
650  * from clock core. Further sets the FSEL values for HighSpeed operations.
651  */
652 static unsigned int
653 exynos5_usbdrd_utmi_set_refclk(struct phy_usb_instance *inst)
654 {
655 	u32 reg;
656 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
657 
658 	/* restore any previous reference clock settings */
659 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
660 
661 	reg &= ~PHYCLKRST_REFCLKSEL;
662 	reg |= FIELD_PREP(PHYCLKRST_REFCLKSEL, PHYCLKRST_REFCLKSEL_EXT_REFCLK);
663 
664 	reg &= ~(PHYCLKRST_FSEL_UTMI |
665 		 PHYCLKRST_MPLL_MULTIPLIER |
666 		 PHYCLKRST_SSC_REFCLKSEL);
667 	reg |= FIELD_PREP(PHYCLKRST_FSEL_UTMI, phy_drd->extrefclk);
668 
669 	return reg;
670 }
671 
672 static void
673 exynos5_usbdrd_apply_phy_tunes(struct exynos5_usbdrd_phy *phy_drd,
674 			       enum exynos5_usbdrd_phy_tuning_state state)
675 {
676 	const struct exynos5_usbdrd_phy_tuning *tune;
677 
678 	tune = phy_drd->drv_data->phy_tunes[state];
679 	if (!tune)
680 		return;
681 
682 	for_each_phy_tune(tune) {
683 		void __iomem *reg_base;
684 		u32 reg = 0;
685 
686 		switch (tune->region) {
687 		case PTR_PHY:
688 			reg_base = phy_drd->reg_phy;
689 			break;
690 		case PTR_PCS:
691 			reg_base = phy_drd->reg_pcs;
692 			break;
693 		case PTR_PMA:
694 			reg_base = phy_drd->reg_pma;
695 			break;
696 		default:
697 			dev_warn_once(phy_drd->dev,
698 				      "unknown phy region %d\n", tune->region);
699 			continue;
700 		}
701 
702 		if (~tune->mask) {
703 			reg = readl(reg_base + tune->off);
704 			reg &= ~tune->mask;
705 		}
706 		reg |= tune->val;
707 		writel(reg, reg_base + tune->off);
708 	}
709 }
710 
711 static void exynos5_usbdrd_pipe3_init(struct exynos5_usbdrd_phy *phy_drd)
712 {
713 	u32 reg;
714 
715 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
716 	/* Set Tx De-Emphasis level */
717 	reg &= ~PHYPARAM1_PCS_TXDEEMPH;
718 	reg |= FIELD_PREP(PHYPARAM1_PCS_TXDEEMPH, PHYPARAM1_PCS_TXDEEMPH_VAL);
719 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
720 
721 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
722 	reg &= ~PHYTEST_POWERDOWN_SSP;
723 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
724 }
725 
726 static void
727 exynos5_usbdrd_usbdp_g2_v4_ctrl_pma_ready(struct exynos5_usbdrd_phy *phy_drd)
728 {
729 	void __iomem *regs_base = phy_drd->reg_phy;
730 	u32 reg;
731 
732 	/* link pipe_clock selection to pclk of PMA */
733 	reg = readl(regs_base + EXYNOS850_DRD_CLKRST);
734 	reg |= CLKRST_LINK_PCLK_SEL;
735 	writel(reg, regs_base + EXYNOS850_DRD_CLKRST);
736 
737 	reg = readl(regs_base + EXYNOS850_DRD_SECPMACTL);
738 	reg &= ~SECPMACTL_PMA_REF_FREQ_SEL;
739 	reg |= FIELD_PREP(SECPMACTL_PMA_REF_FREQ_SEL, 1);
740 	/* SFR reset */
741 	reg |= (SECPMACTL_PMA_LOW_PWR | SECPMACTL_PMA_APB_SW_RST);
742 	reg &= ~(SECPMACTL_PMA_ROPLL_REF_CLK_SEL |
743 		 SECPMACTL_PMA_LCPLL_REF_CLK_SEL);
744 	/* PMA power off */
745 	reg |= (SECPMACTL_PMA_TRSV_SW_RST | SECPMACTL_PMA_CMN_SW_RST |
746 		SECPMACTL_PMA_INIT_SW_RST);
747 	writel(reg, regs_base + EXYNOS850_DRD_SECPMACTL);
748 
749 	udelay(1);
750 
751 	reg = readl(regs_base + EXYNOS850_DRD_SECPMACTL);
752 	reg &= ~SECPMACTL_PMA_LOW_PWR;
753 	writel(reg, regs_base + EXYNOS850_DRD_SECPMACTL);
754 
755 	udelay(1);
756 
757 	/* release override */
758 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
759 	reg &= ~LINKCTRL_FORCE_PIPE_EN;
760 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
761 
762 	udelay(1);
763 
764 	/* APB enable */
765 	reg = readl(regs_base + EXYNOS850_DRD_SECPMACTL);
766 	reg &= ~SECPMACTL_PMA_APB_SW_RST;
767 	writel(reg, regs_base + EXYNOS850_DRD_SECPMACTL);
768 }
769 
770 static void
771 exynos5_usbdrd_usbdp_g2_v4_pma_lane_mux_sel(struct exynos5_usbdrd_phy *phy_drd)
772 {
773 	void __iomem *regs_base = phy_drd->reg_pma;
774 	u32 reg;
775 
776 	/* lane configuration: USB on all lanes */
777 	reg = readl(regs_base + EXYNOS9_PMA_USBDP_CMN_REG00B8);
778 	reg &= ~CMN_REG00B8_LANE_MUX_SEL_DP;
779 	/*
780 	 * USB on lanes 0 & 1 in normal mode, or 2 & 3 if reversed, DP on the
781 	 * other ones.
782 	 */
783 	reg |= FIELD_PREP(CMN_REG00B8_LANE_MUX_SEL_DP,
784 			  ((phy_drd->orientation == TYPEC_ORIENTATION_NORMAL)
785 			   ? (CMN_REG00B8_LANE_MUX_SEL_DP_LANE3
786 			      | CMN_REG00B8_LANE_MUX_SEL_DP_LANE2)
787 			   : (CMN_REG00B8_LANE_MUX_SEL_DP_LANE1
788 			      | CMN_REG00B8_LANE_MUX_SEL_DP_LANE0)));
789 	writel(reg, regs_base + EXYNOS9_PMA_USBDP_CMN_REG00B8);
790 
791 	/* override of TX receiver detector and comparator: lane 1 */
792 	reg = readl(regs_base + EXYNOS9_PMA_USBDP_TRSV_REG0413);
793 	if (phy_drd->orientation == TYPEC_ORIENTATION_NORMAL) {
794 		reg &= ~TRSV_REG0413_OVRD_LN1_TX_RXD_COMP_EN;
795 		reg &= ~TRSV_REG0413_OVRD_LN1_TX_RXD_EN;
796 	} else {
797 		reg |= TRSV_REG0413_OVRD_LN1_TX_RXD_COMP_EN;
798 		reg |= TRSV_REG0413_OVRD_LN1_TX_RXD_EN;
799 	}
800 	writel(reg, regs_base + EXYNOS9_PMA_USBDP_TRSV_REG0413);
801 
802 	/* lane 3 */
803 	reg = readl(regs_base + EXYNOS9_PMA_USBDP_TRSV_REG0813);
804 	if (phy_drd->orientation == TYPEC_ORIENTATION_NORMAL) {
805 		reg |= TRSV_REG0813_OVRD_LN3_TX_RXD_COMP_EN;
806 		reg |= TRSV_REG0813_OVRD_LN3_TX_RXD_EN;
807 	} else {
808 		reg &= ~TRSV_REG0813_OVRD_LN3_TX_RXD_COMP_EN;
809 		reg &= ~TRSV_REG0813_OVRD_LN3_TX_RXD_EN;
810 	}
811 	writel(reg, regs_base + EXYNOS9_PMA_USBDP_TRSV_REG0813);
812 }
813 
814 static int
815 exynos5_usbdrd_usbdp_g2_v4_pma_check_pll_lock(struct exynos5_usbdrd_phy *phy_drd)
816 {
817 	static const unsigned int timeout_us = 40000;
818 	static const unsigned int sleep_us = 40;
819 	static const u32 locked = (CMN_REG01C0_ANA_LCPLL_LOCK_DONE |
820 				   CMN_REG01C0_ANA_LCPLL_AFC_DONE);
821 	u32 reg;
822 	int err;
823 
824 	err = readl_poll_timeout(
825 			phy_drd->reg_pma + EXYNOS9_PMA_USBDP_CMN_REG01C0,
826 			reg, (reg & locked) == locked, sleep_us, timeout_us);
827 	if (err)
828 		dev_err(phy_drd->dev,
829 			"timed out waiting for PLL lock: %#.8x\n", reg);
830 
831 	return err;
832 }
833 
834 static void
835 exynos5_usbdrd_usbdp_g2_v4_pma_check_cdr_lock(struct exynos5_usbdrd_phy *phy_drd)
836 {
837 	static const unsigned int timeout_us = 40000;
838 	static const unsigned int sleep_us = 40;
839 	static const u32 locked =
840 		(TRSV_REG03C3_LN0_MON_RX_CDR_AFC_DONE
841 		 | TRSV_REG03C3_LN0_MON_RX_CDR_CAL_DONE
842 		 | TRSV_REG03C3_LN0_MON_RX_CDR_FLD_PLL_MODE_DONE
843 		 | TRSV_REG03C3_LN0_MON_RX_CDR_LOCK_DONE);
844 	u32 reg;
845 	int err;
846 
847 	err = readl_poll_timeout(
848 			/* lane depends on cable orientation */
849 			(phy_drd->reg_pma
850 			 + ((phy_drd->orientation == TYPEC_ORIENTATION_NORMAL)
851 			    ? EXYNOS9_PMA_USBDP_TRSV_REG03C3
852 			    : EXYNOS9_PMA_USBDP_TRSV_REG07C3)),
853 			reg, (reg & locked) == locked, sleep_us, timeout_us);
854 	if (err)
855 		dev_err(phy_drd->dev,
856 			"timed out waiting for CDR(l%d) lock: %#.8x\n",
857 			((phy_drd->orientation == TYPEC_ORIENTATION_NORMAL)
858 			 ? 0
859 			 : 2), reg);
860 }
861 
862 static void exynos5_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
863 {
864 	u32 reg;
865 
866 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
867 	/* Set Loss-of-Signal Detector sensitivity */
868 	reg &= ~PHYPARAM0_REF_LOSLEVEL;
869 	reg |= FIELD_PREP(PHYPARAM0_REF_LOSLEVEL, PHYPARAM0_REF_LOSLEVEL_VAL);
870 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
871 
872 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
873 	/* Set Tx De-Emphasis level */
874 	reg &= ~PHYPARAM1_PCS_TXDEEMPH;
875 	reg |= FIELD_PREP(PHYPARAM1_PCS_TXDEEMPH, PHYPARAM1_PCS_TXDEEMPH_VAL);
876 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM1);
877 
878 	/* UTMI Power Control */
879 	writel(PHYUTMI_OTGDISABLE, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
880 
881 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
882 	reg &= ~PHYTEST_POWERDOWN_HSP;
883 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
884 }
885 
886 static int exynos5_usbdrd_phy_init(struct phy *phy)
887 {
888 	int ret;
889 	u32 reg;
890 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
891 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
892 
893 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
894 	if (ret)
895 		return ret;
896 
897 	/* Reset USB 3.0 PHY */
898 	writel(0x0, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
899 	writel(0x0, phy_drd->reg_phy + EXYNOS5_DRD_PHYRESUME);
900 
901 	/*
902 	 * Setting the Frame length Adj value[6:1] to default 0x20
903 	 * See xHCI 1.0 spec, 5.2.4
904 	 */
905 	reg =	LINKSYSTEM_XHCI_VERSION_CONTROL |
906 		FIELD_PREP(LINKSYSTEM_FLADJ, 0x20);
907 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
908 
909 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
910 	/* Select PHY CLK source */
911 	reg &= ~PHYPARAM0_REF_USE_PAD;
912 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYPARAM0);
913 
914 	/* This bit must be set for both HS and SS operations */
915 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMICLKSEL);
916 	reg |= PHYUTMICLKSEL_UTMI_CLKSEL;
917 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMICLKSEL);
918 
919 	/* UTMI or PIPE3 specific init */
920 	inst->phy_cfg->phy_init(phy_drd);
921 
922 	/* reference clock settings */
923 	reg = inst->phy_cfg->set_refclk(inst);
924 
925 		/* Digital power supply in normal operating mode */
926 	reg |=	PHYCLKRST_RETENABLEN |
927 		/* Enable ref clock for SS function */
928 		PHYCLKRST_REF_SSP_EN |
929 		/* Enable spread spectrum */
930 		PHYCLKRST_SSC_EN |
931 		/* Power down HS Bias and PLL blocks in suspend mode */
932 		PHYCLKRST_COMMONONN |
933 		/* Reset the port */
934 		PHYCLKRST_PORTRESET;
935 
936 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
937 
938 	fsleep(10);
939 
940 	reg &= ~PHYCLKRST_PORTRESET;
941 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
942 
943 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
944 
945 	return 0;
946 }
947 
948 static int exynos5_usbdrd_phy_exit(struct phy *phy)
949 {
950 	int ret;
951 	u32 reg;
952 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
953 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
954 
955 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
956 	if (ret)
957 		return ret;
958 
959 	reg =	PHYUTMI_OTGDISABLE |
960 		PHYUTMI_FORCESUSPEND |
961 		PHYUTMI_FORCESLEEP;
962 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
963 
964 	/* Resetting the PHYCLKRST enable bits to reduce leakage current */
965 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
966 	reg &= ~(PHYCLKRST_REF_SSP_EN |
967 		 PHYCLKRST_SSC_EN |
968 		 PHYCLKRST_COMMONONN);
969 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
970 
971 	/* Control PHYTEST to remove leakage current */
972 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
973 	reg |=	PHYTEST_POWERDOWN_SSP |
974 		PHYTEST_POWERDOWN_HSP;
975 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYTEST);
976 
977 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
978 
979 	return 0;
980 }
981 
982 static int exynos5_usbdrd_phy_power_on(struct phy *phy)
983 {
984 	int ret;
985 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
986 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
987 
988 	dev_dbg(phy_drd->dev, "Request to power_on usbdrd_phy phy\n");
989 
990 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_core_clks,
991 				      phy_drd->core_clks);
992 	if (ret)
993 		return ret;
994 
995 	/* Enable VBUS supply */
996 	ret = regulator_bulk_enable(phy_drd->drv_data->n_regulators,
997 				    phy_drd->regulators);
998 	if (ret) {
999 		dev_err(phy_drd->dev, "Failed to enable PHY regulator(s)\n");
1000 		goto fail_vbus;
1001 	}
1002 
1003 	/* Power-on PHY */
1004 	inst->phy_cfg->phy_isol(inst, false);
1005 
1006 	return 0;
1007 
1008 fail_vbus:
1009 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_core_clks,
1010 				   phy_drd->core_clks);
1011 
1012 	return ret;
1013 }
1014 
1015 static int exynos5_usbdrd_phy_power_off(struct phy *phy)
1016 {
1017 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1018 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1019 
1020 	dev_dbg(phy_drd->dev, "Request to power_off usbdrd_phy phy\n");
1021 
1022 	/* Power-off the PHY */
1023 	inst->phy_cfg->phy_isol(inst, true);
1024 
1025 	/* Disable VBUS supply */
1026 	regulator_bulk_disable(phy_drd->drv_data->n_regulators,
1027 			       phy_drd->regulators);
1028 
1029 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_core_clks,
1030 				   phy_drd->core_clks);
1031 
1032 	return 0;
1033 }
1034 
1035 static int crport_handshake(struct exynos5_usbdrd_phy *phy_drd,
1036 			    u32 val, u32 cmd)
1037 {
1038 	unsigned int result;
1039 	int err;
1040 
1041 	writel(val | cmd, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1042 
1043 	err = readl_poll_timeout(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1,
1044 				 result, (result & PHYREG1_CR_ACK), 1, 100);
1045 	if (err == -ETIMEDOUT) {
1046 		dev_err(phy_drd->dev, "CRPORT handshake timeout1 (0x%08x)\n", val);
1047 		return err;
1048 	}
1049 
1050 	writel(val, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1051 
1052 	err = readl_poll_timeout(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1,
1053 				 result, !(result & PHYREG1_CR_ACK), 1, 100);
1054 	if (err == -ETIMEDOUT) {
1055 		dev_err(phy_drd->dev, "CRPORT handshake timeout2 (0x%08x)\n", val);
1056 		return err;
1057 	}
1058 
1059 	return 0;
1060 }
1061 
1062 static int crport_ctrl_write(struct exynos5_usbdrd_phy *phy_drd,
1063 			     u32 addr, u32 data)
1064 {
1065 	u32 val;
1066 	int ret;
1067 
1068 	/* Write Address */
1069 	val = FIELD_PREP(PHYREG0_CR_DATA_IN, addr);
1070 	writel(val, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1071 	ret = crport_handshake(phy_drd, val, PHYREG0_CR_CAP_ADDR);
1072 	if (ret)
1073 		return ret;
1074 
1075 	/* Write Data */
1076 	val = FIELD_PREP(PHYREG0_CR_DATA_IN, data);
1077 	writel(val, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1078 	ret = crport_handshake(phy_drd, val, PHYREG0_CR_CAP_DATA);
1079 	if (ret)
1080 		return ret;
1081 
1082 	ret = crport_handshake(phy_drd, val, PHYREG0_CR_WRITE);
1083 
1084 	return ret;
1085 }
1086 
1087 /*
1088  * Calibrate few PHY parameters using CR_PORT register to meet
1089  * SuperSpeed requirements on Exynos5420 and Exynos5800 systems,
1090  * which have 28nm USB 3.0 DRD PHY.
1091  */
1092 static int exynos5420_usbdrd_phy_calibrate(struct exynos5_usbdrd_phy *phy_drd)
1093 {
1094 	unsigned int temp;
1095 	int ret = 0;
1096 
1097 	/*
1098 	 * Change los_bias to (0x5) for 28nm PHY from a
1099 	 * default value (0x0); los_level is set as default
1100 	 * (0x9) as also reflected in los_level[30:26] bits
1101 	 * of PHYPARAM0 register.
1102 	 */
1103 	temp = LOSLEVEL_OVRD_IN_LOS_BIAS_5420 |
1104 		LOSLEVEL_OVRD_IN_EN |
1105 		LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT;
1106 	ret = crport_ctrl_write(phy_drd,
1107 				EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN,
1108 				temp);
1109 	if (ret) {
1110 		dev_err(phy_drd->dev,
1111 			"Failed setting Loss-of-Signal level for SuperSpeed\n");
1112 		return ret;
1113 	}
1114 
1115 	/*
1116 	 * Set tx_vboost_lvl to (0x5) for 28nm PHY Tuning,
1117 	 * to raise Tx signal level from its default value of (0x4)
1118 	 */
1119 	temp = TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420;
1120 	ret = crport_ctrl_write(phy_drd,
1121 				EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN,
1122 				temp);
1123 	if (ret) {
1124 		dev_err(phy_drd->dev,
1125 			"Failed setting Tx-Vboost-Level for SuperSpeed\n");
1126 		return ret;
1127 	}
1128 
1129 	/*
1130 	 * Set proper time to wait for RxDetect measurement, for
1131 	 * desired reference clock of PHY, by tuning the CR_PORT
1132 	 * register LANE0.TX_DEBUG which is internal to PHY.
1133 	 * This fixes issue with few USB 3.0 devices, which are
1134 	 * not detected (not even generate interrupts on the bus
1135 	 * on insertion) without this change.
1136 	 * e.g. Samsung SUM-TSB16S 3.0 USB drive.
1137 	 */
1138 	switch (phy_drd->extrefclk) {
1139 	case EXYNOS5_FSEL_50MHZ:
1140 		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M;
1141 		break;
1142 	case EXYNOS5_FSEL_20MHZ:
1143 	case EXYNOS5_FSEL_19MHZ2:
1144 		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M;
1145 		break;
1146 	case EXYNOS5_FSEL_24MHZ:
1147 	default:
1148 		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M;
1149 		break;
1150 	}
1151 
1152 	ret = crport_ctrl_write(phy_drd,
1153 				EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG,
1154 				temp);
1155 	if (ret)
1156 		dev_err(phy_drd->dev,
1157 			"Fail to set RxDet measurement time for SuperSpeed\n");
1158 
1159 	return ret;
1160 }
1161 
1162 static struct phy *exynos5_usbdrd_phy_xlate(struct device *dev,
1163 					const struct of_phandle_args *args)
1164 {
1165 	struct exynos5_usbdrd_phy *phy_drd = dev_get_drvdata(dev);
1166 
1167 	if (WARN_ON(args->args[0] >= EXYNOS5_DRDPHYS_NUM))
1168 		return ERR_PTR(-ENODEV);
1169 
1170 	return phy_drd->phys[args->args[0]].phy;
1171 }
1172 
1173 static int exynos5_usbdrd_phy_calibrate(struct phy *phy)
1174 {
1175 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1176 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1177 
1178 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI)
1179 		return exynos5420_usbdrd_phy_calibrate(phy_drd);
1180 	return 0;
1181 }
1182 
1183 static const struct phy_ops exynos5_usbdrd_phy_ops = {
1184 	.init		= exynos5_usbdrd_phy_init,
1185 	.exit		= exynos5_usbdrd_phy_exit,
1186 	.power_on	= exynos5_usbdrd_phy_power_on,
1187 	.power_off	= exynos5_usbdrd_phy_power_off,
1188 	.calibrate	= exynos5_usbdrd_phy_calibrate,
1189 	.owner		= THIS_MODULE,
1190 };
1191 
1192 static void exynos7870_usbdrd_phy_isol(struct phy_usb_instance *inst,
1193 				       bool isolate)
1194 {
1195 	unsigned int val;
1196 
1197 	if (!inst->reg_pmu)
1198 		return;
1199 
1200 	val = isolate ? 0 : EXYNOS7870_USB2PHY_ENABLE;
1201 
1202 	regmap_update_bits(inst->reg_pmu, inst->pmu_offset,
1203 			   EXYNOS7870_USB2PHY_ENABLE, val);
1204 }
1205 
1206 static void exynos7870_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
1207 {
1208 	u32 reg;
1209 
1210 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
1211 	/* Use PADREFCLK as ref clock */
1212 	reg &= ~PHYCLKRST_REFCLKSEL;
1213 	reg |= FIELD_PREP(PHYCLKRST_REFCLKSEL, PHYCLKRST_REFCLKSEL_PAD_REFCLK);
1214 	/* Select ref clock rate */
1215 	reg &= ~PHYCLKRST_FSEL_UTMI;
1216 	reg &= ~PHYCLKRST_FSEL_PIPE;
1217 	reg |= FIELD_PREP(PHYCLKRST_FSEL_UTMI, phy_drd->extrefclk);
1218 	/* Enable suspend and reset the port */
1219 	reg |= PHYCLKRST_EN_UTMISUSPEND;
1220 	reg |= PHYCLKRST_COMMONONN;
1221 	reg |= PHYCLKRST_PORTRESET;
1222 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
1223 	udelay(10);
1224 
1225 	/* Clear the port reset bit */
1226 	reg &= ~PHYCLKRST_PORTRESET;
1227 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYCLKRST);
1228 
1229 	/* Change PHY PLL tune value */
1230 	reg = readl(phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYPLLTUNE);
1231 	if (phy_drd->extrefclk == EXYNOS5_FSEL_24MHZ)
1232 		reg |= HSPHYPLLTUNE_PLL_B_TUNE;
1233 	else
1234 		reg &= ~HSPHYPLLTUNE_PLL_B_TUNE;
1235 	reg &= ~HSPHYPLLTUNE_PLL_P_TUNE;
1236 	reg |= FIELD_PREP(HSPHYPLLTUNE_PLL_P_TUNE, 14);
1237 	writel(reg, phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYPLLTUNE);
1238 
1239 	/* High-Speed PHY control */
1240 	reg = readl(phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1241 	reg &= ~HSPHYCTRL_SIDDQ;
1242 	reg &= ~HSPHYCTRL_PHYSWRST;
1243 	reg &= ~HSPHYCTRL_PHYSWRSTALL;
1244 	writel(reg, phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1245 	udelay(500);
1246 
1247 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
1248 	/*
1249 	 * Setting the Frame length Adj value[6:1] to default 0x20
1250 	 * See xHCI 1.0 spec, 5.2.4
1251 	 */
1252 	reg |= LINKSYSTEM_XHCI_VERSION_CONTROL;
1253 	reg &= ~LINKSYSTEM_FLADJ;
1254 	reg |= FIELD_PREP(LINKSYSTEM_FLADJ, 0x20);
1255 	/* Set VBUSVALID signal as the VBUS pad is not used */
1256 	reg |= LINKSYSTEM_FORCE_BVALID;
1257 	reg |= LINKSYSTEM_FORCE_VBUSVALID;
1258 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
1259 
1260 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
1261 	/* Release force_sleep & force_suspend */
1262 	reg &= ~PHYUTMI_FORCESLEEP;
1263 	reg &= ~PHYUTMI_FORCESUSPEND;
1264 	/* DP/DM pull down control */
1265 	reg &= ~PHYUTMI_DMPULLDOWN;
1266 	reg &= ~PHYUTMI_DPPULLDOWN;
1267 	reg &= ~PHYUTMI_DRVVBUS;
1268 	/* Set DP-pull up as the VBUS pad is not used */
1269 	reg |= PHYUTMI_VBUSVLDEXTSEL;
1270 	reg |= PHYUTMI_VBUSVLDEXT;
1271 	/* Disable OTG block and VBUS valid comparator */
1272 	reg |= PHYUTMI_OTGDISABLE;
1273 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
1274 
1275 	/* Configure OVC IO usage */
1276 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_LINKPORT);
1277 	reg |= LINKPORT_HOST_PORT_OVCR_U3_SEL | LINKPORT_HOST_PORT_OVCR_U2_SEL;
1278 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_LINKPORT);
1279 
1280 	/* High-Speed PHY swrst */
1281 	reg = readl(phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1282 	reg |= HSPHYCTRL_PHYSWRST;
1283 	writel(reg, phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1284 	udelay(20);
1285 
1286 	/* Clear the PHY swrst bit */
1287 	reg = readl(phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1288 	reg &= ~HSPHYCTRL_PHYSWRST;
1289 	writel(reg, phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1290 
1291 	if (phy_drd->drv_data->phy_tunes)
1292 		exynos5_usbdrd_apply_phy_tunes(phy_drd,
1293 					       PTS_UTMI_POSTINIT);
1294 }
1295 
1296 static int exynos7870_usbdrd_phy_init(struct phy *phy)
1297 {
1298 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1299 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1300 	int ret;
1301 
1302 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1303 	if (ret)
1304 		return ret;
1305 
1306 	/* UTMI or PIPE3 specific init */
1307 	inst->phy_cfg->phy_init(phy_drd);
1308 
1309 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1310 
1311 	return 0;
1312 }
1313 
1314 static int exynos7870_usbdrd_phy_exit(struct phy *phy)
1315 {
1316 	int ret;
1317 	u32 reg;
1318 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1319 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1320 
1321 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1322 	if (ret)
1323 		return ret;
1324 
1325 	/*
1326 	 * Disable the VBUS signal and the ID pull-up resistor.
1327 	 * Enable force-suspend and force-sleep modes.
1328 	 */
1329 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
1330 	reg &= ~(PHYUTMI_DRVVBUS | PHYUTMI_VBUSVLDEXT | PHYUTMI_VBUSVLDEXTSEL);
1331 	reg &= ~PHYUTMI_IDPULLUP;
1332 	reg |= PHYUTMI_FORCESUSPEND | PHYUTMI_FORCESLEEP;
1333 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_PHYUTMI);
1334 
1335 	/* Power down PHY analog blocks */
1336 	reg = readl(phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1337 	reg |= HSPHYCTRL_SIDDQ;
1338 	writel(reg, phy_drd->reg_phy + EXYNOS7870_DRD_HSPHYCTRL);
1339 
1340 	/* Clear VBUSVALID signal as the VBUS pad is not used */
1341 	reg = readl(phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
1342 	reg &= ~(LINKSYSTEM_FORCE_BVALID | LINKSYSTEM_FORCE_VBUSVALID);
1343 	writel(reg, phy_drd->reg_phy + EXYNOS5_DRD_LINKSYSTEM);
1344 
1345 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1346 
1347 	return 0;
1348 }
1349 
1350 static const struct phy_ops exynos7870_usbdrd_phy_ops = {
1351 	.init		= exynos7870_usbdrd_phy_init,
1352 	.exit		= exynos7870_usbdrd_phy_exit,
1353 	.power_on	= exynos5_usbdrd_phy_power_on,
1354 	.power_off	= exynos5_usbdrd_phy_power_off,
1355 	.owner		= THIS_MODULE,
1356 };
1357 
1358 static void exynos2200_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
1359 {
1360 	/* Configure non-Samsung IP PHY, responsible for UTMI */
1361 	phy_init(phy_drd->hs_phy);
1362 }
1363 
1364 static void exynos2200_usbdrd_link_init(struct exynos5_usbdrd_phy *phy_drd)
1365 {
1366 	void __iomem *regs_base = phy_drd->reg_phy;
1367 	u32 reg;
1368 
1369 	/*
1370 	 * Disable HWACG (hardware auto clock gating control). This will force
1371 	 * QACTIVE signal in Q-Channel interface to HIGH level, to make sure
1372 	 * the PHY clock is not gated by the hardware.
1373 	 */
1374 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
1375 	reg |= LINKCTRL_FORCE_QACT;
1376 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
1377 
1378 	/* De-assert link reset */
1379 	reg = readl(regs_base + EXYNOS2200_DRD_CLKRST);
1380 	reg &= ~CLKRST_LINK_SW_RST;
1381 	writel(reg, regs_base + EXYNOS2200_DRD_CLKRST);
1382 
1383 	/* Set link VBUS Valid */
1384 	reg = readl(regs_base + EXYNOS2200_DRD_UTMI);
1385 	reg |= EXYNOS2200_UTMI_FORCE_BVALID | EXYNOS2200_UTMI_FORCE_VBUSVALID;
1386 	writel(reg, regs_base + EXYNOS2200_DRD_UTMI);
1387 }
1388 
1389 static void
1390 exynos2200_usbdrd_link_attach_detach_pipe3_phy(struct phy_usb_instance *inst)
1391 {
1392 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1393 	void __iomem *regs_base = phy_drd->reg_phy;
1394 	u32 reg;
1395 
1396 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
1397 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI) {
1398 		/* force pipe3 signal for link */
1399 		reg &= ~LINKCTRL_FORCE_PHYSTATUS;
1400 		reg |= LINKCTRL_FORCE_PIPE_EN | LINKCTRL_FORCE_RXELECIDLE;
1401 	} else {
1402 		/* disable forcing pipe interface */
1403 		reg &= ~LINKCTRL_FORCE_PIPE_EN;
1404 	}
1405 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
1406 
1407 	reg = readl(regs_base + EXYNOS2200_DRD_HSP_MISC);
1408 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI) {
1409 		/* calibrate only eUSB phy */
1410 		reg |= FIELD_PREP(HSP_MISC_RES_TUNE, RES_TUNE_PHY1);
1411 		reg |= HSP_MISC_SET_REQ_IN2;
1412 	} else {
1413 		/* calibrate for dual phy */
1414 		reg |= FIELD_PREP(HSP_MISC_RES_TUNE, RES_TUNE_PHY1_PHY2);
1415 		reg &= ~HSP_MISC_SET_REQ_IN2;
1416 	}
1417 	writel(reg, regs_base + EXYNOS2200_DRD_HSP_MISC);
1418 
1419 	reg = readl(regs_base + EXYNOS2200_DRD_CLKRST);
1420 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI)
1421 		reg &= ~EXYNOS2200_CLKRST_LINK_PCLK_SEL;
1422 	else
1423 		reg |= EXYNOS2200_CLKRST_LINK_PCLK_SEL;
1424 
1425 	writel(reg, regs_base + EXYNOS2200_DRD_CLKRST);
1426 }
1427 
1428 static int exynos2200_usbdrd_phy_init(struct phy *phy)
1429 {
1430 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1431 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1432 	int ret;
1433 
1434 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI) {
1435 		/* Power-on PHY ... */
1436 		ret = regulator_bulk_enable(phy_drd->drv_data->n_regulators,
1437 					    phy_drd->regulators);
1438 		if (ret) {
1439 			dev_err(phy_drd->dev,
1440 				"Failed to enable PHY regulator(s)\n");
1441 			return ret;
1442 		}
1443 	}
1444 	/*
1445 	 * ... and ungate power via PMU. Without this here, we get an SError
1446 	 * trying to access PMA registers
1447 	 */
1448 	exynos5_usbdrd_phy_isol(inst, false);
1449 
1450 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1451 	if (ret)
1452 		return ret;
1453 
1454 	/* Set up the link controller */
1455 	exynos2200_usbdrd_link_init(phy_drd);
1456 
1457 	/* UTMI or PIPE3 link preparation */
1458 	exynos2200_usbdrd_link_attach_detach_pipe3_phy(inst);
1459 
1460 	/* UTMI or PIPE3 specific init */
1461 	inst->phy_cfg->phy_init(phy_drd);
1462 
1463 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1464 
1465 	return 0;
1466 }
1467 
1468 static int exynos2200_usbdrd_phy_exit(struct phy *phy)
1469 {
1470 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1471 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1472 	void __iomem *regs_base = phy_drd->reg_phy;
1473 	u32 reg;
1474 	int ret;
1475 
1476 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1477 	if (ret)
1478 		return ret;
1479 
1480 	reg = readl(regs_base + EXYNOS2200_DRD_UTMI);
1481 	reg &= ~(EXYNOS2200_UTMI_FORCE_BVALID | EXYNOS2200_UTMI_FORCE_VBUSVALID);
1482 	writel(reg, regs_base + EXYNOS2200_DRD_UTMI);
1483 
1484 	reg = readl(regs_base + EXYNOS2200_DRD_CLKRST);
1485 	reg |= CLKRST_LINK_SW_RST;
1486 	writel(reg, regs_base + EXYNOS2200_DRD_CLKRST);
1487 
1488 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1489 
1490 	exynos5_usbdrd_phy_isol(inst, true);
1491 	return regulator_bulk_disable(phy_drd->drv_data->n_regulators,
1492 				      phy_drd->regulators);
1493 }
1494 
1495 static const struct phy_ops exynos2200_usbdrd_phy_ops = {
1496 	.init		= exynos2200_usbdrd_phy_init,
1497 	.exit		= exynos2200_usbdrd_phy_exit,
1498 	.owner		= THIS_MODULE,
1499 };
1500 
1501 static void
1502 exynos5_usbdrd_usb_v3p1_pipe_override(struct exynos5_usbdrd_phy *phy_drd)
1503 {
1504 	void __iomem *regs_base = phy_drd->reg_phy;
1505 	u32 reg;
1506 
1507 	/* force pipe3 signal for link */
1508 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
1509 	reg &= ~LINKCTRL_FORCE_PHYSTATUS;
1510 	reg |= LINKCTRL_FORCE_PIPE_EN | LINKCTRL_FORCE_RXELECIDLE;
1511 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
1512 
1513 	/* PMA disable */
1514 	reg = readl(regs_base + EXYNOS850_DRD_SECPMACTL);
1515 	reg |= SECPMACTL_PMA_LOW_PWR;
1516 	writel(reg, regs_base + EXYNOS850_DRD_SECPMACTL);
1517 }
1518 
1519 static void exynos850_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
1520 {
1521 	void __iomem *regs_base = phy_drd->reg_phy;
1522 	u32 reg;
1523 	u32 ss_ports;
1524 
1525 	/*
1526 	 * Disable HWACG (hardware auto clock gating control). This will force
1527 	 * QACTIVE signal in Q-Channel interface to HIGH level, to make sure
1528 	 * the PHY clock is not gated by the hardware.
1529 	 */
1530 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
1531 	reg |= LINKCTRL_FORCE_QACT;
1532 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
1533 
1534 	reg = readl(regs_base + EXYNOS850_DRD_LINKPORT);
1535 	ss_ports = FIELD_GET(LINKPORT_HOST_NUM_U3, reg);
1536 
1537 	/* Start PHY Reset (POR=high) */
1538 	reg = readl(regs_base + EXYNOS850_DRD_CLKRST);
1539 	if (ss_ports) {
1540 		reg |= CLKRST_PHY20_SW_POR;
1541 		reg |= CLKRST_PHY20_SW_POR_SEL;
1542 		reg |= CLKRST_PHY_RESET_SEL;
1543 	}
1544 	reg |= CLKRST_PHY_SW_RST;
1545 	writel(reg, regs_base + EXYNOS850_DRD_CLKRST);
1546 
1547 	/* Enable UTMI+ */
1548 	reg = readl(regs_base + EXYNOS850_DRD_UTMI);
1549 	reg &= ~(UTMI_FORCE_SUSPEND | UTMI_FORCE_SLEEP | UTMI_DP_PULLDOWN |
1550 		 UTMI_DM_PULLDOWN);
1551 	writel(reg, regs_base + EXYNOS850_DRD_UTMI);
1552 
1553 	/* Set PHY clock and control HS PHY */
1554 	reg = readl(regs_base + EXYNOS850_DRD_HSP);
1555 	reg |= HSP_EN_UTMISUSPEND | HSP_COMMONONN;
1556 	writel(reg, regs_base + EXYNOS850_DRD_HSP);
1557 
1558 	/* Set VBUS Valid and D+ pull-up control by VBUS pad usage */
1559 	reg = readl(regs_base + EXYNOS850_DRD_LINKCTRL);
1560 	reg |= FIELD_PREP(LINKCTRL_BUS_FILTER_BYPASS, 0xf);
1561 	writel(reg, regs_base + EXYNOS850_DRD_LINKCTRL);
1562 
1563 	if (!phy_drd->sw) {
1564 		reg = readl(regs_base + EXYNOS850_DRD_UTMI);
1565 		reg |= UTMI_FORCE_BVALID | UTMI_FORCE_VBUSVALID;
1566 		writel(reg, regs_base + EXYNOS850_DRD_UTMI);
1567 
1568 		reg = readl(regs_base + EXYNOS850_DRD_HSP);
1569 		reg |= HSP_VBUSVLDEXT | HSP_VBUSVLDEXTSEL;
1570 		writel(reg, regs_base + EXYNOS850_DRD_HSP);
1571 	}
1572 
1573 	reg = readl(regs_base + EXYNOS850_DRD_SSPPLLCTL);
1574 	reg &= ~SSPPLLCTL_FSEL;
1575 	switch (phy_drd->extrefclk) {
1576 	case EXYNOS5_FSEL_50MHZ:
1577 		reg |= FIELD_PREP(SSPPLLCTL_FSEL, 7);
1578 		break;
1579 	case EXYNOS5_FSEL_26MHZ:
1580 		reg |= FIELD_PREP(SSPPLLCTL_FSEL, 6);
1581 		break;
1582 	case EXYNOS5_FSEL_24MHZ:
1583 		reg |= FIELD_PREP(SSPPLLCTL_FSEL, 2);
1584 		break;
1585 	case EXYNOS5_FSEL_20MHZ:
1586 		reg |= FIELD_PREP(SSPPLLCTL_FSEL, 1);
1587 		break;
1588 	case EXYNOS5_FSEL_19MHZ2:
1589 		reg |= FIELD_PREP(SSPPLLCTL_FSEL, 0);
1590 		break;
1591 	default:
1592 		dev_warn(phy_drd->dev, "unsupported ref clk: %#.2x\n",
1593 			 phy_drd->extrefclk);
1594 		break;
1595 	}
1596 	writel(reg, regs_base + EXYNOS850_DRD_SSPPLLCTL);
1597 
1598 	if (phy_drd->drv_data->phy_tunes)
1599 		exynos5_usbdrd_apply_phy_tunes(phy_drd,
1600 					       PTS_UTMI_POSTINIT);
1601 
1602 	/* Power up PHY analog blocks */
1603 	reg = readl(regs_base + EXYNOS850_DRD_HSP_TEST);
1604 	reg &= ~HSP_TEST_SIDDQ;
1605 	writel(reg, regs_base + EXYNOS850_DRD_HSP_TEST);
1606 
1607 	/* Finish PHY reset (POR=low) */
1608 	fsleep(10); /* required before doing POR=low */
1609 	reg = readl(regs_base + EXYNOS850_DRD_CLKRST);
1610 	if (ss_ports) {
1611 		reg |= CLKRST_PHY20_SW_POR_SEL;
1612 		reg &= ~CLKRST_PHY20_SW_POR;
1613 	}
1614 	reg &= ~(CLKRST_PHY_SW_RST | CLKRST_PORT_RST);
1615 	writel(reg, regs_base + EXYNOS850_DRD_CLKRST);
1616 	fsleep(75); /* required after POR=low for guaranteed PHY clock */
1617 
1618 	/* Disable single ended signal out */
1619 	reg = readl(regs_base + EXYNOS850_DRD_HSP);
1620 	reg &= ~HSP_FSV_OUT_EN;
1621 	writel(reg, regs_base + EXYNOS850_DRD_HSP);
1622 
1623 	if (ss_ports)
1624 		exynos5_usbdrd_usb_v3p1_pipe_override(phy_drd);
1625 }
1626 
1627 static int exynos850_usbdrd_phy_init(struct phy *phy)
1628 {
1629 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1630 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1631 	int ret;
1632 
1633 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1634 	if (ret)
1635 		return ret;
1636 
1637 	/* UTMI or PIPE3 specific init */
1638 	scoped_guard(mutex, &phy_drd->phy_mutex)
1639 		inst->phy_cfg->phy_init(phy_drd);
1640 
1641 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1642 
1643 	return 0;
1644 }
1645 
1646 static int exynos850_usbdrd_phy_exit(struct phy *phy)
1647 {
1648 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1649 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1650 	void __iomem *regs_base = phy_drd->reg_phy;
1651 	u32 reg;
1652 	int ret;
1653 
1654 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1655 	if (ret)
1656 		return ret;
1657 
1658 	guard(mutex)(&phy_drd->phy_mutex);
1659 
1660 	/* Set PHY clock and control HS PHY */
1661 	reg = readl(regs_base + EXYNOS850_DRD_UTMI);
1662 	reg &= ~(UTMI_DP_PULLDOWN | UTMI_DM_PULLDOWN);
1663 	reg |= UTMI_FORCE_SUSPEND | UTMI_FORCE_SLEEP;
1664 	writel(reg, regs_base + EXYNOS850_DRD_UTMI);
1665 
1666 	/* Power down PHY analog blocks */
1667 	reg = readl(regs_base + EXYNOS850_DRD_HSP_TEST);
1668 	reg |= HSP_TEST_SIDDQ;
1669 	writel(reg, regs_base + EXYNOS850_DRD_HSP_TEST);
1670 
1671 	/* Link reset */
1672 	reg = readl(regs_base + EXYNOS850_DRD_CLKRST);
1673 	reg |= CLKRST_LINK_SW_RST;
1674 	writel(reg, regs_base + EXYNOS850_DRD_CLKRST);
1675 	fsleep(10); /* required before doing POR=low */
1676 	reg &= ~CLKRST_LINK_SW_RST;
1677 	writel(reg, regs_base + EXYNOS850_DRD_CLKRST);
1678 
1679 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1680 
1681 	return 0;
1682 }
1683 
1684 static const struct phy_ops exynos850_usbdrd_phy_ops = {
1685 	.init		= exynos850_usbdrd_phy_init,
1686 	.exit		= exynos850_usbdrd_phy_exit,
1687 	.power_on	= exynos5_usbdrd_phy_power_on,
1688 	.power_off	= exynos5_usbdrd_phy_power_off,
1689 	.owner		= THIS_MODULE,
1690 };
1691 
1692 static void exynos5_usbdrd_gs101_pipe3_init(struct exynos5_usbdrd_phy *phy_drd)
1693 {
1694 	void __iomem *regs_pma = phy_drd->reg_pma;
1695 	void __iomem *regs_phy = phy_drd->reg_phy;
1696 	u32 reg;
1697 
1698 	exynos5_usbdrd_usbdp_g2_v4_ctrl_pma_ready(phy_drd);
1699 
1700 	/* force aux off */
1701 	reg = readl(regs_pma + EXYNOS9_PMA_USBDP_CMN_REG0008);
1702 	reg &= ~CMN_REG0008_AUX_EN;
1703 	reg |= CMN_REG0008_OVRD_AUX_EN;
1704 	writel(reg, regs_pma + EXYNOS9_PMA_USBDP_CMN_REG0008);
1705 
1706 	exynos5_usbdrd_apply_phy_tunes(phy_drd, PTS_PIPE3_PREINIT);
1707 	exynos5_usbdrd_apply_phy_tunes(phy_drd, PTS_PIPE3_INIT);
1708 	exynos5_usbdrd_apply_phy_tunes(phy_drd, PTS_PIPE3_POSTINIT);
1709 
1710 	exynos5_usbdrd_usbdp_g2_v4_pma_lane_mux_sel(phy_drd);
1711 
1712 	/* reset release from port */
1713 	reg = readl(regs_phy + EXYNOS850_DRD_SECPMACTL);
1714 	reg &= ~(SECPMACTL_PMA_TRSV_SW_RST | SECPMACTL_PMA_CMN_SW_RST |
1715 		 SECPMACTL_PMA_INIT_SW_RST);
1716 	writel(reg, regs_phy + EXYNOS850_DRD_SECPMACTL);
1717 
1718 	if (!exynos5_usbdrd_usbdp_g2_v4_pma_check_pll_lock(phy_drd))
1719 		exynos5_usbdrd_usbdp_g2_v4_pma_check_cdr_lock(phy_drd);
1720 }
1721 
1722 static int exynos5_usbdrd_gs101_phy_init(struct phy *phy)
1723 {
1724 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1725 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1726 	int ret;
1727 
1728 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI) {
1729 		/* Power-on PHY ... */
1730 		ret = regulator_bulk_enable(phy_drd->drv_data->n_regulators,
1731 					    phy_drd->regulators);
1732 		if (ret) {
1733 			dev_err(phy_drd->dev,
1734 				"Failed to enable PHY regulator(s)\n");
1735 			return ret;
1736 		}
1737 	}
1738 	/*
1739 	 * ... and ungate power via PMU. Without this here, we get an SError
1740 	 * trying to access PMA registers
1741 	 */
1742 	exynos5_usbdrd_phy_isol(inst, false);
1743 
1744 	return exynos850_usbdrd_phy_init(phy);
1745 }
1746 
1747 static int exynos5_usbdrd_gs101_phy_exit(struct phy *phy)
1748 {
1749 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1750 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1751 	int ret;
1752 
1753 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI) {
1754 		ret = exynos850_usbdrd_phy_exit(phy);
1755 		if (ret)
1756 			return ret;
1757 	}
1758 
1759 	exynos5_usbdrd_phy_isol(inst, true);
1760 
1761 	if (inst->phy_cfg->id != EXYNOS5_DRDPHY_UTMI)
1762 		return 0;
1763 
1764 	return regulator_bulk_disable(phy_drd->drv_data->n_regulators,
1765 				      phy_drd->regulators);
1766 }
1767 
1768 static const struct phy_ops gs101_usbdrd_phy_ops = {
1769 	.init		= exynos5_usbdrd_gs101_phy_init,
1770 	.exit		= exynos5_usbdrd_gs101_phy_exit,
1771 	.owner		= THIS_MODULE,
1772 };
1773 
1774 static int exynos5_usbdrd_phy_clk_handle(struct exynos5_usbdrd_phy *phy_drd)
1775 {
1776 	int ret;
1777 	struct clk *ref_clk;
1778 	unsigned long ref_rate;
1779 
1780 	phy_drd->clks = devm_kcalloc(phy_drd->dev, phy_drd->drv_data->n_clks,
1781 				     sizeof(*phy_drd->clks), GFP_KERNEL);
1782 	if (!phy_drd->clks)
1783 		return -ENOMEM;
1784 
1785 	for (int i = 0; i < phy_drd->drv_data->n_clks; ++i)
1786 		phy_drd->clks[i].id = phy_drd->drv_data->clk_names[i];
1787 
1788 	ret = devm_clk_bulk_get(phy_drd->dev, phy_drd->drv_data->n_clks,
1789 				phy_drd->clks);
1790 	if (ret)
1791 		return dev_err_probe(phy_drd->dev, ret,
1792 				     "failed to get phy clock(s)\n");
1793 
1794 	phy_drd->core_clks = devm_kcalloc(phy_drd->dev,
1795 					  phy_drd->drv_data->n_core_clks,
1796 					  sizeof(*phy_drd->core_clks),
1797 					  GFP_KERNEL);
1798 	if (!phy_drd->core_clks)
1799 		return -ENOMEM;
1800 
1801 	for (int i = 0; i < phy_drd->drv_data->n_core_clks; ++i)
1802 		phy_drd->core_clks[i].id = phy_drd->drv_data->core_clk_names[i];
1803 
1804 	ret = devm_clk_bulk_get(phy_drd->dev, phy_drd->drv_data->n_core_clks,
1805 				phy_drd->core_clks);
1806 	if (ret)
1807 		return dev_err_probe(phy_drd->dev, ret,
1808 				     "failed to get phy core clock(s)\n");
1809 
1810 	if (phy_drd->drv_data->n_core_clks) {
1811 		ref_clk = NULL;
1812 		for (int i = 0; i < phy_drd->drv_data->n_core_clks; ++i) {
1813 			if (!strcmp(phy_drd->core_clks[i].id, "ref")) {
1814 				ref_clk = phy_drd->core_clks[i].clk;
1815 				break;
1816 			}
1817 		}
1818 		if (!ref_clk)
1819 			return dev_err_probe(phy_drd->dev, -ENODEV,
1820 					     "failed to find phy reference clock\n");
1821 
1822 		ref_rate = clk_get_rate(ref_clk);
1823 		ret = exynos5_rate_to_clk(ref_rate, &phy_drd->extrefclk);
1824 		if (ret)
1825 			return dev_err_probe(phy_drd->dev, ret,
1826 					     "clock rate (%ld) not supported\n",
1827 					     ref_rate);
1828 	}
1829 
1830 	return 0;
1831 }
1832 
1833 static const struct exynos5_usbdrd_phy_config phy_cfg_exynos2200[] = {
1834 	{
1835 		.id		= EXYNOS5_DRDPHY_UTMI,
1836 		.phy_isol	= exynos5_usbdrd_phy_isol,
1837 		.phy_init	= exynos2200_usbdrd_utmi_init,
1838 	},
1839 };
1840 
1841 static int exynos5_usbdrd_orien_sw_set(struct typec_switch_dev *sw,
1842 				       enum typec_orientation orientation)
1843 {
1844 	struct exynos5_usbdrd_phy *phy_drd = typec_switch_get_drvdata(sw);
1845 	int ret;
1846 
1847 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
1848 	if (ret) {
1849 		dev_err(phy_drd->dev, "Failed to enable PHY clocks(s)\n");
1850 		return ret;
1851 	}
1852 
1853 	scoped_guard(mutex, &phy_drd->phy_mutex) {
1854 		void __iomem * const regs_base = phy_drd->reg_phy;
1855 		unsigned int reg;
1856 
1857 		if (orientation == TYPEC_ORIENTATION_NONE) {
1858 			reg = readl(regs_base + EXYNOS850_DRD_UTMI);
1859 			reg &= ~(UTMI_FORCE_VBUSVALID | UTMI_FORCE_BVALID);
1860 			writel(reg, regs_base +  EXYNOS850_DRD_UTMI);
1861 
1862 			reg = readl(regs_base + EXYNOS850_DRD_HSP);
1863 			reg |= HSP_VBUSVLDEXTSEL;
1864 			reg &= ~HSP_VBUSVLDEXT;
1865 			writel(reg, regs_base + EXYNOS850_DRD_HSP);
1866 		} else {
1867 			reg = readl(regs_base + EXYNOS850_DRD_UTMI);
1868 			reg |= UTMI_FORCE_VBUSVALID | UTMI_FORCE_BVALID;
1869 			writel(reg, regs_base +  EXYNOS850_DRD_UTMI);
1870 
1871 			reg = readl(regs_base + EXYNOS850_DRD_HSP);
1872 			reg |= HSP_VBUSVLDEXTSEL | HSP_VBUSVLDEXT;
1873 			writel(reg, regs_base + EXYNOS850_DRD_HSP);
1874 		}
1875 
1876 		phy_drd->orientation = orientation;
1877 	}
1878 
1879 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
1880 
1881 	return 0;
1882 }
1883 
1884 static void exynos5_usbdrd_orien_switch_unregister(void *data)
1885 {
1886 	struct exynos5_usbdrd_phy *phy_drd = data;
1887 
1888 	typec_switch_unregister(phy_drd->sw);
1889 }
1890 
1891 static int exynos5_usbdrd_setup_notifiers(struct exynos5_usbdrd_phy *phy_drd)
1892 {
1893 	int ret;
1894 
1895 	if (!IS_ENABLED(CONFIG_TYPEC))
1896 		return 0;
1897 
1898 	if (device_property_present(phy_drd->dev, "orientation-switch")) {
1899 		struct typec_switch_desc sw_desc = { };
1900 
1901 		sw_desc.drvdata = phy_drd;
1902 		sw_desc.fwnode = dev_fwnode(phy_drd->dev);
1903 		sw_desc.set = exynos5_usbdrd_orien_sw_set;
1904 
1905 		phy_drd->sw = typec_switch_register(phy_drd->dev, &sw_desc);
1906 		if (IS_ERR(phy_drd->sw))
1907 			return dev_err_probe(phy_drd->dev,
1908 					     PTR_ERR(phy_drd->sw),
1909 					     "Failed to register TypeC orientation switch\n");
1910 
1911 		ret = devm_add_action_or_reset(phy_drd->dev,
1912 					       exynos5_usbdrd_orien_switch_unregister,
1913 					       phy_drd);
1914 		if (ret)
1915 			return dev_err_probe(phy_drd->dev, ret,
1916 					     "Failed to register TypeC orientation devm action\n");
1917 	}
1918 
1919 	return 0;
1920 }
1921 
1922 static const struct exynos5_usbdrd_phy_config phy_cfg_exynos5[] = {
1923 	{
1924 		.id		= EXYNOS5_DRDPHY_UTMI,
1925 		.phy_isol	= exynos5_usbdrd_phy_isol,
1926 		.phy_init	= exynos5_usbdrd_utmi_init,
1927 		.set_refclk	= exynos5_usbdrd_utmi_set_refclk,
1928 	},
1929 	{
1930 		.id		= EXYNOS5_DRDPHY_PIPE3,
1931 		.phy_isol	= exynos5_usbdrd_phy_isol,
1932 		.phy_init	= exynos5_usbdrd_pipe3_init,
1933 		.set_refclk	= exynos5_usbdrd_pipe3_set_refclk,
1934 	},
1935 };
1936 
1937 static const struct exynos5_usbdrd_phy_config phy_cfg_exynos7870[] = {
1938 	{
1939 		.id		= EXYNOS5_DRDPHY_UTMI,
1940 		.phy_isol	= exynos7870_usbdrd_phy_isol,
1941 		.phy_init	= exynos7870_usbdrd_utmi_init,
1942 	},
1943 };
1944 
1945 static const struct exynos5_usbdrd_phy_config phy_cfg_exynos850[] = {
1946 	{
1947 		.id		= EXYNOS5_DRDPHY_UTMI,
1948 		.phy_isol	= exynos5_usbdrd_phy_isol,
1949 		.phy_init	= exynos850_usbdrd_utmi_init,
1950 	},
1951 };
1952 
1953 static
1954 const struct exynos5_usbdrd_phy_tuning exynos7870_tunes_utmi_postinit[] = {
1955 	PHY_TUNING_ENTRY_PHY(EXYNOS5_DRD_PHYPARAM0,
1956 			     (PHYPARAM0_TXVREFTUNE | PHYPARAM0_TXRISETUNE |
1957 			      PHYPARAM0_TXRESTUNE | PHYPARAM0_TXPREEMPPULSETUNE |
1958 			      PHYPARAM0_TXPREEMPAMPTUNE | PHYPARAM0_TXHSXVTUNE |
1959 			      PHYPARAM0_TXFSLSTUNE | PHYPARAM0_SQRXTUNE |
1960 			      PHYPARAM0_OTGTUNE | PHYPARAM0_COMPDISTUNE),
1961 			     (FIELD_PREP_CONST(PHYPARAM0_TXVREFTUNE, 14) |
1962 			      FIELD_PREP_CONST(PHYPARAM0_TXRISETUNE, 1) |
1963 			      FIELD_PREP_CONST(PHYPARAM0_TXRESTUNE, 3) |
1964 			      FIELD_PREP_CONST(PHYPARAM0_TXPREEMPAMPTUNE, 0) |
1965 			      FIELD_PREP_CONST(PHYPARAM0_TXHSXVTUNE, 0) |
1966 			      FIELD_PREP_CONST(PHYPARAM0_TXFSLSTUNE, 3) |
1967 			      FIELD_PREP_CONST(PHYPARAM0_SQRXTUNE, 6) |
1968 			      FIELD_PREP_CONST(PHYPARAM0_OTGTUNE, 2) |
1969 			      FIELD_PREP_CONST(PHYPARAM0_COMPDISTUNE, 3))),
1970 	PHY_TUNING_ENTRY_LAST
1971 };
1972 
1973 static const struct exynos5_usbdrd_phy_tuning *exynos7870_tunes[PTS_MAX] = {
1974 	[PTS_UTMI_POSTINIT] = exynos7870_tunes_utmi_postinit,
1975 };
1976 
1977 static const char * const exynos5_clk_names[] = {
1978 	"phy",
1979 };
1980 
1981 static const char * const exynos5_core_clk_names[] = {
1982 	"ref",
1983 };
1984 
1985 static const char * const exynos5433_core_clk_names[] = {
1986 	"ref", "phy_pipe", "phy_utmi", "itp",
1987 };
1988 
1989 static const char * const exynos5_regulator_names[] = {
1990 	"vbus", "vbus-boost",
1991 };
1992 
1993 static const struct exynos5_usbdrd_phy_drvdata exynos2200_usb32drd_phy = {
1994 	.phy_cfg		= phy_cfg_exynos2200,
1995 	.phy_ops		= &exynos2200_usbdrd_phy_ops,
1996 	.pmu_offset_usbdrd0_phy	= EXYNOS2200_PHY_CTRL_USB20,
1997 	.clk_names		= exynos5_clk_names,
1998 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
1999 	/* clocks and regulators are specific to the underlying PHY blocks */
2000 	.core_clk_names		= NULL,
2001 	.n_core_clks		= 0,
2002 	.regulator_names	= NULL,
2003 	.n_regulators		= 0,
2004 };
2005 
2006 static const struct exynos5_usbdrd_phy_drvdata exynos5420_usbdrd_phy = {
2007 	.phy_cfg		= phy_cfg_exynos5,
2008 	.phy_ops		= &exynos5_usbdrd_phy_ops,
2009 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2010 	.pmu_offset_usbdrd1_phy	= EXYNOS5420_USBDRD1_PHY_CONTROL,
2011 	.clk_names		= exynos5_clk_names,
2012 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2013 	.core_clk_names		= exynos5_core_clk_names,
2014 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2015 	.regulator_names	= exynos5_regulator_names,
2016 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2017 };
2018 
2019 static const struct exynos5_usbdrd_phy_drvdata exynos5250_usbdrd_phy = {
2020 	.phy_cfg		= phy_cfg_exynos5,
2021 	.phy_ops		= &exynos5_usbdrd_phy_ops,
2022 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2023 	.clk_names		= exynos5_clk_names,
2024 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2025 	.core_clk_names		= exynos5_core_clk_names,
2026 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2027 	.regulator_names	= exynos5_regulator_names,
2028 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2029 };
2030 
2031 static const struct exynos5_usbdrd_phy_drvdata exynos5433_usbdrd_phy = {
2032 	.phy_cfg		= phy_cfg_exynos5,
2033 	.phy_ops		= &exynos5_usbdrd_phy_ops,
2034 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2035 	.pmu_offset_usbdrd1_phy	= EXYNOS5433_USBHOST30_PHY_CONTROL,
2036 	.clk_names		= exynos5_clk_names,
2037 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2038 	.core_clk_names		= exynos5433_core_clk_names,
2039 	.n_core_clks		= ARRAY_SIZE(exynos5433_core_clk_names),
2040 	.regulator_names	= exynos5_regulator_names,
2041 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2042 };
2043 
2044 static const struct exynos5_usbdrd_phy_drvdata exynos7_usbdrd_phy = {
2045 	.phy_cfg		= phy_cfg_exynos5,
2046 	.phy_ops		= &exynos5_usbdrd_phy_ops,
2047 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2048 	.clk_names		= exynos5_clk_names,
2049 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2050 	.core_clk_names		= exynos5433_core_clk_names,
2051 	.n_core_clks		= ARRAY_SIZE(exynos5433_core_clk_names),
2052 	.regulator_names	= exynos5_regulator_names,
2053 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2054 };
2055 
2056 static const struct exynos5_usbdrd_phy_drvdata exynos7870_usbdrd_phy = {
2057 	.phy_cfg		= phy_cfg_exynos7870,
2058 	.phy_tunes		= exynos7870_tunes,
2059 	.phy_ops		= &exynos7870_usbdrd_phy_ops,
2060 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2061 	.clk_names		= exynos5_clk_names,
2062 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2063 	.core_clk_names		= exynos5_core_clk_names,
2064 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2065 	.regulator_names	= exynos5_regulator_names,
2066 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2067 };
2068 
2069 static const struct exynos5_usbdrd_phy_drvdata exynos850_usbdrd_phy = {
2070 	.phy_cfg		= phy_cfg_exynos850,
2071 	.phy_ops		= &exynos850_usbdrd_phy_ops,
2072 	.pmu_offset_usbdrd0_phy	= EXYNOS5_USBDRD_PHY_CONTROL,
2073 	.clk_names		= exynos5_clk_names,
2074 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2075 	.core_clk_names		= exynos5_core_clk_names,
2076 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2077 	.regulator_names	= exynos5_regulator_names,
2078 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2079 };
2080 
2081 static const struct exynos5_usbdrd_phy_tuning exynos990_tunes_utmi_postinit[] = {
2082 	PHY_TUNING_ENTRY_PHY(EXYNOS850_DRD_HSPPARACON,
2083 			     (HSPPARACON_TXVREF |
2084 			      HSPPARACON_TXPREEMPAMP | HSPPARACON_SQRX |
2085 			      HSPPARACON_COMPDIS),
2086 			     (FIELD_PREP_CONST(HSPPARACON_TXVREF, 7) |
2087 			      FIELD_PREP_CONST(HSPPARACON_TXPREEMPAMP, 3) |
2088 			      FIELD_PREP_CONST(HSPPARACON_SQRX, 5) |
2089 			      FIELD_PREP_CONST(HSPPARACON_COMPDIS, 7))),
2090 	PHY_TUNING_ENTRY_LAST
2091 };
2092 
2093 static const struct exynos5_usbdrd_phy_tuning *exynos990_tunes[PTS_MAX] = {
2094 	[PTS_UTMI_POSTINIT] = exynos990_tunes_utmi_postinit,
2095 };
2096 
2097 static const struct exynos5_usbdrd_phy_drvdata exynos990_usbdrd_phy = {
2098 	.phy_cfg		= phy_cfg_exynos850,
2099 	.phy_ops		= &exynos850_usbdrd_phy_ops,
2100 	.phy_tunes		= exynos990_tunes,
2101 	.pmu_offset_usbdrd0_phy	= EXYNOS990_PHY_CTRL_USB20,
2102 	.clk_names		= exynos5_clk_names,
2103 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2104 	.core_clk_names		= exynos5_core_clk_names,
2105 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2106 	.regulator_names	= exynos5_regulator_names,
2107 	.n_regulators		= ARRAY_SIZE(exynos5_regulator_names),
2108 };
2109 
2110 static void
2111 exynosautov920_usb31drd_cr_clk(struct exynos5_usbdrd_phy *phy_drd, bool high)
2112 {
2113 	void __iomem *reg_phy = phy_drd->reg_phy;
2114 	u32 reg;
2115 
2116 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2117 	if (high)
2118 		reg |= PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK;
2119 	else
2120 		reg &= ~PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK;
2121 
2122 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2123 	fsleep(1);
2124 }
2125 
2126 static void
2127 exynosautov920_usb31drd_port_phy_ready(struct exynos5_usbdrd_phy *phy_drd)
2128 {
2129 	struct device *dev = phy_drd->dev;
2130 	void __iomem *reg_phy = phy_drd->reg_phy;
2131 	static const unsigned int timeout_us = 20000;
2132 	static const unsigned int sleep_us = 40;
2133 	u32 reg;
2134 	int err;
2135 
2136 	/* Clear cr_para_con */
2137 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2138 	reg &= ~(PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK |
2139 			PHY_CR_PARA_CON0_PHY0_CR_PARA_ADDR);
2140 	reg |= PHY_CR_PARA_CON0_PHY0_CR_PARA_SEL;
2141 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2142 	writel(0x0, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON1);
2143 	writel(0x0, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON2);
2144 
2145 	exynosautov920_usb31drd_cr_clk(phy_drd, true);
2146 	exynosautov920_usb31drd_cr_clk(phy_drd, false);
2147 
2148 	/*
2149 	 * The maximum time from phy reset de-assertion to de-assertion of
2150 	 * tx/rx_ack can be as high as 5ms in fast simulation mode.
2151 	 * Time to phy ready is < 20ms
2152 	 */
2153 	err = readl_poll_timeout(reg_phy +
2154 				EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0,
2155 			reg, !(reg & PHY_CR_PARA_CON0_PHY0_CR_PARA_ACK),
2156 			sleep_us, timeout_us);
2157 	if (err)
2158 		dev_err(dev, "timed out waiting for rx/tx_ack: %#.8x\n", reg);
2159 
2160 	reg &= ~PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK;
2161 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2162 }
2163 
2164 static void
2165 exynosautov920_usb31drd_cr_write(struct exynos5_usbdrd_phy *phy_drd,
2166 				 u16 addr, u16 data)
2167 {
2168 	void __iomem *reg_phy = phy_drd->reg_phy;
2169 	u32 cnt = 0;
2170 	u32 reg;
2171 
2172 	/* Pre Clocking */
2173 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2174 	reg |= PHY_CR_PARA_CON0_PHY0_CR_PARA_SEL;
2175 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2176 
2177 	/*
2178 	 * tx clks must be available prior to assertion of tx req.
2179 	 * tx pstate p2 to p0 transition directly is not permitted.
2180 	 * tx clk ready must be asserted synchronously on tx clk prior
2181 	 * to internal transmit clk alignment sequence in the phy
2182 	 * when entering from p2 to p1 to p0.
2183 	 */
2184 	do {
2185 		exynosautov920_usb31drd_cr_clk(phy_drd, true);
2186 		exynosautov920_usb31drd_cr_clk(phy_drd, false);
2187 		cnt++;
2188 	} while (cnt < 15);
2189 
2190 	reg &= ~PHY_CR_PARA_CON0_PHY0_CR_PARA_SEL;
2191 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2192 
2193 	/*
2194 	 * tx data path is active when tx lane is in p0 state
2195 	 * and tx data en asserted. enable cr_para_wr_en.
2196 	 */
2197 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON2);
2198 	reg &= ~PHY_CR_PARA_CON2_PHY0_CR_PARA_WR_DATA;
2199 	reg |= FIELD_PREP(PHY_CR_PARA_CON2_PHY0_CR_PARA_WR_DATA, data) |
2200 		PHY_CR_PARA_CON2_PHY0_CR_PARA_WR_EN;
2201 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON2);
2202 
2203 	/* write addr */
2204 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2205 	reg &= ~PHY_CR_PARA_CON0_PHY0_CR_PARA_ADDR;
2206 	reg |= FIELD_PREP(PHY_CR_PARA_CON0_PHY0_CR_PARA_ADDR, addr) |
2207 		PHY_CR_PARA_CON0_PHY0_CR_PARA_CLK |
2208 		PHY_CR_PARA_CON0_PHY0_CR_PARA_SEL;
2209 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2210 
2211 	/* check cr_para_ack*/
2212 	cnt = 0;
2213 	do {
2214 		/*
2215 		 * data symbols are captured by phy on rising edge of the
2216 		 * tx_clk when tx data enabled.
2217 		 * completion of the write cycle is acknowledged by assertion
2218 		 * of the cr_para_ack.
2219 		 */
2220 		exynosautov920_usb31drd_cr_clk(phy_drd, true);
2221 		reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CR_PARA_CON0);
2222 		if ((reg & PHY_CR_PARA_CON0_PHY0_CR_PARA_ACK))
2223 			break;
2224 
2225 		exynosautov920_usb31drd_cr_clk(phy_drd, false);
2226 
2227 		/*
2228 		 * wait for minimum of 10 cr_para_clk cycles after phy reset
2229 		 * is negated, before accessing control regs to allow for
2230 		 * internal resets.
2231 		 */
2232 		cnt++;
2233 	} while (cnt < 10);
2234 
2235 	if (cnt < 10)
2236 		exynosautov920_usb31drd_cr_clk(phy_drd, false);
2237 }
2238 
2239 static void
2240 exynosautov920_usb31drd_phy_reset(struct exynos5_usbdrd_phy *phy_drd, int val)
2241 {
2242 	void __iomem *reg_phy = phy_drd->reg_phy;
2243 	u32 reg;
2244 
2245 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2246 	reg &= ~PHY_RST_CTRL_PHY_RESET_OVRD_EN;
2247 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2248 
2249 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2250 	if (val)
2251 		reg |= PHY_RST_CTRL_PHY_RESET;
2252 	else
2253 		reg &= ~PHY_RST_CTRL_PHY_RESET;
2254 
2255 	reg |= PHY_RST_CTRL_PHY_RESET_OVRD_EN;
2256 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2257 }
2258 
2259 static void
2260 exynosautov920_usb31drd_lane0_reset(struct exynos5_usbdrd_phy *phy_drd, int val)
2261 {
2262 	void __iomem *reg_phy = phy_drd->reg_phy;
2263 	u32 reg;
2264 
2265 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2266 	reg |= PHY_RST_CTRL_PIPE_LANE0_RESET_N_OVRD_EN;
2267 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2268 
2269 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2270 	if (val)
2271 		reg &= ~PHY_RST_CTRL_PIPE_LANE0_RESET_N;
2272 	else
2273 		reg |= PHY_RST_CTRL_PIPE_LANE0_RESET_N;
2274 
2275 	reg &= ~PHY_RST_CTRL_PIPE_LANE0_RESET_N_OVRD_EN;
2276 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_RST_CTRL);
2277 }
2278 
2279 static void
2280 exynosautov920_usb31drd_pipe3_init(struct exynos5_usbdrd_phy *phy_drd)
2281 {
2282 	void __iomem *reg_phy = phy_drd->reg_phy;
2283 	u32 reg;
2284 
2285 	/*
2286 	 * Phy and Pipe Lane reset assert.
2287 	 * assert reset (phy_reset = 1).
2288 	 * The lane-ack outputs are asserted during reset (tx_ack = rx_ack = 1)
2289 	 */
2290 	exynosautov920_usb31drd_phy_reset(phy_drd, 1);
2291 	exynosautov920_usb31drd_lane0_reset(phy_drd, 1);
2292 
2293 	/*
2294 	 * ANA Power En, PCS & PMA PWR Stable Set
2295 	 * ramp-up power suppiles
2296 	 */
2297 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG0);
2298 	reg |= PHY_CONFIG0_PHY0_ANA_PWR_EN | PHY_CONFIG0_PHY0_PCS_PWR_STABLE |
2299 		PHY_CONFIG0_PHY0_PMA_PWR_STABLE;
2300 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG0);
2301 
2302 	fsleep(10);
2303 
2304 	/*
2305 	 * phy is not functional in test_powerdown mode, test_powerdown to be
2306 	 * de-asserted for normal operation
2307 	 */
2308 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG7);
2309 	reg &= ~PHY_CONFIG7_PHY_TEST_POWERDOWN;
2310 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG7);
2311 
2312 	/*
2313 	 * phy reset signal be asserted for minimum 10us after power
2314 	 * supplies are ramped-up
2315 	 */
2316 	fsleep(10);
2317 
2318 	/*
2319 	 * Phy and Pipe Lane reset assert de-assert
2320 	 */
2321 	exynosautov920_usb31drd_phy_reset(phy_drd, 0);
2322 	exynosautov920_usb31drd_lane0_reset(phy_drd, 0);
2323 
2324 	/* Pipe_rx0_sris_mode_en  = 1 */
2325 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG4);
2326 	reg |= PHY_CONFIG4_PIPE_RX0_SRIS_MODE_EN;
2327 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG4);
2328 
2329 	/*
2330 	 * wait for lane ack outputs to de-assert (tx_ack = rx_ack = 0)
2331 	 * Exit from the reset state is indicated by de-assertion of *_ack
2332 	 */
2333 	exynosautov920_usb31drd_port_phy_ready(phy_drd);
2334 
2335 	/* override values for level settings */
2336 	exynosautov920_usb31drd_cr_write(phy_drd, 0x22, 0x00F5);
2337 }
2338 
2339 static void
2340 exynosautov920_usb31drd_ssphy_disable(struct exynos5_usbdrd_phy *phy_drd)
2341 {
2342 	void __iomem *reg_phy = phy_drd->reg_phy;
2343 	u32 reg;
2344 
2345 	/* 1. Assert reset (phy_reset = 1) */
2346 	exynosautov920_usb31drd_lane0_reset(phy_drd, 1);
2347 	exynosautov920_usb31drd_phy_reset(phy_drd, 1);
2348 
2349 	/* phy test power down */
2350 	reg = readl(reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG7);
2351 	reg |= PHY_CONFIG7_PHY_TEST_POWERDOWN;
2352 	writel(reg, reg_phy + EXYNOSAUTOV920_USB31DRD_PHY_CONFIG7);
2353 }
2354 
2355 static void
2356 exynosautov920_usbdrd_utmi_init(struct exynos5_usbdrd_phy *phy_drd)
2357 {
2358 	void __iomem *reg_phy = phy_drd->reg_phy;
2359 	u32 reg;
2360 
2361 	/*
2362 	 * Disable HWACG (hardware auto clock gating control). This
2363 	 * forces QACTIVE signal in Q-Channel interface to HIGH level,
2364 	 * to make sure the PHY clock is not gated by the hardware.
2365 	 */
2366 	reg = readl(reg_phy + EXYNOS850_DRD_LINKCTRL);
2367 	reg |= LINKCTRL_FORCE_QACT;
2368 	writel(reg, reg_phy + EXYNOS850_DRD_LINKCTRL);
2369 
2370 	/* De-assert link reset */
2371 	reg = readl(reg_phy + EXYNOS2200_DRD_CLKRST);
2372 	reg &= ~CLKRST_LINK_SW_RST;
2373 	writel(reg, reg_phy + EXYNOS2200_DRD_CLKRST);
2374 
2375 	/* Set PHY POR High */
2376 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSP_CLKRST);
2377 	reg |= HSPCLKRST_PHY20_SW_POR | HSPCLKRST_PHY20_SW_POR_SEL;
2378 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSP_CLKRST);
2379 
2380 	/* Enable UTMI+ */
2381 	reg = readl(reg_phy + EXYNOS2200_DRD_UTMI);
2382 	reg &= ~(UTMICTL_FORCE_UTMI_SUSPEND | UTMICTL_FORCE_UTMI_SLEEP |
2383 		UTMICTL_FORCE_DPPULLDOWN | UTMICTL_FORCE_DMPULLDOWN);
2384 	writel(reg, reg_phy + EXYNOS2200_DRD_UTMI);
2385 
2386 	/* set phy clock & control HS phy */
2387 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSPCTL);
2388 	reg |= HSPCTRL_EN_UTMISUSPEND | HSPCTRL_COMMONONN;
2389 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSPCTL);
2390 
2391 	fsleep(100);
2392 
2393 	/* Set VBUS Valid and DP-Pull up control by VBUS pad usage */
2394 	reg = readl(reg_phy + EXYNOS850_DRD_LINKCTRL);
2395 	reg |= FIELD_PREP_CONST(LINKCTRL_BUS_FILTER_BYPASS, 0xf);
2396 	writel(reg, reg_phy + EXYNOS850_DRD_LINKCTRL);
2397 
2398 	reg = readl(reg_phy + EXYNOS2200_DRD_UTMI);
2399 	reg |= EXYNOS2200_UTMI_FORCE_VBUSVALID | EXYNOS2200_UTMI_FORCE_BVALID;
2400 	writel(reg, reg_phy + EXYNOS2200_DRD_UTMI);
2401 
2402 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSPCTL);
2403 	reg |= HSPCTRL_VBUSVLDEXTSEL | HSPCTRL_VBUSVLDEXT;
2404 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSPCTL);
2405 
2406 	/* Setting FSEL for refference clock */
2407 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSPPLLTUNE);
2408 	reg &= ~HSPPLLTUNE_FSEL;
2409 
2410 	switch (phy_drd->extrefclk) {
2411 	case EXYNOS5_FSEL_50MHZ:
2412 		reg |= FIELD_PREP(HSPPLLTUNE_FSEL, 7);
2413 		break;
2414 	case EXYNOS5_FSEL_26MHZ:
2415 		reg |= FIELD_PREP(HSPPLLTUNE_FSEL, 6);
2416 		break;
2417 	case EXYNOS5_FSEL_24MHZ:
2418 		reg |= FIELD_PREP(HSPPLLTUNE_FSEL, 2);
2419 		break;
2420 	case EXYNOS5_FSEL_20MHZ:
2421 		reg |= FIELD_PREP(HSPPLLTUNE_FSEL, 1);
2422 		break;
2423 	case EXYNOS5_FSEL_19MHZ2:
2424 		reg |= FIELD_PREP(HSPPLLTUNE_FSEL, 0);
2425 		break;
2426 	default:
2427 		dev_warn(phy_drd->dev, "unsupported ref clk: %#.2x\n",
2428 			 phy_drd->extrefclk);
2429 		break;
2430 	}
2431 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSPPLLTUNE);
2432 
2433 	/* Enable PHY Power Mode */
2434 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSP_TEST);
2435 	reg &= ~HSP_TEST_SIDDQ;
2436 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSP_TEST);
2437 
2438 	/* before POR low, 10us delay is needed to Finish PHY reset */
2439 	fsleep(10);
2440 
2441 	/* Set PHY POR Low */
2442 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSP_CLKRST);
2443 	reg |= HSPCLKRST_PHY20_SW_POR_SEL;
2444 	reg &= ~(HSPCLKRST_PHY20_SW_POR | HSPCLKRST_PHY20_SW_PORTRESET);
2445 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSP_CLKRST);
2446 
2447 	/* after POR low and delay 75us, PHYCLOCK is guaranteed. */
2448 	fsleep(75);
2449 
2450 	/* Disable forcing pipe interface */
2451 	reg = readl(reg_phy + EXYNOS850_DRD_LINKCTRL);
2452 	reg &= ~LINKCTRL_FORCE_PIPE_EN;
2453 	writel(reg, reg_phy + EXYNOS850_DRD_LINKCTRL);
2454 
2455 	/* Pclk to pipe_clk */
2456 	reg = readl(reg_phy + EXYNOS2200_DRD_CLKRST);
2457 	reg |= EXYNOS2200_CLKRST_LINK_PCLK_SEL;
2458 	writel(reg, reg_phy + EXYNOS2200_DRD_CLKRST);
2459 }
2460 
2461 static void
2462 exynosautov920_usbdrd_hsphy_disable(struct exynos5_usbdrd_phy *phy_drd)
2463 {
2464 	u32 reg;
2465 	void __iomem *reg_phy = phy_drd->reg_phy;
2466 
2467 	/* set phy clock & control HS phy */
2468 	reg = readl(reg_phy + EXYNOS2200_DRD_UTMI);
2469 	reg |= UTMICTL_FORCE_UTMI_SUSPEND | UTMICTL_FORCE_UTMI_SLEEP;
2470 	reg &= ~(UTMICTL_FORCE_DPPULLDOWN | UTMICTL_FORCE_DMPULLDOWN);
2471 	writel(reg, reg_phy + EXYNOS2200_DRD_UTMI);
2472 
2473 	/* Disable PHY Power Mode */
2474 	reg = readl(reg_phy + EXYNOSAUTOV920_DRD_HSP_TEST);
2475 	reg |= HSP_TEST_SIDDQ;
2476 	writel(reg, reg_phy + EXYNOSAUTOV920_DRD_HSP_TEST);
2477 
2478 	/* clear force q-channel */
2479 	reg = readl(reg_phy + EXYNOS850_DRD_LINKCTRL);
2480 	reg &= ~LINKCTRL_FORCE_QACT;
2481 	writel(reg, reg_phy + EXYNOS850_DRD_LINKCTRL);
2482 
2483 	/* link sw reset is need for USB_DP/DM high-z in host mode */
2484 	reg = readl(reg_phy + EXYNOS2200_DRD_CLKRST);
2485 	reg |= CLKRST_LINK_SW_RST;
2486 	writel(reg, reg_phy + EXYNOS2200_DRD_CLKRST);
2487 	fsleep(10);
2488 	reg &= ~CLKRST_LINK_SW_RST;
2489 	writel(reg, reg_phy + EXYNOS2200_DRD_CLKRST);
2490 }
2491 
2492 static int exynosautov920_usbdrd_phy_init(struct phy *phy)
2493 {
2494 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
2495 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
2496 	int ret;
2497 
2498 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
2499 	if (ret)
2500 		return ret;
2501 
2502 	/* Bypass PHY isol */
2503 	inst->phy_cfg->phy_isol(inst, false);
2504 
2505 	/* UTMI or PIPE3 specific init */
2506 	inst->phy_cfg->phy_init(phy_drd);
2507 
2508 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
2509 
2510 	return 0;
2511 }
2512 
2513 static int exynosautov920_usbdrd_phy_exit(struct phy *phy)
2514 {
2515 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
2516 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
2517 	int ret;
2518 
2519 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
2520 	if (ret)
2521 		return ret;
2522 
2523 	exynos850_usbdrd_phy_exit(phy);
2524 
2525 	/* enable PHY isol */
2526 	inst->phy_cfg->phy_isol(inst, true);
2527 
2528 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
2529 
2530 	return 0;
2531 }
2532 
2533 static int exynosautov920_usbdrd_combo_phy_exit(struct phy *phy)
2534 {
2535 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
2536 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
2537 	int ret = 0;
2538 
2539 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_clks, phy_drd->clks);
2540 	if (ret)
2541 		return ret;
2542 
2543 	if (inst->phy_cfg->id == EXYNOS5_DRDPHY_UTMI)
2544 		exynosautov920_usbdrd_hsphy_disable(phy_drd);
2545 	else if (inst->phy_cfg->id == EXYNOS5_DRDPHY_PIPE3)
2546 		exynosautov920_usb31drd_ssphy_disable(phy_drd);
2547 
2548 	/* enable PHY isol */
2549 	inst->phy_cfg->phy_isol(inst, true);
2550 
2551 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_clks, phy_drd->clks);
2552 
2553 	return 0;
2554 }
2555 
2556 static int exynosautov920_usbdrd_phy_power_on(struct phy *phy)
2557 {
2558 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
2559 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
2560 	int ret;
2561 
2562 	dev_dbg(phy_drd->dev, "Request to power_on usbdrd_phy phy\n");
2563 
2564 	ret = clk_bulk_prepare_enable(phy_drd->drv_data->n_core_clks,
2565 				      phy_drd->core_clks);
2566 	if (ret)
2567 		return ret;
2568 
2569 	/* Enable supply */
2570 	ret = regulator_bulk_enable(phy_drd->drv_data->n_regulators,
2571 				    phy_drd->regulators);
2572 	if (ret) {
2573 		dev_err(phy_drd->dev, "Failed to enable PHY regulator(s)\n");
2574 		goto fail_supply;
2575 	}
2576 
2577 	return 0;
2578 
2579 fail_supply:
2580 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_core_clks,
2581 				   phy_drd->core_clks);
2582 
2583 	return ret;
2584 }
2585 
2586 static int exynosautov920_usbdrd_phy_power_off(struct phy *phy)
2587 {
2588 	struct phy_usb_instance *inst = phy_get_drvdata(phy);
2589 	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
2590 
2591 	dev_dbg(phy_drd->dev, "Request to power_off usbdrd_phy phy\n");
2592 
2593 	/* Disable supply */
2594 	regulator_bulk_disable(phy_drd->drv_data->n_regulators,
2595 			       phy_drd->regulators);
2596 
2597 	clk_bulk_disable_unprepare(phy_drd->drv_data->n_core_clks,
2598 				   phy_drd->core_clks);
2599 
2600 	return 0;
2601 }
2602 
2603 static const char * const exynosautov920_usb30_regulators[] = {
2604 	"dvdd", "vdd18",
2605 };
2606 
2607 static const char * const exynosautov920_usb20_regulators[] = {
2608 	"dvdd", "vdd18", "vdd33",
2609 };
2610 
2611 static const struct
2612 exynos5_usbdrd_phy_config usb31drd_phy_cfg_exynosautov920[] = {
2613 	{
2614 		.id		= EXYNOS5_DRDPHY_PIPE3,
2615 		.phy_isol	= exynos5_usbdrd_phy_isol,
2616 		.phy_init	= exynosautov920_usb31drd_pipe3_init,
2617 	},
2618 };
2619 
2620 static const struct phy_ops exynosautov920_usb31drd_combo_ssphy_ops = {
2621 	.init		= exynosautov920_usbdrd_phy_init,
2622 	.exit		= exynosautov920_usbdrd_combo_phy_exit,
2623 	.power_on	= exynosautov920_usbdrd_phy_power_on,
2624 	.power_off	= exynosautov920_usbdrd_phy_power_off,
2625 	.owner		= THIS_MODULE,
2626 };
2627 
2628 static const
2629 struct exynos5_usbdrd_phy_drvdata exynosautov920_usb31drd_combo_ssphy = {
2630 	.phy_cfg		= usb31drd_phy_cfg_exynosautov920,
2631 	.phy_ops		= &exynosautov920_usb31drd_combo_ssphy_ops,
2632 	.pmu_offset_usbdrd0_phy	= EXYNOSAUTOV920_PHY_CTRL_USB31,
2633 	.clk_names		= exynos5_clk_names,
2634 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2635 	.core_clk_names		= exynos5_core_clk_names,
2636 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2637 	.regulator_names	= exynosautov920_usb30_regulators,
2638 	.n_regulators		= ARRAY_SIZE(exynosautov920_usb30_regulators),
2639 };
2640 
2641 static const struct phy_ops exynosautov920_usbdrd_combo_hsphy_ops = {
2642 	.init		= exynosautov920_usbdrd_phy_init,
2643 	.exit		= exynosautov920_usbdrd_combo_phy_exit,
2644 	.power_on	= exynosautov920_usbdrd_phy_power_on,
2645 	.power_off	= exynosautov920_usbdrd_phy_power_off,
2646 	.owner		= THIS_MODULE,
2647 };
2648 
2649 static const struct
2650 exynos5_usbdrd_phy_config usbdrd_hsphy_cfg_exynosautov920[] = {
2651 	{
2652 		.id		= EXYNOS5_DRDPHY_UTMI,
2653 		.phy_isol	= exynos5_usbdrd_phy_isol,
2654 		.phy_init	= exynosautov920_usbdrd_utmi_init,
2655 	},
2656 };
2657 
2658 static const
2659 struct exynos5_usbdrd_phy_drvdata exynosautov920_usbdrd_combo_hsphy = {
2660 	.phy_cfg		= usbdrd_hsphy_cfg_exynosautov920,
2661 	.phy_ops		= &exynosautov920_usbdrd_combo_hsphy_ops,
2662 	.pmu_offset_usbdrd0_phy	= EXYNOSAUTOV920_PHY_CTRL_USB20,
2663 	.clk_names		= exynos5_clk_names,
2664 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2665 	.core_clk_names		= exynos5_core_clk_names,
2666 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2667 	.regulator_names	= exynosautov920_usb20_regulators,
2668 	.n_regulators		= ARRAY_SIZE(exynosautov920_usb20_regulators),
2669 };
2670 
2671 static const struct phy_ops exynosautov920_usbdrd_phy_ops = {
2672 	.init		= exynosautov920_usbdrd_phy_init,
2673 	.exit		= exynosautov920_usbdrd_phy_exit,
2674 	.power_on       = exynosautov920_usbdrd_phy_power_on,
2675 	.power_off      = exynosautov920_usbdrd_phy_power_off,
2676 	.owner		= THIS_MODULE,
2677 };
2678 
2679 static const struct exynos5_usbdrd_phy_config phy_cfg_exynosautov920[] = {
2680 	{
2681 		.id		= EXYNOS5_DRDPHY_UTMI,
2682 		.phy_isol	= exynos5_usbdrd_phy_isol,
2683 		.phy_init	= exynos850_usbdrd_utmi_init,
2684 	},
2685 };
2686 
2687 static const struct exynos5_usbdrd_phy_drvdata exynosautov920_usbdrd_phy = {
2688 	.phy_cfg		= phy_cfg_exynosautov920,
2689 	.phy_ops		= &exynosautov920_usbdrd_phy_ops,
2690 	.pmu_offset_usbdrd0_phy	= EXYNOSAUTOV920_PHY_CTRL_USB20,
2691 	.clk_names		= exynos5_clk_names,
2692 	.n_clks			= ARRAY_SIZE(exynos5_clk_names),
2693 	.core_clk_names		= exynos5_core_clk_names,
2694 	.n_core_clks		= ARRAY_SIZE(exynos5_core_clk_names),
2695 	.regulator_names	= exynosautov920_usb20_regulators,
2696 	.n_regulators		= ARRAY_SIZE(exynosautov920_usb20_regulators),
2697 };
2698 
2699 static const struct exynos5_usbdrd_phy_config phy_cfg_gs101[] = {
2700 	{
2701 		.id		= EXYNOS5_DRDPHY_UTMI,
2702 		.phy_isol	= exynos5_usbdrd_phy_isol,
2703 		.phy_init	= exynos850_usbdrd_utmi_init,
2704 	},
2705 	{
2706 		.id		= EXYNOS5_DRDPHY_PIPE3,
2707 		.phy_isol	= exynos5_usbdrd_phy_isol,
2708 		.phy_init	= exynos5_usbdrd_gs101_pipe3_init,
2709 	},
2710 };
2711 
2712 static const struct exynos5_usbdrd_phy_tuning gs101_tunes_utmi_postinit[] = {
2713 	PHY_TUNING_ENTRY_PHY(EXYNOS850_DRD_HSPPARACON,
2714 			     (HSPPARACON_TXVREF | HSPPARACON_TXRES |
2715 			      HSPPARACON_TXPREEMPAMP | HSPPARACON_SQRX |
2716 			      HSPPARACON_COMPDIS),
2717 			     (FIELD_PREP_CONST(HSPPARACON_TXVREF, 6) |
2718 			      FIELD_PREP_CONST(HSPPARACON_TXRES, 1) |
2719 			      FIELD_PREP_CONST(HSPPARACON_TXPREEMPAMP, 3) |
2720 			      FIELD_PREP_CONST(HSPPARACON_SQRX, 5) |
2721 			      FIELD_PREP_CONST(HSPPARACON_COMPDIS, 7))),
2722 	PHY_TUNING_ENTRY_LAST
2723 };
2724 
2725 static const struct exynos5_usbdrd_phy_tuning gs101_tunes_pipe3_preinit[] = {
2726 	/* preinit */
2727 	/* CDR data mode exit GEN1 ON / GEN2 OFF */
2728 	PHY_TUNING_ENTRY_PMA(0x0c8c, -1, 0xff),
2729 	PHY_TUNING_ENTRY_PMA(0x1c8c, -1, 0xff),
2730 	PHY_TUNING_ENTRY_PMA(0x0c9c, -1, 0x7d),
2731 	PHY_TUNING_ENTRY_PMA(0x1c9c, -1, 0x7d),
2732 	/* improve EDS distribution */
2733 	PHY_TUNING_ENTRY_PMA(0x0e7c, -1, 0x06),
2734 	PHY_TUNING_ENTRY_PMA(0x09e0, -1, 0x00),
2735 	PHY_TUNING_ENTRY_PMA(0x09e4, -1, 0x36),
2736 	PHY_TUNING_ENTRY_PMA(0x1e7c, -1, 0x06),
2737 	PHY_TUNING_ENTRY_PMA(0x19e0, -1, 0x00),
2738 	PHY_TUNING_ENTRY_PMA(0x19e4, -1, 0x36),
2739 	/* fix bootloader bug */
2740 	PHY_TUNING_ENTRY_PMA(0x1e90, -1, 0x02),
2741 	PHY_TUNING_ENTRY_PMA(0x1e94, -1, 0x0b),
2742 	/* improve LVCC */
2743 	PHY_TUNING_ENTRY_PMA(0x08f0, -1, 0x30),
2744 	PHY_TUNING_ENTRY_PMA(0x18f0, -1, 0x30),
2745 	/* LFPS RX VIH shmoo hole */
2746 	PHY_TUNING_ENTRY_PMA(0x0a08, -1, 0x0c),
2747 	PHY_TUNING_ENTRY_PMA(0x1a08, -1, 0x0c),
2748 	/* remove unrelated option for v4 phy */
2749 	PHY_TUNING_ENTRY_PMA(0x0a0c, -1, 0x05),
2750 	PHY_TUNING_ENTRY_PMA(0x1a0c, -1, 0x05),
2751 	/* improve Gen2 LVCC */
2752 	PHY_TUNING_ENTRY_PMA(0x00f8, -1, 0x1c),
2753 	PHY_TUNING_ENTRY_PMA(0x00fc, -1, 0x54),
2754 	/* Change Vth of RCV_DET because of TD 7.40 Polling Retry Test */
2755 	PHY_TUNING_ENTRY_PMA(0x104c, -1, 0x07),
2756 	PHY_TUNING_ENTRY_PMA(0x204c, -1, 0x07),
2757 	/* reduce Ux Exit time, assuming 26MHz clock */
2758 	/* Gen1 */
2759 	PHY_TUNING_ENTRY_PMA(0x0ca8, -1, 0x00),
2760 	PHY_TUNING_ENTRY_PMA(0x0cac, -1, 0x04),
2761 	PHY_TUNING_ENTRY_PMA(0x1ca8, -1, 0x00),
2762 	PHY_TUNING_ENTRY_PMA(0x1cac, -1, 0x04),
2763 	/* Gen2 */
2764 	PHY_TUNING_ENTRY_PMA(0x0cb8, -1, 0x00),
2765 	PHY_TUNING_ENTRY_PMA(0x0cbc, -1, 0x04),
2766 	PHY_TUNING_ENTRY_PMA(0x1cb8, -1, 0x00),
2767 	PHY_TUNING_ENTRY_PMA(0x1cbc, -1, 0x04),
2768 	/* RX impedance setting */
2769 	PHY_TUNING_ENTRY_PMA(0x0bb0, 0x03, 0x01),
2770 	PHY_TUNING_ENTRY_PMA(0x0bb4, 0xf0, 0xa0),
2771 	PHY_TUNING_ENTRY_PMA(0x1bb0, 0x03, 0x01),
2772 	PHY_TUNING_ENTRY_PMA(0x1bb4, 0xf0, 0xa0),
2773 
2774 	PHY_TUNING_ENTRY_LAST
2775 };
2776 
2777 static const struct exynos5_usbdrd_phy_tuning gs101_tunes_pipe3_init[] = {
2778 	/* init */
2779 	/* abnormal common pattern mask */
2780 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_BACK_END_MODE_VEC,
2781 			     BACK_END_MODE_VEC_DISABLE_DATA_MASK, 0),
2782 	/* de-serializer enabled when U2 */
2783 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_OUT_VEC_2, PCS_OUT_VEC_B4_DYNAMIC,
2784 			     PCS_OUT_VEC_B4_SEL_OUT),
2785 	/* TX Keeper Disable, Squelch on when U3 */
2786 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_OUT_VEC_3, PCS_OUT_VEC_B7_DYNAMIC,
2787 			     PCS_OUT_VEC_B7_SEL_OUT | PCS_OUT_VEC_B2_SEL_OUT),
2788 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_NS_VEC_PS1_N1, -1,
2789 			     (FIELD_PREP_CONST(NS_VEC_NS_REQ, 5) |
2790 			      NS_VEC_ENABLE_TIMER |
2791 			      FIELD_PREP_CONST(NS_VEC_SEL_TIMEOUT, 3))),
2792 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_NS_VEC_PS2_N0, -1,
2793 			     (FIELD_PREP_CONST(NS_VEC_NS_REQ, 1) |
2794 			      NS_VEC_ENABLE_TIMER |
2795 			      FIELD_PREP_CONST(NS_VEC_SEL_TIMEOUT, 3) |
2796 			      FIELD_PREP_CONST(NS_VEC_COND_MASK, 2) |
2797 			      FIELD_PREP_CONST(NS_VEC_EXP_COND, 2))),
2798 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_NS_VEC_PS3_N0, -1,
2799 			     (FIELD_PREP_CONST(NS_VEC_NS_REQ, 1) |
2800 			      NS_VEC_ENABLE_TIMER |
2801 			      FIELD_PREP_CONST(NS_VEC_SEL_TIMEOUT, 3) |
2802 			      FIELD_PREP_CONST(NS_VEC_COND_MASK, 7) |
2803 			      FIELD_PREP_CONST(NS_VEC_EXP_COND, 7))),
2804 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_TIMEOUT_0, -1, 112),
2805 	/* Block Aligner Type B */
2806 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_RX_CONTROL, 0,
2807 			     RX_CONTROL_EN_BLOCK_ALIGNER_TYPE_B),
2808 	/* Block align at TS1/TS2 for Gen2 stability (Gen2 only) */
2809 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_RX_CONTROL_DEBUG,
2810 		RX_CONTROL_DEBUG_NUM_COM_FOUND,
2811 		(RX_CONTROL_DEBUG_EN_TS_CHECK |
2812 		 /*
2813 		  * increase pcs ts1 adding packet-cnt 1 --> 4
2814 		  * lnx_rx_valid_rstn_delay_rise_sp/ssp :
2815 		  * 19.6us(0x200) -> 15.3us(0x4)
2816 		  */
2817 		 FIELD_PREP_CONST(RX_CONTROL_DEBUG_NUM_COM_FOUND, 4))),
2818 	/* Gen1 Tx DRIVER pre-shoot, de-emphasis, level ctrl */
2819 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_HS_TX_COEF_MAP_0,
2820 		(HS_TX_COEF_MAP_0_SSTX_DEEMP | HS_TX_COEF_MAP_0_SSTX_LEVEL |
2821 		 HS_TX_COEF_MAP_0_SSTX_PRE_SHOOT),
2822 		(FIELD_PREP_CONST(HS_TX_COEF_MAP_0_SSTX_DEEMP, 8) |
2823 		 FIELD_PREP_CONST(HS_TX_COEF_MAP_0_SSTX_LEVEL, 0xb) |
2824 		 FIELD_PREP_CONST(HS_TX_COEF_MAP_0_SSTX_PRE_SHOOT, 0))),
2825 	/* Gen2 Tx DRIVER level ctrl */
2826 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_LOCAL_COEF,
2827 		LOCAL_COEF_PMA_CENTER_COEF,
2828 		FIELD_PREP_CONST(LOCAL_COEF_PMA_CENTER_COEF, 0xb)),
2829 	/* Gen2 U1 exit LFPS duration : 900ns ~ 1.2us */
2830 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_TIMEOUT_3, -1, 4096),
2831 	/* set skp_remove_th 0x2 -> 0x7 for avoiding retry problem. */
2832 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_EBUF_PARAM,
2833 		EBUF_PARAM_SKP_REMOVE_TH_EMPTY_MODE,
2834 		FIELD_PREP_CONST(EBUF_PARAM_SKP_REMOVE_TH_EMPTY_MODE, 0x7)),
2835 
2836 	PHY_TUNING_ENTRY_LAST
2837 };
2838 
2839 static const struct exynos5_usbdrd_phy_tuning gs101_tunes_pipe3_postlock[] = {
2840 	/* Squelch off when U3 */
2841 	PHY_TUNING_ENTRY_PCS(EXYNOS9_PCS_OUT_VEC_3, PCS_OUT_VEC_B2_SEL_OUT, 0),
2842 
2843 	PHY_TUNING_ENTRY_LAST
2844 };
2845 
2846 static const struct exynos5_usbdrd_phy_tuning *gs101_tunes[PTS_MAX] = {
2847 	[PTS_UTMI_POSTINIT] = gs101_tunes_utmi_postinit,
2848 	[PTS_PIPE3_PREINIT] = gs101_tunes_pipe3_preinit,
2849 	[PTS_PIPE3_INIT] = gs101_tunes_pipe3_init,
2850 	[PTS_PIPE3_POSTLOCK] = gs101_tunes_pipe3_postlock,
2851 };
2852 
2853 static const char * const gs101_clk_names[] = {
2854 	"phy", "ctrl_aclk", "ctrl_pclk", "scl_pclk",
2855 };
2856 
2857 static const char * const gs101_regulator_names[] = {
2858 	"pll",
2859 	"dvdd-usb20", "vddh-usb20", "vdd33-usb20",
2860 	"vdda-usbdp", "vddh-usbdp",
2861 };
2862 
2863 static const struct exynos5_usbdrd_phy_drvdata gs101_usbd31rd_phy = {
2864 	.phy_cfg			= phy_cfg_gs101,
2865 	.phy_tunes			= gs101_tunes,
2866 	.phy_ops			= &gs101_usbdrd_phy_ops,
2867 	.pmu_offset_usbdrd0_phy		= GS101_PHY_CTRL_USB20,
2868 	.pmu_offset_usbdrd0_phy_ss	= GS101_PHY_CTRL_USBDP,
2869 	.clk_names			= gs101_clk_names,
2870 	.n_clks				= ARRAY_SIZE(gs101_clk_names),
2871 	.core_clk_names			= exynos5_core_clk_names,
2872 	.n_core_clks			= ARRAY_SIZE(exynos5_core_clk_names),
2873 	.regulator_names		= gs101_regulator_names,
2874 	.n_regulators			= ARRAY_SIZE(gs101_regulator_names),
2875 };
2876 
2877 static const struct of_device_id exynos5_usbdrd_phy_of_match[] = {
2878 	{
2879 		.compatible = "google,gs101-usb31drd-phy",
2880 		.data = &gs101_usbd31rd_phy
2881 	}, {
2882 		.compatible = "samsung,exynos2200-usb32drd-phy",
2883 		.data = &exynos2200_usb32drd_phy,
2884 	}, {
2885 		.compatible = "samsung,exynos5250-usbdrd-phy",
2886 		.data = &exynos5250_usbdrd_phy
2887 	}, {
2888 		.compatible = "samsung,exynos5420-usbdrd-phy",
2889 		.data = &exynos5420_usbdrd_phy
2890 	}, {
2891 		.compatible = "samsung,exynos5433-usbdrd-phy",
2892 		.data = &exynos5433_usbdrd_phy
2893 	}, {
2894 		.compatible = "samsung,exynos7-usbdrd-phy",
2895 		.data = &exynos7_usbdrd_phy
2896 	}, {
2897 		.compatible = "samsung,exynos7870-usbdrd-phy",
2898 		.data = &exynos7870_usbdrd_phy
2899 	}, {
2900 		.compatible = "samsung,exynos850-usbdrd-phy",
2901 		.data = &exynos850_usbdrd_phy
2902 	}, {
2903 		.compatible = "samsung,exynos990-usbdrd-phy",
2904 		.data = &exynos990_usbdrd_phy
2905 	}, {
2906 		.compatible = "samsung,exynosautov920-usb31drd-combo-ssphy",
2907 		.data = &exynosautov920_usb31drd_combo_ssphy
2908 	}, {
2909 		.compatible = "samsung,exynosautov920-usbdrd-combo-hsphy",
2910 		.data = &exynosautov920_usbdrd_combo_hsphy
2911 	}, {
2912 		.compatible = "samsung,exynosautov920-usbdrd-phy",
2913 		.data = &exynosautov920_usbdrd_phy
2914 	},
2915 	{ },
2916 };
2917 MODULE_DEVICE_TABLE(of, exynos5_usbdrd_phy_of_match);
2918 
2919 static int exynos5_usbdrd_phy_probe(struct platform_device *pdev)
2920 {
2921 	struct device *dev = &pdev->dev;
2922 	struct device_node *node = dev->of_node;
2923 	struct exynos5_usbdrd_phy *phy_drd;
2924 	struct phy_provider *phy_provider;
2925 	const struct exynos5_usbdrd_phy_drvdata *drv_data;
2926 	struct regmap *reg_pmu;
2927 	u32 pmu_offset;
2928 	int i, ret;
2929 	int channel;
2930 
2931 	phy_drd = devm_kzalloc(dev, sizeof(*phy_drd), GFP_KERNEL);
2932 	if (!phy_drd)
2933 		return -ENOMEM;
2934 
2935 	dev_set_drvdata(dev, phy_drd);
2936 	phy_drd->dev = dev;
2937 
2938 	drv_data = of_device_get_match_data(dev);
2939 	if (!drv_data)
2940 		return -EINVAL;
2941 	phy_drd->drv_data = drv_data;
2942 
2943 	ret = devm_mutex_init(dev, &phy_drd->phy_mutex);
2944 	if (ret)
2945 		return ret;
2946 
2947 	if (of_property_present(dev->of_node, "reg-names")) {
2948 		void __iomem *reg;
2949 
2950 		reg = devm_platform_ioremap_resource_byname(pdev, "phy");
2951 		if (IS_ERR(reg))
2952 			return PTR_ERR(reg);
2953 		phy_drd->reg_phy = reg;
2954 
2955 		reg = devm_platform_ioremap_resource_byname(pdev, "pcs");
2956 		if (IS_ERR(reg))
2957 			return PTR_ERR(reg);
2958 		phy_drd->reg_pcs = reg;
2959 
2960 		reg = devm_platform_ioremap_resource_byname(pdev, "pma");
2961 		if (IS_ERR(reg))
2962 			return PTR_ERR(reg);
2963 		phy_drd->reg_pma = reg;
2964 	} else {
2965 		/* DTB with just a single region */
2966 		phy_drd->reg_phy = devm_platform_ioremap_resource(pdev, 0);
2967 		if (IS_ERR(phy_drd->reg_phy))
2968 			return PTR_ERR(phy_drd->reg_phy);
2969 	}
2970 
2971 	/*
2972 	 * USB32DRD 4nm controller implements Synopsys eUSB2.0 PHY
2973 	 * and Synopsys SS/USBDP COMBOPHY, managed by external code.
2974 	 */
2975 	if (of_property_present(dev->of_node, "phy-names")) {
2976 		phy_drd->hs_phy = devm_of_phy_get(dev, dev->of_node, "hs");
2977 		if (IS_ERR(phy_drd->hs_phy))
2978 			return dev_err_probe(dev, PTR_ERR(phy_drd->hs_phy),
2979 					     "failed to get hs_phy\n");
2980 	}
2981 
2982 	ret = exynos5_usbdrd_phy_clk_handle(phy_drd);
2983 	if (ret)
2984 		return ret;
2985 
2986 	reg_pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
2987 						   "samsung,pmu-syscon");
2988 	if (IS_ERR(reg_pmu))
2989 		return dev_err_probe(dev, PTR_ERR(reg_pmu),
2990 				     "Failed to lookup PMU regmap\n");
2991 
2992 	/*
2993 	 * Exynos5420 SoC has multiple channels for USB 3.0 PHY, with
2994 	 * each having separate power control registers.
2995 	 * 'channel' facilitates to set such registers.
2996 	 */
2997 	channel = of_alias_get_id(node, "usbdrdphy");
2998 	if (channel < 0)
2999 		dev_dbg(dev, "Not a multi-controller usbdrd phy\n");
3000 
3001 	/* Get regulators */
3002 	phy_drd->regulators = devm_kcalloc(dev,
3003 					   drv_data->n_regulators,
3004 					   sizeof(*phy_drd->regulators),
3005 					   GFP_KERNEL);
3006 	if (!phy_drd->regulators)
3007 		return -ENOMEM;
3008 	regulator_bulk_set_supply_names(phy_drd->regulators,
3009 					drv_data->regulator_names,
3010 					drv_data->n_regulators);
3011 	ret = devm_regulator_bulk_get(dev, drv_data->n_regulators,
3012 				      phy_drd->regulators);
3013 	if (ret)
3014 		return dev_err_probe(dev, ret, "failed to get regulators\n");
3015 
3016 	ret = exynos5_usbdrd_setup_notifiers(phy_drd);
3017 	if (ret)
3018 		return ret;
3019 
3020 	dev_vdbg(dev, "Creating usbdrd_phy phy\n");
3021 
3022 	for (i = 0; i < EXYNOS5_DRDPHYS_NUM; i++) {
3023 		struct phy *phy = devm_phy_create(dev, NULL, drv_data->phy_ops);
3024 
3025 		if (IS_ERR(phy))
3026 			return dev_err_probe(dev, PTR_ERR(phy),
3027 					     "Failed to create usbdrd_phy phy\n");
3028 
3029 		phy_drd->phys[i].phy = phy;
3030 		phy_drd->phys[i].index = i;
3031 		phy_drd->phys[i].reg_pmu = reg_pmu;
3032 		switch (channel) {
3033 		case 1:
3034 			pmu_offset = drv_data->pmu_offset_usbdrd1_phy;
3035 			break;
3036 		case 0:
3037 		default:
3038 			pmu_offset = drv_data->pmu_offset_usbdrd0_phy;
3039 			if (i == EXYNOS5_DRDPHY_PIPE3 && drv_data
3040 						->pmu_offset_usbdrd0_phy_ss)
3041 				pmu_offset = drv_data->pmu_offset_usbdrd0_phy_ss;
3042 			break;
3043 		}
3044 		phy_drd->phys[i].pmu_offset = pmu_offset;
3045 		phy_drd->phys[i].phy_cfg = &drv_data->phy_cfg[i];
3046 		phy_set_drvdata(phy, &phy_drd->phys[i]);
3047 	}
3048 
3049 	phy_provider = devm_of_phy_provider_register(dev,
3050 						     exynos5_usbdrd_phy_xlate);
3051 	if (IS_ERR(phy_provider))
3052 		return dev_err_probe(phy_drd->dev, PTR_ERR(phy_provider),
3053 				     "Failed to register phy provider\n");
3054 
3055 	return 0;
3056 }
3057 
3058 static struct platform_driver exynos5_usb3drd_phy = {
3059 	.probe	= exynos5_usbdrd_phy_probe,
3060 	.driver = {
3061 		.of_match_table	= exynos5_usbdrd_phy_of_match,
3062 		.name		= "exynos5_usb3drd_phy",
3063 		.suppress_bind_attrs = true,
3064 	}
3065 };
3066 
3067 module_platform_driver(exynos5_usb3drd_phy);
3068 MODULE_DESCRIPTION("Samsung Exynos5 SoCs USB 3.0 DRD controller PHY driver");
3069 MODULE_AUTHOR("Vivek Gautam <gautam.vivek@samsung.com>");
3070 MODULE_LICENSE("GPL v2");
3071