]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/usb/phy/tegra11x_usb_phy.c
usb: phy: tegra: increase the timeout for phy clk
[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, 2600))
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         /*
1641          * The QC2 charger can be inserted when the system is
1642          * powered down.  In this case it will act as a DCP unless
1643          * it is forced to negotiate voltage.  This is because
1644          * the QC2 charger will not see D+/D- voltage signaling
1645          * with in its negotiation timer window.
1646          * We try to detect this condition.  We know that if
1647          * we enter suspend then system must be active before
1648          * the QC2 charger was seen.
1649          * In this case no reset of the charger is needed, otherwise
1650          * we force the D+ and D- Pull Up down which will reset a
1651          * QC2 charger but have no effect on a normal DCP.
1652          */
1653         if (!phy->qc2_no_reset) {
1654                 phy->qc2_no_reset = true;
1655                 org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1656                         FORCE_PULLDN_DP | FORCE_PULLDN_DM);
1657                 ssleep(1);
1658                 msleep(500);
1659                 utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1660         }
1661
1662         /* Enable charger detection logic */
1663         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1664         val |= UTMIP_OP_SRC_EN | UTMIP_ON_SINK_EN;
1665         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1666
1667         /* Source should be on for 100 ms as per USB charging spec */
1668         msleep(TDP_SRC_ON_MS);
1669
1670         /* if vbus drops we are connected to quick charge 2 */
1671         qc2_timeout_ms = 0;
1672         while (qc2_timeout_ms < 1500) {
1673                 usleep_range(1000, 1200);
1674                 qc2_timeout_ms += 1;
1675                 val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1676                 if (!(val & VDAT_DET_STS)) {
1677                         vbus_stat = 1;
1678                         break;
1679                 }
1680         }
1681
1682         if (vbus_stat && qc2_timeout_ms > 100) {
1683                 status = true;
1684
1685                 DBG("%s(%d) inst:[%d], QC2 DETECTED !!!",
1686                         __func__, __LINE__, phy->inst);
1687
1688                 /* Wall charger needs time before setting D+/D- */
1689                 usleep_range(25000, 26000);
1690
1691                 switch (max_voltage) {
1692                 case TEGRA_USB_QC2_9V:
1693                         /* Set the input voltage to 9V */
1694                         /* D+ 3.3v -- D- 0.6v */
1695                         DBG("%s(%d) inst:[%d], QC 9V D+ 3.3v D- 0.6v",
1696                                 __func__, __LINE__, phy->inst);
1697                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1698                                 FORCE_PULLUP_DP   | DISABLE_PULLUP_DM |
1699                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1700                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1701                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1702                         val |= UTMIP_ON_SRC_EN;
1703                         val &= ~UTMIP_OP_SRC_EN;
1704                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1705                         break;
1706
1707                 case TEGRA_USB_QC2_12V:
1708                         /* 0.6v v on D+ and D- */
1709                         DBG("%s(%d) inst:[%d], QC 12V D+ 0.6v D- 0.6v",
1710                                 __func__, __LINE__, phy->inst);
1711                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1712                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1713                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1714                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1715                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1716                         val |= (UTMIP_OP_SRC_EN | UTMIP_ON_SRC_EN);
1717                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1718                         break;
1719
1720                 case TEGRA_USB_QC2_20V:
1721                         /* 3.3 v on D+ and D- */
1722                         DBG("%s(%d) inst:[%d], QC 20V D+ 3.3v D- 3.3v",
1723                                 __func__, __LINE__, phy->inst);
1724                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1725                                 FORCE_PULLUP_DP   | FORCE_PULLUP_DM |
1726                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1727                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1728                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1729                         val &= ~(UTMIP_OP_SRC_EN | UTMIP_ON_SRC_EN);
1730                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1731                         break;
1732
1733                 case TEGRA_USB_QC2_5V:
1734                 default:
1735                         DBG("%s(%d) inst:[%d], QC 5V D+ 0.6v D- GND",
1736                                 __func__, __LINE__, phy->inst);
1737                         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1738                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1739                                 DISABLE_PULLDN_DP | FORCE_PULLDN_DM);
1740                         val = readl(base + UTMIP_BAT_CHRG_CFG0);
1741                         val &= ~(UTMIP_OP_SINK_EN | UTMIP_ON_SINK_EN);
1742                         val |= UTMIP_OP_SRC_EN;
1743                         val &= ~UTMIP_ON_SRC_EN;
1744                         writel(val, base + UTMIP_BAT_CHRG_CFG0);
1745                         break;
1746                 }
1747
1748 #ifdef QC_MEASURE_VOLTAGES
1749                 timeout = 60;
1750                 DBG("%s(%d) MEASURE VOLTAGES -- you have %d seconds",
1751                         __func__, __LINE__, timeout);
1752                 while (timeout-- > 0) {
1753                         DBG("%s(%d) COUNT = %d",
1754                                 __func__, __LINE__, timeout);
1755                         ssleep(1);
1756                 }
1757 #endif
1758         }
1759
1760         /*
1761          * If QC2 charger detected do not disable the charger
1762          * detection voltage on D- and D+. Doing this will
1763          * cause it to move to 5V
1764          */
1765         if (!status)
1766                 disable_charger_detection(base);
1767
1768         DBG("%s(%d) inst:[%d] DONE Status = %d\n",
1769                 __func__, __LINE__, phy->inst, status);
1770         return status;
1771 }
1772
1773 static bool utmi_phy_is_non_std_charger(struct tegra_usb_phy *phy)
1774 {
1775         /*
1776          * non std charger has D+/D- line float, we can apply pull up/down on
1777          * each line and verify if line status change.
1778          */
1779         /* pull up DP only */
1780         if (USB_PORTSC_LINE_DP_SET != utmi_phy_get_dp_dm_status(phy,
1781                                 FORCE_PULLUP_DP | DISABLE_PULLUP_DM |
1782                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM))
1783                 goto NOT_NON_STD_CHARGER;
1784
1785         /* pull down DP only */
1786         if (0x0 != utmi_phy_get_dp_dm_status(phy,
1787                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1788                                 FORCE_PULLDN_DP | DISABLE_PULLDN_DM))
1789                 goto NOT_NON_STD_CHARGER;
1790
1791         /* pull up DM only */
1792         if (USB_PORTSC_LINE_DM_SET != utmi_phy_get_dp_dm_status(phy,
1793                                 DISABLE_PULLUP_DP | FORCE_PULLUP_DM |
1794                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM))
1795                 goto NOT_NON_STD_CHARGER;
1796
1797         /* pull down DM only */
1798         if (0x0 != utmi_phy_get_dp_dm_status(phy,
1799                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1800                                 DISABLE_PULLDN_DP | FORCE_PULLDN_DM))
1801                 goto NOT_NON_STD_CHARGER;
1802
1803         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1804         return true;
1805
1806 NOT_NON_STD_CHARGER:
1807         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1808         return false;
1809
1810 }
1811
1812 static void utmi_phy_pmc_disable(struct tegra_usb_phy *phy)
1813 {
1814         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
1815         if (phy->pdata->u_data.host.turn_off_vbus_on_lp0 &&
1816                                         phy->pdata->port_otg) {
1817                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
1818                 pmc->pmc_ops->powerdown_pmc_wake_detect(pmc);
1819         }
1820 }
1821 static bool utmi_phy_nv_charger_detect(struct tegra_usb_phy *phy)
1822 {
1823         int status1;
1824         int status2;
1825         int status3;
1826         bool ret;
1827
1828         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1829
1830         if (utmi_phy_is_non_std_charger(phy))
1831                 return false;
1832
1833         ret = false;
1834         /* Turn off all terminations except DP pulldown */
1835         status1 = utmi_phy_get_dp_dm_status(phy,
1836                         DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1837                         FORCE_PULLDN_DP | DISABLE_PULLDN_DM);
1838
1839         /* Turn off all terminations except for DP pullup */
1840         status2 = utmi_phy_get_dp_dm_status(phy,
1841                         FORCE_PULLUP_DP | DISABLE_PULLUP_DM |
1842                         DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1843
1844         /* Check for NV charger DISABLE all terminations */
1845         status3 = utmi_phy_get_dp_dm_status(phy,
1846                         DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1847                         DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1848
1849         if ((status1 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)) &&
1850             (status2 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)) &&
1851             (status3 == (USB_PORTSC_LINE_DP_SET | USB_PORTSC_LINE_DM_SET)))
1852                 ret = true;
1853
1854         /* Restore standard termination by hardware. */
1855         utmi_phy_set_dp_dm_pull_up_down(phy, 0);
1856         return ret;
1857 }
1858
1859 static bool utmi_phy_apple_charger_1000ma_detect(struct tegra_usb_phy *phy)
1860 {
1861         unsigned long val;
1862         bool ret;
1863         void __iomem *base = phy->regs;
1864         unsigned long org_flags;
1865
1866         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1867         ret = false;
1868
1869         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1870                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1871                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1872         usleep_range(20000, 30000);
1873         utmi_phy_set_dp_dm_pull_up_down(phy,
1874                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1875                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1876
1877         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1878         val |= DIV_DET_EN;
1879         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1880
1881         usleep_range(10000, 20000);
1882
1883         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1884         val &= ~DIV_DET_EN;
1885         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1886
1887         if ((val & VOP_DIV2P0_DET) && (val & VON_DIV2P7_DET))
1888                 ret = true;
1889
1890         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1891
1892         return ret;
1893 }
1894
1895 static bool utmi_phy_apple_charger_2000ma_detect(struct tegra_usb_phy *phy)
1896 {
1897         unsigned long val;
1898         bool ret;
1899         void __iomem *base = phy->regs;
1900         unsigned long org_flags;
1901
1902         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1903         ret = false;
1904
1905         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1906                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1907                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1908         usleep_range(20000, 30000);
1909         utmi_phy_set_dp_dm_pull_up_down(phy,
1910                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1911                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1912
1913         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1914         val |= DIV_DET_EN;
1915         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1916
1917         usleep_range(10000, 20000);
1918
1919         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1920         val &= ~DIV_DET_EN;
1921         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1922
1923         if ((val & VOP_DIV2P7_DET) && (val & VON_DIV2P0_DET))
1924                 ret = true;
1925
1926         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1927
1928         return ret;
1929 }
1930
1931 static bool utmi_phy_apple_charger_500ma_detect(struct tegra_usb_phy *phy)
1932 {
1933         unsigned long val;
1934         bool ret;
1935         void __iomem *base = phy->regs;
1936         unsigned long org_flags;
1937
1938         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
1939         ret = false;
1940
1941         org_flags = utmi_phy_set_dp_dm_pull_up_down(phy,
1942                                 FORCE_PULLUP_DP | FORCE_PULLUP_DM |
1943                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1944         usleep_range(20000, 30000);
1945         utmi_phy_set_dp_dm_pull_up_down(phy,
1946                                 DISABLE_PULLUP_DP | DISABLE_PULLUP_DM |
1947                                 DISABLE_PULLDN_DP | DISABLE_PULLDN_DM);
1948
1949         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1950         val |= DIV_DET_EN;
1951         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1952
1953         usleep_range(10000, 20000);
1954
1955         val = readl(base + USB_PHY_VBUS_WAKEUP_ID);
1956         val &= ~DIV_DET_EN;
1957         writel(val, base + USB_PHY_VBUS_WAKEUP_ID);
1958
1959         if ((val & VOP_DIV2P0_DET) && (val & VON_DIV2P0_DET))
1960                 ret = true;
1961
1962         utmi_phy_set_dp_dm_pull_up_down(phy, org_flags);
1963
1964         return ret;
1965 }
1966
1967 static bool uhsic_phy_remotewake_detected(struct tegra_usb_phy *phy)
1968 {
1969         void __iomem *base = phy->regs;
1970         u32 val;
1971         unsigned int inst = phy->inst;
1972
1973         val = readl(base + UHSIC_PMC_WAKEUP0);
1974         if (!(val & EVENT_INT_ENB))
1975                 return false;
1976         val = tegra_usb_pmc_reg_read(UHSIC_STATUS(inst));
1977         if (!(UHSIC_WAKE_ALARM(inst) & val))
1978                 return false;
1979         tegra_usb_pmc_reg_update(PMC_UHSIC_SLEEP_CFG(inst),
1980                 UHSIC_WAKE_VAL(inst, WAKE_VAL_ANY),
1981                 UHSIC_WAKE_VAL(inst, WAKE_VAL_NONE));
1982
1983         tegra_usb_pmc_reg_update(PMC_UHSIC_TRIGGERS(inst),
1984                 0, UHSIC_CLR_WAKE_ALARM(inst));
1985
1986         val = readl(base + UHSIC_PMC_WAKEUP0);
1987         val &= ~EVENT_INT_ENB;
1988         writel(val, base + UHSIC_PMC_WAKEUP0);
1989         phy->pmc_remote_wakeup = true;
1990         DBG("%s:PMC interrupt detected for HSIC\n", __func__);
1991         return true;
1992 }
1993
1994 static int uhsic_phy_pre_resume(struct tegra_usb_phy *phy, bool remote_wakeup)
1995 {
1996         unsigned long val;
1997         void __iomem *base = phy->regs;
1998
1999         if (remote_wakeup) {
2000                 /* Set RUN bit */
2001                 val = readl(base + USB_USBCMD);
2002                 val |= USB_USBCMD_RS;
2003                 writel(val, base + USB_USBCMD);
2004         }
2005
2006         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2007
2008         return 0;
2009 }
2010
2011 static void uhsic_phy_restore_start(struct tegra_usb_phy *phy)
2012 {
2013         unsigned long val;
2014         void __iomem *base = phy->regs;
2015         unsigned int inst = phy->inst;
2016         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2017
2018         val = tegra_usb_pmc_reg_read(UHSIC_STATUS(inst));
2019
2020         /* check whether we wake up from the remote resume */
2021         if (UHSIC_WALK_PTR_VAL(inst) & val) {
2022                 phy->pmc_remote_wakeup = true;
2023                 DBG("%s: uhsic remote wakeup detected\n", __func__);
2024         } else {
2025                 if (!((UHSIC_STROBE_VAL(inst) | UHSIC_DATA_VAL(inst)) & val)) {
2026
2027                         /*
2028                          * If pmc wakeup is detected after putting controller
2029                          * in suspend in usb_phy_bringup_host_cotroller,
2030                          * restart bringing up host controller as
2031                          * in case of only pmc wakeup.
2032                          */
2033                         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2034                                 usb_phy_bringup_host_controller(phy);
2035                                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2036                                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP),
2037                                                 0, FPR_WAIT_TIME_US) < 0)
2038                                         pr_err("%s: timeout waiting" \
2039                                         "for SUSPEND to clear\n",
2040                                                 __func__);
2041                                 phy->ctrlr_suspended = false;
2042                         }
2043
2044                         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
2045                         phy->pmc_remote_wakeup = false;
2046                 } else {
2047                         DBG("%s(%d): setting pretend connect\n", __func__, __LINE__);
2048                         val = readl(base + UHSIC_CMD_CFG0);
2049                         val |= UHSIC_PRETEND_CONNECT_DETECT;
2050                         writel(val, base + UHSIC_CMD_CFG0);
2051                 }
2052         }
2053 }
2054
2055 static void uhsic_phy_restore_end(struct tegra_usb_phy *phy)
2056 {
2057
2058         unsigned long val, flags = 0;
2059         void __iomem *base = phy->regs;
2060         int wait_time_us = FPR_WAIT_TIME_US; /* FPR should be set by this time */
2061         bool irq_disabled = false;
2062         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2063         bool remote_wakeup_detected;
2064
2065         DBG("%s(%d)\n", __func__, __LINE__);
2066
2067         remote_wakeup_detected = phy->pmc_remote_wakeup;
2068         /*
2069          * check whether we wake up from the remote wake detected before putting
2070          * controller in suspend in usb_phy_bringup_host_controller.
2071          */
2072         if (!phy->ctrlr_suspended) {
2073                 /* wait until FPR bit is set automatically on remote resume */
2074                 do {
2075                         val = readl(base + USB_PORTSC);
2076                         udelay(1);
2077                         if (wait_time_us == 0) {
2078                         /*
2079                          * If pmc wakeup is detected after putting controller
2080                          * in suspend in usb_phy_bringup_host_cotroller,
2081                          * restart bringing up host controller as
2082                          * in case of only pmc wakeup.
2083                          */
2084                         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2085                                 usb_phy_bringup_host_controller(phy);
2086                                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2087                                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP),
2088                                                 0, FPR_WAIT_TIME_US) < 0)
2089                                         pr_err("%s: timeout waiting" \
2090                                                 " for SUSPEND to clear\n",
2091                                                 __func__);
2092                                 phy->ctrlr_suspended = false;
2093                         }
2094
2095                                 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
2096                                 phy->pmc_remote_wakeup = false;
2097                                 return;
2098                         }
2099                         wait_time_us--;
2100                 } while (val & (USB_PORTSC_RESUME | USB_PORTSC_SUSP));
2101                 /* In case of remote wakeup, disable local irq to prevent
2102                  * context switch b/t disable PMC and set RUN bit ops */
2103                 local_irq_save(flags);
2104                 irq_disabled = true;
2105         }
2106
2107         /* Disable PMC remote wake-up detection */
2108         val = readl(base + UHSIC_PMC_WAKEUP0);
2109         val &= ~EVENT_INT_ENB;
2110         writel(val, base + UHSIC_PMC_WAKEUP0);
2111
2112         /*
2113          * If pmc wakeup is detected after putting controller in suspend
2114          * in usb_phy_bringup_host_cotroller, restart bringing up host
2115          * controller as in case of only pmc wakeup.
2116          */
2117         if (phy->pmc_remote_wakeup && phy->ctrlr_suspended) {
2118                 usb_phy_bringup_host_controller(phy);
2119                 if (usb_phy_reg_status_wait(base + USB_PORTSC,
2120                         (USB_PORTSC_RESUME | USB_PORTSC_SUSP), 0,
2121                                 FPR_WAIT_TIME_US) < 0)
2122                         pr_err("%s: timeout waiting for SUSPEND to clear\n",
2123                                 __func__);
2124                 phy->ctrlr_suspended = false;
2125         }
2126
2127         val = tegra_usb_pmc_reg_read(PMC_UHSIC_SLEEP_CFG(phy->inst));
2128         if (val & UHSIC_MASTER_ENABLE(phy->inst)) {
2129                 val = readl(base + UHSIC_PADS_CFG1);
2130                 val &= ~(UHSIC_PD_TX);
2131                 writel(val, base + UHSIC_PADS_CFG1);
2132         }
2133
2134         if (irq_disabled) {
2135                 local_irq_restore(flags);
2136                 usleep_range(25000, 26000);
2137                 local_irq_save(flags);
2138         } else
2139                 usleep_range(10000, 11000);
2140
2141         pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 1);
2142         phy->pmc_remote_wakeup = false;
2143
2144         /* Restore local irq if disabled before */
2145         if (irq_disabled)
2146                 local_irq_restore(flags);
2147         if (usb_phy_reg_status_wait(base + USB_USBCMD, USB_USBCMD_RS,
2148                                                  USB_USBCMD_RS, 2000)) {
2149                 pr_err("%s: timeout waiting for USB_USBCMD_RS\n", __func__);
2150                 return;
2151         }
2152         if (remote_wakeup_detected && phy->pdata->ops &&
2153                                         phy->pdata->ops->post_remote_wakeup)
2154                 phy->pdata->ops->post_remote_wakeup();
2155 }
2156
2157 static int uhsic_rail_enable(struct tegra_usb_phy *phy)
2158 {
2159         int ret;
2160
2161         if (phy->hsic_reg == NULL) {
2162                 phy->hsic_reg = regulator_get(&phy->pdev->dev, "vddio_hsic");
2163                 if (IS_ERR_OR_NULL(phy->hsic_reg)) {
2164                         pr_err("UHSIC: Could not get regulator vddio_hsic\n");
2165                         ret = PTR_ERR(phy->hsic_reg);
2166                         phy->hsic_reg = NULL;
2167                         return ret;
2168                 }
2169         }
2170
2171         ret = regulator_enable(phy->hsic_reg);
2172         if (ret < 0) {
2173                 pr_err("%s vddio_hsic could not be enabled\n", __func__);
2174                 return ret;
2175         }
2176
2177         return 0;
2178 }
2179
2180 static int uhsic_rail_disable(struct tegra_usb_phy *phy)
2181 {
2182         int ret;
2183
2184         if (phy->hsic_reg == NULL) {
2185                 pr_warn("%s: unbalanced disable\n", __func__);
2186                 return -EIO;
2187         }
2188
2189         ret = regulator_disable(phy->hsic_reg);
2190         if (ret < 0) {
2191                 pr_err("HSIC regulator vddio_hsic cannot be disabled\n");
2192                 return ret;
2193         }
2194         regulator_put(phy->hsic_reg);
2195         phy->hsic_reg = NULL;
2196         return 0;
2197 }
2198
2199 static int uhsic_phy_open(struct tegra_usb_phy *phy)
2200 {
2201         unsigned long parent_rate;
2202         int i;
2203         int ret;
2204         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2205
2206         phy->hsic_reg = NULL;
2207         ret = uhsic_rail_enable(phy);
2208         if (ret < 0) {
2209                 pr_err("%s vddio_hsic could not be enabled\n", __func__);
2210                 return ret;
2211         }
2212
2213         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2214         parent_rate = clk_get_rate(clk_get_parent(phy->pllu_clk));
2215         for (i = 0; i < ARRAY_SIZE(uhsic_freq_table); i++) {
2216                 if (uhsic_freq_table[i].freq == parent_rate) {
2217                         phy->freq = &uhsic_freq_table[i];
2218                         break;
2219                 }
2220         }
2221         if (!phy->freq) {
2222                 pr_err("invalid pll_u parent rate %ld\n", parent_rate);
2223                 return -EINVAL;
2224         }
2225         /* reset controller for reenumerating hsic device */
2226         tegra_periph_reset_assert(phy->ctrlr_clk);
2227         udelay(2);
2228         tegra_periph_reset_deassert(phy->ctrlr_clk);
2229         udelay(2);
2230
2231         pmc_init(phy);
2232         pmc->pmc_ops->powerup_pmc_wake_detect(pmc);
2233
2234         return 0;
2235 }
2236
2237 static void uhsic_phy_close(struct tegra_usb_phy *phy)
2238 {
2239         int ret;
2240         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2241
2242         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2243         pmc->pmc_ops->powerdown_pmc_wake_detect(pmc);
2244
2245         ret = uhsic_rail_disable(phy);
2246         if (ret < 0)
2247                 pr_err("%s vddio_hsic could not be disabled\n", __func__);
2248 }
2249
2250 static int uhsic_phy_irq(struct tegra_usb_phy *phy)
2251 {
2252         unsigned long val;
2253         void __iomem *base = phy->regs;
2254         int irq_status = IRQ_HANDLED;
2255
2256         /* check if there is any remote wake event */
2257         if (!phy->pdata->unaligned_dma_buf_supported)
2258                 usb_phy_fence_read(phy);
2259         if (uhsic_phy_remotewake_detected(phy))
2260                 DBG("%s: uhsic remote wake detected\n", __func__);
2261
2262         val = readl(base + USB_SUSP_CTRL);
2263         if ((val  & USB_PHY_CLK_VALID_INT_STS) &&
2264                 (val  & USB_PHY_CLK_VALID_INT_ENB)) {
2265                 val &= ~USB_PHY_CLK_VALID_INT_ENB |
2266                                 USB_PHY_CLK_VALID_INT_STS;
2267                 writel(val , (base + USB_SUSP_CTRL));
2268
2269                 val = readl(base + USB_USBSTS);
2270                 if (!(val  & USB_USBSTS_PCI)) {
2271                         irq_status = IRQ_NONE;
2272                         goto exit;
2273                 }
2274
2275                 val = readl(base + USB_PORTSC);
2276                 if (val & USB_PORTSC_CCS)
2277                         val &= ~USB_PORTSC_WKCN;
2278                 val &= ~USB_PORTSC_RWC_BITS;
2279                 writel(val , (base + USB_PORTSC));
2280                 irq_status = IRQ_HANDLED;
2281         }
2282 exit:
2283         return irq_status;
2284 }
2285
2286 static int uhsic_phy_power_on(struct tegra_usb_phy *phy)
2287 {
2288         unsigned long val;
2289         void __iomem *base = phy->regs;
2290 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
2291         void __iomem *padctl_base = IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE);
2292 #endif
2293
2294         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2295
2296         if (phy->phy_clk_on) {
2297                 DBG("%s(%d) inst:[%d] phy clk is already On\n",
2298                                         __func__, __LINE__, phy->inst);
2299                 return 0;
2300         }
2301
2302 #ifdef CONFIG_ARCH_TEGRA_11x_SOC
2303         val = readl(padctl_base + PADCTL_SNPS_OC_MAP);
2304         val |= CONTROLLER_OC(phy->inst, 0x7);
2305         writel(val, padctl_base + PADCTL_SNPS_OC_MAP);
2306 #endif
2307
2308         val = readl(base + UHSIC_PADS_CFG1);
2309         val &= ~(UHSIC_PD_BG | UHSIC_PD_RX |
2310                         UHSIC_PD_ZI | UHSIC_RPD_DATA | UHSIC_RPD_STROBE);
2311         writel(val, base + UHSIC_PADS_CFG1);
2312
2313         val |= (UHSIC_RX_SEL | UHSIC_PD_TX);
2314         val |= UHSIC_AUTO_RTERM_EN;
2315         writel(val, base + UHSIC_PADS_CFG1);
2316
2317         val = readl(base + USB_SUSP_CTRL);
2318         val |= UHSIC_RESET;
2319         writel(val, base + USB_SUSP_CTRL);
2320         udelay(1);
2321
2322         val = readl(base + USB_SUSP_CTRL);
2323         val |= UHSIC_PHY_ENABLE;
2324         writel(val, base + USB_SUSP_CTRL);
2325
2326         val = readl(base + UHSIC_HSRX_CFG0);
2327         val |= UHSIC_IDLE_WAIT(HSIC_IDLE_WAIT_DELAY);
2328         val |= UHSIC_ELASTIC_UNDERRUN_LIMIT(HSIC_ELASTIC_UNDERRUN_LIMIT);
2329         val |= UHSIC_ELASTIC_OVERRUN_LIMIT(HSIC_ELASTIC_OVERRUN_LIMIT);
2330         writel(val, base + UHSIC_HSRX_CFG0);
2331
2332         val = readl(base + UHSIC_HSRX_CFG1);
2333         val |= UHSIC_HS_SYNC_START_DLY(HSIC_SYNC_START_DELAY);
2334         writel(val, base + UHSIC_HSRX_CFG1);
2335
2336         /* WAR HSIC TX */
2337         val = readl(base + UHSIC_TX_CFG0);
2338         val &= ~UHSIC_HS_READY_WAIT_FOR_VALID;
2339         writel(val, base + UHSIC_TX_CFG0);
2340
2341         val = readl(base + UHSIC_MISC_CFG0);
2342         val |= UHSIC_SUSPEND_EXIT_ON_EDGE;
2343         writel(val, base + UHSIC_MISC_CFG0);
2344
2345         val = readl(base + UHSIC_MISC_CFG1);
2346         val |= UHSIC_PLLU_STABLE_COUNT(phy->freq->stable_count);
2347         writel(val, base + UHSIC_MISC_CFG1);
2348
2349         val = readl(base + UHSIC_PLL_CFG1);
2350         val |= UHSIC_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
2351         val |= UHSIC_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count);
2352         writel(val, base + UHSIC_PLL_CFG1);
2353
2354         val = readl(base + USB_SUSP_CTRL);
2355         val &= ~(UHSIC_RESET);
2356         writel(val, base + USB_SUSP_CTRL);
2357         udelay(1);
2358
2359         val = tegra_usb_pmc_reg_read(PMC_UHSIC_SLEEP_CFG(phy->inst));
2360         if (!(val & UHSIC_MASTER_ENABLE(phy->inst))) {
2361                 val = readl(base + UHSIC_PADS_CFG1);
2362                 val &= ~(UHSIC_PD_TX);
2363                 writel(val, base + UHSIC_PADS_CFG1);
2364         }
2365
2366         /* HSIC pad tracking circuit power down sequence */
2367         val = readl(base + UHSIC_PADS_CFG1);
2368         val &= ~(UHSIC_PD_TRK);
2369         writel(val, base + UHSIC_PADS_CFG1);
2370         /* Wait for 25usec */
2371         udelay(25);
2372         val |= UHSIC_PD_TRK;
2373         writel(val, base + UHSIC_PADS_CFG1);
2374
2375         /* Enable bus keepers always */
2376         val = readl(base + UHSIC_SPARE_CFG0);
2377         val |= FORCE_BK_ON;
2378         writel(val, base + UHSIC_SPARE_CFG0);
2379
2380         /*SUSP_CTRL has to be toggled to enable host PHY clock */
2381         val = readl(base + USB_SUSP_CTRL);
2382         val |= USB_SUSP_CLR;
2383         writel(val, base + USB_SUSP_CTRL);
2384
2385         val = readl(base + USB_SUSP_CTRL);
2386         val &= ~USB_SUSP_CLR;
2387         writel(val, base + USB_SUSP_CTRL);
2388
2389         val = readl(base + USB_USBMODE);
2390         val |= USB_USBMODE_HOST;
2391         writel(val, base + USB_USBMODE);
2392
2393         /* Change the USB controller PHY type to HSIC */
2394         val = readl(base + HOSTPC1_DEVLC);
2395         val &= ~HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_MASK);
2396         val |= HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_HSIC);
2397         val &= ~HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_MASK);
2398         val |= HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_HIGH_SPEED);
2399         val &= ~HOSTPC1_DEVLC_STS;
2400         writel(val, base + HOSTPC1_DEVLC);
2401
2402         val = readl(base + USB_PORTSC);
2403         val &= ~(USB_PORTSC_WKOC | USB_PORTSC_WKDS);
2404         writel(val, base + USB_PORTSC);
2405
2406         val = readl(base + UHSIC_TX_CFG0);
2407         val |= UHSIC_HS_POSTAMBLE_OUTPUT_ENABLE;
2408         writel(val, base + UHSIC_TX_CFG0);
2409
2410         val = readl(base + UHSIC_PADS_CFG0);
2411         /* Clear RTUNEP, SLEWP & SLEWN bit fields */
2412         val &= ~(UHSIC_TX_RTUNEP | UHSIC_TX_SLEWP | UHSIC_TX_SLEWN);
2413         /* set Rtune impedance to 50 ohm */
2414 #ifdef CONFIG_ARCH_TEGRA_12x_SOC
2415         val |= UHSIC_TX_RTUNE_P(0xA);
2416 #else
2417         val |= UHSIC_TX_RTUNE_P(0xC);
2418 #endif
2419         writel(val, base + UHSIC_PADS_CFG0);
2420
2421         if (usb_phy_reg_status_wait(base + USB_SUSP_CTRL,
2422                                 USB_PHY_CLK_VALID, USB_PHY_CLK_VALID, 2500)) {
2423                 pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
2424                 return -ETIMEDOUT;
2425         }
2426
2427         phy->phy_clk_on = true;
2428         phy->hw_accessible = true;
2429
2430         if (!readl(base + USB_ASYNCLISTADDR))
2431                 _usb_phy_init(phy);
2432
2433         if (phy->pmc_sleepwalk) {
2434                 DBG("%s(%d) inst:[%d] restore phy\n", __func__, __LINE__,
2435                                         phy->inst);
2436                 uhsic_phy_restore_start(phy);
2437                 usb_phy_bringup_host_controller(phy);
2438                 uhsic_phy_restore_end(phy);
2439                 phy->pmc_sleepwalk = false;
2440         }
2441
2442         val = readl(base + USB_TXFILLTUNING);
2443         if ((val & USB_FIFO_TXFILL_MASK) != USB_FIFO_TXFILL_THRES(0x10)) {
2444                 val = USB_FIFO_TXFILL_THRES(0x10);
2445                 writel(val, base + USB_TXFILLTUNING);
2446         }
2447
2448         return 0;
2449 }
2450
2451 static int uhsic_phy_power_off(struct tegra_usb_phy *phy)
2452 {
2453         unsigned long val;
2454         void __iomem *base = phy->regs;
2455         bool port_connected;
2456         struct tegra_usb_pmc_data *pmc = &pmc_data[phy->inst];
2457
2458         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2459         if (!phy->phy_clk_on) {
2460                 DBG("%s(%d) inst:[%d] phy clk is already off\n",
2461                                         __func__, __LINE__, phy->inst);
2462                 return 0;
2463         }
2464
2465         /* Disable interrupts */
2466         writel(0, base + USB_USBINTR);
2467
2468         /* check for port connect status */
2469         val = readl(base + USB_PORTSC);
2470         port_connected = val & USB_PORTSC_CCS;
2471
2472         if (phy->pmc_sleepwalk == false && port_connected) {
2473                 pmc->pmc_ops->setup_pmc_wake_detect(pmc);
2474
2475                 phy->pmc_remote_wakeup = false;
2476                 phy->pmc_sleepwalk = true;
2477
2478                 val = readl(base + UHSIC_PMC_WAKEUP0);
2479                 val |= EVENT_INT_ENB;
2480                 writel(val, base + UHSIC_PMC_WAKEUP0);
2481
2482                 val = readl(base + USB_SUSP_CTRL);
2483                 val &= ~USB_WAKE_ON_CNNT_EN_DEV;
2484                 writel(val, base + USB_SUSP_CTRL);
2485         } else {
2486                 phy->pmc_sleepwalk = true;
2487         }
2488
2489         val = readl(base + HOSTPC1_DEVLC);
2490         val |= HOSTPC1_DEVLC_PHCD;
2491         writel(val, base + HOSTPC1_DEVLC);
2492
2493         /* Remove power downs for HSIC from PADS CFG1 register */
2494         val = readl(base + UHSIC_PADS_CFG1);
2495         val |= (UHSIC_PD_BG | UHSIC_PD_TRK |
2496                         UHSIC_PD_ZI | UHSIC_PD_TX);
2497         writel(val, base + UHSIC_PADS_CFG1);
2498
2499         if (usb_phy_reg_status_wait(base + USB_SUSP_CTRL,
2500                 USB_PHY_CLK_VALID, 0, 2500))
2501                 pr_warn("%s: timeout waiting for phy to disable\n", __func__);
2502
2503         DBG("%s(%d) inst:[%d] End\n", __func__, __LINE__, phy->inst);
2504
2505         phy->phy_clk_on = false;
2506         phy->hw_accessible = false;
2507
2508         return 0;
2509 }
2510
2511 static int uhsic_phy_bus_port_power(struct tegra_usb_phy *phy)
2512 {
2513         unsigned long val;
2514         void __iomem *base = phy->regs;
2515
2516         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2517
2518         val = readl(base + USB_USBMODE);
2519         val |= USB_USBMODE_HOST;
2520         writel(val, base + USB_USBMODE);
2521
2522         /* Change the USB controller PHY type to HSIC */
2523         val = readl(base + HOSTPC1_DEVLC);
2524         val &= ~(HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_MASK));
2525         val |= HOSTPC1_DEVLC_PTS(HOSTPC1_DEVLC_PTS_HSIC);
2526         val &= ~(HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_MASK));
2527         val |= HOSTPC1_DEVLC_PSPD(HOSTPC1_DEVLC_PSPD_HIGH_SPEED);
2528         writel(val, base + HOSTPC1_DEVLC);
2529
2530         val = readl(base + UHSIC_MISC_CFG0);
2531         val |= UHSIC_DETECT_SHORT_CONNECT;
2532         writel(val, base + UHSIC_MISC_CFG0);
2533         udelay(1);
2534         if (phy->pdata->ops && phy->pdata->ops->port_power)
2535                 phy->pdata->ops->port_power();
2536
2537         return 0;
2538 }
2539
2540 static struct tegra_usb_phy_ops utmi_phy_ops = {
2541         .init           = _usb_phy_init,
2542         .reset          = usb_phy_reset,
2543         .open           = utmi_phy_open,
2544         .close          = utmi_phy_close,
2545         .irq            = utmi_phy_irq,
2546         .power_on       = utmi_phy_power_on,
2547         .power_off      = utmi_phy_power_off,
2548         .pre_resume = utmi_phy_pre_resume,
2549         .resume = utmi_phy_resume,
2550         .post_resume    = utmi_phy_post_resume,
2551         .charger_detect = utmi_phy_charger_detect,
2552         .qc2_charger_detect = utmi_phy_qc2_charger_detect,
2553         .cdp_charger_detect = cdp_charger_detection,
2554         .nv_charger_detect = utmi_phy_nv_charger_detect,
2555         .maxim_charger_14675 = maxim_charger_detection,
2556         .apple_charger_1000ma_detect = utmi_phy_apple_charger_1000ma_detect,
2557         .apple_charger_2000ma_detect = utmi_phy_apple_charger_2000ma_detect,
2558         .apple_charger_500ma_detect = utmi_phy_apple_charger_500ma_detect,
2559         .pmc_disable = utmi_phy_pmc_disable,
2560 };
2561
2562 static struct tegra_usb_phy_ops uhsic_phy_ops = {
2563         .init           = _usb_phy_init,
2564         .reset          = usb_phy_reset,
2565         .open           = uhsic_phy_open,
2566         .close          = uhsic_phy_close,
2567         .irq            = uhsic_phy_irq,
2568         .power_on       = uhsic_phy_power_on,
2569         .power_off      = uhsic_phy_power_off,
2570         .pre_resume     = uhsic_phy_pre_resume,
2571         .port_power = uhsic_phy_bus_port_power,
2572 };
2573
2574 static struct tegra_usb_phy_ops *phy_ops[] = {
2575         [TEGRA_USB_PHY_INTF_UTMI] = &utmi_phy_ops,
2576         [TEGRA_USB_PHY_INTF_HSIC] = &uhsic_phy_ops,
2577 };
2578
2579 int tegra11x_usb_phy_init_ops(struct tegra_usb_phy *phy)
2580 {
2581         DBG("%s(%d) inst:[%d]\n", __func__, __LINE__, phy->inst);
2582
2583         phy->ops = phy_ops[phy->pdata->phy_intf];
2584
2585         /* FIXME: uncommenting below line to make USB host mode fail*/
2586         /* pmc->pmc_ops->power_down_pmc(pmc); */
2587
2588         return 0;
2589 }