xref: /linux/drivers/phy/tegra/xusb-tegra186.c (revision 21eeefe76919c904dd50d543bd6d3eee05d97e15)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016-2022, NVIDIA CORPORATION.  All rights reserved.
4  */
5 
6 #include <linux/delay.h>
7 #include <linux/io.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/phy/phy.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/platform_device.h>
13 #include <linux/clk.h>
14 #include <linux/slab.h>
15 
16 #include <soc/tegra/fuse.h>
17 
18 #include "xusb.h"
19 
20 /* FUSE USB_CALIB registers */
21 #define HS_CURR_LEVEL_PADX_SHIFT(x)	((x) ? (11 + (x - 1) * 6) : 0)
22 #define HS_CURR_LEVEL_PAD_MASK		0x3f
23 #define HS_TERM_RANGE_ADJ_SHIFT		7
24 #define HS_TERM_RANGE_ADJ_MASK		0xf
25 #define HS_SQUELCH_SHIFT		29
26 #define HS_SQUELCH_MASK			0x7
27 
28 #define RPD_CTRL_SHIFT			0
29 #define RPD_CTRL_MASK			0x1f
30 
31 /* XUSB PADCTL registers */
32 #define XUSB_PADCTL_USB2_PAD_MUX	0x4
33 #define  USB2_PORT_SHIFT(x)		((x) * 2)
34 #define  USB2_PORT_MASK			0x3
35 #define   PORT_XUSB			1
36 #define  HSIC_PORT_SHIFT(x)		((x) + 20)
37 #define  HSIC_PORT_MASK			0x1
38 #define   PORT_HSIC			0
39 
40 #define XUSB_PADCTL_USB2_PORT_CAP	0x8
41 #define XUSB_PADCTL_SS_PORT_CAP		0xc
42 #define  PORTX_CAP_SHIFT(x)		((x) * 4)
43 #define  PORT_CAP_MASK			0x3
44 #define   PORT_CAP_DISABLED		0x0
45 #define   PORT_CAP_HOST			0x1
46 #define   PORT_CAP_DEVICE		0x2
47 #define   PORT_CAP_OTG			0x3
48 
49 #define XUSB_PADCTL_ELPG_PROGRAM		0x20
50 #define  USB2_PORT_WAKE_INTERRUPT_ENABLE(x)		BIT(x)
51 #define  USB2_PORT_WAKEUP_EVENT(x)			BIT((x) +  7)
52 #define  SS_PORT_WAKE_INTERRUPT_ENABLE(x)		BIT((x) + 14)
53 #define  SS_PORT_WAKEUP_EVENT(x)			BIT((x) + 21)
54 #define  USB2_HSIC_PORT_WAKE_INTERRUPT_ENABLE(x)	BIT((x) + 28)
55 #define  USB2_HSIC_PORT_WAKEUP_EVENT(x)			BIT((x) + 30)
56 #define  ALL_WAKE_EVENTS						\
57 	(USB2_PORT_WAKEUP_EVENT(0) | USB2_PORT_WAKEUP_EVENT(1) |	\
58 	USB2_PORT_WAKEUP_EVENT(2) | SS_PORT_WAKEUP_EVENT(0) |		\
59 	SS_PORT_WAKEUP_EVENT(1) | SS_PORT_WAKEUP_EVENT(2) |		\
60 	USB2_HSIC_PORT_WAKEUP_EVENT(0))
61 
62 #define XUSB_PADCTL_ELPG_PROGRAM_1		0x24
63 #define  SSPX_ELPG_CLAMP_EN(x)			BIT(0 + (x) * 3)
64 #define  SSPX_ELPG_CLAMP_EN_EARLY(x)		BIT(1 + (x) * 3)
65 #define  SSPX_ELPG_VCORE_DOWN(x)		BIT(2 + (x) * 3)
66 #define XUSB_PADCTL_SS_PORT_CFG			0x2c
67 #define   PORTX_SPEED_SUPPORT_SHIFT(x)		((x) * 4)
68 #define   PORTX_SPEED_SUPPORT_MASK		(0x3)
69 #define     PORT_SPEED_SUPPORT_GEN1		(0x0)
70 
71 #define XUSB_PADCTL_USB2_OTG_PADX_CTL0(x)	(0x88 + (x) * 0x40)
72 #define  HS_CURR_LEVEL(x)			((x) & 0x3f)
73 #define  TERM_SEL				BIT(25)
74 #define  USB2_OTG_PD				BIT(26)
75 #define  USB2_OTG_PD2				BIT(27)
76 #define  USB2_OTG_PD2_OVRD_EN			BIT(28)
77 #define  USB2_OTG_PD_ZI				BIT(29)
78 
79 #define XUSB_PADCTL_USB2_OTG_PADX_CTL1(x)	(0x8c + (x) * 0x40)
80 #define  USB2_OTG_PD_DR				BIT(2)
81 #define  TERM_RANGE_ADJ(x)			(((x) & 0xf) << 3)
82 #define  RPD_CTRL(x)				(((x) & 0x1f) << 26)
83 
84 #define XUSB_PADCTL_USB2_BIAS_PAD_CTL0		0x284
85 #define  BIAS_PAD_PD				BIT(11)
86 #define  HS_SQUELCH_LEVEL(x)			(((x) & 0x7) << 0)
87 
88 #define XUSB_PADCTL_USB2_BIAS_PAD_CTL1		0x288
89 #define  USB2_TRK_START_TIMER(x)		(((x) & 0x7f) << 12)
90 #define  USB2_TRK_DONE_RESET_TIMER(x)		(((x) & 0x7f) << 19)
91 #define  USB2_PD_TRK				BIT(26)
92 #define  USB2_TRK_COMPLETED			BIT(31)
93 
94 #define XUSB_PADCTL_USB2_BIAS_PAD_CTL2		0x28c
95 #define  USB2_TRK_HW_MODE			BIT(0)
96 #define  CYA_TRK_CODE_UPDATE_ON_IDLE		BIT(31)
97 
98 #define XUSB_PADCTL_HSIC_PADX_CTL0(x)		(0x300 + (x) * 0x20)
99 #define  HSIC_PD_TX_DATA0			BIT(1)
100 #define  HSIC_PD_TX_STROBE			BIT(3)
101 #define  HSIC_PD_RX_DATA0			BIT(4)
102 #define  HSIC_PD_RX_STROBE			BIT(6)
103 #define  HSIC_PD_ZI_DATA0			BIT(7)
104 #define  HSIC_PD_ZI_STROBE			BIT(9)
105 #define  HSIC_RPD_DATA0				BIT(13)
106 #define  HSIC_RPD_STROBE			BIT(15)
107 #define  HSIC_RPU_DATA0				BIT(16)
108 #define  HSIC_RPU_STROBE			BIT(18)
109 
110 #define XUSB_PADCTL_HSIC_PAD_TRK_CTL0		0x340
111 #define  HSIC_TRK_START_TIMER(x)		(((x) & 0x7f) << 5)
112 #define  HSIC_TRK_DONE_RESET_TIMER(x)		(((x) & 0x7f) << 12)
113 #define  HSIC_PD_TRK				BIT(19)
114 
115 #define USB2_VBUS_ID				0x360
116 #define  VBUS_OVERRIDE				BIT(14)
117 #define  ID_OVERRIDE(x)				(((x) & 0xf) << 18)
118 #define  ID_OVERRIDE_FLOATING			ID_OVERRIDE(8)
119 #define  ID_OVERRIDE_GROUNDED			ID_OVERRIDE(0)
120 
121 /* XUSB AO registers */
122 #define XUSB_AO_USB_DEBOUNCE_DEL		(0x4)
123 #define   UHSIC_LINE_DEB_CNT(x)			(((x) & 0xf) << 4)
124 #define   UTMIP_LINE_DEB_CNT(x)			((x) & 0xf)
125 
126 #define XUSB_AO_UTMIP_TRIGGERS(x)		(0x40 + (x) * 4)
127 #define   CLR_WALK_PTR				BIT(0)
128 #define   CAP_CFG				BIT(1)
129 #define   CLR_WAKE_ALARM			BIT(3)
130 
131 #define XUSB_AO_UHSIC_TRIGGERS(x)		(0x60 + (x) * 4)
132 #define   HSIC_CLR_WALK_PTR			BIT(0)
133 #define   HSIC_CLR_WAKE_ALARM			BIT(3)
134 #define   HSIC_CAP_CFG				BIT(4)
135 
136 #define XUSB_AO_UTMIP_SAVED_STATE(x)		(0x70 + (x) * 4)
137 #define   SPEED(x)				((x) & 0x3)
138 #define     UTMI_HS				SPEED(0)
139 #define     UTMI_FS				SPEED(1)
140 #define     UTMI_LS				SPEED(2)
141 #define     UTMI_RST				SPEED(3)
142 
143 #define XUSB_AO_UHSIC_SAVED_STATE(x)		(0x90 + (x) * 4)
144 #define   MODE(x)				((x) & 0x1)
145 #define   MODE_HS				MODE(0)
146 #define   MODE_RST				MODE(1)
147 
148 #define XUSB_AO_UTMIP_SLEEPWALK_STATUS(x)	(0xa0 + (x) * 4)
149 
150 #define XUSB_AO_UTMIP_SLEEPWALK_CFG(x)		(0xd0 + (x) * 4)
151 #define XUSB_AO_UHSIC_SLEEPWALK_CFG(x)		(0xf0 + (x) * 4)
152 #define   FAKE_USBOP_VAL			BIT(0)
153 #define   FAKE_USBON_VAL			BIT(1)
154 #define   FAKE_USBOP_EN				BIT(2)
155 #define   FAKE_USBON_EN				BIT(3)
156 #define   FAKE_STROBE_VAL			BIT(0)
157 #define   FAKE_DATA_VAL				BIT(1)
158 #define   FAKE_STROBE_EN			BIT(2)
159 #define   FAKE_DATA_EN				BIT(3)
160 #define   WAKE_WALK_EN				BIT(14)
161 #define   MASTER_ENABLE				BIT(15)
162 #define   LINEVAL_WALK_EN			BIT(16)
163 #define   WAKE_VAL(x)				(((x) & 0xf) << 17)
164 #define     WAKE_VAL_NONE			WAKE_VAL(12)
165 #define     WAKE_VAL_ANY			WAKE_VAL(15)
166 #define     WAKE_VAL_DS10			WAKE_VAL(2)
167 #define   LINE_WAKEUP_EN			BIT(21)
168 #define   MASTER_CFG_SEL			BIT(22)
169 
170 #define XUSB_AO_UTMIP_SLEEPWALK(x)		(0x100 + (x) * 4)
171 /* phase A */
172 #define   USBOP_RPD_A				BIT(0)
173 #define   USBON_RPD_A				BIT(1)
174 #define   AP_A					BIT(4)
175 #define   AN_A					BIT(5)
176 #define   HIGHZ_A				BIT(6)
177 #define   MASTER_ENABLE_A			BIT(7)
178 /* phase B */
179 #define   USBOP_RPD_B				BIT(8)
180 #define   USBON_RPD_B				BIT(9)
181 #define   AP_B					BIT(12)
182 #define   AN_B					BIT(13)
183 #define   HIGHZ_B				BIT(14)
184 #define   MASTER_ENABLE_B			BIT(15)
185 /* phase C */
186 #define   USBOP_RPD_C				BIT(16)
187 #define   USBON_RPD_C				BIT(17)
188 #define   AP_C					BIT(20)
189 #define   AN_C					BIT(21)
190 #define   HIGHZ_C				BIT(22)
191 #define   MASTER_ENABLE_C			BIT(23)
192 /* phase D */
193 #define   USBOP_RPD_D				BIT(24)
194 #define   USBON_RPD_D				BIT(25)
195 #define   AP_D					BIT(28)
196 #define   AN_D					BIT(29)
197 #define   HIGHZ_D				BIT(30)
198 #define   MASTER_ENABLE_D			BIT(31)
199 #define   MASTER_ENABLE_B_C_D					\
200 	 (MASTER_ENABLE_B | MASTER_ENABLE_C | MASTER_ENABLE_D)
201 
202 #define XUSB_AO_UHSIC_SLEEPWALK(x)		(0x120 + (x) * 4)
203 /* phase A */
204 #define   RPD_STROBE_A				BIT(0)
205 #define   RPD_DATA0_A				BIT(1)
206 #define   RPU_STROBE_A				BIT(2)
207 #define   RPU_DATA0_A				BIT(3)
208 /* phase B */
209 #define   RPD_STROBE_B				BIT(8)
210 #define   RPD_DATA0_B				BIT(9)
211 #define   RPU_STROBE_B				BIT(10)
212 #define   RPU_DATA0_B				BIT(11)
213 /* phase C */
214 #define   RPD_STROBE_C				BIT(16)
215 #define   RPD_DATA0_C				BIT(17)
216 #define   RPU_STROBE_C				BIT(18)
217 #define   RPU_DATA0_C				BIT(19)
218 /* phase D */
219 #define   RPD_STROBE_D				BIT(24)
220 #define   RPD_DATA0_D				BIT(25)
221 #define   RPU_STROBE_D				BIT(26)
222 #define   RPU_DATA0_D				BIT(27)
223 
224 #define XUSB_AO_UTMIP_PAD_CFG(x)		(0x130 + (x) * 4)
225 #define   FSLS_USE_XUSB_AO			BIT(3)
226 #define   TRK_CTRL_USE_XUSB_AO			BIT(4)
227 #define   RPD_CTRL_USE_XUSB_AO			BIT(5)
228 #define   RPU_USE_XUSB_AO			BIT(6)
229 #define   VREG_USE_XUSB_AO			BIT(7)
230 #define   USBOP_VAL_PD				BIT(8)
231 #define   USBON_VAL_PD				BIT(9)
232 #define   E_DPD_OVRD_EN				BIT(10)
233 #define   E_DPD_OVRD_VAL			BIT(11)
234 
235 #define XUSB_AO_UHSIC_PAD_CFG(x)		(0x150 + (x) * 4)
236 #define   STROBE_VAL_PD				BIT(0)
237 #define   DATA0_VAL_PD				BIT(1)
238 #define   USE_XUSB_AO				BIT(4)
239 
240 #define TEGRA_UTMI_PAD_MAX 4
241 
242 #define TEGRA186_LANE(_name, _offset, _shift, _mask, _type)		\
243 	{								\
244 		.name = _name,						\
245 		.offset = _offset,					\
246 		.shift = _shift,					\
247 		.mask = _mask,						\
248 		.num_funcs = ARRAY_SIZE(tegra186_##_type##_functions),	\
249 		.funcs = tegra186_##_type##_functions,			\
250 	}
251 
252 struct tegra_xusb_fuse_calibration {
253 	u32 *hs_curr_level;
254 	u32 hs_squelch;
255 	u32 hs_term_range_adj;
256 	u32 rpd_ctrl;
257 };
258 
259 struct tegra186_xusb_padctl_context {
260 	u32 vbus_id;
261 	u32 usb2_pad_mux;
262 	u32 usb2_port_cap;
263 	u32 ss_port_cap;
264 };
265 
266 struct tegra186_xusb_padctl {
267 	struct tegra_xusb_padctl base;
268 	void __iomem *ao_regs;
269 
270 	struct tegra_xusb_fuse_calibration calib;
271 
272 	/* UTMI bias and tracking */
273 	struct clk *usb2_trk_clk;
274 	DECLARE_BITMAP(utmi_pad_enabled, TEGRA_UTMI_PAD_MAX);
275 
276 	/* padctl context */
277 	struct tegra186_xusb_padctl_context context;
278 };
279 
ao_writel(struct tegra186_xusb_padctl * priv,u32 value,unsigned int offset)280 static inline void ao_writel(struct tegra186_xusb_padctl *priv, u32 value, unsigned int offset)
281 {
282 	writel(value, priv->ao_regs + offset);
283 }
284 
ao_readl(struct tegra186_xusb_padctl * priv,unsigned int offset)285 static inline u32 ao_readl(struct tegra186_xusb_padctl *priv, unsigned int offset)
286 {
287 	return readl(priv->ao_regs + offset);
288 }
289 
290 static inline struct tegra186_xusb_padctl *
to_tegra186_xusb_padctl(struct tegra_xusb_padctl * padctl)291 to_tegra186_xusb_padctl(struct tegra_xusb_padctl *padctl)
292 {
293 	return container_of(padctl, struct tegra186_xusb_padctl, base);
294 }
295 
296 /* USB 2.0 UTMI PHY support */
297 static struct tegra_xusb_lane *
tegra186_usb2_lane_probe(struct tegra_xusb_pad * pad,struct device_node * np,unsigned int index)298 tegra186_usb2_lane_probe(struct tegra_xusb_pad *pad, struct device_node *np,
299 			 unsigned int index)
300 {
301 	struct tegra_xusb_usb2_lane *usb2;
302 	int err;
303 
304 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
305 	if (!usb2)
306 		return ERR_PTR(-ENOMEM);
307 
308 	INIT_LIST_HEAD(&usb2->base.list);
309 	usb2->base.soc = &pad->soc->lanes[index];
310 	usb2->base.index = index;
311 	usb2->base.pad = pad;
312 	usb2->base.np = np;
313 
314 	err = tegra_xusb_lane_parse_dt(&usb2->base, np);
315 	if (err < 0) {
316 		kfree(usb2);
317 		return ERR_PTR(err);
318 	}
319 
320 	return &usb2->base;
321 }
322 
tegra186_usb2_lane_remove(struct tegra_xusb_lane * lane)323 static void tegra186_usb2_lane_remove(struct tegra_xusb_lane *lane)
324 {
325 	struct tegra_xusb_usb2_lane *usb2 = to_usb2_lane(lane);
326 
327 	kfree(usb2);
328 }
329 
tegra186_utmi_enable_phy_sleepwalk(struct tegra_xusb_lane * lane,enum usb_device_speed speed)330 static int tegra186_utmi_enable_phy_sleepwalk(struct tegra_xusb_lane *lane,
331 					      enum usb_device_speed speed)
332 {
333 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
334 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
335 	unsigned int index = lane->index;
336 	u32 value;
337 
338 	mutex_lock(&padctl->lock);
339 
340 	/* ensure sleepwalk logic is disabled */
341 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
342 	value &= ~MASTER_ENABLE;
343 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
344 
345 	/* ensure sleepwalk logics are in low power mode */
346 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
347 	value |= MASTER_CFG_SEL;
348 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
349 
350 	/* set debounce time */
351 	value = ao_readl(priv, XUSB_AO_USB_DEBOUNCE_DEL);
352 	value &= ~UTMIP_LINE_DEB_CNT(~0);
353 	value |= UTMIP_LINE_DEB_CNT(1);
354 	ao_writel(priv, value, XUSB_AO_USB_DEBOUNCE_DEL);
355 
356 	/* ensure fake events of sleepwalk logic are desiabled */
357 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
358 	value &= ~(FAKE_USBOP_VAL | FAKE_USBON_VAL |
359 		FAKE_USBOP_EN | FAKE_USBON_EN);
360 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
361 
362 	/* ensure wake events of sleepwalk logic are not latched */
363 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
364 	value &= ~LINE_WAKEUP_EN;
365 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
366 
367 	/* disable wake event triggers of sleepwalk logic */
368 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
369 	value &= ~WAKE_VAL(~0);
370 	value |= WAKE_VAL_NONE;
371 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
372 
373 	/* power down the line state detectors of the pad */
374 	value = ao_readl(priv, XUSB_AO_UTMIP_PAD_CFG(index));
375 	value |= (USBOP_VAL_PD | USBON_VAL_PD);
376 	ao_writel(priv, value, XUSB_AO_UTMIP_PAD_CFG(index));
377 
378 	/* save state per speed */
379 	value = ao_readl(priv, XUSB_AO_UTMIP_SAVED_STATE(index));
380 	value &= ~SPEED(~0);
381 
382 	switch (speed) {
383 	case USB_SPEED_HIGH:
384 		value |= UTMI_HS;
385 		break;
386 
387 	case USB_SPEED_FULL:
388 		value |= UTMI_FS;
389 		break;
390 
391 	case USB_SPEED_LOW:
392 		value |= UTMI_LS;
393 		break;
394 
395 	default:
396 		value |= UTMI_RST;
397 		break;
398 	}
399 
400 	ao_writel(priv, value, XUSB_AO_UTMIP_SAVED_STATE(index));
401 
402 	/* enable the trigger of the sleepwalk logic */
403 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
404 	value |= LINEVAL_WALK_EN;
405 	value &= ~WAKE_WALK_EN;
406 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
407 
408 	/* reset the walk pointer and clear the alarm of the sleepwalk logic,
409 	 * as well as capture the configuration of the USB2.0 pad
410 	 */
411 	value = ao_readl(priv, XUSB_AO_UTMIP_TRIGGERS(index));
412 	value |= (CLR_WALK_PTR | CLR_WAKE_ALARM | CAP_CFG);
413 	ao_writel(priv, value, XUSB_AO_UTMIP_TRIGGERS(index));
414 
415 	/* setup the pull-ups and pull-downs of the signals during the four
416 	 * stages of sleepwalk.
417 	 * if device is connected, program sleepwalk logic to maintain a J and
418 	 * keep driving K upon seeing remote wake.
419 	 */
420 	value = USBOP_RPD_A | USBOP_RPD_B | USBOP_RPD_C | USBOP_RPD_D;
421 	value |= USBON_RPD_A | USBON_RPD_B | USBON_RPD_C | USBON_RPD_D;
422 
423 	switch (speed) {
424 	case USB_SPEED_HIGH:
425 	case USB_SPEED_FULL:
426 		/* J state: D+/D- = high/low, K state: D+/D- = low/high */
427 		value |= HIGHZ_A;
428 		value |= AP_A;
429 		value |= AN_B | AN_C | AN_D;
430 		if (padctl->soc->supports_lp_cfg_en)
431 			value |= MASTER_ENABLE_B_C_D;
432 		break;
433 
434 	case USB_SPEED_LOW:
435 		/* J state: D+/D- = low/high, K state: D+/D- = high/low */
436 		value |= HIGHZ_A;
437 		value |= AN_A;
438 		value |= AP_B | AP_C | AP_D;
439 		if (padctl->soc->supports_lp_cfg_en)
440 			value |= MASTER_ENABLE_B_C_D;
441 		break;
442 
443 	default:
444 		value |= HIGHZ_A | HIGHZ_B | HIGHZ_C | HIGHZ_D;
445 		break;
446 	}
447 
448 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK(index));
449 
450 	/* power up the line state detectors of the pad */
451 	value = ao_readl(priv, XUSB_AO_UTMIP_PAD_CFG(index));
452 	value &= ~(USBOP_VAL_PD | USBON_VAL_PD);
453 	ao_writel(priv, value, XUSB_AO_UTMIP_PAD_CFG(index));
454 
455 	usleep_range(150, 200);
456 
457 	/* switch the electric control of the USB2.0 pad to XUSB_AO */
458 	value = ao_readl(priv, XUSB_AO_UTMIP_PAD_CFG(index));
459 	value |= FSLS_USE_XUSB_AO | TRK_CTRL_USE_XUSB_AO | RPD_CTRL_USE_XUSB_AO |
460 		 RPU_USE_XUSB_AO | VREG_USE_XUSB_AO;
461 	ao_writel(priv, value, XUSB_AO_UTMIP_PAD_CFG(index));
462 
463 	/* set the wake signaling trigger events */
464 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
465 	value &= ~WAKE_VAL(~0);
466 	value |= WAKE_VAL_ANY;
467 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
468 
469 	/* enable the wake detection */
470 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
471 	value |= MASTER_ENABLE | LINE_WAKEUP_EN;
472 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
473 
474 	mutex_unlock(&padctl->lock);
475 
476 	return 0;
477 }
478 
tegra186_utmi_disable_phy_sleepwalk(struct tegra_xusb_lane * lane)479 static int tegra186_utmi_disable_phy_sleepwalk(struct tegra_xusb_lane *lane)
480 {
481 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
482 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
483 	unsigned int index = lane->index;
484 	u32 value;
485 
486 	mutex_lock(&padctl->lock);
487 
488 	/* disable the wake detection */
489 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
490 	value &= ~(MASTER_ENABLE | LINE_WAKEUP_EN);
491 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
492 
493 	/* switch the electric control of the USB2.0 pad to XUSB vcore logic */
494 	value = ao_readl(priv, XUSB_AO_UTMIP_PAD_CFG(index));
495 	value &= ~(FSLS_USE_XUSB_AO | TRK_CTRL_USE_XUSB_AO | RPD_CTRL_USE_XUSB_AO |
496 		   RPU_USE_XUSB_AO | VREG_USE_XUSB_AO);
497 	ao_writel(priv, value, XUSB_AO_UTMIP_PAD_CFG(index));
498 
499 	/* disable wake event triggers of sleepwalk logic */
500 	value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
501 	value &= ~WAKE_VAL(~0);
502 	value |= WAKE_VAL_NONE;
503 	ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK_CFG(index));
504 
505 	if (padctl->soc->supports_lp_cfg_en) {
506 		/* disable the four stages of sleepwalk */
507 		value = ao_readl(priv, XUSB_AO_UTMIP_SLEEPWALK(index));
508 		value &= ~(MASTER_ENABLE_A | MASTER_ENABLE_B_C_D);
509 		ao_writel(priv, value, XUSB_AO_UTMIP_SLEEPWALK(index));
510 	}
511 
512 	/* power down the line state detectors of the port */
513 	value = ao_readl(priv, XUSB_AO_UTMIP_PAD_CFG(index));
514 	value |= USBOP_VAL_PD | USBON_VAL_PD;
515 	ao_writel(priv, value, XUSB_AO_UTMIP_PAD_CFG(index));
516 
517 	/* clear alarm of the sleepwalk logic */
518 	value = ao_readl(priv, XUSB_AO_UTMIP_TRIGGERS(index));
519 	value |= CLR_WAKE_ALARM;
520 	ao_writel(priv, value, XUSB_AO_UTMIP_TRIGGERS(index));
521 
522 	mutex_unlock(&padctl->lock);
523 
524 	return 0;
525 }
526 
tegra186_utmi_enable_phy_wake(struct tegra_xusb_lane * lane)527 static int tegra186_utmi_enable_phy_wake(struct tegra_xusb_lane *lane)
528 {
529 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
530 	unsigned int index = lane->index;
531 	u32 value;
532 
533 	mutex_lock(&padctl->lock);
534 
535 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
536 	value &= ~ALL_WAKE_EVENTS;
537 	value |= USB2_PORT_WAKEUP_EVENT(index);
538 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
539 
540 	usleep_range(10, 20);
541 
542 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
543 	value &= ~ALL_WAKE_EVENTS;
544 	value |= USB2_PORT_WAKE_INTERRUPT_ENABLE(index);
545 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
546 
547 	mutex_unlock(&padctl->lock);
548 
549 	return 0;
550 }
551 
tegra186_utmi_disable_phy_wake(struct tegra_xusb_lane * lane)552 static int tegra186_utmi_disable_phy_wake(struct tegra_xusb_lane *lane)
553 {
554 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
555 	unsigned int index = lane->index;
556 	u32 value;
557 
558 	mutex_lock(&padctl->lock);
559 
560 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
561 	value &= ~ALL_WAKE_EVENTS;
562 	value &= ~USB2_PORT_WAKE_INTERRUPT_ENABLE(index);
563 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
564 
565 	usleep_range(10, 20);
566 
567 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
568 	value &= ~ALL_WAKE_EVENTS;
569 	value |= USB2_PORT_WAKEUP_EVENT(index);
570 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
571 
572 	mutex_unlock(&padctl->lock);
573 
574 	return 0;
575 }
576 
tegra186_utmi_phy_remote_wake_detected(struct tegra_xusb_lane * lane)577 static bool tegra186_utmi_phy_remote_wake_detected(struct tegra_xusb_lane *lane)
578 {
579 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
580 	unsigned int index = lane->index;
581 	u32 value;
582 
583 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
584 	if ((value & USB2_PORT_WAKE_INTERRUPT_ENABLE(index)) &&
585 	    (value & USB2_PORT_WAKEUP_EVENT(index)))
586 		return true;
587 
588 	return false;
589 }
590 
591 static const struct tegra_xusb_lane_ops tegra186_usb2_lane_ops = {
592 	.probe = tegra186_usb2_lane_probe,
593 	.remove = tegra186_usb2_lane_remove,
594 	.enable_phy_sleepwalk = tegra186_utmi_enable_phy_sleepwalk,
595 	.disable_phy_sleepwalk = tegra186_utmi_disable_phy_sleepwalk,
596 	.enable_phy_wake = tegra186_utmi_enable_phy_wake,
597 	.disable_phy_wake = tegra186_utmi_disable_phy_wake,
598 	.remote_wake_detected = tegra186_utmi_phy_remote_wake_detected,
599 };
600 
tegra186_utmi_bias_pad_power_on(struct tegra_xusb_padctl * padctl)601 static void tegra186_utmi_bias_pad_power_on(struct tegra_xusb_padctl *padctl)
602 {
603 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
604 	struct device *dev = padctl->dev;
605 	u32 value;
606 	int err;
607 
608 	if (!bitmap_empty(priv->utmi_pad_enabled, TEGRA_UTMI_PAD_MAX))
609 		return;
610 
611 	err = clk_prepare_enable(priv->usb2_trk_clk);
612 	if (err < 0)
613 		dev_warn(dev, "failed to enable USB2 trk clock: %d\n", err);
614 
615 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
616 	value &= ~USB2_TRK_START_TIMER(~0);
617 	value |= USB2_TRK_START_TIMER(0x1e);
618 	value &= ~USB2_TRK_DONE_RESET_TIMER(~0);
619 	value |= USB2_TRK_DONE_RESET_TIMER(0xa);
620 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
621 
622 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
623 	value &= ~BIAS_PAD_PD;
624 	value &= ~HS_SQUELCH_LEVEL(~0);
625 	value |= HS_SQUELCH_LEVEL(priv->calib.hs_squelch);
626 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
627 
628 	udelay(1);
629 
630 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
631 	value &= ~USB2_PD_TRK;
632 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
633 
634 	if (padctl->soc->poll_trk_completed) {
635 		err = padctl_readl_poll(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1,
636 					USB2_TRK_COMPLETED, USB2_TRK_COMPLETED, 100);
637 		if (err) {
638 			/* The failure with polling on trk complete will not
639 			 * cause the failure of powering on the bias pad.
640 			 */
641 			dev_warn(dev, "failed to poll USB2 trk completed: %d\n", err);
642 		}
643 
644 		value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
645 		value |= USB2_TRK_COMPLETED;
646 		padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
647 	} else {
648 		udelay(100);
649 	}
650 
651 	if (padctl->soc->trk_hw_mode) {
652 		value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL2);
653 		value |= USB2_TRK_HW_MODE;
654 		value &= ~CYA_TRK_CODE_UPDATE_ON_IDLE;
655 		padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL2);
656 	} else {
657 		clk_disable_unprepare(priv->usb2_trk_clk);
658 	}
659 }
660 
tegra186_utmi_bias_pad_power_off(struct tegra_xusb_padctl * padctl)661 static void tegra186_utmi_bias_pad_power_off(struct tegra_xusb_padctl *padctl)
662 {
663 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
664 	u32 value;
665 
666 	if (!bitmap_empty(priv->utmi_pad_enabled, TEGRA_UTMI_PAD_MAX))
667 		return;
668 
669 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
670 	value |= USB2_PD_TRK;
671 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL1);
672 
673 	if (padctl->soc->trk_hw_mode) {
674 		value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL2);
675 		value &= ~USB2_TRK_HW_MODE;
676 		padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL2);
677 		clk_disable_unprepare(priv->usb2_trk_clk);
678 	}
679 
680 }
681 
tegra186_utmi_pad_power_on(struct phy * phy)682 static void tegra186_utmi_pad_power_on(struct phy *phy)
683 {
684 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
685 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
686 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
687 	struct tegra_xusb_usb2_port *port;
688 	struct device *dev = padctl->dev;
689 	unsigned int index = lane->index;
690 	u32 value;
691 
692 	if (!phy)
693 		return;
694 
695 	mutex_lock(&padctl->lock);
696 	if (test_bit(index, priv->utmi_pad_enabled)) {
697 		mutex_unlock(&padctl->lock);
698 		return;
699 	}
700 
701 	port = tegra_xusb_find_usb2_port(padctl, index);
702 	if (!port) {
703 		dev_err(dev, "no port found for USB2 lane %u\n", index);
704 		mutex_unlock(&padctl->lock);
705 		return;
706 	}
707 
708 	dev_dbg(dev, "power on UTMI pad %u\n", index);
709 
710 	tegra186_utmi_bias_pad_power_on(padctl);
711 
712 	udelay(2);
713 
714 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
715 	value &= ~USB2_OTG_PD;
716 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
717 
718 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
719 	value &= ~USB2_OTG_PD_DR;
720 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
721 
722 	set_bit(index, priv->utmi_pad_enabled);
723 	mutex_unlock(&padctl->lock);
724 }
725 
tegra186_utmi_pad_power_down(struct phy * phy)726 static void tegra186_utmi_pad_power_down(struct phy *phy)
727 {
728 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
729 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
730 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
731 	unsigned int index = lane->index;
732 	u32 value;
733 
734 	if (!phy)
735 		return;
736 
737 	mutex_lock(&padctl->lock);
738 	if (!test_bit(index, priv->utmi_pad_enabled)) {
739 		mutex_unlock(&padctl->lock);
740 		return;
741 	}
742 
743 	dev_dbg(padctl->dev, "power down UTMI pad %u\n", index);
744 
745 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
746 	value |= USB2_OTG_PD;
747 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
748 
749 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
750 	value |= USB2_OTG_PD_DR;
751 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
752 
753 	udelay(2);
754 
755 	clear_bit(index, priv->utmi_pad_enabled);
756 
757 	tegra186_utmi_bias_pad_power_off(padctl);
758 
759 	mutex_unlock(&padctl->lock);
760 }
761 
tegra186_xusb_padctl_vbus_override(struct tegra_xusb_padctl * padctl,bool status)762 static int tegra186_xusb_padctl_vbus_override(struct tegra_xusb_padctl *padctl,
763 					       bool status)
764 {
765 	u32 value;
766 
767 	dev_dbg(padctl->dev, "%s vbus override\n", status ? "set" : "clear");
768 
769 	value = padctl_readl(padctl, USB2_VBUS_ID);
770 
771 	if (status) {
772 		value |= VBUS_OVERRIDE;
773 		value &= ~ID_OVERRIDE(~0);
774 		value |= ID_OVERRIDE_FLOATING;
775 	} else {
776 		value &= ~VBUS_OVERRIDE;
777 	}
778 
779 	padctl_writel(padctl, value, USB2_VBUS_ID);
780 
781 	return 0;
782 }
783 
tegra186_xusb_padctl_id_override(struct tegra_xusb_padctl * padctl,bool status)784 static int tegra186_xusb_padctl_id_override(struct tegra_xusb_padctl *padctl,
785 					    bool status)
786 {
787 	u32 value;
788 
789 	dev_dbg(padctl->dev, "%s id override\n", status ? "set" : "clear");
790 
791 	value = padctl_readl(padctl, USB2_VBUS_ID);
792 
793 	if (status) {
794 		if (value & VBUS_OVERRIDE) {
795 			value &= ~VBUS_OVERRIDE;
796 			padctl_writel(padctl, value, USB2_VBUS_ID);
797 			usleep_range(1000, 2000);
798 
799 			value = padctl_readl(padctl, USB2_VBUS_ID);
800 		}
801 
802 		value &= ~ID_OVERRIDE(~0);
803 		value |= ID_OVERRIDE_GROUNDED;
804 	} else {
805 		value &= ~ID_OVERRIDE(~0);
806 		value |= ID_OVERRIDE_FLOATING;
807 	}
808 
809 	padctl_writel(padctl, value, USB2_VBUS_ID);
810 
811 	return 0;
812 }
813 
tegra186_utmi_phy_set_mode(struct phy * phy,enum phy_mode mode,int submode)814 static int tegra186_utmi_phy_set_mode(struct phy *phy, enum phy_mode mode,
815 				      int submode)
816 {
817 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
818 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
819 	struct tegra_xusb_usb2_port *port = tegra_xusb_find_usb2_port(padctl,
820 								lane->index);
821 	int err = 0;
822 
823 	mutex_lock(&padctl->lock);
824 
825 	dev_dbg(&port->base.dev, "%s: mode %d", __func__, mode);
826 
827 	if (mode == PHY_MODE_USB_OTG) {
828 		if (submode == USB_ROLE_HOST) {
829 			tegra186_xusb_padctl_id_override(padctl, true);
830 
831 			err = regulator_enable(port->supply);
832 		} else if (submode == USB_ROLE_DEVICE) {
833 			tegra186_xusb_padctl_vbus_override(padctl, true);
834 		} else if (submode == USB_ROLE_NONE) {
835 			/*
836 			 * When port is peripheral only or role transitions to
837 			 * USB_ROLE_NONE from USB_ROLE_DEVICE, regulator is not
838 			 * enabled.
839 			 */
840 			if (regulator_is_enabled(port->supply))
841 				regulator_disable(port->supply);
842 
843 			tegra186_xusb_padctl_id_override(padctl, false);
844 			tegra186_xusb_padctl_vbus_override(padctl, false);
845 		}
846 	}
847 
848 	mutex_unlock(&padctl->lock);
849 
850 	return err;
851 }
852 
tegra186_utmi_phy_power_on(struct phy * phy)853 static int tegra186_utmi_phy_power_on(struct phy *phy)
854 {
855 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
856 	struct tegra_xusb_usb2_lane *usb2 = to_usb2_lane(lane);
857 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
858 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
859 	struct tegra_xusb_usb2_port *port;
860 	unsigned int index = lane->index;
861 	struct device *dev = padctl->dev;
862 	u32 value;
863 
864 	port = tegra_xusb_find_usb2_port(padctl, index);
865 	if (!port) {
866 		dev_err(dev, "no port found for USB2 lane %u\n", index);
867 		return -ENODEV;
868 	}
869 
870 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_PAD_MUX);
871 	value &= ~(USB2_PORT_MASK << USB2_PORT_SHIFT(index));
872 	value |= (PORT_XUSB << USB2_PORT_SHIFT(index));
873 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_PAD_MUX);
874 
875 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
876 	value &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(index));
877 
878 	if (port->mode == USB_DR_MODE_UNKNOWN)
879 		value |= (PORT_CAP_DISABLED << PORTX_CAP_SHIFT(index));
880 	else if (port->mode == USB_DR_MODE_PERIPHERAL)
881 		value |= (PORT_CAP_DEVICE << PORTX_CAP_SHIFT(index));
882 	else if (port->mode == USB_DR_MODE_HOST)
883 		value |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(index));
884 	else if (port->mode == USB_DR_MODE_OTG)
885 		value |= (PORT_CAP_OTG << PORTX_CAP_SHIFT(index));
886 
887 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_PORT_CAP);
888 
889 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
890 	value &= ~USB2_OTG_PD_ZI;
891 	value |= TERM_SEL;
892 	value &= ~HS_CURR_LEVEL(~0);
893 
894 	if (usb2->hs_curr_level_offset) {
895 		int hs_current_level;
896 
897 		hs_current_level = (int)priv->calib.hs_curr_level[index] +
898 						usb2->hs_curr_level_offset;
899 
900 		if (hs_current_level < 0)
901 			hs_current_level = 0;
902 		if (hs_current_level > 0x3f)
903 			hs_current_level = 0x3f;
904 
905 		value |= HS_CURR_LEVEL(hs_current_level);
906 	} else {
907 		value |= HS_CURR_LEVEL(priv->calib.hs_curr_level[index]);
908 	}
909 
910 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL0(index));
911 
912 	value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
913 	value &= ~TERM_RANGE_ADJ(~0);
914 	value |= TERM_RANGE_ADJ(priv->calib.hs_term_range_adj);
915 	value &= ~RPD_CTRL(~0);
916 	value |= RPD_CTRL(priv->calib.rpd_ctrl);
917 	padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL1(index));
918 
919 	tegra186_utmi_pad_power_on(phy);
920 
921 	return 0;
922 }
923 
tegra186_utmi_phy_power_off(struct phy * phy)924 static int tegra186_utmi_phy_power_off(struct phy *phy)
925 {
926 	tegra186_utmi_pad_power_down(phy);
927 
928 	return 0;
929 }
930 
tegra186_utmi_phy_init(struct phy * phy)931 static int tegra186_utmi_phy_init(struct phy *phy)
932 {
933 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
934 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
935 	struct tegra_xusb_usb2_port *port;
936 	unsigned int index = lane->index;
937 	struct device *dev = padctl->dev;
938 	int err;
939 	u32 reg;
940 
941 	port = tegra_xusb_find_usb2_port(padctl, index);
942 	if (!port) {
943 		dev_err(dev, "no port found for USB2 lane %u\n", index);
944 		return -ENODEV;
945 	}
946 
947 	if (port->mode == USB_DR_MODE_OTG ||
948 	    port->mode == USB_DR_MODE_PERIPHERAL) {
949 		/* reset VBUS&ID OVERRIDE */
950 		reg = padctl_readl(padctl, USB2_VBUS_ID);
951 		reg &= ~VBUS_OVERRIDE;
952 		reg &= ~ID_OVERRIDE(~0);
953 		reg |= ID_OVERRIDE_FLOATING;
954 		padctl_writel(padctl, reg, USB2_VBUS_ID);
955 	}
956 
957 	if (port->supply && port->mode == USB_DR_MODE_HOST) {
958 		err = regulator_enable(port->supply);
959 		if (err) {
960 			dev_err(dev, "failed to enable port %u VBUS: %d\n",
961 				index, err);
962 			return err;
963 		}
964 	}
965 
966 	return 0;
967 }
968 
tegra186_utmi_phy_exit(struct phy * phy)969 static int tegra186_utmi_phy_exit(struct phy *phy)
970 {
971 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
972 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
973 	struct tegra_xusb_usb2_port *port;
974 	unsigned int index = lane->index;
975 	struct device *dev = padctl->dev;
976 	int err;
977 
978 	port = tegra_xusb_find_usb2_port(padctl, index);
979 	if (!port) {
980 		dev_err(dev, "no port found for USB2 lane %u\n", index);
981 		return -ENODEV;
982 	}
983 
984 	if (port->supply && port->mode == USB_DR_MODE_HOST) {
985 		err = regulator_disable(port->supply);
986 		if (err) {
987 			dev_err(dev, "failed to disable port %u VBUS: %d\n",
988 				index, err);
989 			return err;
990 		}
991 	}
992 
993 	return 0;
994 }
995 
996 static const struct phy_ops utmi_phy_ops = {
997 	.init = tegra186_utmi_phy_init,
998 	.exit = tegra186_utmi_phy_exit,
999 	.power_on = tegra186_utmi_phy_power_on,
1000 	.power_off = tegra186_utmi_phy_power_off,
1001 	.set_mode = tegra186_utmi_phy_set_mode,
1002 	.owner = THIS_MODULE,
1003 };
1004 
1005 static struct tegra_xusb_pad *
tegra186_usb2_pad_probe(struct tegra_xusb_padctl * padctl,const struct tegra_xusb_pad_soc * soc,struct device_node * np)1006 tegra186_usb2_pad_probe(struct tegra_xusb_padctl *padctl,
1007 			const struct tegra_xusb_pad_soc *soc,
1008 			struct device_node *np)
1009 {
1010 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
1011 	struct tegra_xusb_usb2_pad *usb2;
1012 	struct tegra_xusb_pad *pad;
1013 	int err;
1014 
1015 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
1016 	if (!usb2)
1017 		return ERR_PTR(-ENOMEM);
1018 
1019 	pad = &usb2->base;
1020 	pad->ops = &tegra186_usb2_lane_ops;
1021 	pad->soc = soc;
1022 
1023 	err = tegra_xusb_pad_init(pad, padctl, np);
1024 	if (err < 0) {
1025 		kfree(usb2);
1026 		goto out;
1027 	}
1028 
1029 	priv->usb2_trk_clk = devm_clk_get(&pad->dev, "trk");
1030 	if (IS_ERR(priv->usb2_trk_clk)) {
1031 		err = PTR_ERR(priv->usb2_trk_clk);
1032 		dev_dbg(&pad->dev, "failed to get usb2 trk clock: %d\n", err);
1033 		goto unregister;
1034 	}
1035 
1036 	err = tegra_xusb_pad_register(pad, &utmi_phy_ops);
1037 	if (err < 0)
1038 		goto unregister;
1039 
1040 	dev_set_drvdata(&pad->dev, pad);
1041 
1042 	return pad;
1043 
1044 unregister:
1045 	device_unregister(&pad->dev);
1046 out:
1047 	return ERR_PTR(err);
1048 }
1049 
tegra186_usb2_pad_remove(struct tegra_xusb_pad * pad)1050 static void tegra186_usb2_pad_remove(struct tegra_xusb_pad *pad)
1051 {
1052 	struct tegra_xusb_usb2_pad *usb2 = to_usb2_pad(pad);
1053 
1054 	kfree(usb2);
1055 }
1056 
1057 static const struct tegra_xusb_pad_ops tegra186_usb2_pad_ops = {
1058 	.probe = tegra186_usb2_pad_probe,
1059 	.remove = tegra186_usb2_pad_remove,
1060 };
1061 
1062 static const char * const tegra186_usb2_functions[] = {
1063 	"xusb",
1064 };
1065 
tegra186_usb2_port_enable(struct tegra_xusb_port * port)1066 static int tegra186_usb2_port_enable(struct tegra_xusb_port *port)
1067 {
1068 	return 0;
1069 }
1070 
tegra186_usb2_port_disable(struct tegra_xusb_port * port)1071 static void tegra186_usb2_port_disable(struct tegra_xusb_port *port)
1072 {
1073 }
1074 
1075 static struct tegra_xusb_lane *
tegra186_usb2_port_map(struct tegra_xusb_port * port)1076 tegra186_usb2_port_map(struct tegra_xusb_port *port)
1077 {
1078 	return tegra_xusb_find_lane(port->padctl, "usb2", port->index);
1079 }
1080 
1081 static const struct tegra_xusb_port_ops tegra186_usb2_port_ops = {
1082 	.release = tegra_xusb_usb2_port_release,
1083 	.remove = tegra_xusb_usb2_port_remove,
1084 	.enable = tegra186_usb2_port_enable,
1085 	.disable = tegra186_usb2_port_disable,
1086 	.map = tegra186_usb2_port_map,
1087 };
1088 
1089 /* SuperSpeed PHY support */
1090 static struct tegra_xusb_lane *
tegra186_usb3_lane_probe(struct tegra_xusb_pad * pad,struct device_node * np,unsigned int index)1091 tegra186_usb3_lane_probe(struct tegra_xusb_pad *pad, struct device_node *np,
1092 			 unsigned int index)
1093 {
1094 	struct tegra_xusb_usb3_lane *usb3;
1095 	int err;
1096 
1097 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
1098 	if (!usb3)
1099 		return ERR_PTR(-ENOMEM);
1100 
1101 	INIT_LIST_HEAD(&usb3->base.list);
1102 	usb3->base.soc = &pad->soc->lanes[index];
1103 	usb3->base.index = index;
1104 	usb3->base.pad = pad;
1105 	usb3->base.np = np;
1106 
1107 	err = tegra_xusb_lane_parse_dt(&usb3->base, np);
1108 	if (err < 0) {
1109 		kfree(usb3);
1110 		return ERR_PTR(err);
1111 	}
1112 
1113 	return &usb3->base;
1114 }
1115 
tegra186_usb3_lane_remove(struct tegra_xusb_lane * lane)1116 static void tegra186_usb3_lane_remove(struct tegra_xusb_lane *lane)
1117 {
1118 	struct tegra_xusb_usb3_lane *usb3 = to_usb3_lane(lane);
1119 
1120 	kfree(usb3);
1121 }
1122 
tegra186_usb3_enable_phy_sleepwalk(struct tegra_xusb_lane * lane,enum usb_device_speed speed)1123 static int tegra186_usb3_enable_phy_sleepwalk(struct tegra_xusb_lane *lane,
1124 					      enum usb_device_speed speed)
1125 {
1126 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1127 	unsigned int index = lane->index;
1128 	u32 value;
1129 
1130 	mutex_lock(&padctl->lock);
1131 
1132 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1133 	value |= SSPX_ELPG_CLAMP_EN_EARLY(index);
1134 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1135 
1136 	usleep_range(100, 200);
1137 
1138 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1139 	value |= SSPX_ELPG_CLAMP_EN(index);
1140 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1141 
1142 	usleep_range(250, 350);
1143 
1144 	mutex_unlock(&padctl->lock);
1145 
1146 	return 0;
1147 }
1148 
tegra186_usb3_disable_phy_sleepwalk(struct tegra_xusb_lane * lane)1149 static int tegra186_usb3_disable_phy_sleepwalk(struct tegra_xusb_lane *lane)
1150 {
1151 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1152 	unsigned int index = lane->index;
1153 	u32 value;
1154 
1155 	mutex_lock(&padctl->lock);
1156 
1157 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1158 	value &= ~SSPX_ELPG_CLAMP_EN_EARLY(index);
1159 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1160 
1161 	usleep_range(100, 200);
1162 
1163 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1164 	value &= ~SSPX_ELPG_CLAMP_EN(index);
1165 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1166 
1167 	mutex_unlock(&padctl->lock);
1168 
1169 	return 0;
1170 }
1171 
tegra186_usb3_enable_phy_wake(struct tegra_xusb_lane * lane)1172 static int tegra186_usb3_enable_phy_wake(struct tegra_xusb_lane *lane)
1173 {
1174 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1175 	unsigned int index = lane->index;
1176 	u32 value;
1177 
1178 	mutex_lock(&padctl->lock);
1179 
1180 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
1181 	value &= ~ALL_WAKE_EVENTS;
1182 	value |= SS_PORT_WAKEUP_EVENT(index);
1183 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
1184 
1185 	usleep_range(10, 20);
1186 
1187 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
1188 	value &= ~ALL_WAKE_EVENTS;
1189 	value |= SS_PORT_WAKE_INTERRUPT_ENABLE(index);
1190 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
1191 
1192 	mutex_unlock(&padctl->lock);
1193 
1194 	return 0;
1195 }
1196 
tegra186_usb3_disable_phy_wake(struct tegra_xusb_lane * lane)1197 static int tegra186_usb3_disable_phy_wake(struct tegra_xusb_lane *lane)
1198 {
1199 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1200 	unsigned int index = lane->index;
1201 	u32 value;
1202 
1203 	mutex_lock(&padctl->lock);
1204 
1205 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
1206 	value &= ~ALL_WAKE_EVENTS;
1207 	value &= ~SS_PORT_WAKE_INTERRUPT_ENABLE(index);
1208 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
1209 
1210 	usleep_range(10, 20);
1211 
1212 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
1213 	value &= ~ALL_WAKE_EVENTS;
1214 	value |= SS_PORT_WAKEUP_EVENT(index);
1215 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
1216 
1217 	mutex_unlock(&padctl->lock);
1218 
1219 	return 0;
1220 }
1221 
tegra186_usb3_phy_remote_wake_detected(struct tegra_xusb_lane * lane)1222 static bool tegra186_usb3_phy_remote_wake_detected(struct tegra_xusb_lane *lane)
1223 {
1224 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1225 	unsigned int index = lane->index;
1226 	u32 value;
1227 
1228 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
1229 	if ((value & SS_PORT_WAKE_INTERRUPT_ENABLE(index)) && (value & SS_PORT_WAKEUP_EVENT(index)))
1230 		return true;
1231 
1232 	return false;
1233 }
1234 
1235 static const struct tegra_xusb_lane_ops tegra186_usb3_lane_ops = {
1236 	.probe = tegra186_usb3_lane_probe,
1237 	.remove = tegra186_usb3_lane_remove,
1238 	.enable_phy_sleepwalk = tegra186_usb3_enable_phy_sleepwalk,
1239 	.disable_phy_sleepwalk = tegra186_usb3_disable_phy_sleepwalk,
1240 	.enable_phy_wake = tegra186_usb3_enable_phy_wake,
1241 	.disable_phy_wake = tegra186_usb3_disable_phy_wake,
1242 	.remote_wake_detected = tegra186_usb3_phy_remote_wake_detected,
1243 };
1244 
tegra186_usb3_port_enable(struct tegra_xusb_port * port)1245 static int tegra186_usb3_port_enable(struct tegra_xusb_port *port)
1246 {
1247 	return 0;
1248 }
1249 
tegra186_usb3_port_disable(struct tegra_xusb_port * port)1250 static void tegra186_usb3_port_disable(struct tegra_xusb_port *port)
1251 {
1252 }
1253 
1254 static struct tegra_xusb_lane *
tegra186_usb3_port_map(struct tegra_xusb_port * port)1255 tegra186_usb3_port_map(struct tegra_xusb_port *port)
1256 {
1257 	return tegra_xusb_find_lane(port->padctl, "usb3", port->index);
1258 }
1259 
1260 static const struct tegra_xusb_port_ops tegra186_usb3_port_ops = {
1261 	.release = tegra_xusb_usb3_port_release,
1262 	.enable = tegra186_usb3_port_enable,
1263 	.disable = tegra186_usb3_port_disable,
1264 	.map = tegra186_usb3_port_map,
1265 };
1266 
tegra186_usb3_phy_power_on(struct phy * phy)1267 static int tegra186_usb3_phy_power_on(struct phy *phy)
1268 {
1269 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1270 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1271 	struct tegra_xusb_usb3_port *port;
1272 	struct tegra_xusb_usb2_port *usb2;
1273 	unsigned int index = lane->index;
1274 	struct device *dev = padctl->dev;
1275 	u32 value;
1276 
1277 	port = tegra_xusb_find_usb3_port(padctl, index);
1278 	if (!port) {
1279 		dev_err(dev, "no port found for USB3 lane %u\n", index);
1280 		return -ENODEV;
1281 	}
1282 
1283 	usb2 = tegra_xusb_find_usb2_port(padctl, port->port);
1284 	if (!usb2) {
1285 		dev_err(dev, "no companion port found for USB3 lane %u\n",
1286 			index);
1287 		return -ENODEV;
1288 	}
1289 
1290 	mutex_lock(&padctl->lock);
1291 
1292 	value = padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CAP);
1293 	value &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(index));
1294 
1295 	if (usb2->mode == USB_DR_MODE_UNKNOWN)
1296 		value |= (PORT_CAP_DISABLED << PORTX_CAP_SHIFT(index));
1297 	else if (usb2->mode == USB_DR_MODE_PERIPHERAL)
1298 		value |= (PORT_CAP_DEVICE << PORTX_CAP_SHIFT(index));
1299 	else if (usb2->mode == USB_DR_MODE_HOST)
1300 		value |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(index));
1301 	else if (usb2->mode == USB_DR_MODE_OTG)
1302 		value |= (PORT_CAP_OTG << PORTX_CAP_SHIFT(index));
1303 
1304 	padctl_writel(padctl, value, XUSB_PADCTL_SS_PORT_CAP);
1305 
1306 	if (padctl->soc->supports_gen2 && port->disable_gen2) {
1307 		value = padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CFG);
1308 		value &= ~(PORTX_SPEED_SUPPORT_MASK <<
1309 			PORTX_SPEED_SUPPORT_SHIFT(index));
1310 		value |= (PORT_SPEED_SUPPORT_GEN1 <<
1311 			PORTX_SPEED_SUPPORT_SHIFT(index));
1312 		padctl_writel(padctl, value, XUSB_PADCTL_SS_PORT_CFG);
1313 	}
1314 
1315 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1316 	value &= ~SSPX_ELPG_VCORE_DOWN(index);
1317 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1318 
1319 	usleep_range(100, 200);
1320 
1321 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1322 	value &= ~SSPX_ELPG_CLAMP_EN_EARLY(index);
1323 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1324 
1325 	usleep_range(100, 200);
1326 
1327 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1328 	value &= ~SSPX_ELPG_CLAMP_EN(index);
1329 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1330 
1331 	mutex_unlock(&padctl->lock);
1332 
1333 	return 0;
1334 }
1335 
tegra186_usb3_phy_power_off(struct phy * phy)1336 static int tegra186_usb3_phy_power_off(struct phy *phy)
1337 {
1338 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1339 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1340 	struct tegra_xusb_usb3_port *port;
1341 	unsigned int index = lane->index;
1342 	struct device *dev = padctl->dev;
1343 	u32 value;
1344 
1345 	port = tegra_xusb_find_usb3_port(padctl, index);
1346 	if (!port) {
1347 		dev_err(dev, "no port found for USB3 lane %u\n", index);
1348 		return -ENODEV;
1349 	}
1350 
1351 	mutex_lock(&padctl->lock);
1352 
1353 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1354 	value |= SSPX_ELPG_CLAMP_EN_EARLY(index);
1355 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1356 
1357 	usleep_range(100, 200);
1358 
1359 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1360 	value |= SSPX_ELPG_CLAMP_EN(index);
1361 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1362 
1363 	usleep_range(250, 350);
1364 
1365 	value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM_1);
1366 	value |= SSPX_ELPG_VCORE_DOWN(index);
1367 	padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM_1);
1368 
1369 	mutex_unlock(&padctl->lock);
1370 
1371 	return 0;
1372 }
1373 
tegra186_usb3_phy_init(struct phy * phy)1374 static int tegra186_usb3_phy_init(struct phy *phy)
1375 {
1376 	return 0;
1377 }
1378 
tegra186_usb3_phy_exit(struct phy * phy)1379 static int tegra186_usb3_phy_exit(struct phy *phy)
1380 {
1381 	return 0;
1382 }
1383 
1384 static const struct phy_ops usb3_phy_ops = {
1385 	.init = tegra186_usb3_phy_init,
1386 	.exit = tegra186_usb3_phy_exit,
1387 	.power_on = tegra186_usb3_phy_power_on,
1388 	.power_off = tegra186_usb3_phy_power_off,
1389 	.owner = THIS_MODULE,
1390 };
1391 
1392 static struct tegra_xusb_pad *
tegra186_usb3_pad_probe(struct tegra_xusb_padctl * padctl,const struct tegra_xusb_pad_soc * soc,struct device_node * np)1393 tegra186_usb3_pad_probe(struct tegra_xusb_padctl *padctl,
1394 			const struct tegra_xusb_pad_soc *soc,
1395 			struct device_node *np)
1396 {
1397 	struct tegra_xusb_usb3_pad *usb3;
1398 	struct tegra_xusb_pad *pad;
1399 	int err;
1400 
1401 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
1402 	if (!usb3)
1403 		return ERR_PTR(-ENOMEM);
1404 
1405 	pad = &usb3->base;
1406 	pad->ops = &tegra186_usb3_lane_ops;
1407 	pad->soc = soc;
1408 
1409 	err = tegra_xusb_pad_init(pad, padctl, np);
1410 	if (err < 0) {
1411 		kfree(usb3);
1412 		goto out;
1413 	}
1414 
1415 	err = tegra_xusb_pad_register(pad, &usb3_phy_ops);
1416 	if (err < 0)
1417 		goto unregister;
1418 
1419 	dev_set_drvdata(&pad->dev, pad);
1420 
1421 	return pad;
1422 
1423 unregister:
1424 	device_unregister(&pad->dev);
1425 out:
1426 	return ERR_PTR(err);
1427 }
1428 
tegra186_usb3_pad_remove(struct tegra_xusb_pad * pad)1429 static void tegra186_usb3_pad_remove(struct tegra_xusb_pad *pad)
1430 {
1431 	struct tegra_xusb_usb2_pad *usb2 = to_usb2_pad(pad);
1432 
1433 	kfree(usb2);
1434 }
1435 
1436 static const struct tegra_xusb_pad_ops tegra186_usb3_pad_ops = {
1437 	.probe = tegra186_usb3_pad_probe,
1438 	.remove = tegra186_usb3_pad_remove,
1439 };
1440 
1441 static const char * const tegra186_usb3_functions[] = {
1442 	"xusb",
1443 };
1444 
1445 static int
tegra186_xusb_read_fuse_calibration(struct tegra186_xusb_padctl * padctl)1446 tegra186_xusb_read_fuse_calibration(struct tegra186_xusb_padctl *padctl)
1447 {
1448 	struct device *dev = padctl->base.dev;
1449 	unsigned int i, count;
1450 	u32 value, *level;
1451 	int err;
1452 
1453 	count = padctl->base.soc->ports.usb2.count;
1454 
1455 	level = devm_kcalloc(dev, count, sizeof(u32), GFP_KERNEL);
1456 	if (!level)
1457 		return -ENOMEM;
1458 
1459 	err = tegra_fuse_readl(TEGRA_FUSE_SKU_CALIB_0, &value);
1460 	if (err)
1461 		return dev_err_probe(dev, err,
1462 				     "failed to read calibration fuse\n");
1463 
1464 	dev_dbg(dev, "FUSE_USB_CALIB_0 %#x\n", value);
1465 
1466 	for (i = 0; i < count; i++)
1467 		level[i] = (value >> HS_CURR_LEVEL_PADX_SHIFT(i)) &
1468 				HS_CURR_LEVEL_PAD_MASK;
1469 
1470 	padctl->calib.hs_curr_level = level;
1471 
1472 	padctl->calib.hs_squelch = (value >> HS_SQUELCH_SHIFT) &
1473 					HS_SQUELCH_MASK;
1474 	padctl->calib.hs_term_range_adj = (value >> HS_TERM_RANGE_ADJ_SHIFT) &
1475 						HS_TERM_RANGE_ADJ_MASK;
1476 
1477 	err = tegra_fuse_readl(TEGRA_FUSE_USB_CALIB_EXT_0, &value);
1478 	if (err) {
1479 		dev_err(dev, "failed to read calibration fuse: %d\n", err);
1480 		return err;
1481 	}
1482 
1483 	dev_dbg(dev, "FUSE_USB_CALIB_EXT_0 %#x\n", value);
1484 
1485 	padctl->calib.rpd_ctrl = (value >> RPD_CTRL_SHIFT) & RPD_CTRL_MASK;
1486 
1487 	return 0;
1488 }
1489 
1490 static struct tegra_xusb_padctl *
tegra186_xusb_padctl_probe(struct device * dev,const struct tegra_xusb_padctl_soc * soc)1491 tegra186_xusb_padctl_probe(struct device *dev,
1492 			   const struct tegra_xusb_padctl_soc *soc)
1493 {
1494 	struct platform_device *pdev = to_platform_device(dev);
1495 	struct tegra186_xusb_padctl *priv;
1496 	struct resource *res;
1497 	int err;
1498 
1499 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1500 	if (!priv)
1501 		return ERR_PTR(-ENOMEM);
1502 
1503 	priv->base.dev = dev;
1504 	priv->base.soc = soc;
1505 
1506 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ao");
1507 	priv->ao_regs = devm_ioremap_resource(dev, res);
1508 	if (IS_ERR(priv->ao_regs))
1509 		return ERR_CAST(priv->ao_regs);
1510 
1511 	err = tegra186_xusb_read_fuse_calibration(priv);
1512 	if (err < 0)
1513 		return ERR_PTR(err);
1514 
1515 	return &priv->base;
1516 }
1517 
tegra186_xusb_padctl_save(struct tegra_xusb_padctl * padctl)1518 static void tegra186_xusb_padctl_save(struct tegra_xusb_padctl *padctl)
1519 {
1520 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
1521 
1522 	priv->context.vbus_id = padctl_readl(padctl, USB2_VBUS_ID);
1523 	priv->context.usb2_pad_mux = padctl_readl(padctl, XUSB_PADCTL_USB2_PAD_MUX);
1524 	priv->context.usb2_port_cap = padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
1525 	priv->context.ss_port_cap = padctl_readl(padctl, XUSB_PADCTL_SS_PORT_CAP);
1526 }
1527 
tegra186_xusb_padctl_restore(struct tegra_xusb_padctl * padctl)1528 static void tegra186_xusb_padctl_restore(struct tegra_xusb_padctl *padctl)
1529 {
1530 	struct tegra186_xusb_padctl *priv = to_tegra186_xusb_padctl(padctl);
1531 
1532 	padctl_writel(padctl, priv->context.usb2_pad_mux, XUSB_PADCTL_USB2_PAD_MUX);
1533 	padctl_writel(padctl, priv->context.usb2_port_cap, XUSB_PADCTL_USB2_PORT_CAP);
1534 	padctl_writel(padctl, priv->context.ss_port_cap, XUSB_PADCTL_SS_PORT_CAP);
1535 	padctl_writel(padctl, priv->context.vbus_id, USB2_VBUS_ID);
1536 }
1537 
tegra186_xusb_padctl_suspend_noirq(struct tegra_xusb_padctl * padctl)1538 static int tegra186_xusb_padctl_suspend_noirq(struct tegra_xusb_padctl *padctl)
1539 {
1540 	tegra186_xusb_padctl_save(padctl);
1541 
1542 	return 0;
1543 }
1544 
tegra186_xusb_padctl_resume_noirq(struct tegra_xusb_padctl * padctl)1545 static int tegra186_xusb_padctl_resume_noirq(struct tegra_xusb_padctl *padctl)
1546 {
1547 	tegra186_xusb_padctl_restore(padctl);
1548 
1549 	return 0;
1550 }
1551 
tegra186_xusb_padctl_remove(struct tegra_xusb_padctl * padctl)1552 static void tegra186_xusb_padctl_remove(struct tegra_xusb_padctl *padctl)
1553 {
1554 }
1555 
1556 static const struct tegra_xusb_padctl_ops tegra186_xusb_padctl_ops = {
1557 	.probe = tegra186_xusb_padctl_probe,
1558 	.remove = tegra186_xusb_padctl_remove,
1559 	.suspend_noirq = tegra186_xusb_padctl_suspend_noirq,
1560 	.resume_noirq = tegra186_xusb_padctl_resume_noirq,
1561 	.vbus_override = tegra186_xusb_padctl_vbus_override,
1562 	.utmi_pad_power_on = tegra186_utmi_pad_power_on,
1563 	.utmi_pad_power_down = tegra186_utmi_pad_power_down,
1564 };
1565 
1566 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC)
1567 static const char * const tegra186_xusb_padctl_supply_names[] = {
1568 	"avdd-pll-erefeut",
1569 	"avdd-usb",
1570 	"vclamp-usb",
1571 	"vddio-hsic",
1572 };
1573 
1574 static const struct tegra_xusb_lane_soc tegra186_usb2_lanes[] = {
1575 	TEGRA186_LANE("usb2-0", 0,  0, 0, usb2),
1576 	TEGRA186_LANE("usb2-1", 0,  0, 0, usb2),
1577 	TEGRA186_LANE("usb2-2", 0,  0, 0, usb2),
1578 };
1579 
1580 static const struct tegra_xusb_pad_soc tegra186_usb2_pad = {
1581 	.name = "usb2",
1582 	.num_lanes = ARRAY_SIZE(tegra186_usb2_lanes),
1583 	.lanes = tegra186_usb2_lanes,
1584 	.ops = &tegra186_usb2_pad_ops,
1585 };
1586 
1587 static const struct tegra_xusb_lane_soc tegra186_usb3_lanes[] = {
1588 	TEGRA186_LANE("usb3-0", 0,  0, 0, usb3),
1589 	TEGRA186_LANE("usb3-1", 0,  0, 0, usb3),
1590 	TEGRA186_LANE("usb3-2", 0,  0, 0, usb3),
1591 };
1592 
1593 static const struct tegra_xusb_pad_soc tegra186_usb3_pad = {
1594 	.name = "usb3",
1595 	.num_lanes = ARRAY_SIZE(tegra186_usb3_lanes),
1596 	.lanes = tegra186_usb3_lanes,
1597 	.ops = &tegra186_usb3_pad_ops,
1598 };
1599 
1600 static const struct tegra_xusb_pad_soc * const tegra186_pads[] = {
1601 	&tegra186_usb2_pad,
1602 	&tegra186_usb3_pad,
1603 #if 0 /* TODO implement */
1604 	&tegra186_hsic_pad,
1605 #endif
1606 };
1607 
1608 const struct tegra_xusb_padctl_soc tegra186_xusb_padctl_soc = {
1609 	.num_pads = ARRAY_SIZE(tegra186_pads),
1610 	.pads = tegra186_pads,
1611 	.ports = {
1612 		.usb2 = {
1613 			.ops = &tegra186_usb2_port_ops,
1614 			.count = 3,
1615 		},
1616 #if 0 /* TODO implement */
1617 		.hsic = {
1618 			.ops = &tegra186_hsic_port_ops,
1619 			.count = 1,
1620 		},
1621 #endif
1622 		.usb3 = {
1623 			.ops = &tegra186_usb3_port_ops,
1624 			.count = 3,
1625 		},
1626 	},
1627 	.ops = &tegra186_xusb_padctl_ops,
1628 	.supply_names = tegra186_xusb_padctl_supply_names,
1629 	.num_supplies = ARRAY_SIZE(tegra186_xusb_padctl_supply_names),
1630 };
1631 EXPORT_SYMBOL_GPL(tegra186_xusb_padctl_soc);
1632 #endif
1633 
1634 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) || \
1635 	IS_ENABLED(CONFIG_ARCH_TEGRA_234_SOC)
1636 static const char * const tegra194_xusb_padctl_supply_names[] = {
1637 	"avdd-usb",
1638 	"vclamp-usb",
1639 };
1640 
1641 static const struct tegra_xusb_lane_soc tegra194_usb2_lanes[] = {
1642 	TEGRA186_LANE("usb2-0", 0,  0, 0, usb2),
1643 	TEGRA186_LANE("usb2-1", 0,  0, 0, usb2),
1644 	TEGRA186_LANE("usb2-2", 0,  0, 0, usb2),
1645 	TEGRA186_LANE("usb2-3", 0,  0, 0, usb2),
1646 };
1647 
1648 static const struct tegra_xusb_pad_soc tegra194_usb2_pad = {
1649 	.name = "usb2",
1650 	.num_lanes = ARRAY_SIZE(tegra194_usb2_lanes),
1651 	.lanes = tegra194_usb2_lanes,
1652 	.ops = &tegra186_usb2_pad_ops,
1653 };
1654 
1655 static const struct tegra_xusb_lane_soc tegra194_usb3_lanes[] = {
1656 	TEGRA186_LANE("usb3-0", 0,  0, 0, usb3),
1657 	TEGRA186_LANE("usb3-1", 0,  0, 0, usb3),
1658 	TEGRA186_LANE("usb3-2", 0,  0, 0, usb3),
1659 	TEGRA186_LANE("usb3-3", 0,  0, 0, usb3),
1660 };
1661 
1662 static const struct tegra_xusb_pad_soc tegra194_usb3_pad = {
1663 	.name = "usb3",
1664 	.num_lanes = ARRAY_SIZE(tegra194_usb3_lanes),
1665 	.lanes = tegra194_usb3_lanes,
1666 	.ops = &tegra186_usb3_pad_ops,
1667 };
1668 
1669 static const struct tegra_xusb_pad_soc * const tegra194_pads[] = {
1670 	&tegra194_usb2_pad,
1671 	&tegra194_usb3_pad,
1672 };
1673 
1674 const struct tegra_xusb_padctl_soc tegra194_xusb_padctl_soc = {
1675 	.num_pads = ARRAY_SIZE(tegra194_pads),
1676 	.pads = tegra194_pads,
1677 	.ports = {
1678 		.usb2 = {
1679 			.ops = &tegra186_usb2_port_ops,
1680 			.count = 4,
1681 		},
1682 		.usb3 = {
1683 			.ops = &tegra186_usb3_port_ops,
1684 			.count = 4,
1685 		},
1686 	},
1687 	.ops = &tegra186_xusb_padctl_ops,
1688 	.supply_names = tegra194_xusb_padctl_supply_names,
1689 	.num_supplies = ARRAY_SIZE(tegra194_xusb_padctl_supply_names),
1690 	.supports_gen2 = true,
1691 	.poll_trk_completed = true,
1692 };
1693 EXPORT_SYMBOL_GPL(tegra194_xusb_padctl_soc);
1694 
1695 const struct tegra_xusb_padctl_soc tegra234_xusb_padctl_soc = {
1696 	.num_pads = ARRAY_SIZE(tegra194_pads),
1697 	.pads = tegra194_pads,
1698 	.ports = {
1699 		.usb2 = {
1700 			.ops = &tegra186_usb2_port_ops,
1701 			.count = 4,
1702 		},
1703 		.usb3 = {
1704 			.ops = &tegra186_usb3_port_ops,
1705 			.count = 4,
1706 		},
1707 	},
1708 	.ops = &tegra186_xusb_padctl_ops,
1709 	.supply_names = tegra194_xusb_padctl_supply_names,
1710 	.num_supplies = ARRAY_SIZE(tegra194_xusb_padctl_supply_names),
1711 	.supports_gen2 = true,
1712 	.poll_trk_completed = true,
1713 	.trk_hw_mode = true,
1714 	.supports_lp_cfg_en = true,
1715 };
1716 EXPORT_SYMBOL_GPL(tegra234_xusb_padctl_soc);
1717 #endif
1718 
1719 MODULE_AUTHOR("JC Kuo <jckuo@nvidia.com>");
1720 MODULE_DESCRIPTION("NVIDIA Tegra186 XUSB Pad Controller driver");
1721 MODULE_LICENSE("GPL v2");
1722