blob: 5487d38481af38dead7dee1ed9b7cbc839a7ad22 [file] [log] [blame]
Benoit Goby91525d02011-03-09 16:28:55 -08001/*
Benoit Goby91525d02011-03-09 16:28:55 -08002 * Copyright (C) 2010 Google, Inc.
3 *
4 * Author:
5 * Erik Gilling <konkers@google.com>
6 * Benoit Goby <benoit@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/resource.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/err.h>
Arnd Bergmann4265cbf2012-03-02 15:58:42 -050023#include <linux/export.h>
Benoit Goby91525d02011-03-09 16:28:55 -080024#include <linux/platform_device.h>
25#include <linux/io.h>
26#include <linux/gpio.h>
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +000027#include <linux/of.h>
Stephen Warrenaa607eb2012-04-12 15:46:49 -060028#include <linux/of_gpio.h>
Benoit Goby91525d02011-03-09 16:28:55 -080029#include <linux/usb/otg.h>
30#include <linux/usb/ulpi.h>
31#include <asm/mach-types.h>
Venu Byravarasu1ba82162012-09-05 18:50:23 +053032#include <linux/usb/tegra_usb_phy.h>
Stephen Warrenffc74932012-10-02 16:50:02 -060033
34#define TEGRA_USB_BASE 0xC5000000
35#define TEGRA_USB_SIZE SZ_16K
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{
201 phy->pad_clk = clk_get_sys("utmip-pad", NULL);
202 if (IS_ERR(phy->pad_clk)) {
203 pr_err("%s: can't get utmip pad clock\n", __func__);
204 return PTR_ERR(phy->pad_clk);
205 }
206
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000207 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800208 phy->pad_regs = phy->regs;
209 } else {
210 phy->pad_regs = ioremap(TEGRA_USB_BASE, TEGRA_USB_SIZE);
211 if (!phy->pad_regs) {
212 pr_err("%s: can't remap usb registers\n", __func__);
213 clk_put(phy->pad_clk);
214 return -ENOMEM;
215 }
216 }
217 return 0;
218}
219
220static void utmip_pad_close(struct tegra_usb_phy *phy)
221{
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000222 if (!phy->is_legacy_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800223 iounmap(phy->pad_regs);
224 clk_put(phy->pad_clk);
225}
226
227static void utmip_pad_power_on(struct tegra_usb_phy *phy)
228{
229 unsigned long val, flags;
230 void __iomem *base = phy->pad_regs;
231
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530232 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800233
234 spin_lock_irqsave(&utmip_pad_lock, flags);
235
236 if (utmip_pad_count++ == 0) {
237 val = readl(base + UTMIP_BIAS_CFG0);
238 val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
239 writel(val, base + UTMIP_BIAS_CFG0);
240 }
241
242 spin_unlock_irqrestore(&utmip_pad_lock, flags);
243
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530244 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800245}
246
247static int utmip_pad_power_off(struct tegra_usb_phy *phy)
248{
249 unsigned long val, flags;
250 void __iomem *base = phy->pad_regs;
251
252 if (!utmip_pad_count) {
253 pr_err("%s: utmip pad already powered off\n", __func__);
254 return -EINVAL;
255 }
256
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530257 clk_prepare_enable(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800258
259 spin_lock_irqsave(&utmip_pad_lock, flags);
260
261 if (--utmip_pad_count == 0) {
262 val = readl(base + UTMIP_BIAS_CFG0);
263 val |= UTMIP_OTGPD | UTMIP_BIASPD;
264 writel(val, base + UTMIP_BIAS_CFG0);
265 }
266
267 spin_unlock_irqrestore(&utmip_pad_lock, flags);
268
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530269 clk_disable_unprepare(phy->pad_clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800270
271 return 0;
272}
273
274static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
275{
276 unsigned long timeout = 2000;
277 do {
278 if ((readl(reg) & mask) == result)
279 return 0;
280 udelay(1);
281 timeout--;
282 } while (timeout);
283 return -1;
284}
285
286static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
287{
288 unsigned long val;
289 void __iomem *base = phy->regs;
290
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000291 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800292 val = readl(base + USB_SUSP_CTRL);
293 val |= USB_SUSP_SET;
294 writel(val, base + USB_SUSP_CTRL);
295
296 udelay(10);
297
298 val = readl(base + USB_SUSP_CTRL);
299 val &= ~USB_SUSP_SET;
300 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000301 } else
302 tegra_ehci_set_phcd(&phy->u_phy, true);
Benoit Goby91525d02011-03-09 16:28:55 -0800303
304 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
305 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
306}
307
308static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
309{
310 unsigned long val;
311 void __iomem *base = phy->regs;
312
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000313 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800314 val = readl(base + USB_SUSP_CTRL);
315 val |= USB_SUSP_CLR;
316 writel(val, base + USB_SUSP_CTRL);
317
318 udelay(10);
319
320 val = readl(base + USB_SUSP_CTRL);
321 val &= ~USB_SUSP_CLR;
322 writel(val, base + USB_SUSP_CTRL);
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000323 } else
324 tegra_ehci_set_phcd(&phy->u_phy, false);
Benoit Goby91525d02011-03-09 16:28:55 -0800325
326 if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
327 USB_PHY_CLK_VALID))
328 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
329}
330
331static int utmi_phy_power_on(struct tegra_usb_phy *phy)
332{
333 unsigned long val;
334 void __iomem *base = phy->regs;
335 struct tegra_utmip_config *config = phy->config;
336
337 val = readl(base + USB_SUSP_CTRL);
338 val |= UTMIP_RESET;
339 writel(val, base + USB_SUSP_CTRL);
340
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000341 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800342 val = readl(base + USB1_LEGACY_CTRL);
343 val |= USB1_NO_LEGACY_MODE;
344 writel(val, base + USB1_LEGACY_CTRL);
345 }
346
347 val = readl(base + UTMIP_TX_CFG0);
348 val &= ~UTMIP_FS_PREABMLE_J;
349 writel(val, base + UTMIP_TX_CFG0);
350
351 val = readl(base + UTMIP_HSRX_CFG0);
352 val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
353 val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
354 val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
355 writel(val, base + UTMIP_HSRX_CFG0);
356
357 val = readl(base + UTMIP_HSRX_CFG1);
358 val &= ~UTMIP_HS_SYNC_START_DLY(~0);
359 val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
360 writel(val, base + UTMIP_HSRX_CFG1);
361
362 val = readl(base + UTMIP_DEBOUNCE_CFG0);
363 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
364 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
365 writel(val, base + UTMIP_DEBOUNCE_CFG0);
366
367 val = readl(base + UTMIP_MISC_CFG0);
368 val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
369 writel(val, base + UTMIP_MISC_CFG0);
370
371 val = readl(base + UTMIP_MISC_CFG1);
372 val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) | UTMIP_PLLU_STABLE_COUNT(~0));
373 val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
374 UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
375 writel(val, base + UTMIP_MISC_CFG1);
376
377 val = readl(base + UTMIP_PLL_CFG1);
378 val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) | UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
379 val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
380 UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
381 writel(val, base + UTMIP_PLL_CFG1);
382
383 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
384 val = readl(base + USB_SUSP_CTRL);
385 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
386 writel(val, base + USB_SUSP_CTRL);
387 }
388
389 utmip_pad_power_on(phy);
390
391 val = readl(base + UTMIP_XCVR_CFG0);
392 val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
393 UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_SETUP(~0) |
394 UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
395 UTMIP_XCVR_HSSLEW_MSB(~0));
396 val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
397 val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
398 val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
399 writel(val, base + UTMIP_XCVR_CFG0);
400
401 val = readl(base + UTMIP_XCVR_CFG1);
402 val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
403 UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
404 val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
405 writel(val, base + UTMIP_XCVR_CFG1);
406
407 val = readl(base + UTMIP_BAT_CHRG_CFG0);
408 val &= ~UTMIP_PD_CHRG;
409 writel(val, base + UTMIP_BAT_CHRG_CFG0);
410
411 val = readl(base + UTMIP_BIAS_CFG1);
412 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
413 val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
414 writel(val, base + UTMIP_BIAS_CFG1);
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 + UTMIP_SPARE_CFG0);
418 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE)
419 val &= ~FUSE_SETUP_SEL;
420 else
421 val |= FUSE_SETUP_SEL;
422 writel(val, base + UTMIP_SPARE_CFG0);
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000423 } else {
Benoit Goby91525d02011-03-09 16:28:55 -0800424 val = readl(base + USB_SUSP_CTRL);
425 val |= UTMIP_PHY_ENABLE;
426 writel(val, base + USB_SUSP_CTRL);
427 }
428
429 val = readl(base + USB_SUSP_CTRL);
430 val &= ~UTMIP_RESET;
431 writel(val, base + USB_SUSP_CTRL);
432
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000433 if (phy->is_legacy_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800434 val = readl(base + USB1_LEGACY_CTRL);
435 val &= ~USB1_VBUS_SENSE_CTL_MASK;
436 val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
437 writel(val, base + USB1_LEGACY_CTRL);
438
439 val = readl(base + USB_SUSP_CTRL);
440 val &= ~USB_SUSP_SET;
441 writel(val, base + USB_SUSP_CTRL);
442 }
443
444 utmi_phy_clk_enable(phy);
445
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000446 if (!phy->is_legacy_phy)
447 tegra_ehci_set_pts(&phy->u_phy, 0);
Benoit Goby91525d02011-03-09 16:28:55 -0800448
449 return 0;
450}
451
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530452static int utmi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800453{
454 unsigned long val;
455 void __iomem *base = phy->regs;
456
457 utmi_phy_clk_disable(phy);
458
459 if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
460 val = readl(base + USB_SUSP_CTRL);
461 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
462 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
463 writel(val, base + USB_SUSP_CTRL);
464 }
465
466 val = readl(base + USB_SUSP_CTRL);
467 val |= UTMIP_RESET;
468 writel(val, base + USB_SUSP_CTRL);
469
470 val = readl(base + UTMIP_BAT_CHRG_CFG0);
471 val |= UTMIP_PD_CHRG;
472 writel(val, base + UTMIP_BAT_CHRG_CFG0);
473
474 val = readl(base + UTMIP_XCVR_CFG0);
475 val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
476 UTMIP_FORCE_PDZI_POWERDOWN;
477 writel(val, base + UTMIP_XCVR_CFG0);
478
479 val = readl(base + UTMIP_XCVR_CFG1);
480 val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
481 UTMIP_FORCE_PDDR_POWERDOWN;
482 writel(val, base + UTMIP_XCVR_CFG1);
483
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530484 return utmip_pad_power_off(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800485}
486
487static void utmi_phy_preresume(struct tegra_usb_phy *phy)
488{
489 unsigned long val;
490 void __iomem *base = phy->regs;
491
492 val = readl(base + UTMIP_TX_CFG0);
493 val |= UTMIP_HS_DISCON_DISABLE;
494 writel(val, base + UTMIP_TX_CFG0);
495}
496
497static void utmi_phy_postresume(struct tegra_usb_phy *phy)
498{
499 unsigned long val;
500 void __iomem *base = phy->regs;
501
502 val = readl(base + UTMIP_TX_CFG0);
503 val &= ~UTMIP_HS_DISCON_DISABLE;
504 writel(val, base + UTMIP_TX_CFG0);
505}
506
507static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
508 enum tegra_usb_phy_port_speed port_speed)
509{
510 unsigned long val;
511 void __iomem *base = phy->regs;
512
513 val = readl(base + UTMIP_MISC_CFG0);
514 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
515 if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
516 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
517 else
518 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
519 writel(val, base + UTMIP_MISC_CFG0);
520 udelay(1);
521
522 val = readl(base + UTMIP_MISC_CFG0);
523 val |= UTMIP_DPDM_OBSERVE;
524 writel(val, base + UTMIP_MISC_CFG0);
525 udelay(10);
526}
527
528static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
529{
530 unsigned long val;
531 void __iomem *base = phy->regs;
532
533 val = readl(base + UTMIP_MISC_CFG0);
534 val &= ~UTMIP_DPDM_OBSERVE;
535 writel(val, base + UTMIP_MISC_CFG0);
536 udelay(10);
537}
538
539static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
540{
541 int ret;
542 unsigned long val;
543 void __iomem *base = phy->regs;
544 struct tegra_ulpi_config *config = phy->config;
545
546 gpio_direction_output(config->reset_gpio, 0);
547 msleep(5);
548 gpio_direction_output(config->reset_gpio, 1);
549
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530550 clk_prepare_enable(phy->clk);
Benoit Goby91525d02011-03-09 16:28:55 -0800551 msleep(1);
552
553 val = readl(base + USB_SUSP_CTRL);
554 val |= UHSIC_RESET;
555 writel(val, base + USB_SUSP_CTRL);
556
557 val = readl(base + ULPI_TIMING_CTRL_0);
558 val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
559 writel(val, base + ULPI_TIMING_CTRL_0);
560
561 val = readl(base + USB_SUSP_CTRL);
562 val |= ULPI_PHY_ENABLE;
563 writel(val, base + USB_SUSP_CTRL);
564
565 val = 0;
566 writel(val, base + ULPI_TIMING_CTRL_1);
567
568 val |= ULPI_DATA_TRIMMER_SEL(4);
569 val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
570 val |= ULPI_DIR_TRIMMER_SEL(4);
571 writel(val, base + ULPI_TIMING_CTRL_1);
572 udelay(10);
573
574 val |= ULPI_DATA_TRIMMER_LOAD;
575 val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
576 val |= ULPI_DIR_TRIMMER_LOAD;
577 writel(val, base + ULPI_TIMING_CTRL_1);
578
579 /* Fix VbusInvalid due to floating VBUS */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200580 ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
Benoit Goby91525d02011-03-09 16:28:55 -0800581 if (ret) {
582 pr_err("%s: ulpi write failed\n", __func__);
583 return ret;
584 }
585
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200586 ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
Benoit Goby91525d02011-03-09 16:28:55 -0800587 if (ret) {
588 pr_err("%s: ulpi write failed\n", __func__);
589 return ret;
590 }
591
Benoit Goby91525d02011-03-09 16:28:55 -0800592 val = readl(base + USB_SUSP_CTRL);
593 val |= USB_SUSP_CLR;
594 writel(val, base + USB_SUSP_CTRL);
595 udelay(100);
596
597 val = readl(base + USB_SUSP_CTRL);
598 val &= ~USB_SUSP_CLR;
599 writel(val, base + USB_SUSP_CTRL);
600
601 return 0;
602}
603
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530604static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800605{
Benoit Goby91525d02011-03-09 16:28:55 -0800606 struct tegra_ulpi_config *config = phy->config;
607
Benoit Goby91525d02011-03-09 16:28:55 -0800608 clk_disable(phy->clk);
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530609 return gpio_direction_output(config->reset_gpio, 0);
610}
611
612static int tegra_phy_init(struct usb_phy *x)
613{
614 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
615 struct tegra_ulpi_config *ulpi_config;
616 int err;
617
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000618 if (phy->is_ulpi_phy) {
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530619 ulpi_config = phy->config;
620 phy->clk = clk_get_sys(NULL, ulpi_config->clk);
621 if (IS_ERR(phy->clk)) {
622 pr_err("%s: can't get ulpi clock\n", __func__);
623 err = -ENXIO;
624 goto err1;
625 }
626 if (!gpio_is_valid(ulpi_config->reset_gpio))
627 ulpi_config->reset_gpio =
628 of_get_named_gpio(phy->dev->of_node,
629 "nvidia,phy-reset-gpio", 0);
630 if (!gpio_is_valid(ulpi_config->reset_gpio)) {
631 pr_err("%s: invalid reset gpio: %d\n", __func__,
632 ulpi_config->reset_gpio);
633 err = -EINVAL;
634 goto err1;
635 }
636 gpio_request(ulpi_config->reset_gpio, "ulpi_phy_reset_b");
637 gpio_direction_output(ulpi_config->reset_gpio, 0);
638 phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
639 phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
640 } else {
641 err = utmip_pad_open(phy);
642 if (err < 0)
643 goto err1;
644 }
645 return 0;
646err1:
647 clk_disable_unprepare(phy->pll_u);
648 clk_put(phy->pll_u);
649 return err;
650}
651
652static void tegra_usb_phy_close(struct usb_phy *x)
653{
654 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
655
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000656 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530657 clk_put(phy->clk);
658 else
659 utmip_pad_close(phy);
660 clk_disable_unprepare(phy->pll_u);
661 clk_put(phy->pll_u);
662 kfree(phy);
663}
664
665static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
666{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000667 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530668 return ulpi_phy_power_on(phy);
669 else
670 return utmi_phy_power_on(phy);
671}
672
673static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
674{
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000675 if (phy->is_ulpi_phy)
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530676 return ulpi_phy_power_off(phy);
677 else
678 return utmi_phy_power_off(phy);
679}
680
681static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
682{
683 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
684 if (suspend)
685 return tegra_usb_phy_power_off(phy);
686 else
687 return tegra_usb_phy_power_on(phy);
Benoit Goby91525d02011-03-09 16:28:55 -0800688}
689
Stephen Warrenaa607eb2012-04-12 15:46:49 -0600690struct tegra_usb_phy *tegra_usb_phy_open(struct device *dev, int instance,
691 void __iomem *regs, void *config, enum tegra_usb_phy_mode phy_mode)
Benoit Goby91525d02011-03-09 16:28:55 -0800692{
693 struct tegra_usb_phy *phy;
Benoit Goby91525d02011-03-09 16:28:55 -0800694 unsigned long parent_rate;
695 int i;
696 int err;
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000697 struct device_node *np = dev->of_node;
Benoit Goby91525d02011-03-09 16:28:55 -0800698
Venu Byravarasu5b26c942013-01-11 17:31:29 -0700699 phy = kzalloc(sizeof(struct tegra_usb_phy), GFP_KERNEL);
Benoit Goby91525d02011-03-09 16:28:55 -0800700 if (!phy)
701 return ERR_PTR(-ENOMEM);
702
703 phy->instance = instance;
704 phy->regs = regs;
705 phy->config = config;
706 phy->mode = phy_mode;
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530707 phy->dev = dev;
Venu Byravarasu3a55c6a2013-01-16 03:30:20 +0000708 phy->is_legacy_phy =
709 of_property_read_bool(np, "nvidia,has-legacy-mode");
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000710 err = of_property_match_string(np, "phy_type", "ulpi");
711 if (err < 0)
712 phy->is_ulpi_phy = false;
713 else
714 phy->is_ulpi_phy = true;
Benoit Goby91525d02011-03-09 16:28:55 -0800715
716 if (!phy->config) {
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000717 if (phy->is_ulpi_phy) {
Benoit Goby91525d02011-03-09 16:28:55 -0800718 pr_err("%s: ulpi phy configuration missing", __func__);
719 err = -EINVAL;
720 goto err0;
721 } else {
722 phy->config = &utmip_default[instance];
723 }
724 }
725
726 phy->pll_u = clk_get_sys(NULL, "pll_u");
727 if (IS_ERR(phy->pll_u)) {
728 pr_err("Can't get pll_u clock\n");
729 err = PTR_ERR(phy->pll_u);
730 goto err0;
731 }
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530732 clk_prepare_enable(phy->pll_u);
Benoit Goby91525d02011-03-09 16:28:55 -0800733
734 parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
735 for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
736 if (tegra_freq_table[i].freq == parent_rate) {
737 phy->freq = &tegra_freq_table[i];
738 break;
739 }
740 }
741 if (!phy->freq) {
742 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
743 err = -EINVAL;
744 goto err1;
745 }
746
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530747 phy->u_phy.init = tegra_phy_init;
748 phy->u_phy.shutdown = tegra_usb_phy_close;
749 phy->u_phy.set_suspend = tegra_usb_phy_suspend;
Benoit Goby91525d02011-03-09 16:28:55 -0800750
751 return phy;
752
753err1:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530754 clk_disable_unprepare(phy->pll_u);
Benoit Goby91525d02011-03-09 16:28:55 -0800755 clk_put(phy->pll_u);
756err0:
757 kfree(phy);
758 return ERR_PTR(err);
759}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500760EXPORT_SYMBOL_GPL(tegra_usb_phy_open);
Benoit Goby91525d02011-03-09 16:28:55 -0800761
Venu Byravarasuab137d02013-01-24 15:57:03 +0530762void tegra_usb_phy_preresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800763{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530764 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
765
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000766 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800767 utmi_phy_preresume(phy);
768}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500769EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800770
Venu Byravarasuab137d02013-01-24 15:57:03 +0530771void tegra_usb_phy_postresume(struct usb_phy *x)
Benoit Goby91525d02011-03-09 16:28:55 -0800772{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530773 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
774
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000775 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800776 utmi_phy_postresume(phy);
777}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500778EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
Benoit Goby91525d02011-03-09 16:28:55 -0800779
Venu Byravarasuab137d02013-01-24 15:57:03 +0530780void tegra_ehci_phy_restore_start(struct usb_phy *x,
Benoit Goby91525d02011-03-09 16:28:55 -0800781 enum tegra_usb_phy_port_speed port_speed)
782{
Venu Byravarasuab137d02013-01-24 15:57:03 +0530783 struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
784
Venu Byravarasu3f9db1a2013-01-16 03:30:21 +0000785 if (!phy->is_ulpi_phy)
Benoit Goby91525d02011-03-09 16:28:55 -0800786 utmi_phy_restore_start(phy, port_speed);
787}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500788EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
Benoit Goby91525d02011-03-09 16:28:55 -0800789
Venu Byravarasuab137d02013-01-24 15:57:03 +0530790void tegra_ehci_phy_restore_end(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_restore_end(phy);
796}
Arnd Bergmann4265cbf2012-03-02 15:58:42 -0500797EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
Benoit Goby91525d02011-03-09 16:28:55 -0800798