blob: 5d9af11d27319f4e4814dd042a0867027048a5cc [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>
Benoit Goby91525d02011-03-09 16:28:55 -080026#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/gpio.h>
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +000029#include <linux/of.h>
Stephen Warrenaa607eb2012-04-12 15:46:49 -060030#include <linux/of_gpio.h>
Benoit Goby91525d02011-03-09 16:28:55 -080031#include <linux/usb/otg.h>
32#include <linux/usb/ulpi.h>
33#include <asm/mach-types.h>
Venu Byravarasu1ba82162012-09-05 18:50:23 +053034#include <linux/usb/tegra_usb_phy.h>
Venu Byravarasu2d22b422013-05-16 19:43:02 +053035#include <linux/module.h>
Benoit Goby91525d02011-03-09 16:28:55 -080036
37#define ULPI_VIEWPORT 0x170
38
Benoit Goby91525d02011-03-09 16:28:55 -080039#define USB_SUSP_CTRL 0x400
40#define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
41#define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
42#define USB_SUSP_CLR (1 << 5)
43#define USB_PHY_CLK_VALID (1 << 7)
44#define UTMIP_RESET (1 << 11)
45#define UHSIC_RESET (1 << 11)
46#define UTMIP_PHY_ENABLE (1 << 12)
47#define ULPI_PHY_ENABLE (1 << 13)
48#define USB_SUSP_SET (1 << 14)
49#define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
50
51#define USB1_LEGACY_CTRL 0x410
52#define USB1_NO_LEGACY_MODE (1 << 0)
53#define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
54#define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
55#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
56 (1 << 1)
57#define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
58#define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
59
60#define ULPI_TIMING_CTRL_0 0x424
61#define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
62#define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
63
64#define ULPI_TIMING_CTRL_1 0x428
65#define ULPI_DATA_TRIMMER_LOAD (1 << 0)
66#define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
67#define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
68#define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
69#define ULPI_DIR_TRIMMER_LOAD (1 << 24)
70#define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
71
72#define UTMIP_PLL_CFG1 0x804
73#define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
74#define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
75
76#define UTMIP_XCVR_CFG0 0x808
77#define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
78#define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
79#define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
80#define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
81#define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
82#define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
83#define UTMIP_XCVR_HSSLEW_MSB(x) (((x) & 0x7f) << 25)
84
85#define UTMIP_BIAS_CFG0 0x80c
86#define UTMIP_OTGPD (1 << 11)
87#define UTMIP_BIASPD (1 << 10)
88
89#define UTMIP_HSRX_CFG0 0x810
90#define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
91#define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
92
93#define UTMIP_HSRX_CFG1 0x814
94#define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
95
96#define UTMIP_TX_CFG0 0x820
97#define UTMIP_FS_PREABMLE_J (1 << 19)
98#define UTMIP_HS_DISCON_DISABLE (1 << 8)
99
100#define UTMIP_MISC_CFG0 0x824
101#define UTMIP_DPDM_OBSERVE (1 << 26)
102#define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
103#define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
104#define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
105#define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
106#define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
107#define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
108
109#define UTMIP_MISC_CFG1 0x828
110#define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
111#define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
112
113#define UTMIP_DEBOUNCE_CFG0 0x82c
114#define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
115
116#define UTMIP_BAT_CHRG_CFG0 0x830
117#define UTMIP_PD_CHRG (1 << 0)
118
119#define UTMIP_SPARE_CFG0 0x834
120#define FUSE_SETUP_SEL (1 << 3)
121
122#define UTMIP_XCVR_CFG1 0x838
123#define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
124#define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
125#define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
126#define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
127
128#define UTMIP_BIAS_CFG1 0x83c
129#define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
130
131static DEFINE_SPINLOCK(utmip_pad_lock);
132static int utmip_pad_count;
133
134struct tegra_xtal_freq {
135 int freq;
136 u8 enable_delay;
137 u8 stable_count;
138 u8 active_delay;
139 u8 xtal_freq_count;
140 u16 debounce;
141};
142
143static const struct tegra_xtal_freq tegra_freq_table[] = {
144 {
145 .freq = 12000000,
146 .enable_delay = 0x02,
147 .stable_count = 0x2F,
148 .active_delay = 0x04,
149 .xtal_freq_count = 0x76,
150 .debounce = 0x7530,
151 },
152 {
153 .freq = 13000000,
154 .enable_delay = 0x02,
155 .stable_count = 0x33,
156 .active_delay = 0x05,
157 .xtal_freq_count = 0x7F,
158 .debounce = 0x7EF4,
159 },
160 {
161 .freq = 19200000,
162 .enable_delay = 0x03,
163 .stable_count = 0x4B,
164 .active_delay = 0x06,
165 .xtal_freq_count = 0xBB,
166 .debounce = 0xBB80,
167 },
168 {
169 .freq = 26000000,
170 .enable_delay = 0x04,
171 .stable_count = 0x66,
172 .active_delay = 0x09,
173 .xtal_freq_count = 0xFE,
174 .debounce = 0xFDE8,
175 },
176};
177
178static struct tegra_utmip_config utmip_default[] = {
179 [0] = {
180 .hssync_start_delay = 9,
181 .idle_wait_delay = 17,
182 .elastic_limit = 16,
183 .term_range_adj = 6,
184 .xcvr_setup = 9,
185 .xcvr_lsfslew = 1,
186 .xcvr_lsrslew = 1,
187 },
188 [2] = {
189 .hssync_start_delay = 9,
190 .idle_wait_delay = 17,
191 .elastic_limit = 16,
192 .term_range_adj = 6,
193 .xcvr_setup = 9,
194 .xcvr_lsfslew = 2,
195 .xcvr_lsrslew = 2,
196 },
197};
198
Benoit Goby91525d02011-03-09 16:28:55 -0800199static int utmip_pad_open(struct tegra_usb_phy *phy)
200{
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530201 phy->pad_clk = devm_clk_get(phy->dev, "utmi-pads");
Benoit Goby91525d02011-03-09 16:28:55 -0800202 if (IS_ERR(phy->pad_clk)) {
203 pr_err("%s: can't get utmip pad clock\n", __func__);
204 return PTR_ERR(phy->pad_clk);
205 }
206
Benoit Goby91525d02011-03-09 16:28:55 -0800207 return 0;
208}
209
Benoit Goby91525d02011-03-09 16:28:55 -0800210static void utmip_pad_power_on(struct tegra_usb_phy *phy)
211{
212 unsigned long val, flags;
213 void __iomem *base = phy->pad_regs;
214
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530215 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800216
217 spin_lock_irqsave(&utmip_pad_lock, flags);
218
219 if (utmip_pad_count++ == 0) {
220 val = readl(base + UTMIP_BIAS_CFG0);
221 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
222 writel(val, base + UTMIP_BIAS_CFG0);
223 }
224
225 spin_unlock_irqrestore(&utmip_pad_lock, flags);
226
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530227 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800228}
229
230static int utmip_pad_power_off(struct tegra_usb_phy *phy)
231{
232 unsigned long val, flags;
233 void __iomem *base = phy->pad_regs;
234
235 if (!utmip_pad_count) {
236 pr_err("%s: utmip pad already powered off\n", __func__);
237 return -EINVAL;
238 }
239
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530240 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800241
242 spin_lock_irqsave(&utmip_pad_lock, flags);
243
244 if (--utmip_pad_count == 0) {
245 val = readl(base + UTMIP_BIAS_CFG0);
246 val |= UTMIP_OTGPD | UTMIP_BIASPD;
247 writel(val, base + UTMIP_BIAS_CFG0);
248 }
249
250 spin_unlock_irqrestore(&utmip_pad_lock, flags);
251
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530252 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800253
254 return 0;
255}
256
257static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
258{
259 unsigned long timeout = 2000;
260 do {
261 if ((readl(reg) & mask) == result)
262 return 0;
263 udelay(1);
264 timeout--;
265 } while (timeout);
266 return -1;
267}
268
269static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
270{
271 unsigned long val;
272 void __iomem *base = phy->regs;
273
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000274 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800275 val = readl(base + USB_SUSP_CTRL);
276 val |= USB_SUSP_SET;
277 writel(val, base + USB_SUSP_CTRL);
278
279 udelay(10);
280
281 val = readl(base + USB_SUSP_CTRL);
282 val &= ~USB_SUSP_SET;
283 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000284 } else
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530285 tegra_ehci_set_phcd(&phy->u_phy, true);
Benoit Goby91525d02011-03-09 16:28:55 -0800286
287 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
288 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
289}
290
291static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
292{
293 unsigned long val;
294 void __iomem *base = phy->regs;
295
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000296 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800297 val = readl(base + USB_SUSP_CTRL);
298 val |= USB_SUSP_CLR;
299 writel(val, base + USB_SUSP_CTRL);
300
301 udelay(10);
302
303 val = readl(base + USB_SUSP_CTRL);
304 val &= ~USB_SUSP_CLR;
305 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000306 } else
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530307 tegra_ehci_set_phcd(&phy->u_phy, false);
Benoit Goby91525d02011-03-09 16:28:55 -0800308
309 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
310 USB_PHY_CLK_VALID))
311 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
312}
313
314static int utmi_phy_power_on(struct tegra_usb_phy *phy)
315{
316 unsigned long val;
317 void __iomem *base = phy->regs;
318 struct tegra_utmip_config *config = phy->config;
319
320 val = readl(base + USB_SUSP_CTRL);
321 val |= UTMIP_RESET;
322 writel(val, base + USB_SUSP_CTRL);
323
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000324 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800325 val = readl(base + USB1_LEGACY_CTRL);
326 val |= USB1_NO_LEGACY_MODE;
327 writel(val, base + USB1_LEGACY_CTRL);
328 }
329
330 val = readl(base + UTMIP_TX_CFG0);
331 val &= ~UTMIP_FS_PREABMLE_J;
332 writel(val, base + UTMIP_TX_CFG0);
333
334 val = readl(base + UTMIP_HSRX_CFG0);
335 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
336 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
337 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
338 writel(val, base + UTMIP_HSRX_CFG0);
339
340 val = readl(base + UTMIP_HSRX_CFG1);
341 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
342 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
343 writel(val, base + UTMIP_HSRX_CFG1);
344
345 val = readl(base + UTMIP_DEBOUNCE_CFG0);
346 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
347 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
348 writel(val, base + UTMIP_DEBOUNCE_CFG0);
349
350 val = readl(base + UTMIP_MISC_CFG0);
351 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
352 writel(val, base + UTMIP_MISC_CFG0);
353
354 val = readl(base + UTMIP_MISC_CFG1);
355 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) | UTMIP_PLLU_STABLE_COUNT(~0));
356 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
357 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
358 writel(val, base + UTMIP_MISC_CFG1);
359
360 val = readl(base + UTMIP_PLL_CFG1);
361 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) | UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
362 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
363 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
364 writel(val, base + UTMIP_PLL_CFG1);
365
366 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
367 val = readl(base + USB_SUSP_CTRL);
368 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
369 writel(val, base + USB_SUSP_CTRL);
370 }
371
372 utmip_pad_power_on(phy);
373
374 val = readl(base + UTMIP_XCVR_CFG0);
375 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
376 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_SETUP(~0) |
377 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
378 UTMIP_XCVR_HSSLEW_MSB(~0));
379 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
380 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
381 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
382 writel(val, base + UTMIP_XCVR_CFG0);
383
384 val = readl(base + UTMIP_XCVR_CFG1);
385 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
386 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
387 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
388 writel(val, base + UTMIP_XCVR_CFG1);
389
390 val = readl(base + UTMIP_BAT_CHRG_CFG0);
391 val &= ~UTMIP_PD_CHRG;
392 writel(val, base + UTMIP_BAT_CHRG_CFG0);
393
394 val = readl(base + UTMIP_BIAS_CFG1);
395 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
396 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
397 writel(val, base + UTMIP_BIAS_CFG1);
398
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000399 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800400 val = readl(base + UTMIP_SPARE_CFG0);
401 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE)
402 val &= ~FUSE_SETUP_SEL;
403 else
404 val |= FUSE_SETUP_SEL;
405 writel(val, base + UTMIP_SPARE_CFG0);
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000406 } else {
Benoit Goby91525d02011-03-09 16:28:55 -0800407 val = readl(base + USB_SUSP_CTRL);
408 val |= UTMIP_PHY_ENABLE;
409 writel(val, base + USB_SUSP_CTRL);
410 }
411
412 val = readl(base + USB_SUSP_CTRL);
413 val &= ~UTMIP_RESET;
414 writel(val, base + USB_SUSP_CTRL);
415
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000416 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800417 val = readl(base + USB1_LEGACY_CTRL);
418 val &= ~USB1_VBUS_SENSE_CTL_MASK;
419 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
420 writel(val, base + USB1_LEGACY_CTRL);
421
422 val = readl(base + USB_SUSP_CTRL);
423 val &= ~USB_SUSP_SET;
424 writel(val, base + USB_SUSP_CTRL);
425 }
426
427 utmi_phy_clk_enable(phy);
428
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000429 if (!phy->is_legacy_phy)
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530430 tegra_ehci_set_pts(&phy->u_phy, 0);
Benoit Goby91525d02011-03-09 16:28:55 -0800431
432 return 0;
433}
434
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530435static int utmi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800436{
437 unsigned long val;
438 void __iomem *base = phy->regs;
439
440 utmi_phy_clk_disable(phy);
441
442 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
443 val = readl(base + USB_SUSP_CTRL);
444 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
445 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
446 writel(val, base + USB_SUSP_CTRL);
447 }
448
449 val = readl(base + USB_SUSP_CTRL);
450 val |= UTMIP_RESET;
451 writel(val, base + USB_SUSP_CTRL);
452
453 val = readl(base + UTMIP_BAT_CHRG_CFG0);
454 val |= UTMIP_PD_CHRG;
455 writel(val, base + UTMIP_BAT_CHRG_CFG0);
456
457 val = readl(base + UTMIP_XCVR_CFG0);
458 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
459 UTMIP_FORCE_PDZI_POWERDOWN;
460 writel(val, base + UTMIP_XCVR_CFG0);
461
462 val = readl(base + UTMIP_XCVR_CFG1);
463 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
464 UTMIP_FORCE_PDDR_POWERDOWN;
465 writel(val, base + UTMIP_XCVR_CFG1);
466
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530467 return utmip_pad_power_off(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800468}
469
470static void utmi_phy_preresume(struct tegra_usb_phy *phy)
471{
472 unsigned long val;
473 void __iomem *base = phy->regs;
474
475 val = readl(base + UTMIP_TX_CFG0);
476 val |= UTMIP_HS_DISCON_DISABLE;
477 writel(val, base + UTMIP_TX_CFG0);
478}
479
480static void utmi_phy_postresume(struct tegra_usb_phy *phy)
481{
482 unsigned long val;
483 void __iomem *base = phy->regs;
484
485 val = readl(base + UTMIP_TX_CFG0);
486 val &= ~UTMIP_HS_DISCON_DISABLE;
487 writel(val, base + UTMIP_TX_CFG0);
488}
489
490static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
491 enum tegra_usb_phy_port_speed port_speed)
492{
493 unsigned long val;
494 void __iomem *base = phy->regs;
495
496 val = readl(base + UTMIP_MISC_CFG0);
497 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
498 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
499 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
500 else
501 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
502 writel(val, base + UTMIP_MISC_CFG0);
503 udelay(1);
504
505 val = readl(base + UTMIP_MISC_CFG0);
506 val |= UTMIP_DPDM_OBSERVE;
507 writel(val, base + UTMIP_MISC_CFG0);
508 udelay(10);
509}
510
511static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
512{
513 unsigned long val;
514 void __iomem *base = phy->regs;
515
516 val = readl(base + UTMIP_MISC_CFG0);
517 val &= ~UTMIP_DPDM_OBSERVE;
518 writel(val, base + UTMIP_MISC_CFG0);
519 udelay(10);
520}
521
522static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
523{
524 int ret;
525 unsigned long val;
526 void __iomem *base = phy->regs;
Benoit Goby91525d02011-03-09 16:28:55 -0800527
Venu Byravarasu6829f922013-05-16 19:43:01 +0530528 ret = gpio_direction_output(phy->reset_gpio, 0);
529 if (ret < 0) {
530 dev_err(phy->dev, "gpio %d not set to 0\n", phy->reset_gpio);
531 return ret;
532 }
Benoit Goby91525d02011-03-09 16:28:55 -0800533 msleep(5);
Venu Byravarasu6829f922013-05-16 19:43:01 +0530534 ret = gpio_direction_output(phy->reset_gpio, 1);
535 if (ret < 0) {
536 dev_err(phy->dev, "gpio %d not set to 1\n", phy->reset_gpio);
537 return ret;
538 }
Benoit Goby91525d02011-03-09 16:28:55 -0800539
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530540 clk_prepare_enable(phy->clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800541 msleep(1);
542
543 val = readl(base + USB_SUSP_CTRL);
544 val |= UHSIC_RESET;
545 writel(val, base + USB_SUSP_CTRL);
546
547 val = readl(base + ULPI_TIMING_CTRL_0);
548 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
549 writel(val, base + ULPI_TIMING_CTRL_0);
550
551 val = readl(base + USB_SUSP_CTRL);
552 val |= ULPI_PHY_ENABLE;
553 writel(val, base + USB_SUSP_CTRL);
554
555 val = 0;
556 writel(val, base + ULPI_TIMING_CTRL_1);
557
558 val |= ULPI_DATA_TRIMMER_SEL(4);
559 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
560 val |= ULPI_DIR_TRIMMER_SEL(4);
561 writel(val, base + ULPI_TIMING_CTRL_1);
562 udelay(10);
563
564 val |= ULPI_DATA_TRIMMER_LOAD;
565 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
566 val |= ULPI_DIR_TRIMMER_LOAD;
567 writel(val, base + ULPI_TIMING_CTRL_1);
568
569 /* Fix VbusInvalid due to floating VBUS */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200570 ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
Benoit Goby91525d02011-03-09 16:28:55 -0800571 if (ret) {
572 pr_err("%s: ulpi write failed\n", __func__);
573 return ret;
574 }
575
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200576 ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
Benoit Goby91525d02011-03-09 16:28:55 -0800577 if (ret) {
578 pr_err("%s: ulpi write failed\n", __func__);
579 return ret;
580 }
581
Benoit Goby91525d02011-03-09 16:28:55 -0800582 val = readl(base + USB_SUSP_CTRL);
583 val |= USB_SUSP_CLR;
584 writel(val, base + USB_SUSP_CTRL);
585 udelay(100);
586
587 val = readl(base + USB_SUSP_CTRL);
588 val &= ~USB_SUSP_CLR;
589 writel(val, base + USB_SUSP_CTRL);
590
591 return 0;
592}
593
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530594static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800595{
Benoit Goby91525d02011-03-09 16:28:55 -0800596 clk_disable(phy->clk);
Venu Byravarasu12ea18e2013-05-16 19:43:00 +0530597 return gpio_direction_output(phy->reset_gpio, 0);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530598}
599
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530600static void tegra_usb_phy_close(struct usb_phy *x)
601{
602 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
603
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530604 clk_disable_unprepare(phy->pll_u);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530605}
606
607static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
608{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000609 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530610 return ulpi_phy_power_on(phy);
611 else
612 return utmi_phy_power_on(phy);
613}
614
615static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
616{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000617 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530618 return ulpi_phy_power_off(phy);
619 else
620 return utmi_phy_power_off(phy);
621}
622
623static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
624{
625 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
626 if (suspend)
627 return tegra_usb_phy_power_off(phy);
628 else
629 return tegra_usb_phy_power_on(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800630}
631
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530632static int ulpi_open(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800633{
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530634 int err;
635
636 phy->clk = devm_clk_get(phy->dev, "ulpi-link");
637 if (IS_ERR(phy->clk)) {
638 pr_err("%s: can't get ulpi clock\n", __func__);
639 return PTR_ERR(phy->clk);
640 }
641
642 err = devm_gpio_request(phy->dev, phy->reset_gpio, "ulpi_phy_reset_b");
643 if (err < 0) {
644 dev_err(phy->dev, "request failed for gpio: %d\n",
645 phy->reset_gpio);
646 return err;
647 }
648
649 err = gpio_direction_output(phy->reset_gpio, 0);
650 if (err < 0) {
651 dev_err(phy->dev, "gpio %d direction not set to output\n",
652 phy->reset_gpio);
653 return err;
654 }
655
656 phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
657 if (!phy->ulpi) {
658 dev_err(phy->dev, "otg_ulpi_create returned NULL\n");
659 err = -ENOMEM;
660 return err;
661 }
662
663 phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
664 return 0;
665}
666
667static int tegra_usb_phy_init(struct tegra_usb_phy *phy)
668{
Benoit Goby91525d02011-03-09 16:28:55 -0800669 unsigned long parent_rate;
670 int i;
671 int err;
672
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530673 if (!phy->is_ulpi_phy) {
674 if (phy->is_legacy_phy)
675 phy->config = &utmip_default[0];
Venu Byravarasu9cd93842013-05-16 19:42:59 +0530676 else
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530677 phy->config = &utmip_default[2];
Benoit Goby91525d02011-03-09 16:28:55 -0800678 }
679
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530680 phy->pll_u = devm_clk_get(phy->dev, "pll_u");
Benoit Goby91525d02011-03-09 16:28:55 -0800681 if (IS_ERR(phy->pll_u)) {
682 pr_err("Can't get pll_u clock\n");
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530683 return PTR_ERR(phy->pll_u);
Benoit Goby91525d02011-03-09 16:28:55 -0800684 }
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530685
686 err = clk_prepare_enable(phy->pll_u);
687 if (err)
688 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800689
690 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
691 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
692 if (tegra_freq_table[i].freq == parent_rate) {
693 phy->freq = &tegra_freq_table[i];
694 break;
695 }
696 }
697 if (!phy->freq) {
698 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
699 err = -EINVAL;
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530700 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800701 }
702
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530703 if (phy->is_ulpi_phy)
704 err = ulpi_open(phy);
705 else
706 err = utmip_pad_open(phy);
707 if (err < 0)
708 goto fail;
Benoit Goby91525d02011-03-09 16:28:55 -0800709
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530710 return 0;
Benoit Goby91525d02011-03-09 16:28:55 -0800711
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530712fail:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530713 clk_disable_unprepare(phy->pll_u);
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530714 return err;
Benoit Goby91525d02011-03-09 16:28:55 -0800715}
716
Venu Byravarasuab137d02013-01-24 15:57:03 +0530717void tegra_usb_phy_preresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800718{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530719 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
720
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000721 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800722 utmi_phy_preresume(phy);
723}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500724EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800725
Venu Byravarasuab137d02013-01-24 15:57:03 +0530726void tegra_usb_phy_postresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800727{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530728 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
729
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000730 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800731 utmi_phy_postresume(phy);
732}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500733EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800734
Venu Byravarasuab137d02013-01-24 15:57:03 +0530735void tegra_ehci_phy_restore_start(struct usb_phy *x,
Benoit Goby91525d02011-03-09 16:28:55 -0800736 enum tegra_usb_phy_port_speed port_speed)
737{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530738 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
739
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000740 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800741 utmi_phy_restore_start(phy, port_speed);
742}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500743EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
Benoit Goby91525d02011-03-09 16:28:55 -0800744
Venu Byravarasuab137d02013-01-24 15:57:03 +0530745void tegra_ehci_phy_restore_end(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800746{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530747 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
748
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000749 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800750 utmi_phy_restore_end(phy);
751}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500752EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
Benoit Goby91525d02011-03-09 16:28:55 -0800753
Venu Byravarasu2d22b422013-05-16 19:43:02 +0530754static int tegra_usb_phy_probe(struct platform_device *pdev)
755{
756 struct resource *res;
757 struct tegra_usb_phy *tegra_phy = NULL;
758 struct device_node *np = pdev->dev.of_node;
759 int err;
760
761 tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
762 if (!tegra_phy) {
763 dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
764 return -ENOMEM;
765 }
766
767 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
768 if (!res) {
769 dev_err(&pdev->dev, "Failed to get I/O memory\n");
770 return -ENXIO;
771 }
772
773 tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
774 resource_size(res));
775 if (!tegra_phy->regs) {
776 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
777 return -ENOMEM;
778 }
779
780 tegra_phy->is_legacy_phy =
781 of_property_read_bool(np, "nvidia,has-legacy-mode");
782
783 err = of_property_match_string(np, "phy_type", "ulpi");
784 if (err < 0) {
785 tegra_phy->is_ulpi_phy = false;
786
787 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
788 if (!res) {
789 dev_err(&pdev->dev, "Failed to get UTMI Pad regs\n");
790 return -ENXIO;
791 }
792
793 tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
794 resource_size(res));
795 if (!tegra_phy->regs) {
796 dev_err(&pdev->dev, "Failed to remap UTMI Pad regs\n");
797 return -ENOMEM;
798 }
799 } else {
800 tegra_phy->is_ulpi_phy = true;
801
802 tegra_phy->reset_gpio =
803 of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
804 if (!gpio_is_valid(tegra_phy->reset_gpio)) {
805 dev_err(&pdev->dev, "invalid gpio: %d\n",
806 tegra_phy->reset_gpio);
807 return tegra_phy->reset_gpio;
808 }
809 }
810
811 err = of_property_match_string(np, "dr_mode", "otg");
812 if (err < 0) {
813 err = of_property_match_string(np, "dr_mode", "peripheral");
814 if (err < 0)
815 tegra_phy->mode = TEGRA_USB_PHY_MODE_HOST;
816 else
817 tegra_phy->mode = TEGRA_USB_PHY_MODE_DEVICE;
818 } else
819 tegra_phy->mode = TEGRA_USB_PHY_MODE_OTG;
820
821 tegra_phy->dev = &pdev->dev;
822 err = tegra_usb_phy_init(tegra_phy);
823 if (err < 0)
824 return err;
825
826 tegra_phy->u_phy.shutdown = tegra_usb_phy_close;
827 tegra_phy->u_phy.set_suspend = tegra_usb_phy_suspend;
828
829 dev_set_drvdata(&pdev->dev, tegra_phy);
830 return 0;
831}
832
833static struct of_device_id tegra_usb_phy_id_table[] = {
834 { .compatible = "nvidia,tegra20-usb-phy", },
835 { },
836};
837MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
838
839static struct platform_driver tegra_usb_phy_driver = {
840 .probe = tegra_usb_phy_probe,
841 .driver = {
842 .name = "tegra-phy",
843 .owner = THIS_MODULE,
844 .of_match_table = of_match_ptr(tegra_usb_phy_id_table),
845 },
846};
847module_platform_driver(tegra_usb_phy_driver);
848
849static int tegra_usb_phy_match(struct device *dev, void *data)
850{
851 struct tegra_usb_phy *tegra_phy = dev_get_drvdata(dev);
852 struct device_node *dn = data;
853
854 return (tegra_phy->dev->of_node == dn) ? 1 : 0;
855}
856
857struct usb_phy *tegra_usb_get_phy(struct device_node *dn)
858{
859 struct device *dev;
860 struct tegra_usb_phy *tegra_phy;
861
862 dev = driver_find_device(&tegra_usb_phy_driver.driver, NULL, dn,
863 tegra_usb_phy_match);
864 if (!dev)
865 return ERR_PTR(-EPROBE_DEFER);
866
867 tegra_phy = dev_get_drvdata(dev);
868
869 return &tegra_phy->u_phy;
870}
871EXPORT_SYMBOL_GPL(tegra_usb_get_phy);