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