blob: 9adbcdf8d3a18b4c752eae1dfbb33f65e9e9c87a [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Benoit Goby91525d02011-03-09 16:28:55 -08002/*
Benoit Goby91525d02011-03-09 16:28:55 -08003 * Copyright (C) 2010 Google, Inc.
Venu Byravarasu2d22b422013-05-16 19:43:02 +05304 * Copyright (C) 2013 NVIDIA Corporation
Benoit Goby91525d02011-03-09 16:28:55 -08005 *
6 * Author:
7 * Erik Gilling <konkers@google.com>
8 * Benoit Goby <benoit@android.com>
Venu Byravarasu2d22b422013-05-16 19:43:02 +05309 * Venu Byravarasu <vbyravarasu@nvidia.com>
Benoit Goby91525d02011-03-09 16:28:55 -080010 */
11
Benoit Goby91525d02011-03-09 16:28:55 -080012#include <linux/delay.h>
Benoit Goby91525d02011-03-09 16:28:55 -080013#include <linux/err.h>
Arnd Bergmann4265cbf2012-03-02 15:58:42 -050014#include <linux/export.h>
Benoit Goby91525d02011-03-09 16:28:55 -080015#include <linux/gpio.h>
Dmitry Osipenko5bb69852020-01-06 04:34:03 +030016#include <linux/iopoll.h>
17#include <linux/module.h>
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +000018#include <linux/of.h>
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030019#include <linux/of_device.h>
Stephen Warrenaa607eb2012-04-12 15:46:49 -060020#include <linux/of_gpio.h>
Dmitry Osipenko5bb69852020-01-06 04:34:03 +030021#include <linux/platform_device.h>
22#include <linux/resource.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +030026#include <linux/regulator/consumer.h>
Benoit Goby91525d02011-03-09 16:28:55 -080027
Dmitry Osipenko5bb69852020-01-06 04:34:03 +030028#include <linux/usb/ehci_def.h>
29#include <linux/usb/of.h>
30#include <linux/usb/tegra_usb_phy.h>
31#include <linux/usb/ulpi.h>
32
Dmitry Osipenko545592e2020-01-06 04:34:02 +030033#define ULPI_VIEWPORT 0x170
Benoit Goby91525d02011-03-09 16:28:55 -080034
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030035/* PORTSC PTS/PHCD bits, Tegra20 only */
Dmitry Osipenko545592e2020-01-06 04:34:02 +030036#define TEGRA_USB_PORTSC1 0x184
37#define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
38#define TEGRA_USB_PORTSC1_PHCD BIT(23)
Stephen Warren91a687d2013-06-13 11:24:11 -060039
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030040/* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
Dmitry Osipenko545592e2020-01-06 04:34:02 +030041#define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
42#define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
43#define TEGRA_USB_HOSTPC1_DEVLC_PHCD BIT(22)
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030044
Stephen Warren91a687d2013-06-13 11:24:11 -060045/* Bits of PORTSC1, which will get cleared by writing 1 into them */
46#define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
47
Dmitry Osipenko545592e2020-01-06 04:34:02 +030048#define USB_SUSP_CTRL 0x400
49#define USB_WAKE_ON_CNNT_EN_DEV BIT(3)
50#define USB_WAKE_ON_DISCON_EN_DEV BIT(4)
51#define USB_SUSP_CLR BIT(5)
52#define USB_PHY_CLK_VALID BIT(7)
53#define UTMIP_RESET BIT(11)
54#define UHSIC_RESET BIT(11)
55#define UTMIP_PHY_ENABLE BIT(12)
56#define ULPI_PHY_ENABLE BIT(13)
57#define USB_SUSP_SET BIT(14)
58#define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
Benoit Goby91525d02011-03-09 16:28:55 -080059
Dmitry Osipenko545592e2020-01-06 04:34:02 +030060#define USB1_LEGACY_CTRL 0x410
61#define USB1_NO_LEGACY_MODE BIT(0)
Benoit Goby91525d02011-03-09 16:28:55 -080062#define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
63#define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
64#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
65 (1 << 1)
66#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
67#define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
68
Dmitry Osipenko545592e2020-01-06 04:34:02 +030069#define ULPI_TIMING_CTRL_0 0x424
70#define ULPI_OUTPUT_PINMUX_BYP BIT(10)
71#define ULPI_CLKOUT_PINMUX_BYP BIT(11)
Benoit Goby91525d02011-03-09 16:28:55 -080072
Dmitry Osipenko545592e2020-01-06 04:34:02 +030073#define ULPI_TIMING_CTRL_1 0x428
74#define ULPI_DATA_TRIMMER_LOAD BIT(0)
75#define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
76#define ULPI_STPDIRNXT_TRIMMER_LOAD BIT(16)
77#define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
78#define ULPI_DIR_TRIMMER_LOAD BIT(24)
79#define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
Benoit Goby91525d02011-03-09 16:28:55 -080080
Dmitry Osipenko545592e2020-01-06 04:34:02 +030081#define UTMIP_PLL_CFG1 0x804
Benoit Goby91525d02011-03-09 16:28:55 -080082#define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
83#define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
84
Dmitry Osipenko545592e2020-01-06 04:34:02 +030085#define UTMIP_XCVR_CFG0 0x808
Benoit Goby91525d02011-03-09 16:28:55 -080086#define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +030087#define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
Benoit Goby91525d02011-03-09 16:28:55 -080088#define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
89#define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
Dmitry Osipenko545592e2020-01-06 04:34:02 +030090#define UTMIP_FORCE_PD_POWERDOWN BIT(14)
91#define UTMIP_FORCE_PD2_POWERDOWN BIT(16)
92#define UTMIP_FORCE_PDZI_POWERDOWN BIT(18)
93#define UTMIP_XCVR_LSBIAS_SEL BIT(21)
Tuomas Tynkkynene497a242013-08-12 16:06:53 +030094#define UTMIP_XCVR_HSSLEW(x) (((x) & 0x3) << 4)
95#define UTMIP_XCVR_HSSLEW_MSB(x) ((((x) & 0x1fc) >> 2) << 25)
Benoit Goby91525d02011-03-09 16:28:55 -080096
Dmitry Osipenko545592e2020-01-06 04:34:02 +030097#define UTMIP_BIAS_CFG0 0x80c
98#define UTMIP_OTGPD BIT(11)
99#define UTMIP_BIASPD BIT(10)
100#define UTMIP_HSSQUELCH_LEVEL(x) (((x) & 0x3) << 0)
101#define UTMIP_HSDISCON_LEVEL(x) (((x) & 0x3) << 2)
102#define UTMIP_HSDISCON_LEVEL_MSB(x) ((((x) & 0x4) >> 2) << 24)
Benoit Goby91525d02011-03-09 16:28:55 -0800103
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300104#define UTMIP_HSRX_CFG0 0x810
105#define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
106#define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
Benoit Goby91525d02011-03-09 16:28:55 -0800107
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300108#define UTMIP_HSRX_CFG1 0x814
109#define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
Benoit Goby91525d02011-03-09 16:28:55 -0800110
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300111#define UTMIP_TX_CFG0 0x820
112#define UTMIP_FS_PREABMLE_J BIT(19)
113#define UTMIP_HS_DISCON_DISABLE BIT(8)
Benoit Goby91525d02011-03-09 16:28:55 -0800114
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300115#define UTMIP_MISC_CFG0 0x824
116#define UTMIP_DPDM_OBSERVE BIT(26)
117#define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
118#define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
119#define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
120#define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
121#define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
122#define UTMIP_SUSPEND_EXIT_ON_EDGE BIT(22)
Benoit Goby91525d02011-03-09 16:28:55 -0800123
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300124#define UTMIP_MISC_CFG1 0x828
125#define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
126#define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
Benoit Goby91525d02011-03-09 16:28:55 -0800127
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300128#define UTMIP_DEBOUNCE_CFG0 0x82c
129#define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
Benoit Goby91525d02011-03-09 16:28:55 -0800130
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300131#define UTMIP_BAT_CHRG_CFG0 0x830
132#define UTMIP_PD_CHRG BIT(0)
Benoit Goby91525d02011-03-09 16:28:55 -0800133
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300134#define UTMIP_SPARE_CFG0 0x834
135#define FUSE_SETUP_SEL BIT(3)
Benoit Goby91525d02011-03-09 16:28:55 -0800136
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300137#define UTMIP_XCVR_CFG1 0x838
138#define UTMIP_FORCE_PDDISC_POWERDOWN BIT(0)
139#define UTMIP_FORCE_PDCHRP_POWERDOWN BIT(2)
140#define UTMIP_FORCE_PDDR_POWERDOWN BIT(4)
141#define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
Benoit Goby91525d02011-03-09 16:28:55 -0800142
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300143#define UTMIP_BIAS_CFG1 0x83c
144#define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
Benoit Goby91525d02011-03-09 16:28:55 -0800145
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300146/* For Tegra30 and above only, the address is different in Tegra20 */
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300147#define USB_USBMODE 0x1f8
148#define USB_USBMODE_MASK (3 << 0)
149#define USB_USBMODE_HOST (3 << 0)
150#define USB_USBMODE_DEVICE (2 << 0)
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300151
Benoit Goby91525d02011-03-09 16:28:55 -0800152static DEFINE_SPINLOCK(utmip_pad_lock);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300153static unsigned int utmip_pad_count;
Benoit Goby91525d02011-03-09 16:28:55 -0800154
155struct tegra_xtal_freq {
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300156 unsigned int freq;
Benoit Goby91525d02011-03-09 16:28:55 -0800157 u8 enable_delay;
158 u8 stable_count;
159 u8 active_delay;
160 u8 xtal_freq_count;
161 u16 debounce;
162};
163
164static const struct tegra_xtal_freq tegra_freq_table[] = {
165 {
166 .freq = 12000000,
167 .enable_delay = 0x02,
168 .stable_count = 0x2F,
169 .active_delay = 0x04,
170 .xtal_freq_count = 0x76,
171 .debounce = 0x7530,
172 },
173 {
174 .freq = 13000000,
175 .enable_delay = 0x02,
176 .stable_count = 0x33,
177 .active_delay = 0x05,
178 .xtal_freq_count = 0x7F,
179 .debounce = 0x7EF4,
180 },
181 {
182 .freq = 19200000,
183 .enable_delay = 0x03,
184 .stable_count = 0x4B,
185 .active_delay = 0x06,
186 .xtal_freq_count = 0xBB,
187 .debounce = 0xBB80,
188 },
189 {
190 .freq = 26000000,
191 .enable_delay = 0x04,
192 .stable_count = 0x66,
193 .active_delay = 0x09,
194 .xtal_freq_count = 0xFE,
195 .debounce = 0xFDE8,
196 },
197};
198
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300199static inline struct tegra_usb_phy *to_tegra_usb_phy(struct usb_phy *u_phy)
200{
201 return container_of(u_phy, struct tegra_usb_phy, u_phy);
202}
203
Stephen Warren91a687d2013-06-13 11:24:11 -0600204static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
205{
206 void __iomem *base = phy->regs;
207 unsigned long val;
208
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300209 if (phy->soc_config->has_hostpc) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300210 val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300211 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
212 val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300213 writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300214 } else {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300215 val = readl_relaxed(base + TEGRA_USB_PORTSC1);
216 val &= ~TEGRA_PORTSC1_RWC_BITS;
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300217 val &= ~TEGRA_USB_PORTSC1_PTS(~0);
218 val |= TEGRA_USB_PORTSC1_PTS(pts_val);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300219 writel_relaxed(val, base + TEGRA_USB_PORTSC1);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300220 }
Stephen Warren91a687d2013-06-13 11:24:11 -0600221}
222
223static void set_phcd(struct tegra_usb_phy *phy, bool enable)
224{
225 void __iomem *base = phy->regs;
226 unsigned long val;
227
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300228 if (phy->soc_config->has_hostpc) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300229 val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300230 if (enable)
231 val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
232 else
233 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300234 writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300235 } else {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300236 val = readl_relaxed(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300237 if (enable)
238 val |= TEGRA_USB_PORTSC1_PHCD;
239 else
240 val &= ~TEGRA_USB_PORTSC1_PHCD;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300241 writel_relaxed(val, base + TEGRA_USB_PORTSC1);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300242 }
Stephen Warren91a687d2013-06-13 11:24:11 -0600243}
244
Benoit Goby91525d02011-03-09 16:28:55 -0800245static int utmip_pad_open(struct tegra_usb_phy *phy)
246{
Dmitry Osipenko14347032018-04-10 01:02:58 +0300247 int ret;
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300248
Dmitry Osipenko14347032018-04-10 01:02:58 +0300249 ret = clk_prepare_enable(phy->pad_clk);
250 if (ret) {
251 dev_err(phy->u_phy.dev,
252 "Failed to enable UTMI-pads clock: %d\n", ret);
253 return ret;
254 }
255
256 spin_lock(&utmip_pad_lock);
257
258 ret = reset_control_deassert(phy->pad_rst);
259 if (ret) {
260 dev_err(phy->u_phy.dev,
261 "Failed to initialize UTMI-pads reset: %d\n", ret);
262 goto unlock;
263 }
264
265 ret = reset_control_assert(phy->pad_rst);
266 if (ret) {
267 dev_err(phy->u_phy.dev,
268 "Failed to assert UTMI-pads reset: %d\n", ret);
269 goto unlock;
270 }
271
272 udelay(1);
273
274 ret = reset_control_deassert(phy->pad_rst);
275 if (ret)
276 dev_err(phy->u_phy.dev,
277 "Failed to deassert UTMI-pads reset: %d\n", ret);
278unlock:
279 spin_unlock(&utmip_pad_lock);
280
281 clk_disable_unprepare(phy->pad_clk);
282
283 return ret;
284}
285
286static int utmip_pad_close(struct tegra_usb_phy *phy)
287{
288 int ret;
289
290 ret = clk_prepare_enable(phy->pad_clk);
291 if (ret) {
292 dev_err(phy->u_phy.dev,
293 "Failed to enable UTMI-pads clock: %d\n", ret);
294 return ret;
295 }
296
297 ret = reset_control_assert(phy->pad_rst);
298 if (ret)
299 dev_err(phy->u_phy.dev,
300 "Failed to assert UTMI-pads reset: %d\n", ret);
301
302 udelay(1);
303
304 clk_disable_unprepare(phy->pad_clk);
305
306 return ret;
Benoit Goby91525d02011-03-09 16:28:55 -0800307}
308
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300309static int utmip_pad_power_on(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800310{
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300311 struct tegra_utmip_config *config = phy->config;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300312 void __iomem *base = phy->pad_regs;
313 unsigned long val, flags;
314 int err;
Benoit Goby91525d02011-03-09 16:28:55 -0800315
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300316 err = clk_prepare_enable(phy->pad_clk);
317 if (err)
318 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800319
320 spin_lock_irqsave(&utmip_pad_lock, flags);
321
322 if (utmip_pad_count++ == 0) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300323 val = readl_relaxed(base + UTMIP_BIAS_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800324 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300325
326 if (phy->soc_config->requires_extra_tuning_parameters) {
327 val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
328 UTMIP_HSDISCON_LEVEL(~0) |
329 UTMIP_HSDISCON_LEVEL_MSB(~0));
330
331 val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
332 val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
333 val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
334 }
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300335 writel_relaxed(val, base + UTMIP_BIAS_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800336 }
337
338 spin_unlock_irqrestore(&utmip_pad_lock, flags);
339
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530340 clk_disable_unprepare(phy->pad_clk);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300341
342 return 0;
Benoit Goby91525d02011-03-09 16:28:55 -0800343}
344
345static int utmip_pad_power_off(struct tegra_usb_phy *phy)
346{
Benoit Goby91525d02011-03-09 16:28:55 -0800347 void __iomem *base = phy->pad_regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300348 unsigned long val, flags;
349 int err;
Benoit Goby91525d02011-03-09 16:28:55 -0800350
351 if (!utmip_pad_count) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300352 dev_err(phy->u_phy.dev, "UTMIP pad already powered off\n");
Benoit Goby91525d02011-03-09 16:28:55 -0800353 return -EINVAL;
354 }
355
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300356 err = clk_prepare_enable(phy->pad_clk);
357 if (err)
358 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800359
360 spin_lock_irqsave(&utmip_pad_lock, flags);
361
362 if (--utmip_pad_count == 0) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300363 val = readl_relaxed(base + UTMIP_BIAS_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800364 val |= UTMIP_OTGPD | UTMIP_BIASPD;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300365 writel_relaxed(val, base + UTMIP_BIAS_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800366 }
367
368 spin_unlock_irqrestore(&utmip_pad_lock, flags);
369
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530370 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800371
372 return 0;
373}
374
375static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
376{
Dmitry Osipenko43bcf642017-12-17 20:02:39 +0300377 u32 tmp;
378
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300379 return readl_relaxed_poll_timeout(reg, tmp, (tmp & mask) == result,
380 2000, 6000);
Benoit Goby91525d02011-03-09 16:28:55 -0800381}
382
383static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
384{
Benoit Goby91525d02011-03-09 16:28:55 -0800385 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300386 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800387
Jon Hunter203f44c42017-10-02 12:22:53 +0100388 /*
389 * The USB driver may have already initiated the phy clock
390 * disable so wait to see if the clock turns off and if not
391 * then proceed with gating the clock.
392 */
393 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
394 return;
395
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000396 if (phy->is_legacy_phy) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300397 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800398 val |= USB_SUSP_SET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300399 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800400
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300401 usleep_range(10, 100);
Benoit Goby91525d02011-03-09 16:28:55 -0800402
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300403 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800404 val &= ~USB_SUSP_SET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300405 writel_relaxed(val, base + USB_SUSP_CTRL);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300406 } else {
Stephen Warren91a687d2013-06-13 11:24:11 -0600407 set_phcd(phy, true);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300408 }
Benoit Goby91525d02011-03-09 16:28:55 -0800409
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300410 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0))
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300411 dev_err(phy->u_phy.dev,
412 "Timeout waiting for PHY to stabilize on disable\n");
Benoit Goby91525d02011-03-09 16:28:55 -0800413}
414
415static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
416{
Benoit Goby91525d02011-03-09 16:28:55 -0800417 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300418 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800419
Jon Hunter203f44c42017-10-02 12:22:53 +0100420 /*
421 * The USB driver may have already initiated the phy clock
422 * enable so wait to see if the clock turns on and if not
423 * then proceed with ungating the clock.
424 */
425 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
426 USB_PHY_CLK_VALID) == 0)
427 return;
428
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000429 if (phy->is_legacy_phy) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300430 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800431 val |= USB_SUSP_CLR;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300432 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800433
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300434 usleep_range(10, 100);
Benoit Goby91525d02011-03-09 16:28:55 -0800435
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300436 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800437 val &= ~USB_SUSP_CLR;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300438 writel_relaxed(val, base + USB_SUSP_CTRL);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300439 } else {
Stephen Warren91a687d2013-06-13 11:24:11 -0600440 set_phcd(phy, false);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300441 }
Benoit Goby91525d02011-03-09 16:28:55 -0800442
443 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300444 USB_PHY_CLK_VALID))
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300445 dev_err(phy->u_phy.dev,
446 "Timeout waiting for PHY to stabilize on enable\n");
Benoit Goby91525d02011-03-09 16:28:55 -0800447}
448
449static int utmi_phy_power_on(struct tegra_usb_phy *phy)
450{
Benoit Goby91525d02011-03-09 16:28:55 -0800451 struct tegra_utmip_config *config = phy->config;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300452 void __iomem *base = phy->regs;
453 unsigned long val;
454 int err;
Benoit Goby91525d02011-03-09 16:28:55 -0800455
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300456 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800457 val |= UTMIP_RESET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300458 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800459
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000460 if (phy->is_legacy_phy) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300461 val = readl_relaxed(base + USB1_LEGACY_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800462 val |= USB1_NO_LEGACY_MODE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300463 writel_relaxed(val, base + USB1_LEGACY_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800464 }
465
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300466 val = readl_relaxed(base + UTMIP_TX_CFG0);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300467 val |= UTMIP_FS_PREABMLE_J;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300468 writel_relaxed(val, base + UTMIP_TX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800469
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300470 val = readl_relaxed(base + UTMIP_HSRX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800471 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
472 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
473 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300474 writel_relaxed(val, base + UTMIP_HSRX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800475
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300476 val = readl_relaxed(base + UTMIP_HSRX_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800477 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
478 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300479 writel_relaxed(val, base + UTMIP_HSRX_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800480
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300481 val = readl_relaxed(base + UTMIP_DEBOUNCE_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800482 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
483 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300484 writel_relaxed(val, base + UTMIP_DEBOUNCE_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800485
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300486 val = readl_relaxed(base + UTMIP_MISC_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800487 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300488 writel_relaxed(val, base + UTMIP_MISC_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800489
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300490 if (!phy->soc_config->utmi_pll_config_in_car_module) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300491 val = readl_relaxed(base + UTMIP_MISC_CFG1);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300492 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
493 UTMIP_PLLU_STABLE_COUNT(~0));
494 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
495 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300496 writel_relaxed(val, base + UTMIP_MISC_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800497
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300498 val = readl_relaxed(base + UTMIP_PLL_CFG1);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300499 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
500 UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
501 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
502 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300503 writel_relaxed(val, base + UTMIP_PLL_CFG1);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300504 }
Benoit Goby91525d02011-03-09 16:28:55 -0800505
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300506 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300507 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800508 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300509 writel_relaxed(val, base + USB_SUSP_CTRL);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300510
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300511 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300512 val &= ~UTMIP_PD_CHRG;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300513 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300514 } else {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300515 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300516 val |= UTMIP_PD_CHRG;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300517 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800518 }
519
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300520 err = utmip_pad_power_on(phy);
521 if (err)
522 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800523
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300524 val = readl_relaxed(base + UTMIP_XCVR_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800525 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300526 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
527 UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300528 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
529
530 if (!config->xcvr_setup_use_fuses) {
531 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
532 val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
533 }
Benoit Goby91525d02011-03-09 16:28:55 -0800534 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
535 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300536
537 if (phy->soc_config->requires_extra_tuning_parameters) {
538 val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
539 val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
540 val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
541 }
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300542 writel_relaxed(val, base + UTMIP_XCVR_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800543
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300544 val = readl_relaxed(base + UTMIP_XCVR_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800545 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
546 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
547 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300548 writel_relaxed(val, base + UTMIP_XCVR_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800549
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300550 val = readl_relaxed(base + UTMIP_BIAS_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800551 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
552 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300553 writel_relaxed(val, base + UTMIP_BIAS_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800554
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300555 val = readl_relaxed(base + UTMIP_SPARE_CFG0);
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300556 if (config->xcvr_setup_use_fuses)
557 val |= FUSE_SETUP_SEL;
558 else
559 val &= ~FUSE_SETUP_SEL;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300560 writel_relaxed(val, base + UTMIP_SPARE_CFG0);
Tuomas Tynkkynene497a242013-08-12 16:06:53 +0300561
562 if (!phy->is_legacy_phy) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300563 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800564 val |= UTMIP_PHY_ENABLE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300565 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800566 }
567
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300568 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800569 val &= ~UTMIP_RESET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300570 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800571
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000572 if (phy->is_legacy_phy) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300573 val = readl_relaxed(base + USB1_LEGACY_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800574 val &= ~USB1_VBUS_SENSE_CTL_MASK;
575 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300576 writel_relaxed(val, base + USB1_LEGACY_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800577
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300578 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800579 val &= ~USB_SUSP_SET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300580 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800581 }
582
583 utmi_phy_clk_enable(phy);
584
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300585 if (phy->soc_config->requires_usbmode_setup) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300586 val = readl_relaxed(base + USB_USBMODE);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300587 val &= ~USB_USBMODE_MASK;
588 if (phy->mode == USB_DR_MODE_HOST)
589 val |= USB_USBMODE_HOST;
590 else
591 val |= USB_USBMODE_DEVICE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300592 writel_relaxed(val, base + USB_USBMODE);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300593 }
594
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000595 if (!phy->is_legacy_phy)
Stephen Warren91a687d2013-06-13 11:24:11 -0600596 set_pts(phy, 0);
Benoit Goby91525d02011-03-09 16:28:55 -0800597
598 return 0;
599}
600
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530601static int utmi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800602{
Benoit Goby91525d02011-03-09 16:28:55 -0800603 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300604 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800605
606 utmi_phy_clk_disable(phy);
607
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300608 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300609 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800610 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
611 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300612 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800613 }
614
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300615 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800616 val |= UTMIP_RESET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300617 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800618
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300619 val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800620 val |= UTMIP_PD_CHRG;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300621 writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800622
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300623 val = readl_relaxed(base + UTMIP_XCVR_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800624 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
625 UTMIP_FORCE_PDZI_POWERDOWN;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300626 writel_relaxed(val, base + UTMIP_XCVR_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800627
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300628 val = readl_relaxed(base + UTMIP_XCVR_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800629 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
630 UTMIP_FORCE_PDDR_POWERDOWN;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300631 writel_relaxed(val, base + UTMIP_XCVR_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800632
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530633 return utmip_pad_power_off(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800634}
635
636static void utmi_phy_preresume(struct tegra_usb_phy *phy)
637{
Benoit Goby91525d02011-03-09 16:28:55 -0800638 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300639 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800640
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300641 val = readl_relaxed(base + UTMIP_TX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800642 val |= UTMIP_HS_DISCON_DISABLE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300643 writel_relaxed(val, base + UTMIP_TX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800644}
645
646static void utmi_phy_postresume(struct tegra_usb_phy *phy)
647{
Benoit Goby91525d02011-03-09 16:28:55 -0800648 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300649 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800650
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300651 val = readl_relaxed(base + UTMIP_TX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800652 val &= ~UTMIP_HS_DISCON_DISABLE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300653 writel_relaxed(val, base + UTMIP_TX_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800654}
655
656static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
657 enum tegra_usb_phy_port_speed port_speed)
658{
Benoit Goby91525d02011-03-09 16:28:55 -0800659 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300660 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800661
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300662 val = readl_relaxed(base + UTMIP_MISC_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800663 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
664 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
665 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
666 else
667 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300668 writel_relaxed(val, base + UTMIP_MISC_CFG0);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300669 usleep_range(1, 10);
Benoit Goby91525d02011-03-09 16:28:55 -0800670
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300671 val = readl_relaxed(base + UTMIP_MISC_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800672 val |= UTMIP_DPDM_OBSERVE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300673 writel_relaxed(val, base + UTMIP_MISC_CFG0);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300674 usleep_range(10, 100);
Benoit Goby91525d02011-03-09 16:28:55 -0800675}
676
677static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
678{
Benoit Goby91525d02011-03-09 16:28:55 -0800679 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300680 unsigned long val;
Benoit Goby91525d02011-03-09 16:28:55 -0800681
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300682 val = readl_relaxed(base + UTMIP_MISC_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800683 val &= ~UTMIP_DPDM_OBSERVE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300684 writel_relaxed(val, base + UTMIP_MISC_CFG0);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300685 usleep_range(10, 100);
Benoit Goby91525d02011-03-09 16:28:55 -0800686}
687
688static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
689{
Benoit Goby91525d02011-03-09 16:28:55 -0800690 void __iomem *base = phy->regs;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300691 unsigned long val;
692 int err;
Benoit Goby91525d02011-03-09 16:28:55 -0800693
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300694 err = gpio_direction_output(phy->reset_gpio, 0);
695 if (err) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300696 dev_err(phy->u_phy.dev, "GPIO %d not set to 0: %d\n",
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300697 phy->reset_gpio, err);
698 return err;
Venu Byravarasu6829f922013-05-16 19:43:01 +0530699 }
Benoit Goby91525d02011-03-09 16:28:55 -0800700
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300701 err = clk_prepare_enable(phy->clk);
702 if (err)
703 return err;
704
705 usleep_range(5000, 6000);
706
707 err = gpio_direction_output(phy->reset_gpio, 1);
708 if (err) {
709 dev_err(phy->u_phy.dev, "GPIO %d not set to 1: %d\n",
710 phy->reset_gpio, err);
711 goto disable_clk;
712 }
713
714 usleep_range(1000, 2000);
Benoit Goby91525d02011-03-09 16:28:55 -0800715
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300716 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800717 val |= UHSIC_RESET;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300718 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800719
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300720 val = readl_relaxed(base + ULPI_TIMING_CTRL_0);
Benoit Goby91525d02011-03-09 16:28:55 -0800721 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300722 writel_relaxed(val, base + ULPI_TIMING_CTRL_0);
Benoit Goby91525d02011-03-09 16:28:55 -0800723
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300724 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800725 val |= ULPI_PHY_ENABLE;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300726 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800727
728 val = 0;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300729 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
Benoit Goby91525d02011-03-09 16:28:55 -0800730
731 val |= ULPI_DATA_TRIMMER_SEL(4);
732 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
733 val |= ULPI_DIR_TRIMMER_SEL(4);
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300734 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300735 usleep_range(10, 100);
Benoit Goby91525d02011-03-09 16:28:55 -0800736
737 val |= ULPI_DATA_TRIMMER_LOAD;
738 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
739 val |= ULPI_DIR_TRIMMER_LOAD;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300740 writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
Benoit Goby91525d02011-03-09 16:28:55 -0800741
742 /* Fix VbusInvalid due to floating VBUS */
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300743 err = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
744 if (err) {
745 dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
746 goto disable_clk;
Benoit Goby91525d02011-03-09 16:28:55 -0800747 }
748
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300749 err = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
750 if (err) {
751 dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
752 goto disable_clk;
Benoit Goby91525d02011-03-09 16:28:55 -0800753 }
754
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300755 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800756 val |= USB_SUSP_CLR;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300757 writel_relaxed(val, base + USB_SUSP_CTRL);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300758 usleep_range(100, 1000);
Benoit Goby91525d02011-03-09 16:28:55 -0800759
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300760 val = readl_relaxed(base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800761 val &= ~USB_SUSP_CLR;
Dmitry Osipenkob07e5f82020-01-06 04:34:04 +0300762 writel_relaxed(val, base + USB_SUSP_CTRL);
Benoit Goby91525d02011-03-09 16:28:55 -0800763
764 return 0;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300765
766disable_clk:
767 clk_disable_unprepare(phy->clk);
768
769 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800770}
771
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530772static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800773{
Dmitry Osipenko28d190a2020-01-06 04:33:59 +0300774 int err;
775
776 err = gpio_direction_output(phy->reset_gpio, 0);
777 if (err) {
778 dev_err(phy->u_phy.dev, "reset GPIO not asserted: %d\n", err);
779 return err;
780 }
781
782 usleep_range(5000, 6000);
783
784 clk_disable_unprepare(phy->clk);
785
786 return 0;
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530787}
788
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530789static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
790{
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300791 int err;
792
793 if (phy->powered_on)
794 return 0;
795
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000796 if (phy->is_ulpi_phy)
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300797 err = ulpi_phy_power_on(phy);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530798 else
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300799 err = utmi_phy_power_on(phy);
800 if (err)
801 return err;
802
803 phy->powered_on = true;
804
805 return 0;
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530806}
807
808static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
809{
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300810 int err;
811
812 if (!phy->powered_on)
813 return 0;
814
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000815 if (phy->is_ulpi_phy)
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300816 err = ulpi_phy_power_off(phy);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530817 else
Dmitry Osipenko18bd8bf2020-01-06 04:34:00 +0300818 err = utmi_phy_power_off(phy);
819 if (err)
820 return err;
821
822 phy->powered_on = false;
823
824 return 0;
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530825}
826
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300827static void tegra_usb_phy_shutdown(struct usb_phy *u_phy)
828{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300829 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300830
831 if (WARN_ON(!phy->freq))
832 return;
833
834 tegra_usb_phy_power_off(phy);
835
836 if (!phy->is_ulpi_phy)
837 utmip_pad_close(phy);
838
Dmitry Osipenko9df3adc2020-01-06 04:34:05 +0300839 regulator_disable(phy->vbus);
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300840 clk_disable_unprepare(phy->pll_u);
841
842 phy->freq = NULL;
843}
844
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300845static int tegra_usb_phy_set_suspend(struct usb_phy *u_phy, int suspend)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530846{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300847 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300848
849 if (WARN_ON(!phy->freq))
850 return -EINVAL;
851
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530852 if (suspend)
853 return tegra_usb_phy_power_off(phy);
854 else
855 return tegra_usb_phy_power_on(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800856}
857
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530858static int ulpi_open(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800859{
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530860 int err;
861
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530862 err = gpio_direction_output(phy->reset_gpio, 0);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300863 if (err) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300864 dev_err(phy->u_phy.dev,
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300865 "ULPI reset GPIO %d direction not asserted: %d\n",
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300866 phy->reset_gpio, err);
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530867 return err;
868 }
869
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530870 return 0;
871}
872
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300873static int tegra_usb_phy_init(struct usb_phy *u_phy)
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530874{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300875 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800876 unsigned long parent_rate;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300877 unsigned int i;
Benoit Goby91525d02011-03-09 16:28:55 -0800878 int err;
879
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300880 if (WARN_ON(phy->freq))
881 return 0;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530882
883 err = clk_prepare_enable(phy->pll_u);
884 if (err)
885 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800886
887 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
888 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
889 if (tegra_freq_table[i].freq == parent_rate) {
890 phy->freq = &tegra_freq_table[i];
891 break;
892 }
893 }
894 if (!phy->freq) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300895 dev_err(phy->u_phy.dev, "Invalid pll_u parent rate %ld\n",
896 parent_rate);
Benoit Goby91525d02011-03-09 16:28:55 -0800897 err = -EINVAL;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530898 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800899 }
900
Dmitry Osipenko9df3adc2020-01-06 04:34:05 +0300901 err = regulator_enable(phy->vbus);
902 if (err) {
903 dev_err(phy->u_phy.dev,
904 "Failed to enable USB VBUS regulator: %d\n", err);
905 goto fail;
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +0300906 }
907
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530908 if (phy->is_ulpi_phy)
909 err = ulpi_open(phy);
910 else
911 err = utmip_pad_open(phy);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300912 if (err)
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530913 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800914
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300915 err = tegra_usb_phy_power_on(phy);
916 if (err)
917 goto close_phy;
918
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530919 return 0;
Benoit Goby91525d02011-03-09 16:28:55 -0800920
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300921close_phy:
922 if (!phy->is_ulpi_phy)
923 utmip_pad_close(phy);
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530924fail:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530925 clk_disable_unprepare(phy->pll_u);
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +0300926
927 phy->freq = NULL;
928
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530929 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800930}
931
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300932void tegra_usb_phy_preresume(struct usb_phy *u_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800933{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300934 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Venu Byravarasuab137d02013-01-24 15:57:03 +0530935
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000936 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800937 utmi_phy_preresume(phy);
938}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500939EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800940
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300941void tegra_usb_phy_postresume(struct usb_phy *u_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800942{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300943 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Venu Byravarasuab137d02013-01-24 15:57:03 +0530944
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000945 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800946 utmi_phy_postresume(phy);
947}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500948EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800949
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300950void tegra_ehci_phy_restore_start(struct usb_phy *u_phy,
951 enum tegra_usb_phy_port_speed port_speed)
Benoit Goby91525d02011-03-09 16:28:55 -0800952{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300953 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Venu Byravarasuab137d02013-01-24 15:57:03 +0530954
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000955 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800956 utmi_phy_restore_start(phy, port_speed);
957}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500958EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
Benoit Goby91525d02011-03-09 16:28:55 -0800959
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300960void tegra_ehci_phy_restore_end(struct usb_phy *u_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800961{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300962 struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
Venu Byravarasuab137d02013-01-24 15:57:03 +0530963
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000964 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800965 utmi_phy_restore_end(phy);
966}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500967EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
Benoit Goby91525d02011-03-09 16:28:55 -0800968
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300969static int read_utmi_param(struct platform_device *pdev, const char *param,
970 u8 *dest)
971{
972 u32 value;
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300973 int err;
974
975 err = of_property_read_u32(pdev->dev.of_node, param, &value);
976 if (err)
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300977 dev_err(&pdev->dev,
978 "Failed to read USB UTMI parameter %s: %d\n",
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300979 param, err);
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300980 else
981 *dest = value;
982
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300983 return err;
984}
985
986static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
987 struct platform_device *pdev)
988{
Dmitry Osipenko545592e2020-01-06 04:34:02 +0300989 struct tegra_utmip_config *config;
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300990 struct resource *res;
991 int err;
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300992
993 tegra_phy->is_ulpi_phy = false;
994
995 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
996 if (!res) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +0300997 dev_err(&pdev->dev, "Failed to get UTMI pad regs\n");
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300998 return -ENXIO;
999 }
1000
1001 tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001002 resource_size(res));
Chris Ruehl851dd022013-12-04 10:02:44 +08001003 if (!tegra_phy->pad_regs) {
Dmitry Osipenkof59cd942018-04-10 01:02:57 +03001004 dev_err(&pdev->dev, "Failed to remap UTMI pad regs\n");
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001005 return -ENOMEM;
1006 }
1007
Thierry Reding9ce9ec92014-07-21 13:37:37 +02001008 tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
1009 GFP_KERNEL);
Peter Chen01ad32d2014-10-14 15:56:13 +08001010 if (!tegra_phy->config)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001011 return -ENOMEM;
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001012
1013 config = tegra_phy->config;
1014
1015 err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001016 &config->hssync_start_delay);
1017 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001018 return err;
1019
1020 err = read_utmi_param(pdev, "nvidia,elastic-limit",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001021 &config->elastic_limit);
1022 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001023 return err;
1024
1025 err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001026 &config->idle_wait_delay);
1027 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001028 return err;
1029
1030 err = read_utmi_param(pdev, "nvidia,term-range-adj",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001031 &config->term_range_adj);
1032 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001033 return err;
1034
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001035 err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001036 &config->xcvr_lsfslew);
1037 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001038 return err;
1039
1040 err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001041 &config->xcvr_lsrslew);
1042 if (err)
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001043 return err;
1044
Tuomas Tynkkynene497a242013-08-12 16:06:53 +03001045 if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
1046 err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001047 &config->xcvr_hsslew);
1048 if (err)
Tuomas Tynkkynene497a242013-08-12 16:06:53 +03001049 return err;
1050
1051 err = read_utmi_param(pdev, "nvidia,hssquelch-level",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001052 &config->hssquelch_level);
1053 if (err)
Tuomas Tynkkynene497a242013-08-12 16:06:53 +03001054 return err;
1055
1056 err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001057 &config->hsdiscon_level);
1058 if (err)
Tuomas Tynkkynene497a242013-08-12 16:06:53 +03001059 return err;
1060 }
1061
1062 config->xcvr_setup_use_fuses = of_property_read_bool(
1063 pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
1064
1065 if (!config->xcvr_setup_use_fuses) {
1066 err = read_utmi_param(pdev, "nvidia,xcvr-setup",
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001067 &config->xcvr_setup);
1068 if (err)
Tuomas Tynkkynene497a242013-08-12 16:06:53 +03001069 return err;
1070 }
1071
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +03001072 return 0;
1073}
1074
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +03001075static const struct tegra_phy_soc_config tegra20_soc_config = {
1076 .utmi_pll_config_in_car_module = false,
1077 .has_hostpc = false,
1078 .requires_usbmode_setup = false,
1079 .requires_extra_tuning_parameters = false,
1080};
1081
1082static const struct tegra_phy_soc_config tegra30_soc_config = {
1083 .utmi_pll_config_in_car_module = true,
1084 .has_hostpc = true,
1085 .requires_usbmode_setup = true,
1086 .requires_extra_tuning_parameters = true,
1087};
1088
Jingoo Han0f0520b2014-06-18 13:43:50 +09001089static const struct of_device_id tegra_usb_phy_id_table[] = {
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +03001090 { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
1091 { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
1092 { },
1093};
1094MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
1095
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301096static int tegra_usb_phy_probe(struct platform_device *pdev)
1097{
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301098 struct device_node *np = pdev->dev.of_node;
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001099 struct tegra_usb_phy *tegra_phy;
Tuomas Tynkkynen9fdb07f2013-07-25 21:38:08 +03001100 enum usb_phy_interface phy_type;
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001101 struct reset_control *reset;
1102 struct resource *res;
Dmitry Osipenko87541742020-01-06 04:34:07 +03001103 struct usb_phy *phy;
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301104 int err;
1105
1106 tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
Peter Chen01ad32d2014-10-14 15:56:13 +08001107 if (!tegra_phy)
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301108 return -ENOMEM;
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301109
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001110 tegra_phy->soc_config = of_device_get_match_data(&pdev->dev);
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +03001111
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1113 if (!res) {
1114 dev_err(&pdev->dev, "Failed to get I/O memory\n");
1115 return -ENXIO;
1116 }
1117
1118 tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001119 resource_size(res));
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301120 if (!tegra_phy->regs) {
1121 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
1122 return -ENOMEM;
1123 }
1124
1125 tegra_phy->is_legacy_phy =
1126 of_property_read_bool(np, "nvidia,has-legacy-mode");
1127
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +03001128 if (of_find_property(np, "dr_mode", NULL))
Heikki Krogerus06e71142015-09-21 11:14:34 +03001129 tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +03001130 else
1131 tegra_phy->mode = USB_DR_MODE_HOST;
1132
1133 if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
1134 dev_err(&pdev->dev, "dr_mode is invalid\n");
1135 return -EINVAL;
1136 }
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301137
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +03001138 /* On some boards, the VBUS regulator doesn't need to be controlled */
Dmitry Osipenko9df3adc2020-01-06 04:34:05 +03001139 tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
1140 if (IS_ERR(tegra_phy->vbus))
1141 return PTR_ERR(tegra_phy->vbus);
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +03001142
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001143 tegra_phy->pll_u = devm_clk_get(&pdev->dev, "pll_u");
1144 err = PTR_ERR_OR_ZERO(tegra_phy->pll_u);
1145 if (err) {
1146 dev_err(&pdev->dev, "Failed to get pll_u clock: %d\n", err);
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301147 return err;
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001148 }
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301149
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001150 phy_type = of_usb_get_phy_mode(np);
1151 switch (phy_type) {
1152 case USBPHY_INTERFACE_MODE_UTMI:
1153 err = utmi_phy_probe(tegra_phy, pdev);
1154 if (err)
1155 return err;
1156
1157 tegra_phy->pad_clk = devm_clk_get(&pdev->dev, "utmi-pads");
1158 err = PTR_ERR_OR_ZERO(tegra_phy->pad_clk);
1159 if (err) {
1160 dev_err(&pdev->dev,
1161 "Failed to get UTMIP pad clock: %d\n", err);
1162 return err;
1163 }
1164
1165 reset = devm_reset_control_get_optional_shared(&pdev->dev,
1166 "utmi-pads");
1167 err = PTR_ERR_OR_ZERO(reset);
1168 if (err) {
1169 dev_err(&pdev->dev,
1170 "Failed to get UTMI-pads reset: %d\n", err);
1171 return err;
1172 }
1173 tegra_phy->pad_rst = reset;
1174 break;
1175
1176 case USBPHY_INTERFACE_MODE_ULPI:
1177 tegra_phy->is_ulpi_phy = true;
1178
1179 tegra_phy->reset_gpio =
1180 of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
1181
1182 if (!gpio_is_valid(tegra_phy->reset_gpio)) {
1183 dev_err(&pdev->dev,
1184 "Invalid GPIO: %d\n", tegra_phy->reset_gpio);
1185 return tegra_phy->reset_gpio;
1186 }
1187
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001188 tegra_phy->clk = devm_clk_get(&pdev->dev, "ulpi-link");
1189 err = PTR_ERR_OR_ZERO(tegra_phy->clk);
1190 if (err) {
1191 dev_err(&pdev->dev,
1192 "Failed to get ULPI clock: %d\n", err);
1193 return err;
1194 }
1195
1196 err = devm_gpio_request(&pdev->dev, tegra_phy->reset_gpio,
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001197 "ulpi_phy_reset_b");
1198 if (err) {
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001199 dev_err(&pdev->dev, "Request failed for GPIO %d: %d\n",
1200 tegra_phy->reset_gpio, err);
1201 return err;
1202 }
1203
Dmitry Osipenko87541742020-01-06 04:34:07 +03001204 phy = devm_otg_ulpi_create(&pdev->dev,
1205 &ulpi_viewport_access_ops, 0);
1206 if (!phy) {
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001207 dev_err(&pdev->dev, "Failed to create ULPI OTG\n");
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001208 return -ENOMEM;
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001209 }
1210
Dmitry Osipenko87541742020-01-06 04:34:07 +03001211 tegra_phy->ulpi = phy;
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001212 tegra_phy->ulpi->io_priv = tegra_phy->regs + ULPI_VIEWPORT;
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001213 break;
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001214
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001215 default:
1216 dev_err(&pdev->dev, "phy_type %u is invalid or unsupported\n",
1217 phy_type);
1218 return -EINVAL;
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001219 }
1220
1221 tegra_phy->u_phy.dev = &pdev->dev;
1222 tegra_phy->u_phy.init = tegra_usb_phy_init;
1223 tegra_phy->u_phy.shutdown = tegra_usb_phy_shutdown;
1224 tegra_phy->u_phy.set_suspend = tegra_usb_phy_set_suspend;
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301225
Libo Chen72031b52013-08-30 11:23:32 +08001226 platform_set_drvdata(pdev, tegra_phy);
Tuomas Tynkkynen0ee5b4a2013-07-25 21:38:05 +03001227
1228 err = usb_add_phy_dev(&tegra_phy->u_phy);
Dmitry Osipenko545592e2020-01-06 04:34:02 +03001229 if (err)
Dmitry Osipenko87541742020-01-06 04:34:07 +03001230 return err;
Tuomas Tynkkynen0ee5b4a2013-07-25 21:38:05 +03001231
1232 return 0;
1233}
1234
1235static int tegra_usb_phy_remove(struct platform_device *pdev)
1236{
1237 struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
1238
1239 usb_remove_phy(&tegra_phy->u_phy);
Dmitry Osipenko5dcdafd2020-01-06 04:34:01 +03001240
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301241 return 0;
1242}
1243
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301244static struct platform_driver tegra_usb_phy_driver = {
1245 .probe = tegra_usb_phy_probe,
Tuomas Tynkkynen0ee5b4a2013-07-25 21:38:05 +03001246 .remove = tegra_usb_phy_remove,
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301247 .driver = {
1248 .name = "tegra-phy",
Sachin Kamat787239202013-09-30 09:44:47 +05301249 .of_match_table = tegra_usb_phy_id_table,
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301250 },
1251};
1252module_platform_driver(tegra_usb_phy_driver);
1253
Stephen Warren587376a2013-06-13 11:24:08 -06001254MODULE_DESCRIPTION("Tegra USB PHY driver");
1255MODULE_LICENSE("GPL v2");