blob: 1ad184af7601102d69dd1fef277c795ab0b1c61a [file] [log] [blame]
Benoit Goby91525d02011-03-09 16:28:55 -08001/*
Benoit Goby91525d02011-03-09 16:28:55 -08002 * Copyright (C) 2010 Google, Inc.
Venu Byravarasu2d22b422013-05-16 19:43:02 +05303 * Copyright (C) 2013 NVIDIA Corporation
Benoit Goby91525d02011-03-09 16:28:55 -08004 *
5 * Author:
6 * Erik Gilling <konkers@google.com>
7 * Benoit Goby <benoit@android.com>
Venu Byravarasu2d22b422013-05-16 19:43:02 +05308 * Venu Byravarasu <vbyravarasu@nvidia.com>
Benoit Goby91525d02011-03-09 16:28:55 -08009 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/resource.h>
22#include <linux/delay.h>
23#include <linux/slab.h>
24#include <linux/err.h>
Arnd Bergmann4265cbf2012-03-02 15:58:42 -050025#include <linux/export.h>
Stephen Warren587376a2013-06-13 11:24:08 -060026#include <linux/module.h>
Benoit Goby91525d02011-03-09 16:28:55 -080027#include <linux/platform_device.h>
28#include <linux/io.h>
29#include <linux/gpio.h>
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +000030#include <linux/of.h>
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030031#include <linux/of_device.h>
Stephen Warrenaa607eb2012-04-12 15:46:49 -060032#include <linux/of_gpio.h>
Benoit Goby91525d02011-03-09 16:28:55 -080033#include <linux/usb/otg.h>
34#include <linux/usb/ulpi.h>
Tuomas Tynkkynen9fdb07f2013-07-25 21:38:08 +030035#include <linux/usb/of.h>
Benoit Goby91525d02011-03-09 16:28:55 -080036#include <asm/mach-types.h>
Stephen Warren91a687d2013-06-13 11:24:11 -060037#include <linux/usb/ehci_def.h>
Venu Byravarasu1ba82162012-09-05 18:50:23 +053038#include <linux/usb/tegra_usb_phy.h>
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +030039#include <linux/regulator/consumer.h>
Benoit Goby91525d02011-03-09 16:28:55 -080040
41#define ULPI_VIEWPORT 0x170
42
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030043/* PORTSC PTS/PHCD bits, Tegra20 only */
Stephen Warren91a687d2013-06-13 11:24:11 -060044#define TEGRA_USB_PORTSC1 0x184
45#define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
46#define TEGRA_USB_PORTSC1_PHCD (1 << 23)
47
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +030048/* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
49#define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
50#define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
51#define TEGRA_USB_HOSTPC1_DEVLC_PHCD (1 << 22)
52
Stephen Warren91a687d2013-06-13 11:24:11 -060053/* Bits of PORTSC1, which will get cleared by writing 1 into them */
54#define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
55
Benoit Goby91525d02011-03-09 16:28:55 -080056#define USB_SUSP_CTRL 0x400
57#define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
58#define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
59#define USB_SUSP_CLR (1 << 5)
60#define USB_PHY_CLK_VALID (1 << 7)
61#define UTMIP_RESET (1 << 11)
62#define UHSIC_RESET (1 << 11)
63#define UTMIP_PHY_ENABLE (1 << 12)
64#define ULPI_PHY_ENABLE (1 << 13)
65#define USB_SUSP_SET (1 << 14)
66#define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
67
68#define USB1_LEGACY_CTRL 0x410
69#define USB1_NO_LEGACY_MODE (1 << 0)
70#define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
71#define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
72#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
73 (1 << 1)
74#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
75#define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
76
77#define ULPI_TIMING_CTRL_0 0x424
78#define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
79#define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
80
81#define ULPI_TIMING_CTRL_1 0x428
82#define ULPI_DATA_TRIMMER_LOAD (1 << 0)
83#define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
84#define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
85#define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
86#define ULPI_DIR_TRIMMER_LOAD (1 << 24)
87#define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
88
89#define UTMIP_PLL_CFG1 0x804
90#define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
91#define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
92
93#define UTMIP_XCVR_CFG0 0x808
94#define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +030095#define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
Benoit Goby91525d02011-03-09 16:28:55 -080096#define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
97#define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
98#define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
99#define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
100#define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300101#define UTMIP_XCVR_LSBIAS_SEL (1 << 21)
Benoit Goby91525d02011-03-09 16:28:55 -0800102#define UTMIP_XCVR_HSSLEW_MSB(x) (((x) & 0x7f) << 25)
103
104#define UTMIP_BIAS_CFG0 0x80c
105#define UTMIP_OTGPD (1 << 11)
106#define UTMIP_BIASPD (1 << 10)
107
108#define UTMIP_HSRX_CFG0 0x810
109#define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
110#define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
111
112#define UTMIP_HSRX_CFG1 0x814
113#define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
114
115#define UTMIP_TX_CFG0 0x820
116#define UTMIP_FS_PREABMLE_J (1 << 19)
117#define UTMIP_HS_DISCON_DISABLE (1 << 8)
118
119#define UTMIP_MISC_CFG0 0x824
120#define UTMIP_DPDM_OBSERVE (1 << 26)
121#define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
122#define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
123#define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
124#define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
125#define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
126#define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
127
128#define UTMIP_MISC_CFG1 0x828
129#define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
130#define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
131
132#define UTMIP_DEBOUNCE_CFG0 0x82c
133#define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
134
135#define UTMIP_BAT_CHRG_CFG0 0x830
136#define UTMIP_PD_CHRG (1 << 0)
137
138#define UTMIP_SPARE_CFG0 0x834
139#define FUSE_SETUP_SEL (1 << 3)
140
141#define UTMIP_XCVR_CFG1 0x838
142#define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
143#define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
144#define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
145#define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
146
147#define UTMIP_BIAS_CFG1 0x83c
148#define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
149
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300150/* For Tegra30 and above only, the address is different in Tegra20 */
151#define USB_USBMODE 0x1f8
152#define USB_USBMODE_MASK (3 << 0)
153#define USB_USBMODE_HOST (3 << 0)
154#define USB_USBMODE_DEVICE (2 << 0)
155
Benoit Goby91525d02011-03-09 16:28:55 -0800156static DEFINE_SPINLOCK(utmip_pad_lock);
157static int utmip_pad_count;
158
159struct tegra_xtal_freq {
160 int freq;
161 u8 enable_delay;
162 u8 stable_count;
163 u8 active_delay;
164 u8 xtal_freq_count;
165 u16 debounce;
166};
167
168static const struct tegra_xtal_freq tegra_freq_table[] = {
169 {
170 .freq = 12000000,
171 .enable_delay = 0x02,
172 .stable_count = 0x2F,
173 .active_delay = 0x04,
174 .xtal_freq_count = 0x76,
175 .debounce = 0x7530,
176 },
177 {
178 .freq = 13000000,
179 .enable_delay = 0x02,
180 .stable_count = 0x33,
181 .active_delay = 0x05,
182 .xtal_freq_count = 0x7F,
183 .debounce = 0x7EF4,
184 },
185 {
186 .freq = 19200000,
187 .enable_delay = 0x03,
188 .stable_count = 0x4B,
189 .active_delay = 0x06,
190 .xtal_freq_count = 0xBB,
191 .debounce = 0xBB80,
192 },
193 {
194 .freq = 26000000,
195 .enable_delay = 0x04,
196 .stable_count = 0x66,
197 .active_delay = 0x09,
198 .xtal_freq_count = 0xFE,
199 .debounce = 0xFDE8,
200 },
201};
202
Stephen Warren91a687d2013-06-13 11:24:11 -0600203static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
204{
205 void __iomem *base = phy->regs;
206 unsigned long val;
207
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300208 if (phy->soc_config->has_hostpc) {
209 val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
210 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
211 val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
212 writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
213 } else {
214 val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
215 val &= ~TEGRA_USB_PORTSC1_PTS(~0);
216 val |= TEGRA_USB_PORTSC1_PTS(pts_val);
217 writel(val, base + TEGRA_USB_PORTSC1);
218 }
Stephen Warren91a687d2013-06-13 11:24:11 -0600219}
220
221static void set_phcd(struct tegra_usb_phy *phy, bool enable)
222{
223 void __iomem *base = phy->regs;
224 unsigned long val;
225
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300226 if (phy->soc_config->has_hostpc) {
227 val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
228 if (enable)
229 val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
230 else
231 val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
232 writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
233 } else {
234 val = readl(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
235 if (enable)
236 val |= TEGRA_USB_PORTSC1_PHCD;
237 else
238 val &= ~TEGRA_USB_PORTSC1_PHCD;
239 writel(val, base + TEGRA_USB_PORTSC1);
240 }
Stephen Warren91a687d2013-06-13 11:24:11 -0600241}
242
Benoit Goby91525d02011-03-09 16:28:55 -0800243static int utmip_pad_open(struct tegra_usb_phy *phy)
244{
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300245 phy->pad_clk = devm_clk_get(phy->u_phy.dev, "utmi-pads");
Benoit Goby91525d02011-03-09 16:28:55 -0800246 if (IS_ERR(phy->pad_clk)) {
247 pr_err("%s: can't get utmip pad clock\n", __func__);
248 return PTR_ERR(phy->pad_clk);
249 }
250
Benoit Goby91525d02011-03-09 16:28:55 -0800251 return 0;
252}
253
Benoit Goby91525d02011-03-09 16:28:55 -0800254static void utmip_pad_power_on(struct tegra_usb_phy *phy)
255{
256 unsigned long val, flags;
257 void __iomem *base = phy->pad_regs;
258
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530259 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800260
261 spin_lock_irqsave(&utmip_pad_lock, flags);
262
263 if (utmip_pad_count++ == 0) {
264 val = readl(base + UTMIP_BIAS_CFG0);
265 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
266 writel(val, base + UTMIP_BIAS_CFG0);
267 }
268
269 spin_unlock_irqrestore(&utmip_pad_lock, flags);
270
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530271 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800272}
273
274static int utmip_pad_power_off(struct tegra_usb_phy *phy)
275{
276 unsigned long val, flags;
277 void __iomem *base = phy->pad_regs;
278
279 if (!utmip_pad_count) {
280 pr_err("%s: utmip pad already powered off\n", __func__);
281 return -EINVAL;
282 }
283
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530284 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800285
286 spin_lock_irqsave(&utmip_pad_lock, flags);
287
288 if (--utmip_pad_count == 0) {
289 val = readl(base + UTMIP_BIAS_CFG0);
290 val |= UTMIP_OTGPD | UTMIP_BIASPD;
291 writel(val, base + UTMIP_BIAS_CFG0);
292 }
293
294 spin_unlock_irqrestore(&utmip_pad_lock, flags);
295
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530296 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800297
298 return 0;
299}
300
301static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
302{
303 unsigned long timeout = 2000;
304 do {
305 if ((readl(reg) & mask) == result)
306 return 0;
307 udelay(1);
308 timeout--;
309 } while (timeout);
310 return -1;
311}
312
313static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
314{
315 unsigned long val;
316 void __iomem *base = phy->regs;
317
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000318 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800319 val = readl(base + USB_SUSP_CTRL);
320 val |= USB_SUSP_SET;
321 writel(val, base + USB_SUSP_CTRL);
322
323 udelay(10);
324
325 val = readl(base + USB_SUSP_CTRL);
326 val &= ~USB_SUSP_SET;
327 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000328 } else
Stephen Warren91a687d2013-06-13 11:24:11 -0600329 set_phcd(phy, true);
Benoit Goby91525d02011-03-09 16:28:55 -0800330
331 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
332 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
333}
334
335static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
336{
337 unsigned long val;
338 void __iomem *base = phy->regs;
339
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000340 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800341 val = readl(base + USB_SUSP_CTRL);
342 val |= USB_SUSP_CLR;
343 writel(val, base + USB_SUSP_CTRL);
344
345 udelay(10);
346
347 val = readl(base + USB_SUSP_CTRL);
348 val &= ~USB_SUSP_CLR;
349 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000350 } else
Stephen Warren91a687d2013-06-13 11:24:11 -0600351 set_phcd(phy, false);
Benoit Goby91525d02011-03-09 16:28:55 -0800352
353 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
354 USB_PHY_CLK_VALID))
355 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
356}
357
358static int utmi_phy_power_on(struct tegra_usb_phy *phy)
359{
360 unsigned long val;
361 void __iomem *base = phy->regs;
362 struct tegra_utmip_config *config = phy->config;
363
364 val = readl(base + USB_SUSP_CTRL);
365 val |= UTMIP_RESET;
366 writel(val, base + USB_SUSP_CTRL);
367
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000368 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800369 val = readl(base + USB1_LEGACY_CTRL);
370 val |= USB1_NO_LEGACY_MODE;
371 writel(val, base + USB1_LEGACY_CTRL);
372 }
373
374 val = readl(base + UTMIP_TX_CFG0);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300375 val |= UTMIP_FS_PREABMLE_J;
Benoit Goby91525d02011-03-09 16:28:55 -0800376 writel(val, base + UTMIP_TX_CFG0);
377
378 val = readl(base + UTMIP_HSRX_CFG0);
379 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
380 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
381 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
382 writel(val, base + UTMIP_HSRX_CFG0);
383
384 val = readl(base + UTMIP_HSRX_CFG1);
385 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
386 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
387 writel(val, base + UTMIP_HSRX_CFG1);
388
389 val = readl(base + UTMIP_DEBOUNCE_CFG0);
390 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
391 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
392 writel(val, base + UTMIP_DEBOUNCE_CFG0);
393
394 val = readl(base + UTMIP_MISC_CFG0);
395 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
396 writel(val, base + UTMIP_MISC_CFG0);
397
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300398 if (!phy->soc_config->utmi_pll_config_in_car_module) {
399 val = readl(base + UTMIP_MISC_CFG1);
400 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
401 UTMIP_PLLU_STABLE_COUNT(~0));
402 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
403 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
404 writel(val, base + UTMIP_MISC_CFG1);
Benoit Goby91525d02011-03-09 16:28:55 -0800405
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300406 val = readl(base + UTMIP_PLL_CFG1);
407 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
408 UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
409 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
410 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
411 writel(val, base + UTMIP_PLL_CFG1);
412 }
Benoit Goby91525d02011-03-09 16:28:55 -0800413
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300414 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
Benoit Goby91525d02011-03-09 16:28:55 -0800415 val = readl(base + USB_SUSP_CTRL);
416 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
417 writel(val, base + USB_SUSP_CTRL);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300418
419 val = readl(base + UTMIP_BAT_CHRG_CFG0);
420 val &= ~UTMIP_PD_CHRG;
421 writel(val, base + UTMIP_BAT_CHRG_CFG0);
422 } else {
423 val = readl(base + UTMIP_BAT_CHRG_CFG0);
424 val |= UTMIP_PD_CHRG;
425 writel(val, base + UTMIP_BAT_CHRG_CFG0);
Benoit Goby91525d02011-03-09 16:28:55 -0800426 }
427
428 utmip_pad_power_on(phy);
429
430 val = readl(base + UTMIP_XCVR_CFG0);
431 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300432 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
433 UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
Benoit Goby91525d02011-03-09 16:28:55 -0800434 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
435 UTMIP_XCVR_HSSLEW_MSB(~0));
436 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
Tuomas Tynkkynenf5833a02013-08-12 16:06:50 +0300437 val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
Benoit Goby91525d02011-03-09 16:28:55 -0800438 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
439 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
440 writel(val, base + UTMIP_XCVR_CFG0);
441
442 val = readl(base + UTMIP_XCVR_CFG1);
443 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
444 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
445 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
446 writel(val, base + UTMIP_XCVR_CFG1);
447
Benoit Goby91525d02011-03-09 16:28:55 -0800448 val = readl(base + UTMIP_BIAS_CFG1);
449 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
450 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
451 writel(val, base + UTMIP_BIAS_CFG1);
452
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000453 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800454 val = readl(base + UTMIP_SPARE_CFG0);
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300455 if (phy->mode == USB_DR_MODE_PERIPHERAL)
Benoit Goby91525d02011-03-09 16:28:55 -0800456 val &= ~FUSE_SETUP_SEL;
457 else
458 val |= FUSE_SETUP_SEL;
459 writel(val, base + UTMIP_SPARE_CFG0);
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000460 } else {
Benoit Goby91525d02011-03-09 16:28:55 -0800461 val = readl(base + USB_SUSP_CTRL);
462 val |= UTMIP_PHY_ENABLE;
463 writel(val, base + USB_SUSP_CTRL);
464 }
465
466 val = readl(base + USB_SUSP_CTRL);
467 val &= ~UTMIP_RESET;
468 writel(val, base + USB_SUSP_CTRL);
469
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000470 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800471 val = readl(base + USB1_LEGACY_CTRL);
472 val &= ~USB1_VBUS_SENSE_CTL_MASK;
473 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
474 writel(val, base + USB1_LEGACY_CTRL);
475
476 val = readl(base + USB_SUSP_CTRL);
477 val &= ~USB_SUSP_SET;
478 writel(val, base + USB_SUSP_CTRL);
479 }
480
481 utmi_phy_clk_enable(phy);
482
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300483 if (phy->soc_config->requires_usbmode_setup) {
484 val = readl(base + USB_USBMODE);
485 val &= ~USB_USBMODE_MASK;
486 if (phy->mode == USB_DR_MODE_HOST)
487 val |= USB_USBMODE_HOST;
488 else
489 val |= USB_USBMODE_DEVICE;
490 writel(val, base + USB_USBMODE);
491 }
492
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000493 if (!phy->is_legacy_phy)
Stephen Warren91a687d2013-06-13 11:24:11 -0600494 set_pts(phy, 0);
Benoit Goby91525d02011-03-09 16:28:55 -0800495
496 return 0;
497}
498
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530499static int utmi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800500{
501 unsigned long val;
502 void __iomem *base = phy->regs;
503
504 utmi_phy_clk_disable(phy);
505
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300506 if (phy->mode == USB_DR_MODE_PERIPHERAL) {
Benoit Goby91525d02011-03-09 16:28:55 -0800507 val = readl(base + USB_SUSP_CTRL);
508 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
509 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
510 writel(val, base + USB_SUSP_CTRL);
511 }
512
513 val = readl(base + USB_SUSP_CTRL);
514 val |= UTMIP_RESET;
515 writel(val, base + USB_SUSP_CTRL);
516
517 val = readl(base + UTMIP_BAT_CHRG_CFG0);
518 val |= UTMIP_PD_CHRG;
519 writel(val, base + UTMIP_BAT_CHRG_CFG0);
520
521 val = readl(base + UTMIP_XCVR_CFG0);
522 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
523 UTMIP_FORCE_PDZI_POWERDOWN;
524 writel(val, base + UTMIP_XCVR_CFG0);
525
526 val = readl(base + UTMIP_XCVR_CFG1);
527 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
528 UTMIP_FORCE_PDDR_POWERDOWN;
529 writel(val, base + UTMIP_XCVR_CFG1);
530
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530531 return utmip_pad_power_off(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800532}
533
534static void utmi_phy_preresume(struct tegra_usb_phy *phy)
535{
536 unsigned long val;
537 void __iomem *base = phy->regs;
538
539 val = readl(base + UTMIP_TX_CFG0);
540 val |= UTMIP_HS_DISCON_DISABLE;
541 writel(val, base + UTMIP_TX_CFG0);
542}
543
544static void utmi_phy_postresume(struct tegra_usb_phy *phy)
545{
546 unsigned long val;
547 void __iomem *base = phy->regs;
548
549 val = readl(base + UTMIP_TX_CFG0);
550 val &= ~UTMIP_HS_DISCON_DISABLE;
551 writel(val, base + UTMIP_TX_CFG0);
552}
553
554static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
555 enum tegra_usb_phy_port_speed port_speed)
556{
557 unsigned long val;
558 void __iomem *base = phy->regs;
559
560 val = readl(base + UTMIP_MISC_CFG0);
561 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
562 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
563 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
564 else
565 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
566 writel(val, base + UTMIP_MISC_CFG0);
567 udelay(1);
568
569 val = readl(base + UTMIP_MISC_CFG0);
570 val |= UTMIP_DPDM_OBSERVE;
571 writel(val, base + UTMIP_MISC_CFG0);
572 udelay(10);
573}
574
575static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
576{
577 unsigned long val;
578 void __iomem *base = phy->regs;
579
580 val = readl(base + UTMIP_MISC_CFG0);
581 val &= ~UTMIP_DPDM_OBSERVE;
582 writel(val, base + UTMIP_MISC_CFG0);
583 udelay(10);
584}
585
586static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
587{
588 int ret;
589 unsigned long val;
590 void __iomem *base = phy->regs;
Benoit Goby91525d02011-03-09 16:28:55 -0800591
Venu Byravarasu6829f922013-05-16 19:43:01 +0530592 ret = gpio_direction_output(phy->reset_gpio, 0);
593 if (ret < 0) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300594 dev_err(phy->u_phy.dev, "gpio %d not set to 0\n",
595 phy->reset_gpio);
Venu Byravarasu6829f922013-05-16 19:43:01 +0530596 return ret;
597 }
Benoit Goby91525d02011-03-09 16:28:55 -0800598 msleep(5);
Venu Byravarasu6829f922013-05-16 19:43:01 +0530599 ret = gpio_direction_output(phy->reset_gpio, 1);
600 if (ret < 0) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300601 dev_err(phy->u_phy.dev, "gpio %d not set to 1\n",
602 phy->reset_gpio);
Venu Byravarasu6829f922013-05-16 19:43:01 +0530603 return ret;
604 }
Benoit Goby91525d02011-03-09 16:28:55 -0800605
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530606 clk_prepare_enable(phy->clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800607 msleep(1);
608
609 val = readl(base + USB_SUSP_CTRL);
610 val |= UHSIC_RESET;
611 writel(val, base + USB_SUSP_CTRL);
612
613 val = readl(base + ULPI_TIMING_CTRL_0);
614 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
615 writel(val, base + ULPI_TIMING_CTRL_0);
616
617 val = readl(base + USB_SUSP_CTRL);
618 val |= ULPI_PHY_ENABLE;
619 writel(val, base + USB_SUSP_CTRL);
620
621 val = 0;
622 writel(val, base + ULPI_TIMING_CTRL_1);
623
624 val |= ULPI_DATA_TRIMMER_SEL(4);
625 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
626 val |= ULPI_DIR_TRIMMER_SEL(4);
627 writel(val, base + ULPI_TIMING_CTRL_1);
628 udelay(10);
629
630 val |= ULPI_DATA_TRIMMER_LOAD;
631 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
632 val |= ULPI_DIR_TRIMMER_LOAD;
633 writel(val, base + ULPI_TIMING_CTRL_1);
634
635 /* Fix VbusInvalid due to floating VBUS */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200636 ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
Benoit Goby91525d02011-03-09 16:28:55 -0800637 if (ret) {
638 pr_err("%s: ulpi write failed\n", __func__);
639 return ret;
640 }
641
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200642 ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
Benoit Goby91525d02011-03-09 16:28:55 -0800643 if (ret) {
644 pr_err("%s: ulpi write failed\n", __func__);
645 return ret;
646 }
647
Benoit Goby91525d02011-03-09 16:28:55 -0800648 val = readl(base + USB_SUSP_CTRL);
649 val |= USB_SUSP_CLR;
650 writel(val, base + USB_SUSP_CTRL);
651 udelay(100);
652
653 val = readl(base + USB_SUSP_CTRL);
654 val &= ~USB_SUSP_CLR;
655 writel(val, base + USB_SUSP_CTRL);
656
657 return 0;
658}
659
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530660static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800661{
Benoit Goby91525d02011-03-09 16:28:55 -0800662 clk_disable(phy->clk);
Venu Byravarasu12ea18e2013-05-16 19:43:00 +0530663 return gpio_direction_output(phy->reset_gpio, 0);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530664}
665
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530666static void tegra_usb_phy_close(struct usb_phy *x)
667{
668 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
669
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +0300670 if (!IS_ERR(phy->vbus))
671 regulator_disable(phy->vbus);
672
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530673 clk_disable_unprepare(phy->pll_u);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530674}
675
676static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
677{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000678 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530679 return ulpi_phy_power_on(phy);
680 else
681 return utmi_phy_power_on(phy);
682}
683
684static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
685{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000686 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530687 return ulpi_phy_power_off(phy);
688 else
689 return utmi_phy_power_off(phy);
690}
691
692static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
693{
694 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
695 if (suspend)
696 return tegra_usb_phy_power_off(phy);
697 else
698 return tegra_usb_phy_power_on(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800699}
700
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530701static int ulpi_open(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800702{
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530703 int err;
704
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300705 phy->clk = devm_clk_get(phy->u_phy.dev, "ulpi-link");
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530706 if (IS_ERR(phy->clk)) {
707 pr_err("%s: can't get ulpi clock\n", __func__);
708 return PTR_ERR(phy->clk);
709 }
710
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300711 err = devm_gpio_request(phy->u_phy.dev, phy->reset_gpio,
712 "ulpi_phy_reset_b");
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530713 if (err < 0) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300714 dev_err(phy->u_phy.dev, "request failed for gpio: %d\n",
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530715 phy->reset_gpio);
716 return err;
717 }
718
719 err = gpio_direction_output(phy->reset_gpio, 0);
720 if (err < 0) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300721 dev_err(phy->u_phy.dev, "gpio %d direction not set to output\n",
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530722 phy->reset_gpio);
723 return err;
724 }
725
726 phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
727 if (!phy->ulpi) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300728 dev_err(phy->u_phy.dev, "otg_ulpi_create returned NULL\n");
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530729 err = -ENOMEM;
730 return err;
731 }
732
733 phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
734 return 0;
735}
736
737static int tegra_usb_phy_init(struct tegra_usb_phy *phy)
738{
Benoit Goby91525d02011-03-09 16:28:55 -0800739 unsigned long parent_rate;
740 int i;
741 int err;
742
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300743 phy->pll_u = devm_clk_get(phy->u_phy.dev, "pll_u");
Benoit Goby91525d02011-03-09 16:28:55 -0800744 if (IS_ERR(phy->pll_u)) {
745 pr_err("Can't get pll_u clock\n");
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530746 return PTR_ERR(phy->pll_u);
Benoit Goby91525d02011-03-09 16:28:55 -0800747 }
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530748
749 err = clk_prepare_enable(phy->pll_u);
750 if (err)
751 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800752
753 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
754 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
755 if (tegra_freq_table[i].freq == parent_rate) {
756 phy->freq = &tegra_freq_table[i];
757 break;
758 }
759 }
760 if (!phy->freq) {
761 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
762 err = -EINVAL;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530763 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800764 }
765
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +0300766 if (!IS_ERR(phy->vbus)) {
767 err = regulator_enable(phy->vbus);
768 if (err) {
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +0300769 dev_err(phy->u_phy.dev,
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +0300770 "failed to enable usb vbus regulator: %d\n",
771 err);
772 goto fail;
773 }
774 }
775
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530776 if (phy->is_ulpi_phy)
777 err = ulpi_open(phy);
778 else
779 err = utmip_pad_open(phy);
780 if (err < 0)
781 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800782
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530783 return 0;
Benoit Goby91525d02011-03-09 16:28:55 -0800784
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530785fail:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530786 clk_disable_unprepare(phy->pll_u);
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530787 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800788}
789
Venu Byravarasuab137d02013-01-24 15:57:03 +0530790void tegra_usb_phy_preresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800791{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530792 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
793
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000794 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800795 utmi_phy_preresume(phy);
796}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500797EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800798
Venu Byravarasuab137d02013-01-24 15:57:03 +0530799void tegra_usb_phy_postresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800800{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530801 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
802
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000803 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800804 utmi_phy_postresume(phy);
805}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500806EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800807
Venu Byravarasuab137d02013-01-24 15:57:03 +0530808void tegra_ehci_phy_restore_start(struct usb_phy *x,
Benoit Goby91525d02011-03-09 16:28:55 -0800809 enum tegra_usb_phy_port_speed port_speed)
810{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530811 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
812
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000813 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800814 utmi_phy_restore_start(phy, port_speed);
815}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500816EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
Benoit Goby91525d02011-03-09 16:28:55 -0800817
Venu Byravarasuab137d02013-01-24 15:57:03 +0530818void tegra_ehci_phy_restore_end(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800819{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530820 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
821
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000822 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800823 utmi_phy_restore_end(phy);
824}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500825EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
Benoit Goby91525d02011-03-09 16:28:55 -0800826
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300827static int read_utmi_param(struct platform_device *pdev, const char *param,
828 u8 *dest)
829{
830 u32 value;
831 int err = of_property_read_u32(pdev->dev.of_node, param, &value);
832 *dest = (u8)value;
833 if (err < 0)
834 dev_err(&pdev->dev, "Failed to read USB UTMI parameter %s: %d\n",
835 param, err);
836 return err;
837}
838
839static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
840 struct platform_device *pdev)
841{
842 struct resource *res;
843 int err;
844 struct tegra_utmip_config *config;
845
846 tegra_phy->is_ulpi_phy = false;
847
848 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
849 if (!res) {
850 dev_err(&pdev->dev, "Failed to get UTMI Pad regs\n");
851 return -ENXIO;
852 }
853
854 tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
855 resource_size(res));
856 if (!tegra_phy->regs) {
857 dev_err(&pdev->dev, "Failed to remap UTMI Pad regs\n");
858 return -ENOMEM;
859 }
860
861 tegra_phy->config = devm_kzalloc(&pdev->dev,
862 sizeof(*tegra_phy->config), GFP_KERNEL);
863 if (!tegra_phy->config) {
864 dev_err(&pdev->dev,
865 "unable to allocate memory for USB UTMIP config\n");
866 return -ENOMEM;
867 }
868
869 config = tegra_phy->config;
870
871 err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
872 &config->hssync_start_delay);
873 if (err < 0)
874 return err;
875
876 err = read_utmi_param(pdev, "nvidia,elastic-limit",
877 &config->elastic_limit);
878 if (err < 0)
879 return err;
880
881 err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
882 &config->idle_wait_delay);
883 if (err < 0)
884 return err;
885
886 err = read_utmi_param(pdev, "nvidia,term-range-adj",
887 &config->term_range_adj);
888 if (err < 0)
889 return err;
890
891 err = read_utmi_param(pdev, "nvidia,xcvr-setup",
892 &config->xcvr_setup);
893 if (err < 0)
894 return err;
895
896 err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
897 &config->xcvr_lsfslew);
898 if (err < 0)
899 return err;
900
901 err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
902 &config->xcvr_lsrslew);
903 if (err < 0)
904 return err;
905
906 return 0;
907}
908
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300909static const struct tegra_phy_soc_config tegra20_soc_config = {
910 .utmi_pll_config_in_car_module = false,
911 .has_hostpc = false,
912 .requires_usbmode_setup = false,
913 .requires_extra_tuning_parameters = false,
914};
915
916static const struct tegra_phy_soc_config tegra30_soc_config = {
917 .utmi_pll_config_in_car_module = true,
918 .has_hostpc = true,
919 .requires_usbmode_setup = true,
920 .requires_extra_tuning_parameters = true,
921};
922
923static struct of_device_id tegra_usb_phy_id_table[] = {
924 { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
925 { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
926 { },
927};
928MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
929
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530930static int tegra_usb_phy_probe(struct platform_device *pdev)
931{
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300932 const struct of_device_id *match;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530933 struct resource *res;
934 struct tegra_usb_phy *tegra_phy = NULL;
935 struct device_node *np = pdev->dev.of_node;
Tuomas Tynkkynen9fdb07f2013-07-25 21:38:08 +0300936 enum usb_phy_interface phy_type;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530937 int err;
938
939 tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
940 if (!tegra_phy) {
941 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
942 return -ENOMEM;
943 }
944
Tuomas Tynkkynen3e635202013-08-12 16:06:51 +0300945 match = of_match_device(tegra_usb_phy_id_table, &pdev->dev);
946 if (!match) {
947 dev_err(&pdev->dev, "Error: No device match found\n");
948 return -ENODEV;
949 }
950 tegra_phy->soc_config = match->data;
951
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530952 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
953 if (!res) {
954 dev_err(&pdev->dev, "Failed to get I/O memory\n");
955 return -ENXIO;
956 }
957
958 tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
959 resource_size(res));
960 if (!tegra_phy->regs) {
961 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
962 return -ENOMEM;
963 }
964
965 tegra_phy->is_legacy_phy =
966 of_property_read_bool(np, "nvidia,has-legacy-mode");
967
Tuomas Tynkkynen9fdb07f2013-07-25 21:38:08 +0300968 phy_type = of_usb_get_phy_mode(np);
Tuomas Tynkkynena554aea2013-07-25 22:24:09 +0300969 switch (phy_type) {
970 case USBPHY_INTERFACE_MODE_UTMI:
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300971 err = utmi_phy_probe(tegra_phy, pdev);
972 if (err < 0)
973 return err;
Tuomas Tynkkynena554aea2013-07-25 22:24:09 +0300974 break;
975
976 case USBPHY_INTERFACE_MODE_ULPI:
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530977 tegra_phy->is_ulpi_phy = true;
978
979 tegra_phy->reset_gpio =
980 of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
981 if (!gpio_is_valid(tegra_phy->reset_gpio)) {
982 dev_err(&pdev->dev, "invalid gpio: %d\n",
983 tegra_phy->reset_gpio);
984 return tegra_phy->reset_gpio;
985 }
Mikko Perttunen81d5dfe2013-07-17 09:31:01 +0300986 tegra_phy->config = NULL;
Tuomas Tynkkynena554aea2013-07-25 22:24:09 +0300987 break;
988
989 default:
Tuomas Tynkkynen9fdb07f2013-07-25 21:38:08 +0300990 dev_err(&pdev->dev, "phy_type is invalid or unsupported\n");
991 return -EINVAL;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530992 }
993
Tuomas Tynkkynen6558d7e2013-07-25 21:38:09 +0300994 if (of_find_property(np, "dr_mode", NULL))
995 tegra_phy->mode = of_usb_get_dr_mode(np);
996 else
997 tegra_phy->mode = USB_DR_MODE_HOST;
998
999 if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
1000 dev_err(&pdev->dev, "dr_mode is invalid\n");
1001 return -EINVAL;
1002 }
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301003
Mikko Perttunenf5b8c8b2013-07-17 10:37:49 +03001004 /* On some boards, the VBUS regulator doesn't need to be controlled */
1005 if (of_find_property(np, "vbus-supply", NULL)) {
1006 tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
1007 if (IS_ERR(tegra_phy->vbus))
1008 return PTR_ERR(tegra_phy->vbus);
1009 } else {
1010 dev_notice(&pdev->dev, "no vbus regulator");
1011 tegra_phy->vbus = ERR_PTR(-ENODEV);
1012 }
1013
Tuomas Tynkkynen185d0fd2013-07-25 21:38:01 +03001014 tegra_phy->u_phy.dev = &pdev->dev;
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301015 err = tegra_usb_phy_init(tegra_phy);
1016 if (err < 0)
1017 return err;
1018
1019 tegra_phy->u_phy.shutdown = tegra_usb_phy_close;
1020 tegra_phy->u_phy.set_suspend = tegra_usb_phy_suspend;
1021
1022 dev_set_drvdata(&pdev->dev, tegra_phy);
Tuomas Tynkkynen0ee5b4a2013-07-25 21:38:05 +03001023
1024 err = usb_add_phy_dev(&tegra_phy->u_phy);
1025 if (err < 0) {
1026 tegra_usb_phy_close(&tegra_phy->u_phy);
1027 return err;
1028 }
1029
1030 return 0;
1031}
1032
1033static int tegra_usb_phy_remove(struct platform_device *pdev)
1034{
1035 struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
1036
1037 usb_remove_phy(&tegra_phy->u_phy);
1038
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301039 return 0;
1040}
1041
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301042static struct platform_driver tegra_usb_phy_driver = {
1043 .probe = tegra_usb_phy_probe,
Tuomas Tynkkynen0ee5b4a2013-07-25 21:38:05 +03001044 .remove = tegra_usb_phy_remove,
Venu Byravarasu2d22b422013-05-16 19:43:02 +05301045 .driver = {
1046 .name = "tegra-phy",
1047 .owner = THIS_MODULE,
1048 .of_match_table = of_match_ptr(tegra_usb_phy_id_table),
1049 },
1050};
1051module_platform_driver(tegra_usb_phy_driver);
1052
Stephen Warren587376a2013-06-13 11:24:08 -06001053MODULE_DESCRIPTION("Tegra USB PHY driver");
1054MODULE_LICENSE("GPL v2");