Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2010 Google, Inc.
0004  * Copyright (C) 2013 NVIDIA Corporation
0005  *
0006  * Author:
0007  *  Erik Gilling <konkers@google.com>
0008  *  Benoit Goby <benoit@android.com>
0009  *  Venu Byravarasu <vbyravarasu@nvidia.com>
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/err.h>
0014 #include <linux/export.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/iopoll.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/of_device.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/resource.h>
0022 #include <linux/slab.h>
0023 #include <linux/spinlock.h>
0024 
0025 #include <linux/regulator/consumer.h>
0026 
0027 #include <linux/usb/ehci_def.h>
0028 #include <linux/usb/of.h>
0029 #include <linux/usb/tegra_usb_phy.h>
0030 #include <linux/usb/ulpi.h>
0031 
0032 #define ULPI_VIEWPORT               0x170
0033 
0034 /* PORTSC PTS/PHCD bits, Tegra20 only */
0035 #define TEGRA_USB_PORTSC1           0x184
0036 #define TEGRA_USB_PORTSC1_PTS(x)        (((x) & 0x3) << 30)
0037 #define TEGRA_USB_PORTSC1_PHCD          BIT(23)
0038 
0039 /* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
0040 #define TEGRA_USB_HOSTPC1_DEVLC         0x1b4
0041 #define TEGRA_USB_HOSTPC1_DEVLC_PTS(x)      (((x) & 0x7) << 29)
0042 #define TEGRA_USB_HOSTPC1_DEVLC_PHCD        BIT(22)
0043 
0044 /* Bits of PORTSC1, which will get cleared by writing 1 into them */
0045 #define TEGRA_PORTSC1_RWC_BITS  (PORT_CSC | PORT_PEC | PORT_OCC)
0046 
0047 #define USB_SUSP_CTRL               0x400
0048 #define   USB_WAKE_ON_RESUME_EN         BIT(2)
0049 #define   USB_WAKE_ON_CNNT_EN_DEV       BIT(3)
0050 #define   USB_WAKE_ON_DISCON_EN_DEV     BIT(4)
0051 #define   USB_SUSP_CLR              BIT(5)
0052 #define   USB_PHY_CLK_VALID         BIT(7)
0053 #define   UTMIP_RESET               BIT(11)
0054 #define   UHSIC_RESET               BIT(11)
0055 #define   UTMIP_PHY_ENABLE          BIT(12)
0056 #define   ULPI_PHY_ENABLE           BIT(13)
0057 #define   USB_SUSP_SET              BIT(14)
0058 #define   USB_WAKEUP_DEBOUNCE_COUNT(x)      (((x) & 0x7) << 16)
0059 
0060 #define USB_PHY_VBUS_SENSORS            0x404
0061 #define   B_SESS_VLD_WAKEUP_EN          BIT(14)
0062 #define   A_SESS_VLD_WAKEUP_EN          BIT(22)
0063 #define   A_VBUS_VLD_WAKEUP_EN          BIT(30)
0064 
0065 #define USB_PHY_VBUS_WAKEUP_ID          0x408
0066 #define   ID_INT_EN             BIT(0)
0067 #define   ID_CHG_DET                BIT(1)
0068 #define   VBUS_WAKEUP_INT_EN            BIT(8)
0069 #define   VBUS_WAKEUP_CHG_DET           BIT(9)
0070 #define   VBUS_WAKEUP_STS           BIT(10)
0071 #define   VBUS_WAKEUP_WAKEUP_EN         BIT(30)
0072 
0073 #define USB1_LEGACY_CTRL            0x410
0074 #define   USB1_NO_LEGACY_MODE           BIT(0)
0075 #define   USB1_VBUS_SENSE_CTL_MASK      (3 << 1)
0076 #define   USB1_VBUS_SENSE_CTL_VBUS_WAKEUP   (0 << 1)
0077 #define   USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
0078                         (1 << 1)
0079 #define   USB1_VBUS_SENSE_CTL_AB_SESS_VLD   (2 << 1)
0080 #define   USB1_VBUS_SENSE_CTL_A_SESS_VLD    (3 << 1)
0081 
0082 #define ULPI_TIMING_CTRL_0          0x424
0083 #define   ULPI_OUTPUT_PINMUX_BYP        BIT(10)
0084 #define   ULPI_CLKOUT_PINMUX_BYP        BIT(11)
0085 
0086 #define ULPI_TIMING_CTRL_1          0x428
0087 #define   ULPI_DATA_TRIMMER_LOAD        BIT(0)
0088 #define   ULPI_DATA_TRIMMER_SEL(x)      (((x) & 0x7) << 1)
0089 #define   ULPI_STPDIRNXT_TRIMMER_LOAD       BIT(16)
0090 #define   ULPI_STPDIRNXT_TRIMMER_SEL(x)     (((x) & 0x7) << 17)
0091 #define   ULPI_DIR_TRIMMER_LOAD         BIT(24)
0092 #define   ULPI_DIR_TRIMMER_SEL(x)       (((x) & 0x7) << 25)
0093 
0094 #define UTMIP_PLL_CFG1              0x804
0095 #define   UTMIP_XTAL_FREQ_COUNT(x)      (((x) & 0xfff) << 0)
0096 #define   UTMIP_PLLU_ENABLE_DLY_COUNT(x)    (((x) & 0x1f) << 27)
0097 
0098 #define UTMIP_XCVR_CFG0             0x808
0099 #define   UTMIP_XCVR_SETUP(x)           (((x) & 0xf) << 0)
0100 #define   UTMIP_XCVR_SETUP_MSB(x)       ((((x) & 0x70) >> 4) << 22)
0101 #define   UTMIP_XCVR_LSRSLEW(x)         (((x) & 0x3) << 8)
0102 #define   UTMIP_XCVR_LSFSLEW(x)         (((x) & 0x3) << 10)
0103 #define   UTMIP_FORCE_PD_POWERDOWN      BIT(14)
0104 #define   UTMIP_FORCE_PD2_POWERDOWN     BIT(16)
0105 #define   UTMIP_FORCE_PDZI_POWERDOWN        BIT(18)
0106 #define   UTMIP_XCVR_LSBIAS_SEL         BIT(21)
0107 #define   UTMIP_XCVR_HSSLEW(x)          (((x) & 0x3) << 4)
0108 #define   UTMIP_XCVR_HSSLEW_MSB(x)      ((((x) & 0x1fc) >> 2) << 25)
0109 
0110 #define UTMIP_BIAS_CFG0             0x80c
0111 #define   UTMIP_OTGPD               BIT(11)
0112 #define   UTMIP_BIASPD              BIT(10)
0113 #define   UTMIP_HSSQUELCH_LEVEL(x)      (((x) & 0x3) << 0)
0114 #define   UTMIP_HSDISCON_LEVEL(x)       (((x) & 0x3) << 2)
0115 #define   UTMIP_HSDISCON_LEVEL_MSB(x)       ((((x) & 0x4) >> 2) << 24)
0116 
0117 #define UTMIP_HSRX_CFG0             0x810
0118 #define   UTMIP_ELASTIC_LIMIT(x)        (((x) & 0x1f) << 10)
0119 #define   UTMIP_IDLE_WAIT(x)            (((x) & 0x1f) << 15)
0120 
0121 #define UTMIP_HSRX_CFG1             0x814
0122 #define   UTMIP_HS_SYNC_START_DLY(x)        (((x) & 0x1f) << 1)
0123 
0124 #define UTMIP_TX_CFG0               0x820
0125 #define   UTMIP_FS_PREABMLE_J           BIT(19)
0126 #define   UTMIP_HS_DISCON_DISABLE       BIT(8)
0127 
0128 #define UTMIP_MISC_CFG0             0x824
0129 #define   UTMIP_DPDM_OBSERVE            BIT(26)
0130 #define   UTMIP_DPDM_OBSERVE_SEL(x)     (((x) & 0xf) << 27)
0131 #define   UTMIP_DPDM_OBSERVE_SEL_FS_J       UTMIP_DPDM_OBSERVE_SEL(0xf)
0132 #define   UTMIP_DPDM_OBSERVE_SEL_FS_K       UTMIP_DPDM_OBSERVE_SEL(0xe)
0133 #define   UTMIP_DPDM_OBSERVE_SEL_FS_SE1     UTMIP_DPDM_OBSERVE_SEL(0xd)
0134 #define   UTMIP_DPDM_OBSERVE_SEL_FS_SE0     UTMIP_DPDM_OBSERVE_SEL(0xc)
0135 #define   UTMIP_SUSPEND_EXIT_ON_EDGE        BIT(22)
0136 
0137 #define UTMIP_MISC_CFG1             0x828
0138 #define   UTMIP_PLL_ACTIVE_DLY_COUNT(x)     (((x) & 0x1f) << 18)
0139 #define   UTMIP_PLLU_STABLE_COUNT(x)        (((x) & 0xfff) << 6)
0140 
0141 #define UTMIP_DEBOUNCE_CFG0         0x82c
0142 #define   UTMIP_BIAS_DEBOUNCE_A(x)      (((x) & 0xffff) << 0)
0143 
0144 #define UTMIP_BAT_CHRG_CFG0         0x830
0145 #define   UTMIP_PD_CHRG             BIT(0)
0146 
0147 #define UTMIP_SPARE_CFG0            0x834
0148 #define   FUSE_SETUP_SEL            BIT(3)
0149 
0150 #define UTMIP_XCVR_CFG1             0x838
0151 #define   UTMIP_FORCE_PDDISC_POWERDOWN      BIT(0)
0152 #define   UTMIP_FORCE_PDCHRP_POWERDOWN      BIT(2)
0153 #define   UTMIP_FORCE_PDDR_POWERDOWN        BIT(4)
0154 #define   UTMIP_XCVR_TERM_RANGE_ADJ(x)      (((x) & 0xf) << 18)
0155 
0156 #define UTMIP_BIAS_CFG1             0x83c
0157 #define   UTMIP_BIAS_PDTRK_COUNT(x)     (((x) & 0x1f) << 3)
0158 
0159 /* For Tegra30 and above only, the address is different in Tegra20 */
0160 #define USB_USBMODE             0x1f8
0161 #define   USB_USBMODE_MASK          (3 << 0)
0162 #define   USB_USBMODE_HOST          (3 << 0)
0163 #define   USB_USBMODE_DEVICE            (2 << 0)
0164 
0165 #define PMC_USB_AO              0xf0
0166 #define   VBUS_WAKEUP_PD_P0         BIT(2)
0167 #define   ID_PD_P0              BIT(3)
0168 
0169 static DEFINE_SPINLOCK(utmip_pad_lock);
0170 static unsigned int utmip_pad_count;
0171 
0172 struct tegra_xtal_freq {
0173     unsigned int freq;
0174     u8 enable_delay;
0175     u8 stable_count;
0176     u8 active_delay;
0177     u8 xtal_freq_count;
0178     u16 debounce;
0179 };
0180 
0181 static const struct tegra_xtal_freq tegra_freq_table[] = {
0182     {
0183         .freq = 12000000,
0184         .enable_delay = 0x02,
0185         .stable_count = 0x2F,
0186         .active_delay = 0x04,
0187         .xtal_freq_count = 0x76,
0188         .debounce = 0x7530,
0189     },
0190     {
0191         .freq = 13000000,
0192         .enable_delay = 0x02,
0193         .stable_count = 0x33,
0194         .active_delay = 0x05,
0195         .xtal_freq_count = 0x7F,
0196         .debounce = 0x7EF4,
0197     },
0198     {
0199         .freq = 19200000,
0200         .enable_delay = 0x03,
0201         .stable_count = 0x4B,
0202         .active_delay = 0x06,
0203         .xtal_freq_count = 0xBB,
0204         .debounce = 0xBB80,
0205     },
0206     {
0207         .freq = 26000000,
0208         .enable_delay = 0x04,
0209         .stable_count = 0x66,
0210         .active_delay = 0x09,
0211         .xtal_freq_count = 0xFE,
0212         .debounce = 0xFDE8,
0213     },
0214 };
0215 
0216 static inline struct tegra_usb_phy *to_tegra_usb_phy(struct usb_phy *u_phy)
0217 {
0218     return container_of(u_phy, struct tegra_usb_phy, u_phy);
0219 }
0220 
0221 static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
0222 {
0223     void __iomem *base = phy->regs;
0224     u32 val;
0225 
0226     if (phy->soc_config->has_hostpc) {
0227         val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
0228         val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
0229         val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
0230         writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
0231     } else {
0232         val = readl_relaxed(base + TEGRA_USB_PORTSC1);
0233         val &= ~TEGRA_PORTSC1_RWC_BITS;
0234         val &= ~TEGRA_USB_PORTSC1_PTS(~0);
0235         val |= TEGRA_USB_PORTSC1_PTS(pts_val);
0236         writel_relaxed(val, base + TEGRA_USB_PORTSC1);
0237     }
0238 }
0239 
0240 static void set_phcd(struct tegra_usb_phy *phy, bool enable)
0241 {
0242     void __iomem *base = phy->regs;
0243     u32 val;
0244 
0245     if (phy->soc_config->has_hostpc) {
0246         val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
0247         if (enable)
0248             val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
0249         else
0250             val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
0251         writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
0252     } else {
0253         val = readl_relaxed(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
0254         if (enable)
0255             val |= TEGRA_USB_PORTSC1_PHCD;
0256         else
0257             val &= ~TEGRA_USB_PORTSC1_PHCD;
0258         writel_relaxed(val, base + TEGRA_USB_PORTSC1);
0259     }
0260 }
0261 
0262 static int utmip_pad_open(struct tegra_usb_phy *phy)
0263 {
0264     int ret;
0265 
0266     ret = clk_prepare_enable(phy->pad_clk);
0267     if (ret) {
0268         dev_err(phy->u_phy.dev,
0269             "Failed to enable UTMI-pads clock: %d\n", ret);
0270         return ret;
0271     }
0272 
0273     spin_lock(&utmip_pad_lock);
0274 
0275     ret = reset_control_deassert(phy->pad_rst);
0276     if (ret) {
0277         dev_err(phy->u_phy.dev,
0278             "Failed to initialize UTMI-pads reset: %d\n", ret);
0279         goto unlock;
0280     }
0281 
0282     ret = reset_control_assert(phy->pad_rst);
0283     if (ret) {
0284         dev_err(phy->u_phy.dev,
0285             "Failed to assert UTMI-pads reset: %d\n", ret);
0286         goto unlock;
0287     }
0288 
0289     udelay(1);
0290 
0291     ret = reset_control_deassert(phy->pad_rst);
0292     if (ret)
0293         dev_err(phy->u_phy.dev,
0294             "Failed to deassert UTMI-pads reset: %d\n", ret);
0295 unlock:
0296     spin_unlock(&utmip_pad_lock);
0297 
0298     clk_disable_unprepare(phy->pad_clk);
0299 
0300     return ret;
0301 }
0302 
0303 static int utmip_pad_close(struct tegra_usb_phy *phy)
0304 {
0305     int ret;
0306 
0307     ret = clk_prepare_enable(phy->pad_clk);
0308     if (ret) {
0309         dev_err(phy->u_phy.dev,
0310             "Failed to enable UTMI-pads clock: %d\n", ret);
0311         return ret;
0312     }
0313 
0314     ret = reset_control_assert(phy->pad_rst);
0315     if (ret)
0316         dev_err(phy->u_phy.dev,
0317             "Failed to assert UTMI-pads reset: %d\n", ret);
0318 
0319     udelay(1);
0320 
0321     clk_disable_unprepare(phy->pad_clk);
0322 
0323     return ret;
0324 }
0325 
0326 static int utmip_pad_power_on(struct tegra_usb_phy *phy)
0327 {
0328     struct tegra_utmip_config *config = phy->config;
0329     void __iomem *base = phy->pad_regs;
0330     u32 val;
0331     int err;
0332 
0333     err = clk_prepare_enable(phy->pad_clk);
0334     if (err)
0335         return err;
0336 
0337     spin_lock(&utmip_pad_lock);
0338 
0339     if (utmip_pad_count++ == 0) {
0340         val = readl_relaxed(base + UTMIP_BIAS_CFG0);
0341         val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
0342 
0343         if (phy->soc_config->requires_extra_tuning_parameters) {
0344             val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
0345                 UTMIP_HSDISCON_LEVEL(~0) |
0346                 UTMIP_HSDISCON_LEVEL_MSB(~0));
0347 
0348             val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
0349             val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
0350             val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
0351         }
0352         writel_relaxed(val, base + UTMIP_BIAS_CFG0);
0353     }
0354 
0355     if (phy->pad_wakeup) {
0356         phy->pad_wakeup = false;
0357         utmip_pad_count--;
0358     }
0359 
0360     spin_unlock(&utmip_pad_lock);
0361 
0362     clk_disable_unprepare(phy->pad_clk);
0363 
0364     return 0;
0365 }
0366 
0367 static int utmip_pad_power_off(struct tegra_usb_phy *phy)
0368 {
0369     void __iomem *base = phy->pad_regs;
0370     u32 val;
0371     int ret;
0372 
0373     ret = clk_prepare_enable(phy->pad_clk);
0374     if (ret)
0375         return ret;
0376 
0377     spin_lock(&utmip_pad_lock);
0378 
0379     if (!utmip_pad_count) {
0380         dev_err(phy->u_phy.dev, "UTMIP pad already powered off\n");
0381         ret = -EINVAL;
0382         goto ulock;
0383     }
0384 
0385     /*
0386      * In accordance to TRM, OTG and Bias pad circuits could be turned off
0387      * to save power if wake is enabled, but the VBUS-change detection
0388      * method is board-specific and these circuits may need to be enabled
0389      * to generate wakeup event, hence we will just keep them both enabled.
0390      */
0391     if (phy->wakeup_enabled) {
0392         phy->pad_wakeup = true;
0393         utmip_pad_count++;
0394     }
0395 
0396     if (--utmip_pad_count == 0) {
0397         val = readl_relaxed(base + UTMIP_BIAS_CFG0);
0398         val |= UTMIP_OTGPD | UTMIP_BIASPD;
0399         writel_relaxed(val, base + UTMIP_BIAS_CFG0);
0400     }
0401 ulock:
0402     spin_unlock(&utmip_pad_lock);
0403 
0404     clk_disable_unprepare(phy->pad_clk);
0405 
0406     return ret;
0407 }
0408 
0409 static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
0410 {
0411     u32 tmp;
0412 
0413     return readl_relaxed_poll_timeout(reg, tmp, (tmp & mask) == result,
0414                       2000, 6000);
0415 }
0416 
0417 static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
0418 {
0419     void __iomem *base = phy->regs;
0420     u32 val;
0421 
0422     /*
0423      * The USB driver may have already initiated the phy clock
0424      * disable so wait to see if the clock turns off and if not
0425      * then proceed with gating the clock.
0426      */
0427     if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
0428         return;
0429 
0430     if (phy->is_legacy_phy) {
0431         val = readl_relaxed(base + USB_SUSP_CTRL);
0432         val |= USB_SUSP_SET;
0433         writel_relaxed(val, base + USB_SUSP_CTRL);
0434 
0435         usleep_range(10, 100);
0436 
0437         val = readl_relaxed(base + USB_SUSP_CTRL);
0438         val &= ~USB_SUSP_SET;
0439         writel_relaxed(val, base + USB_SUSP_CTRL);
0440     } else {
0441         set_phcd(phy, true);
0442     }
0443 
0444     if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0))
0445         dev_err(phy->u_phy.dev,
0446             "Timeout waiting for PHY to stabilize on disable\n");
0447 }
0448 
0449 static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
0450 {
0451     void __iomem *base = phy->regs;
0452     u32 val;
0453 
0454     /*
0455      * The USB driver may have already initiated the phy clock
0456      * enable so wait to see if the clock turns on and if not
0457      * then proceed with ungating the clock.
0458      */
0459     if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
0460                    USB_PHY_CLK_VALID) == 0)
0461         return;
0462 
0463     if (phy->is_legacy_phy) {
0464         val = readl_relaxed(base + USB_SUSP_CTRL);
0465         val |= USB_SUSP_CLR;
0466         writel_relaxed(val, base + USB_SUSP_CTRL);
0467 
0468         usleep_range(10, 100);
0469 
0470         val = readl_relaxed(base + USB_SUSP_CTRL);
0471         val &= ~USB_SUSP_CLR;
0472         writel_relaxed(val, base + USB_SUSP_CTRL);
0473     } else {
0474         set_phcd(phy, false);
0475     }
0476 
0477     if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
0478                    USB_PHY_CLK_VALID))
0479         dev_err(phy->u_phy.dev,
0480             "Timeout waiting for PHY to stabilize on enable\n");
0481 }
0482 
0483 static int utmi_phy_power_on(struct tegra_usb_phy *phy)
0484 {
0485     struct tegra_utmip_config *config = phy->config;
0486     void __iomem *base = phy->regs;
0487     u32 val;
0488     int err;
0489 
0490     val = readl_relaxed(base + USB_SUSP_CTRL);
0491     val |= UTMIP_RESET;
0492     writel_relaxed(val, base + USB_SUSP_CTRL);
0493 
0494     if (phy->is_legacy_phy) {
0495         val = readl_relaxed(base + USB1_LEGACY_CTRL);
0496         val |= USB1_NO_LEGACY_MODE;
0497         writel_relaxed(val, base + USB1_LEGACY_CTRL);
0498     }
0499 
0500     val = readl_relaxed(base + UTMIP_TX_CFG0);
0501     val |= UTMIP_FS_PREABMLE_J;
0502     writel_relaxed(val, base + UTMIP_TX_CFG0);
0503 
0504     val = readl_relaxed(base + UTMIP_HSRX_CFG0);
0505     val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
0506     val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
0507     val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
0508     writel_relaxed(val, base + UTMIP_HSRX_CFG0);
0509 
0510     val = readl_relaxed(base + UTMIP_HSRX_CFG1);
0511     val &= ~UTMIP_HS_SYNC_START_DLY(~0);
0512     val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
0513     writel_relaxed(val, base + UTMIP_HSRX_CFG1);
0514 
0515     val = readl_relaxed(base + UTMIP_DEBOUNCE_CFG0);
0516     val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
0517     val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
0518     writel_relaxed(val, base + UTMIP_DEBOUNCE_CFG0);
0519 
0520     val = readl_relaxed(base + UTMIP_MISC_CFG0);
0521     val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
0522     writel_relaxed(val, base + UTMIP_MISC_CFG0);
0523 
0524     if (!phy->soc_config->utmi_pll_config_in_car_module) {
0525         val = readl_relaxed(base + UTMIP_MISC_CFG1);
0526         val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
0527             UTMIP_PLLU_STABLE_COUNT(~0));
0528         val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
0529             UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
0530         writel_relaxed(val, base + UTMIP_MISC_CFG1);
0531 
0532         val = readl_relaxed(base + UTMIP_PLL_CFG1);
0533         val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
0534             UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
0535         val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
0536             UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
0537         writel_relaxed(val, base + UTMIP_PLL_CFG1);
0538     }
0539 
0540     val = readl_relaxed(base + USB_SUSP_CTRL);
0541     val &= ~USB_WAKE_ON_RESUME_EN;
0542     writel_relaxed(val, base + USB_SUSP_CTRL);
0543 
0544     if (phy->mode != USB_DR_MODE_HOST) {
0545         val = readl_relaxed(base + USB_SUSP_CTRL);
0546         val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
0547         writel_relaxed(val, base + USB_SUSP_CTRL);
0548 
0549         val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
0550         val &= ~VBUS_WAKEUP_WAKEUP_EN;
0551         val &= ~(ID_CHG_DET | VBUS_WAKEUP_CHG_DET);
0552         writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
0553 
0554         val = readl_relaxed(base + USB_PHY_VBUS_SENSORS);
0555         val &= ~(A_VBUS_VLD_WAKEUP_EN | A_SESS_VLD_WAKEUP_EN);
0556         val &= ~(B_SESS_VLD_WAKEUP_EN);
0557         writel_relaxed(val, base + USB_PHY_VBUS_SENSORS);
0558 
0559         val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
0560         val &= ~UTMIP_PD_CHRG;
0561         writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
0562     } else {
0563         val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
0564         val |= UTMIP_PD_CHRG;
0565         writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
0566     }
0567 
0568     err = utmip_pad_power_on(phy);
0569     if (err)
0570         return err;
0571 
0572     val = readl_relaxed(base + UTMIP_XCVR_CFG0);
0573     val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
0574          UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
0575          UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
0576          UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
0577 
0578     if (!config->xcvr_setup_use_fuses) {
0579         val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
0580         val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
0581     }
0582     val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
0583     val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
0584 
0585     if (phy->soc_config->requires_extra_tuning_parameters) {
0586         val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
0587         val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
0588         val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
0589     }
0590     writel_relaxed(val, base + UTMIP_XCVR_CFG0);
0591 
0592     val = readl_relaxed(base + UTMIP_XCVR_CFG1);
0593     val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
0594          UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
0595     val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
0596     writel_relaxed(val, base + UTMIP_XCVR_CFG1);
0597 
0598     val = readl_relaxed(base + UTMIP_BIAS_CFG1);
0599     val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
0600     val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
0601     writel_relaxed(val, base + UTMIP_BIAS_CFG1);
0602 
0603     val = readl_relaxed(base + UTMIP_SPARE_CFG0);
0604     if (config->xcvr_setup_use_fuses)
0605         val |= FUSE_SETUP_SEL;
0606     else
0607         val &= ~FUSE_SETUP_SEL;
0608     writel_relaxed(val, base + UTMIP_SPARE_CFG0);
0609 
0610     if (!phy->is_legacy_phy) {
0611         val = readl_relaxed(base + USB_SUSP_CTRL);
0612         val |= UTMIP_PHY_ENABLE;
0613         writel_relaxed(val, base + USB_SUSP_CTRL);
0614     }
0615 
0616     val = readl_relaxed(base + USB_SUSP_CTRL);
0617     val &= ~UTMIP_RESET;
0618     writel_relaxed(val, base + USB_SUSP_CTRL);
0619 
0620     if (phy->is_legacy_phy) {
0621         val = readl_relaxed(base + USB1_LEGACY_CTRL);
0622         val &= ~USB1_VBUS_SENSE_CTL_MASK;
0623         val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
0624         writel_relaxed(val, base + USB1_LEGACY_CTRL);
0625 
0626         val = readl_relaxed(base + USB_SUSP_CTRL);
0627         val &= ~USB_SUSP_SET;
0628         writel_relaxed(val, base + USB_SUSP_CTRL);
0629     }
0630 
0631     utmi_phy_clk_enable(phy);
0632 
0633     if (phy->soc_config->requires_usbmode_setup) {
0634         val = readl_relaxed(base + USB_USBMODE);
0635         val &= ~USB_USBMODE_MASK;
0636         if (phy->mode == USB_DR_MODE_HOST)
0637             val |= USB_USBMODE_HOST;
0638         else
0639             val |= USB_USBMODE_DEVICE;
0640         writel_relaxed(val, base + USB_USBMODE);
0641     }
0642 
0643     if (!phy->is_legacy_phy)
0644         set_pts(phy, 0);
0645 
0646     return 0;
0647 }
0648 
0649 static int utmi_phy_power_off(struct tegra_usb_phy *phy)
0650 {
0651     void __iomem *base = phy->regs;
0652     u32 val;
0653 
0654     /*
0655      * Give hardware time to settle down after VBUS disconnection,
0656      * otherwise PHY will immediately wake up from suspend.
0657      */
0658     if (phy->wakeup_enabled && phy->mode != USB_DR_MODE_HOST)
0659         readl_relaxed_poll_timeout(base + USB_PHY_VBUS_WAKEUP_ID,
0660                        val, !(val & VBUS_WAKEUP_STS),
0661                        5000, 100000);
0662 
0663     utmi_phy_clk_disable(phy);
0664 
0665     /* PHY won't resume if reset is asserted */
0666     if (!phy->wakeup_enabled) {
0667         val = readl_relaxed(base + USB_SUSP_CTRL);
0668         val |= UTMIP_RESET;
0669         writel_relaxed(val, base + USB_SUSP_CTRL);
0670     }
0671 
0672     val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
0673     val |= UTMIP_PD_CHRG;
0674     writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
0675 
0676     if (!phy->wakeup_enabled) {
0677         val = readl_relaxed(base + UTMIP_XCVR_CFG0);
0678         val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
0679                UTMIP_FORCE_PDZI_POWERDOWN;
0680         writel_relaxed(val, base + UTMIP_XCVR_CFG0);
0681     }
0682 
0683     val = readl_relaxed(base + UTMIP_XCVR_CFG1);
0684     val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
0685            UTMIP_FORCE_PDDR_POWERDOWN;
0686     writel_relaxed(val, base + UTMIP_XCVR_CFG1);
0687 
0688     if (phy->wakeup_enabled) {
0689         val = readl_relaxed(base + USB_SUSP_CTRL);
0690         val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
0691         val |= USB_WAKEUP_DEBOUNCE_COUNT(5);
0692         val |= USB_WAKE_ON_RESUME_EN;
0693         writel_relaxed(val, base + USB_SUSP_CTRL);
0694 
0695         /*
0696          * Ask VBUS sensor to generate wake event once cable is
0697          * connected.
0698          */
0699         if (phy->mode != USB_DR_MODE_HOST) {
0700             val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
0701             val |= VBUS_WAKEUP_WAKEUP_EN;
0702             val &= ~(ID_CHG_DET | VBUS_WAKEUP_CHG_DET);
0703             writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
0704 
0705             val = readl_relaxed(base + USB_PHY_VBUS_SENSORS);
0706             val |= A_VBUS_VLD_WAKEUP_EN;
0707             writel_relaxed(val, base + USB_PHY_VBUS_SENSORS);
0708         }
0709     }
0710 
0711     return utmip_pad_power_off(phy);
0712 }
0713 
0714 static void utmi_phy_preresume(struct tegra_usb_phy *phy)
0715 {
0716     void __iomem *base = phy->regs;
0717     u32 val;
0718 
0719     val = readl_relaxed(base + UTMIP_TX_CFG0);
0720     val |= UTMIP_HS_DISCON_DISABLE;
0721     writel_relaxed(val, base + UTMIP_TX_CFG0);
0722 }
0723 
0724 static void utmi_phy_postresume(struct tegra_usb_phy *phy)
0725 {
0726     void __iomem *base = phy->regs;
0727     u32 val;
0728 
0729     val = readl_relaxed(base + UTMIP_TX_CFG0);
0730     val &= ~UTMIP_HS_DISCON_DISABLE;
0731     writel_relaxed(val, base + UTMIP_TX_CFG0);
0732 }
0733 
0734 static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
0735                    enum tegra_usb_phy_port_speed port_speed)
0736 {
0737     void __iomem *base = phy->regs;
0738     u32 val;
0739 
0740     val = readl_relaxed(base + UTMIP_MISC_CFG0);
0741     val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
0742     if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
0743         val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
0744     else
0745         val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
0746     writel_relaxed(val, base + UTMIP_MISC_CFG0);
0747     usleep_range(1, 10);
0748 
0749     val = readl_relaxed(base + UTMIP_MISC_CFG0);
0750     val |= UTMIP_DPDM_OBSERVE;
0751     writel_relaxed(val, base + UTMIP_MISC_CFG0);
0752     usleep_range(10, 100);
0753 }
0754 
0755 static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
0756 {
0757     void __iomem *base = phy->regs;
0758     u32 val;
0759 
0760     val = readl_relaxed(base + UTMIP_MISC_CFG0);
0761     val &= ~UTMIP_DPDM_OBSERVE;
0762     writel_relaxed(val, base + UTMIP_MISC_CFG0);
0763     usleep_range(10, 100);
0764 }
0765 
0766 static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
0767 {
0768     void __iomem *base = phy->regs;
0769     u32 val;
0770     int err;
0771 
0772     gpiod_set_value_cansleep(phy->reset_gpio, 1);
0773 
0774     err = clk_prepare_enable(phy->clk);
0775     if (err)
0776         return err;
0777 
0778     usleep_range(5000, 6000);
0779 
0780     gpiod_set_value_cansleep(phy->reset_gpio, 0);
0781 
0782     usleep_range(1000, 2000);
0783 
0784     val = readl_relaxed(base + USB_SUSP_CTRL);
0785     val |= UHSIC_RESET;
0786     writel_relaxed(val, base + USB_SUSP_CTRL);
0787 
0788     val = readl_relaxed(base + ULPI_TIMING_CTRL_0);
0789     val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
0790     writel_relaxed(val, base + ULPI_TIMING_CTRL_0);
0791 
0792     val = readl_relaxed(base + USB_SUSP_CTRL);
0793     val |= ULPI_PHY_ENABLE;
0794     writel_relaxed(val, base + USB_SUSP_CTRL);
0795 
0796     val = 0;
0797     writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
0798 
0799     val |= ULPI_DATA_TRIMMER_SEL(4);
0800     val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
0801     val |= ULPI_DIR_TRIMMER_SEL(4);
0802     writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
0803     usleep_range(10, 100);
0804 
0805     val |= ULPI_DATA_TRIMMER_LOAD;
0806     val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
0807     val |= ULPI_DIR_TRIMMER_LOAD;
0808     writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
0809 
0810     /* Fix VbusInvalid due to floating VBUS */
0811     err = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
0812     if (err) {
0813         dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
0814         goto disable_clk;
0815     }
0816 
0817     err = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
0818     if (err) {
0819         dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
0820         goto disable_clk;
0821     }
0822 
0823     val = readl_relaxed(base + USB_SUSP_CTRL);
0824     val |= USB_SUSP_CLR;
0825     writel_relaxed(val, base + USB_SUSP_CTRL);
0826     usleep_range(100, 1000);
0827 
0828     val = readl_relaxed(base + USB_SUSP_CTRL);
0829     val &= ~USB_SUSP_CLR;
0830     writel_relaxed(val, base + USB_SUSP_CTRL);
0831 
0832     return 0;
0833 
0834 disable_clk:
0835     clk_disable_unprepare(phy->clk);
0836 
0837     return err;
0838 }
0839 
0840 static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
0841 {
0842     gpiod_set_value_cansleep(phy->reset_gpio, 1);
0843     usleep_range(5000, 6000);
0844     clk_disable_unprepare(phy->clk);
0845 
0846     /*
0847      * Wakeup currently unimplemented for ULPI, thus PHY needs to be
0848      * force-resumed.
0849      */
0850     if (WARN_ON_ONCE(phy->wakeup_enabled)) {
0851         ulpi_phy_power_on(phy);
0852         return -EOPNOTSUPP;
0853     }
0854 
0855     return 0;
0856 }
0857 
0858 static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
0859 {
0860     int err;
0861 
0862     if (phy->powered_on)
0863         return 0;
0864 
0865     if (phy->is_ulpi_phy)
0866         err = ulpi_phy_power_on(phy);
0867     else
0868         err = utmi_phy_power_on(phy);
0869     if (err)
0870         return err;
0871 
0872     phy->powered_on = true;
0873 
0874     /* Let PHY settle down */
0875     usleep_range(2000, 2500);
0876 
0877     return 0;
0878 }
0879 
0880 static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
0881 {
0882     int err;
0883 
0884     if (!phy->powered_on)
0885         return 0;
0886 
0887     if (phy->is_ulpi_phy)
0888         err = ulpi_phy_power_off(phy);
0889     else
0890         err = utmi_phy_power_off(phy);
0891     if (err)
0892         return err;
0893 
0894     phy->powered_on = false;
0895 
0896     return 0;
0897 }
0898 
0899 static void tegra_usb_phy_shutdown(struct usb_phy *u_phy)
0900 {
0901     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
0902 
0903     if (WARN_ON(!phy->freq))
0904         return;
0905 
0906     usb_phy_set_wakeup(u_phy, false);
0907     tegra_usb_phy_power_off(phy);
0908 
0909     if (!phy->is_ulpi_phy)
0910         utmip_pad_close(phy);
0911 
0912     regulator_disable(phy->vbus);
0913     clk_disable_unprepare(phy->pll_u);
0914 
0915     phy->freq = NULL;
0916 }
0917 
0918 static irqreturn_t tegra_usb_phy_isr(int irq, void *data)
0919 {
0920     u32 val, int_mask = ID_CHG_DET | VBUS_WAKEUP_CHG_DET;
0921     struct tegra_usb_phy *phy = data;
0922     void __iomem *base = phy->regs;
0923 
0924     /*
0925      * The PHY interrupt also wakes the USB controller driver since
0926      * interrupt is shared. We don't do anything in the PHY driver,
0927      * so just clear the interrupt.
0928      */
0929     val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
0930     writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
0931 
0932     return val & int_mask ? IRQ_HANDLED : IRQ_NONE;
0933 }
0934 
0935 static int tegra_usb_phy_set_wakeup(struct usb_phy *u_phy, bool enable)
0936 {
0937     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
0938     void __iomem *base = phy->regs;
0939     int ret = 0;
0940     u32 val;
0941 
0942     if (phy->wakeup_enabled && phy->mode != USB_DR_MODE_HOST &&
0943         phy->irq > 0) {
0944         disable_irq(phy->irq);
0945 
0946         val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
0947         val &= ~(ID_INT_EN | VBUS_WAKEUP_INT_EN);
0948         writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
0949 
0950         enable_irq(phy->irq);
0951 
0952         free_irq(phy->irq, phy);
0953 
0954         phy->wakeup_enabled = false;
0955     }
0956 
0957     if (enable && phy->mode != USB_DR_MODE_HOST && phy->irq > 0) {
0958         ret = request_irq(phy->irq, tegra_usb_phy_isr, IRQF_SHARED,
0959                   dev_name(phy->u_phy.dev), phy);
0960         if (!ret) {
0961             disable_irq(phy->irq);
0962 
0963             /*
0964              * USB clock will be resumed once wake event will be
0965              * generated.  The ID-change event requires to have
0966              * interrupts enabled, otherwise it won't be generated.
0967              */
0968             val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
0969             val |= ID_INT_EN | VBUS_WAKEUP_INT_EN;
0970             writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
0971 
0972             enable_irq(phy->irq);
0973         } else {
0974             dev_err(phy->u_phy.dev,
0975                 "Failed to request interrupt: %d", ret);
0976             enable = false;
0977         }
0978     }
0979 
0980     phy->wakeup_enabled = enable;
0981 
0982     return ret;
0983 }
0984 
0985 static int tegra_usb_phy_set_suspend(struct usb_phy *u_phy, int suspend)
0986 {
0987     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
0988     int ret;
0989 
0990     if (WARN_ON(!phy->freq))
0991         return -EINVAL;
0992 
0993     /*
0994      * PHY is sharing IRQ with the CI driver, hence here we either
0995      * disable interrupt for both PHY and CI or for CI only.  The
0996      * interrupt needs to be disabled while hardware is reprogrammed
0997      * because interrupt touches the programmed registers, and thus,
0998      * there could be a race condition.
0999      */
1000     if (phy->irq > 0)
1001         disable_irq(phy->irq);
1002 
1003     if (suspend)
1004         ret = tegra_usb_phy_power_off(phy);
1005     else
1006         ret = tegra_usb_phy_power_on(phy);
1007 
1008     if (phy->irq > 0)
1009         enable_irq(phy->irq);
1010 
1011     return ret;
1012 }
1013 
1014 static int tegra_usb_phy_configure_pmc(struct tegra_usb_phy *phy)
1015 {
1016     int err, val = 0;
1017 
1018     /* older device-trees don't have PMC regmap */
1019     if (!phy->pmc_regmap)
1020         return 0;
1021 
1022     /*
1023      * Tegra20 has a different layout of PMC USB register bits and AO is
1024      * enabled by default after system reset on Tegra20, so assume nothing
1025      * to do on Tegra20.
1026      */
1027     if (!phy->soc_config->requires_pmc_ao_power_up)
1028         return 0;
1029 
1030     /* enable VBUS wake-up detector */
1031     if (phy->mode != USB_DR_MODE_HOST)
1032         val |= VBUS_WAKEUP_PD_P0 << phy->instance * 4;
1033 
1034     /* enable ID-pin ACC detector for OTG mode switching */
1035     if (phy->mode == USB_DR_MODE_OTG)
1036         val |= ID_PD_P0 << phy->instance * 4;
1037 
1038     /* disable detectors to reset them */
1039     err = regmap_set_bits(phy->pmc_regmap, PMC_USB_AO, val);
1040     if (err) {
1041         dev_err(phy->u_phy.dev, "Failed to disable PMC AO: %d\n", err);
1042         return err;
1043     }
1044 
1045     usleep_range(10, 100);
1046 
1047     /* enable detectors */
1048     err = regmap_clear_bits(phy->pmc_regmap, PMC_USB_AO, val);
1049     if (err) {
1050         dev_err(phy->u_phy.dev, "Failed to enable PMC AO: %d\n", err);
1051         return err;
1052     }
1053 
1054     /* detectors starts to work after 10ms */
1055     usleep_range(10000, 15000);
1056 
1057     return 0;
1058 }
1059 
1060 static int tegra_usb_phy_init(struct usb_phy *u_phy)
1061 {
1062     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
1063     unsigned long parent_rate;
1064     unsigned int i;
1065     int err;
1066 
1067     if (WARN_ON(phy->freq))
1068         return 0;
1069 
1070     err = clk_prepare_enable(phy->pll_u);
1071     if (err)
1072         return err;
1073 
1074     parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
1075     for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
1076         if (tegra_freq_table[i].freq == parent_rate) {
1077             phy->freq = &tegra_freq_table[i];
1078             break;
1079         }
1080     }
1081     if (!phy->freq) {
1082         dev_err(phy->u_phy.dev, "Invalid pll_u parent rate %ld\n",
1083             parent_rate);
1084         err = -EINVAL;
1085         goto disable_clk;
1086     }
1087 
1088     err = regulator_enable(phy->vbus);
1089     if (err) {
1090         dev_err(phy->u_phy.dev,
1091             "Failed to enable USB VBUS regulator: %d\n", err);
1092         goto disable_clk;
1093     }
1094 
1095     if (!phy->is_ulpi_phy) {
1096         err = utmip_pad_open(phy);
1097         if (err)
1098             goto disable_vbus;
1099     }
1100 
1101     err = tegra_usb_phy_configure_pmc(phy);
1102     if (err)
1103         goto close_phy;
1104 
1105     err = tegra_usb_phy_power_on(phy);
1106     if (err)
1107         goto close_phy;
1108 
1109     return 0;
1110 
1111 close_phy:
1112     if (!phy->is_ulpi_phy)
1113         utmip_pad_close(phy);
1114 
1115 disable_vbus:
1116     regulator_disable(phy->vbus);
1117 
1118 disable_clk:
1119     clk_disable_unprepare(phy->pll_u);
1120 
1121     phy->freq = NULL;
1122 
1123     return err;
1124 }
1125 
1126 void tegra_usb_phy_preresume(struct usb_phy *u_phy)
1127 {
1128     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
1129 
1130     if (!phy->is_ulpi_phy)
1131         utmi_phy_preresume(phy);
1132 }
1133 EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
1134 
1135 void tegra_usb_phy_postresume(struct usb_phy *u_phy)
1136 {
1137     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
1138 
1139     if (!phy->is_ulpi_phy)
1140         utmi_phy_postresume(phy);
1141 }
1142 EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
1143 
1144 void tegra_ehci_phy_restore_start(struct usb_phy *u_phy,
1145                   enum tegra_usb_phy_port_speed port_speed)
1146 {
1147     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
1148 
1149     if (!phy->is_ulpi_phy)
1150         utmi_phy_restore_start(phy, port_speed);
1151 }
1152 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
1153 
1154 void tegra_ehci_phy_restore_end(struct usb_phy *u_phy)
1155 {
1156     struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
1157 
1158     if (!phy->is_ulpi_phy)
1159         utmi_phy_restore_end(phy);
1160 }
1161 EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
1162 
1163 static int read_utmi_param(struct platform_device *pdev, const char *param,
1164                u8 *dest)
1165 {
1166     u32 value;
1167     int err;
1168 
1169     err = of_property_read_u32(pdev->dev.of_node, param, &value);
1170     if (err)
1171         dev_err(&pdev->dev,
1172             "Failed to read USB UTMI parameter %s: %d\n",
1173             param, err);
1174     else
1175         *dest = value;
1176 
1177     return err;
1178 }
1179 
1180 static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
1181               struct platform_device *pdev)
1182 {
1183     struct tegra_utmip_config *config;
1184     struct resource *res;
1185     int err;
1186 
1187     tegra_phy->is_ulpi_phy = false;
1188 
1189     res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1190     if (!res) {
1191         dev_err(&pdev->dev, "Failed to get UTMI pad regs\n");
1192         return  -ENXIO;
1193     }
1194 
1195     /*
1196      * Note that UTMI pad registers are shared by all PHYs, therefore
1197      * devm_platform_ioremap_resource() can't be used here.
1198      */
1199     tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
1200                        resource_size(res));
1201     if (!tegra_phy->pad_regs) {
1202         dev_err(&pdev->dev, "Failed to remap UTMI pad regs\n");
1203         return -ENOMEM;
1204     }
1205 
1206     tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
1207                      GFP_KERNEL);
1208     if (!tegra_phy->config)
1209         return -ENOMEM;
1210 
1211     config = tegra_phy->config;
1212 
1213     err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
1214                   &config->hssync_start_delay);
1215     if (err)
1216         return err;
1217 
1218     err = read_utmi_param(pdev, "nvidia,elastic-limit",
1219                   &config->elastic_limit);
1220     if (err)
1221         return err;
1222 
1223     err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
1224                   &config->idle_wait_delay);
1225     if (err)
1226         return err;
1227 
1228     err = read_utmi_param(pdev, "nvidia,term-range-adj",
1229                   &config->term_range_adj);
1230     if (err)
1231         return err;
1232 
1233     err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
1234                   &config->xcvr_lsfslew);
1235     if (err)
1236         return err;
1237 
1238     err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
1239                   &config->xcvr_lsrslew);
1240     if (err)
1241         return err;
1242 
1243     if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
1244         err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
1245                       &config->xcvr_hsslew);
1246         if (err)
1247             return err;
1248 
1249         err = read_utmi_param(pdev, "nvidia,hssquelch-level",
1250                       &config->hssquelch_level);
1251         if (err)
1252             return err;
1253 
1254         err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
1255                       &config->hsdiscon_level);
1256         if (err)
1257             return err;
1258     }
1259 
1260     config->xcvr_setup_use_fuses = of_property_read_bool(
1261         pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
1262 
1263     if (!config->xcvr_setup_use_fuses) {
1264         err = read_utmi_param(pdev, "nvidia,xcvr-setup",
1265                       &config->xcvr_setup);
1266         if (err)
1267             return err;
1268     }
1269 
1270     return 0;
1271 }
1272 
1273 static void tegra_usb_phy_put_pmc_device(void *dev)
1274 {
1275     put_device(dev);
1276 }
1277 
1278 static int tegra_usb_phy_parse_pmc(struct device *dev,
1279                    struct tegra_usb_phy *phy)
1280 {
1281     struct platform_device *pmc_pdev;
1282     struct of_phandle_args args;
1283     int err;
1284 
1285     err = of_parse_phandle_with_fixed_args(dev->of_node, "nvidia,pmc",
1286                            1, 0, &args);
1287     if (err) {
1288         if (err != -ENOENT)
1289             return err;
1290 
1291         dev_warn_once(dev, "nvidia,pmc is missing, please update your device-tree\n");
1292         return 0;
1293     }
1294 
1295     pmc_pdev = of_find_device_by_node(args.np);
1296     of_node_put(args.np);
1297     if (!pmc_pdev)
1298         return -ENODEV;
1299 
1300     err = devm_add_action_or_reset(dev, tegra_usb_phy_put_pmc_device,
1301                        &pmc_pdev->dev);
1302     if (err)
1303         return err;
1304 
1305     if (!platform_get_drvdata(pmc_pdev))
1306         return -EPROBE_DEFER;
1307 
1308     phy->pmc_regmap = dev_get_regmap(&pmc_pdev->dev, "usb_sleepwalk");
1309     if (!phy->pmc_regmap)
1310         return -EINVAL;
1311 
1312     phy->instance = args.args[0];
1313 
1314     return 0;
1315 }
1316 
1317 static const struct tegra_phy_soc_config tegra20_soc_config = {
1318     .utmi_pll_config_in_car_module = false,
1319     .has_hostpc = false,
1320     .requires_usbmode_setup = false,
1321     .requires_extra_tuning_parameters = false,
1322     .requires_pmc_ao_power_up = false,
1323 };
1324 
1325 static const struct tegra_phy_soc_config tegra30_soc_config = {
1326     .utmi_pll_config_in_car_module = true,
1327     .has_hostpc = true,
1328     .requires_usbmode_setup = true,
1329     .requires_extra_tuning_parameters = true,
1330     .requires_pmc_ao_power_up = true,
1331 };
1332 
1333 static const struct of_device_id tegra_usb_phy_id_table[] = {
1334     { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
1335     { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
1336     { },
1337 };
1338 MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
1339 
1340 static int tegra_usb_phy_probe(struct platform_device *pdev)
1341 {
1342     struct device_node *np = pdev->dev.of_node;
1343     struct tegra_usb_phy *tegra_phy;
1344     enum usb_phy_interface phy_type;
1345     struct reset_control *reset;
1346     struct gpio_desc *gpiod;
1347     struct resource *res;
1348     struct usb_phy *phy;
1349     int err;
1350 
1351     tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
1352     if (!tegra_phy)
1353         return -ENOMEM;
1354 
1355     tegra_phy->soc_config = of_device_get_match_data(&pdev->dev);
1356     tegra_phy->irq = platform_get_irq_optional(pdev, 0);
1357 
1358     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1359     if (!res) {
1360         dev_err(&pdev->dev, "Failed to get I/O memory\n");
1361         return  -ENXIO;
1362     }
1363 
1364     /*
1365      * Note that PHY and USB controller are using shared registers,
1366      * therefore devm_platform_ioremap_resource() can't be used here.
1367      */
1368     tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
1369                        resource_size(res));
1370     if (!tegra_phy->regs) {
1371         dev_err(&pdev->dev, "Failed to remap I/O memory\n");
1372         return -ENOMEM;
1373     }
1374 
1375     tegra_phy->is_legacy_phy =
1376         of_property_read_bool(np, "nvidia,has-legacy-mode");
1377 
1378     if (of_find_property(np, "dr_mode", NULL))
1379         tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
1380     else
1381         tegra_phy->mode = USB_DR_MODE_HOST;
1382 
1383     if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
1384         dev_err(&pdev->dev, "dr_mode is invalid\n");
1385         return -EINVAL;
1386     }
1387 
1388     /* On some boards, the VBUS regulator doesn't need to be controlled */
1389     tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
1390     if (IS_ERR(tegra_phy->vbus))
1391         return PTR_ERR(tegra_phy->vbus);
1392 
1393     tegra_phy->pll_u = devm_clk_get(&pdev->dev, "pll_u");
1394     err = PTR_ERR_OR_ZERO(tegra_phy->pll_u);
1395     if (err) {
1396         dev_err(&pdev->dev, "Failed to get pll_u clock: %d\n", err);
1397         return err;
1398     }
1399 
1400     err = tegra_usb_phy_parse_pmc(&pdev->dev, tegra_phy);
1401     if (err) {
1402         dev_err_probe(&pdev->dev, err, "Failed to get PMC regmap\n");
1403         return err;
1404     }
1405 
1406     phy_type = of_usb_get_phy_mode(np);
1407     switch (phy_type) {
1408     case USBPHY_INTERFACE_MODE_UTMI:
1409         err = utmi_phy_probe(tegra_phy, pdev);
1410         if (err)
1411             return err;
1412 
1413         tegra_phy->pad_clk = devm_clk_get(&pdev->dev, "utmi-pads");
1414         err = PTR_ERR_OR_ZERO(tegra_phy->pad_clk);
1415         if (err) {
1416             dev_err(&pdev->dev,
1417                 "Failed to get UTMIP pad clock: %d\n", err);
1418             return err;
1419         }
1420 
1421         reset = devm_reset_control_get_optional_shared(&pdev->dev,
1422                                    "utmi-pads");
1423         err = PTR_ERR_OR_ZERO(reset);
1424         if (err) {
1425             dev_err(&pdev->dev,
1426                 "Failed to get UTMI-pads reset: %d\n", err);
1427             return err;
1428         }
1429         tegra_phy->pad_rst = reset;
1430         break;
1431 
1432     case USBPHY_INTERFACE_MODE_ULPI:
1433         tegra_phy->is_ulpi_phy = true;
1434 
1435         tegra_phy->clk = devm_clk_get(&pdev->dev, "ulpi-link");
1436         err = PTR_ERR_OR_ZERO(tegra_phy->clk);
1437         if (err) {
1438             dev_err(&pdev->dev,
1439                 "Failed to get ULPI clock: %d\n", err);
1440             return err;
1441         }
1442 
1443         gpiod = devm_gpiod_get_from_of_node(&pdev->dev, np,
1444                             "nvidia,phy-reset-gpio",
1445                             0, GPIOD_OUT_HIGH,
1446                             "ulpi_phy_reset_b");
1447         err = PTR_ERR_OR_ZERO(gpiod);
1448         if (err) {
1449             dev_err(&pdev->dev,
1450                 "Request failed for reset GPIO: %d\n", err);
1451             return err;
1452         }
1453         tegra_phy->reset_gpio = gpiod;
1454 
1455         phy = devm_otg_ulpi_create(&pdev->dev,
1456                        &ulpi_viewport_access_ops, 0);
1457         if (!phy) {
1458             dev_err(&pdev->dev, "Failed to create ULPI OTG\n");
1459             return -ENOMEM;
1460         }
1461 
1462         tegra_phy->ulpi = phy;
1463         tegra_phy->ulpi->io_priv = tegra_phy->regs + ULPI_VIEWPORT;
1464         break;
1465 
1466     default:
1467         dev_err(&pdev->dev, "phy_type %u is invalid or unsupported\n",
1468             phy_type);
1469         return -EINVAL;
1470     }
1471 
1472     tegra_phy->u_phy.dev = &pdev->dev;
1473     tegra_phy->u_phy.init = tegra_usb_phy_init;
1474     tegra_phy->u_phy.shutdown = tegra_usb_phy_shutdown;
1475     tegra_phy->u_phy.set_wakeup = tegra_usb_phy_set_wakeup;
1476     tegra_phy->u_phy.set_suspend = tegra_usb_phy_set_suspend;
1477 
1478     platform_set_drvdata(pdev, tegra_phy);
1479 
1480     return usb_add_phy_dev(&tegra_phy->u_phy);
1481 }
1482 
1483 static int tegra_usb_phy_remove(struct platform_device *pdev)
1484 {
1485     struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
1486 
1487     usb_remove_phy(&tegra_phy->u_phy);
1488 
1489     return 0;
1490 }
1491 
1492 static struct platform_driver tegra_usb_phy_driver = {
1493     .probe      = tegra_usb_phy_probe,
1494     .remove     = tegra_usb_phy_remove,
1495     .driver     = {
1496         .name   = "tegra-phy",
1497         .of_match_table = tegra_usb_phy_id_table,
1498     },
1499 };
1500 module_platform_driver(tegra_usb_phy_driver);
1501 
1502 MODULE_DESCRIPTION("Tegra USB PHY driver");
1503 MODULE_LICENSE("GPL v2");