Merge tag 'drm-misc-next-fixes-2020-04-04' of git://anongit.freedesktop.org/drm/drm...
[linux-block.git] / drivers / usb / phy / phy-tegra-usb.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
91525d08 2/*
91525d08 3 * Copyright (C) 2010 Google, Inc.
2d22b42d 4 * Copyright (C) 2013 NVIDIA Corporation
91525d08
BG
5 *
6 * Author:
7 * Erik Gilling <konkers@google.com>
8 * Benoit Goby <benoit@android.com>
2d22b42d 9 * Venu Byravarasu <vbyravarasu@nvidia.com>
91525d08
BG
10 */
11
91525d08 12#include <linux/delay.h>
91525d08 13#include <linux/err.h>
4265cbfd 14#include <linux/export.h>
91525d08 15#include <linux/gpio.h>
5bb69850
DO
16#include <linux/iopoll.h>
17#include <linux/module.h>
3a55c6a8 18#include <linux/of.h>
3e635202 19#include <linux/of_device.h>
aa607ebf 20#include <linux/of_gpio.h>
5bb69850
DO
21#include <linux/platform_device.h>
22#include <linux/resource.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25
26#include <linux/regulator/consumer.h>
27
91a687d8 28#include <linux/usb/ehci_def.h>
5bb69850 29#include <linux/usb/of.h>
1ba8216f 30#include <linux/usb/tegra_usb_phy.h>
5bb69850 31#include <linux/usb/ulpi.h>
91525d08 32
545592e8 33#define ULPI_VIEWPORT 0x170
91525d08 34
3e635202 35/* PORTSC PTS/PHCD bits, Tegra20 only */
545592e8
DO
36#define TEGRA_USB_PORTSC1 0x184
37#define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
38#define TEGRA_USB_PORTSC1_PHCD BIT(23)
91a687d8 39
3e635202 40/* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
545592e8
DO
41#define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
42#define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
43#define TEGRA_USB_HOSTPC1_DEVLC_PHCD BIT(22)
3e635202 44
91a687d8
SW
45/* Bits of PORTSC1, which will get cleared by writing 1 into them */
46#define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
47
545592e8
DO
48#define USB_SUSP_CTRL 0x400
49#define USB_WAKE_ON_CNNT_EN_DEV BIT(3)
50#define USB_WAKE_ON_DISCON_EN_DEV BIT(4)
51#define USB_SUSP_CLR BIT(5)
52#define USB_PHY_CLK_VALID BIT(7)
53#define UTMIP_RESET BIT(11)
54#define UHSIC_RESET BIT(11)
55#define UTMIP_PHY_ENABLE BIT(12)
56#define ULPI_PHY_ENABLE BIT(13)
57#define USB_SUSP_SET BIT(14)
58#define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
59
60#define USB1_LEGACY_CTRL 0x410
61#define USB1_NO_LEGACY_MODE BIT(0)
91525d08
BG
62#define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
63#define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
64#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
65 (1 << 1)
66#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
67#define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
68
545592e8
DO
69#define ULPI_TIMING_CTRL_0 0x424
70#define ULPI_OUTPUT_PINMUX_BYP BIT(10)
71#define ULPI_CLKOUT_PINMUX_BYP BIT(11)
91525d08 72
545592e8
DO
73#define ULPI_TIMING_CTRL_1 0x428
74#define ULPI_DATA_TRIMMER_LOAD BIT(0)
75#define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
76#define ULPI_STPDIRNXT_TRIMMER_LOAD BIT(16)
77#define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
78#define ULPI_DIR_TRIMMER_LOAD BIT(24)
79#define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
91525d08 80
545592e8 81#define UTMIP_PLL_CFG1 0x804
91525d08
BG
82#define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
83#define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
84
545592e8 85#define UTMIP_XCVR_CFG0 0x808
91525d08 86#define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
f5833a0b 87#define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
91525d08
BG
88#define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
89#define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
545592e8
DO
90#define UTMIP_FORCE_PD_POWERDOWN BIT(14)
91#define UTMIP_FORCE_PD2_POWERDOWN BIT(16)
92#define UTMIP_FORCE_PDZI_POWERDOWN BIT(18)
93#define UTMIP_XCVR_LSBIAS_SEL BIT(21)
e497a24d
TT
94#define UTMIP_XCVR_HSSLEW(x) (((x) & 0x3) << 4)
95#define UTMIP_XCVR_HSSLEW_MSB(x) ((((x) & 0x1fc) >> 2) << 25)
91525d08 96
545592e8
DO
97#define UTMIP_BIAS_CFG0 0x80c
98#define UTMIP_OTGPD BIT(11)
99#define UTMIP_BIASPD BIT(10)
100#define UTMIP_HSSQUELCH_LEVEL(x) (((x) & 0x3) << 0)
101#define UTMIP_HSDISCON_LEVEL(x) (((x) & 0x3) << 2)
102#define UTMIP_HSDISCON_LEVEL_MSB(x) ((((x) & 0x4) >> 2) << 24)
91525d08 103
545592e8
DO
104#define UTMIP_HSRX_CFG0 0x810
105#define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
106#define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
91525d08 107
545592e8
DO
108#define UTMIP_HSRX_CFG1 0x814
109#define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
91525d08 110
545592e8
DO
111#define UTMIP_TX_CFG0 0x820
112#define UTMIP_FS_PREABMLE_J BIT(19)
113#define UTMIP_HS_DISCON_DISABLE BIT(8)
91525d08 114
545592e8
DO
115#define UTMIP_MISC_CFG0 0x824
116#define UTMIP_DPDM_OBSERVE BIT(26)
117#define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
118#define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
119#define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
120#define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
121#define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
122#define UTMIP_SUSPEND_EXIT_ON_EDGE BIT(22)
91525d08 123
545592e8
DO
124#define UTMIP_MISC_CFG1 0x828
125#define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
126#define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
91525d08 127
545592e8
DO
128#define UTMIP_DEBOUNCE_CFG0 0x82c
129#define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
91525d08 130
545592e8
DO
131#define UTMIP_BAT_CHRG_CFG0 0x830
132#define UTMIP_PD_CHRG BIT(0)
91525d08 133
545592e8
DO
134#define UTMIP_SPARE_CFG0 0x834
135#define FUSE_SETUP_SEL BIT(3)
91525d08 136
545592e8
DO
137#define UTMIP_XCVR_CFG1 0x838
138#define UTMIP_FORCE_PDDISC_POWERDOWN BIT(0)
139#define UTMIP_FORCE_PDCHRP_POWERDOWN BIT(2)
140#define UTMIP_FORCE_PDDR_POWERDOWN BIT(4)
141#define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
91525d08 142
545592e8
DO
143#define UTMIP_BIAS_CFG1 0x83c
144#define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
91525d08 145
3e635202 146/* For Tegra30 and above only, the address is different in Tegra20 */
545592e8
DO
147#define USB_USBMODE 0x1f8
148#define USB_USBMODE_MASK (3 << 0)
149#define USB_USBMODE_HOST (3 << 0)
150#define USB_USBMODE_DEVICE (2 << 0)
3e635202 151
91525d08 152static DEFINE_SPINLOCK(utmip_pad_lock);
545592e8 153static unsigned int utmip_pad_count;
91525d08
BG
154
155struct tegra_xtal_freq {
545592e8 156 unsigned int freq;
91525d08
BG
157 u8 enable_delay;
158 u8 stable_count;
159 u8 active_delay;
160 u8 xtal_freq_count;
161 u16 debounce;
162};
163
164static const struct tegra_xtal_freq tegra_freq_table[] = {
165 {
166 .freq = 12000000,
167 .enable_delay = 0x02,
168 .stable_count = 0x2F,
169 .active_delay = 0x04,
170 .xtal_freq_count = 0x76,
171 .debounce = 0x7530,
172 },
173 {
174 .freq = 13000000,
175 .enable_delay = 0x02,
176 .stable_count = 0x33,
177 .active_delay = 0x05,
178 .xtal_freq_count = 0x7F,
179 .debounce = 0x7EF4,
180 },
181 {
182 .freq = 19200000,
183 .enable_delay = 0x03,
184 .stable_count = 0x4B,
185 .active_delay = 0x06,
186 .xtal_freq_count = 0xBB,
187 .debounce = 0xBB80,
188 },
189 {
190 .freq = 26000000,
191 .enable_delay = 0x04,
192 .stable_count = 0x66,
193 .active_delay = 0x09,
194 .xtal_freq_count = 0xFE,
195 .debounce = 0xFDE8,
196 },
197};
198
545592e8
DO
199static inline struct tegra_usb_phy *to_tegra_usb_phy(struct usb_phy *u_phy)
200{
201 return container_of(u_phy, struct tegra_usb_phy, u_phy);
202}
203
91a687d8
SW
204static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
205{
206 void __iomem *base = phy->regs;
01d6ea31 207 u32 val;
91a687d8 208
3e635202 209 if (phy->soc_config->has_hostpc) {
b07e5f86 210 val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
3e635202
TT
211 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
212 val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
b07e5f86 213 writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
3e635202 214 } else {
b07e5f86
DO
215 val = readl_relaxed(base + TEGRA_USB_PORTSC1);
216 val &= ~TEGRA_PORTSC1_RWC_BITS;
3e635202
TT
217 val &= ~TEGRA_USB_PORTSC1_PTS(~0);
218 val |= TEGRA_USB_PORTSC1_PTS(pts_val);
b07e5f86 219 writel_relaxed(val, base + TEGRA_USB_PORTSC1);
3e635202 220 }
91a687d8
SW
221}
222
223static void set_phcd(struct tegra_usb_phy *phy, bool enable)
224{
225 void __iomem *base = phy->regs;
01d6ea31 226 u32 val;
91a687d8 227
3e635202 228 if (phy->soc_config->has_hostpc) {
b07e5f86 229 val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
3e635202
TT
230 if (enable)
231 val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
232 else
233 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
b07e5f86 234 writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
3e635202 235 } else {
b07e5f86 236 val = readl_relaxed(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
3e635202
TT
237 if (enable)
238 val |= TEGRA_USB_PORTSC1_PHCD;
239 else
240 val &= ~TEGRA_USB_PORTSC1_PHCD;
b07e5f86 241 writel_relaxed(val, base + TEGRA_USB_PORTSC1);
3e635202 242 }
91a687d8
SW
243}
244
91525d08
BG
245static int utmip_pad_open(struct tegra_usb_phy *phy)
246{
14347036 247 int ret;
f59cd940 248
14347036
DO
249 ret = clk_prepare_enable(phy->pad_clk);
250 if (ret) {
251 dev_err(phy->u_phy.dev,
252 "Failed to enable UTMI-pads clock: %d\n", ret);
253 return ret;
254 }
255
256 spin_lock(&utmip_pad_lock);
257
258 ret = reset_control_deassert(phy->pad_rst);
259 if (ret) {
260 dev_err(phy->u_phy.dev,
261 "Failed to initialize UTMI-pads reset: %d\n", ret);
262 goto unlock;
263 }
264
265 ret = reset_control_assert(phy->pad_rst);
266 if (ret) {
267 dev_err(phy->u_phy.dev,
268 "Failed to assert UTMI-pads reset: %d\n", ret);
269 goto unlock;
270 }
271
272 udelay(1);
273
274 ret = reset_control_deassert(phy->pad_rst);
275 if (ret)
276 dev_err(phy->u_phy.dev,
277 "Failed to deassert UTMI-pads reset: %d\n", ret);
278unlock:
279 spin_unlock(&utmip_pad_lock);
280
281 clk_disable_unprepare(phy->pad_clk);
282
283 return ret;
284}
285
286static int utmip_pad_close(struct tegra_usb_phy *phy)
287{
288 int ret;
289
290 ret = clk_prepare_enable(phy->pad_clk);
291 if (ret) {
292 dev_err(phy->u_phy.dev,
293 "Failed to enable UTMI-pads clock: %d\n", ret);
294 return ret;
295 }
296
297 ret = reset_control_assert(phy->pad_rst);
298 if (ret)
299 dev_err(phy->u_phy.dev,
300 "Failed to assert UTMI-pads reset: %d\n", ret);
301
302 udelay(1);
303
304 clk_disable_unprepare(phy->pad_clk);
305
306 return ret;
91525d08
BG
307}
308
545592e8 309static int utmip_pad_power_on(struct tegra_usb_phy *phy)
91525d08 310{
e497a24d 311 struct tegra_utmip_config *config = phy->config;
545592e8 312 void __iomem *base = phy->pad_regs;
01d6ea31 313 u32 val;
545592e8 314 int err;
91525d08 315
545592e8
DO
316 err = clk_prepare_enable(phy->pad_clk);
317 if (err)
318 return err;
91525d08 319
f1f0c751 320 spin_lock(&utmip_pad_lock);
91525d08
BG
321
322 if (utmip_pad_count++ == 0) {
b07e5f86 323 val = readl_relaxed(base + UTMIP_BIAS_CFG0);
91525d08 324 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
e497a24d
TT
325
326 if (phy->soc_config->requires_extra_tuning_parameters) {
327 val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
328 UTMIP_HSDISCON_LEVEL(~0) |
329 UTMIP_HSDISCON_LEVEL_MSB(~0));
330
331 val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
332 val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
333 val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
334 }
b07e5f86 335 writel_relaxed(val, base + UTMIP_BIAS_CFG0);
91525d08
BG
336 }
337
f1f0c751 338 spin_unlock(&utmip_pad_lock);
91525d08 339
6a5278d0 340 clk_disable_unprepare(phy->pad_clk);
545592e8
DO
341
342 return 0;
91525d08
BG
343}
344
345static int utmip_pad_power_off(struct tegra_usb_phy *phy)
346{
91525d08 347 void __iomem *base = phy->pad_regs;
01d6ea31 348 u32 val;
92bd2ef2
DO
349 int ret;
350
351 ret = clk_prepare_enable(phy->pad_clk);
352 if (ret)
353 return ret;
354
f1f0c751 355 spin_lock(&utmip_pad_lock);
91525d08
BG
356
357 if (!utmip_pad_count) {
f59cd940 358 dev_err(phy->u_phy.dev, "UTMIP pad already powered off\n");
92bd2ef2
DO
359 ret = -EINVAL;
360 goto ulock;
91525d08
BG
361 }
362
91525d08 363 if (--utmip_pad_count == 0) {
b07e5f86 364 val = readl_relaxed(base + UTMIP_BIAS_CFG0);
91525d08 365 val |= UTMIP_OTGPD | UTMIP_BIASPD;
b07e5f86 366 writel_relaxed(val, base + UTMIP_BIAS_CFG0);
91525d08 367 }
92bd2ef2 368ulock:
f1f0c751 369 spin_unlock(&utmip_pad_lock);
91525d08 370
6a5278d0 371 clk_disable_unprepare(phy->pad_clk);
91525d08 372
92bd2ef2 373 return ret;
91525d08
BG
374}
375
376static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
377{
43bcf64e
DO
378 u32 tmp;
379
b07e5f86
DO
380 return readl_relaxed_poll_timeout(reg, tmp, (tmp & mask) == result,
381 2000, 6000);
91525d08
BG
382}
383
384static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
385{
91525d08 386 void __iomem *base = phy->regs;
01d6ea31 387 u32 val;
91525d08 388
203f44c4
JH
389 /*
390 * The USB driver may have already initiated the phy clock
391 * disable so wait to see if the clock turns off and if not
392 * then proceed with gating the clock.
393 */
394 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
395 return;
396
3a55c6a8 397 if (phy->is_legacy_phy) {
b07e5f86 398 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 399 val |= USB_SUSP_SET;
b07e5f86 400 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 401
545592e8 402 usleep_range(10, 100);
91525d08 403
b07e5f86 404 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 405 val &= ~USB_SUSP_SET;
b07e5f86 406 writel_relaxed(val, base + USB_SUSP_CTRL);
545592e8 407 } else {
91a687d8 408 set_phcd(phy, true);
545592e8 409 }
91525d08 410
545592e8 411 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0))
f59cd940
DO
412 dev_err(phy->u_phy.dev,
413 "Timeout waiting for PHY to stabilize on disable\n");
91525d08
BG
414}
415
416static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
417{
91525d08 418 void __iomem *base = phy->regs;
01d6ea31 419 u32 val;
91525d08 420
203f44c4
JH
421 /*
422 * The USB driver may have already initiated the phy clock
423 * enable so wait to see if the clock turns on and if not
424 * then proceed with ungating the clock.
425 */
426 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
427 USB_PHY_CLK_VALID) == 0)
428 return;
429
3a55c6a8 430 if (phy->is_legacy_phy) {
b07e5f86 431 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 432 val |= USB_SUSP_CLR;
b07e5f86 433 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 434
545592e8 435 usleep_range(10, 100);
91525d08 436
b07e5f86 437 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 438 val &= ~USB_SUSP_CLR;
b07e5f86 439 writel_relaxed(val, base + USB_SUSP_CTRL);
545592e8 440 } else {
91a687d8 441 set_phcd(phy, false);
545592e8 442 }
91525d08
BG
443
444 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
545592e8 445 USB_PHY_CLK_VALID))
f59cd940
DO
446 dev_err(phy->u_phy.dev,
447 "Timeout waiting for PHY to stabilize on enable\n");
91525d08
BG
448}
449
450static int utmi_phy_power_on(struct tegra_usb_phy *phy)
451{
91525d08 452 struct tegra_utmip_config *config = phy->config;
545592e8 453 void __iomem *base = phy->regs;
01d6ea31 454 u32 val;
545592e8 455 int err;
91525d08 456
b07e5f86 457 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 458 val |= UTMIP_RESET;
b07e5f86 459 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 460
3a55c6a8 461 if (phy->is_legacy_phy) {
b07e5f86 462 val = readl_relaxed(base + USB1_LEGACY_CTRL);
91525d08 463 val |= USB1_NO_LEGACY_MODE;
b07e5f86 464 writel_relaxed(val, base + USB1_LEGACY_CTRL);
91525d08
BG
465 }
466
b07e5f86 467 val = readl_relaxed(base + UTMIP_TX_CFG0);
f5833a0b 468 val |= UTMIP_FS_PREABMLE_J;
b07e5f86 469 writel_relaxed(val, base + UTMIP_TX_CFG0);
91525d08 470
b07e5f86 471 val = readl_relaxed(base + UTMIP_HSRX_CFG0);
91525d08
BG
472 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
473 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
474 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
b07e5f86 475 writel_relaxed(val, base + UTMIP_HSRX_CFG0);
91525d08 476
b07e5f86 477 val = readl_relaxed(base + UTMIP_HSRX_CFG1);
91525d08
BG
478 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
479 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
b07e5f86 480 writel_relaxed(val, base + UTMIP_HSRX_CFG1);
91525d08 481
b07e5f86 482 val = readl_relaxed(base + UTMIP_DEBOUNCE_CFG0);
91525d08
BG
483 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
484 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
b07e5f86 485 writel_relaxed(val, base + UTMIP_DEBOUNCE_CFG0);
91525d08 486
b07e5f86 487 val = readl_relaxed(base + UTMIP_MISC_CFG0);
91525d08 488 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
b07e5f86 489 writel_relaxed(val, base + UTMIP_MISC_CFG0);
91525d08 490
3e635202 491 if (!phy->soc_config->utmi_pll_config_in_car_module) {
b07e5f86 492 val = readl_relaxed(base + UTMIP_MISC_CFG1);
3e635202
TT
493 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
494 UTMIP_PLLU_STABLE_COUNT(~0));
495 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
496 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
b07e5f86 497 writel_relaxed(val, base + UTMIP_MISC_CFG1);
3e635202 498
b07e5f86 499 val = readl_relaxed(base + UTMIP_PLL_CFG1);
3e635202
TT
500 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
501 UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
502 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
503 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
b07e5f86 504 writel_relaxed(val, base + UTMIP_PLL_CFG1);
3e635202 505 }
91525d08 506
6558d7ed 507 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
b07e5f86 508 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 509 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
b07e5f86 510 writel_relaxed(val, base + USB_SUSP_CTRL);
f5833a0b 511
b07e5f86 512 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
f5833a0b 513 val &= ~UTMIP_PD_CHRG;
b07e5f86 514 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
f5833a0b 515 } else {
b07e5f86 516 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
f5833a0b 517 val |= UTMIP_PD_CHRG;
b07e5f86 518 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
91525d08
BG
519 }
520
545592e8
DO
521 err = utmip_pad_power_on(phy);
522 if (err)
523 return err;
91525d08 524
b07e5f86 525 val = readl_relaxed(base + UTMIP_XCVR_CFG0);
91525d08 526 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
f5833a0b
TT
527 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
528 UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
e497a24d
TT
529 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
530
531 if (!config->xcvr_setup_use_fuses) {
532 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
533 val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
534 }
91525d08
BG
535 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
536 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
e497a24d
TT
537
538 if (phy->soc_config->requires_extra_tuning_parameters) {
539 val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
540 val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
541 val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
542 }
b07e5f86 543 writel_relaxed(val, base + UTMIP_XCVR_CFG0);
91525d08 544
b07e5f86 545 val = readl_relaxed(base + UTMIP_XCVR_CFG1);
91525d08
BG
546 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
547 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
548 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
b07e5f86 549 writel_relaxed(val, base + UTMIP_XCVR_CFG1);
91525d08 550
b07e5f86 551 val = readl_relaxed(base + UTMIP_BIAS_CFG1);
91525d08
BG
552 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
553 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
b07e5f86 554 writel_relaxed(val, base + UTMIP_BIAS_CFG1);
91525d08 555
b07e5f86 556 val = readl_relaxed(base + UTMIP_SPARE_CFG0);
e497a24d
TT
557 if (config->xcvr_setup_use_fuses)
558 val |= FUSE_SETUP_SEL;
559 else
560 val &= ~FUSE_SETUP_SEL;
b07e5f86 561 writel_relaxed(val, base + UTMIP_SPARE_CFG0);
e497a24d
TT
562
563 if (!phy->is_legacy_phy) {
b07e5f86 564 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 565 val |= UTMIP_PHY_ENABLE;
b07e5f86 566 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08
BG
567 }
568
b07e5f86 569 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 570 val &= ~UTMIP_RESET;
b07e5f86 571 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 572
3a55c6a8 573 if (phy->is_legacy_phy) {
b07e5f86 574 val = readl_relaxed(base + USB1_LEGACY_CTRL);
91525d08
BG
575 val &= ~USB1_VBUS_SENSE_CTL_MASK;
576 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
b07e5f86 577 writel_relaxed(val, base + USB1_LEGACY_CTRL);
91525d08 578
b07e5f86 579 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 580 val &= ~USB_SUSP_SET;
b07e5f86 581 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08
BG
582 }
583
584 utmi_phy_clk_enable(phy);
585
3e635202 586 if (phy->soc_config->requires_usbmode_setup) {
b07e5f86 587 val = readl_relaxed(base + USB_USBMODE);
3e635202
TT
588 val &= ~USB_USBMODE_MASK;
589 if (phy->mode == USB_DR_MODE_HOST)
590 val |= USB_USBMODE_HOST;
591 else
592 val |= USB_USBMODE_DEVICE;
b07e5f86 593 writel_relaxed(val, base + USB_USBMODE);
3e635202
TT
594 }
595
bbdabdb6 596 if (!phy->is_legacy_phy)
91a687d8 597 set_pts(phy, 0);
91525d08
BG
598
599 return 0;
600}
601
1ba8216f 602static int utmi_phy_power_off(struct tegra_usb_phy *phy)
91525d08 603{
91525d08 604 void __iomem *base = phy->regs;
01d6ea31 605 u32 val;
91525d08
BG
606
607 utmi_phy_clk_disable(phy);
608
6558d7ed 609 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
b07e5f86 610 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08
BG
611 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
612 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
b07e5f86 613 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08
BG
614 }
615
b07e5f86 616 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 617 val |= UTMIP_RESET;
b07e5f86 618 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 619
b07e5f86 620 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
91525d08 621 val |= UTMIP_PD_CHRG;
b07e5f86 622 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
91525d08 623
b07e5f86 624 val = readl_relaxed(base + UTMIP_XCVR_CFG0);
91525d08
BG
625 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
626 UTMIP_FORCE_PDZI_POWERDOWN;
b07e5f86 627 writel_relaxed(val, base + UTMIP_XCVR_CFG0);
91525d08 628
b07e5f86 629 val = readl_relaxed(base + UTMIP_XCVR_CFG1);
91525d08
BG
630 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
631 UTMIP_FORCE_PDDR_POWERDOWN;
b07e5f86 632 writel_relaxed(val, base + UTMIP_XCVR_CFG1);
91525d08 633
1ba8216f 634 return utmip_pad_power_off(phy);
91525d08
BG
635}
636
637static void utmi_phy_preresume(struct tegra_usb_phy *phy)
638{
91525d08 639 void __iomem *base = phy->regs;
01d6ea31 640 u32 val;
91525d08 641
b07e5f86 642 val = readl_relaxed(base + UTMIP_TX_CFG0);
91525d08 643 val |= UTMIP_HS_DISCON_DISABLE;
b07e5f86 644 writel_relaxed(val, base + UTMIP_TX_CFG0);
91525d08
BG
645}
646
647static void utmi_phy_postresume(struct tegra_usb_phy *phy)
648{
91525d08 649 void __iomem *base = phy->regs;
01d6ea31 650 u32 val;
91525d08 651
b07e5f86 652 val = readl_relaxed(base + UTMIP_TX_CFG0);
91525d08 653 val &= ~UTMIP_HS_DISCON_DISABLE;
b07e5f86 654 writel_relaxed(val, base + UTMIP_TX_CFG0);
91525d08
BG
655}
656
657static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
658 enum tegra_usb_phy_port_speed port_speed)
659{
91525d08 660 void __iomem *base = phy->regs;
01d6ea31 661 u32 val;
91525d08 662
b07e5f86 663 val = readl_relaxed(base + UTMIP_MISC_CFG0);
91525d08
BG
664 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
665 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
666 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
667 else
668 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
b07e5f86 669 writel_relaxed(val, base + UTMIP_MISC_CFG0);
545592e8 670 usleep_range(1, 10);
91525d08 671
b07e5f86 672 val = readl_relaxed(base + UTMIP_MISC_CFG0);
91525d08 673 val |= UTMIP_DPDM_OBSERVE;
b07e5f86 674 writel_relaxed(val, base + UTMIP_MISC_CFG0);
545592e8 675 usleep_range(10, 100);
91525d08
BG
676}
677
678static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
679{
91525d08 680 void __iomem *base = phy->regs;
01d6ea31 681 u32 val;
91525d08 682
b07e5f86 683 val = readl_relaxed(base + UTMIP_MISC_CFG0);
91525d08 684 val &= ~UTMIP_DPDM_OBSERVE;
b07e5f86 685 writel_relaxed(val, base + UTMIP_MISC_CFG0);
545592e8 686 usleep_range(10, 100);
91525d08
BG
687}
688
689static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
690{
91525d08 691 void __iomem *base = phy->regs;
01d6ea31 692 u32 val;
545592e8 693 int err;
91525d08 694
06e60e50 695 gpiod_set_value_cansleep(phy->reset_gpio, 1);
545592e8
DO
696
697 err = clk_prepare_enable(phy->clk);
698 if (err)
699 return err;
700
701 usleep_range(5000, 6000);
702
06e60e50 703 gpiod_set_value_cansleep(phy->reset_gpio, 0);
91525d08 704
545592e8 705 usleep_range(1000, 2000);
91525d08 706
b07e5f86 707 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 708 val |= UHSIC_RESET;
b07e5f86 709 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08 710
b07e5f86 711 val = readl_relaxed(base + ULPI_TIMING_CTRL_0);
91525d08 712 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
b07e5f86 713 writel_relaxed(val, base + ULPI_TIMING_CTRL_0);
91525d08 714
b07e5f86 715 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 716 val |= ULPI_PHY_ENABLE;
b07e5f86 717 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08
BG
718
719 val = 0;
b07e5f86 720 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
91525d08
BG
721
722 val |= ULPI_DATA_TRIMMER_SEL(4);
723 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
724 val |= ULPI_DIR_TRIMMER_SEL(4);
b07e5f86 725 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
545592e8 726 usleep_range(10, 100);
91525d08
BG
727
728 val |= ULPI_DATA_TRIMMER_LOAD;
729 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
730 val |= ULPI_DIR_TRIMMER_LOAD;
b07e5f86 731 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
91525d08
BG
732
733 /* Fix VbusInvalid due to floating VBUS */
545592e8
DO
734 err = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
735 if (err) {
736 dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
737 goto disable_clk;
91525d08
BG
738 }
739
545592e8
DO
740 err = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
741 if (err) {
742 dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
743 goto disable_clk;
91525d08
BG
744 }
745
b07e5f86 746 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 747 val |= USB_SUSP_CLR;
b07e5f86 748 writel_relaxed(val, base + USB_SUSP_CTRL);
545592e8 749 usleep_range(100, 1000);
91525d08 750
b07e5f86 751 val = readl_relaxed(base + USB_SUSP_CTRL);
91525d08 752 val &= ~USB_SUSP_CLR;
b07e5f86 753 writel_relaxed(val, base + USB_SUSP_CTRL);
91525d08
BG
754
755 return 0;
545592e8
DO
756
757disable_clk:
758 clk_disable_unprepare(phy->clk);
759
760 return err;
91525d08
BG
761}
762
1ba8216f 763static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
91525d08 764{
06e60e50 765 gpiod_set_value_cansleep(phy->reset_gpio, 1);
28d190ac 766 usleep_range(5000, 6000);
28d190ac
DO
767 clk_disable_unprepare(phy->clk);
768
769 return 0;
1ba8216f
VB
770}
771
1ba8216f
VB
772static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
773{
18bd8bff
DO
774 int err;
775
776 if (phy->powered_on)
777 return 0;
778
3f9db1a1 779 if (phy->is_ulpi_phy)
18bd8bff 780 err = ulpi_phy_power_on(phy);
1ba8216f 781 else
18bd8bff
DO
782 err = utmi_phy_power_on(phy);
783 if (err)
784 return err;
785
786 phy->powered_on = true;
787
788 return 0;
1ba8216f
VB
789}
790
791static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
792{
18bd8bff
DO
793 int err;
794
795 if (!phy->powered_on)
796 return 0;
797
3f9db1a1 798 if (phy->is_ulpi_phy)
18bd8bff 799 err = ulpi_phy_power_off(phy);
1ba8216f 800 else
18bd8bff
DO
801 err = utmi_phy_power_off(phy);
802 if (err)
803 return err;
804
805 phy->powered_on = false;
806
807 return 0;
1ba8216f
VB
808}
809
5dcdafdd
DO
810static void tegra_usb_phy_shutdown(struct usb_phy *u_phy)
811{
545592e8 812 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
5dcdafdd
DO
813
814 if (WARN_ON(!phy->freq))
815 return;
816
817 tegra_usb_phy_power_off(phy);
818
819 if (!phy->is_ulpi_phy)
820 utmip_pad_close(phy);
821
9df3adca 822 regulator_disable(phy->vbus);
5dcdafdd
DO
823 clk_disable_unprepare(phy->pll_u);
824
825 phy->freq = NULL;
826}
827
545592e8 828static int tegra_usb_phy_set_suspend(struct usb_phy *u_phy, int suspend)
1ba8216f 829{
545592e8 830 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
5dcdafdd
DO
831
832 if (WARN_ON(!phy->freq))
833 return -EINVAL;
834
1ba8216f
VB
835 if (suspend)
836 return tegra_usb_phy_power_off(phy);
837 else
838 return tegra_usb_phy_power_on(phy);
91525d08
BG
839}
840
5dcdafdd 841static int tegra_usb_phy_init(struct usb_phy *u_phy)
2d22b42d 842{
545592e8 843 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
2d22b42d 844 unsigned long parent_rate;
545592e8 845 unsigned int i;
2d22b42d
VB
846 int err;
847
5dcdafdd
DO
848 if (WARN_ON(phy->freq))
849 return 0;
2d22b42d
VB
850
851 err = clk_prepare_enable(phy->pll_u);
852 if (err)
853 return err;
91525d08
BG
854
855 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
856 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
857 if (tegra_freq_table[i].freq == parent_rate) {
858 phy->freq = &tegra_freq_table[i];
859 break;
860 }
861 }
862 if (!phy->freq) {
f59cd940
DO
863 dev_err(phy->u_phy.dev, "Invalid pll_u parent rate %ld\n",
864 parent_rate);
91525d08 865 err = -EINVAL;
aecc5af3 866 goto disable_clk;
91525d08
BG
867 }
868
9df3adca
DO
869 err = regulator_enable(phy->vbus);
870 if (err) {
871 dev_err(phy->u_phy.dev,
872 "Failed to enable USB VBUS regulator: %d\n", err);
aecc5af3 873 goto disable_clk;
f5b8c8b6
MP
874 }
875
06e60e50 876 if (!phy->is_ulpi_phy) {
2d22b42d 877 err = utmip_pad_open(phy);
06e60e50 878 if (err)
aecc5af3 879 goto disable_vbus;
06e60e50 880 }
91525d08 881
5dcdafdd
DO
882 err = tegra_usb_phy_power_on(phy);
883 if (err)
884 goto close_phy;
885
2d22b42d 886 return 0;
91525d08 887
5dcdafdd
DO
888close_phy:
889 if (!phy->is_ulpi_phy)
890 utmip_pad_close(phy);
aecc5af3
DO
891
892disable_vbus:
893 regulator_disable(phy->vbus);
894
895disable_clk:
6a5278d0 896 clk_disable_unprepare(phy->pll_u);
5dcdafdd
DO
897
898 phy->freq = NULL;
899
2d22b42d 900 return err;
91525d08
BG
901}
902
545592e8 903void tegra_usb_phy_preresume(struct usb_phy *u_phy)
91525d08 904{
545592e8 905 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
ab137d04 906
3f9db1a1 907 if (!phy->is_ulpi_phy)
91525d08
BG
908 utmi_phy_preresume(phy);
909}
4265cbfd 910EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
91525d08 911
545592e8 912void tegra_usb_phy_postresume(struct usb_phy *u_phy)
91525d08 913{
545592e8 914 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
ab137d04 915
3f9db1a1 916 if (!phy->is_ulpi_phy)
91525d08
BG
917 utmi_phy_postresume(phy);
918}
4265cbfd 919EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
91525d08 920
545592e8
DO
921void tegra_ehci_phy_restore_start(struct usb_phy *u_phy,
922 enum tegra_usb_phy_port_speed port_speed)
91525d08 923{
545592e8 924 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
ab137d04 925
3f9db1a1 926 if (!phy->is_ulpi_phy)
91525d08
BG
927 utmi_phy_restore_start(phy, port_speed);
928}
4265cbfd 929EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
91525d08 930
545592e8 931void tegra_ehci_phy_restore_end(struct usb_phy *u_phy)
91525d08 932{
545592e8 933 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
ab137d04 934
3f9db1a1 935 if (!phy->is_ulpi_phy)
91525d08
BG
936 utmi_phy_restore_end(phy);
937}
4265cbfd 938EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
91525d08 939
81d5dfe6
MP
940static int read_utmi_param(struct platform_device *pdev, const char *param,
941 u8 *dest)
942{
943 u32 value;
545592e8
DO
944 int err;
945
946 err = of_property_read_u32(pdev->dev.of_node, param, &value);
947 if (err)
f59cd940
DO
948 dev_err(&pdev->dev,
949 "Failed to read USB UTMI parameter %s: %d\n",
81d5dfe6 950 param, err);
545592e8
DO
951 else
952 *dest = value;
953
81d5dfe6
MP
954 return err;
955}
956
957static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
958 struct platform_device *pdev)
959{
545592e8 960 struct tegra_utmip_config *config;
81d5dfe6
MP
961 struct resource *res;
962 int err;
81d5dfe6
MP
963
964 tegra_phy->is_ulpi_phy = false;
965
966 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
967 if (!res) {
f59cd940 968 dev_err(&pdev->dev, "Failed to get UTMI pad regs\n");
81d5dfe6
MP
969 return -ENXIO;
970 }
971
a4a60194
DO
972 /*
973 * Note that UTMI pad registers are shared by all PHYs, therefore
974 * devm_platform_ioremap_resource() can't be used here.
975 */
81d5dfe6 976 tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
545592e8 977 resource_size(res));
851dd02b 978 if (!tegra_phy->pad_regs) {
f59cd940 979 dev_err(&pdev->dev, "Failed to remap UTMI pad regs\n");
81d5dfe6
MP
980 return -ENOMEM;
981 }
982
9ce9ec95
TR
983 tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
984 GFP_KERNEL);
01ad32d5 985 if (!tegra_phy->config)
81d5dfe6 986 return -ENOMEM;
81d5dfe6
MP
987
988 config = tegra_phy->config;
989
990 err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
545592e8
DO
991 &config->hssync_start_delay);
992 if (err)
81d5dfe6
MP
993 return err;
994
995 err = read_utmi_param(pdev, "nvidia,elastic-limit",
545592e8
DO
996 &config->elastic_limit);
997 if (err)
81d5dfe6
MP
998 return err;
999
1000 err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
545592e8
DO
1001 &config->idle_wait_delay);
1002 if (err)
81d5dfe6
MP
1003 return err;
1004
1005 err = read_utmi_param(pdev, "nvidia,term-range-adj",
545592e8
DO
1006 &config->term_range_adj);
1007 if (err)
81d5dfe6
MP
1008 return err;
1009
81d5dfe6 1010 err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
545592e8
DO
1011 &config->xcvr_lsfslew);
1012 if (err)
81d5dfe6
MP
1013 return err;
1014
1015 err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
545592e8
DO
1016 &config->xcvr_lsrslew);
1017 if (err)
81d5dfe6
MP
1018 return err;
1019
e497a24d
TT
1020 if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
1021 err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
545592e8
DO
1022 &config->xcvr_hsslew);
1023 if (err)
e497a24d
TT
1024 return err;
1025
1026 err = read_utmi_param(pdev, "nvidia,hssquelch-level",
545592e8
DO
1027 &config->hssquelch_level);
1028 if (err)
e497a24d
TT
1029 return err;
1030
1031 err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
545592e8
DO
1032 &config->hsdiscon_level);
1033 if (err)
e497a24d
TT
1034 return err;
1035 }
1036
1037 config->xcvr_setup_use_fuses = of_property_read_bool(
1038 pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
1039
1040 if (!config->xcvr_setup_use_fuses) {
1041 err = read_utmi_param(pdev, "nvidia,xcvr-setup",
545592e8
DO
1042 &config->xcvr_setup);
1043 if (err)
e497a24d
TT
1044 return err;
1045 }
1046
81d5dfe6
MP
1047 return 0;
1048}
1049
3e635202
TT
1050static const struct tegra_phy_soc_config tegra20_soc_config = {
1051 .utmi_pll_config_in_car_module = false,
1052 .has_hostpc = false,
1053 .requires_usbmode_setup = false,
1054 .requires_extra_tuning_parameters = false,
1055};
1056
1057static const struct tegra_phy_soc_config tegra30_soc_config = {
1058 .utmi_pll_config_in_car_module = true,
1059 .has_hostpc = true,
1060 .requires_usbmode_setup = true,
1061 .requires_extra_tuning_parameters = true,
1062};
1063
0f0520ba 1064static const struct of_device_id tegra_usb_phy_id_table[] = {
3e635202
TT
1065 { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
1066 { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
1067 { },
1068};
1069MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
1070
2d22b42d
VB
1071static int tegra_usb_phy_probe(struct platform_device *pdev)
1072{
2d22b42d 1073 struct device_node *np = pdev->dev.of_node;
545592e8 1074 struct tegra_usb_phy *tegra_phy;
9fdb07f7 1075 enum usb_phy_interface phy_type;
545592e8 1076 struct reset_control *reset;
06e60e50 1077 struct gpio_desc *gpiod;
545592e8 1078 struct resource *res;
87541747 1079 struct usb_phy *phy;
2d22b42d
VB
1080 int err;
1081
1082 tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
01ad32d5 1083 if (!tegra_phy)
2d22b42d 1084 return -ENOMEM;
2d22b42d 1085
545592e8 1086 tegra_phy->soc_config = of_device_get_match_data(&pdev->dev);
3e635202 1087
2d22b42d
VB
1088 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1089 if (!res) {
1090 dev_err(&pdev->dev, "Failed to get I/O memory\n");
1091 return -ENXIO;
1092 }
1093
a4a60194
DO
1094 /*
1095 * Note that PHY and USB controller are using shared registers,
1096 * therefore devm_platform_ioremap_resource() can't be used here.
1097 */
2d22b42d 1098 tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
545592e8 1099 resource_size(res));
2d22b42d
VB
1100 if (!tegra_phy->regs) {
1101 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
1102 return -ENOMEM;
1103 }
1104
1105 tegra_phy->is_legacy_phy =
1106 of_property_read_bool(np, "nvidia,has-legacy-mode");
1107
6558d7ed 1108 if (of_find_property(np, "dr_mode", NULL))
06e7114f 1109 tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
6558d7ed
TT
1110 else
1111 tegra_phy->mode = USB_DR_MODE_HOST;
1112
1113 if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
1114 dev_err(&pdev->dev, "dr_mode is invalid\n");
1115 return -EINVAL;
1116 }
2d22b42d 1117
f5b8c8b6 1118 /* On some boards, the VBUS regulator doesn't need to be controlled */
9df3adca
DO
1119 tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
1120 if (IS_ERR(tegra_phy->vbus))
1121 return PTR_ERR(tegra_phy->vbus);
f5b8c8b6 1122
5dcdafdd
DO
1123 tegra_phy->pll_u = devm_clk_get(&pdev->dev, "pll_u");
1124 err = PTR_ERR_OR_ZERO(tegra_phy->pll_u);
1125 if (err) {
1126 dev_err(&pdev->dev, "Failed to get pll_u clock: %d\n", err);
2d22b42d 1127 return err;
5dcdafdd
DO
1128 }
1129
545592e8
DO
1130 phy_type = of_usb_get_phy_mode(np);
1131 switch (phy_type) {
1132 case USBPHY_INTERFACE_MODE_UTMI:
1133 err = utmi_phy_probe(tegra_phy, pdev);
1134 if (err)
1135 return err;
1136
1137 tegra_phy->pad_clk = devm_clk_get(&pdev->dev, "utmi-pads");
1138 err = PTR_ERR_OR_ZERO(tegra_phy->pad_clk);
1139 if (err) {
1140 dev_err(&pdev->dev,
1141 "Failed to get UTMIP pad clock: %d\n", err);
1142 return err;
1143 }
1144
1145 reset = devm_reset_control_get_optional_shared(&pdev->dev,
1146 "utmi-pads");
1147 err = PTR_ERR_OR_ZERO(reset);
1148 if (err) {
1149 dev_err(&pdev->dev,
1150 "Failed to get UTMI-pads reset: %d\n", err);
1151 return err;
1152 }
1153 tegra_phy->pad_rst = reset;
1154 break;
1155
1156 case USBPHY_INTERFACE_MODE_ULPI:
1157 tegra_phy->is_ulpi_phy = true;
1158
5dcdafdd
DO
1159 tegra_phy->clk = devm_clk_get(&pdev->dev, "ulpi-link");
1160 err = PTR_ERR_OR_ZERO(tegra_phy->clk);
1161 if (err) {
1162 dev_err(&pdev->dev,
1163 "Failed to get ULPI clock: %d\n", err);
1164 return err;
1165 }
2d22b42d 1166
06e60e50
DO
1167 gpiod = devm_gpiod_get_from_of_node(&pdev->dev, np,
1168 "nvidia,phy-reset-gpio",
1169 0, GPIOD_OUT_HIGH,
1170 "ulpi_phy_reset_b");
1171 err = PTR_ERR_OR_ZERO(gpiod);
545592e8 1172 if (err) {
06e60e50
DO
1173 dev_err(&pdev->dev,
1174 "Request failed for reset GPIO: %d\n", err);
5dcdafdd
DO
1175 return err;
1176 }
06e60e50 1177 tegra_phy->reset_gpio = gpiod;
5dcdafdd 1178
87541747
DO
1179 phy = devm_otg_ulpi_create(&pdev->dev,
1180 &ulpi_viewport_access_ops, 0);
1181 if (!phy) {
5dcdafdd 1182 dev_err(&pdev->dev, "Failed to create ULPI OTG\n");
545592e8 1183 return -ENOMEM;
5dcdafdd
DO
1184 }
1185
87541747 1186 tegra_phy->ulpi = phy;
5dcdafdd 1187 tegra_phy->ulpi->io_priv = tegra_phy->regs + ULPI_VIEWPORT;
545592e8 1188 break;
5dcdafdd 1189
545592e8
DO
1190 default:
1191 dev_err(&pdev->dev, "phy_type %u is invalid or unsupported\n",
1192 phy_type);
1193 return -EINVAL;
5dcdafdd
DO
1194 }
1195
1196 tegra_phy->u_phy.dev = &pdev->dev;
1197 tegra_phy->u_phy.init = tegra_usb_phy_init;
1198 tegra_phy->u_phy.shutdown = tegra_usb_phy_shutdown;
1199 tegra_phy->u_phy.set_suspend = tegra_usb_phy_set_suspend;
2d22b42d 1200
72031b52 1201 platform_set_drvdata(pdev, tegra_phy);
0ee5b4ab
TT
1202
1203 err = usb_add_phy_dev(&tegra_phy->u_phy);
545592e8 1204 if (err)
87541747 1205 return err;
0ee5b4ab
TT
1206
1207 return 0;
1208}
1209
1210static int tegra_usb_phy_remove(struct platform_device *pdev)
1211{
1212 struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
1213
1214 usb_remove_phy(&tegra_phy->u_phy);
5dcdafdd 1215
2d22b42d
VB
1216 return 0;
1217}
1218
2d22b42d
VB
1219static struct platform_driver tegra_usb_phy_driver = {
1220 .probe = tegra_usb_phy_probe,
0ee5b4ab 1221 .remove = tegra_usb_phy_remove,
2d22b42d
VB
1222 .driver = {
1223 .name = "tegra-phy",
78723920 1224 .of_match_table = tegra_usb_phy_id_table,
2d22b42d
VB
1225 },
1226};
1227module_platform_driver(tegra_usb_phy_driver);
1228
587376a1
SW
1229MODULE_DESCRIPTION("Tegra USB PHY driver");
1230MODULE_LICENSE("GPL v2");