]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/usb/phy/tegra11x_usb_phy.c
96a1e6db0a58c97d03f08e302350e1d3b2b783ee
[sojka/nv-tegra/linux-3.10.git] / drivers / usb / phy / tegra11x_usb_phy.c
1 /*
2  * drivers/usb/phy/tegra11x_usb_phy.c
3  *
4  * Copyright (c) 2012-2014 NVIDIA Corporation. All rights reserved.
5  *
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 #include <linux/resource.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/platform_device.h>
23 #include <linux/io.h>
24 #include <linux/gpio.h>
25 #include <linux/interrupt.h>
26 #include <linux/clk.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/platform_data/tegra_usb.h>
29 #include <linux/clk/tegra.h>
30 #include <linux/tegra-soc.h>
31 #include <linux/tegra-fuse.h>
32 #include <mach/pinmux.h>
33 #include <mach/tegra_usb_pmc.h>
34 #include <mach/tegra_usb_pad_ctrl.h>
35
36 #ifdef CONFIG_ARCH_TEGRA_14x_SOC
37 #include <mach/pinmux-t14.h>
38 #else
39 #include <mach/pinmux-t11.h>
40 #endif
41 #include "tegra_usb_phy.h"
42 #include "../../../arch/arm/mach-tegra/gpio-names.h"
43
44 /* HACK! This needs to come from DT */
45 #include "../../../arch/arm/mach-tegra/iomap.h"
46
47 #define USB_USBCMD              0x130
48 #define   USB_USBCMD_RS         (1 << 0)
49 #define   USB_CMD_RESET (1<<1)
50
51 #define USB_USBSTS              0x134
52 #define   USB_USBSTS_PCI        (1 << 2)
53 #define   USB_USBSTS_SRI        (1 << 7)
54 #define   USB_USBSTS_HCH        (1 << 12)
55
56 #define USB_TXFILLTUNING        0x154
57 #define   USB_FIFO_TXFILL_THRES(x)   (((x) & 0x1f) << 16)
58 #define   USB_FIFO_TXFILL_MASK    0x1f0000
59
60 #define USB_ASYNCLISTADDR       0x148
61
62 #define ICUSB_CTRL              0x15c
63
64 #define USB_USBMODE             0x1f8
65 #define   USB_USBMODE_MASK              (3 << 0)
66 #define   USB_USBMODE_HOST              (3 << 0)
67 #define   USB_USBMODE_DEVICE            (2 << 0)
68
69 #define USB_SUSP_CTRL           0x400
70 #define   USB_WAKE_ON_CNNT_EN_DEV       (1 << 3)
71 #define   USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
72 #define   USB_SUSP_CLR                  (1 << 5)
73 #define   USB_CLKEN             (1 << 6)
74 #define   USB_PHY_CLK_VALID             (1 << 7)
75 #define   USB_PHY_CLK_VALID_INT_ENB     (1 << 9)
76 #define   USB_PHY_CLK_VALID_INT_STS     (1 << 8)
77 #define   UTMIP_RESET                   (1 << 11)
78 #define   UTMIP_PHY_ENABLE              (1 << 12)
79 #define   UHSIC_RESET                   (1 << 14)
80 #define   USB_WAKEUP_DEBOUNCE_COUNT(x)  (((x) & 0x7) << 16)
81 #define   UHSIC_PHY_ENABLE              (1 << 19)
82
83 #define USB_PHY_VBUS_WAKEUP_ID  0x408
84 #define   DIV_DET_EN            (1 << 31)
85 #define   VDCD_DET_STS          (1 << 26)
86 #define   VDCD_DET_CHG_DET      (1 << 25)
87 #define   VOP_DIV2P7_DET        (1 << 23)
88 #define   VOP_DIV2P0_DET        (1 << 22)
89 #define   VON_DIV2P7_DET        (1 << 15)
90 #define   VON_DIV2P0_DET        (1 << 14)
91 #define   VDAT_DET_INT_EN       (1 << 16)
92 #define   VDAT_DET_CHG_DET      (1 << 17)
93 #define   VDAT_DET_STS          (1 << 18)
94 #define   USB_ID_STATUS         (1 << 2)
95
96 #define USB_IF_SPARE    0x498
97 #define   USB_HS_RSM_EOP_EN         (1 << 4)
98 #define   USB_PORT_SUSPEND_EN       (1 << 5)
99
100 #define USB_NEW_CONTROL  0x4c0
101 #define   USB_COHRENCY_EN           (1 << 0)
102 #define   USB_MEM_ALLIGNMENT_MUX_EN (1 << 1)
103
104 #define UTMIP_XCVR_CFG0         0x808
105 #define   UTMIP_XCVR_SETUP(x)                   (((x) & 0xf) << 0)
106 #define   UTMIP_XCVR_LSRSLEW(x)                 (((x) & 0x3) << 8)
107 #define   UTMIP_XCVR_LSFSLEW(x)                 (((x) & 0x3) << 10)
108 #define   UTMIP_FORCE_PD_POWERDOWN              (1 << 14)
109 #define   UTMIP_FORCE_PD2_POWERDOWN             (1 << 16)
110 #define   UTMIP_FORCE_PDZI_POWERDOWN            (1 << 18)
111 #define   UTMIP_XCVR_LSBIAS_SEL                 (1 << 21)
112 #define   UTMIP_XCVR_SETUP_MSB(x)               (((x) & 0x7) << 22)
113 #define   UTMIP_XCVR_HSSLEW_MSB(x)              (((x) & 0x7f) << 25)
114 #define   UTMIP_XCVR_HSSLEW_LSB(x)              (((x) & 0x3) << 4)
115 #define   UTMIP_XCVR_MAX_OFFSET         2
116 #define   UTMIP_XCVR_SETUP_MAX_VALUE    0x7f
117 #define   UTMIP_XCVR_SETUP_MIN_VALUE    0
118 #define   XCVR_SETUP_MSB_CALIB(x) ((x) >> 4)
119
120 #define UTMIP_HSRX_CFG0         0x810
121 #define   UTMIP_ELASTIC_LIMIT(x)        (((x) & 0x1f) << 10)
122 #define   UTMIP_IDLE_WAIT(x)            (((x) & 0x1f) << 15)
123
124 #define UTMIP_HSRX_CFG1         0x814
125 #define   UTMIP_HS_SYNC_START_DLY(x)    (((x) & 0x1f) << 1)
126
127 #define UTMIP_TX_CFG0           0x820
128 #define   UTMIP_FS_PREABMLE_J           (1 << 19)
129 #define   UTMIP_HS_DISCON_DISABLE       (1 << 8)
130
131 #define UTMIP_DEBOUNCE_CFG0 0x82c
132 #define   UTMIP_BIAS_DEBOUNCE_A(x)      (((x) & 0xffff) << 0)
133
134 #define UTMIP_BAT_CHRG_CFG0 0x830
135 #define   UTMIP_PD_CHRG                 (1 << 0)
136 #define   UTMIP_OP_SINK_EN              (1 << 1)
137 #define   UTMIP_ON_SINK_EN              (1 << 2)
138 #define   UTMIP_OP_SRC_EN               (1 << 3)
139 #define   UTMIP_ON_SRC_EN               (1 << 4)
140 #define   UTMIP_OP_I_SRC_EN             (1 << 5)
141
142 #define UTMIP_XCVR_CFG1         0x838
143 #define   UTMIP_FORCE_PDDISC_POWERDOWN  (1 << 0)
144 #define   UTMIP_FORCE_PDCHRP_POWERDOWN  (1 << 2)
145 #define   UTMIP_FORCE_PDDR_POWERDOWN    (1 << 4)
146 #define   UTMIP_XCVR_TERM_RANGE_ADJ(x)  (((x) & 0xf) << 18)
147
148 #define UTMIP_MISC_CFG0         0x824
149 #define   UTMIP_DPDM_OBSERVE            (1 << 26)
150 #define   UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
151 #define   UTMIP_DPDM_OBSERVE_SEL_FS_J   UTMIP_DPDM_OBSERVE_SEL(0xf)
152 #define   UTMIP_DPDM_OBSERVE_SEL_FS_K   UTMIP_DPDM_OBSERVE_SEL(0xe)
153 #define   UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
154 #define   UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
155 #define   UTMIP_SUSPEND_EXIT_ON_EDGE    (1 << 22)
156 #define   DISABLE_PULLUP_DP (1 << 15)
157 #define   DISABLE_PULLUP_DM (1 << 14)
158 #define   DISABLE_PULLDN_DP (1 << 13)
159 #define   DISABLE_PULLDN_DM (1 << 12)
160 #define   FORCE_PULLUP_DP       (1 << 11)
161 #define   FORCE_PULLUP_DM       (1 << 10)
162 #define   FORCE_PULLDN_DP       (1 << 9)
163 #define   FORCE_PULLDN_DM       (1 << 8)
164 #define   COMB_TERMS            (1 << 0)
165 #define   ALWAYS_FREE_RUNNING_TERMS (1 << 1)
166 #define   MASK_ALL_PULLUP_PULLDOWN (0xff << 8)
167
168 #define UTMIP_SPARE_CFG0        0x834
169 #define   FUSE_SETUP_SEL                (1 << 3)
170 #define   FUSE_ATERM_SEL                (1 << 4)
171
172 #define UHSIC_PLL_CFG1                          0xc04
173 #define   UHSIC_XTAL_FREQ_COUNT(x)              (((x) & 0xfff) << 0)
174 #define   UHSIC_PLLU_ENABLE_DLY_COUNT(x)        (((x) & 0x1f) << 14)
175
176 #define UHSIC_HSRX_CFG0                         0xc08
177 #define   UHSIC_ELASTIC_UNDERRUN_LIMIT(x)       (((x) & 0x1f) << 2)
178 #define   UHSIC_ELASTIC_OVERRUN_LIMIT(x)        (((x) & 0x1f) << 8)
179 #define   UHSIC_IDLE_WAIT(x)                    (((x) & 0x1f) << 13)
180
181 #define UHSIC_HSRX_CFG1                         0xc0c
182 #define   UHSIC_HS_SYNC_START_DLY(x)            (((x) & 0x1f) << 1)
183
184 #define UHSIC_TX_CFG0                           0xc10
185 #define   UHSIC_HS_READY_WAIT_FOR_VALID (1 << 9)
186 #define   UHSIC_HS_POSTAMBLE_OUTPUT_ENABLE      (1 << 6)
187
188
189 #define UHSIC_MISC_CFG0                         0xc14
190 #define   UHSIC_SUSPEND_EXIT_ON_EDGE            (1 << 7)
191 #define   UHSIC_DETECT_SHORT_CONNECT            (1 << 8)
192 #define   UHSIC_FORCE_XCVR_MODE                 (1 << 15)
193 #define   UHSIC_DISABLE_BUSRESET                (1 << 20)
194
195 #define UHSIC_MISC_CFG1                         0xc18
196 #define   UHSIC_PLLU_STABLE_COUNT(x)            (((x) & 0xfff) << 2)
197
198 #define UHSIC_PADS_CFG0                         0xc1c
199 #define   UHSIC_TX_RTUNEN                       0xf000
200 #define   UHSIC_TX_RTUNEP                       0xf00
201 #define   UHSIC_TX_RTUNE_P(x)                   (((x) & 0xf) << 8)
202 #define   UHSIC_TX_SLEWP                        (0xf << 16)
203 #define   UHSIC_TX_SLEWN                        (0xf << 20)
204
205 #define UHSIC_PADS_CFG1                         0xc20
206 #define   UHSIC_AUTO_RTERM_EN                   (1 << 0)
207 #define   UHSIC_PD_BG                           (1 << 2)
208 #define   UHSIC_PD_TX                           (1 << 3)
209 #define   UHSIC_PD_TRK                          (1 << 4)
210 #define   UHSIC_PD_RX                           (1 << 5)
211 #define   UHSIC_PD_ZI                           (1 << 6)
212 #define   UHSIC_RX_SEL                          (1 << 7)
213 #define   UHSIC_RPD_DATA                        (1 << 9)
214 #define   UHSIC_RPD_STROBE                      (1 << 10)
215 #define   UHSIC_RPU_DATA                        (1 << 11)
216 #define   UHSIC_RPU_STROBE                      (1 << 12)
217
218 #define UHSIC_SPARE_CFG0                        0xc2c
219 #define   FORCE_BK_ON                           (1 << 12)
220
221 #define UHSIC_STAT_CFG0                 0xc28
222 #define   UHSIC_CONNECT_DETECT          (1 << 0)
223
224 #define UHSIC_STATUS(inst)              UHSIC_INST(inst, 0x214, 0x290)
225 #define   UHSIC_WAKE_ALARM(inst)        (1 << UHSIC_INST(inst, 19, 4))
226 #define   UHSIC_WALK_PTR_VAL(inst)      (0x3 << UHSIC_INST(inst, 6, 0))
227 #define   UHSIC_DATA_VAL(inst)          (1 << UHSIC_INST(inst, 15, 3))
228 #define   UHSIC_STROBE_VAL(inst)        (1 << UHSIC_INST(inst, 14, 2))
229
230 #define UHSIC_CMD_CFG0                  0xc24
231 #define   UHSIC_PRETEND_CONNECT_DETECT  (1 << 5)
232
233 #define USB_USBINTR                                             0x138
234
235 #define UTMIP_STATUS            0x214
236 #define   UTMIP_WALK_PTR_VAL(inst)      (0x3 << ((inst)*2))
237 #define   UTMIP_USBOP_VAL(inst)         (1 << ((2*(inst)) + 8))
238 #define   UTMIP_USBOP_VAL_P2            (1 << 12)
239 #define   UTMIP_USBOP_VAL_P1            (1 << 10)
240 #define   UTMIP_USBOP_VAL_P0            (1 << 8)
241 #define   UTMIP_USBON_VAL(inst)         (1 << ((2*(inst)) + 9))
242 #define   UTMIP_USBON_VAL_P2            (1 << 13)
243 #define   UTMIP_USBON_VAL_P1            (1 << 11)
244 #define   UTMIP_USBON_VAL_P0            (1 << 9)
245 #define   UTMIP_WAKE_ALARM(inst)                (1 << ((inst) + 16))
246 #define   UTMIP_WAKE_ALARM_P2   (1 << 18)
247 #define   UTMIP_WAKE_ALARM_P1   (1 << 17)
248 #define   UTMIP_WAKE_ALARM_P0   (1 << 16)
249 #define   UTMIP_WALK_PTR(inst)  (1 << ((inst)*2))
250 #define   UTMIP_WALK_PTR_P2     (1 << 4)
251 #define   UTMIP_WALK_PTR_P1     (1 << 2)
252 #define   UTMIP_WALK_PTR_P0     (1 << 0)
253
254 #define USB1_PREFETCH_ID               6
255 #define USB2_PREFETCH_ID               18
256 #define USB3_PREFETCH_ID               17
257
258 #define FUSE_USB_CALIB_0                0x1F0
259 #define   XCVR_SETUP(x) (((x) & 0x7F) << 0)
260 #define   XCVR_SETUP_LSB_MASK   0xF
261 #define   XCVR_SETUP_MSB_MASK   0x70
262 #define   XCVR_SETUP_LSB_MAX_VAL        0xF
263
264 #define APB_MISC_GP_OBSCTRL_0   0x818
265 #define APB_MISC_GP_OBSDATA_0   0x81c
266
267 #define PADCTL_SNPS_OC_MAP      0xC
268 #define   CONTROLLER_OC(inst, x)        (((x) & 0x7) << (3 * (inst)))
269 #define   CONTROLLER_OC_P0(x)   (((x) & 0x7) << 0)
270 #define   CONTROLLER_OC_P1(x)   (((x) & 0x7) << 3)
271 #define   CONTROLLER_OC_P2(x)   (((x) & 0x7) << 6)
272
273 #define PADCTL_OC_DET           0x18
274 #define   ENABLE0_OC_MAP(x)     (((x) & 0x7) << 10)
275 #define   ENABLE1_OC_MAP(x)     (((x) & 0x7) << 13)
276
277 #define TEGRA_STREAM_DISABLE    0x1f8
278 #define TEGRA_STREAM_DISABLE_OFFSET     (1 << 4)
279
280 /* These values (in milli second) are taken from the battery charging spec */
281 #define TDP_SRC_ON_MS    100
282 #define TDPSRC_CON_MS    40
283
284 /* Maxim Debounce Timer */
285 #define MAXIM_DEBOUNCE_TIME 120
286
287 /* Force port resume wait time in micro second on remote resume */
288 #define FPR_WAIT_TIME_US 25000
289
290 /* define HSIC phy params */
291 #define HSIC_SYNC_START_DELAY           9
292 #define HSIC_IDLE_WAIT_DELAY            17
293 #define HSIC_ELASTIC_UNDERRUN_LIMIT     16
294 #define HSIC_ELASTIC_OVERRUN_LIMIT      16
295
296 struct tegra_usb_pmc_data pmc_data[3];
297
298 static struct tegra_xtal_freq utmip_freq_table[] = {
299         {
300                 .freq = 12000000,
301                 .enable_delay = 0x02,
302                 .stable_count = 0x2F,
303                 .active_delay = 0x04,
304                 .xtal_freq_count = 0x76,
305                 .debounce = 0x7530,
306                 .pdtrk_count = 5,
307         },
308         {
309                 .freq = 13000000,
310                 .enable_delay = 0x02,
311                 .stable_count = 0x33,
312                 .active_delay = 0x05,
313                 .xtal_freq_count = 0x7F,
314                 .debounce = 0x7EF4,
315                 .pdtrk_count = 5,
316         },
317         {
318                 .freq = 19200000,
319                 .enable_delay = 0x03,
320                 .stable_count = 0x4B,
321                 .active_delay = 0x06,
322                 .xtal_freq_count = 0xBB,
323                 .debounce = 0xBB80,
324                 .pdtrk_count = 7,
325         },
326         {
327                 .freq = 26000000,
328                 .enable_delay = 0x04,
329                 .stable_count = 0x66,
330                 .active_delay = 0x09,
331                 .xtal_freq_count = 0xFE,
332                 .debounce = 0xFDE8,
333                 .pdtrk_count = 9,
334         },
335 };
336
337 static struct tegra_xtal_freq uhsic_freq_table[] = {
338         {
339                 .freq = 12000000,
340                 .enable_delay = 0x02,
341                 .stable_count = 0x2F,
342                 .active_delay = 0x0,
343                 .xtal_freq_count = 0x1CA,
344         },
345         {
346                 .freq = 13000000,
347                 .enable_delay = 0x02,
348                 .stable_count = 0x33,
349                 .active_delay = 0x0,
350                 .xtal_freq_count = 0x1F0,
351         },
352         {
353                 .freq = 19200000,
354                 .enable_delay = 0x03,
355                 .stable_count = 0x4B,
356                 .active_delay = 0x0,
357                 .xtal_freq_count = 0x2DD,
358         },
359         {
360                 .freq = 26000000,
361                 .enable_delay = 0x04,
362                 .stable_count = 0x66,
363                 .active_delay = 0x0,
364                 .xtal_freq_count = 0x3E0,
365         },
366 };
367
368 static void pmc_init(struct tegra_usb_phy *phy)
369 {
370         pmc_data[phy->inst].instance = phy->inst;
371         pmc_data[phy->inst].phy_type = phy->pdata->phy_intf;
372         pmc_data[phy->inst].controller_type = TEGRA_USB_2_0;
373         pmc_data[phy->inst].usb_base = phy->regs;
374         tegra_usb_pmc_init(&pmc_data[phy->inst]);
375 }
376
377 static int _usb_phy_init(struct tegra_usb_phy *phy)
378 {
379         unsigned long val;
380         void __iomem *base = phy->regs;
381
382         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
383
384         val = readl(base + HOSTPC1_DEVLC);
385         val &= ~HOSTPC1_DEVLC_STS;
386         writel(val, base + HOSTPC1_DEVLC);
387
388         val = readl(base + USB_IF_SPARE);
389         val |= USB_HS_RSM_EOP_EN;
390         val |= USB_PORT_SUSPEND_EN;
391         writel(val, base + USB_IF_SPARE);
392
393         if (phy->pdata->unaligned_dma_buf_supported == true) {
394                 val = readl(base + USB_NEW_CONTROL);
395                 val |= USB_COHRENCY_EN;
396                 val |= USB_MEM_ALLIGNMENT_MUX_EN;
397                 writel(val, base + USB_NEW_CONTROL);
398         }
399         val =  readl(base + TEGRA_STREAM_DISABLE);
400         if (!tegra_platform_is_silicon())
401                 val |= TEGRA_STREAM_DISABLE_OFFSET;
402         else
403                 val &= ~TEGRA_STREAM_DISABLE_OFFSET;
404         writel(val , base + TEGRA_STREAM_DISABLE);
405
406         return 0;
407 }
408
409 static void usb_phy_fence_read(struct tegra_usb_phy *phy)
410 {
411         /* Fence read for coherency of AHB master intiated writes */
412         if (phy->inst == 0)
413                 readb(IO_ADDRESS(IO_PPCS_PHYS + USB1_PREFETCH_ID));
414         else if (phy->inst == 1)
415                 readb(IO_ADDRESS(IO_PPCS_PHYS + USB2_PREFETCH_ID));
416         else if (phy->inst == 2)
417                 readb(IO_ADDRESS(IO_PPCS_PHYS + USB3_PREFETCH_ID));
418         return;
419 }
420
421 static int usb_phy_reset(struct tegra_usb_phy *phy)
422 {
423         unsigned long val;
424         void __iomem *base = phy->regs;
425
426         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
427
428         if (!tegra_platform_is_silicon()) {
429                 val =  readl(base + TEGRA_STREAM_DISABLE);
430                 val |= TEGRA_STREAM_DISABLE_OFFSET;
431                 writel(val , base + TEGRA_STREAM_DISABLE);
432         }
433         val = readl(base + USB_TXFILLTUNING);
434         if ((val & USB_FIFO_TXFILL_MASK) != USB_FIFO_TXFILL_THRES(0x10)) {
435                 val = USB_FIFO_TXFILL_THRES(0x10);
436                 writel(val, base + USB_TXFILLTUNING);
437         }
438
439 return 0;
440 }
441
442 static bool utmi_phy_remotewake_detected(struct tegra_usb_phy *phy)
443 {
444         void __iomem *base = phy->regs;
445         unsigned  int inst = phy->inst;
446         u32 val;
447
448         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
449         val = readl(base + UTMIP_PMC_WAKEUP0);
450         if (val & EVENT_INT_ENB) {
451                 val = tegra_usb_pmc_reg_read(UTMIP_STATUS);
452                 if (UTMIP_WAKE_ALARM(inst) & val) {
453                         tegra_usb_pmc_reg_update(PMC_SLEEP_CFG,
454                                 UTMIP_WAKE_VAL(inst, 0xF),
455                                 UTMIP_WAKE_VAL(inst, WAKE_VAL_NONE));
456
457                         tegra_usb_pmc_reg_update(PMC_TRIGGERS, 0,
458                                 UTMIP_CLR_WAKE_ALARM(inst));
459
460                         val = readl(base + UTMIP_PMC_WAKEUP0);
461                         val &= ~EVENT_INT_ENB;
462                         writel(val, base + UTMIP_PMC_WAKEUP0);
463
464                         val = tegra_usb_pmc_reg_read(UTMIP_STATUS);
465                         if (phy->port_speed < USB_PHY_PORT_SPEED_UNKNOWN)
466                                 phy->pmc_remote_wakeup = true;
467                         else
468                                 phy->pmc_hotplug_wakeup = true;
469
470                         DBG("%s: utmip PMC interrupt detected\n", __func__);
471                         return true;
472                 }
473         }
474         return false;
475 }
476
477 static int usb_phy_bringup_host_controller(struct tegra_usb_phy *phy)
478 {
479         unsigned long val;
480         void __iomem *base = phy->regs;
481
482         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
483         PHY_DBG("[%d] USB_USBSTS[0x%x] USB_PORTSC[0x%x] port_speed[%d]\n",
484         __LINE__, readl(base + USB_USBSTS), readl(base + USB_PORTSC),
485                                                         phy->port_speed);
486
487         /* Device is plugged in when system is in LP0 */
488         /* Bring up the controller from LP0*/
489         val = readl(base + USB_USBCMD);
490         val |= USB_CMD_RESET;
491         writel(val, base + USB_USBCMD);
492
493         if (usb_phy_reg_status_wait(base + USB_USBCMD,
494                 USB_CMD_RESET, 0, 2500) < 0) {
495                 pr_err("%s: timeout waiting for reset\n", __func__);
496         }
497
498         val = readl(base + USB_USBMODE);
499         val &= ~USB_USBMODE_MASK;
500         val |= USB_USBMODE_HOST;
501         writel(val, base + USB_USBMODE);
502         val = readl(base + HOSTPC1_DEVLC);
503         val &= ~HOSTPC1_DEVLC_PTS(~0);
504
505         if (phy->pdata->phy_intf == TEGRA_USB_PHY_INTF_HSIC)
506                 val |= HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_HSIC);
507         else
508                 val |= HOSTPC1_DEVLC_STS;
509         writel(val, base + HOSTPC1_DEVLC);
510
511         /* Enable Port Power */
512         val = readl(base + USB_PORTSC);
513         val |= USB_PORTSC_PP;
514         writel(val, base + USB_PORTSC);
515         udelay(10);
516
517         /* Check if the phy resume from LP0. When the phy resume from LP0
518          * USB register will be reset.to zero */
519         if (!readl(base + USB_ASYNCLISTADDR)) {
520                 /* Program the field PTC based on the saved speed mode */
521                 val = readl(base + USB_PORTSC);
522                 val &= ~USB_PORTSC_PTC(~0);
523                 if ((phy->port_speed == USB_PHY_PORT_SPEED_HIGH) ||
524                         (phy->pdata->phy_intf == TEGRA_USB_PHY_INTF_HSIC))
525                         val |= USB_PORTSC_PTC(5);
526                 else if (phy->port_speed == USB_PHY_PORT_SPEED_FULL)
527                         val |= USB_PORTSC_PTC(6);
528                 else if (phy->port_speed == USB_PHY_PORT_SPEED_LOW)
529                         val |= USB_PORTSC_PTC(7);
530                 writel(val, base + USB_PORTSC);
531                 udelay(10);
532
533                 /* Disable test mode by setting PTC field to NORMAL_OP */
534                 val = readl(base + USB_PORTSC);
535                 val &= ~USB_PORTSC_PTC(~0);
536                 writel(val, base + USB_PORTSC);
537                 udelay(10);
538         }
539
540         /* Poll until CCS is enabled */
541         if (usb_phy_reg_status_wait(base + USB_PORTSC, USB_PORTSC_CCS,
542                                                  USB_PORTSC_CCS, 2000)) {
543                 pr_err("%s: timeout waiting for USB_PORTSC_CCS\n", __func__);
544         }
545
546         /* Poll until PE is enabled */
547         if (usb_phy_reg_status_wait(base + USB_PORTSC, USB_PORTSC_PE,
548                                                  USB_PORTSC_PE, 2000)) {
549                 pr_err("%s: timeout waiting for USB_PORTSC_PE\n", __func__);
550         }
551
552         /* Clear the PCI status, to avoid an interrupt taken upon resume */
553         val = readl(base + USB_USBSTS);
554         val |= USB_USBSTS_PCI;
555         writel(val, base + USB_USBSTS);
556
557         phy->ctrlr_suspended = false;
558         if (!phy->pmc_remote_wakeup) {
559                 /* Put controller in suspend mode by writing 1
560                  * to SUSP bit of PORTSC */
561                 val = readl(base + USB_PORTSC);
562                 if ((val & USB_PORTSC_PP) && (val & USB_PORTSC_PE)) {
563                         val |= USB_PORTSC_SUSP;
564                         writel(val, base + USB_PORTSC);
565                         phy->ctrlr_suspended = true;
566                         /* Wait until port suspend completes */
567                         if (usb_phy_reg_status_wait(base + USB_PORTSC,
568                                 USB_PORTSC_SUSP, USB_PORTSC_SUSP, 4000)) {
569                                 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
570                                         __func__);
571                         }
572                 }
573         }
574         PHY_DBG("[%d] USB_USBSTS[0x%x] USB_PORTSC[0x%x] port_speed[%d]\n",
575                 __LINE__, readl(base + USB_USBSTS), readl(base + USB_PORTSC),
576                 phy->port_speed);
577
578         DBG("USB_USBSTS[0x%x] USB_PORTSC[0x%x]\n",
579                         readl(base + USB_USBSTS), readl(base + USB_PORTSC));
580         return 0;
581 }
582
583 static unsigned int utmi_phy_xcvr_setup_value(struct tegra_usb_phy *phy)
584 {
585         struct tegra_utmi_config *cfg = &phy->pdata->u_cfg.utmi;
586         signed long val;
587
588         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
589
590         if (cfg->xcvr_use_fuses) {
591                 val = XCVR_SETUP(tegra_fuse_readl(FUSE_USB_CALIB_0));
592                 if (cfg->xcvr_use_lsb) {
593                         val = min((unsigned int) ((val & XCVR_SETUP_LSB_MASK)
594                                 + cfg->xcvr_setup_offset),
595                                 (unsigned int) XCVR_SETUP_LSB_MAX_VAL);
596                         val |= (cfg->xcvr_setup & XCVR_SETUP_MSB_MASK);
597                 } else {
598                         if (cfg->xcvr_setup_offset <= UTMIP_XCVR_MAX_OFFSET)
599                                 val = val + cfg->xcvr_setup_offset;
600
601                         if (val > UTMIP_XCVR_SETUP_MAX_VALUE) {
602                                 val = UTMIP_XCVR_SETUP_MAX_VALUE;
603                                 pr_info("%s: reset XCVR_SETUP to max value\n",
604                                                 __func__);
605                         } else if (val < UTMIP_XCVR_SETUP_MIN_VALUE) {
606                                 val = UTMIP_XCVR_SETUP_MIN_VALUE;
607                                 pr_info("%s: reset XCVR_SETUP to min value\n",
608                                                 __func__);
609                         }
610                 }
611         } else {
612                 val = cfg->xcvr_setup;
613         }
614
615         return (unsigned int) val;
616 }
617
618 static int utmi_phy_open(struct tegra_usb_phy *phy)
619 {
620         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
621
622         unsigned long parent_rate;
623         int i;
624
625         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
626
627         phy->utmi_pad_clk = clk_get_sys("utmip-pad", NULL);
628         if (IS_ERR(phy->utmi_pad_clk)) {
629                 pr_err("%s: can't get utmip pad clock\n", __func__);
630                 return PTR_ERR(phy->utmi_pad_clk);
631         }
632         pmc_init(phy);
633
634         phy->utmi_xcvr_setup = utmi_phy_xcvr_setup_value(phy);
635
636         if (tegra_platform_is_silicon()) {
637                 parent_rate = clk_get_rate(clk_get_parent(phy->pllu_clk));
638                 for (i = 0; i < ARRAY_SIZE(utmip_freq_table); i++) {
639                         if (utmip_freq_table[i].freq == parent_rate) {
640                                 phy->freq = &utmip_freq_table[i];
641                                 break;
642                         }
643                 }
644                 if (!phy->freq) {
645                         pr_err("invalid pll_u parent rate %ld\n", parent_rate);
646                         return -EINVAL;
647                 }
648         }
649
650         /* Power-up the VBUS, ID detector for UTMIP PHY */
651         if (phy->pdata->id_det_type == TEGRA_USB_ID)
652                 tegra_usb_pmc_reg_update(PMC_USB_AO,
653                         PMC_USB_AO_VBUS_WAKEUP_PD_P0 | PMC_USB_AO_ID_PD_P0, 0);
654         else
655                 tegra_usb_pmc_reg_update(PMC_USB_AO,
656                         PMC_USB_AO_VBUS_WAKEUP_PD_P0, PMC_USB_AO_ID_PD_P0);
657
658         pmc->pmc_ops->powerup_pmc_wake_detect(pmc);
659
660         return 0;
661 }
662
663 static void utmi_phy_close(struct tegra_usb_phy *phy)
664 {
665         unsigned long val;
666         void __iomem *base = phy->regs;
667         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
668
669         DBG("%s inst:[%d]\n", __func__, phy->inst);
670
671         /* Disable PHY clock valid interrupts while going into suspend*/
672         if (phy->hot_plug) {
673                 val = readl(base + USB_SUSP_CTRL);
674                 val &= ~USB_PHY_CLK_VALID_INT_ENB;
675                 writel(val, base + USB_SUSP_CTRL);
676         }
677
678         val = tegra_usb_pmc_reg_read(PMC_SLEEP_CFG);
679         if (val & UTMIP_MASTER_ENABLE(phy->inst)) {
680                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
681
682                 phy->pmc_remote_wakeup = false;
683                 phy->pmc_hotplug_wakeup = false;
684                 PHY_DBG("%s DISABLE_PMC inst = %d\n", __func__, phy->inst);
685         }
686
687         clk_put(phy->utmi_pad_clk);
688 }
689
690 static int utmi_phy_irq(struct tegra_usb_phy *phy)
691 {
692         void __iomem *base = phy->regs;
693         unsigned long val = 0;
694         bool remote_wakeup = false;
695         int irq_status = IRQ_HANDLED;
696
697         if (phy->phy_clk_on) {
698                 DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
699                 DBG("USB_USBSTS[0x%x] USB_PORTSC[0x%x]\n",
700                         readl(base + USB_USBSTS), readl(base + USB_PORTSC));
701                 DBG("USB_USBMODE[0x%x] USB_USBCMD[0x%x]\n",
702                         readl(base + USB_USBMODE), readl(base + USB_USBCMD));
703         }
704         if (!phy->pdata->unaligned_dma_buf_supported)
705                 usb_phy_fence_read(phy);
706         /* check if it is pmc wake event */
707         if (utmi_phy_remotewake_detected(phy))
708                 remote_wakeup = phy->pmc_remote_wakeup;
709
710         if (phy->hot_plug) {
711                 val = readl(base + USB_SUSP_CTRL);
712                 if ((val  & USB_PHY_CLK_VALID_INT_STS) &&
713                         (val  & USB_PHY_CLK_VALID_INT_ENB)) {
714                         val &= ~USB_PHY_CLK_VALID_INT_ENB |
715                                         USB_PHY_CLK_VALID_INT_STS;
716                         writel(val , (base + USB_SUSP_CTRL));
717
718                         /* In case of remote wakeup PHY clock will not up
719                          * immediately, so should not access any controller
720                          * register but normal plug-in/plug-out should be
721                          * executed
722                          */
723                         if (!remote_wakeup) {
724                                 val = readl(base + USB_USBSTS);
725                                 if (!(val  & USB_USBSTS_PCI)) {
726                                         irq_status = IRQ_NONE;
727                                         goto exit;
728                                 }
729
730                                 val = readl(base + USB_PORTSC);
731                                 if (val & USB_PORTSC_CCS)
732                                         val &= ~USB_PORTSC_WKCN;
733                                 else
734                                         val &= ~USB_PORTSC_WKDS;
735                                 val &= ~USB_PORTSC_RWC_BITS;
736                                 writel(val , (base + USB_PORTSC));
737                         }
738                 } else if (!phy->phy_clk_on) {
739                         if (remote_wakeup)
740                                 irq_status = IRQ_HANDLED;
741                         else
742                                 irq_status = IRQ_NONE;
743                         goto exit;
744                 }
745         }
746 exit:
747         return irq_status;
748 }
749
750 static void utmi_phy_enable_obs_bus(struct tegra_usb_phy *phy)
751 {
752         unsigned long val;
753         void __iomem *base = phy->regs;
754
755         /* (2LS WAR)is not required for LS and FS devices and is only for HS */
756         /* Force DP/DM pulldown active for Host mode */
757         val = readl(base + UTMIP_MISC_CFG0);
758         val |= FORCE_PULLDN_DM | FORCE_PULLDN_DP |
759                 COMB_TERMS | ALWAYS_FREE_RUNNING_TERMS;
760         writel(val, base + UTMIP_MISC_CFG0);
761         val = readl(base + UTMIP_MISC_CFG0);
762         val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
763         if (phy->port_speed == USB_PHY_PORT_SPEED_LOW)
764                 val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
765         else
766                 val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
767         writel(val, base + UTMIP_MISC_CFG0);
768         udelay(1);
769
770         val = readl(base + UTMIP_MISC_CFG0);
771         val |= UTMIP_DPDM_OBSERVE;
772         writel(val, base + UTMIP_MISC_CFG0);
773         udelay(10);
774         DBG("%s(%d) Enable OBS bus\n", __func__, __LINE__);
775         PHY_DBG("ENABLE_OBS_BUS\n");
776 }
777
778 static int utmi_phy_disable_obs_bus(struct tegra_usb_phy *phy)
779 {
780         void __iomem *base = phy->regs;
781         unsigned long val, port_speed;
782         unsigned long flags;
783         /* check if OBS bus is already enabled */
784         val = readl(base + UTMIP_MISC_CFG0);
785         if (val & UTMIP_DPDM_OBSERVE) {
786                 PHY_DBG("DISABLE_OBS_BUS\n");
787
788                 /* disable ALL interrupts on current CPU */
789                 local_irq_save(flags);
790
791                 /* Change the UTMIP OBS bus to drive SE0 */
792                 val = readl(base + UTMIP_MISC_CFG0);
793                 val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
794                 val |= UTMIP_DPDM_OBSERVE_SEL_FS_SE0;
795                 writel(val, base + UTMIP_MISC_CFG0);
796
797                 port_speed = (readl(base + USB_PORTSC) >> 26) &
798                         USB_PORTSC_PSPD_MASK;
799                 if (port_speed == USB_PHY_PORT_SPEED_FULL) {
800                         val = readl(base + UTMIP_MISC_CFG0);
801                         val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
802                         writel(val, base + UTMIP_MISC_CFG0);
803                 }
804
805                 /* Release UTMIP OBS bus */
806                 val = readl(base + UTMIP_MISC_CFG0);
807                 val &= ~UTMIP_DPDM_OBSERVE;
808                 writel(val, base + UTMIP_MISC_CFG0);
809
810                 /* Release DP/DM pulldown for Host mode */
811                 val = readl(base + UTMIP_MISC_CFG0);
812                 val &= ~(FORCE_PULLDN_DM | FORCE_PULLDN_DP |
813                 COMB_TERMS | ALWAYS_FREE_RUNNING_TERMS);
814                 writel(val, base + UTMIP_MISC_CFG0);
815
816                 val = readl(base + USB_USBCMD);
817                 val |= USB_USBCMD_RS;
818                 writel(val, base + USB_USBCMD);
819                 /* restore ALL interrupts on current CPU */
820                 local_irq_restore(flags);
821
822                 if (usb_phy_reg_status_wait(base + USB_USBCMD, USB_USBCMD_RS,
823                         USB_USBCMD_RS, 2000)) {
824                         pr_err("%s: timeout waiting for USB_USBCMD_RS\n",
825                                 __func__);
826                         return -ETIMEDOUT;
827                 }
828         }
829         return 0;
830 }
831
832 static int utmi_phy_post_resume(struct tegra_usb_phy *phy)
833 {
834         unsigned long val;
835         unsigned  int inst = phy->inst;
836         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
837
838         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
839         if (phy->port_speed == USB_PHY_PORT_SPEED_FULL) {
840                 val = tegra_usb_pmc_reg_read(PMC_SLEEP_CFG);
841                 /* if PMC is not disabled by now then disable it */
842                 if (val & UTMIP_MASTER_ENABLE(inst))
843                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
844
845                 utmi_phy_disable_obs_bus(phy);
846         }
847         return 0;
848 }
849
850 static int utmi_phy_pre_resume(struct tegra_usb_phy *phy, bool remote_wakeup)
851 {
852         unsigned long val;
853         unsigned  int inst = phy->inst;
854         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
855
856         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
857         val = tegra_usb_pmc_reg_read(PMC_SLEEP_CFG);
858         if (val & UTMIP_MASTER_ENABLE(inst)) {
859                 if (!remote_wakeup) {
860                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
861                         phy->pmc_remote_wakeup = false;
862                         phy->pmc_hotplug_wakeup = false;
863                         PHY_DBG("%s DISABLE_PMC inst = %d\n", __func__, inst);
864                 }
865         } else {
866                 if (phy->port_speed == USB_PHY_PORT_SPEED_FULL)
867                         utmi_phy_enable_obs_bus(phy);
868         }
869
870         return 0;
871 }
872
873 static int utmi_phy_power_off(struct tegra_usb_phy *phy)
874 {
875         unsigned long val;
876         void __iomem *base = phy->regs;
877         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
878
879         PHY_DBG("%s(%d) inst:[%d] BEGIN\n", __func__, __LINE__, phy->inst);
880         if (!phy->phy_clk_on) {
881                 PHY_DBG("%s(%d) inst:[%d] phy clk is already off\n",
882                                         __func__, __LINE__, phy->inst);
883                 return 0;
884         }
885
886         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_DEVICE) {
887                 pmc_data[phy->inst].is_xhci = phy->pdata->u_data.dev.is_xhci;
888                 pmc->pmc_ops->powerdown_pmc_wake_detect(pmc);
889                 val = readl(base + USB_SUSP_CTRL);
890                 val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
891                 val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
892                 writel(val, base + USB_SUSP_CTRL);
893
894                 val = readl(base + UTMIP_BAT_CHRG_CFG0);
895                 val |= UTMIP_PD_CHRG;
896                 writel(val, base + UTMIP_BAT_CHRG_CFG0);
897         } else {
898                 phy->port_speed = (readl(base + HOSTPC1_DEVLC) >> 25) &
899                                 HOSTPC1_DEVLC_PSPD_MASK;
900
901                 /* Disable interrupts */
902                 writel(0, base + USB_USBINTR);
903
904                 /* Clear the run bit to stop SOFs when USB is suspended */
905                 val = readl(base + USB_USBCMD);
906                 val &= ~USB_USBCMD_RS;
907                 writel(val, base + USB_USBCMD);
908
909                 if (usb_phy_reg_status_wait(base + USB_USBSTS, USB_USBSTS_HCH,
910                                                  USB_USBSTS_HCH, 2000)) {
911                         pr_err("%s: timeout waiting for USB_USBSTS_HCH\n"
912                                                         , __func__);
913                 }
914
915                 pmc_data[phy->inst].port_speed = (readl(base +
916                                 HOSTPC1_DEVLC) >> 25) &
917                                 HOSTPC1_DEVLC_PSPD_MASK;
918
919                 if (pmc_data[phy->inst].port_speed <
920                                         USB_PMC_PORT_SPEED_UNKNOWN)
921                         phy->pmc_remote_wakeup = false;
922                 else
923                         phy->pmc_hotplug_wakeup = false;
924
925                 if (phy->pdata->port_otg) {
926                         bool id_present = false;
927                         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
928                         id_present = (val & USB_ID_STATUS) ? false : true;
929                         if (id_present)
930                                 pmc->pmc_ops->setup_pmc_wake_detect(pmc);
931                 } else {
932                         pmc->pmc_ops->setup_pmc_wake_detect(pmc);
933                 }
934
935                 val = readl(base + UTMIP_PMC_WAKEUP0);
936                 val |= EVENT_INT_ENB;
937                 writel(val, base + UTMIP_PMC_WAKEUP0);
938                 PHY_DBG("%s ENABLE_PMC inst = %d\n", __func__, phy->inst);
939
940                 val = readl(base + USB_SUSP_CTRL);
941                 val &= ~USB_WAKE_ON_CNNT_EN_DEV;
942                 writel(val, base + USB_SUSP_CTRL);
943         }
944
945         if (!phy->hot_plug) {
946                 val = readl(base + UTMIP_XCVR_CFG0);
947                 val |= (UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
948                          UTMIP_FORCE_PDZI_POWERDOWN);
949                 writel(val, base + UTMIP_XCVR_CFG0);
950         }
951
952         val = readl(base + UTMIP_XCVR_CFG1);
953         val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
954                    UTMIP_FORCE_PDDR_POWERDOWN;
955         writel(val, base + UTMIP_XCVR_CFG1);
956
957         val = readl(base + UTMIP_BIAS_CFG1);
958         val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
959         writel(val, base + UTMIP_BIAS_CFG1);
960
961         if (phy->hot_plug) {
962                 bool enable_hotplug = true;
963                 /* if it is OTG port then make sure to enable hot-plug feature
964                    only if host adaptor is connected, i.e id is low */
965                 if (phy->pdata->port_otg) {
966                         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
967                         enable_hotplug = (val & USB_ID_STATUS) ? false : true;
968                 }
969                 if (enable_hotplug) {
970                         /* Enable wakeup event of device plug-in/plug-out */
971                         val = readl(base + USB_PORTSC);
972                         if (val & USB_PORTSC_CCS)
973                                 val |= USB_PORTSC_WKDS;
974                         else
975                                 val |= USB_PORTSC_WKCN;
976                         writel(val, base + USB_PORTSC);
977
978                         if (val & USB_PORTSC_CCS) {
979                                 val = readl(base + USB_SUSP_CTRL);
980                                 val &= ~USB_PHY_CLK_VALID_INT_ENB;
981                         } else {
982                                 val = readl(base + USB_SUSP_CTRL);
983                                 val |= USB_PHY_CLK_VALID_INT_ENB;
984                         }
985                         writel(val, base + USB_SUSP_CTRL);
986                 } else {
987                         /* Disable PHY clock valid interrupts while going into suspend*/
988                         val = readl(base + USB_SUSP_CTRL);
989                         val &= ~USB_PHY_CLK_VALID_INT_ENB;
990                         writel(val, base + USB_SUSP_CTRL);
991                 }
992         }
993
994         /* Disable PHY clock */
995         val = readl(base + HOSTPC1_DEVLC);
996         val |= HOSTPC1_DEVLC_PHCD;
997         writel(val, base + HOSTPC1_DEVLC);
998
999         if (!phy->hot_plug) {
1000                 val = readl(base + USB_SUSP_CTRL);
1001                 val |= UTMIP_RESET;
1002                 writel(val, base + USB_SUSP_CTRL);
1003         }
1004
1005         utmi_phy_pad_disable();
1006         utmi_phy_iddq_override(true);
1007
1008         phy->phy_clk_on = false;
1009         phy->hw_accessible = false;
1010
1011         PHY_DBG("%s(%d) inst:[%d] END\n", __func__, __LINE__, phy->inst);
1012
1013         return 0;
1014 }
1015
1016
1017 static int utmi_phy_power_on(struct tegra_usb_phy *phy)
1018 {
1019         unsigned long val;
1020         void __iomem *base = phy->regs;
1021 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
1022         void __iomem *padctl_base = IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE);
1023 #endif
1024         struct tegra_utmi_config *config = &phy->pdata->u_cfg.utmi;
1025         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1026
1027         PHY_DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1028         if (phy->phy_clk_on) {
1029                 PHY_DBG("%s(%d) inst:[%d] phy clk is already On\n",
1030                                         __func__, __LINE__, phy->inst);
1031                 return 0;
1032         }
1033         val = readl(base + USB_SUSP_CTRL);
1034         val |= UTMIP_RESET;
1035         writel(val, base + USB_SUSP_CTRL);
1036
1037         val = readl(base + UTMIP_TX_CFG0);
1038         val |= UTMIP_FS_PREABMLE_J;
1039         writel(val, base + UTMIP_TX_CFG0);
1040
1041         val = readl(base + USB_USBMODE);
1042         val &= ~USB_USBMODE_MASK;
1043         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_HOST)
1044                 val |= USB_USBMODE_HOST;
1045         else
1046                 val |= USB_USBMODE_DEVICE;
1047         writel(val, base + USB_USBMODE);
1048
1049         val = readl(base + UTMIP_HSRX_CFG0);
1050         val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
1051         val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
1052         val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
1053         writel(val, base + UTMIP_HSRX_CFG0);
1054
1055         val = readl(base + UTMIP_HSRX_CFG1);
1056         val &= ~UTMIP_HS_SYNC_START_DLY(~0);
1057         val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
1058         writel(val, base + UTMIP_HSRX_CFG1);
1059
1060         if (tegra_platform_is_silicon()) {
1061                 val = readl(base + UTMIP_DEBOUNCE_CFG0);
1062                 val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
1063                 val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
1064                 writel(val, base + UTMIP_DEBOUNCE_CFG0);
1065         }
1066
1067         val = readl(base + UTMIP_MISC_CFG0);
1068         val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
1069         writel(val, base + UTMIP_MISC_CFG0);
1070
1071         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_DEVICE) {
1072                 val = readl(base + USB_SUSP_CTRL);
1073                 val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
1074                 writel(val, base + USB_SUSP_CTRL);
1075
1076                 val = readl(base + UTMIP_BAT_CHRG_CFG0);
1077                 val &= ~UTMIP_PD_CHRG;
1078                 writel(val, base + UTMIP_BAT_CHRG_CFG0);
1079         } else {
1080                 val = readl(base + UTMIP_BAT_CHRG_CFG0);
1081                 val |= UTMIP_PD_CHRG;
1082                 writel(val, base + UTMIP_BAT_CHRG_CFG0);
1083         }
1084
1085         utmi_phy_pad_enable();
1086
1087         val = readl(base + UTMIP_XCVR_CFG0);
1088         val &= ~(UTMIP_XCVR_LSBIAS_SEL | UTMIP_FORCE_PD_POWERDOWN |
1089                  UTMIP_FORCE_PD2_POWERDOWN | UTMIP_FORCE_PDZI_POWERDOWN |
1090                  UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_LSFSLEW(~0) |
1091                  UTMIP_XCVR_LSRSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
1092         val |= UTMIP_XCVR_SETUP(phy->utmi_xcvr_setup);
1093         val |= UTMIP_XCVR_SETUP_MSB(XCVR_SETUP_MSB_CALIB(phy->utmi_xcvr_setup));
1094         val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
1095         val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
1096         if (!config->xcvr_use_lsb) {
1097                 if (!config->xcvr_hsslew_msb)
1098                         val |= UTMIP_XCVR_HSSLEW_MSB(3);
1099                 else
1100                         val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew_msb);
1101         }
1102         if (config->xcvr_hsslew_lsb)
1103                 val |= UTMIP_XCVR_HSSLEW_LSB(config->xcvr_hsslew_lsb);
1104         writel(val, base + UTMIP_XCVR_CFG0);
1105
1106         val = readl(base + UTMIP_XCVR_CFG1);
1107         val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
1108                  UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
1109         val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
1110         writel(val, base + UTMIP_XCVR_CFG1);
1111
1112         if (tegra_platform_is_silicon()) {
1113                 val = readl(base + UTMIP_BIAS_CFG1);
1114                 val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
1115                 val |= UTMIP_BIAS_PDTRK_COUNT(phy->freq->pdtrk_count);
1116                 writel(val, base + UTMIP_BIAS_CFG1);
1117         }
1118
1119         val = readl(base + UTMIP_SPARE_CFG0);
1120         val &= ~FUSE_SETUP_SEL;
1121         val |= FUSE_ATERM_SEL;
1122         writel(val, base + UTMIP_SPARE_CFG0);
1123
1124         val = readl(base + USB_SUSP_CTRL);
1125         val |= UTMIP_PHY_ENABLE;
1126         writel(val, base + USB_SUSP_CTRL);
1127
1128         val = readl(base + USB_SUSP_CTRL);
1129         val &= ~UTMIP_RESET;
1130         writel(val, base + USB_SUSP_CTRL);
1131
1132         /* Bring UTMIPLL out of IDDQ mode while exiting from reset/suspend */
1133         utmi_phy_iddq_override(false);
1134
1135         if (usb_phy_reg_status_wait(base + USB_SUSP_CTRL,
1136                 USB_PHY_CLK_VALID, USB_PHY_CLK_VALID, 2500))
1137                 pr_warn("%s: timeout waiting for phy to stabilize\n", __func__);
1138
1139         val = readl(base + HOSTPC1_DEVLC);
1140         val &= ~HOSTPC1_DEVLC_PHCD;
1141         writel(val, base + HOSTPC1_DEVLC);
1142
1143         utmi_phy_set_snps_trking_data();
1144
1145         if (phy->inst == 2)
1146                 writel(0, base + ICUSB_CTRL);
1147
1148         val = readl(base + USB_USBMODE);
1149         val &= ~USB_USBMODE_MASK;
1150         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_HOST)
1151                 val |= USB_USBMODE_HOST;
1152         else
1153                 val |= USB_USBMODE_DEVICE;
1154         writel(val, base + USB_USBMODE);
1155
1156         val = readl(base + HOSTPC1_DEVLC);
1157         val &= ~HOSTPC1_DEVLC_PTS(~0);
1158         val |= HOSTPC1_DEVLC_STS;
1159         writel(val, base + HOSTPC1_DEVLC);
1160
1161         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_DEVICE) {
1162                 pmc_data[phy->inst].is_xhci = phy->pdata->u_data.dev.is_xhci;
1163                 pmc->pmc_ops->powerup_pmc_wake_detect(pmc);
1164         }
1165
1166         if (!readl(base + USB_ASYNCLISTADDR))
1167                 _usb_phy_init(phy);
1168         val = readl(base + USB_TXFILLTUNING);
1169         if ((val & USB_FIFO_TXFILL_MASK) !=
1170                 USB_FIFO_TXFILL_THRES(0x10)) {
1171                 val = USB_FIFO_TXFILL_THRES(0x10);
1172                 writel(val, base + USB_TXFILLTUNING);
1173         }
1174
1175         phy->phy_clk_on = true;
1176         phy->hw_accessible = true;
1177
1178 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
1179         val = readl(padctl_base + PADCTL_SNPS_OC_MAP);
1180         val |= CONTROLLER_OC(phy->inst, 0x4);
1181         writel(val, padctl_base + PADCTL_SNPS_OC_MAP);
1182
1183         val = readl(padctl_base + PADCTL_OC_DET);
1184         if (phy->inst == 0)
1185                 val |= ENABLE0_OC_MAP(config->vbus_oc_map);
1186         if (phy->inst == 2)
1187                 val |= ENABLE1_OC_MAP(config->vbus_oc_map);
1188         writel(val, padctl_base + PADCTL_OC_DET);
1189 #endif
1190
1191         PHY_DBG("%s(%d) End inst:[%d]\n", __func__, __LINE__, phy->inst);
1192         return 0;
1193 }
1194
1195 static void utmi_phy_restore_start(struct tegra_usb_phy *phy)
1196 {
1197         unsigned long val;
1198         int inst = phy->inst;
1199         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1200
1201         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1202         val = tegra_usb_pmc_reg_read(UTMIP_STATUS);
1203         /* Check whether we wake up from the remote resume.
1204            For lp1 case, pmc is not responsible for waking the
1205            system, it's the flow controller and hence
1206            UTMIP_WALK_PTR_VAL(inst) will return 0.
1207            Also, for lp1 case phy->pmc_remote_wakeup will already be set
1208            to true by utmi_phy_irq() when the remote wakeup happens.
1209            Hence change the logic in the else part to enter only
1210            if phy->pmc_remote_wakeup is not set to true by the
1211            utmi_phy_irq(). */
1212         if (UTMIP_WALK_PTR_VAL(inst) & val) {
1213                 phy->pmc_remote_wakeup = true;
1214         } else if (!phy->pmc_remote_wakeup) {
1215                 val = tegra_usb_pmc_reg_read(PMC_SLEEP_CFG);
1216                 if (val & UTMIP_MASTER_ENABLE(inst))
1217                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1218         }
1219         if (phy->port_speed == USB_PHY_PORT_SPEED_FULL)
1220                 utmi_phy_enable_obs_bus(phy);
1221
1222 }
1223
1224 static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
1225 {
1226         unsigned long val, flags = 0;
1227         void __iomem *base = phy->regs;
1228         int wait_time_us = 25000; /* FPR should be set by this time */
1229         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1230
1231         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1232         /* check whether we wake up from the remote resume */
1233         if (phy->pmc_remote_wakeup) {
1234                 /* wait until SUSPEND and RESUME bit
1235                  * is cleared on remote resume */
1236                 do {
1237                         val = readl(base + USB_PORTSC);
1238                         udelay(1);
1239                         if (wait_time_us == 0) {
1240                                 PHY_DBG("%s PMC FPR" \
1241                                 "timeout val = 0x%x instance = %d\n", \
1242                                 __func__, (u32)val, phy->inst);
1243                                 if (phy->port_speed ==
1244                                         USB_PHY_PORT_SPEED_FULL) {
1245                                         val = readl(base + UTMIP_PMC_WAKEUP0);
1246                                         val &= ~EVENT_INT_ENB;
1247                                         writel(val, base + UTMIP_PMC_WAKEUP0);
1248                                 }
1249                                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1250                                 phy->pmc_remote_wakeup = false;
1251                                 phy->pmc_hotplug_wakeup = false;
1252
1253                                 if (phy->port_speed == USB_PHY_PORT_SPEED_FULL)
1254                                         utmi_phy_post_resume(phy);
1255                                 return;
1256                         }
1257                         wait_time_us--;
1258                 } while (val & (USB_PORTSC_RESUME | USB_PORTSC_SUSP));
1259
1260                 /* Add delay sothat resume will be driven for more than 20 ms */
1261                 if (phy->port_speed != USB_PHY_PORT_SPEED_FULL) {
1262                         usleep_range(10000, 11000);
1263                         local_irq_save(flags);
1264                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 1);
1265                         phy->pmc_remote_wakeup = false;
1266                         phy->pmc_hotplug_wakeup = false;
1267                         local_irq_restore(flags);
1268                 } else {
1269                         usleep_range(25000, 26000);
1270                         local_irq_save(flags);
1271                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1272                         phy->pmc_remote_wakeup = false;
1273                         phy->pmc_hotplug_wakeup = false;
1274                         local_irq_restore(flags);
1275                 }
1276
1277                 PHY_DBG("%s DISABLE_PMC inst = %d\n", __func__, phy->inst);
1278
1279                 if (usb_phy_reg_status_wait(base + USB_USBCMD, USB_USBCMD_RS,
1280                                                          USB_USBCMD_RS, 2000)) {
1281                         pr_err("%s: timeout waiting for USB_USBCMD_RS\n",\
1282                         __func__);
1283                 }
1284
1285                 /* Clear PCI and SRI bits to avoid an interrupt upon resume */
1286                 val = readl(base + USB_USBSTS);
1287                 writel(val, base + USB_USBSTS);
1288                 /* wait to avoid SOF if there is any */
1289                 if (usb_phy_reg_status_wait(base + USB_USBSTS,
1290                         USB_USBSTS_SRI, USB_USBSTS_SRI, 2500) < 0) {
1291                         pr_err("%s: timeout waiting for SOF\n", __func__);
1292                 }
1293         } else {
1294                 if (phy->port_speed != USB_PHY_PORT_SPEED_FULL) {
1295                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1296                         phy->pmc_remote_wakeup = false;
1297                         phy->pmc_hotplug_wakeup = false;
1298                         PHY_DBG("%s DISABLE_PMC inst = %d\n",
1299                                 __func__, phy->inst);
1300                 }
1301         }
1302         if (phy->port_speed == USB_PHY_PORT_SPEED_FULL)
1303                 utmi_phy_post_resume(phy);
1304 }
1305
1306 static int utmi_phy_resume(struct tegra_usb_phy *phy)
1307 {
1308         int status = 0;
1309         unsigned long val;
1310         int port_connected = 0;
1311         int is_lp0;
1312         void __iomem *base = phy->regs;
1313         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1314
1315         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1316         if (phy->pdata->op_mode == TEGRA_USB_OPMODE_HOST) {
1317                 if (readl(base + USB_ASYNCLISTADDR) &&
1318                         !phy->pdata->u_data.host.power_off_on_suspend)
1319                         return 0;
1320
1321                 val = readl(base + USB_PORTSC);
1322                 port_connected = val & USB_PORTSC_CCS;
1323                 is_lp0 = !(readl(base + USB_ASYNCLISTADDR));
1324
1325                 if ((phy->port_speed < USB_PHY_PORT_SPEED_UNKNOWN) &&
1326                         (port_connected ^ is_lp0)) {
1327                         utmi_phy_restore_start(phy);
1328                         usb_phy_bringup_host_controller(phy);
1329                         utmi_phy_restore_end(phy);
1330                 } else {
1331                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1332                         phy->pmc_remote_wakeup = false;
1333                         phy->pmc_hotplug_wakeup = false;
1334
1335                         /* bring up the controller from suspend*/
1336                         val = readl(base + USB_USBCMD);
1337                         val |= USB_CMD_RESET;
1338                         writel(val, base + USB_USBCMD);
1339
1340                         if (usb_phy_reg_status_wait(base + USB_USBCMD,
1341                                 USB_CMD_RESET, 0, 2500) < 0) {
1342                                 pr_err("%s: timeout waiting for reset\n", __func__);
1343                         }
1344
1345                         val = readl(base + USB_USBMODE);
1346                         val &= ~USB_USBMODE_MASK;
1347                         val |= USB_USBMODE_HOST;
1348                         writel(val, base + USB_USBMODE);
1349
1350                         val = readl(base + HOSTPC1_DEVLC);
1351                         val &= ~HOSTPC1_DEVLC_PTS(~0);
1352                         val |= HOSTPC1_DEVLC_STS;
1353                         writel(val, base + HOSTPC1_DEVLC);
1354
1355                         writel(USB_USBCMD_RS, base + USB_USBCMD);
1356
1357                         if (usb_phy_reg_status_wait(base + USB_USBCMD,
1358                                 USB_USBCMD_RS, USB_USBCMD_RS, 2500) < 0) {
1359                                 pr_err("%s: timeout waiting for run bit\n", __func__);
1360                         }
1361
1362                         /* Enable Port Power */
1363                         val = readl(base + USB_PORTSC);
1364                         val |= USB_PORTSC_PP;
1365                         writel(val, base + USB_PORTSC);
1366                         udelay(10);
1367
1368                         DBG("USB_USBSTS[0x%x] USB_PORTSC[0x%x]\n",
1369                         readl(base + USB_USBSTS), readl(base + USB_PORTSC));
1370                 }
1371                 val = readl(base + USB_TXFILLTUNING);
1372                 if ((val & USB_FIFO_TXFILL_MASK) !=
1373                         USB_FIFO_TXFILL_THRES(0x10)) {
1374                         val = USB_FIFO_TXFILL_THRES(0x10);
1375                                 writel(val, base + USB_TXFILLTUNING);
1376                 }
1377         }
1378
1379         return status;
1380 }
1381
1382 static unsigned long utmi_phy_set_dp_dm_pull_up_down(struct tegra_usb_phy *phy,
1383                 unsigned long pull_up_down_flags)
1384 {
1385         unsigned long val;
1386         unsigned long org;
1387         void __iomem *base = phy->regs;
1388
1389         org = readl(base + UTMIP_MISC_CFG0);
1390
1391         val = org & ~MASK_ALL_PULLUP_PULLDOWN;
1392         val |= pull_up_down_flags;
1393
1394         writel(val, base + UTMIP_MISC_CFG0);
1395
1396         usleep_range(500, 2000);
1397         return org;
1398 }
1399
1400 unsigned long utmi_phy_get_dp_dm_status(struct tegra_usb_phy *phy,
1401                 unsigned long pull_up_down_flags)
1402 {
1403         void __iomem *base = phy->regs;
1404         unsigned long org_flags;
1405         unsigned long ret;
1406
1407         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy, pull_up_down_flags);
1408         ret = USB_PORTSC_LINE_STATE(readl(base + USB_PORTSC));
1409         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1410         return ret;
1411 }
1412
1413 static void disable_charger_detection(void __iomem *base)
1414 {
1415         unsigned long val;
1416
1417         /* Disable charger detection logic */
1418         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1419         val &= ~(UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN);
1420         val &= ~(UTMIP_ON_SRC_EN | UTMIP_OP_SINK_EN);
1421         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1422
1423         /* Delay of 40 ms before we pull the D+ as per battery charger spec */
1424         msleep(TDPSRC_CON_MS);
1425 }
1426
1427 /*
1428  * Per Battery Charging Specification 1.2  section 3.2.3:
1429  * We check Data Contact Detect (DCD) before we check the USB cable type.
1430  */
1431 static bool utmi_phy_dcd_detect(struct tegra_usb_phy *phy)
1432 {
1433         void __iomem *base = phy->regs;
1434         unsigned long val;
1435         unsigned long org_flags;
1436         bool ret;
1437
1438         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1439         /* inject IDP_SRC */
1440         val |= UTMIP_OP_I_SRC_EN;
1441         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1442         /* enable pull down resistor RDM_DWN on D- */
1443         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1444                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1445                                 DISABLE_PULLDN_DP | FORCE_PULLDN_DM);
1446         usleep_range(20000, 30000);
1447
1448         ret = false;
1449         if (0 == USB_PORTSC_LINE_STATE(readl(base + USB_PORTSC))) {
1450                 /* minimum debounce time is 10mS per TDCD_DBNC */
1451                 usleep_range(10000, 12000);
1452                 if (0 == USB_PORTSC_LINE_STATE(readl(base + USB_PORTSC)))
1453                         ret = true;
1454         }
1455
1456         val &= ~UTMIP_OP_I_SRC_EN;
1457         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1458         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1459         return ret;
1460 }
1461
1462 static bool utmi_phy_charger_detect(struct tegra_usb_phy *phy)
1463 {
1464         unsigned long val;
1465         void __iomem *base = phy->regs;
1466         bool status;
1467         int dcd_timeout_ms;
1468
1469         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1470         if (phy->pdata->op_mode != TEGRA_USB_OPMODE_DEVICE) {
1471                 /* Charger detection is not there for ULPI
1472                  * return Charger not available */
1473                 return false;
1474         }
1475
1476         /* ensure we start from an initial state */
1477         writel(0, base + UTMIP_BAT_CHRG_CFG0);
1478         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1479
1480         /* log initial values */
1481         DBG("%s(%d) inst:[%d], UTMIP_BAT_CHRG_CFG0 = %08X\n",
1482                 __func__, __LINE__,
1483                 phy->inst, readl(base + UTMIP_BAT_CHRG_CFG0));
1484         DBG("%s(%d) inst:[%d], UTMIP_MISC_CFG0 = %08X\n",
1485                 __func__, __LINE__,
1486                 phy->inst, readl(base + UTMIP_MISC_CFG0));
1487
1488         /* DCD timeout max value is 900mS */
1489         dcd_timeout_ms = 0;
1490         while (dcd_timeout_ms < 900) {
1491                 /* standard DCD detect for SDP/DCP/CDP */
1492                 if (utmi_phy_dcd_detect(phy))
1493                         break;
1494                 /* for NV-charger, we wait D+/D- both set */
1495                 if ((USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET) ==
1496                         utmi_phy_get_dp_dm_status(phy,
1497                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1498                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM))
1499                         break;
1500                 usleep_range(20000, 22000);
1501                 dcd_timeout_ms += 22;
1502         }
1503
1504         /* Enable charger detection logic */
1505         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1506         val |= UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN;
1507         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1508
1509         /* Source should be on for 100 ms as per USB charging spec */
1510         msleep(TDP_SRC_ON_MS);
1511
1512         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1513         /* If charger is not connected disable the interrupt */
1514         val &= ~VDAT_DET_INT_EN;
1515         val |= VDAT_DET_CHG_DET;
1516         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1517
1518         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1519         if (val & VDAT_DET_STS)
1520                 status = true;
1521         else {
1522                 status = false;
1523                 disable_charger_detection(base);
1524         }
1525         DBG("%s(%d) inst:[%d], UTMIP_BAT_CHRG_CFG0 = %08X\n",
1526                 __func__, __LINE__,
1527                 phy->inst, readl(base + UTMIP_BAT_CHRG_CFG0));
1528         DBG("%s(%d) inst:[%d], UTMIP_MISC_CFG0 = %08X\n",
1529                 __func__, __LINE__,
1530                 phy->inst, readl(base + UTMIP_MISC_CFG0));
1531
1532         DBG("%s(%d) inst:[%d] DONE Status = %d\n",
1533                 __func__, __LINE__, phy->inst, status);
1534
1535         return status;
1536 }
1537
1538 static bool cdp_charger_detection(struct tegra_usb_phy *phy)
1539 {
1540         unsigned long val;
1541         int status;
1542         void __iomem *base = phy->regs;
1543
1544         /* SRC D- to D+ */
1545         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1546         val &= ~(UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN);
1547         val |= UTMIP_ON_SRC_EN | UTMIP_OP_SINK_EN;
1548         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1549
1550         DBG("%s(%d) UTMIP_BAT_CHRG_CFG0 = %08x\n",
1551                 __func__, __LINE__,
1552                 readl(base + UTMIP_BAT_CHRG_CFG0));
1553
1554         msleep(10);
1555
1556         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1557         if (val & VDAT_DET_STS) {
1558                 status = false;
1559                 DBG("%s: DCP detected\n", __func__);
1560         } else {
1561                 status = true;
1562                 disable_charger_detection(base);
1563                 DBG("%s: No voltage from D- to D+ found\n", __func__);
1564         }
1565
1566         return status;
1567 }
1568
1569 static bool maxim_charger_detection(struct tegra_usb_phy *phy)
1570 {
1571         void __iomem *base = phy->regs;
1572         unsigned long val;
1573         unsigned long org_flags;
1574         int status;
1575
1576         /*
1577          * Enable charger detection logic
1578          * 3.3V on D+, Sink D- (high speed usb)
1579          * Maxim charger will remove short !! -- DCP will not
1580          */
1581         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1582                 FORCE_PULLUP_DP   | DISABLE_PULLUP_DM |
1583                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1584         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1585         val &= ~(UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN);
1586         val &= ~(UTMIP_ON_SRC_EN | UTMIP_OP_SINK_EN);
1587         val |= UTMIP_ON_SINK_EN;
1588         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1589
1590         /* Source should be on at least 120 ms per Maxim spec */
1591         msleep(MAXIM_DEBOUNCE_TIME);
1592
1593         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1594         if (val & VDAT_DET_STS) {
1595                 status = false;
1596                 DBG("%s: Maxim charger not found\n", __func__);
1597                 utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1598         } else {
1599                 status = true;
1600                 DBG("%s: Maxim Charger detected\n", __func__);
1601         }
1602
1603         disable_charger_detection(base);
1604         return status;
1605 }
1606
1607 static bool utmi_phy_qc2_charger_detect(struct tegra_usb_phy *phy,
1608                 int max_voltage)
1609 {
1610         unsigned long val;
1611         void __iomem *base = phy->regs;
1612         int status;
1613         int qc2_timeout_ms;
1614         int vbus_stat = 0;
1615         unsigned long org_flags;
1616 #ifdef QC_MEASURE_VOLTAGES
1617         int timeout;
1618 #endif
1619
1620         DBG("%s(%d) inst:[%d] max_voltage = %d\n",
1621                 __func__, __LINE__, phy->inst, max_voltage);
1622
1623         status = false;
1624         /* no need to detect qc2 if operating at 5V */
1625         switch (max_voltage) {
1626         case TEGRA_USB_QC2_5V:
1627         case TEGRA_USB_QC2_9V:
1628         case TEGRA_USB_QC2_12V:
1629         case TEGRA_USB_QC2_20V:
1630                 break;
1631         default:
1632                 disable_charger_detection(base);
1633                 return status;
1634         }
1635
1636         /* Ensure we start from an initial state */
1637         writel(0, base + UTMIP_BAT_CHRG_CFG0);
1638         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1639
1640         /* Force wall charger detection logic to reset */
1641         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1642                 FORCE_PULLDN_DP | FORCE_PULLDN_DM);
1643         ssleep(1);
1644         msleep(500);
1645         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1646
1647         /* Enable charger detection logic */
1648         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1649         val |= UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN;
1650         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1651
1652         /* Source should be on for 100 ms as per USB charging spec */
1653         msleep(TDP_SRC_ON_MS);
1654
1655         /* if vbus drops we are connected to quick charge 2 */
1656         qc2_timeout_ms = 0;
1657         while (qc2_timeout_ms < 1500) {
1658                 usleep_range(1000, 1200);
1659                 qc2_timeout_ms += 1;
1660                 val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1661                 if (!(val & VDAT_DET_STS)) {
1662                         vbus_stat = 1;
1663                         break;
1664                 }
1665         }
1666
1667         if (vbus_stat && qc2_timeout_ms > 100) {
1668                 status = true;
1669
1670                 DBG("%s(%d) inst:[%d], QC2 DETECTED !!!",
1671                         __func__, __LINE__, phy->inst);
1672
1673                 /* Wall charger needs time before setting D+/D- */
1674                 usleep_range(25000, 26000);
1675
1676                 switch (max_voltage) {
1677                 case TEGRA_USB_QC2_9V:
1678                         /* Set the input voltage to 9V */
1679                         /* D+ 3.3v -- D- 0.6v */
1680                         DBG("%s(%d) inst:[%d], QC 9V D+ 3.3v D- 0.6v",
1681                                 __func__, __LINE__, phy->inst);
1682                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1683                                 FORCE_PULLUP_DP   | DISABLE_PULLUP_DM |
1684                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1685                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1686                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1687                         val |= UTMIP_ON_SRC_EN;
1688                         val &= ~UTMIP_OP_SRC_EN;
1689                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1690                         break;
1691
1692                 case TEGRA_USB_QC2_12V:
1693                         /* 0.6v v on D+ and D- */
1694                         DBG("%s(%d) inst:[%d], QC 12V D+ 0.6v D- 0.6v",
1695                                 __func__, __LINE__, phy->inst);
1696                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1697                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1698                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1699                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1700                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1701                         val |= (UTMIP_OP_SRC_EN | UTMIP_ON_SRC_EN);
1702                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1703                         break;
1704
1705                 case TEGRA_USB_QC2_20V:
1706                         /* 3.3 v on D+ and D- */
1707                         DBG("%s(%d) inst:[%d], QC 20V D+ 3.3v D- 3.3v",
1708                                 __func__, __LINE__, phy->inst);
1709                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1710                                 FORCE_PULLUP_DP   | FORCE_PULLUP_DM |
1711                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1712                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1713                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1714                         val &= ~(UTMIP_OP_SRC_EN | UTMIP_ON_SRC_EN);
1715                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1716                         break;
1717
1718                 case TEGRA_USB_QC2_5V:
1719                 default:
1720                         DBG("%s(%d) inst:[%d], QC 5V D+ 0.6v D- GND",
1721                                 __func__, __LINE__, phy->inst);
1722                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1723                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1724                                 DISABLE_PULLDN_DP | FORCE_PULLDN_DM);
1725                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1726                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1727                         val |= UTMIP_OP_SRC_EN;
1728                         val &= ~UTMIP_ON_SRC_EN;
1729                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1730                         break;
1731                 }
1732
1733 #ifdef QC_MEASURE_VOLTAGES
1734                 timeout = 60;
1735                 DBG("%s(%d) MEASURE VOLTAGES -- you have %d seconds",
1736                         __func__, __LINE__, timeout);
1737                 while (timeout-- > 0) {
1738                         DBG("%s(%d) COUNT = %d",
1739                                 __func__, __LINE__, timeout);
1740                         ssleep(1);
1741                 }
1742 #endif
1743         }
1744
1745         /*
1746          * If QC2 charger detected do not disable the charger
1747          * detection voltage on D- and D+. Doing this will
1748          * cause it to move to 5V
1749          */
1750         if (!status)
1751                 disable_charger_detection(base);
1752
1753         DBG("%s(%d) inst:[%d] DONE Status = %d\n",
1754                 __func__, __LINE__, phy->inst, status);
1755         return status;
1756 }
1757
1758 static bool utmi_phy_is_non_std_charger(struct tegra_usb_phy *phy)
1759 {
1760         /*
1761          * non std charger has D+/D- line float, we can apply pull up/down on
1762          * each line and verify if line status change.
1763          */
1764         /* pull up DP only */
1765         if (USB_PORTSC_LINE_DP_SET != utmi_phy_get_dp_dm_status(phy,
1766                                 FORCE_PULLUP_DP | DISABLE_PULLUP_DM |
1767                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM))
1768                 goto NOT_NON_STD_CHARGER;
1769
1770         /* pull down DP only */
1771         if (0x0 != utmi_phy_get_dp_dm_status(phy,
1772                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1773                                 FORCE_PULLDN_DP | DISABLE_PULLDN_DM))
1774                 goto NOT_NON_STD_CHARGER;
1775
1776         /* pull up DM only */
1777         if (USB_PORTSC_LINE_DM_SET != utmi_phy_get_dp_dm_status(phy,
1778                                 DISABLE_PULLUP_DP | FORCE_PULLUP_DM |
1779                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM))
1780                 goto NOT_NON_STD_CHARGER;
1781
1782         /* pull down DM only */
1783         if (0x0 != utmi_phy_get_dp_dm_status(phy,
1784                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1785                                 DISABLE_PULLDN_DP | FORCE_PULLDN_DM))
1786                 goto NOT_NON_STD_CHARGER;
1787
1788         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1789         return true;
1790
1791 NOT_NON_STD_CHARGER:
1792         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1793         return false;
1794
1795 }
1796
1797 static void utmi_phy_pmc_disable(struct tegra_usb_phy *phy)
1798 {
1799         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1800         if (phy->pdata->u_data.host.turn_off_vbus_on_lp0 &&
1801                                         phy->pdata->port_otg) {
1802                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1803                 pmc->pmc_ops->powerdown_pmc_wake_detect(pmc);
1804         }
1805 }
1806 static bool utmi_phy_nv_charger_detect(struct tegra_usb_phy *phy)
1807 {
1808         int status1;
1809         int status2;
1810         int status3;
1811         bool ret;
1812
1813         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1814
1815         if (utmi_phy_is_non_std_charger(phy))
1816                 return false;
1817
1818         ret = false;
1819         /* Turn off all terminations except DP pulldown */
1820         status1 = utmi_phy_get_dp_dm_status(phy,
1821                         DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1822                         FORCE_PULLDN_DP | DISABLE_PULLDN_DM);
1823
1824         /* Turn off all terminations except for DP pullup */
1825         status2 = utmi_phy_get_dp_dm_status(phy,
1826                         FORCE_PULLUP_DP | DISABLE_PULLUP_DM |
1827                         DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1828
1829         /* Check for NV charger DISABLE all terminations */
1830         status3 = utmi_phy_get_dp_dm_status(phy,
1831                         DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1832                         DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1833
1834         if ((status1 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)) &&
1835             (status2 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)) &&
1836             (status3 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)))
1837                 ret = true;
1838
1839         /* Restore standard termination by hardware. */
1840         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1841         return ret;
1842 }
1843
1844 static bool utmi_phy_apple_charger_1000ma_detect(struct tegra_usb_phy *phy)
1845 {
1846         unsigned long val;
1847         bool ret;
1848         void __iomem *base = phy->regs;
1849         unsigned long org_flags;
1850
1851         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1852         ret = false;
1853
1854         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1855                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1856                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1857         usleep_range(20000, 30000);
1858         utmi_phy_set_dp_dm_pull_up_down(phy,
1859                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1860                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1861
1862         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1863         val |= DIV_DET_EN;
1864         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1865
1866         usleep_range(10000, 20000);
1867
1868         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1869         val &= ~DIV_DET_EN;
1870         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1871
1872         if ((val & VOP_DIV2P0_DET) && (val & VON_DIV2P7_DET))
1873                 ret = true;
1874
1875         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1876
1877         return ret;
1878 }
1879
1880 static bool utmi_phy_apple_charger_2000ma_detect(struct tegra_usb_phy *phy)
1881 {
1882         unsigned long val;
1883         bool ret;
1884         void __iomem *base = phy->regs;
1885         unsigned long org_flags;
1886
1887         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1888         ret = false;
1889
1890         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1891                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1892                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1893         usleep_range(20000, 30000);
1894         utmi_phy_set_dp_dm_pull_up_down(phy,
1895                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1896                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1897
1898         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1899         val |= DIV_DET_EN;
1900         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1901
1902         usleep_range(10000, 20000);
1903
1904         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1905         val &= ~DIV_DET_EN;
1906         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1907
1908         if ((val & VOP_DIV2P7_DET) && (val & VON_DIV2P0_DET))
1909                 ret = true;
1910
1911         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1912
1913         return ret;
1914 }
1915
1916 static bool utmi_phy_apple_charger_500ma_detect(struct tegra_usb_phy *phy)
1917 {
1918         unsigned long val;
1919         bool ret;
1920         void __iomem *base = phy->regs;
1921         unsigned long org_flags;
1922
1923         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1924         ret = false;
1925
1926         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1927                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1928                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1929         usleep_range(20000, 30000);
1930         utmi_phy_set_dp_dm_pull_up_down(phy,
1931                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1932                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1933
1934         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1935         val |= DIV_DET_EN;
1936         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1937
1938         usleep_range(10000, 20000);
1939
1940         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1941         val &= ~DIV_DET_EN;
1942         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1943
1944         if ((val & VOP_DIV2P0_DET) && (val & VON_DIV2P0_DET))
1945                 ret = true;
1946
1947         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1948
1949         return ret;
1950 }
1951
1952 static bool uhsic_phy_remotewake_detected(struct tegra_usb_phy *phy)
1953 {
1954         void __iomem *base = phy->regs;
1955         u32 val;
1956         unsigned int inst = phy->inst;
1957
1958         val = readl(base + UHSIC_PMC_WAKEUP0);
1959         if (!(val & EVENT_INT_ENB))
1960                 return false;
1961         val = tegra_usb_pmc_reg_read(UHSIC_STATUS(inst));
1962         if (!(UHSIC_WAKE_ALARM(inst) & val))
1963                 return false;
1964         tegra_usb_pmc_reg_update(PMC_UHSIC_SLEEP_CFG(inst),
1965                 UHSIC_WAKE_VAL(inst, WAKE_VAL_ANY),
1966                 UHSIC_WAKE_VAL(inst, WAKE_VAL_NONE));
1967
1968         tegra_usb_pmc_reg_update(PMC_UHSIC_TRIGGERS(inst),
1969                 0, UHSIC_CLR_WAKE_ALARM(inst));
1970
1971         val = readl(base + UHSIC_PMC_WAKEUP0);
1972         val &= ~EVENT_INT_ENB;
1973         writel(val, base + UHSIC_PMC_WAKEUP0);
1974         phy->pmc_remote_wakeup = true;
1975         DBG("%s:PMC interrupt detected for HSIC\n", __func__);
1976         return true;
1977 }
1978
1979 static int uhsic_phy_pre_resume(struct tegra_usb_phy *phy, bool remote_wakeup)
1980 {
1981         unsigned long val;
1982         void __iomem *base = phy->regs;
1983
1984         if (remote_wakeup) {
1985                 /* Set RUN bit */
1986                 val = readl(base + USB_USBCMD);
1987                 val |= USB_USBCMD_RS;
1988                 writel(val, base + USB_USBCMD);
1989         }
1990
1991         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1992
1993         return 0;
1994 }
1995
1996 static void uhsic_phy_restore_start(struct tegra_usb_phy *phy)
1997 {
1998         unsigned long val;
1999         void __iomem *base = phy->regs;
2000         unsigned int inst = phy->inst;
2001         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2002
2003         val = tegra_usb_pmc_reg_read(UHSIC_STATUS(inst));
2004
2005         /* check whether we wake up from the remote resume */
2006         if (UHSIC_WALK_PTR_VAL(inst) & val) {
2007                 phy->pmc_remote_wakeup = true;
2008                 DBG("%s: uhsic remote wakeup detected\n", __func__);
2009         } else {
2010                 if (!((UHSIC_STROBE_VAL(inst) | UHSIC_DATA_VAL(inst)) & val)) {
2011
2012                         /*
2013                          * If pmc wakeup is detected after putting controller
2014                          * in suspend in usb_phy_bringup_host_cotroller,
2015                          * restart bringing up host controller as
2016                          * in case of only pmc wakeup.
2017                          */
2018                         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2019                                 usb_phy_bringup_host_controller(phy);
2020                                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2021                                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP),
2022                                                 0, FPR_WAIT_TIME_US) < 0)
2023                                         pr_err("%s: timeout waiting" \
2024                                         "for SUSPEND to clear\n",
2025                                                 __func__);
2026                                 phy->ctrlr_suspended = false;
2027                         }
2028
2029                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
2030                         phy->pmc_remote_wakeup = false;
2031                 } else {
2032                         DBG("%s(%d): setting pretend connect\n", __func__, __LINE__);
2033                         val = readl(base + UHSIC_CMD_CFG0);
2034                         val |= UHSIC_PRETEND_CONNECT_DETECT;
2035                         writel(val, base + UHSIC_CMD_CFG0);
2036                 }
2037         }
2038 }
2039
2040 static void uhsic_phy_restore_end(struct tegra_usb_phy *phy)
2041 {
2042
2043         unsigned long val, flags = 0;
2044         void __iomem *base = phy->regs;
2045         int wait_time_us = FPR_WAIT_TIME_US; /* FPR should be set by this time */
2046         bool irq_disabled = false;
2047         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2048         bool remote_wakeup_detected;
2049
2050         DBG("%s(%d)\n", __func__, __LINE__);
2051
2052         remote_wakeup_detected = phy->pmc_remote_wakeup;
2053         /*
2054          * check whether we wake up from the remote wake detected before putting
2055          * controller in suspend in usb_phy_bringup_host_controller.
2056          */
2057         if (!phy->ctrlr_suspended) {
2058                 /* wait until FPR bit is set automatically on remote resume */
2059                 do {
2060                         val = readl(base + USB_PORTSC);
2061                         udelay(1);
2062                         if (wait_time_us == 0) {
2063                         /*
2064                          * If pmc wakeup is detected after putting controller
2065                          * in suspend in usb_phy_bringup_host_cotroller,
2066                          * restart bringing up host controller as
2067                          * in case of only pmc wakeup.
2068                          */
2069                         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2070                                 usb_phy_bringup_host_controller(phy);
2071                                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2072                                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP),
2073                                                 0, FPR_WAIT_TIME_US) < 0)
2074                                         pr_err("%s: timeout waiting" \
2075                                                 " for SUSPEND to clear\n",
2076                                                 __func__);
2077                                 phy->ctrlr_suspended = false;
2078                         }
2079
2080                                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
2081                                 phy->pmc_remote_wakeup = false;
2082                                 return;
2083                         }
2084                         wait_time_us--;
2085                 } while (val & (USB_PORTSC_RESUME | USB_PORTSC_SUSP));
2086                 /* In case of remote wakeup, disable local irq to prevent
2087                  * context switch b/t disable PMC and set RUN bit ops */
2088                 local_irq_save(flags);
2089                 irq_disabled = true;
2090         }
2091
2092         /* Disable PMC remote wake-up detection */
2093         val = readl(base + UHSIC_PMC_WAKEUP0);
2094         val &= ~EVENT_INT_ENB;
2095         writel(val, base + UHSIC_PMC_WAKEUP0);
2096
2097         /*
2098          * If pmc wakeup is detected after putting controller in suspend
2099          * in usb_phy_bringup_host_cotroller, restart bringing up host
2100          * controller as in case of only pmc wakeup.
2101          */
2102         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2103                 usb_phy_bringup_host_controller(phy);
2104                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2105                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP), 0,
2106                                 FPR_WAIT_TIME_US) < 0)
2107                         pr_err("%s: timeout waiting for SUSPEND to clear\n",
2108                                 __func__);
2109                 phy->ctrlr_suspended = false;
2110         }
2111
2112         val = tegra_usb_pmc_reg_read(PMC_UHSIC_SLEEP_CFG(phy->inst));
2113         if (val & UHSIC_MASTER_ENABLE(phy->inst)) {
2114                 val = readl(base + UHSIC_PADS_CFG1);
2115                 val &= ~(UHSIC_PD_TX);
2116                 writel(val, base + UHSIC_PADS_CFG1);
2117         }
2118
2119         if (irq_disabled) {
2120                 local_irq_restore(flags);
2121                 usleep_range(25000, 26000);
2122                 local_irq_save(flags);
2123         } else
2124                 usleep_range(10000, 11000);
2125
2126         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 1);
2127         phy->pmc_remote_wakeup = false;
2128
2129         /* Restore local irq if disabled before */
2130         if (irq_disabled)
2131                 local_irq_restore(flags);
2132         if (usb_phy_reg_status_wait(base + USB_USBCMD, USB_USBCMD_RS,
2133                                                  USB_USBCMD_RS, 2000)) {
2134                 pr_err("%s: timeout waiting for USB_USBCMD_RS\n", __func__);
2135                 return;
2136         }
2137         if (remote_wakeup_detected && phy->pdata->ops &&
2138                                         phy->pdata->ops->post_remote_wakeup)
2139                 phy->pdata->ops->post_remote_wakeup();
2140 }
2141
2142 static int uhsic_rail_enable(struct tegra_usb_phy *phy)
2143 {
2144         int ret;
2145
2146         if (phy->hsic_reg == NULL) {
2147                 phy->hsic_reg = regulator_get(&phy->pdev->dev, "vddio_hsic");
2148                 if (IS_ERR_OR_NULL(phy->hsic_reg)) {
2149                         pr_err("UHSIC: Could not get regulator vddio_hsic\n");
2150                         ret = PTR_ERR(phy->hsic_reg);
2151                         phy->hsic_reg = NULL;
2152                         return ret;
2153                 }
2154         }
2155
2156         ret = regulator_enable(phy->hsic_reg);
2157         if (ret < 0) {
2158                 pr_err("%s vddio_hsic could not be enabled\n", __func__);
2159                 return ret;
2160         }
2161
2162         return 0;
2163 }
2164
2165 static int uhsic_rail_disable(struct tegra_usb_phy *phy)
2166 {
2167         int ret;
2168
2169         if (phy->hsic_reg == NULL) {
2170                 pr_warn("%s: unbalanced disable\n", __func__);
2171                 return -EIO;
2172         }
2173
2174         ret = regulator_disable(phy->hsic_reg);
2175         if (ret < 0) {
2176                 pr_err("HSIC regulator vddio_hsic cannot be disabled\n");
2177                 return ret;
2178         }
2179         regulator_put(phy->hsic_reg);
2180         phy->hsic_reg = NULL;
2181         return 0;
2182 }
2183
2184 static int uhsic_phy_open(struct tegra_usb_phy *phy)
2185 {
2186         unsigned long parent_rate;
2187         int i;
2188         int ret;
2189         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2190
2191         phy->hsic_reg = NULL;
2192         ret = uhsic_rail_enable(phy);
2193         if (ret < 0) {
2194                 pr_err("%s vddio_hsic could not be enabled\n", __func__);
2195                 return ret;
2196         }
2197
2198         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2199         parent_rate = clk_get_rate(clk_get_parent(phy->pllu_clk));
2200         for (i = 0; i < ARRAY_SIZE(uhsic_freq_table); i++) {
2201                 if (uhsic_freq_table[i].freq == parent_rate) {
2202                         phy->freq = &uhsic_freq_table[i];
2203                         break;
2204                 }
2205         }
2206         if (!phy->freq) {
2207                 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
2208                 return -EINVAL;
2209         }
2210         /* reset controller for reenumerating hsic device */
2211         tegra_periph_reset_assert(phy->ctrlr_clk);
2212         udelay(2);
2213         tegra_periph_reset_deassert(phy->ctrlr_clk);
2214         udelay(2);
2215
2216         pmc_init(phy);
2217         pmc->pmc_ops->powerup_pmc_wake_detect(pmc);
2218
2219         return 0;
2220 }
2221
2222 static void uhsic_phy_close(struct tegra_usb_phy *phy)
2223 {
2224         int ret;
2225         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2226
2227         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2228         pmc->pmc_ops->powerdown_pmc_wake_detect(pmc);
2229
2230         ret = uhsic_rail_disable(phy);
2231         if (ret < 0)
2232                 pr_err("%s vddio_hsic could not be disabled\n", __func__);
2233 }
2234
2235 static int uhsic_phy_irq(struct tegra_usb_phy *phy)
2236 {
2237         unsigned long val;
2238         void __iomem *base = phy->regs;
2239         int irq_status = IRQ_HANDLED;
2240
2241         /* check if there is any remote wake event */
2242         if (!phy->pdata->unaligned_dma_buf_supported)
2243                 usb_phy_fence_read(phy);
2244         if (uhsic_phy_remotewake_detected(phy))
2245                 DBG("%s: uhsic remote wake detected\n", __func__);
2246
2247         val = readl(base + USB_SUSP_CTRL);
2248         if ((val  & USB_PHY_CLK_VALID_INT_STS) &&
2249                 (val  & USB_PHY_CLK_VALID_INT_ENB)) {
2250                 val &= ~USB_PHY_CLK_VALID_INT_ENB |
2251                                 USB_PHY_CLK_VALID_INT_STS;
2252                 writel(val , (base + USB_SUSP_CTRL));
2253
2254                 val = readl(base + USB_USBSTS);
2255                 if (!(val  & USB_USBSTS_PCI)) {
2256                         irq_status = IRQ_NONE;
2257                         goto exit;
2258                 }
2259
2260                 val = readl(base + USB_PORTSC);
2261                 if (val & USB_PORTSC_CCS)
2262                         val &= ~USB_PORTSC_WKCN;
2263                 val &= ~USB_PORTSC_RWC_BITS;
2264                 writel(val , (base + USB_PORTSC));
2265                 irq_status = IRQ_HANDLED;
2266         }
2267 exit:
2268         return irq_status;
2269 }
2270
2271 static int uhsic_phy_power_on(struct tegra_usb_phy *phy)
2272 {
2273         unsigned long val;
2274         void __iomem *base = phy->regs;
2275 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
2276         void __iomem *padctl_base = IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE);
2277 #endif
2278
2279         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2280
2281         if (phy->phy_clk_on) {
2282                 DBG("%s(%d) inst:[%d] phy clk is already On\n",
2283                                         __func__, __LINE__, phy->inst);
2284                 return 0;
2285         }
2286
2287 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
2288         val = readl(padctl_base + PADCTL_SNPS_OC_MAP);
2289         val |= CONTROLLER_OC(phy->inst, 0x7);
2290         writel(val, padctl_base + PADCTL_SNPS_OC_MAP);
2291 #endif
2292
2293         val = readl(base + UHSIC_PADS_CFG1);
2294         val &= ~(UHSIC_PD_BG | UHSIC_PD_RX |
2295                         UHSIC_PD_ZI | UHSIC_RPD_DATA | UHSIC_RPD_STROBE);
2296         writel(val, base + UHSIC_PADS_CFG1);
2297
2298         val |= (UHSIC_RX_SEL | UHSIC_PD_TX);
2299         val |= UHSIC_AUTO_RTERM_EN;
2300         writel(val, base + UHSIC_PADS_CFG1);
2301
2302         val = readl(base + USB_SUSP_CTRL);
2303         val |= UHSIC_RESET;
2304         writel(val, base + USB_SUSP_CTRL);
2305         udelay(1);
2306
2307         val = readl(base + USB_SUSP_CTRL);
2308         val |= UHSIC_PHY_ENABLE;
2309         writel(val, base + USB_SUSP_CTRL);
2310
2311         val = readl(base + UHSIC_HSRX_CFG0);
2312         val |= UHSIC_IDLE_WAIT(HSIC_IDLE_WAIT_DELAY);
2313         val |= UHSIC_ELASTIC_UNDERRUN_LIMIT(HSIC_ELASTIC_UNDERRUN_LIMIT);
2314         val |= UHSIC_ELASTIC_OVERRUN_LIMIT(HSIC_ELASTIC_OVERRUN_LIMIT);
2315         writel(val, base + UHSIC_HSRX_CFG0);
2316
2317         val = readl(base + UHSIC_HSRX_CFG1);
2318         val |= UHSIC_HS_SYNC_START_DLY(HSIC_SYNC_START_DELAY);
2319         writel(val, base + UHSIC_HSRX_CFG1);
2320
2321         /* WAR HSIC TX */
2322         val = readl(base + UHSIC_TX_CFG0);
2323         val &= ~UHSIC_HS_READY_WAIT_FOR_VALID;
2324         writel(val, base + UHSIC_TX_CFG0);
2325
2326         val = readl(base + UHSIC_MISC_CFG0);
2327         val |= UHSIC_SUSPEND_EXIT_ON_EDGE;
2328         writel(val, base + UHSIC_MISC_CFG0);
2329
2330         val = readl(base + UHSIC_MISC_CFG1);
2331         val |= UHSIC_PLLU_STABLE_COUNT(phy->freq->stable_count);
2332         writel(val, base + UHSIC_MISC_CFG1);
2333
2334         val = readl(base + UHSIC_PLL_CFG1);
2335         val |= UHSIC_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
2336         val |= UHSIC_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count);
2337         writel(val, base + UHSIC_PLL_CFG1);
2338
2339         val = readl(base + USB_SUSP_CTRL);
2340         val &= ~(UHSIC_RESET);
2341         writel(val, base + USB_SUSP_CTRL);
2342         udelay(1);
2343
2344         val = tegra_usb_pmc_reg_read(PMC_UHSIC_SLEEP_CFG(phy->inst));
2345         if (!(val & UHSIC_MASTER_ENABLE(phy->inst))) {
2346                 val = readl(base + UHSIC_PADS_CFG1);
2347                 val &= ~(UHSIC_PD_TX);
2348                 writel(val, base + UHSIC_PADS_CFG1);
2349         }
2350
2351         /* HSIC pad tracking circuit power down sequence */
2352         val = readl(base + UHSIC_PADS_CFG1);
2353         val &= ~(UHSIC_PD_TRK);
2354         writel(val, base + UHSIC_PADS_CFG1);
2355         /* Wait for 25usec */
2356         udelay(25);
2357         val |= UHSIC_PD_TRK;
2358         writel(val, base + UHSIC_PADS_CFG1);
2359
2360         /* Enable bus keepers always */
2361         val = readl(base + UHSIC_SPARE_CFG0);
2362         val |= FORCE_BK_ON;
2363         writel(val, base + UHSIC_SPARE_CFG0);
2364
2365         /*SUSP_CTRL has to be toggled to enable host PHY clock */
2366         val = readl(base + USB_SUSP_CTRL);
2367         val |= USB_SUSP_CLR;
2368         writel(val, base + USB_SUSP_CTRL);
2369
2370         val = readl(base + USB_SUSP_CTRL);
2371         val &= ~USB_SUSP_CLR;
2372         writel(val, base + USB_SUSP_CTRL);
2373
2374         val = readl(base + USB_USBMODE);
2375         val |= USB_USBMODE_HOST;
2376         writel(val, base + USB_USBMODE);
2377
2378         /* Change the USB controller PHY type to HSIC */
2379         val = readl(base + HOSTPC1_DEVLC);
2380         val &= ~HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_MASK);
2381         val |= HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_HSIC);
2382         val &= ~HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_MASK);
2383         val |= HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_HIGH_SPEED);
2384         val &= ~HOSTPC1_DEVLC_STS;
2385         writel(val, base + HOSTPC1_DEVLC);
2386
2387         val = readl(base + USB_PORTSC);
2388         val &= ~(USB_PORTSC_WKOC | USB_PORTSC_WKDS);
2389         writel(val, base + USB_PORTSC);
2390
2391         val = readl(base + UHSIC_TX_CFG0);
2392         val |= UHSIC_HS_POSTAMBLE_OUTPUT_ENABLE;
2393         writel(val, base + UHSIC_TX_CFG0);
2394
2395         val = readl(base + UHSIC_PADS_CFG0);
2396         /* Clear RTUNEP, SLEWP & SLEWN bit fields */
2397         val &= ~(UHSIC_TX_RTUNEP | UHSIC_TX_SLEWP | UHSIC_TX_SLEWN);
2398         /* set Rtune impedance to 50 ohm */
2399 #ifdef CONFIG_ARCH_TEGRA_12x_SOC
2400         val |= UHSIC_TX_RTUNE_P(0xA);
2401 #else
2402         val |= UHSIC_TX_RTUNE_P(0xC);
2403 #endif
2404         writel(val, base + UHSIC_PADS_CFG0);
2405
2406         if (usb_phy_reg_status_wait(base + USB_SUSP_CTRL,
2407                                 USB_PHY_CLK_VALID, USB_PHY_CLK_VALID, 2500)) {
2408                 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
2409                 return -ETIMEDOUT;
2410         }
2411
2412         phy->phy_clk_on = true;
2413         phy->hw_accessible = true;
2414
2415         if (!readl(base + USB_ASYNCLISTADDR))
2416                 _usb_phy_init(phy);
2417
2418         if (phy->pmc_sleepwalk) {
2419                 DBG("%s(%d) inst:[%d] restore phy\n", __func__, __LINE__,
2420                                         phy->inst);
2421                 uhsic_phy_restore_start(phy);
2422                 usb_phy_bringup_host_controller(phy);
2423                 uhsic_phy_restore_end(phy);
2424                 phy->pmc_sleepwalk = false;
2425         }
2426
2427         val = readl(base + USB_TXFILLTUNING);
2428         if ((val & USB_FIFO_TXFILL_MASK) != USB_FIFO_TXFILL_THRES(0x10)) {
2429                 val = USB_FIFO_TXFILL_THRES(0x10);
2430                 writel(val, base + USB_TXFILLTUNING);
2431         }
2432
2433         return 0;
2434 }
2435
2436 static int uhsic_phy_power_off(struct tegra_usb_phy *phy)
2437 {
2438         unsigned long val;
2439         void __iomem *base = phy->regs;
2440         bool port_connected;
2441         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2442
2443         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2444         if (!phy->phy_clk_on) {
2445                 DBG("%s(%d) inst:[%d] phy clk is already off\n",
2446                                         __func__, __LINE__, phy->inst);
2447                 return 0;
2448         }
2449
2450         /* Disable interrupts */
2451         writel(0, base + USB_USBINTR);
2452
2453         /* check for port connect status */
2454         val = readl(base + USB_PORTSC);
2455         port_connected = val & USB_PORTSC_CCS;
2456
2457         if (phy->pmc_sleepwalk == false && port_connected) {
2458                 pmc->pmc_ops->setup_pmc_wake_detect(pmc);
2459
2460                 phy->pmc_remote_wakeup = false;
2461                 phy->pmc_sleepwalk = true;
2462
2463                 val = readl(base + UHSIC_PMC_WAKEUP0);
2464                 val |= EVENT_INT_ENB;
2465                 writel(val, base + UHSIC_PMC_WAKEUP0);
2466
2467                 val = readl(base + USB_SUSP_CTRL);
2468                 val &= ~USB_WAKE_ON_CNNT_EN_DEV;
2469                 writel(val, base + USB_SUSP_CTRL);
2470         } else {
2471                 phy->pmc_sleepwalk = true;
2472         }
2473
2474         val = readl(base + HOSTPC1_DEVLC);
2475         val |= HOSTPC1_DEVLC_PHCD;
2476         writel(val, base + HOSTPC1_DEVLC);
2477
2478         /* Remove power downs for HSIC from PADS CFG1 register */
2479         val = readl(base + UHSIC_PADS_CFG1);
2480         val |= (UHSIC_PD_BG | UHSIC_PD_TRK |
2481                         UHSIC_PD_ZI | UHSIC_PD_TX);
2482         writel(val, base + UHSIC_PADS_CFG1);
2483
2484         if (usb_phy_reg_status_wait(base + USB_SUSP_CTRL,
2485                 USB_PHY_CLK_VALID, 0, 2500))
2486                 pr_warn("%s: timeout waiting for phy to disable\n", __func__);
2487
2488         DBG("%s(%d) inst:[%d] End\n", __func__, __LINE__, phy->inst);
2489
2490         phy->phy_clk_on = false;
2491         phy->hw_accessible = false;
2492
2493         return 0;
2494 }
2495
2496 static int uhsic_phy_bus_port_power(struct tegra_usb_phy *phy)
2497 {
2498         unsigned long val;
2499         void __iomem *base = phy->regs;
2500
2501         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2502
2503         val = readl(base + USB_USBMODE);
2504         val |= USB_USBMODE_HOST;
2505         writel(val, base + USB_USBMODE);
2506
2507         /* Change the USB controller PHY type to HSIC */
2508         val = readl(base + HOSTPC1_DEVLC);
2509         val &= ~(HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_MASK));
2510         val |= HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_HSIC);
2511         val &= ~(HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_MASK));
2512         val |= HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_HIGH_SPEED);
2513         writel(val, base + HOSTPC1_DEVLC);
2514
2515         val = readl(base + UHSIC_MISC_CFG0);
2516         val |= UHSIC_DETECT_SHORT_CONNECT;
2517         writel(val, base + UHSIC_MISC_CFG0);
2518         udelay(1);
2519         if (phy->pdata->ops && phy->pdata->ops->port_power)
2520                 phy->pdata->ops->port_power();
2521
2522         return 0;
2523 }
2524
2525 static struct tegra_usb_phy_ops utmi_phy_ops = {
2526         .init           = _usb_phy_init,
2527         .reset          = usb_phy_reset,
2528         .open           = utmi_phy_open,
2529         .close          = utmi_phy_close,
2530         .irq            = utmi_phy_irq,
2531         .power_on       = utmi_phy_power_on,
2532         .power_off      = utmi_phy_power_off,
2533         .pre_resume = utmi_phy_pre_resume,
2534         .resume = utmi_phy_resume,
2535         .post_resume    = utmi_phy_post_resume,
2536         .charger_detect = utmi_phy_charger_detect,
2537         .qc2_charger_detect = utmi_phy_qc2_charger_detect,
2538         .cdp_charger_detect = cdp_charger_detection,
2539         .nv_charger_detect = utmi_phy_nv_charger_detect,
2540         .maxim_charger_14675 = maxim_charger_detection,
2541         .apple_charger_1000ma_detect = utmi_phy_apple_charger_1000ma_detect,
2542         .apple_charger_2000ma_detect = utmi_phy_apple_charger_2000ma_detect,
2543         .apple_charger_500ma_detect = utmi_phy_apple_charger_500ma_detect,
2544         .pmc_disable = utmi_phy_pmc_disable,
2545 };
2546
2547 static struct tegra_usb_phy_ops uhsic_phy_ops = {
2548         .init           = _usb_phy_init,
2549         .reset          = usb_phy_reset,
2550         .open           = uhsic_phy_open,
2551         .close          = uhsic_phy_close,
2552         .irq            = uhsic_phy_irq,
2553         .power_on       = uhsic_phy_power_on,
2554         .power_off      = uhsic_phy_power_off,
2555         .pre_resume     = uhsic_phy_pre_resume,
2556         .port_power = uhsic_phy_bus_port_power,
2557 };
2558
2559 static struct tegra_usb_phy_ops *phy_ops[] = {
2560         [TEGRA_USB_PHY_INTF_UTMI] = &utmi_phy_ops,
2561         [TEGRA_USB_PHY_INTF_HSIC] = &uhsic_phy_ops,
2562 };
2563
2564 int tegra11x_usb_phy_init_ops(struct tegra_usb_phy *phy)
2565 {
2566         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2567
2568         phy->ops = phy_ops[phy->pdata->phy_intf];
2569
2570         /* FIXME: uncommenting below line to make USB host mode fail*/
2571         /* pmc->pmc_ops->power_down_pmc(pmc); */
2572
2573         return 0;
2574 }