1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Google, Inc.
4 * Copyright (C) 2013 NVIDIA Corporation
12 #include <linux/resource.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/export.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/iopoll.h>
20 #include <linux/gpio.h>
22 #include <linux/of_device.h>
23 #include <linux/of_gpio.h>
24 #include <linux/usb/otg.h>
25 #include <linux/usb/ulpi.h>
26 #include <linux/usb/of.h>
27 #include <linux/usb/ehci_def.h>
28 #include <linux/usb/tegra_usb_phy.h>
29 #include <linux/regulator/consumer.h>
31 #define ULPI_VIEWPORT 0x170
33 /* PORTSC PTS/PHCD bits, Tegra20 only */
34 #define TEGRA_USB_PORTSC1 0x184
35 #define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
36 #define TEGRA_USB_PORTSC1_PHCD (1 << 23)
38 /* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
39 #define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
40 #define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
41 #define TEGRA_USB_HOSTPC1_DEVLC_PHCD (1 << 22)
43 /* Bits of PORTSC1, which will get cleared by writing 1 into them */
44 #define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
46 #define USB_SUSP_CTRL 0x400
47 #define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
48 #define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
49 #define USB_SUSP_CLR (1 << 5)
50 #define USB_PHY_CLK_VALID (1 << 7)
51 #define UTMIP_RESET (1 << 11)
52 #define UHSIC_RESET (1 << 11)
53 #define UTMIP_PHY_ENABLE (1 << 12)
54 #define ULPI_PHY_ENABLE (1 << 13)
55 #define USB_SUSP_SET (1 << 14)
56 #define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
58 #define USB1_LEGACY_CTRL 0x410
59 #define USB1_NO_LEGACY_MODE (1 << 0)
60 #define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
61 #define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
62 #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
64 #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
65 #define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
67 #define ULPI_TIMING_CTRL_0 0x424
68 #define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
69 #define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
71 #define ULPI_TIMING_CTRL_1 0x428
72 #define ULPI_DATA_TRIMMER_LOAD (1 << 0)
73 #define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
74 #define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
75 #define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
76 #define ULPI_DIR_TRIMMER_LOAD (1 << 24)
77 #define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
79 #define UTMIP_PLL_CFG1 0x804
80 #define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
81 #define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
83 #define UTMIP_XCVR_CFG0 0x808
84 #define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
85 #define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
86 #define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
87 #define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
88 #define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
89 #define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
90 #define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
91 #define UTMIP_XCVR_LSBIAS_SEL (1 << 21)
92 #define UTMIP_XCVR_HSSLEW(x) (((x) & 0x3) << 4)
93 #define UTMIP_XCVR_HSSLEW_MSB(x) ((((x) & 0x1fc) >> 2) << 25)
95 #define UTMIP_BIAS_CFG0 0x80c
96 #define UTMIP_OTGPD (1 << 11)
97 #define UTMIP_BIASPD (1 << 10)
98 #define UTMIP_HSSQUELCH_LEVEL(x) (((x) & 0x3) << 0)
99 #define UTMIP_HSDISCON_LEVEL(x) (((x) & 0x3) << 2)
100 #define UTMIP_HSDISCON_LEVEL_MSB(x) ((((x) & 0x4) >> 2) << 24)
102 #define UTMIP_HSRX_CFG0 0x810
103 #define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
104 #define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
106 #define UTMIP_HSRX_CFG1 0x814
107 #define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
109 #define UTMIP_TX_CFG0 0x820
110 #define UTMIP_FS_PREABMLE_J (1 << 19)
111 #define UTMIP_HS_DISCON_DISABLE (1 << 8)
113 #define UTMIP_MISC_CFG0 0x824
114 #define UTMIP_DPDM_OBSERVE (1 << 26)
115 #define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
116 #define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
117 #define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
118 #define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
119 #define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
120 #define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
122 #define UTMIP_MISC_CFG1 0x828
123 #define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
124 #define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
126 #define UTMIP_DEBOUNCE_CFG0 0x82c
127 #define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
129 #define UTMIP_BAT_CHRG_CFG0 0x830
130 #define UTMIP_PD_CHRG (1 << 0)
132 #define UTMIP_SPARE_CFG0 0x834
133 #define FUSE_SETUP_SEL (1 << 3)
135 #define UTMIP_XCVR_CFG1 0x838
136 #define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
137 #define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
138 #define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
139 #define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
141 #define UTMIP_BIAS_CFG1 0x83c
142 #define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
144 /* For Tegra30 and above only, the address is different in Tegra20 */
145 #define USB_USBMODE 0x1f8
146 #define USB_USBMODE_MASK (3 << 0)
147 #define USB_USBMODE_HOST (3 << 0)
148 #define USB_USBMODE_DEVICE (2 << 0)
150 static DEFINE_SPINLOCK(utmip_pad_lock);
151 static int utmip_pad_count;
153 struct tegra_xtal_freq {
162 static const struct tegra_xtal_freq tegra_freq_table[] = {
165 .enable_delay = 0x02,
166 .stable_count = 0x2F,
167 .active_delay = 0x04,
168 .xtal_freq_count = 0x76,
173 .enable_delay = 0x02,
174 .stable_count = 0x33,
175 .active_delay = 0x05,
176 .xtal_freq_count = 0x7F,
181 .enable_delay = 0x03,
182 .stable_count = 0x4B,
183 .active_delay = 0x06,
184 .xtal_freq_count = 0xBB,
189 .enable_delay = 0x04,
190 .stable_count = 0x66,
191 .active_delay = 0x09,
192 .xtal_freq_count = 0xFE,
197 static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
199 void __iomem *base = phy->regs;
202 if (phy->soc_config->has_hostpc) {
203 val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
204 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
205 val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
206 writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
208 val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
209 val &= ~TEGRA_USB_PORTSC1_PTS(~0);
210 val |= TEGRA_USB_PORTSC1_PTS(pts_val);
211 writel(val, base + TEGRA_USB_PORTSC1);
215 static void set_phcd(struct tegra_usb_phy *phy, bool enable)
217 void __iomem *base = phy->regs;
220 if (phy->soc_config->has_hostpc) {
221 val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
223 val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
225 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
226 writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
228 val = readl(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
230 val |= TEGRA_USB_PORTSC1_PHCD;
232 val &= ~TEGRA_USB_PORTSC1_PHCD;
233 writel(val, base + TEGRA_USB_PORTSC1);
237 static int utmip_pad_open(struct tegra_usb_phy *phy)
239 phy->pad_clk = devm_clk_get(phy->u_phy.dev, "utmi-pads");
240 if (IS_ERR(phy->pad_clk)) {
241 pr_err("%s: can't get utmip pad clock\n", __func__);
242 return PTR_ERR(phy->pad_clk);
248 static void utmip_pad_power_on(struct tegra_usb_phy *phy)
250 unsigned long val, flags;
251 void __iomem *base = phy->pad_regs;
252 struct tegra_utmip_config *config = phy->config;
254 clk_prepare_enable(phy->pad_clk);
256 spin_lock_irqsave(&utmip_pad_lock, flags);
258 if (utmip_pad_count++ == 0) {
259 val = readl(base + UTMIP_BIAS_CFG0);
260 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
262 if (phy->soc_config->requires_extra_tuning_parameters) {
263 val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
264 UTMIP_HSDISCON_LEVEL(~0) |
265 UTMIP_HSDISCON_LEVEL_MSB(~0));
267 val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
268 val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
269 val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
271 writel(val, base + UTMIP_BIAS_CFG0);
274 spin_unlock_irqrestore(&utmip_pad_lock, flags);
276 clk_disable_unprepare(phy->pad_clk);
279 static int utmip_pad_power_off(struct tegra_usb_phy *phy)
281 unsigned long val, flags;
282 void __iomem *base = phy->pad_regs;
284 if (!utmip_pad_count) {
285 pr_err("%s: utmip pad already powered off\n", __func__);
289 clk_prepare_enable(phy->pad_clk);
291 spin_lock_irqsave(&utmip_pad_lock, flags);
293 if (--utmip_pad_count == 0) {
294 val = readl(base + UTMIP_BIAS_CFG0);
295 val |= UTMIP_OTGPD | UTMIP_BIASPD;
296 writel(val, base + UTMIP_BIAS_CFG0);
299 spin_unlock_irqrestore(&utmip_pad_lock, flags);
301 clk_disable_unprepare(phy->pad_clk);
306 static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
310 return readl_poll_timeout(reg, tmp, (tmp & mask) == result,
314 static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
317 void __iomem *base = phy->regs;
320 * The USB driver may have already initiated the phy clock
321 * disable so wait to see if the clock turns off and if not
322 * then proceed with gating the clock.
324 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
327 if (phy->is_legacy_phy) {
328 val = readl(base + USB_SUSP_CTRL);
330 writel(val, base + USB_SUSP_CTRL);
334 val = readl(base + USB_SUSP_CTRL);
335 val &= ~USB_SUSP_SET;
336 writel(val, base + USB_SUSP_CTRL);
340 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
341 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
344 static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
347 void __iomem *base = phy->regs;
350 * The USB driver may have already initiated the phy clock
351 * enable so wait to see if the clock turns on and if not
352 * then proceed with ungating the clock.
354 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
355 USB_PHY_CLK_VALID) == 0)
358 if (phy->is_legacy_phy) {
359 val = readl(base + USB_SUSP_CTRL);
361 writel(val, base + USB_SUSP_CTRL);
365 val = readl(base + USB_SUSP_CTRL);
366 val &= ~USB_SUSP_CLR;
367 writel(val, base + USB_SUSP_CTRL);
369 set_phcd(phy, false);
371 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
373 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
376 static int utmi_phy_power_on(struct tegra_usb_phy *phy)
379 void __iomem *base = phy->regs;
380 struct tegra_utmip_config *config = phy->config;
382 val = readl(base + USB_SUSP_CTRL);
384 writel(val, base + USB_SUSP_CTRL);
386 if (phy->is_legacy_phy) {
387 val = readl(base + USB1_LEGACY_CTRL);
388 val |= USB1_NO_LEGACY_MODE;
389 writel(val, base + USB1_LEGACY_CTRL);
392 val = readl(base + UTMIP_TX_CFG0);
393 val |= UTMIP_FS_PREABMLE_J;
394 writel(val, base + UTMIP_TX_CFG0);
396 val = readl(base + UTMIP_HSRX_CFG0);
397 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
398 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
399 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
400 writel(val, base + UTMIP_HSRX_CFG0);
402 val = readl(base + UTMIP_HSRX_CFG1);
403 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
404 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
405 writel(val, base + UTMIP_HSRX_CFG1);
407 val = readl(base + UTMIP_DEBOUNCE_CFG0);
408 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
409 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
410 writel(val, base + UTMIP_DEBOUNCE_CFG0);
412 val = readl(base + UTMIP_MISC_CFG0);
413 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
414 writel(val, base + UTMIP_MISC_CFG0);
416 if (!phy->soc_config->utmi_pll_config_in_car_module) {
417 val = readl(base + UTMIP_MISC_CFG1);
418 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
419 UTMIP_PLLU_STABLE_COUNT(~0));
420 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
421 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
422 writel(val, base + UTMIP_MISC_CFG1);
424 val = readl(base + UTMIP_PLL_CFG1);
425 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
426 UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
427 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
428 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
429 writel(val, base + UTMIP_PLL_CFG1);
432 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
433 val = readl(base + USB_SUSP_CTRL);
434 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
435 writel(val, base + USB_SUSP_CTRL);
437 val = readl(base + UTMIP_BAT_CHRG_CFG0);
438 val &= ~UTMIP_PD_CHRG;
439 writel(val, base + UTMIP_BAT_CHRG_CFG0);
441 val = readl(base + UTMIP_BAT_CHRG_CFG0);
442 val |= UTMIP_PD_CHRG;
443 writel(val, base + UTMIP_BAT_CHRG_CFG0);
446 utmip_pad_power_on(phy);
448 val = readl(base + UTMIP_XCVR_CFG0);
449 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
450 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
451 UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
452 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
454 if (!config->xcvr_setup_use_fuses) {
455 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
456 val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
458 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
459 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
461 if (phy->soc_config->requires_extra_tuning_parameters) {
462 val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
463 val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
464 val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
466 writel(val, base + UTMIP_XCVR_CFG0);
468 val = readl(base + UTMIP_XCVR_CFG1);
469 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
470 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
471 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
472 writel(val, base + UTMIP_XCVR_CFG1);
474 val = readl(base + UTMIP_BIAS_CFG1);
475 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
476 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
477 writel(val, base + UTMIP_BIAS_CFG1);
479 val = readl(base + UTMIP_SPARE_CFG0);
480 if (config->xcvr_setup_use_fuses)
481 val |= FUSE_SETUP_SEL;
483 val &= ~FUSE_SETUP_SEL;
484 writel(val, base + UTMIP_SPARE_CFG0);
486 if (!phy->is_legacy_phy) {
487 val = readl(base + USB_SUSP_CTRL);
488 val |= UTMIP_PHY_ENABLE;
489 writel(val, base + USB_SUSP_CTRL);
492 val = readl(base + USB_SUSP_CTRL);
494 writel(val, base + USB_SUSP_CTRL);
496 if (phy->is_legacy_phy) {
497 val = readl(base + USB1_LEGACY_CTRL);
498 val &= ~USB1_VBUS_SENSE_CTL_MASK;
499 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
500 writel(val, base + USB1_LEGACY_CTRL);
502 val = readl(base + USB_SUSP_CTRL);
503 val &= ~USB_SUSP_SET;
504 writel(val, base + USB_SUSP_CTRL);
507 utmi_phy_clk_enable(phy);
509 if (phy->soc_config->requires_usbmode_setup) {
510 val = readl(base + USB_USBMODE);
511 val &= ~USB_USBMODE_MASK;
512 if (phy->mode == USB_DR_MODE_HOST)
513 val |= USB_USBMODE_HOST;
515 val |= USB_USBMODE_DEVICE;
516 writel(val, base + USB_USBMODE);
519 if (!phy->is_legacy_phy)
525 static int utmi_phy_power_off(struct tegra_usb_phy *phy)
528 void __iomem *base = phy->regs;
530 utmi_phy_clk_disable(phy);
532 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
533 val = readl(base + USB_SUSP_CTRL);
534 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
535 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
536 writel(val, base + USB_SUSP_CTRL);
539 val = readl(base + USB_SUSP_CTRL);
541 writel(val, base + USB_SUSP_CTRL);
543 val = readl(base + UTMIP_BAT_CHRG_CFG0);
544 val |= UTMIP_PD_CHRG;
545 writel(val, base + UTMIP_BAT_CHRG_CFG0);
547 val = readl(base + UTMIP_XCVR_CFG0);
548 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
549 UTMIP_FORCE_PDZI_POWERDOWN;
550 writel(val, base + UTMIP_XCVR_CFG0);
552 val = readl(base + UTMIP_XCVR_CFG1);
553 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
554 UTMIP_FORCE_PDDR_POWERDOWN;
555 writel(val, base + UTMIP_XCVR_CFG1);
557 return utmip_pad_power_off(phy);
560 static void utmi_phy_preresume(struct tegra_usb_phy *phy)
563 void __iomem *base = phy->regs;
565 val = readl(base + UTMIP_TX_CFG0);
566 val |= UTMIP_HS_DISCON_DISABLE;
567 writel(val, base + UTMIP_TX_CFG0);
570 static void utmi_phy_postresume(struct tegra_usb_phy *phy)
573 void __iomem *base = phy->regs;
575 val = readl(base + UTMIP_TX_CFG0);
576 val &= ~UTMIP_HS_DISCON_DISABLE;
577 writel(val, base + UTMIP_TX_CFG0);
580 static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
581 enum tegra_usb_phy_port_speed port_speed)
584 void __iomem *base = phy->regs;
586 val = readl(base + UTMIP_MISC_CFG0);
587 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
588 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
589 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
591 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
592 writel(val, base + UTMIP_MISC_CFG0);
595 val = readl(base + UTMIP_MISC_CFG0);
596 val |= UTMIP_DPDM_OBSERVE;
597 writel(val, base + UTMIP_MISC_CFG0);
601 static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
604 void __iomem *base = phy->regs;
606 val = readl(base + UTMIP_MISC_CFG0);
607 val &= ~UTMIP_DPDM_OBSERVE;
608 writel(val, base + UTMIP_MISC_CFG0);
612 static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
616 void __iomem *base = phy->regs;
618 ret = gpio_direction_output(phy->reset_gpio, 0);
620 dev_err(phy->u_phy.dev, "gpio %d not set to 0\n",
625 ret = gpio_direction_output(phy->reset_gpio, 1);
627 dev_err(phy->u_phy.dev, "gpio %d not set to 1\n",
632 clk_prepare_enable(phy->clk);
635 val = readl(base + USB_SUSP_CTRL);
637 writel(val, base + USB_SUSP_CTRL);
639 val = readl(base + ULPI_TIMING_CTRL_0);
640 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
641 writel(val, base + ULPI_TIMING_CTRL_0);
643 val = readl(base + USB_SUSP_CTRL);
644 val |= ULPI_PHY_ENABLE;
645 writel(val, base + USB_SUSP_CTRL);
648 writel(val, base + ULPI_TIMING_CTRL_1);
650 val |= ULPI_DATA_TRIMMER_SEL(4);
651 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
652 val |= ULPI_DIR_TRIMMER_SEL(4);
653 writel(val, base + ULPI_TIMING_CTRL_1);
656 val |= ULPI_DATA_TRIMMER_LOAD;
657 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
658 val |= ULPI_DIR_TRIMMER_LOAD;
659 writel(val, base + ULPI_TIMING_CTRL_1);
661 /* Fix VbusInvalid due to floating VBUS */
662 ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
664 pr_err("%s: ulpi write failed\n", __func__);
668 ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
670 pr_err("%s: ulpi write failed\n", __func__);
674 val = readl(base + USB_SUSP_CTRL);
676 writel(val, base + USB_SUSP_CTRL);
679 val = readl(base + USB_SUSP_CTRL);
680 val &= ~USB_SUSP_CLR;
681 writel(val, base + USB_SUSP_CTRL);
686 static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
688 clk_disable(phy->clk);
689 return gpio_direction_output(phy->reset_gpio, 0);
692 static void tegra_usb_phy_close(struct tegra_usb_phy *phy)
694 if (!IS_ERR(phy->vbus))
695 regulator_disable(phy->vbus);
697 clk_disable_unprepare(phy->pll_u);
700 static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
702 if (phy->is_ulpi_phy)
703 return ulpi_phy_power_on(phy);
705 return utmi_phy_power_on(phy);
708 static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
710 if (phy->is_ulpi_phy)
711 return ulpi_phy_power_off(phy);
713 return utmi_phy_power_off(phy);
716 static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
718 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
720 return tegra_usb_phy_power_off(phy);
722 return tegra_usb_phy_power_on(phy);
725 static int ulpi_open(struct tegra_usb_phy *phy)
729 phy->clk = devm_clk_get(phy->u_phy.dev, "ulpi-link");
730 if (IS_ERR(phy->clk)) {
731 pr_err("%s: can't get ulpi clock\n", __func__);
732 return PTR_ERR(phy->clk);
735 err = devm_gpio_request(phy->u_phy.dev, phy->reset_gpio,
738 dev_err(phy->u_phy.dev, "request failed for gpio: %d\n",
743 err = gpio_direction_output(phy->reset_gpio, 0);
745 dev_err(phy->u_phy.dev, "gpio %d direction not set to output\n",
750 phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
752 dev_err(phy->u_phy.dev, "otg_ulpi_create returned NULL\n");
757 phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
761 static int tegra_usb_phy_init(struct tegra_usb_phy *phy)
763 unsigned long parent_rate;
767 phy->pll_u = devm_clk_get(phy->u_phy.dev, "pll_u");
768 if (IS_ERR(phy->pll_u)) {
769 pr_err("Can't get pll_u clock\n");
770 return PTR_ERR(phy->pll_u);
773 err = clk_prepare_enable(phy->pll_u);
777 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
778 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
779 if (tegra_freq_table[i].freq == parent_rate) {
780 phy->freq = &tegra_freq_table[i];
785 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
790 if (!IS_ERR(phy->vbus)) {
791 err = regulator_enable(phy->vbus);
793 dev_err(phy->u_phy.dev,
794 "failed to enable usb vbus regulator: %d\n",
800 if (phy->is_ulpi_phy)
801 err = ulpi_open(phy);
803 err = utmip_pad_open(phy);
810 clk_disable_unprepare(phy->pll_u);
814 void tegra_usb_phy_preresume(struct usb_phy *x)
816 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
818 if (!phy->is_ulpi_phy)
819 utmi_phy_preresume(phy);
821 EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
823 void tegra_usb_phy_postresume(struct usb_phy *x)
825 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
827 if (!phy->is_ulpi_phy)
828 utmi_phy_postresume(phy);
830 EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
832 void tegra_ehci_phy_restore_start(struct usb_phy *x,
833 enum tegra_usb_phy_port_speed port_speed)
835 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
837 if (!phy->is_ulpi_phy)
838 utmi_phy_restore_start(phy, port_speed);
840 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
842 void tegra_ehci_phy_restore_end(struct usb_phy *x)
844 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
846 if (!phy->is_ulpi_phy)
847 utmi_phy_restore_end(phy);
849 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
851 static int read_utmi_param(struct platform_device *pdev, const char *param,
855 int err = of_property_read_u32(pdev->dev.of_node, param, &value);
858 dev_err(&pdev->dev, "Failed to read USB UTMI parameter %s: %d\n",
863 static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
864 struct platform_device *pdev)
866 struct resource *res;
868 struct tegra_utmip_config *config;
870 tegra_phy->is_ulpi_phy = false;
872 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
874 dev_err(&pdev->dev, "Failed to get UTMI Pad regs\n");
878 tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
880 if (!tegra_phy->pad_regs) {
881 dev_err(&pdev->dev, "Failed to remap UTMI Pad regs\n");
885 tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
887 if (!tegra_phy->config)
890 config = tegra_phy->config;
892 err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
893 &config->hssync_start_delay);
897 err = read_utmi_param(pdev, "nvidia,elastic-limit",
898 &config->elastic_limit);
902 err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
903 &config->idle_wait_delay);
907 err = read_utmi_param(pdev, "nvidia,term-range-adj",
908 &config->term_range_adj);
912 err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
913 &config->xcvr_lsfslew);
917 err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
918 &config->xcvr_lsrslew);
922 if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
923 err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
924 &config->xcvr_hsslew);
928 err = read_utmi_param(pdev, "nvidia,hssquelch-level",
929 &config->hssquelch_level);
933 err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
934 &config->hsdiscon_level);
939 config->xcvr_setup_use_fuses = of_property_read_bool(
940 pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
942 if (!config->xcvr_setup_use_fuses) {
943 err = read_utmi_param(pdev, "nvidia,xcvr-setup",
944 &config->xcvr_setup);
952 static const struct tegra_phy_soc_config tegra20_soc_config = {
953 .utmi_pll_config_in_car_module = false,
955 .requires_usbmode_setup = false,
956 .requires_extra_tuning_parameters = false,
959 static const struct tegra_phy_soc_config tegra30_soc_config = {
960 .utmi_pll_config_in_car_module = true,
962 .requires_usbmode_setup = true,
963 .requires_extra_tuning_parameters = true,
966 static const struct of_device_id tegra_usb_phy_id_table[] = {
967 { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
968 { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
971 MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
973 static int tegra_usb_phy_probe(struct platform_device *pdev)
975 const struct of_device_id *match;
976 struct resource *res;
977 struct tegra_usb_phy *tegra_phy = NULL;
978 struct device_node *np = pdev->dev.of_node;
979 enum usb_phy_interface phy_type;
982 tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
986 match = of_match_device(tegra_usb_phy_id_table, &pdev->dev);
988 dev_err(&pdev->dev, "Error: No device match found\n");
991 tegra_phy->soc_config = match->data;
993 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
995 dev_err(&pdev->dev, "Failed to get I/O memory\n");
999 tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
1000 resource_size(res));
1001 if (!tegra_phy->regs) {
1002 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
1006 tegra_phy->is_legacy_phy =
1007 of_property_read_bool(np, "nvidia,has-legacy-mode");
1009 phy_type = of_usb_get_phy_mode(np);
1011 case USBPHY_INTERFACE_MODE_UTMI:
1012 err = utmi_phy_probe(tegra_phy, pdev);
1017 case USBPHY_INTERFACE_MODE_ULPI:
1018 tegra_phy->is_ulpi_phy = true;
1020 tegra_phy->reset_gpio =
1021 of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
1022 if (!gpio_is_valid(tegra_phy->reset_gpio)) {
1023 dev_err(&pdev->dev, "invalid gpio: %d\n",
1024 tegra_phy->reset_gpio);
1025 return tegra_phy->reset_gpio;
1027 tegra_phy->config = NULL;
1031 dev_err(&pdev->dev, "phy_type is invalid or unsupported\n");
1035 if (of_find_property(np, "dr_mode", NULL))
1036 tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
1038 tegra_phy->mode = USB_DR_MODE_HOST;
1040 if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
1041 dev_err(&pdev->dev, "dr_mode is invalid\n");
1045 /* On some boards, the VBUS regulator doesn't need to be controlled */
1046 if (of_find_property(np, "vbus-supply", NULL)) {
1047 tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
1048 if (IS_ERR(tegra_phy->vbus))
1049 return PTR_ERR(tegra_phy->vbus);
1051 dev_notice(&pdev->dev, "no vbus regulator");
1052 tegra_phy->vbus = ERR_PTR(-ENODEV);
1055 tegra_phy->u_phy.dev = &pdev->dev;
1056 err = tegra_usb_phy_init(tegra_phy);
1060 tegra_phy->u_phy.set_suspend = tegra_usb_phy_suspend;
1062 platform_set_drvdata(pdev, tegra_phy);
1064 err = usb_add_phy_dev(&tegra_phy->u_phy);
1066 tegra_usb_phy_close(tegra_phy);
1073 static int tegra_usb_phy_remove(struct platform_device *pdev)
1075 struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
1077 usb_remove_phy(&tegra_phy->u_phy);
1078 tegra_usb_phy_close(tegra_phy);
1083 static struct platform_driver tegra_usb_phy_driver = {
1084 .probe = tegra_usb_phy_probe,
1085 .remove = tegra_usb_phy_remove,
1087 .name = "tegra-phy",
1088 .of_match_table = tegra_usb_phy_id_table,
1091 module_platform_driver(tegra_usb_phy_driver);
1093 MODULE_DESCRIPTION("Tegra USB PHY driver");
1094 MODULE_LICENSE("GPL v2");