2 * Copyright (C) 2010 Google, Inc.
5 * Erik Gilling <konkers@google.com>
6 * Benoit Goby <benoit@android.com>
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/resource.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/export.h>
24 #include <linux/platform_device.h>
26 #include <linux/gpio.h>
28 #include <linux/of_gpio.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/ulpi.h>
31 #include <asm/mach-types.h>
32 #include <linux/usb/tegra_usb_phy.h>
34 #define TEGRA_USB_BASE 0xC5000000
35 #define TEGRA_USB_SIZE SZ_16K
37 #define ULPI_VIEWPORT 0x170
39 #define USB_SUSP_CTRL 0x400
40 #define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
41 #define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
42 #define USB_SUSP_CLR (1 << 5)
43 #define USB_PHY_CLK_VALID (1 << 7)
44 #define UTMIP_RESET (1 << 11)
45 #define UHSIC_RESET (1 << 11)
46 #define UTMIP_PHY_ENABLE (1 << 12)
47 #define ULPI_PHY_ENABLE (1 << 13)
48 #define USB_SUSP_SET (1 << 14)
49 #define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
51 #define USB1_LEGACY_CTRL 0x410
52 #define USB1_NO_LEGACY_MODE (1 << 0)
53 #define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
54 #define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
55 #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
57 #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
58 #define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
60 #define ULPI_TIMING_CTRL_0 0x424
61 #define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
62 #define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
64 #define ULPI_TIMING_CTRL_1 0x428
65 #define ULPI_DATA_TRIMMER_LOAD (1 << 0)
66 #define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
67 #define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
68 #define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
69 #define ULPI_DIR_TRIMMER_LOAD (1 << 24)
70 #define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
72 #define UTMIP_PLL_CFG1 0x804
73 #define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
74 #define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
76 #define UTMIP_XCVR_CFG0 0x808
77 #define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
78 #define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
79 #define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
80 #define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
81 #define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
82 #define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
83 #define UTMIP_XCVR_HSSLEW_MSB(x) (((x) & 0x7f) << 25)
85 #define UTMIP_BIAS_CFG0 0x80c
86 #define UTMIP_OTGPD (1 << 11)
87 #define UTMIP_BIASPD (1 << 10)
89 #define UTMIP_HSRX_CFG0 0x810
90 #define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
91 #define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
93 #define UTMIP_HSRX_CFG1 0x814
94 #define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
96 #define UTMIP_TX_CFG0 0x820
97 #define UTMIP_FS_PREABMLE_J (1 << 19)
98 #define UTMIP_HS_DISCON_DISABLE (1 << 8)
100 #define UTMIP_MISC_CFG0 0x824
101 #define UTMIP_DPDM_OBSERVE (1 << 26)
102 #define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
103 #define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
104 #define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
105 #define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
106 #define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
107 #define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
109 #define UTMIP_MISC_CFG1 0x828
110 #define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
111 #define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
113 #define UTMIP_DEBOUNCE_CFG0 0x82c
114 #define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
116 #define UTMIP_BAT_CHRG_CFG0 0x830
117 #define UTMIP_PD_CHRG (1 << 0)
119 #define UTMIP_SPARE_CFG0 0x834
120 #define FUSE_SETUP_SEL (1 << 3)
122 #define UTMIP_XCVR_CFG1 0x838
123 #define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
124 #define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
125 #define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
126 #define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
128 #define UTMIP_BIAS_CFG1 0x83c
129 #define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
131 static DEFINE_SPINLOCK(utmip_pad_lock
);
132 static int utmip_pad_count
;
134 struct tegra_xtal_freq
{
143 static const struct tegra_xtal_freq tegra_freq_table
[] = {
146 .enable_delay
= 0x02,
147 .stable_count
= 0x2F,
148 .active_delay
= 0x04,
149 .xtal_freq_count
= 0x76,
154 .enable_delay
= 0x02,
155 .stable_count
= 0x33,
156 .active_delay
= 0x05,
157 .xtal_freq_count
= 0x7F,
162 .enable_delay
= 0x03,
163 .stable_count
= 0x4B,
164 .active_delay
= 0x06,
165 .xtal_freq_count
= 0xBB,
170 .enable_delay
= 0x04,
171 .stable_count
= 0x66,
172 .active_delay
= 0x09,
173 .xtal_freq_count
= 0xFE,
178 static struct tegra_utmip_config utmip_default
[] = {
180 .hssync_start_delay
= 9,
181 .idle_wait_delay
= 17,
189 .hssync_start_delay
= 9,
190 .idle_wait_delay
= 17,
199 static int utmip_pad_open(struct tegra_usb_phy
*phy
)
201 phy
->pad_clk
= clk_get_sys("utmip-pad", NULL
);
202 if (IS_ERR(phy
->pad_clk
)) {
203 pr_err("%s: can't get utmip pad clock\n", __func__
);
204 return PTR_ERR(phy
->pad_clk
);
207 if (phy
->is_legacy_phy
) {
208 phy
->pad_regs
= phy
->regs
;
210 phy
->pad_regs
= ioremap(TEGRA_USB_BASE
, TEGRA_USB_SIZE
);
211 if (!phy
->pad_regs
) {
212 pr_err("%s: can't remap usb registers\n", __func__
);
213 clk_put(phy
->pad_clk
);
220 static void utmip_pad_close(struct tegra_usb_phy
*phy
)
222 if (!phy
->is_legacy_phy
)
223 iounmap(phy
->pad_regs
);
224 clk_put(phy
->pad_clk
);
227 static void utmip_pad_power_on(struct tegra_usb_phy
*phy
)
229 unsigned long val
, flags
;
230 void __iomem
*base
= phy
->pad_regs
;
232 clk_prepare_enable(phy
->pad_clk
);
234 spin_lock_irqsave(&utmip_pad_lock
, flags
);
236 if (utmip_pad_count
++ == 0) {
237 val
= readl(base
+ UTMIP_BIAS_CFG0
);
238 val
&= ~(UTMIP_OTGPD
| UTMIP_BIASPD
);
239 writel(val
, base
+ UTMIP_BIAS_CFG0
);
242 spin_unlock_irqrestore(&utmip_pad_lock
, flags
);
244 clk_disable_unprepare(phy
->pad_clk
);
247 static int utmip_pad_power_off(struct tegra_usb_phy
*phy
)
249 unsigned long val
, flags
;
250 void __iomem
*base
= phy
->pad_regs
;
252 if (!utmip_pad_count
) {
253 pr_err("%s: utmip pad already powered off\n", __func__
);
257 clk_prepare_enable(phy
->pad_clk
);
259 spin_lock_irqsave(&utmip_pad_lock
, flags
);
261 if (--utmip_pad_count
== 0) {
262 val
= readl(base
+ UTMIP_BIAS_CFG0
);
263 val
|= UTMIP_OTGPD
| UTMIP_BIASPD
;
264 writel(val
, base
+ UTMIP_BIAS_CFG0
);
267 spin_unlock_irqrestore(&utmip_pad_lock
, flags
);
269 clk_disable_unprepare(phy
->pad_clk
);
274 static int utmi_wait_register(void __iomem
*reg
, u32 mask
, u32 result
)
276 unsigned long timeout
= 2000;
278 if ((readl(reg
) & mask
) == result
)
286 static void utmi_phy_clk_disable(struct tegra_usb_phy
*phy
)
289 void __iomem
*base
= phy
->regs
;
291 if (phy
->is_legacy_phy
) {
292 val
= readl(base
+ USB_SUSP_CTRL
);
294 writel(val
, base
+ USB_SUSP_CTRL
);
298 val
= readl(base
+ USB_SUSP_CTRL
);
299 val
&= ~USB_SUSP_SET
;
300 writel(val
, base
+ USB_SUSP_CTRL
);
302 phy
->set_phcd(&phy
->u_phy
, true);
304 if (utmi_wait_register(base
+ USB_SUSP_CTRL
, USB_PHY_CLK_VALID
, 0) < 0)
305 pr_err("%s: timeout waiting for phy to stabilize\n", __func__
);
308 static void utmi_phy_clk_enable(struct tegra_usb_phy
*phy
)
311 void __iomem
*base
= phy
->regs
;
313 if (phy
->is_legacy_phy
) {
314 val
= readl(base
+ USB_SUSP_CTRL
);
316 writel(val
, base
+ USB_SUSP_CTRL
);
320 val
= readl(base
+ USB_SUSP_CTRL
);
321 val
&= ~USB_SUSP_CLR
;
322 writel(val
, base
+ USB_SUSP_CTRL
);
324 phy
->set_phcd(&phy
->u_phy
, false);
326 if (utmi_wait_register(base
+ USB_SUSP_CTRL
, USB_PHY_CLK_VALID
,
328 pr_err("%s: timeout waiting for phy to stabilize\n", __func__
);
331 static int utmi_phy_power_on(struct tegra_usb_phy
*phy
)
334 void __iomem
*base
= phy
->regs
;
335 struct tegra_utmip_config
*config
= phy
->config
;
337 val
= readl(base
+ USB_SUSP_CTRL
);
339 writel(val
, base
+ USB_SUSP_CTRL
);
341 if (phy
->is_legacy_phy
) {
342 val
= readl(base
+ USB1_LEGACY_CTRL
);
343 val
|= USB1_NO_LEGACY_MODE
;
344 writel(val
, base
+ USB1_LEGACY_CTRL
);
347 val
= readl(base
+ UTMIP_TX_CFG0
);
348 val
&= ~UTMIP_FS_PREABMLE_J
;
349 writel(val
, base
+ UTMIP_TX_CFG0
);
351 val
= readl(base
+ UTMIP_HSRX_CFG0
);
352 val
&= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
353 val
|= UTMIP_IDLE_WAIT(config
->idle_wait_delay
);
354 val
|= UTMIP_ELASTIC_LIMIT(config
->elastic_limit
);
355 writel(val
, base
+ UTMIP_HSRX_CFG0
);
357 val
= readl(base
+ UTMIP_HSRX_CFG1
);
358 val
&= ~UTMIP_HS_SYNC_START_DLY(~0);
359 val
|= UTMIP_HS_SYNC_START_DLY(config
->hssync_start_delay
);
360 writel(val
, base
+ UTMIP_HSRX_CFG1
);
362 val
= readl(base
+ UTMIP_DEBOUNCE_CFG0
);
363 val
&= ~UTMIP_BIAS_DEBOUNCE_A(~0);
364 val
|= UTMIP_BIAS_DEBOUNCE_A(phy
->freq
->debounce
);
365 writel(val
, base
+ UTMIP_DEBOUNCE_CFG0
);
367 val
= readl(base
+ UTMIP_MISC_CFG0
);
368 val
&= ~UTMIP_SUSPEND_EXIT_ON_EDGE
;
369 writel(val
, base
+ UTMIP_MISC_CFG0
);
371 val
= readl(base
+ UTMIP_MISC_CFG1
);
372 val
&= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) | UTMIP_PLLU_STABLE_COUNT(~0));
373 val
|= UTMIP_PLL_ACTIVE_DLY_COUNT(phy
->freq
->active_delay
) |
374 UTMIP_PLLU_STABLE_COUNT(phy
->freq
->stable_count
);
375 writel(val
, base
+ UTMIP_MISC_CFG1
);
377 val
= readl(base
+ UTMIP_PLL_CFG1
);
378 val
&= ~(UTMIP_XTAL_FREQ_COUNT(~0) | UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
379 val
|= UTMIP_XTAL_FREQ_COUNT(phy
->freq
->xtal_freq_count
) |
380 UTMIP_PLLU_ENABLE_DLY_COUNT(phy
->freq
->enable_delay
);
381 writel(val
, base
+ UTMIP_PLL_CFG1
);
383 if (phy
->mode
== TEGRA_USB_PHY_MODE_DEVICE
) {
384 val
= readl(base
+ USB_SUSP_CTRL
);
385 val
&= ~(USB_WAKE_ON_CNNT_EN_DEV
| USB_WAKE_ON_DISCON_EN_DEV
);
386 writel(val
, base
+ USB_SUSP_CTRL
);
389 utmip_pad_power_on(phy
);
391 val
= readl(base
+ UTMIP_XCVR_CFG0
);
392 val
&= ~(UTMIP_FORCE_PD_POWERDOWN
| UTMIP_FORCE_PD2_POWERDOWN
|
393 UTMIP_FORCE_PDZI_POWERDOWN
| UTMIP_XCVR_SETUP(~0) |
394 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
395 UTMIP_XCVR_HSSLEW_MSB(~0));
396 val
|= UTMIP_XCVR_SETUP(config
->xcvr_setup
);
397 val
|= UTMIP_XCVR_LSFSLEW(config
->xcvr_lsfslew
);
398 val
|= UTMIP_XCVR_LSRSLEW(config
->xcvr_lsrslew
);
399 writel(val
, base
+ UTMIP_XCVR_CFG0
);
401 val
= readl(base
+ UTMIP_XCVR_CFG1
);
402 val
&= ~(UTMIP_FORCE_PDDISC_POWERDOWN
| UTMIP_FORCE_PDCHRP_POWERDOWN
|
403 UTMIP_FORCE_PDDR_POWERDOWN
| UTMIP_XCVR_TERM_RANGE_ADJ(~0));
404 val
|= UTMIP_XCVR_TERM_RANGE_ADJ(config
->term_range_adj
);
405 writel(val
, base
+ UTMIP_XCVR_CFG1
);
407 val
= readl(base
+ UTMIP_BAT_CHRG_CFG0
);
408 val
&= ~UTMIP_PD_CHRG
;
409 writel(val
, base
+ UTMIP_BAT_CHRG_CFG0
);
411 val
= readl(base
+ UTMIP_BIAS_CFG1
);
412 val
&= ~UTMIP_BIAS_PDTRK_COUNT(~0);
413 val
|= UTMIP_BIAS_PDTRK_COUNT(0x5);
414 writel(val
, base
+ UTMIP_BIAS_CFG1
);
416 if (phy
->is_legacy_phy
) {
417 val
= readl(base
+ UTMIP_SPARE_CFG0
);
418 if (phy
->mode
== TEGRA_USB_PHY_MODE_DEVICE
)
419 val
&= ~FUSE_SETUP_SEL
;
421 val
|= FUSE_SETUP_SEL
;
422 writel(val
, base
+ UTMIP_SPARE_CFG0
);
424 val
= readl(base
+ USB_SUSP_CTRL
);
425 val
|= UTMIP_PHY_ENABLE
;
426 writel(val
, base
+ USB_SUSP_CTRL
);
429 val
= readl(base
+ USB_SUSP_CTRL
);
431 writel(val
, base
+ USB_SUSP_CTRL
);
433 if (phy
->is_legacy_phy
) {
434 val
= readl(base
+ USB1_LEGACY_CTRL
);
435 val
&= ~USB1_VBUS_SENSE_CTL_MASK
;
436 val
|= USB1_VBUS_SENSE_CTL_A_SESS_VLD
;
437 writel(val
, base
+ USB1_LEGACY_CTRL
);
439 val
= readl(base
+ USB_SUSP_CTRL
);
440 val
&= ~USB_SUSP_SET
;
441 writel(val
, base
+ USB_SUSP_CTRL
);
444 utmi_phy_clk_enable(phy
);
446 if (!phy
->is_legacy_phy
)
447 phy
->set_pts(&phy
->u_phy
, 0);
452 static int utmi_phy_power_off(struct tegra_usb_phy
*phy
)
455 void __iomem
*base
= phy
->regs
;
457 utmi_phy_clk_disable(phy
);
459 if (phy
->mode
== TEGRA_USB_PHY_MODE_DEVICE
) {
460 val
= readl(base
+ USB_SUSP_CTRL
);
461 val
&= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
462 val
|= USB_WAKE_ON_CNNT_EN_DEV
| USB_WAKEUP_DEBOUNCE_COUNT(5);
463 writel(val
, base
+ USB_SUSP_CTRL
);
466 val
= readl(base
+ USB_SUSP_CTRL
);
468 writel(val
, base
+ USB_SUSP_CTRL
);
470 val
= readl(base
+ UTMIP_BAT_CHRG_CFG0
);
471 val
|= UTMIP_PD_CHRG
;
472 writel(val
, base
+ UTMIP_BAT_CHRG_CFG0
);
474 val
= readl(base
+ UTMIP_XCVR_CFG0
);
475 val
|= UTMIP_FORCE_PD_POWERDOWN
| UTMIP_FORCE_PD2_POWERDOWN
|
476 UTMIP_FORCE_PDZI_POWERDOWN
;
477 writel(val
, base
+ UTMIP_XCVR_CFG0
);
479 val
= readl(base
+ UTMIP_XCVR_CFG1
);
480 val
|= UTMIP_FORCE_PDDISC_POWERDOWN
| UTMIP_FORCE_PDCHRP_POWERDOWN
|
481 UTMIP_FORCE_PDDR_POWERDOWN
;
482 writel(val
, base
+ UTMIP_XCVR_CFG1
);
484 return utmip_pad_power_off(phy
);
487 static void utmi_phy_preresume(struct tegra_usb_phy
*phy
)
490 void __iomem
*base
= phy
->regs
;
492 val
= readl(base
+ UTMIP_TX_CFG0
);
493 val
|= UTMIP_HS_DISCON_DISABLE
;
494 writel(val
, base
+ UTMIP_TX_CFG0
);
497 static void utmi_phy_postresume(struct tegra_usb_phy
*phy
)
500 void __iomem
*base
= phy
->regs
;
502 val
= readl(base
+ UTMIP_TX_CFG0
);
503 val
&= ~UTMIP_HS_DISCON_DISABLE
;
504 writel(val
, base
+ UTMIP_TX_CFG0
);
507 static void utmi_phy_restore_start(struct tegra_usb_phy
*phy
,
508 enum tegra_usb_phy_port_speed port_speed
)
511 void __iomem
*base
= phy
->regs
;
513 val
= readl(base
+ UTMIP_MISC_CFG0
);
514 val
&= ~UTMIP_DPDM_OBSERVE_SEL(~0);
515 if (port_speed
== TEGRA_USB_PHY_PORT_SPEED_LOW
)
516 val
|= UTMIP_DPDM_OBSERVE_SEL_FS_K
;
518 val
|= UTMIP_DPDM_OBSERVE_SEL_FS_J
;
519 writel(val
, base
+ UTMIP_MISC_CFG0
);
522 val
= readl(base
+ UTMIP_MISC_CFG0
);
523 val
|= UTMIP_DPDM_OBSERVE
;
524 writel(val
, base
+ UTMIP_MISC_CFG0
);
528 static void utmi_phy_restore_end(struct tegra_usb_phy
*phy
)
531 void __iomem
*base
= phy
->regs
;
533 val
= readl(base
+ UTMIP_MISC_CFG0
);
534 val
&= ~UTMIP_DPDM_OBSERVE
;
535 writel(val
, base
+ UTMIP_MISC_CFG0
);
539 static int ulpi_phy_power_on(struct tegra_usb_phy
*phy
)
543 void __iomem
*base
= phy
->regs
;
544 struct tegra_ulpi_config
*config
= phy
->config
;
546 gpio_direction_output(config
->reset_gpio
, 0);
548 gpio_direction_output(config
->reset_gpio
, 1);
550 clk_prepare_enable(phy
->clk
);
553 val
= readl(base
+ USB_SUSP_CTRL
);
555 writel(val
, base
+ USB_SUSP_CTRL
);
557 val
= readl(base
+ ULPI_TIMING_CTRL_0
);
558 val
|= ULPI_OUTPUT_PINMUX_BYP
| ULPI_CLKOUT_PINMUX_BYP
;
559 writel(val
, base
+ ULPI_TIMING_CTRL_0
);
561 val
= readl(base
+ USB_SUSP_CTRL
);
562 val
|= ULPI_PHY_ENABLE
;
563 writel(val
, base
+ USB_SUSP_CTRL
);
566 writel(val
, base
+ ULPI_TIMING_CTRL_1
);
568 val
|= ULPI_DATA_TRIMMER_SEL(4);
569 val
|= ULPI_STPDIRNXT_TRIMMER_SEL(4);
570 val
|= ULPI_DIR_TRIMMER_SEL(4);
571 writel(val
, base
+ ULPI_TIMING_CTRL_1
);
574 val
|= ULPI_DATA_TRIMMER_LOAD
;
575 val
|= ULPI_STPDIRNXT_TRIMMER_LOAD
;
576 val
|= ULPI_DIR_TRIMMER_LOAD
;
577 writel(val
, base
+ ULPI_TIMING_CTRL_1
);
579 /* Fix VbusInvalid due to floating VBUS */
580 ret
= usb_phy_io_write(phy
->ulpi
, 0x40, 0x08);
582 pr_err("%s: ulpi write failed\n", __func__
);
586 ret
= usb_phy_io_write(phy
->ulpi
, 0x80, 0x0B);
588 pr_err("%s: ulpi write failed\n", __func__
);
592 val
= readl(base
+ USB_SUSP_CTRL
);
594 writel(val
, base
+ USB_SUSP_CTRL
);
597 val
= readl(base
+ USB_SUSP_CTRL
);
598 val
&= ~USB_SUSP_CLR
;
599 writel(val
, base
+ USB_SUSP_CTRL
);
604 static int ulpi_phy_power_off(struct tegra_usb_phy
*phy
)
606 struct tegra_ulpi_config
*config
= phy
->config
;
608 clk_disable(phy
->clk
);
609 return gpio_direction_output(config
->reset_gpio
, 0);
612 static int tegra_phy_init(struct usb_phy
*x
)
614 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
615 struct tegra_ulpi_config
*ulpi_config
;
618 if (phy
->is_ulpi_phy
) {
619 ulpi_config
= phy
->config
;
620 phy
->clk
= clk_get_sys(NULL
, ulpi_config
->clk
);
621 if (IS_ERR(phy
->clk
)) {
622 pr_err("%s: can't get ulpi clock\n", __func__
);
626 if (!gpio_is_valid(ulpi_config
->reset_gpio
))
627 ulpi_config
->reset_gpio
=
628 of_get_named_gpio(phy
->dev
->of_node
,
629 "nvidia,phy-reset-gpio", 0);
630 if (!gpio_is_valid(ulpi_config
->reset_gpio
)) {
631 pr_err("%s: invalid reset gpio: %d\n", __func__
,
632 ulpi_config
->reset_gpio
);
636 gpio_request(ulpi_config
->reset_gpio
, "ulpi_phy_reset_b");
637 gpio_direction_output(ulpi_config
->reset_gpio
, 0);
638 phy
->ulpi
= otg_ulpi_create(&ulpi_viewport_access_ops
, 0);
639 phy
->ulpi
->io_priv
= phy
->regs
+ ULPI_VIEWPORT
;
641 err
= utmip_pad_open(phy
);
647 clk_disable_unprepare(phy
->pll_u
);
652 static void tegra_usb_phy_close(struct usb_phy
*x
)
654 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
656 if (phy
->is_ulpi_phy
)
659 utmip_pad_close(phy
);
660 clk_disable_unprepare(phy
->pll_u
);
665 static int tegra_usb_phy_power_on(struct tegra_usb_phy
*phy
)
667 if (phy
->is_ulpi_phy
)
668 return ulpi_phy_power_on(phy
);
670 return utmi_phy_power_on(phy
);
673 static int tegra_usb_phy_power_off(struct tegra_usb_phy
*phy
)
675 if (phy
->is_ulpi_phy
)
676 return ulpi_phy_power_off(phy
);
678 return utmi_phy_power_off(phy
);
681 static int tegra_usb_phy_suspend(struct usb_phy
*x
, int suspend
)
683 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
685 return tegra_usb_phy_power_off(phy
);
687 return tegra_usb_phy_power_on(phy
);
690 struct tegra_usb_phy
*tegra_usb_phy_open(struct device
*dev
, int instance
,
691 void __iomem
*regs
, void *config
, enum tegra_usb_phy_mode phy_mode
,
692 void (*set_pts
)(struct usb_phy
*x
, u8 pts_val
),
693 void (*set_phcd
)(struct usb_phy
*x
, bool enable
))
696 struct tegra_usb_phy
*phy
;
697 unsigned long parent_rate
;
700 struct device_node
*np
= dev
->of_node
;
702 phy
= kzalloc(sizeof(struct tegra_usb_phy
), GFP_KERNEL
);
704 return ERR_PTR(-ENOMEM
);
706 phy
->instance
= instance
;
708 phy
->config
= config
;
709 phy
->mode
= phy_mode
;
712 of_property_read_bool(np
, "nvidia,has-legacy-mode");
713 phy
->set_pts
= set_pts
;
714 phy
->set_phcd
= set_phcd
;
715 err
= of_property_match_string(np
, "phy_type", "ulpi");
717 phy
->is_ulpi_phy
= false;
719 phy
->is_ulpi_phy
= true;
722 if (phy
->is_ulpi_phy
) {
723 pr_err("%s: ulpi phy configuration missing", __func__
);
727 phy
->config
= &utmip_default
[instance
];
731 phy
->pll_u
= clk_get_sys(NULL
, "pll_u");
732 if (IS_ERR(phy
->pll_u
)) {
733 pr_err("Can't get pll_u clock\n");
734 err
= PTR_ERR(phy
->pll_u
);
737 clk_prepare_enable(phy
->pll_u
);
739 parent_rate
= clk_get_rate(clk_get_parent(phy
->pll_u
));
740 for (i
= 0; i
< ARRAY_SIZE(tegra_freq_table
); i
++) {
741 if (tegra_freq_table
[i
].freq
== parent_rate
) {
742 phy
->freq
= &tegra_freq_table
[i
];
747 pr_err("invalid pll_u parent rate %ld\n", parent_rate
);
752 phy
->u_phy
.init
= tegra_phy_init
;
753 phy
->u_phy
.shutdown
= tegra_usb_phy_close
;
754 phy
->u_phy
.set_suspend
= tegra_usb_phy_suspend
;
759 clk_disable_unprepare(phy
->pll_u
);
765 EXPORT_SYMBOL_GPL(tegra_usb_phy_open
);
767 void tegra_usb_phy_preresume(struct usb_phy
*x
)
769 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
771 if (!phy
->is_ulpi_phy
)
772 utmi_phy_preresume(phy
);
774 EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume
);
776 void tegra_usb_phy_postresume(struct usb_phy
*x
)
778 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
780 if (!phy
->is_ulpi_phy
)
781 utmi_phy_postresume(phy
);
783 EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume
);
785 void tegra_ehci_phy_restore_start(struct usb_phy
*x
,
786 enum tegra_usb_phy_port_speed port_speed
)
788 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
790 if (!phy
->is_ulpi_phy
)
791 utmi_phy_restore_start(phy
, port_speed
);
793 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start
);
795 void tegra_ehci_phy_restore_end(struct usb_phy
*x
)
797 struct tegra_usb_phy
*phy
= container_of(x
, struct tegra_usb_phy
, u_phy
);
799 if (!phy
->is_ulpi_phy
)
800 utmi_phy_restore_end(phy
);
802 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end
);