]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/pinctrl/pinctrl-tegra186-padctl-uphy.c
arm64: create android devkit 24x7 DT file
[sojka/nv-tegra/linux-3.10.git] / drivers / pinctrl / pinctrl-tegra186-padctl-uphy.c
1 /*
2  * Copyright (c) 2015, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  */
13
14 #include <linux/delay.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/phy/phy.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/workqueue.h>
24 #include <linux/tegra-fuse.h>
25 #include <linux/tegra-soc.h>
26 #include <linux/clk/tegra.h>
27 #include <linux/tegra_prod.h>
28
29 #define TEGRA_XUSB_PCIE_PHYS 1
30 #define TEGRA_XUSB_SATA_PHYS 1
31 #define TEGRA_XUSB_UFS_PHYS 1
32 #define TEGRA_XUSB_USB3_PHYS 3
33 #define TEGRA_XUSB_UTMI_PHYS 3
34 #define TEGRA_XUSB_HSIC_PHYS 1
35 #define TEGRA_XUSB_NUM_USB_PHYS (TEGRA_XUSB_USB3_PHYS + TEGRA_XUSB_UTMI_PHYS + \
36                                  TEGRA_XUSB_HSIC_PHYS)
37 #define TEGRA_XUSB_NUM_PHYS (TEGRA_XUSB_NUM_USB_PHYS + TEGRA_XUSB_PCIE_PHYS + \
38                                 TEGRA_XUSB_SATA_PHYS + TEGRA_XUSB_UFS_PHYS)
39
40 #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
41 #include "../../../arch/arm/mach-tegra/iomap.h" /* FIXME */
42
43 #ifdef TRACE
44 #undef TRACE
45 #endif
46 #define TRACE(fmt, args...) pr_info("%s %d " fmt "\n", __func__, __LINE__, ## args)
47
48 #ifdef TRACE_DEV
49 #undef TRACE_DEV
50 #endif
51 #define TRACE_DEV(dev, fmt, args...) dev_info(dev, "%s %d " fmt "\n", __func__, __LINE__, ## args)
52
53 #include "core.h"
54 #include "pinctrl-utils.h"
55
56 #define T186_UPHY_LANES                         (6)
57
58 /* UPHY PLL registers */
59 #define UPHY_PLL_CTL_1                          (0x0)
60 #define   PLL_IDDQ                              (1 << 0)
61 #define   PLL_SLEEP(x)                          (((x) & 0x3) << 1)
62 #define   PLL_ENABLE                            (1 << 3)
63 #define   PWR_OVRD                              (1 << 4)
64 #define   RATE_ID(x)                            (((x) & 0x3) << 8)
65 #define   RATE_ID_OVRD                          (1 << 11)
66 #define   LOCKDET_STATUS                        (1 << 15)
67
68 #define UPHY_PLL_CTL_2                          (0x4)
69 #define   CAL_EN                                (1 << 0)
70 #define   CAL_DONE                              (1 << 1)
71 #define   CAL_OVRD                              (1 << 2)
72 #define   CAL_RESET                             (1 << 3)
73 #define   RCAL_EN                               (1 << 12)
74 #define   RCAL_CLK_EN                           (1 << 13)
75 #define   RCAL_OVRD                             (1 << 15)
76 #define   RCAL_DONE                             (1 << 31)
77
78 #define UPHY_PLL_CTL_4                          (0xc)
79
80 /* UPHY Lane registers */
81 #define UPHY_LANE_AUX_CTL_1                     (0x0)
82 #define   AUX_RX_IDLE_TH(x)                     (((x) & 0x3) << 24)
83
84 #define UPHY_LANE_DIRECT_CTL_1                  (0x10)
85 #define   MISC_CTRL(x)                          (((x) & 0xff) << 0)
86 #define   MISC_OUT(x)                           (((x) & 0xff) << 16)
87
88 #define UPHY_LANE_DIRECT_CTL_2                  (0x14)
89 #define   CFG_WDATA(x)                          (((x) & 0xffff) << 0)
90 #define   CFG_ADDR(x)                           (((x) & 0xff) << 16)
91 #define   CFG_WDS(x)                            (((x) & 0x1) << 24)
92 #define   CFG_RDS(x)                            (((x) & 0x1) << 25)
93 #define   CFG_RESET(x)                          (((x) & 0x1) << 27)
94
95 #define UPHY_LANE_MUX                           (0x284)
96 #define   SEL(x)                                (((x) & 0x7) << 0)
97 #define     SEL_XUSB                            SEL(0)
98 #define     SEL_PCIE                            SEL(1)
99 #define     SEL_SATA                            SEL(2)
100 #define     SEL_MPHY                            SEL(3)
101 #define   CLAMP_EN_EARLY                        (1 << 8)
102 #define   FORCE_IDDQ_DISABLE                    (1 << 9)
103
104 /* UPHY APB Dynamic DYN_CTL registers */
105 #define UPHY_LANE_DYN_CTL_1                     (0x80)
106 #define   TX_DRV_AMP_SEL0(x)                    (((x) & 0x3f) << 0)
107 #define   TX_DRV_AMP_SEL1(x)                    (((x) & 0x3f) << 8)
108 #define   TX_DRV_AMP_SEL2(x)                    (((x) & 0x3f) << 16)
109 #define   TX_DRV_AMP_SEL3(x)                    (((x) & 0x3f) << 24)
110
111 #define UPHY_LANE_DYN_CTL_2                     (0x84)
112 #define   TX_DRV_AMP_SEL4(x)                    (((x) & 0x3f) << 0)
113 #define   TX_DRV_AMP_SEL5(x)                    (((x) & 0x3f) << 8)
114 #define   TX_DRV_AMP_SEL6(x)                    (((x) & 0x3f) << 16)
115 #define   TX_DRV_AMP_SEL7(x)                    (((x) & 0x3f) << 24)
116
117 #define UPHY_LANE_DYN_CTL_3                     (0x88)
118 #define   TX_DRV_AMP_SEL8(x)                    (((x) & 0x3f) << 0)
119 #define   TX_DRV_AMP_SEL9(x)                    (((x) & 0x3f) << 8)
120
121 #define UPHY_LANE_DYN_CTL_4                     (0x8c)
122 #define   TX_DRV_POST_SEL0(x)                   (((x) & 0x3f) << 0)
123 #define   TX_DRV_PRE_SEL0(x)                    (((x) & 0x3f) << 8)
124 #define   TX_DRV_POST_SEL1(x)                   (((x) & 0x3f) << 16)
125 #define   TX_DRV_PRE_SEL1(x)                    (((x) & 0x3f) << 24)
126
127 #define UPHY_LANE_DYN_CTL_5                     (0x90)
128 #define   TX_DRV_POST_SEL2(x)                   (((x) & 0x3f) << 0)
129 #define   TX_DRV_PRE_SEL2(x)                    (((x) & 0x3f) << 8)
130 #define   TX_DRV_POST_SEL3(x)                   (((x) & 0x3f) << 16)
131 #define   TX_DRV_PRE_SEL3(x)                    (((x) & 0x3f) << 24)
132
133 /* FUSE USB_CALIB registers */ /* TODO: check spec */
134 #define USB_CALIB_HS_CURR_LEVEL_PADX_SHIFT(x)   ((x) ? (11 + (x - 1) * 6) : 0)
135 #define USB_CALIB_HS_CURR_LEVEL_PAD_MASK        (0x3f)
136 #define USB_CALIB_HS_TERM_RANGE_ADJ_SHIFT       (7)
137 #define USB_CALIB_HS_TERM_RANGE_ADJ_MASK        (0xf)
138
139 #define USB_CALIB_EXT_RPD_CTRL_SHIFT            (7)
140 #define USB_CALIB_EXT_RPD_CTRL_MASK             (0xf)
141
142 /* FUSE SATA MPHY registers */
143 #define FUSE_SATA_MPHY_ODM_CALIB_0      (0x224)
144 #define    SATA_MPHY_ODM_CALIB_0_1(x)           (((x) & 0x3) << 0)
145
146 #define FUSE_SATA_NV_CALIB_0            (0x49c)
147 #define    SATA_NV_CALIB_0_1(x)                 (((x) & (0x3 << 0)) >> 0)
148 #define    SATA_NV_CALIB_2_3(x)                 (((x) & (0x3 << 2)) >> 2)
149
150 #define FUSE_MPHY_NV_CALIB_0            (0x4a0)
151 #define    MPHY_NV_CALIB_0_1(x)                 (((x) & (0x3 << 0)) >> 0)
152 #define    MPHY_NV_CALIB_2_3(x)                 (((x) & (0x3 << 2)) >> 2)
153 #define    MPHY_NV_CALIB_4_5(x)                 (((x) & (0x3 << 4)) >> 4)
154
155 /* XUSB PADCTL registers */
156 #define XUSB_PADCTL_USB2_PAD_MUX                (0x4)
157
158 #define XUSB_PADCTL_USB2_PORT_CAP               (0x8)
159 #define XUSB_PADCTL_SS_PORT_CAP                 (0xc)
160 #define   PORTX_CAP_SHIFT(x)                    ((x) * 4)
161 #define   PORT_CAP_MASK                         (0x3)
162 #define     PORT_CAP_DISABLED                   (0x0)
163 #define     PORT_CAP_HOST                       (0x1)
164 #define     PORT_CAP_DEVICE                     (0x2)
165 #define     PORT_CAP_OTG                        (0x3)
166
167 #define XUSB_PADCTL_ELPG_PROGRAM                (0x20)
168
169 #define XUSB_PADCTL_ELPG_PROGRAM_1              (0x24)
170 #define   SSPX_ELPG_CLAMP_EN(x)                 (1 << (0 + (x) * 3))
171 #define   SSPX_ELPG_CLAMP_EN_EARLY(x)           (1 << (1 + (x) * 3))
172 #define   SSPX_ELPG_VCORE_DOWN(x)               (1 << (2 + (x) * 3))
173
174 #define USB2_BATTERY_CHRG_OTGPADX_CTL1(x)       (0x84 + (x) * 0x40)
175 #define   VREG_LEV(x)                           (((x) & 0x3) << 7)
176 #define   VREG_FIX18                            (1 << 6)
177
178 #define XUSB_PADCTL_USB2_OTG_PADX_CTL0(x)       (0x88 + (x) * 0x40)
179 #define   HS_CURR_LEVEL(x)                      ((x) & 0x3f)
180 #define   USB2_OTG_PD                           (1 << 26)
181 #define   USB2_OTG_PD2                          (1 << 27)
182 #define   USB2_OTG_PD_ZI                        (1 << 29)
183
184 #define XUSB_PADCTL_USB2_OTG_PADX_CTL1(x)       (0x8c + (x) * 0x40)
185 #define   USB2_OTG_PD_DR                        (1 << 2)
186 #define   TERM_RANGE_ADJ(x)                     (((x) & 0xf) << 3)
187 #define   RPD_CTRL(x)                           (((x) & 0x1f) << 26)
188
189 #define XUSB_PADCTL_USB2_BIAS_PAD_CTL0          (0x284)
190 #define   BIAS_PAD_PD                           (1 << 11)
191
192 #define USB2_VBUS_ID                            (0x360)
193 #define   VBUS_OVERRIDE                         (1 << 14)
194
195 /* XUSB AO registers */
196 #define XUSB_AO_UTMIP_TRIGGERS(x)               (0x40 + (x) * 4)
197 #define   CLR_WALK_PTR                          (1 << 0)
198 #define   CAP_CFG                               (1 << 1)
199 #define   CLR_WAKE_ALARM                        (1 << 3)
200
201 #define XUSB_AO_UTMIP_SLEEPWALK_CFG(x)          (0xd0 + (x) * 4)
202 #define XUSB_AO_UHSIC_SLEEPWALK_CFG(x)          (0xf0 + (x) * 4)
203 #define   MASTER_ENABLE                         (1 << 15)
204 #define   MASTER_CFG_SEL                        (1 << 22)
205
206 #define XUSB_AO_UTMIP_SLEEPWALK(x)              (0x120 + (x) * 4)
207 #if 0 /* TODO: implement */
208 #define padctl_ao_readl         padctl_readl /* FIXME */
209 #define padctl_ao_writel        padctl_writel /* FIXME */
210 static int utmip_setup_sleepwalk(struct tegra_padctl_uphy *ctx, int pad)
211 {
212         u32 reg;
213
214         /* ensure sleepwalk logic is disabled */
215         reg = padctl_ao_readl(ctx, XUSB_AO_UTMIP_SLEEPWALK_CFG(pad));
216         reg &= ~MASTER_ENABLE;
217         padctl_ao_writel(ctx, reg, XUSB_AO_UTMIP_SLEEPWALK_CFG(pad));
218
219
220         return 0;
221 }
222 #endif
223 /* UPHY PLL config space registers */
224 #define MGMT_FREQ_CTRL_ID0                      (0)
225 #define MGMT_FREQ_CTRL_ID1                      (1)
226 #define   CFG_FREQ_NDIV(x)                      (((x) & 0xff) << 0)
227 #define   CFG_FREQ_MDIV(x)                      (((x) & 0x3) << 8)
228 #define   CFG_FREQ_PSDIV(x)                     (((x) & 0x3) << 10)
229 #define   CFG_MODE(x)                           (((x) & 0x3) << 12)
230
231 #define MGMT_REFCLK_CTRL                        (2)
232
233 #define MGMT_CORECLK_CTRL_ID0                   (3)
234 #define MGMT_CORECLK_CTRL_ID1                   (4)
235 #define   CFG_TXCLKREF_EN(x)                    (((x) & 0x1) << 0)
236 #define   CFG_TXCLKREF_SEL(x)                   (((x) & 0x3) << 4)
237 #define     DIVIDE_TX_BY_10                     (0)
238 #define     DIVIDE_TX_BY_8                      (1)
239 #define     DIVIDE_TX_BY_5                      (2)
240 #define     DIVIDE_TX_BY_4                      (3)
241 #define   CFG_XDIGCLK_EN(x)                     (((x) & 0x1) << 8)
242 #define   CFG_XDIGCLK_SEL(x)                    (((x) & 0x3) << 12)
243
244 #define PLLC_CRSWRD_OVRD_ID0                    (5)
245
246 #define PLLC_CRSWRD_OVRD_ID1                    (6)
247
248 #define MGMT_CYA_CTRL                           (29)
249 #define   CFG_MGMT_CLK_SEL(x)                   (((x) & 0x1) << 4)
250
251 /* UPHY Lane config space registers */
252 #define MGMT_TX_RATE_CTRL_ID0                   (0)
253 #define MGMT_TX_RATE_CTRL_ID1                   (1)
254 #define MGMT_TX_RATE_CTRL_ID2                   (2)
255 #define MGMT_TX_RATE_CTRL_ID3                   (3)
256 #define   TX_RATE_SDIV(x)                       (((x) & 0x3) << 0)
257 #define     SDIV4                               (0)
258 #define     SDIV2                               (1)
259 #define     SDIV1                               (2)
260 #define     SDIVX                               (3)
261
262 #define MGMT_RX_RATE_CTRL_ID0                   (4)
263 #define MGMT_RX_RATE_CTRL_ID1                   (5)
264 #define MGMT_RX_RATE_CTRL_ID2                   (6)
265 #define MGMT_RX_RATE_CTRL_ID3                   (7)
266 #define   RX_RATE_SDIV(x)                       (((x) & 0x3) << 0)
267 #define   RX_RATE_CDIV(x)                       (((x) & 0x3) << 4)
268 #define     CDIV1                               (0)
269 #define     CDIV2                               (1)
270 #define     CDIV4                               (2)
271 #define     CDIV8                               (3)
272
273 #define MGMT_TX_CTRL                            (8)
274 #define   TX_TERM_MODE(x)                       (((x) & 0x1) << 0)
275 #define   SYNC_DLY(x)                           (((x) & 0xf) << 4)
276
277 #define AE_CTLE_CTRL_ID0                        (10)
278 #define AE_CTLE_CTRL_ID1                        (11)
279 #define AE_CTLE_CTRL_ID2                        (12)
280 #define AE_CTLE_CTRL_ID3                        (13)
281 #define   LF_UGRAY(x)                           (((x) & 0xf) << 0)
282 #define   HF_UBIN(x)                            (((x) & 0xf) << 4)
283
284 #define AE_DFE0_CTRL_ID0                        (14)
285 #define AE_DFE0_CTRL_ID1                        (15)
286 #define AE_DFE0_CTRL_ID2                        (16)
287 #define AE_DFE1_CTRL_ID2                        (20)
288 #define   H1_SBIN(x)                            (((x) & 0x1f) << 0)
289 #define   H2_SBIN(x)                            (((x) & 0xf) << 8)
290 #define   H3_SBIN(x)                            (((x) & 0x7) << 12)
291
292 #define AE_DFE1_CTRL_ID0                        (18)
293 #define   H4_SBIN(x)                            (((x) & 0x7) << 0)
294 #define   H0_SBIN(x)                            (((x) & 0x3f) << 8)
295
296 #define AE_CDR_CTRL_ID0                         (22)
297 #define AE_CDR_CTRL_ID1                         (23)
298 #define AE_CDR_CTRL_ID2                         (24)
299 #define AE_CDR_CTRL_ID3                         (25)
300 #define   PHGAIN(x)                             (((x) & 0xf) << 0)
301 #define   FRGAIN(x)                             (((x) & 0xf) << 4)
302 #define   FRLOOP_EN(x)                          (((x) & 0x1) << 8)
303
304 #define AE_EQ0_CTRL_ID0                         (26)
305 #define AE_EQ0_CTRL_ID1                         (27)
306 #define AE_EQ0_CTRL_ID2                         (28)
307 #define   EQ0_SEQ_MODE(x)                       (((x) & 0x3) << 0)
308 #define     SEQ_MODE_HF_Z_H0_HN_Z               (0)
309 #define     SEQ_MODE_H0_HN_Z_HF_Z               (1)
310 #define     SEQ_MODE_HF_H0_HN_Z                 (2)
311 #define     SEQ_MODE_HF_H0_HN_Z_HF              (3)
312 #define   H0INIT_ITERS(x)                       (((x) & 0xf) << 4)
313 #define   H4_GRAD_INV(x)                        (((x) & 0x1) << 8)
314 #define   H3_GRAD_INV(x)                        (((x) & 0x1) << 9)
315 #define   H2_GRAD_INV(x)                        (((x) & 0x1) << 10)
316 #define   H1_GRAD_INV(x)                        (((x) & 0x1) << 11)
317 #define   H0_GRAD_INV(x)                        (((x) & 0x1) << 12)
318 #define   CTLE_HF_GRAD_INV(x)                   (((x) & 0x1) << 13)
319 #define   SAMP_VOS_GRAD_INV(x)                  (((x) & 0x1) << 14)
320 #define   CTLE_VOS_GRAD_INV(x)                  (((x) & 0x1) << 15)
321
322 #define AE_EQ1_CTRL_ID0                         (30)
323 #define AE_EQ1_CTRL_ID1                         (31)
324 #define AE_EQ1_CTRL_ID2                         (32)
325 #define AE_EQ1_CTRL_ID3                         (33)
326 #define   CTLE_HF_MODE(x)                       (((x) & 0x3) << 0)
327 #define     VAR_MODE_AUTO                       (0)
328 #define     VAR_MODE_OFF                        (1)
329 #define     VAR_MODE_HOLD                       (2)
330 #define     VAR_MODE_LOAD                       (3)
331 #define   CTLE_HF_GRAD(x)                       (((x) & 0x3) << 2)
332 #define      CTLE_HF_GRAD_1P5                   (0)
333 #define      CTLE_HF_GRAD_2P5                   (1)
334 #define      CTLE_HF_GRAD_3P5                   (2)
335 #define      CTLE_HF_GRAD_1TP                   (3)
336 #define   H1_MODE(x)                            (((x) & 0x3) << 8)
337 #define   H2_MODE(x)                            (((x) & 0x3) << 10)
338 #define   H3_MODE(x)                            (((x) & 0x3) << 12)
339 #define   H4_MODE(x)                            (((x) & 0x3) << 14)
340
341 #define AE_EQ2_CTRL_ID0                         (34)
342 #define AE_EQ2_CTRL_ID1                         (35)
343 #define AE_EQ2_CTRL_ID2                         (36)
344 #define AE_EQ2_CTRL_ID3                         (37)
345 #define   H0_MODE(x)                            (((x) & 0x3) << 0)
346 #define   H0_DAC_TIME(x)                        (((x) & 0x3) << 2)
347 #define   H0_TIME(x)                            (((x) & 0xf) << 4)
348 #define   H0_ITERS(x)                           (((x) & 0xf) << 8)
349 #define   HN_ITERS(x)                           (((x) & 0xf) << 12)
350
351 #define AE_EQ3_CTRL_ID0                         (38)
352 #define AE_EQ3_CTRL_ID2                         (40)
353 #define   H0_GAIN(x)                            (((x) & 0xf) << 0)
354 #define   HN_GAIN(x)                            (((x) & 0xf) << 4)
355 #define   CTLE_HF_TIME(x)                       (((x) & 0xf) << 8)
356 #define   CTLE_HF_GAIN(x)                       (((x) & 0xf) << 12)
357
358 #define MGMT_RX_PI_CTRL_ID2                     (48)
359 #define   TX_SLEW(x)                            (((x) & 0x3) << 0)
360 #define   RX_SLEW(x)                            (((x) & 0x3) << 2)
361
362 #define CLK_RST_CONTROLLER_RST_DEV_UPHY_0               (0x4000c)
363 #define   SWR_UPHY_RST                                  (1 << 0)
364
365 #define CLK_RST_CONTROLLER_RST_DEV_PEX_USB_UPHY_0       (0x40000)
366 #define   SWR_PEX_USB_UPHY_RST                          (1 << 0)
367 #define   SWR_PEX_USB_UPHY_PLL0_RST                     (1 << 7)
368 #define   SWR_PEX_USB_UPHY_PLL1_RST                     (1 << 15)
369 #define   SWR_PEX_USB_UPHY_LANE_RST(lane)               (1 << (16+lane))
370
371 enum tegra186_function {
372         TEGRA186_FUNC_HSIC,
373         TEGRA186_FUNC_HSIC_PLUS,
374         TEGRA186_FUNC_XUSB,
375         TEGRA186_FUNC_UART,
376         TEGRA186_FUNC_PCIE,
377         TEGRA186_FUNC_USB3,
378         TEGRA186_FUNC_SATA,
379         TEGRA186_FUNC_MPHY,
380         TEGRA186_FUNC_RSVD_0,
381         TEGRA186_FUNC_RSVD_3,
382 };
383
384 struct tegra_padctl_uphy_function {
385         const char *name;
386         const char * const *groups;
387         unsigned int num_groups;
388 };
389
390 struct tegra_padctl_uphy_group {
391         const unsigned int *funcs;
392         unsigned int num_funcs;
393 };
394
395 struct tegra_padctl_uphy_soc {
396         const struct pinctrl_pin_desc *pins;
397         unsigned int num_pins;
398
399         const struct tegra_padctl_uphy_function *functions;
400         unsigned int num_functions;
401
402         const struct tegra_padctl_uphy_lane *lanes;
403         unsigned int num_lanes;
404
405         unsigned int hsic_port_offset;
406 };
407
408 struct tegra_padctl_uphy_lane {
409         const char *name;
410
411         unsigned int offset;
412         unsigned int shift;
413         unsigned int mask;
414         unsigned int iddq;
415
416         const unsigned int *funcs;
417         unsigned int num_funcs;
418 };
419
420 struct tegra_xusb_fuse_calibration {
421         u32 hs_curr_level[TEGRA_XUSB_UTMI_PHYS];
422         u32 hs_term_range_adj;
423         u32 rpd_ctrl;
424 };
425
426 struct tegra_fuse_calibration {
427         u32 sata_mphy_odm;
428         u32 sata_nv;
429         u32 mphy_nv;
430 };
431
432 struct tegra_xusb_usb3_port {
433         unsigned int lane;
434 };
435
436 struct tegra_padctl_uphy {
437         struct device *dev;
438         void __iomem *padctl_regs;
439         void __iomem *uphy_regs;
440         void __iomem *uphy_pll_regs[2];
441         void __iomem *uphy_lane_regs[6];
442
443         struct clk *clk; /* uphy pad macro clock */
444
445         struct mutex lock;
446
447         const struct tegra_padctl_uphy_soc *soc;
448         struct tegra_xusb_fuse_calibration calib;
449         struct tegra_fuse_calibration fuse_calib;
450         struct tegra_prod_list *prod_list;
451         struct pinctrl_dev *pinctrl;
452         struct pinctrl_desc desc;
453
454         struct phy_provider *provider;
455 //      struct phy *phys[TEGRA_XUSB_NUM_PHYS];
456         struct phy *usb3_phys[TEGRA_XUSB_USB3_PHYS];
457         struct phy *utmi_phys[TEGRA_XUSB_UTMI_PHYS];
458         struct phy *hsic_phys[TEGRA_XUSB_HSIC_PHYS];
459         struct phy *pcie_phys[TEGRA_XUSB_PCIE_PHYS];
460         struct phy *sata_phys[TEGRA_XUSB_SATA_PHYS];
461         struct phy *ufs_phys[TEGRA_XUSB_UFS_PHYS];
462         unsigned long usb3_lanes;
463         unsigned long pcie_lanes;
464         unsigned long sata_lanes;
465         unsigned long ufs_lanes;
466
467         unsigned int padctl_clients;
468         bool sata_bypass_fuse;
469
470         struct tegra_xusb_usb3_port usb3_ports[TEGRA_XUSB_USB3_PHYS];
471         unsigned int utmi_enable;
472         unsigned int hs_curr_level_offset[TEGRA_XUSB_UTMI_PHYS];
473         struct regulator *vbus[TEGRA_XUSB_UTMI_PHYS];
474         struct regulator *vddio_hsic;
475
476         int uphy_pll_clients[2];
477         struct clk *uphy_pll_clk[2];
478         struct clk *uphy_lane_clk[6];
479
480 };
481
482 #ifdef SIM
483 #undef writel
484 #define writel(v,c) {*(u32 *)(c) = v;}
485 #undef readl
486 #define readl(c) ({ *(u32 *)(c);})
487 #endif
488
489 #if 0
490 static inline void padctl_writel(struct tegra_padctl_uphy *padctl, u32 value,
491                                  unsigned long offset)
492 {
493         writel(value, padctl->padctl_regs + offset);
494 }
495
496 static inline u32 padctl_readl(struct tegra_padctl_uphy *padctl,
497                                unsigned long offset)
498 {
499         return readl(padctl->padctl_regs + offset);
500 }
501 #else
502 #define padctl_writel(_padctl, _value, _offset)                         \
503 {                                                                       \
504         unsigned long v = _value, o = _offset;                          \
505         pr_info("%s padctl_write %s(@0x%lx) with 0x%lx\n", __func__,    \
506                 #_offset, o, v);                                        \
507         writel(v, _padctl->padctl_regs + o);                                    \
508 }
509
510 #define padctl_readl(_padctl, _offset)                                  \
511 ({                                                                      \
512         unsigned long v, o = _offset;                                   \
513         v = readl(_padctl->padctl_regs + o);                            \
514         pr_info("%s padctl_read %s(@0x%lx) = 0x%lx\n", __func__,        \
515                 #_offset, o, v);                                        \
516         v;                                                              \
517 })
518 #endif
519
520 #if 0
521 static inline void uphy_pll_writel(struct tegra_padctl_uphy *ctx, int pll,
522                                    u32 value, unsigned long offset)
523 {
524         writel(value, ctx->uphy_pll_regs[pll] + offset);
525 }
526
527 static inline u32 uphy_pll_readl(struct tegra_padctl_uphy *ctx, int pll,
528                                  unsigned long offset)
529 {
530         return readl(ctx->uphy_pll_regs[pll] + offset);
531 }
532 #else
533 #define uphy_pll_writel(_ctx, _pll, _value, _offset)                    \
534 {                                                                       \
535         unsigned long v = _value, o = _offset;                          \
536         pr_info("%s uphy_pll_writel pll %d %s(@0x%lx) with 0x%lx\n", __func__, \
537                 _pll, #_offset, o, v);                                  \
538         writel(v, _ctx->uphy_pll_regs[_pll] + o);                       \
539 }
540
541 #define uphy_pll_readl(_ctx, _pll, _offset)                             \
542 ({                                                                      \
543         unsigned long v, o = _offset;                                   \
544         v = readl(_ctx->uphy_pll_regs[_pll] + o);                       \
545         pr_info("%s uphy_pll_readl pll %d %s(@0x%lx) = 0x%lx\n", __func__, \
546                 _pll, #_offset, o, v);                                  \
547         v;                                                              \
548 })
549 #endif
550
551 #if 0
552 static inline void uphy_lane_writel(struct tegra_padctl_uphy *ctx, int lane,
553                                    u32 value, unsigned long offset)
554 {
555         writel(value, ctx->uphy_lane_regs[lane] + offset);
556 }
557
558 static inline u32 uphy_pll_readl(struct tegra_padctl_uphy *ctx, int lane,
559                                  unsigned long offset)
560 {
561         return readl(ctx->uphy_lane_regs[lane] + offset);
562 }
563 #else
564 #define uphy_lane_writel(_ctx, _lane, _value, _offset)                  \
565 {                                                                       \
566         unsigned long v = _value, o = _offset;                          \
567         pr_info("%s uphy_lane_writel lane %d %s(@0x%lx) with 0x%lx\n", __func__, \
568                 _lane, #_offset, o, v);                                 \
569         writel(v, _ctx->uphy_lane_regs[_lane] + o);                     \
570 }
571
572 #define uphy_lane_readl(_ctx, _lane, _offset)                           \
573 ({                                                                      \
574         unsigned long v, o = _offset;                                   \
575         v = readl(_ctx->uphy_lane_regs[_lane] + o);                     \
576         pr_info("%s uphy_lane_readl lane %d %s(@0x%lx) = 0x%lx\n", __func__, \
577                 _lane, #_offset, o, v);                                 \
578         v;                                                              \
579 })
580 #endif
581
582 struct tegra_mphy_sata_calib {
583         u8 aux_rx_idle_th;
584         u8 tx_drv_amp_sel0;
585         u8 tx_drv_amp_sel1;
586         u8 tx_drv_amp_sel2;
587         u8 tx_drv_amp_sel3;
588         u8 tx_drv_amp_sel4;
589         u8 tx_drv_amp_sel5;
590         u8 tx_drv_amp_sel6;
591         u8 tx_drv_amp_sel7;
592         u8 tx_drv_amp_sel8;
593         u8 tx_drv_amp_sel9;
594         u8 tx_drv_post_sel0;
595         u8 tx_drv_post_sel1;
596         u8 tx_drv_post_sel2;
597         u8 tx_drv_post_sel3;
598         u8 tx_drv_pre_sel3;
599         u8 ae_ctle_ctrl_id0;
600         u8 ae_ctle_ctrl_id1;
601 };
602
603 static struct tegra_mphy_sata_calib mphy_data[] = {
604         {
605                 .aux_rx_idle_th = 0x0,
606                 .tx_drv_amp_sel0 = 0x8,
607                 .tx_drv_amp_sel1 = 0x11,
608                 .tx_drv_amp_sel2 = 0x8,
609                 .tx_drv_amp_sel3 = 0x11,
610                 .tx_drv_amp_sel4 = 0x8,
611                 .tx_drv_amp_sel5 = 0x11,
612                 .tx_drv_amp_sel6 = 0x8,
613                 .tx_drv_amp_sel7 = 0x11,
614                 .tx_drv_amp_sel8 = 0x8,
615                 .tx_drv_amp_sel9 = 0x11,
616                 .tx_drv_post_sel0 = 0x0,
617                 .tx_drv_post_sel1 = 0xa,
618                 .tx_drv_post_sel2 = 0xf,
619                 .tx_drv_post_sel3 = 0x8,
620                 .tx_drv_pre_sel3 = 0x8,
621         },
622         {
623                 .aux_rx_idle_th = 0x1,
624                 .tx_drv_amp_sel0 = 0x8,
625                 .tx_drv_amp_sel1 = 0x11,
626                 .tx_drv_amp_sel2 = 0x8,
627                 .tx_drv_amp_sel3 = 0x11,
628                 .tx_drv_amp_sel4 = 0x8,
629                 .tx_drv_amp_sel5 = 0x11,
630                 .tx_drv_amp_sel6 = 0x8,
631                 .tx_drv_amp_sel7 = 0x11,
632                 .tx_drv_amp_sel8 = 0x8,
633                 .tx_drv_amp_sel9 = 0x11,
634                 .tx_drv_post_sel0 = 0x0,
635                 .tx_drv_post_sel1 = 0xa,
636                 .tx_drv_post_sel2 = 0xf,
637                 .tx_drv_post_sel3 = 0x8,
638                 .tx_drv_pre_sel3 = 0x8,
639         },
640         {
641                 .aux_rx_idle_th = 0x2,
642                 .tx_drv_amp_sel0 = 0x8,
643                 .tx_drv_amp_sel1 = 0x11,
644                 .tx_drv_amp_sel2 = 0x8,
645                 .tx_drv_amp_sel3 = 0x11,
646                 .tx_drv_amp_sel4 = 0x8,
647                 .tx_drv_amp_sel5 = 0x11,
648                 .tx_drv_amp_sel6 = 0x8,
649                 .tx_drv_amp_sel7 = 0x11,
650                 .tx_drv_amp_sel8 = 0x8,
651                 .tx_drv_amp_sel9 = 0x11,
652                 .tx_drv_post_sel0 = 0x0,
653                 .tx_drv_post_sel1 = 0xa,
654                 .tx_drv_post_sel2 = 0xf,
655                 .tx_drv_post_sel3 = 0x8,
656                 .tx_drv_pre_sel3 = 0x8,
657         },
658         {
659                 .aux_rx_idle_th = 0x3,
660                 .tx_drv_amp_sel0 = 0x8,
661                 .tx_drv_amp_sel1 = 0x11,
662                 .tx_drv_amp_sel2 = 0x8,
663                 .tx_drv_amp_sel3 = 0x11,
664                 .tx_drv_amp_sel4 = 0x8,
665                 .tx_drv_amp_sel5 = 0x11,
666                 .tx_drv_amp_sel6 = 0x8,
667                 .tx_drv_amp_sel7 = 0x11,
668                 .tx_drv_amp_sel8 = 0x8,
669                 .tx_drv_amp_sel9 = 0x11,
670                 .tx_drv_post_sel0 = 0x0,
671                 .tx_drv_post_sel1 = 0xa,
672                 .tx_drv_post_sel2 = 0xf,
673                 .tx_drv_post_sel3 = 0x8,
674                 .tx_drv_pre_sel3 = 0x8,
675         },
676 };
677
678 static struct tegra_mphy_sata_calib sata_data[] = {
679         {
680                 .aux_rx_idle_th = 0x0,
681                 .tx_drv_amp_sel0 = 0x1b,
682                 .tx_drv_amp_sel1 = 0x1f,
683                 .tx_drv_post_sel0 = 0x7,
684                 .tx_drv_post_sel1 = 0xa,
685                 .ae_ctle_ctrl_id0 = 0xf,
686                 .ae_ctle_ctrl_id1 = 0x8f,
687         },
688         {
689                 .aux_rx_idle_th = 0x1,
690                 .tx_drv_amp_sel0 = 0x17,
691                 .tx_drv_amp_sel1 = 0x1b,
692                 .tx_drv_post_sel0 = 0x5,
693                 .tx_drv_post_sel1 = 0xa,
694                 .ae_ctle_ctrl_id0 = 0xf,
695                 .ae_ctle_ctrl_id1 = 0x4f,
696         },
697         {
698                 .aux_rx_idle_th = 0x2,
699                 .tx_drv_amp_sel0 = 0x13,
700                 .tx_drv_amp_sel1 = 0x17,
701                 .tx_drv_post_sel0 = 0x4,
702                 .tx_drv_post_sel1 = 0xa,
703                 .ae_ctle_ctrl_id0 = 0xf,
704                 .ae_ctle_ctrl_id1 = 0xf,
705         },
706         {
707                 .aux_rx_idle_th = 0x3,
708                 .tx_drv_amp_sel0 = 0x1f,
709                 .tx_drv_amp_sel1 = 0x23,
710                 .tx_drv_post_sel0 = 0xa,
711                 .tx_drv_post_sel1 = 0xe,
712                 .ae_ctle_ctrl_id0 = 0xf,
713                 .ae_ctle_ctrl_id1 = 0xcd,
714         },
715 };
716
717 struct init_data {
718         u8 cfg_addr;
719         u16 cfg_wdata;
720         bool cfg_wds;
721         bool cfg_rds;
722         bool cfg_rst;
723 };
724
725 static struct init_data usb3_pll_g1_init_data[] = {
726         {
727                 .cfg_addr = MGMT_CORECLK_CTRL_ID0,
728                 .cfg_wdata = CFG_TXCLKREF_EN(1) |
729                              CFG_TXCLKREF_SEL(DIVIDE_TX_BY_5),
730         },
731 };
732
733 static void pcie_usb3_pll_defaults(struct tegra_padctl_uphy *ctx)
734 {
735         u32 reg;
736         int i;
737
738         for (i = 0; i < ARRAY_SIZE(usb3_pll_g1_init_data); i++) {
739                 reg = CFG_ADDR(usb3_pll_g1_init_data[i].cfg_addr);
740                 reg |= CFG_WDATA(usb3_pll_g1_init_data[i].cfg_wdata);
741                 uphy_pll_writel(ctx, 0, reg, UPHY_PLL_CTL_4);
742         }
743 }
744
745 #define pcie_pll_init pcie_usb3_pll_defaults
746
747 static struct init_data sata_pll_g1_g2_g3_init_data[] = {
748         {
749                 .cfg_addr = MGMT_FREQ_CTRL_ID0,
750                 .cfg_wdata = CFG_FREQ_NDIV(30),
751         },
752         {
753                 .cfg_addr = MGMT_CORECLK_CTRL_ID0,
754                 .cfg_wdata = CFG_TXCLKREF_EN(1) |
755                              CFG_TXCLKREF_SEL(DIVIDE_TX_BY_10),
756         },
757 };
758
759 static void sata_pll_init(struct tegra_padctl_uphy *ctx)
760 {
761         u32 reg;
762         int i;
763
764         for (i = 0; i < ARRAY_SIZE(sata_pll_g1_g2_g3_init_data); i++) {
765                 reg = CFG_ADDR(sata_pll_g1_g2_g3_init_data[i].cfg_addr);
766                 reg |= CFG_WDATA(sata_pll_g1_g2_g3_init_data[i].cfg_wdata);
767                 uphy_pll_writel(ctx, 1, reg, UPHY_PLL_CTL_4);
768         }
769 }
770
771 static struct init_data ufs_pll_g1_g2_g3_A_B_init_data[] = {
772         {
773                 .cfg_addr = MGMT_FREQ_CTRL_ID0,
774                 .cfg_wdata = CFG_FREQ_NDIV(24) | CFG_FREQ_MDIV(1),
775         },
776         {
777                 .cfg_addr = MGMT_FREQ_CTRL_ID1,
778                 .cfg_wdata = CFG_FREQ_NDIV(28) | CFG_FREQ_MDIV(1),
779         },
780         {
781                 .cfg_addr = MGMT_CORECLK_CTRL_ID0,
782                 .cfg_wdata = CFG_TXCLKREF_EN(1) |
783                              CFG_TXCLKREF_SEL(DIVIDE_TX_BY_10),
784         },
785         {
786                 .cfg_addr = MGMT_CORECLK_CTRL_ID1,
787                 .cfg_wdata = CFG_TXCLKREF_EN(1) |
788                              CFG_TXCLKREF_SEL(DIVIDE_TX_BY_10),
789         },
790         {
791                 .cfg_addr = PLLC_CRSWRD_OVRD_ID1,
792                 .cfg_wdata = 0,
793         },
794         {
795                 .cfg_addr = MGMT_CYA_CTRL,
796                 .cfg_wdata = CFG_MGMT_CLK_SEL(1),
797         },
798 };
799 static void ufs_pll_init(struct tegra_padctl_uphy *ctx)
800 {
801         u32 reg;
802         int i;
803
804         for (i = 0; i < ARRAY_SIZE(ufs_pll_g1_g2_g3_A_B_init_data); i++) {
805                 reg = CFG_ADDR(ufs_pll_g1_g2_g3_A_B_init_data[i].cfg_addr);
806                 reg |= CFG_WDATA(ufs_pll_g1_g2_g3_A_B_init_data[i].cfg_wdata);
807                 uphy_pll_writel(ctx, 1, reg, UPHY_PLL_CTL_4);
808         }
809 }
810
811 static struct init_data ufs_pll_rateid_init_data[] = {
812         {
813                 .cfg_addr = MGMT_FREQ_CTRL_ID0,
814                 .cfg_wdata = CFG_FREQ_NDIV(0x18) | CFG_FREQ_MDIV(0x1),
815                 .cfg_wds = true,
816                 .cfg_rst = true,
817         },
818         {
819                 .cfg_addr = MGMT_FREQ_CTRL_ID1,
820                 .cfg_wdata = CFG_FREQ_NDIV(0x1c) | CFG_FREQ_MDIV(0x1),
821                 .cfg_wds = true,
822                 .cfg_rst = true,
823         },
824         {
825                 .cfg_addr = MGMT_REFCLK_CTRL,
826                 .cfg_wdata = 0x0,
827                 .cfg_wds = true,
828                 .cfg_rst = true,
829         },
830         {
831                 .cfg_addr = MGMT_CORECLK_CTRL_ID0,
832                 .cfg_wdata = CFG_XDIGCLK_SEL(0x7) | CFG_XDIGCLK_EN(0x1)
833                         | CFG_TXCLKREF_SEL(0x2) | CFG_TXCLKREF_EN(1),
834                 .cfg_wds = true,
835                 .cfg_rst = true,
836         },
837         {
838                 .cfg_addr = MGMT_TX_RATE_CTRL_ID1,
839                 .cfg_wdata = CFG_XDIGCLK_SEL(0x7) | CFG_XDIGCLK_EN(0x1)
840                         | CFG_TXCLKREF_SEL(0x2) | CFG_TXCLKREF_EN(1),
841                 .cfg_wds = true,
842                 .cfg_rst = true,
843         },
844         {
845                 .cfg_addr = PLLC_CRSWRD_OVRD_ID0,
846                 .cfg_wdata = 0x3e,
847                 .cfg_wds = true,
848                 .cfg_rst = true,
849         }
850 };
851
852 static void ufs_pll_rateid_init(struct tegra_padctl_uphy *ctx)
853 {
854         u32 reg;
855         int i;
856
857         for (i = 0; i < ARRAY_SIZE(ufs_pll_rateid_init_data); i++) {
858                 reg = CFG_ADDR(ufs_pll_rateid_init_data[i].cfg_addr);
859                 reg |= CFG_WDATA(ufs_pll_rateid_init_data[i].cfg_wdata);
860                 reg |= CFG_WDS(ufs_pll_rateid_init_data[i].cfg_wds);
861                 reg |= CFG_RDS(ufs_pll_rateid_init_data[i].cfg_rds);
862                 reg |= CFG_RESET(ufs_pll_rateid_init_data[i].cfg_rst);
863                 uphy_pll_writel(ctx, 1, reg, UPHY_PLL_CTL_4);
864         }
865 }
866
867 static struct init_data ufs_lane_rateid_init_data[] = {
868         {
869                 .cfg_addr = MGMT_TX_RATE_CTRL_ID0,
870                 .cfg_wdata = TX_RATE_SDIV(SDIV4),
871                 .cfg_wds = true,
872                 .cfg_rst = true,
873         },
874         {
875                 .cfg_addr = MGMT_TX_RATE_CTRL_ID1,
876                 .cfg_wdata = TX_RATE_SDIV(SDIV2),
877                 .cfg_wds = true,
878                 .cfg_rst = true,
879         },
880         {
881                 .cfg_addr = MGMT_TX_RATE_CTRL_ID2,
882                 .cfg_wdata = TX_RATE_SDIV(SDIV1),
883                 .cfg_wds = true,
884                 .cfg_rst = true,
885         },
886         {
887                 .cfg_addr = MGMT_TX_RATE_CTRL_ID3,
888                 .cfg_wdata = TX_RATE_SDIV(SDIV1),
889                 .cfg_wds = true,
890                 .cfg_rst = true,
891         },
892         {
893                 .cfg_addr = MGMT_RX_RATE_CTRL_ID0,
894                 .cfg_wdata = RX_RATE_SDIV(CDIV1),
895                 .cfg_wds = true,
896                 .cfg_rst = true,
897         },
898         {
899                 .cfg_addr = MGMT_RX_RATE_CTRL_ID1,
900                 .cfg_wdata = RX_RATE_SDIV(CDIV2),
901                 .cfg_wds = true,
902                 .cfg_rst = true,
903         },
904         {
905                 .cfg_addr = MGMT_RX_RATE_CTRL_ID2,
906                 .cfg_wdata = RX_RATE_SDIV(CDIV4),
907                 .cfg_wds = true,
908                 .cfg_rst = true,
909         },
910         {
911                 .cfg_addr = MGMT_RX_RATE_CTRL_ID0,
912                 .cfg_wdata = RX_RATE_SDIV(CDIV4),
913                 .cfg_wds = true,
914                 .cfg_rst = true,
915         },
916         {
917                 .cfg_addr = MGMT_TX_CTRL,
918                 .cfg_wdata = TX_TERM_MODE(0x1),
919                 .cfg_wds = true,
920                 .cfg_rst = true,
921         },
922         {
923                 .cfg_addr = AE_CDR_CTRL_ID0,
924                 .cfg_wdata = FRLOOP_EN(0x1) | FRGAIN(0x3) | PHGAIN(0x7),
925                 .cfg_wds = true,
926                 .cfg_rst = true,
927         },
928         {
929                 .cfg_addr = AE_CDR_CTRL_ID1,
930                 .cfg_wdata = FRLOOP_EN(0x1) | FRGAIN(0x6) | PHGAIN(0x7),
931                 .cfg_wds = true,
932                 .cfg_rst = true,
933         },
934         {
935                 .cfg_addr = AE_CDR_CTRL_ID2,
936                 .cfg_wdata = FRLOOP_EN(0x1) | FRGAIN(0xc) | PHGAIN(0x7),
937                 .cfg_wds = true,
938                 .cfg_rst = true,
939         },
940         {
941                 .cfg_addr = AE_CDR_CTRL_ID3,
942                 .cfg_wdata = FRLOOP_EN(0x1) | FRGAIN(0xc) | PHGAIN(0x7),
943                 .cfg_wds = true,
944                 .cfg_rst = true,
945         },
946         {
947                 .cfg_addr = AE_CTLE_CTRL_ID0,
948                 .cfg_wdata = HF_UBIN(0x0) | LF_UGRAY(0xf),
949                 .cfg_wds = true,
950                 .cfg_rst = true,
951         },
952         {
953                 .cfg_addr = AE_CTLE_CTRL_ID1,
954                 .cfg_wdata = HF_UBIN(0x0) | LF_UGRAY(0xf),
955                 .cfg_wds = true,
956                 .cfg_rst = true,
957         },
958         {
959                 .cfg_addr = AE_CTLE_CTRL_ID2,
960                 .cfg_wdata = HF_UBIN(0x8) | LF_UGRAY(0xf),
961                 .cfg_wds = true,
962                 .cfg_rst = true,
963         },
964         {
965                 .cfg_addr = AE_CTLE_CTRL_ID3,
966                 .cfg_wdata = HF_UBIN(0xf) | LF_UGRAY(0xd),
967                 .cfg_wds = true,
968                 .cfg_rst = true,
969         },
970         {
971                 .cfg_addr = AE_EQ1_CTRL_ID0,
972                 .cfg_wdata = CTLE_HF_MODE(0x1),
973                 .cfg_wds = true,
974                 .cfg_rst = true,
975         },
976         {
977                 .cfg_addr = AE_EQ1_CTRL_ID1,
978                 .cfg_wdata = CTLE_HF_MODE(0x1),
979                 .cfg_wds = true,
980                 .cfg_rst = true,
981         },
982         {
983                 .cfg_addr = AE_EQ1_CTRL_ID2,
984                 .cfg_wdata = CTLE_HF_MODE(0x1),
985                 .cfg_wds = true,
986                 .cfg_rst = true,
987         },
988         {
989                 .cfg_addr = AE_EQ1_CTRL_ID3,
990                 .cfg_wdata = CTLE_HF_MODE(0x0),
991                 .cfg_wds = true,
992                 .cfg_rst = true,
993         },
994         {
995                 .cfg_addr = AE_EQ2_CTRL_ID0,
996                 .cfg_wdata = H0_MODE(0x1),
997                 .cfg_wds = true,
998                 .cfg_rst = true,
999         },
1000         {
1001                 .cfg_addr = AE_EQ2_CTRL_ID0,
1002                 .cfg_wdata = H0_MODE(0x1),
1003                 .cfg_wds = true,
1004                 .cfg_rst = true,
1005         },
1006         {
1007                 .cfg_addr = AE_EQ2_CTRL_ID0,
1008                 .cfg_wdata = H0_MODE(0x1),
1009                 .cfg_wds = true,
1010                 .cfg_rst = true,
1011         },
1012         {
1013                 .cfg_addr = AE_EQ2_CTRL_ID0,
1014                 .cfg_wdata = H0_MODE(0x0),
1015                 .cfg_wds = true,
1016                 .cfg_rst = true,
1017         },
1018 };
1019
1020 static void ufs_lane_rateid_init(struct tegra_padctl_uphy *ctx, int lane)
1021 {
1022         u32 reg;
1023         int i;
1024
1025         for (i = 0; i < ARRAY_SIZE(ufs_lane_rateid_init_data); i++) {
1026                 reg = CFG_ADDR(ufs_lane_rateid_init_data[i].cfg_addr);
1027                 reg |= CFG_WDATA(ufs_lane_rateid_init_data[i].cfg_wdata);
1028                 reg |= CFG_WDS(ufs_lane_rateid_init_data[i].cfg_wds);
1029                 reg |= CFG_RDS(ufs_lane_rateid_init_data[i].cfg_rds);
1030                 reg |= CFG_RESET(ufs_lane_rateid_init_data[i].cfg_rst);
1031                 uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
1032         }
1033 }
1034
1035
1036 static struct init_data pcie_lane_g1_g2_init_data[] = {
1037 };
1038
1039 static void pcie_lane_defaults(struct tegra_padctl_uphy *ctx, int lane)
1040 {
1041         u32 reg;
1042         int i;
1043
1044         for (i = 0; i < ARRAY_SIZE(pcie_lane_g1_g2_init_data); i++) {
1045                 reg = CFG_ADDR(pcie_lane_g1_g2_init_data[i].cfg_addr);
1046                 reg |= CFG_WDATA(pcie_lane_g1_g2_init_data[i].cfg_wdata);
1047                 uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
1048         }
1049 }
1050
1051 static struct init_data usb3_lane_g1_init_data[] = {
1052         {
1053                 .cfg_addr = MGMT_TX_RATE_CTRL_ID0,
1054                 .cfg_wdata = TX_RATE_SDIV(SDIV1),
1055         },
1056         {
1057                 .cfg_addr = MGMT_RX_RATE_CTRL_ID0,
1058                 .cfg_wdata = RX_RATE_SDIV(SDIV1) | RX_RATE_CDIV(CDIV4),
1059         },
1060         {
1061                 .cfg_addr = MGMT_TX_CTRL,
1062                 .cfg_wdata = TX_TERM_MODE(0),
1063         },
1064         {
1065                 .cfg_addr = AE_CTLE_CTRL_ID0,
1066                 .cfg_wdata = LF_UGRAY(0xd) | HF_UBIN(0xf),
1067         },
1068         {
1069                 .cfg_addr = AE_DFE0_CTRL_ID0,
1070                 .cfg_wdata = LF_UGRAY(0xd) | HF_UBIN(0xf),
1071         },
1072         {
1073                 .cfg_addr = AE_DFE1_CTRL_ID0,
1074                 .cfg_wdata = H0_SBIN(48) | H4_SBIN(-1),
1075         },
1076         {
1077                 .cfg_addr = AE_CDR_CTRL_ID0,
1078                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0xc) | FRLOOP_EN(1),
1079         },
1080         {
1081                 .cfg_addr = AE_EQ0_CTRL_ID0,
1082                 .cfg_wdata = EQ0_SEQ_MODE(SEQ_MODE_HF_Z_H0_HN_Z) |
1083                              H0INIT_ITERS(0x3),
1084         },
1085         {
1086                 .cfg_addr = AE_EQ1_CTRL_ID0,
1087                 .cfg_wdata = CTLE_HF_MODE(VAR_MODE_AUTO) |
1088                              CTLE_HF_GRAD(CTLE_HF_GRAD_1P5) |
1089                              H1_MODE(VAR_MODE_AUTO) |
1090                              H2_MODE(VAR_MODE_AUTO) |
1091                              H3_MODE(VAR_MODE_AUTO) |
1092                              H4_MODE(VAR_MODE_AUTO),
1093         },
1094         {
1095                 .cfg_addr = AE_EQ2_CTRL_ID0,
1096                 .cfg_wdata = H1_MODE(VAR_MODE_AUTO) | H0_DAC_TIME(0x2) |
1097                              H0_TIME(0x6) | H0_ITERS(0x3) | HN_ITERS(0x1),
1098         },
1099         {
1100                 .cfg_addr = AE_EQ3_CTRL_ID0,
1101                 .cfg_wdata = HN_GAIN(0Xf) | CTLE_HF_TIME(0xc) |
1102                              CTLE_HF_GAIN(0xf),
1103         },
1104 };
1105
1106 static void usb3_lane_defaults(struct tegra_padctl_uphy *ctx, int lane)
1107 {
1108         u32 reg;
1109         int i;
1110
1111         for (i = 0; i < ARRAY_SIZE(usb3_lane_g1_init_data); i++) {
1112                 reg = CFG_ADDR(usb3_lane_g1_init_data[i].cfg_addr);
1113                 reg |= CFG_WDATA(usb3_lane_g1_init_data[i].cfg_wdata);
1114                 uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
1115         }
1116
1117 }
1118
1119 static struct init_data sata_lane_g1_g2_init_data[] = {
1120         {
1121                 .cfg_addr = MGMT_TX_RATE_CTRL_ID0,
1122                 .cfg_wdata = TX_RATE_SDIV(SDIV4),
1123         },
1124         {
1125                 .cfg_addr = MGMT_TX_RATE_CTRL_ID1,
1126                 .cfg_wdata = TX_RATE_SDIV(SDIV2),
1127         },
1128         {
1129                 .cfg_addr = MGMT_RX_RATE_CTRL_ID0,
1130                 .cfg_wdata = RX_RATE_SDIV(SDIV4) | RX_RATE_CDIV(CDIV1),
1131         },
1132         {
1133                 .cfg_addr = MGMT_RX_RATE_CTRL_ID1,
1134                 .cfg_wdata = RX_RATE_SDIV(SDIV2) | RX_RATE_CDIV(CDIV2),
1135         },
1136         {
1137                 .cfg_addr = MGMT_TX_CTRL,
1138                 .cfg_wdata = TX_TERM_MODE(0),
1139         },
1140         {
1141                 .cfg_addr = AE_CTLE_CTRL_ID0,
1142                 .cfg_wdata = LF_UGRAY(0x8),
1143         },
1144         {
1145                 .cfg_addr = AE_CDR_CTRL_ID0,
1146                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0x3) | FRLOOP_EN(1),
1147         },
1148         {
1149                 .cfg_addr = AE_CDR_CTRL_ID1,
1150                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0x6) | FRLOOP_EN(1),
1151         },
1152 };
1153
1154 static void sata_lane_init(struct tegra_padctl_uphy *ctx, int lane)
1155 {
1156         u32 reg;
1157         int i;
1158
1159         for (i = 0; i < ARRAY_SIZE(sata_lane_g1_g2_init_data); i++) {
1160                 reg = CFG_ADDR(sata_lane_g1_g2_init_data[i].cfg_addr);
1161                 reg |= CFG_WDATA(sata_lane_g1_g2_init_data[i].cfg_wdata);
1162                 uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
1163         }
1164
1165 }
1166
1167 static struct init_data ufs_lane_g1_g2_g3_init_data[] = {
1168         {
1169                 .cfg_addr = MGMT_TX_RATE_CTRL_ID0,
1170                 .cfg_wdata = TX_RATE_SDIV(SDIV4),
1171         },
1172         {
1173                 .cfg_addr = MGMT_TX_RATE_CTRL_ID1,
1174                 .cfg_wdata = TX_RATE_SDIV(SDIV2),
1175         },
1176         {
1177                 .cfg_addr = MGMT_RX_RATE_CTRL_ID0,
1178                 .cfg_wdata = RX_RATE_SDIV(SDIV4) | RX_RATE_CDIV(CDIV1),
1179         },
1180         {
1181                 .cfg_addr = MGMT_RX_RATE_CTRL_ID1,
1182                 .cfg_wdata = RX_RATE_SDIV(SDIV2) | RX_RATE_CDIV(CDIV2),
1183         },
1184         {
1185                 .cfg_addr = MGMT_TX_CTRL,
1186                 .cfg_wdata = TX_TERM_MODE(1),
1187         },
1188         {
1189                 .cfg_addr = AE_CTLE_CTRL_ID0,
1190                 .cfg_wdata = LF_UGRAY(0xf),
1191         },
1192         {
1193                 .cfg_addr = AE_CTLE_CTRL_ID1,
1194                 .cfg_wdata = LF_UGRAY(0xf),
1195         },
1196         {
1197                 .cfg_addr = AE_CTLE_CTRL_ID2,
1198                 .cfg_wdata = LF_UGRAY(0xf) | HF_UBIN(8),
1199         },
1200
1201         {
1202                 .cfg_addr = AE_DFE0_CTRL_ID2,
1203                 .cfg_wdata = 0,
1204         },
1205         {
1206                 .cfg_addr = AE_DFE1_CTRL_ID2,
1207                 .cfg_wdata = 0,
1208         },
1209         {
1210                 .cfg_addr = AE_CDR_CTRL_ID0,
1211                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0x3) | FRLOOP_EN(1),
1212         },
1213         {
1214                 .cfg_addr = AE_CDR_CTRL_ID1,
1215                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0x6) | FRLOOP_EN(1),
1216         },
1217         {
1218                 .cfg_addr = AE_CDR_CTRL_ID2,
1219                 .cfg_wdata = PHGAIN(0x7) | FRGAIN(0xc) | FRLOOP_EN(1),
1220         },
1221         {
1222                 .cfg_addr = AE_EQ0_CTRL_ID2,
1223                 .cfg_wdata = EQ0_SEQ_MODE(SEQ_MODE_HF_Z_H0_HN_Z),
1224         },
1225         {
1226                 .cfg_addr = AE_EQ1_CTRL_ID2,
1227                 .cfg_wdata = CTLE_HF_MODE(VAR_MODE_OFF) |
1228                              CTLE_HF_GRAD(CTLE_HF_GRAD_1P5) |
1229                              H1_MODE(VAR_MODE_OFF) |
1230                              H2_MODE(VAR_MODE_OFF) |
1231                              H3_MODE(VAR_MODE_OFF) |
1232                              H4_MODE(VAR_MODE_OFF),
1233         },
1234         {
1235                 .cfg_addr = AE_EQ2_CTRL_ID2,
1236                 .cfg_wdata = H1_MODE(VAR_MODE_OFF),
1237         },
1238         {
1239                 .cfg_addr = AE_EQ3_CTRL_ID2,
1240                 .cfg_wdata = 0,
1241         },
1242         {
1243                 .cfg_addr = MGMT_RX_PI_CTRL_ID2,
1244                 .cfg_wdata = 0,
1245         },
1246 };
1247
1248 static void ufs_lane_init(struct tegra_padctl_uphy *ctx, int lane)
1249 {
1250         u32 reg;
1251         int i;
1252
1253         for (i = 0; i < ARRAY_SIZE(ufs_lane_g1_g2_g3_init_data); i++) {
1254                 reg = CFG_ADDR(ufs_lane_g1_g2_g3_init_data[i].cfg_addr);
1255                 reg |= CFG_WDATA(ufs_lane_g1_g2_g3_init_data[i].cfg_wdata);
1256                 uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
1257         }
1258
1259 }
1260
1261 static int __uphy_pll_init(struct tegra_padctl_uphy *ctx, int pll)
1262 {
1263         struct device *dev = ctx->dev;
1264         u32 reg;
1265         int i;
1266
1267         /* FIXME: Need to see needed ??? */
1268         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1269         reg |= PWR_OVRD;
1270         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1271
1272         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1273         reg |= (CAL_OVRD | RCAL_OVRD | CAL_RESET);
1274         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1275
1276         /* power up PLL */
1277         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1278         reg &= ~PLL_IDDQ;
1279         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1280
1281         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1282         reg &= ~PLL_SLEEP(~0);
1283         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1284
1285         ndelay(100);
1286
1287         /* perform PLL calibration */
1288         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1289         reg |= CAL_EN;
1290         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1291         for (i = 0; i < 20; i++) {
1292                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1293                 if (reg & CAL_DONE)
1294                         break;
1295                 usleep_range(10, 15);
1296         }
1297         if (!(reg & CAL_DONE)) {
1298                 dev_err(dev, "start PLL %d calibration timeout\n", pll);
1299                 return -ETIMEDOUT;
1300         }
1301
1302         /* stop PLL calibration */
1303         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1304         reg &= ~CAL_EN;
1305         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1306         for (i = 0; i < 20; i++) {
1307                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1308                 if (!(reg & CAL_DONE))
1309                         break;
1310                 usleep_range(10, 15);
1311         }
1312         if (reg & CAL_DONE) {
1313                 dev_err(dev, "stop PLL %d calibration timeout\n", pll);
1314                 return -ETIMEDOUT;
1315         }
1316
1317         if (pll == 1) {
1318                 /* perform PLL rate change for UPHY_PLL_1, used by UFS */
1319                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1320                 reg &= ~RATE_ID(~0);
1321                 reg |= (RATE_ID(1) | RATE_ID_OVRD);
1322                 uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1323
1324                 ndelay(100);
1325
1326                 /* perform PLL calibration for rate B */
1327                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1328                 reg |= CAL_EN;
1329                 uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1330                 for (i = 0; i < 20; i++) {
1331                         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1332                         if (reg & CAL_DONE)
1333                                 break;
1334                         usleep_range(10, 15);
1335                 }
1336                 if (!(reg & CAL_DONE)) {
1337                         dev_err(dev, "start PLL %d calibration timeout\n", pll);
1338                         return -ETIMEDOUT;
1339                 }
1340
1341                 /* stop PLL calibration */
1342                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1343                 reg &= ~CAL_EN;
1344                 uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1345                 for (i = 0; i < 20; i++) {
1346                         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1347                         if (!(reg & CAL_DONE))
1348                                 break;
1349                         usleep_range(10, 15);
1350                 }
1351                 if (reg & CAL_DONE) {
1352                         dev_err(dev, "stop PLL %d calibration timeout\n", pll);
1353                         return -ETIMEDOUT;
1354                 }
1355
1356                 reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1357                 reg &= ~RATE_ID(~0);
1358                 reg |= RATE_ID_OVRD;
1359                 uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1360                 ndelay(100);
1361         }
1362
1363         /* enable PLL and wait for lock */
1364         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1365         reg |= PLL_ENABLE;
1366         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1367         usleep_range(20, 25);
1368         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1369         if (!(reg & LOCKDET_STATUS)) {
1370                 dev_err(dev, "enable PLL %d timeout\n", pll);
1371                 return -ETIMEDOUT;
1372         }
1373
1374         /* perform resistor calibration */
1375         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1376         reg |= RCAL_EN;
1377         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1378         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1379         reg |= RCAL_CLK_EN;
1380         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1381         usleep_range(5, 10);
1382         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1383         if (!(reg & RCAL_DONE)) {
1384                 dev_err(dev, "start resistor %d calibration timeout\n", pll);
1385                 return -ETIMEDOUT;
1386         }
1387
1388         /* stop resistor calibration */
1389         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1390         reg &= ~RCAL_EN;
1391         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1392         usleep_range(5, 10);
1393         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1394         if (reg & RCAL_DONE) {
1395                 dev_err(dev, "stop resistor %d calibration timeout\n", pll);
1396                 return -ETIMEDOUT;
1397         }
1398         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1399         reg &= ~RCAL_CLK_EN;
1400         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1401
1402         /* remove SW overrides to allow HW sequencer to run */
1403         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_1);
1404         reg &= ~PWR_OVRD;
1405         if (pll == 1)
1406                 reg &= ~RATE_ID_OVRD;
1407         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_1);
1408         reg = uphy_pll_readl(ctx, pll, UPHY_PLL_CTL_2);
1409         reg &= ~(CAL_OVRD | RCAL_OVRD);
1410         uphy_pll_writel(ctx, pll, reg, UPHY_PLL_CTL_2);
1411
1412         return 0;
1413 }
1414
1415 static int uphy_pll_init(struct tegra_padctl_uphy *ctx, int pll)
1416 {
1417         struct device *dev = ctx->dev;
1418         char clk_name[] = "uphy_pllX";
1419         int rc = 0;
1420
1421         if (pll < 0 || pll > 1)
1422                 return -EINVAL;
1423
1424         if (!ctx->uphy_pll_clk[pll]) {
1425                 snprintf(clk_name, sizeof(clk_name), "uphy_pll%d", pll);
1426                 ctx->uphy_pll_clk[pll] = clk_get_sys(NULL, clk_name);
1427
1428                 if (IS_ERR(ctx->uphy_pll_clk[pll])) {
1429                         rc = PTR_ERR(ctx->uphy_pll_clk[pll]);
1430                         dev_err(dev, "fail get uphy_pll%d clk %d\n", pll, rc);
1431                         ctx->uphy_pll_clk[pll] = NULL;
1432                         return rc;
1433                 }
1434         }
1435
1436         /* FIXME: lock */
1437         TRACE_DEV(dev, "clients %d", ctx->uphy_pll_clients[pll]);
1438         if (ctx->uphy_pll_clients[pll]++ > 0)
1439                 goto done;
1440
1441         tegra_periph_reset_deassert(ctx->uphy_pll_clk[pll]);
1442         rc = __uphy_pll_init(ctx, pll);
1443
1444 done:
1445         return rc;
1446 }
1447
1448 static int uphy_pll_deinit(struct tegra_padctl_uphy *ctx, int pll)
1449 {
1450         struct device *dev = ctx->dev;
1451
1452         TRACE_DEV(dev, "clients %d", ctx->uphy_pll_clients[pll]);
1453         ctx->uphy_pll_clients[pll]--;
1454
1455         TRACE("FIXME: implement!\n"); /* TODO */
1456         return 0;
1457 }
1458
1459 static int uphy_lane_init(struct tegra_padctl_uphy *ctx, int lane)
1460 {
1461         struct device *dev = ctx->dev;
1462         char clk_name[] = "uphy_laneX";
1463         int rc;
1464
1465         if (lane < 0 || lane > 5)
1466                 return -EINVAL;
1467
1468         if (!ctx->uphy_lane_clk[lane]) {
1469                 snprintf(clk_name, sizeof(clk_name), "uphy_lane%d", lane);
1470                 ctx->uphy_lane_clk[lane] = clk_get_sys(NULL, clk_name);
1471
1472                 if (IS_ERR(ctx->uphy_lane_clk[lane])) {
1473                         rc = PTR_ERR(ctx->uphy_lane_clk[lane]);
1474                         dev_err(dev, "fail get uphy_lane%d clk %d\n", lane, rc);
1475                         ctx->uphy_lane_clk[lane] = NULL;
1476                         return rc;
1477                 }
1478         }
1479
1480         tegra_periph_reset_deassert(ctx->uphy_lane_clk[lane]);
1481
1482         return 0;
1483 }
1484
1485 static int uphy_lane_deinit(struct tegra_padctl_uphy *ctx, int lane)
1486 {
1487         if (lane < 0 || lane > 5)
1488                 return -EINVAL;
1489
1490         if (ctx->uphy_lane_clk[lane])
1491                 tegra_periph_reset_assert(ctx->uphy_lane_clk[lane]);
1492
1493         return 0;
1494 }
1495
1496
1497 #define PIN_OTG_0       0
1498 #define PIN_OTG_1       1
1499 #define PIN_OTG_2       2
1500 #define PIN_OTG_3       3
1501 #define PIN_HSIC_0      4
1502 #define PIN_HSIC_1      5
1503 #define PIN_UPHY_0      6
1504 #define PIN_UPHY_1      7
1505 #define PIN_UPHY_2      8
1506 #define PIN_UPHY_3      9
1507 #define PIN_UPHY_4      10
1508 #define PIN_UPHY_5      11
1509 #define PIN_UPHY_6      12
1510 #define PIN_SATA_0      13
1511
1512 static inline bool lane_is_otg(unsigned int lane)
1513 {
1514         return lane >= PIN_OTG_0 && lane <= PIN_OTG_3;
1515 }
1516
1517 static inline bool lane_is_hsic(unsigned int lane)
1518 {
1519         return lane >= PIN_HSIC_0 && lane <= PIN_HSIC_1;
1520 }
1521
1522 static inline bool lane_is_uphy(unsigned int lane)
1523 {
1524         return lane >= PIN_UPHY_0 && lane <= PIN_UPHY_6;
1525 }
1526
1527 static inline bool lane_is_pcie_or_sata(unsigned int lane)
1528 {
1529         return lane >= PIN_UPHY_0 && lane <= PIN_SATA_0;
1530 }
1531
1532 static int lane_to_usb3_port(struct tegra_padctl_uphy *padctl,
1533                              unsigned int lane)
1534 {
1535         unsigned int i;
1536
1537         for (i = 0; i < TEGRA_XUSB_USB3_PHYS; i++) {
1538                 if (padctl->usb3_ports[i].lane == lane)
1539                         return i;
1540         }
1541
1542         return -EINVAL;
1543 }
1544
1545 static int tegra_xusb_padctl_get_groups_count(struct pinctrl_dev *pinctrl)
1546 {
1547         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1548
1549         TRACE("num_pins %u", padctl->soc->num_pins);
1550         return padctl->soc->num_pins;
1551 }
1552
1553 static const char *tegra_xusb_padctl_get_group_name(struct pinctrl_dev *pinctrl,
1554                                                     unsigned int group)
1555 {
1556         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1557
1558         TRACE("group %u name %s", group, padctl->soc->pins[group].name);
1559         return padctl->soc->pins[group].name;
1560 }
1561
1562 static int tegra_xusb_padctl_get_group_pins(struct pinctrl_dev *pinctrl,
1563                                  unsigned group,
1564                                  const unsigned **pins,
1565                                  unsigned *num_pins)
1566 {
1567         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1568
1569         *pins = &padctl->soc->pins[group].number;
1570         *num_pins = 1; /* one pin per group */
1571
1572         TRACE("group %u num_pins %u pins[0] %u", group, *num_pins, *pins[0]);
1573
1574         return 0;
1575 }
1576
1577 enum tegra_xusb_padctl_param {
1578         TEGRA_XUSB_PADCTL_USB3_PORT,
1579         TEGRA_XUSB_PADCTL_USB2_PORT,
1580 };
1581
1582 static const struct tegra_xusb_padctl_property {
1583         const char *name;
1584         enum tegra_xusb_padctl_param param;
1585 } properties[] = {
1586         {"nvidia,usb3-port", TEGRA_XUSB_PADCTL_USB3_PORT},
1587         {"nvidia,usb2-port", TEGRA_XUSB_PADCTL_USB2_PORT},
1588 };
1589
1590 #define TEGRA_XUSB_PADCTL_PACK(param, value) ((param) << 16 | (value))
1591 #define TEGRA_XUSB_PADCTL_UNPACK_PARAM(config) ((config) >> 16)
1592 #define TEGRA_XUSB_PADCTL_UNPACK_VALUE(config) ((config) & 0xffff)
1593
1594 static int tegra186_padctl_uphy_parse_subnode(struct tegra_padctl_uphy *padctl,
1595                                            struct device_node *np,
1596                                            struct pinctrl_map **maps,
1597                                            unsigned int *reserved_maps,
1598                                            unsigned int *num_maps)
1599 {
1600         unsigned int i, reserve = 0, num_configs = 0;
1601         unsigned long config, *configs = NULL;
1602         const char *function, *group;
1603         struct property *prop;
1604         int err = 0;
1605         u32 value;
1606
1607         TRACE();
1608         err = of_property_read_string(np, "nvidia,function", &function);
1609         TRACE("err %d function %s", err, function ? function : "(null)");
1610         if (err < 0) {
1611                 if (err != -EINVAL)
1612                         return err;
1613
1614                 function = NULL;
1615         }
1616
1617         for (i = 0; i < ARRAY_SIZE(properties); i++) {
1618                 err = of_property_read_u32(np, properties[i].name, &value);
1619                 TRACE("err %d property %s (%d) value %d",
1620                         err, properties[i].name, i, value);
1621                 if (err < 0) {
1622                         if (err == -EINVAL)
1623                                 continue;
1624
1625                         return err;
1626                 }
1627
1628                 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, value);
1629
1630                 err = pinctrl_utils_add_config(padctl->pinctrl, &configs,
1631                                                &num_configs, config);
1632                 TRACE("err %d num_configs %d", err, num_configs);
1633                 if (err < 0)
1634                         return err;
1635         }
1636
1637         if (function)
1638                 reserve++;
1639
1640         if (num_configs)
1641                 reserve++;
1642
1643         err = of_property_count_strings(np, "nvidia,lanes");
1644         TRACE("of_property_count_strings(\"nvidia,lanes\") %d", err);
1645         if (err < 0)
1646                 return err;
1647
1648         reserve *= err;
1649
1650         err = pinctrl_utils_reserve_map(padctl->pinctrl, maps, reserved_maps,
1651                                         num_maps, reserve);
1652         TRACE("pinctrl_utils_reserve_map reserve %d num_maps %d err %d",
1653                 reserve, *num_maps, err);
1654         if (err < 0)
1655                 return err;
1656
1657         of_property_for_each_string(np, "nvidia,lanes", prop, group) {
1658                 if (function) {
1659                         err = pinctrl_utils_add_map_mux(padctl->pinctrl, maps,
1660                                         reserved_maps, num_maps, group,
1661                                         function);
1662                         TRACE("pinctrl_utils_add_map_mux() err %d num_maps %u group %s function %s",
1663                                 err, *num_maps, group, function);
1664                         if (err < 0)
1665                                 return err;
1666                 }
1667
1668                 if (num_configs) {
1669                         err = pinctrl_utils_add_map_configs(padctl->pinctrl,
1670                                         maps, reserved_maps, num_maps, group,
1671                                         configs, num_configs,
1672                                         PIN_MAP_TYPE_CONFIGS_GROUP);
1673                         TRACE("pinctrl_utils_add_map_configs() err %d", err);
1674                         if (err < 0)
1675                                 return err;
1676                 }
1677         }
1678
1679         return 0;
1680 }
1681
1682 static int tegra_xusb_padctl_dt_node_to_map(struct pinctrl_dev *pinctrl,
1683                                             struct device_node *parent,
1684                                             struct pinctrl_map **maps,
1685                                             unsigned int *num_maps)
1686 {
1687         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1688         unsigned int reserved_maps = 0;
1689         struct device_node *np;
1690         int err;
1691
1692         *num_maps = 0;
1693         *maps = NULL;
1694
1695         pr_info("%s %d\n", __func__, __LINE__);
1696         for_each_child_of_node(parent, np) {
1697                 err = tegra186_padctl_uphy_parse_subnode(padctl, np, maps,
1698                                                       &reserved_maps,
1699                                                       num_maps);
1700                 if (err < 0) {
1701                         pr_info("%s %d err %d\n", __func__, __LINE__, err);
1702                         return err;
1703                 }
1704         }
1705
1706         return 0;
1707 }
1708
1709 static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
1710         .get_groups_count = tegra_xusb_padctl_get_groups_count,
1711         .get_group_name = tegra_xusb_padctl_get_group_name,
1712         .get_group_pins = tegra_xusb_padctl_get_group_pins,
1713         .dt_node_to_map = tegra_xusb_padctl_dt_node_to_map,
1714         .dt_free_map = pinctrl_utils_dt_free_map,
1715 };
1716
1717 static int tegra186_padctl_uphy_get_functions_count(struct pinctrl_dev *pinctrl)
1718 {
1719         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1720
1721         TRACE("num_functions %u", padctl->soc->num_functions);
1722         return padctl->soc->num_functions;
1723 }
1724
1725 static const char *
1726 tegra186_padctl_uphy_get_function_name(struct pinctrl_dev *pinctrl,
1727                                     unsigned int function)
1728 {
1729         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1730
1731         TRACE("function %u name %s", function, padctl->soc->functions[function].name);
1732         return padctl->soc->functions[function].name;
1733 }
1734
1735 static int tegra186_padctl_uphy_get_function_groups(struct pinctrl_dev *pinctrl,
1736                                                  unsigned int function,
1737                                                  const char * const **groups,
1738                                                  unsigned * const num_groups)
1739 {
1740         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1741
1742         *num_groups = padctl->soc->functions[function].num_groups;
1743         *groups = padctl->soc->functions[function].groups;
1744
1745         TRACE("function %u *num_groups %u groups %s",
1746                                 function, *num_groups, *groups[0]);
1747         return 0;
1748 }
1749
1750 static int tegra186_padctl_uphy_pinmux_enable(struct pinctrl_dev *pinctrl,
1751                                            unsigned int function,
1752                                            unsigned int group)
1753 {
1754         struct tegra_padctl_uphy *ctx = pinctrl_dev_get_drvdata(pinctrl);
1755         const struct tegra_padctl_uphy_lane *lane;
1756         unsigned int i;
1757         u32 value;
1758
1759         lane = &ctx->soc->lanes[group];
1760
1761         TRACE_DEV(pinctrl->dev, "group %u (%s) function %u num_funcs %d",
1762                         group, lane->name, function, lane->num_funcs);
1763
1764         for (i = 0; i < lane->num_funcs; i++) {
1765                 TRACE_DEV(pinctrl->dev, "funcs[%d] %d", i, lane->funcs[i]);
1766                 if (lane->funcs[i] == function)
1767                         break;
1768         }
1769
1770         if (i >= lane->num_funcs)
1771                 return -EINVAL;
1772
1773         TRACE_DEV(pinctrl->dev, "group %s set to function %d", lane->name, i);
1774
1775         if (lane_is_otg(group) || lane_is_hsic(group)) {
1776                 value = padctl_readl(ctx, lane->offset);
1777                 value &= ~(lane->mask << lane->shift);
1778                 value |= i << lane->shift;
1779                 padctl_writel(ctx, value, lane->offset);
1780         } else if (lane_is_uphy(group)) {
1781                 int uphy_lane = group - PIN_UPHY_0;
1782                 if (function == TEGRA186_FUNC_USB3)
1783                         set_bit(uphy_lane, &ctx->usb3_lanes);
1784                 else if (function == TEGRA186_FUNC_PCIE)
1785                         set_bit(uphy_lane, &ctx->pcie_lanes);
1786                 else if (function == TEGRA186_FUNC_SATA)
1787                         set_bit(uphy_lane, &ctx->sata_lanes);
1788                 else if (function == TEGRA186_FUNC_MPHY)
1789                         set_bit(uphy_lane, &ctx->ufs_lanes);
1790 #if 0
1791                 value = uphy_lane_readl(ctx, uphy_lane, lane->offset);
1792                 value &= ~(lane->mask << lane->shift);
1793                 value |= i << lane->shift;
1794                 uphy_lane_writel(ctx, uphy_lane, value, lane->offset);
1795 #endif
1796         } else
1797                 return -EINVAL;
1798
1799         return 0;
1800 }
1801
1802 static const struct pinmux_ops tegra186_padctl_uphy_pinmux_ops = {
1803         .get_functions_count = tegra186_padctl_uphy_get_functions_count,
1804         .get_function_name = tegra186_padctl_uphy_get_function_name,
1805         .get_function_groups = tegra186_padctl_uphy_get_function_groups,
1806         .enable = tegra186_padctl_uphy_pinmux_enable,
1807 };
1808
1809 static int tegra_xusb_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
1810                                                unsigned int group,
1811                                                unsigned long *config)
1812 {
1813         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1814         const struct tegra_padctl_uphy_lane *lane;
1815         enum tegra_xusb_padctl_param param;
1816         int value = 0;
1817         int port;
1818
1819         param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);
1820         lane = &padctl->soc->lanes[group];
1821
1822         TRACE("group %u param 0x%x\n", group, param);
1823
1824         switch (param) {
1825         case TEGRA_XUSB_PADCTL_USB3_PORT:
1826                 value = lane_to_usb3_port(padctl, group);
1827                 if (value < 0) {
1828                         dev_err(padctl->dev,
1829                                 "Pin %d not mapped to USB3 port\n", group);
1830                         return -EINVAL;
1831                 }
1832                 break;
1833
1834         case TEGRA_XUSB_PADCTL_USB2_PORT:
1835                 port = lane_to_usb3_port(padctl, group);
1836                 if (port < 0) {
1837                         dev_err(padctl->dev,
1838                                 "Pin %d not mapped to USB3 port\n", group);
1839                         return -EINVAL;
1840                 }
1841
1842                 break;
1843
1844         default:
1845                 dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
1846                         param);
1847                 return -ENOTSUPP;
1848         }
1849
1850         *config = TEGRA_XUSB_PADCTL_PACK(param, value);
1851         return 0;
1852 }
1853
1854 #if 0
1855 static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
1856                                                unsigned int group,
1857                                                unsigned long *configs,
1858                                                unsigned int num_configs)
1859 {
1860         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1861         const struct tegra_padctl_uphy_lane *lane;
1862         enum tegra_xusb_padctl_param param;
1863         unsigned long value;
1864         unsigned int i;
1865         int port;
1866
1867         lane = &padctl->soc->lanes[group];
1868
1869         for (i = 0; i < num_configs; i++) {
1870                 param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
1871                 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);
1872
1873                 TRACE("group %u config %d param 0x%x value 0x%lx",
1874                         group, i, param, value);
1875
1876                 switch (param) {
1877                 case TEGRA_XUSB_PADCTL_USB3_PORT:
1878                         if (value >= TEGRA_XUSB_USB3_PHYS) {
1879                                 dev_err(padctl->dev, "Invalid USB3 port: %lu\n",
1880                                         value);
1881                                 return -EINVAL;
1882                         }
1883                         if (!lane_is_uphy(group)) {
1884                                 dev_err(padctl->dev,
1885                                         "USB3 port not applicable for pin %d\n",
1886                                         group);
1887                                 return -EINVAL;
1888                         }
1889
1890                         padctl->usb3_ports[value].lane = group;
1891                         break;
1892
1893                 case TEGRA_XUSB_PADCTL_USB2_PORT:
1894                         if (value >= TEGRA_XUSB_UTMI_PHYS) {
1895                                 dev_err(padctl->dev, "Invalid USB2 port: %lu\n",
1896                                         value);
1897                                 return -EINVAL;
1898                         }
1899                         if (!lane_is_pcie_or_sata(group)) {
1900                                 dev_err(padctl->dev,
1901                                         "USB2 port not applicable for pin %d\n",
1902                                         group);
1903                                 return -EINVAL;
1904                         }
1905                         port = lane_to_usb3_port(padctl, group);
1906                         if (port < 0) {
1907                                 dev_err(padctl->dev,
1908                                         "Pin %d not mapped to USB3 port\n",
1909                                         group);
1910                                 return -EINVAL;
1911                         }
1912
1913                         break;
1914
1915                 default:
1916                         dev_err(padctl->dev,
1917                                 "invalid configuration parameter: %04x\n",
1918                                 param);
1919                         return -ENOTSUPP;
1920                 }
1921         }
1922
1923         return 0;
1924 }
1925 #else
1926 static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
1927                                                unsigned int group,
1928                                                unsigned long config)
1929 {
1930         struct tegra_padctl_uphy *padctl = pinctrl_dev_get_drvdata(pinctrl);
1931         const struct tegra_padctl_uphy_lane *lane;
1932         enum tegra_xusb_padctl_param param;
1933         unsigned long value;
1934         int port;
1935
1936         lane = &padctl->soc->lanes[group];
1937
1938         param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
1939         value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
1940
1941         TRACE("group %u config 0x%lx param 0x%x value 0x%lx",
1942                 group, config, param, value);
1943
1944         switch (param) {
1945         case TEGRA_XUSB_PADCTL_USB3_PORT:
1946                 if (value >= TEGRA_XUSB_USB3_PHYS) {
1947                         dev_err(padctl->dev, "Invalid USB3 port: %lu\n",
1948                                 value);
1949                         return -EINVAL;
1950                 }
1951                 if (!lane_is_uphy(group)) {
1952                         dev_err(padctl->dev,
1953                                 "USB3 port not applicable for pin %d\n",
1954                                 group);
1955                         return -EINVAL;
1956                 }
1957
1958                 padctl->usb3_ports[value].lane = group;
1959                 break;
1960
1961         case TEGRA_XUSB_PADCTL_USB2_PORT:
1962                 if (value >= TEGRA_XUSB_UTMI_PHYS) {
1963                         dev_err(padctl->dev, "Invalid USB2 port: %lu\n",
1964                                 value);
1965                         return -EINVAL;
1966                 }
1967                 if (!lane_is_pcie_or_sata(group)) {
1968                         dev_err(padctl->dev,
1969                                 "USB2 port not applicable for pin %d\n",
1970                                 group);
1971                         return -EINVAL;
1972                 }
1973                 port = lane_to_usb3_port(padctl, group);
1974                 if (port < 0) {
1975                         dev_err(padctl->dev,
1976                                 "Pin %d not mapped to USB3 port\n",
1977                                 group);
1978                         return -EINVAL;
1979                 }
1980
1981                 break;
1982
1983         default:
1984                 dev_err(padctl->dev,
1985                         "invalid configuration parameter: %04x\n",
1986                         param);
1987                 return -ENOTSUPP;
1988         }
1989
1990         return 0;
1991 }
1992 #endif
1993
1994 #ifdef CONFIG_DEBUG_FS
1995 static const char *strip_prefix(const char *s)
1996 {
1997         const char *comma = strchr(s, ',');
1998         if (!comma)
1999                 return s;
2000
2001         return comma + 1;
2002 }
2003
2004 static void
2005 tegra_xusb_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
2006                                          struct seq_file *s,
2007                                          unsigned int group)
2008 {
2009         unsigned int i;
2010
2011         for (i = 0; i < ARRAY_SIZE(properties); i++) {
2012                 unsigned long config, value;
2013                 int err;
2014
2015                 config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);
2016
2017                 err = tegra_xusb_padctl_pinconf_group_get(pinctrl, group,
2018                                                           &config);
2019                 if (err < 0)
2020                         continue;
2021
2022                 value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
2023
2024                 seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
2025                            value);
2026         }
2027 }
2028
2029 static void
2030 tegra_xusb_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
2031                                           struct seq_file *s,
2032                                           unsigned long config)
2033 {
2034         enum tegra_xusb_padctl_param param;
2035         const char *name = "unknown";
2036         unsigned long value;
2037         unsigned int i;
2038
2039         param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
2040         value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
2041
2042         for (i = 0; i < ARRAY_SIZE(properties); i++) {
2043                 if (properties[i].param == param) {
2044                         name = properties[i].name;
2045                         break;
2046                 }
2047         }
2048
2049         seq_printf(s, "%s=%lu", strip_prefix(name), value);
2050 }
2051 #endif
2052
2053 static const struct pinconf_ops tegra_xusb_padctl_pinconf_ops = {
2054         .pin_config_group_get = tegra_xusb_padctl_pinconf_group_get,
2055         .pin_config_group_set = tegra_xusb_padctl_pinconf_group_set,
2056 #ifdef CONFIG_DEBUG_FS
2057         .pin_config_group_dbg_show = tegra_xusb_padctl_pinconf_group_dbg_show,
2058         .pin_config_config_dbg_show = tegra_xusb_padctl_pinconf_config_dbg_show,
2059 #endif
2060 };
2061
2062 static int tegra_xusb_padctl_enable(struct tegra_padctl_uphy *ctx)
2063 {
2064
2065         mutex_lock(&ctx->lock);
2066         TRACE_DEV(ctx->dev, "padctl_clients %d", ctx->padctl_clients);
2067
2068         if (ctx->padctl_clients++ > 0)
2069                 goto out;
2070
2071 out:
2072         mutex_unlock(&ctx->lock);
2073         return 0;
2074 }
2075
2076 static int tegra_xusb_padctl_disable(struct tegra_padctl_uphy *ctx)
2077 {
2078
2079         mutex_lock(&ctx->lock);
2080
2081         TRACE_DEV(ctx->dev, "padctl_clients %d", ctx->padctl_clients);
2082
2083         if (WARN_ON(ctx->padctl_clients == 0))
2084                 goto out;
2085
2086         if (--ctx->padctl_clients > 0)
2087                 goto out;
2088
2089 out:
2090         mutex_unlock(&ctx->lock);
2091         return 0;
2092 }
2093
2094 static int tegra_xusb_phy_init(struct phy *phy)
2095 {
2096         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2097
2098         return tegra_xusb_padctl_enable(padctl);
2099 }
2100
2101 static int tegra_xusb_phy_exit(struct phy *phy)
2102 {
2103         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2104
2105         return tegra_xusb_padctl_disable(padctl);
2106 }
2107
2108 static int tegra186_pcie_phy_power_on(struct phy *phy)
2109 {
2110         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2111         unsigned int uphy_lane;
2112         u32 reg;
2113
2114         for_each_set_bit(uphy_lane, &ctx->pcie_lanes, T186_UPHY_LANES) {
2115                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2116
2117                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2118                 reg &= SEL(~0);
2119                 reg |= SEL_PCIE;
2120                 reg |= FORCE_IDDQ_DISABLE;
2121                 reg &= ~CLAMP_EN_EARLY;
2122                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2123         }
2124
2125         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO: more? */
2126         return 0;
2127 }
2128
2129 static int tegra186_pcie_phy_power_off(struct phy *phy)
2130 {
2131         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2132         unsigned int uphy_lane;
2133         u32 reg;
2134
2135         for_each_set_bit(uphy_lane, &ctx->pcie_lanes, T186_UPHY_LANES) {
2136                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2137
2138                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2139                 reg &= ~FORCE_IDDQ_DISABLE;
2140                 reg |= CLAMP_EN_EARLY;
2141                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2142         }
2143
2144         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO */
2145         return 0;
2146 }
2147
2148 static int tegra186_pcie_phy_init(struct phy *phy)
2149 {
2150         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2151         unsigned uphy_lane;
2152
2153         for_each_set_bit(uphy_lane, &ctx->pcie_lanes, T186_UPHY_LANES) {
2154                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2155
2156                 uphy_pll_init(ctx, 0);
2157                 uphy_lane_init(ctx, uphy_lane);
2158                 pcie_lane_defaults(ctx, uphy_lane);
2159                 pcie_usb3_pll_defaults(ctx);
2160         }
2161
2162         return tegra_xusb_phy_init(phy);
2163 }
2164
2165 static int tegra186_pcie_phy_exit(struct phy *phy)
2166 {
2167         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2168         unsigned uphy_lane;
2169
2170         for_each_set_bit(uphy_lane, &ctx->pcie_lanes, T186_UPHY_LANES) {
2171                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2172
2173                 uphy_lane_deinit(ctx, uphy_lane);
2174                 uphy_pll_deinit(ctx, 0);
2175         }
2176
2177         return tegra_xusb_phy_exit(phy);
2178 }
2179
2180 static const struct phy_ops pcie_phy_ops = {
2181         .init = tegra186_pcie_phy_init,
2182         .exit = tegra186_pcie_phy_exit,
2183         .power_on = tegra186_pcie_phy_power_on,
2184         .power_off = tegra186_pcie_phy_power_off,
2185         .owner = THIS_MODULE,
2186 };
2187
2188 static int tegra186_sata_phy_power_on(struct phy *phy)
2189 {
2190         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2191         unsigned int uphy_lane;
2192         u32 reg;
2193
2194         for_each_set_bit(uphy_lane, &ctx->sata_lanes, T186_UPHY_LANES) {
2195                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2196
2197                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2198                 reg &= SEL(~0);
2199                 reg |= SEL_SATA;
2200                 reg |= FORCE_IDDQ_DISABLE;
2201                 reg &= ~CLAMP_EN_EARLY;
2202                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2203         }
2204
2205         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO: more? */
2206         return 0;
2207 }
2208
2209 static int tegra186_sata_phy_power_off(struct phy *phy)
2210 {
2211         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2212         unsigned int uphy_lane;
2213         u32 reg;
2214
2215         for_each_set_bit(uphy_lane, &ctx->sata_lanes, T186_UPHY_LANES) {
2216                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2217
2218                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2219                 reg &= ~FORCE_IDDQ_DISABLE;
2220                 reg |= CLAMP_EN_EARLY;
2221                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2222         }
2223
2224         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO */
2225         return 0;
2226 }
2227
2228 static int tegra186_sata_fuse_calibration(struct tegra_padctl_uphy *ctx,
2229                                                 int lane)
2230 {
2231         void __iomem *base;
2232         u32 reg;
2233         int idx, err;
2234
2235         if (ctx->sata_bypass_fuse && ctx->prod_list) {
2236                 base = ctx->uphy_lane_regs[lane];
2237                 err = tegra_prod_set_by_name(&base,
2238                                         "prod_c_sata", ctx->prod_list);
2239                 if (!err)
2240                         return 0;
2241
2242                 /* In case of err update setting based on fuse */
2243                 dev_warn(ctx->dev,
2244                         "Failed to set sata prod settings, err %d", err);
2245         }
2246
2247         /* Update based on fuse_sata_nv_calib[1:0] value */
2248         idx = SATA_NV_CALIB_0_1(ctx->fuse_calib.sata_nv);
2249
2250         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_AUX_CTL_1);
2251         reg |= AUX_RX_IDLE_TH(sata_data[idx].aux_rx_idle_th);
2252         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_AUX_CTL_1);
2253
2254         /* Update based on fuse_sata_nv_calib[3:2] value TBD */
2255         idx = SATA_NV_CALIB_2_3(ctx->fuse_calib.sata_nv);
2256
2257         /* Update based on fuse_sata_mphy_odm_calib[1:0] value */
2258         idx = SATA_MPHY_ODM_CALIB_0_1(ctx->fuse_calib.sata_mphy_odm);
2259
2260         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_1);
2261         reg |= TX_DRV_AMP_SEL0(sata_data[idx].tx_drv_amp_sel0);
2262         reg |= TX_DRV_AMP_SEL1(sata_data[idx].tx_drv_amp_sel1);
2263         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_1);
2264
2265         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_4);
2266         reg |= TX_DRV_POST_SEL0(sata_data[idx].tx_drv_post_sel0);
2267         reg |= TX_DRV_POST_SEL1(sata_data[idx].tx_drv_post_sel1);
2268         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_4);
2269
2270         reg = CFG_ADDR(AE_CTLE_CTRL_ID0);
2271         reg |= CFG_WDATA(sata_data[idx].ae_ctle_ctrl_id0);
2272         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
2273
2274         reg = CFG_ADDR(AE_CTLE_CTRL_ID1);
2275         reg |= CFG_WDATA(sata_data[idx].ae_ctle_ctrl_id1);
2276         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DIRECT_CTL_2);
2277
2278         return 0;
2279 }
2280
2281 static int tegra186_sata_phy_init(struct phy *phy)
2282 {
2283         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2284         unsigned uphy_lane;
2285
2286         sata_pll_init(ctx);
2287
2288         for_each_set_bit(uphy_lane, &ctx->sata_lanes, T186_UPHY_LANES) {
2289                 TRACE_DEV(&phy->dev, "uphy_lane %u\n", uphy_lane);
2290                 sata_lane_init(ctx, uphy_lane);
2291         }
2292
2293         tegra186_sata_fuse_calibration(ctx, ctx->sata_lanes);
2294
2295         return tegra_xusb_phy_init(phy);
2296 }
2297
2298 static int tegra186_sata_phy_exit(struct phy *phy)
2299 {
2300         TRACE_DEV(&phy->dev, "");
2301         return tegra_xusb_phy_exit(phy);
2302 }
2303
2304 static const struct phy_ops sata_phy_ops = {
2305         .init = tegra186_sata_phy_init,
2306         .exit = tegra186_sata_phy_exit,
2307         .power_on = tegra186_sata_phy_power_on,
2308         .power_off = tegra186_sata_phy_power_off,
2309         .owner = THIS_MODULE,
2310 };
2311
2312
2313 static int tegra186_ufs_phy_power_on(struct phy *phy)
2314 {
2315
2316         return 0;
2317 }
2318
2319 static int tegra186_ufs_phy_power_off(struct phy *phy)
2320 {
2321         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2322         unsigned int uphy_lane;
2323         u32 reg;
2324
2325         TRACE();
2326         for_each_set_bit(uphy_lane, &ctx->ufs_lanes, T186_UPHY_LANES) {
2327                 TRACE_DEV(&phy->dev, "lane %d", uphy_lane);
2328                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2329                 reg &= ~FORCE_IDDQ_DISABLE;
2330                 reg |= CLAMP_EN_EARLY;
2331                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2332         }
2333
2334         return 0;
2335 }
2336
2337 static void tegra186_uphy_lane_pad_macro(bool on)
2338 {
2339         void __iomem *car_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
2340         u32 reg;
2341
2342         reg = ioread32(car_base + CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
2343         if (on)
2344                 reg |= SWR_UPHY_RST;
2345         else
2346                 reg &= ~SWR_UPHY_RST;
2347         iowrite32(reg, car_base + CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
2348 }
2349
2350 static void tegra186_uphy_lane_and_pll_reset(int function, unsigned long lanes)
2351 {
2352         void __iomem *car_base = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
2353         unsigned uphy_lane;
2354         u32 reg;
2355
2356         reg = ioread32(car_base + CLK_RST_CONTROLLER_RST_DEV_PEX_USB_UPHY_0);
2357
2358         /* De-assert global reset */
2359         reg |= SWR_PEX_USB_UPHY_RST;
2360
2361         /* De-assert corresponding pll reset */
2362         if (function == TEGRA186_FUNC_MPHY)
2363                 reg &= ~SWR_PEX_USB_UPHY_PLL1_RST;
2364         else
2365                 reg &= ~SWR_PEX_USB_UPHY_PLL0_RST;
2366
2367         /* De-assert corresponding lane reset */
2368         for_each_set_bit(uphy_lane, &lanes, T186_UPHY_LANES) {
2369                 reg &= ~SWR_PEX_USB_UPHY_LANE_RST(uphy_lane);
2370         }
2371
2372         iowrite32(reg, car_base + CLK_RST_CONTROLLER_RST_DEV_PEX_USB_UPHY_0);
2373 }
2374
2375 static int tegra186_ufs_fuse_calibration(struct tegra_padctl_uphy *ctx,
2376                                                 int lane)
2377 {
2378         u32 reg;
2379         int idx;
2380
2381         /* Update based on fuse_mphy_nv_calib[1:0] value */
2382         idx = MPHY_NV_CALIB_0_1(ctx->fuse_calib.mphy_nv);
2383
2384         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_AUX_CTL_1);
2385         reg |= AUX_RX_IDLE_TH(mphy_data[idx].aux_rx_idle_th);
2386         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_AUX_CTL_1);
2387
2388         /* Update based on fuse_mphy_nv_calib[3:2] value TBD */
2389         idx = MPHY_NV_CALIB_2_3(ctx->fuse_calib.mphy_nv);
2390
2391
2392         /* Update based on fuse_mphy_nv_calib[5:4] value TBD */
2393         idx = MPHY_NV_CALIB_4_5(ctx->fuse_calib.mphy_nv);
2394
2395
2396         /* Update based on fuse_sata_mphy_odm_calib[1:0] value */
2397         idx = SATA_MPHY_ODM_CALIB_0_1(ctx->fuse_calib.sata_mphy_odm);
2398
2399         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_1);
2400         reg |= TX_DRV_AMP_SEL0(mphy_data[idx].tx_drv_amp_sel0);
2401         reg |= TX_DRV_AMP_SEL1(mphy_data[idx].tx_drv_amp_sel1);
2402         reg |= TX_DRV_AMP_SEL2(mphy_data[idx].tx_drv_amp_sel2);
2403         reg |= TX_DRV_AMP_SEL3(mphy_data[idx].tx_drv_amp_sel3);
2404         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_1);
2405
2406         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_2);
2407         reg |= TX_DRV_AMP_SEL4(mphy_data[idx].tx_drv_amp_sel4);
2408         reg |= TX_DRV_AMP_SEL5(mphy_data[idx].tx_drv_amp_sel5);
2409         reg |= TX_DRV_AMP_SEL6(mphy_data[idx].tx_drv_amp_sel6);
2410         reg |= TX_DRV_AMP_SEL7(mphy_data[idx].tx_drv_amp_sel7);
2411         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_2);
2412
2413         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_3);
2414         reg |= TX_DRV_AMP_SEL8(mphy_data[idx].tx_drv_amp_sel8);
2415         reg |= TX_DRV_AMP_SEL9(mphy_data[idx].tx_drv_amp_sel9);
2416         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_3);
2417
2418         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_4);
2419         reg |= TX_DRV_POST_SEL0(mphy_data[idx].tx_drv_post_sel0);
2420         reg |= TX_DRV_POST_SEL1(mphy_data[idx].tx_drv_post_sel1);
2421         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_4);
2422
2423         reg = uphy_lane_readl(ctx, lane, UPHY_LANE_DYN_CTL_5);
2424         reg |= TX_DRV_POST_SEL2(mphy_data[idx].tx_drv_post_sel2);
2425         reg |= TX_DRV_POST_SEL3(mphy_data[idx].tx_drv_post_sel3);
2426         reg |= TX_DRV_PRE_SEL3(mphy_data[idx].tx_drv_pre_sel3);
2427         uphy_lane_writel(ctx, lane, reg, UPHY_LANE_DYN_CTL_5);
2428
2429         return 0;
2430 }
2431
2432 static int tegra186_ufs_phy_init(struct phy *phy)
2433 {
2434         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2435         unsigned uphy_lane;
2436         u32 reg;
2437
2438         TRACE();
2439         /* FIXME: step 1: Enable refplle to 208M and pllp 102M */
2440
2441         /* step 2: De-assert UPHY LANE PAD Macro */
2442         /* FIXME: Should be part of probe ? */
2443         tegra186_uphy_lane_pad_macro(false);
2444
2445         /* Bring the lanes out of IDDQ, remove clamps, select MPHY for mux */
2446         for_each_set_bit(uphy_lane, &ctx->ufs_lanes, T186_UPHY_LANES) {
2447                 reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2448                 reg &= SEL(~0);
2449                 reg |= SEL_MPHY;
2450                 reg |= FORCE_IDDQ_DISABLE;
2451                 reg &= ~CLAMP_EN_EARLY;
2452                 uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2453         }
2454
2455         /* FIXME: bring refPLLE to under HW control. Needed ? */
2456
2457
2458         /* step 3: Enable PLL1 and lane by releasing resets */
2459         tegra186_uphy_lane_and_pll_reset(TEGRA186_FUNC_MPHY, ctx->ufs_lanes);
2460
2461         /* pll parameters init */
2462         ufs_pll_init(ctx);
2463
2464         /* lane parameters init */
2465         for_each_set_bit(uphy_lane, &ctx->ufs_lanes, T186_UPHY_LANES) {
2466                 TRACE_DEV(&phy->dev, "uphy_lane %u\n", uphy_lane);
2467                 ufs_lane_init(ctx, uphy_lane);
2468         }
2469
2470         /* step 4: electrical parameters programming based on fuses */
2471         for_each_set_bit(uphy_lane, &ctx->ufs_lanes, T186_UPHY_LANES)
2472                 tegra186_ufs_fuse_calibration(ctx, uphy_lane);
2473
2474         /* step 5: rate id programming */
2475         ufs_pll_rateid_init(ctx);
2476         ufs_lane_rateid_init(ctx, ctx->ufs_lanes);
2477
2478         /* step 6: uphy pll1 calibration */
2479         uphy_pll_init(ctx, 0);
2480
2481         /* FIXME: Need to add MPHY programming in above step ? */
2482
2483         return tegra_xusb_phy_init(phy);
2484 }
2485
2486 static int tegra186_ufs_phy_exit(struct phy *phy)
2487 {
2488         TRACE_DEV(&phy->dev, "");
2489         return tegra_xusb_phy_exit(phy);
2490 }
2491
2492 static const struct phy_ops ufs_phy_ops = {
2493         .init = tegra186_ufs_phy_init,
2494         .exit = tegra186_ufs_phy_exit,
2495         .power_on = tegra186_ufs_phy_power_on,
2496         .power_off = tegra186_ufs_phy_power_off,
2497         .owner = THIS_MODULE,
2498 };
2499
2500 static int usb3_phy_to_port(struct phy *phy)
2501 {
2502         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2503         unsigned int i;
2504
2505         for (i = 0; i < TEGRA_XUSB_USB3_PHYS; i++) {
2506                 if (phy == ctx->usb3_phys[i])
2507                         return i;
2508         }
2509         WARN_ON(1);
2510
2511         return -EINVAL;
2512 }
2513
2514 static int tegra186_usb3_phy_power_on(struct phy *phy)
2515 {
2516         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2517         int port = usb3_phy_to_port(phy);
2518         unsigned int uphy_lane;
2519         unsigned int lane;
2520         u32 reg;
2521
2522         TRACE_DEV(&phy->dev, "port %d", port);
2523         if (port < 0)
2524                 return port;
2525
2526         lane = ctx->usb3_ports[port].lane;
2527         if (!lane_is_uphy(lane)) {
2528                 dev_err(ctx->dev, "USB3 PHY %d mapped to invalid lane: %d\n",
2529                         port, lane);
2530                 return -EINVAL;
2531         }
2532
2533         reg = padctl_readl(ctx, XUSB_PADCTL_SS_PORT_CAP);
2534         reg &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(port));
2535         reg |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(port));
2536         padctl_writel(ctx, reg, XUSB_PADCTL_SS_PORT_CAP);
2537
2538         uphy_lane = lane - PIN_UPHY_0;
2539         reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2540         reg &= SEL(~0);
2541         reg |= SEL_XUSB;
2542         reg |= FORCE_IDDQ_DISABLE;
2543         reg &= ~CLAMP_EN_EARLY;
2544         uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2545
2546         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2547         reg &= ~SSPX_ELPG_CLAMP_EN_EARLY(port);
2548         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2549
2550         usleep_range(100, 200);
2551
2552         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2553         reg &= ~SSPX_ELPG_CLAMP_EN_EARLY(port);
2554         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2555
2556         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2557         reg &= ~SSPX_ELPG_VCORE_DOWN(port);
2558         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2559
2560         return 0;
2561 }
2562
2563 static int tegra186_usb3_phy_power_off(struct phy *phy)
2564 {
2565         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2566         int port = usb3_phy_to_port(phy);
2567         unsigned int uphy_lane;
2568         unsigned int lane;
2569         u32 reg;
2570
2571         TRACE_DEV(&phy->dev, "port %d", port);
2572         if (port < 0)
2573                 return port;
2574
2575         lane = ctx->usb3_ports[port].lane;
2576         if (!lane_is_uphy(lane)) {
2577                 dev_err(ctx->dev, "USB3 PHY %d mapped to invalid lane: %d\n",
2578                         port, lane);
2579                 return -EINVAL;
2580         }
2581
2582         uphy_lane = lane - PIN_UPHY_0;
2583         reg = uphy_lane_readl(ctx, uphy_lane, UPHY_LANE_MUX);
2584         reg &= ~FORCE_IDDQ_DISABLE;
2585         reg |= CLAMP_EN_EARLY;
2586         uphy_lane_writel(ctx, uphy_lane, reg, UPHY_LANE_MUX);
2587
2588         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2589         reg |= SSPX_ELPG_CLAMP_EN_EARLY(port);
2590         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2591
2592         usleep_range(100, 200);
2593
2594         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2595         reg |= SSPX_ELPG_CLAMP_EN_EARLY(port);
2596         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2597
2598         usleep_range(250, 350);
2599
2600         reg = padctl_readl(ctx, XUSB_PADCTL_ELPG_PROGRAM_1);
2601         reg |= SSPX_ELPG_VCORE_DOWN(port);
2602         padctl_writel(ctx, reg, XUSB_PADCTL_ELPG_PROGRAM_1);
2603
2604         return 0;
2605 }
2606
2607 static int tegra186_usb3_phy_init(struct phy *phy)
2608 {
2609         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2610         int port = usb3_phy_to_port(phy);
2611         unsigned uphy_lane;
2612         unsigned int lane;
2613
2614         TRACE_DEV(&phy->dev, "port %d", port);
2615         if (port < 0)
2616                 return port;
2617
2618         lane = ctx->usb3_ports[port].lane;
2619         if (!lane_is_uphy(lane)) {
2620                 dev_err(ctx->dev, "USB3 PHY %d mapped to invalid lane: %d\n",
2621                         port, lane);
2622                 return -EINVAL;
2623         }
2624
2625         uphy_pll_init(ctx, 0);
2626
2627         uphy_lane = lane - PIN_UPHY_0;
2628         uphy_lane_init(ctx, uphy_lane);
2629         usb3_lane_defaults(ctx, uphy_lane);
2630         pcie_usb3_pll_defaults(ctx);
2631
2632         return tegra_xusb_phy_init(phy);
2633 }
2634
2635 static int tegra186_usb3_phy_exit(struct phy *phy)
2636 {
2637         struct tegra_padctl_uphy *ctx = phy_get_drvdata(phy);
2638         int port = usb3_phy_to_port(phy);
2639         unsigned uphy_lane;
2640         unsigned int lane;
2641
2642         TRACE_DEV(&phy->dev, "port %d", port);
2643         if (port < 0)
2644                 return port;
2645
2646         lane = ctx->usb3_ports[port].lane;
2647         if (!lane_is_uphy(lane)) {
2648                 dev_err(ctx->dev, "USB3 PHY %d mapped to invalid lane: %d\n",
2649                         port, lane);
2650                 return -EINVAL;
2651         }
2652
2653         uphy_lane = lane - PIN_UPHY_0;
2654         uphy_lane_deinit(ctx, lane);
2655         uphy_pll_deinit(ctx, 0);
2656
2657         return tegra_xusb_phy_exit(phy);
2658 }
2659
2660 static const struct phy_ops usb3_phy_ops = {
2661         .init = tegra186_usb3_phy_init,
2662         .exit = tegra186_usb3_phy_exit,
2663         .power_on = tegra186_usb3_phy_power_on,
2664         .power_off = tegra186_usb3_phy_power_off,
2665         .owner = THIS_MODULE,
2666 };
2667
2668 static int utmi_phy_to_port(struct phy *phy)
2669 {
2670         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2671         unsigned int i;
2672
2673         for (i = 0; i < TEGRA_XUSB_UTMI_PHYS; i++) {
2674                 if (phy == padctl->utmi_phys[i])
2675                         return i;
2676         }
2677         WARN_ON(1);
2678
2679         return -EINVAL;
2680 }
2681
2682 static int tegra186_utmi_phy_power_on(struct phy *phy)
2683 {
2684         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2685         int port = utmi_phy_to_port(phy);
2686         int err;
2687         u32 value;
2688
2689         TRACE_DEV(&phy->dev, "port %d", port);
2690         if (port < 0)
2691                 return port;
2692
2693 #if 0
2694         value = padctl_readl(padctl, XUSB_PADCTL_USB2_PORT_CAP);
2695         value &= ~(PORT_CAP_MASK << PORTX_CAP_SHIFT(port));
2696 #if 1
2697         if (port == 0) /* TODO: support OTG */
2698                 value |= (PORT_CAP_DEVICE << PORTX_CAP_SHIFT(port));
2699         else
2700 #endif
2701                 value |= (PORT_CAP_HOST << PORTX_CAP_SHIFT(port));
2702         padctl_writel(padctl, value, XUSB_PADCTL_USB2_PORT_CAP);
2703 #endif
2704
2705         value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
2706         value &= ~(USB2_OTG_PD | USB2_OTG_PD2 | USB2_OTG_PD_ZI);
2707         value &= ~HS_CURR_LEVEL(~0);
2708         value |= HS_CURR_LEVEL(padctl->calib.hs_curr_level[port]);
2709         padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL0(port));
2710
2711         value = padctl_readl(padctl, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));
2712         value &= ~USB2_OTG_PD_DR;
2713         value &= ~TERM_RANGE_ADJ(~0);
2714         value |= TERM_RANGE_ADJ(padctl->calib.hs_term_range_adj);
2715         padctl_writel(padctl, value, XUSB_PADCTL_USB2_OTG_PADX_CTL1(port));
2716
2717         err = regulator_enable(padctl->vbus[port]);
2718         if (err)
2719                 return err;
2720
2721         mutex_lock(&padctl->lock);
2722
2723         if (padctl->utmi_enable++ > 0)
2724                 goto out;
2725
2726         value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
2727         value &= ~BIAS_PAD_PD;
2728         padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
2729
2730 out:
2731         mutex_unlock(&padctl->lock);
2732         return 0;
2733 }
2734
2735 static int tegra186_utmi_phy_power_off(struct phy *phy)
2736 {
2737         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2738         int port = utmi_phy_to_port(phy);
2739         u32 value;
2740
2741         TRACE_DEV(&phy->dev, "port %d", port);
2742         if (port < 0)
2743                 return port;
2744
2745         regulator_disable(padctl->vbus[port]);
2746
2747         mutex_lock(&padctl->lock);
2748
2749         if (WARN_ON(padctl->utmi_enable == 0))
2750                 goto out;
2751
2752         if (--padctl->utmi_enable > 0)
2753                 goto out;
2754
2755         value = padctl_readl(padctl, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
2756         value |= BIAS_PAD_PD;
2757         padctl_writel(padctl, value, XUSB_PADCTL_USB2_BIAS_PAD_CTL0);
2758
2759 out:
2760         mutex_unlock(&padctl->lock);
2761         return 0;
2762 }
2763
2764 static int tegra186_utmi_phy_init(struct phy *phy)
2765 {
2766         TRACE_DEV(&phy->dev, "");
2767         return tegra_xusb_phy_init(phy);
2768 }
2769
2770 static int tegra186_utmi_phy_exit(struct phy *phy)
2771 {
2772         TRACE_DEV(&phy->dev, "");
2773         return tegra_xusb_phy_exit(phy);
2774 }
2775
2776 static const struct phy_ops utmi_phy_ops = {
2777         .init = tegra186_utmi_phy_init,
2778         .exit = tegra186_utmi_phy_exit,
2779         .power_on = tegra186_utmi_phy_power_on,
2780         .power_off = tegra186_utmi_phy_power_off,
2781         .owner = THIS_MODULE,
2782 };
2783
2784 #if 0
2785 static int utmi_phy_set_role(struct phy *phy, bool host)
2786 {
2787         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2788         int pad = utmi_phy_to_port(phy);
2789         u32 reg;
2790
2791         dev_info(&phy->dev, "%s %d port %d\n", __func__, __LINE__, pad);
2792         if (pad < 0)
2793                 return pad;
2794
2795         reg = padctl_readl(padctl, USB2_BATTERY_CHRG_OTGPADX_CTL1(pad));
2796         reg &= ~(VREG_FIX18 | VREG_LEV(~0));
2797         if (host)
2798                 reg |= VREG_FIX18;
2799         else
2800                 reg |= VREG_LEV(3);
2801         padctl_writel(padctl, reg, USB2_BATTERY_CHRG_OTGPADX_CTL1(pad));
2802
2803         reg = padctl_readl(padctl, USB2_VBUS_ID);
2804         if (host)
2805                 reg &= ~VBUS_OVERRIDE;
2806         else
2807                 reg |= VBUS_OVERRIDE;
2808         padctl_writel(padctl, reg, USB2_VBUS_ID);
2809
2810         return 0;
2811 }
2812
2813 static int tegra_phy_otg_set_host(struct phy *phy)
2814 {
2815         return utmi_phy_set_role(phy, true);
2816 }
2817
2818 static int tegra_phy_otg_set_device(struct phy *phy)
2819 {
2820         return utmi_phy_set_role(phy, false);
2821 }
2822 #endif
2823 static int hsic_phy_to_port(struct phy *phy)
2824 {
2825         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2826         unsigned int i;
2827
2828         for (i = 0; i < TEGRA_XUSB_HSIC_PHYS; i++) {
2829                 if (phy == padctl->hsic_phys[i])
2830                         return i;
2831         }
2832         WARN_ON(1);
2833
2834         return -EINVAL;
2835 }
2836
2837 static int tegra186_hsic_phy_power_on(struct phy *phy)
2838 {
2839         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2840         int port = hsic_phy_to_port(phy);
2841         int err;
2842         u32 value;
2843
2844         TRACE_DEV(&phy->dev, "port %d", port);
2845         if (port < 0)
2846                 return port;
2847
2848         err = regulator_enable(padctl->vddio_hsic);
2849         if (err)
2850                 return err;
2851 #if 0
2852         value = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL1(port));
2853         value &= ~(XUSB_PADCTL_HSIC_PAD_CTL1_RPD_STROBE |
2854                    XUSB_PADCTL_HSIC_PAD_CTL1_RPU_DATA |
2855                    XUSB_PADCTL_HSIC_PAD_CTL1_PD_RX |
2856                    XUSB_PADCTL_HSIC_PAD_CTL1_PD_ZI |
2857                    XUSB_PADCTL_HSIC_PAD_CTL1_PD_TRX |
2858                    XUSB_PADCTL_HSIC_PAD_CTL1_PD_TX);
2859         value |= XUSB_PADCTL_HSIC_PAD_CTL1_RPD_DATA |
2860                  XUSB_PADCTL_HSIC_PAD_CTL1_RPU_STROBE;
2861         padctl_writel(padctl, value, XUSB_PADCTL_HSIC_PADX_CTL1(port));
2862 #else
2863         value = 0;
2864         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO */
2865 #endif
2866         return 0;
2867 }
2868
2869 static int tegra186_hsic_phy_power_off(struct phy *phy)
2870 {
2871         struct tegra_padctl_uphy *padctl = phy_get_drvdata(phy);
2872         int port = hsic_phy_to_port(phy);
2873         u32 value;
2874
2875         TRACE_DEV(&phy->dev, "port %d\n", port);
2876         if (port < 0)
2877                 return port;
2878 #if 0
2879         value = padctl_readl(padctl, XUSB_PADCTL_HSIC_PADX_CTL1(port));
2880         value |= XUSB_PADCTL_HSIC_PAD_CTL1_PD_RX |
2881                  XUSB_PADCTL_HSIC_PAD_CTL1_PD_ZI |
2882                  XUSB_PADCTL_HSIC_PAD_CTL1_PD_TRX |
2883                  XUSB_PADCTL_HSIC_PAD_CTL1_PD_TX;
2884         padctl_writel(padctl, value, XUSB_PADCTL_HSIC_PADX_CTL1(port));
2885 #else
2886         value = 0;
2887         TRACE_DEV(&phy->dev, "FIXME: implement!"); /* TODO */
2888 #endif
2889         regulator_disable(padctl->vddio_hsic);
2890
2891         return 0;
2892 }
2893
2894 static int tegra186_hsic_phy_init(struct phy *phy)
2895 {
2896         TRACE_DEV(&phy->dev, "");
2897         return tegra_xusb_phy_init(phy);
2898 }
2899
2900 static int tegra186_hsic_phy_exit(struct phy *phy)
2901 {
2902         TRACE_DEV(&phy->dev, "");
2903         return tegra_xusb_phy_exit(phy);
2904 }
2905
2906 static const struct phy_ops hsic_phy_ops = {
2907         .init = tegra186_hsic_phy_init,
2908         .exit = tegra186_hsic_phy_exit,
2909         .power_on = tegra186_hsic_phy_power_on,
2910         .power_off = tegra186_hsic_phy_power_off,
2911         .owner = THIS_MODULE,
2912 };
2913
2914 static struct phy *tegra186_padctl_uphy_xlate(struct device *dev,
2915                                            struct of_phandle_args *args)
2916 {
2917         struct tegra_padctl_uphy *padctl = dev_get_drvdata(dev);
2918         unsigned int index = args->args[0];
2919         unsigned int phy_index;
2920         struct phy *phy = NULL;
2921
2922         if (args->args_count <= 0)
2923                 return ERR_PTR(-EINVAL);
2924
2925         TRACE_DEV(dev, "index %d", index);
2926
2927         if ((index >= TEGRA_USB3_PADCTL_USB3_BASE) &&
2928                 (index < TEGRA_USB3_PADCTL_USB3_BASE + 16)) {
2929
2930                 phy_index = index - TEGRA_USB3_PADCTL_USB3_BASE;
2931                 if (phy_index < TEGRA_XUSB_USB3_PHYS)
2932                         phy = padctl->usb3_phys[phy_index];
2933
2934         } else if ((index >= TEGRA_USB3_PADCTL_UTMI_BASE) &&
2935                 (index < TEGRA_USB3_PADCTL_UTMI_BASE + 16)) {
2936
2937                 phy_index = index - TEGRA_USB3_PADCTL_UTMI_BASE;
2938                 if (phy_index < TEGRA_XUSB_UTMI_PHYS)
2939                         phy = padctl->utmi_phys[phy_index];
2940
2941         } else if ((index >= TEGRA_USB3_PADCTL_HSIC_BASE) &&
2942                 (index < TEGRA_USB3_PADCTL_HSIC_BASE + 16)) {
2943
2944                 phy_index = index - TEGRA_USB3_PADCTL_HSIC_BASE;
2945                 if (phy_index < TEGRA_XUSB_HSIC_PHYS)
2946                         phy = padctl->hsic_phys[phy_index];
2947
2948         } else if ((index >= TEGRA_USB3_PADCTL_PCIE_BASE) &&
2949                 (index < TEGRA_USB3_PADCTL_PCIE_BASE + 16)) {
2950
2951                 phy_index = index - TEGRA_USB3_PADCTL_PCIE_BASE;
2952                 if (phy_index < TEGRA_XUSB_PCIE_PHYS)
2953                         phy = padctl->pcie_phys[phy_index];
2954
2955         } else if ((index >= TEGRA_USB3_PADCTL_SATA_BASE) &&
2956                 (index < TEGRA_USB3_PADCTL_SATA_BASE + 16)) {
2957
2958                 phy_index = index - TEGRA_USB3_PADCTL_SATA_BASE;
2959                 if (phy_index < TEGRA_XUSB_SATA_PHYS)
2960                         phy = padctl->sata_phys[phy_index];
2961
2962         } else if ((index >= TEGRA_USB3_PADCTL_UFS_BASE) &&
2963                 (index < TEGRA_USB3_PADCTL_UFS_BASE + 16)) {
2964
2965                 phy_index = index - TEGRA_USB3_PADCTL_UFS_BASE;
2966                 if (phy_index < TEGRA_XUSB_UFS_PHYS)
2967                         phy = padctl->ufs_phys[phy_index];
2968
2969         }
2970
2971         return (phy) ? phy : ERR_PTR(-EINVAL);
2972 }
2973
2974 static const struct pinctrl_pin_desc tegra186_pins[] = {
2975         PINCTRL_PIN(PIN_OTG_0,  "otg-0"),
2976         PINCTRL_PIN(PIN_OTG_1,  "otg-1"),
2977         PINCTRL_PIN(PIN_OTG_2,  "otg-2"),
2978         PINCTRL_PIN(PIN_OTG_3,  "otg-3"),
2979         PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
2980         PINCTRL_PIN(PIN_HSIC_1, "hsic-1"),
2981         PINCTRL_PIN(PIN_UPHY_0, "uphy-lane-0"),
2982         PINCTRL_PIN(PIN_UPHY_1, "uphy-lane-1"),
2983         PINCTRL_PIN(PIN_UPHY_2, "uphy-lane-2"),
2984         PINCTRL_PIN(PIN_UPHY_3, "uphy-lane-3"),
2985         PINCTRL_PIN(PIN_UPHY_4, "uphy-lane-4"),
2986         PINCTRL_PIN(PIN_UPHY_5, "uphy-lane-5"),
2987 };
2988
2989
2990 static const char * const tegra186_hsic_groups[] = {
2991         "hsic-0",
2992         "hsic-1",
2993 };
2994
2995 static const char * const tegra186_hsic_plus_groups[] = {
2996         "hsic-0",
2997         "hsic-1",
2998 };
2999
3000 static const char * const tegra186_xusb_groups[] = {
3001         "otg-0",
3002         "otg-1",
3003         "otg-2",
3004         "otg-3",
3005 };
3006
3007 static const char * const tegra186_uart_groups[] = {
3008         "otg-0",
3009         "otg-1",
3010         "otg-2",
3011         "otg-3"
3012 };
3013
3014 static const char * const tegra186_pcie_groups[] = {
3015         "uphy-lane-0",
3016         "uphy-lane-1",
3017         "uphy-lane-2",
3018         "uphy-lane-3",
3019         "uphy-lane-4",
3020         "uphy-lane-5",
3021 };
3022
3023 static const char * const tegra186_usb3_groups[] = {
3024         "uphy-lane-0",
3025         "uphy-lane-1",
3026         "uphy-lane-2",
3027         "uphy-lane-3",
3028         "uphy-lane-4",
3029         "uphy-lane-5",
3030 };
3031
3032 static const char * const tegra186_sata_groups[] = {
3033         "uphy-lane-0",
3034         "uphy-lane-1",
3035         "uphy-lane-2",
3036         "uphy-lane-3",
3037         "uphy-lane-4",
3038         "uphy-lane-5",
3039 };
3040
3041 static const char * const tegra186_mphy_groups[] = {
3042         "uphy-lane-0",
3043         "uphy-lane-1",
3044         "uphy-lane-2",
3045         "uphy-lane-3",
3046         "uphy-lane-4",
3047         "uphy-lane-5",
3048 };
3049
3050 #define TEGRA186_FUNCTION(_name)                                        \
3051         {                                                               \
3052                 .name = #_name,                                         \
3053                 .num_groups = ARRAY_SIZE(tegra186_##_name##_groups),    \
3054                 .groups = tegra186_##_name##_groups,                    \
3055         }
3056
3057 static struct tegra_padctl_uphy_function tegra186_functions[] = {
3058         TEGRA186_FUNCTION(hsic),
3059         TEGRA186_FUNCTION(hsic_plus),
3060         TEGRA186_FUNCTION(xusb),
3061         TEGRA186_FUNCTION(uart),
3062         TEGRA186_FUNCTION(pcie),
3063         TEGRA186_FUNCTION(usb3),
3064         TEGRA186_FUNCTION(sata),
3065         TEGRA186_FUNCTION(mphy),
3066 };
3067
3068 static const unsigned int tegra186_otg_functions[] = {
3069         TEGRA186_FUNC_RSVD_0,
3070         TEGRA186_FUNC_XUSB,
3071         TEGRA186_FUNC_UART,
3072         TEGRA186_FUNC_RSVD_3
3073 };
3074
3075 static const unsigned int tegra186_hsic_functions[] = {
3076         TEGRA186_FUNC_HSIC,
3077         TEGRA186_FUNC_HSIC_PLUS,
3078 };
3079
3080 static const unsigned int tegra186_uphy_functions[] = {
3081         TEGRA186_FUNC_USB3,
3082         TEGRA186_FUNC_PCIE,
3083         TEGRA186_FUNC_SATA,
3084         TEGRA186_FUNC_MPHY,
3085 };
3086
3087 #define TEGRA186_LANE(_name, _offset, _shift, _mask, _iddq, _funcs)     \
3088         {                                                               \
3089                 .name = _name,                                          \
3090                 .offset = _offset,                                      \
3091                 .shift = _shift,                                        \
3092                 .mask = _mask,                                          \
3093                 .iddq = _iddq,                                          \
3094                 .num_funcs = ARRAY_SIZE(tegra186_##_funcs##_functions), \
3095                 .funcs = tegra186_##_funcs##_functions,                 \
3096         }
3097
3098 static const struct tegra_padctl_uphy_lane tegra186_lanes[] = {
3099         TEGRA186_LANE("otg-0",  0x004,  0, 0x3, 0, otg),
3100         TEGRA186_LANE("otg-1",  0x004,  2, 0x3, 0, otg),
3101         TEGRA186_LANE("otg-2",  0x004,  4, 0x3, 0, otg),
3102         TEGRA186_LANE("otg-3",  0x004,  6, 0x3, 0, otg),
3103         TEGRA186_LANE("hsic-0", 0x004, 20, 0x1, 0, hsic),
3104         TEGRA186_LANE("hsic-1", 0x004, 21, 0x1, 0, hsic),
3105         TEGRA186_LANE("uphy-lane-0", 0x284, 0, 0x3, 9, uphy),
3106         TEGRA186_LANE("uphy-lane-1", 0x284, 0, 0x3, 9, uphy),
3107         TEGRA186_LANE("uphy-lane-2", 0x284, 0, 0x3, 9, uphy),
3108         TEGRA186_LANE("uphy-lane-3", 0x284, 0, 0x3, 9, uphy),
3109         TEGRA186_LANE("uphy-lane-4", 0x284, 0, 0x3, 9, uphy),
3110         TEGRA186_LANE("uphy-lane-5", 0x284, 0, 0x3, 9, uphy),
3111 };
3112
3113 static const struct tegra_padctl_uphy_soc tegra186_soc = {
3114         .num_pins = ARRAY_SIZE(tegra186_pins),
3115         .pins = tegra186_pins,
3116         .num_functions = ARRAY_SIZE(tegra186_functions),
3117         .functions = tegra186_functions,
3118         .num_lanes = ARRAY_SIZE(tegra186_lanes),
3119         .lanes = tegra186_lanes,
3120         .hsic_port_offset = 6,
3121 };
3122
3123 static const struct of_device_id tegra_padctl_uphy_of_match[] = {
3124         { .compatible = "nvidia,tegra186-padctl-uphy", .data = &tegra186_soc },
3125         { }
3126 };
3127 MODULE_DEVICE_TABLE(of, tegra_padctl_uphy_of_match);
3128
3129 static int tegra_xusb_read_fuse_calibration(struct tegra_padctl_uphy *padctl)
3130 {
3131         unsigned int i;
3132         u32 value;
3133
3134         value = tegra_fuse_readl(FUSE_SKU_USB_CALIB_0);
3135         dev_info(padctl->dev, "FUSE_SKU_USB_CALIB_0 0x%x\n", value);
3136         for (i = 0; i < TEGRA_XUSB_UTMI_PHYS; i++) {
3137                 padctl->calib.hs_curr_level[i] =
3138                         (value >> USB_CALIB_HS_CURR_LEVEL_PADX_SHIFT(i)) &
3139                         USB_CALIB_HS_CURR_LEVEL_PAD_MASK;
3140         }
3141         padctl->calib.hs_term_range_adj =
3142                 (value >> USB_CALIB_HS_TERM_RANGE_ADJ_SHIFT) &
3143                 USB_CALIB_HS_TERM_RANGE_ADJ_MASK;
3144
3145         value = tegra_fuse_readl(FUSE_USB_CALIB_EXT_0);
3146         dev_info(padctl->dev, "FUSE_USB_CALIB_EXT_0 0x%x\n", value);
3147         padctl->calib.rpd_ctrl =
3148                 (value >> USB_CALIB_EXT_RPD_CTRL_SHIFT) &
3149                 USB_CALIB_EXT_RPD_CTRL_MASK;
3150
3151         return 0;
3152 }
3153
3154 static int tegra_mphy_stata_fuse_calibration(struct tegra_padctl_uphy *padctl)
3155 {
3156         u32 value;
3157
3158         value = tegra_fuse_readl(FUSE_SATA_MPHY_ODM_CALIB_0);
3159         padctl->fuse_calib.sata_mphy_odm = value;
3160
3161         value = tegra_fuse_readl(FUSE_SATA_NV_CALIB_0);
3162         padctl->fuse_calib.sata_nv = value;
3163
3164         value = tegra_fuse_readl(FUSE_MPHY_NV_CALIB_0);
3165         padctl->fuse_calib.mphy_nv = value;
3166
3167         return 0;
3168 }
3169
3170 static int tegra_xusb_setup_usb(struct tegra_padctl_uphy *ctx)
3171 {
3172         struct phy *phy;
3173         unsigned int i;
3174
3175         for (i = 0; i < TEGRA_XUSB_USB3_PHYS; i++) {
3176                 phy = devm_phy_create(ctx->dev, &usb3_phy_ops, NULL);
3177                 TRACE("SS P%d phy %p\n", i, phy);
3178                 if (IS_ERR(phy))
3179                         return PTR_ERR(phy);
3180
3181                 ctx->usb3_phys[i] = phy;
3182                 phy_set_drvdata(phy, ctx);
3183         }
3184
3185         for (i = 0; i < TEGRA_XUSB_UTMI_PHYS; i++) {
3186                 char reg_name[sizeof("vbus-N")];
3187
3188                 sprintf(reg_name, "vbus-%d", i);
3189                 ctx->vbus[i] = devm_regulator_get(ctx->dev, reg_name);
3190                 TRACE("UTMI P%d vbus %p IS_ERR %ld PTR_ERR %ld\n", i, ctx->vbus[i], IS_ERR(ctx->vbus[i]), PTR_ERR(ctx->vbus[i]));
3191                 if (IS_ERR(ctx->vbus[i]))
3192                         return PTR_ERR(ctx->vbus[i]);
3193
3194                 phy = devm_phy_create(ctx->dev, &utmi_phy_ops, NULL);
3195                 TRACE("UTMI P%d phy %p IS_ERR %ld PTR_ERR %ld\n", i, phy, IS_ERR(phy), PTR_ERR(phy));
3196                 if (IS_ERR(phy))
3197                         return PTR_ERR(phy);
3198
3199                 ctx->utmi_phys[i] = phy;
3200                 phy_set_drvdata(phy, ctx);
3201         }
3202
3203         ctx->vddio_hsic = devm_regulator_get(ctx->dev, "vddio_hsic");
3204         if (IS_ERR(ctx->vddio_hsic))
3205                 return PTR_ERR(ctx->vddio_hsic);
3206
3207         for (i = 0; i < TEGRA_XUSB_HSIC_PHYS; i++) {
3208                 phy = devm_phy_create(ctx->dev, &hsic_phy_ops, NULL);
3209                 TRACE("HSIC P%d phy %p\n", i, phy);
3210                 if (IS_ERR(phy))
3211                         return PTR_ERR(phy);
3212
3213                 ctx->hsic_phys[i] = phy;
3214                 phy_set_drvdata(phy, ctx);
3215         }
3216
3217         return 0;
3218 }
3219
3220 #define DEBUG
3221 #ifdef DEBUG
3222 #define reg_dump(_dev, _base, _reg) \
3223         dev_info(_dev, "%s @%x = 0x%x\n", #_reg, _reg, ioread32(_base + _reg));
3224 #else
3225 #define reg_dump(_dev, _base, _reg)     do {} while (0)
3226 #endif
3227
3228 static void __iomem *car_base;
3229 static void tegra186_fpga_hacks_init(struct platform_device *pdev)
3230 {
3231 #if defined(CONFIG_ARCH_TEGRA_18x_SOC)
3232         car_base = devm_ioremap(&pdev->dev, 0x05000000, 0x1000000);
3233 #endif
3234         if (!car_base)
3235                 dev_err(&pdev->dev, "failed to map CAR mmio\n");
3236 }
3237
3238 static
3239 void tegra186_fpga_hacks_uphy_reset(struct platform_device *pdev, bool on)
3240 {
3241         int reg;
3242         if (!car_base) {
3243                 dev_err(&pdev->dev, "CAR mmio is not mapped\n");
3244                 return;
3245         }
3246
3247         reg_dump(&pdev->dev, car_base, CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
3248         reg = ioread32(car_base + CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
3249         if (on)
3250                 reg |= SWR_UPHY_RST;
3251         else
3252                 reg &= ~SWR_UPHY_RST;
3253         iowrite32(reg, car_base + CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
3254         reg_dump(&pdev->dev, car_base, CLK_RST_CONTROLLER_RST_DEV_UPHY_0);
3255 }
3256
3257 static int tegra186_padctl_uphy_probe(struct platform_device *pdev)
3258 {
3259         struct tegra_padctl_uphy *ctx;
3260         const struct of_device_id *match;
3261         struct device_node *np = pdev->dev.of_node;
3262         struct resource *res;
3263         struct phy *phy;
3264         int err;
3265
3266         TRACE();
3267         ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
3268         if (!ctx)
3269                 return -ENOMEM;
3270
3271         TRACE();
3272         platform_set_drvdata(pdev, ctx);
3273         mutex_init(&ctx->lock);
3274         ctx->dev = &pdev->dev;
3275
3276         TRACE();
3277         match = of_match_node(tegra_padctl_uphy_of_match, pdev->dev.of_node);
3278         ctx->soc = match->data;
3279
3280         TRACE();
3281
3282
3283         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3284         ctx->padctl_regs = devm_ioremap_resource(&pdev->dev, res);
3285         TRACE_DEV(&pdev->dev, "padctl mmio start %pa end %pa\n", &res->start, &res->end);
3286         if (IS_ERR(ctx->padctl_regs))
3287                 return PTR_ERR(ctx->padctl_regs);
3288
3289         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3290         ctx->uphy_regs = devm_ioremap_resource(&pdev->dev, res);
3291         TRACE_DEV(&pdev->dev, "uphy mmio start %pa end %pa\n", &res->start, &res->end);
3292         if (IS_ERR(ctx->uphy_regs))
3293                 return PTR_ERR(ctx->uphy_regs);
3294
3295         ctx->uphy_pll_regs[0] = ctx->uphy_regs;
3296         ctx->uphy_lane_regs[0] = ctx->uphy_regs + 0x20000;
3297         ctx->uphy_lane_regs[1] = ctx->uphy_regs + 0x30000;
3298         ctx->uphy_lane_regs[2] = ctx->uphy_regs + 0x40000;
3299         ctx->uphy_lane_regs[3] = ctx->uphy_regs + 0x50000;
3300         ctx->uphy_lane_regs[4] = ctx->uphy_regs + 0x60000;
3301         ctx->uphy_lane_regs[5] = ctx->uphy_regs + 0x70000;
3302         ctx->uphy_pll_regs[1] = ctx->uphy_regs + 0x80000;
3303
3304         TRACE();
3305         if (tegra_platform_is_silicon()) {
3306                 err = tegra_xusb_read_fuse_calibration(ctx);
3307                 if (err < 0)
3308                         return err;
3309
3310                 err = tegra_mphy_stata_fuse_calibration(ctx);
3311                 if (err < 0)
3312                         return err;
3313         }
3314
3315         TRACE();
3316         if (tegra_platform_is_silicon()) {
3317                 /* TODO: check clock framework */
3318                 ctx->clk = devm_clk_get(ctx->dev, "uphy");
3319                 if (IS_ERR(ctx->clk)) {
3320                         dev_err(ctx->dev, "failed to get uphy pad clock\n");
3321                         return PTR_ERR(ctx->clk);
3322                 }
3323                 tegra_periph_reset_deassert(ctx->clk);
3324         }
3325
3326         if (tegra_platform_is_fpga()) {
3327                 tegra186_fpga_hacks_init(pdev);
3328                 tegra186_fpga_hacks_uphy_reset(pdev, false);
3329         }
3330
3331         TRACE();
3332         memset(&ctx->desc, 0, sizeof(ctx->desc));
3333         ctx->desc.name = dev_name(ctx->dev);
3334         ctx->desc.pins = ctx->soc->pins;
3335         ctx->desc.npins = ctx->soc->num_pins;
3336         ctx->desc.pctlops = &tegra_xusb_padctl_pinctrl_ops;
3337         ctx->desc.pmxops = &tegra186_padctl_uphy_pinmux_ops;
3338         ctx->desc.confops = &tegra_xusb_padctl_pinconf_ops;
3339         ctx->desc.owner = THIS_MODULE;
3340
3341         ctx->pinctrl = pinctrl_register(&ctx->desc, &pdev->dev, ctx);
3342         if (!ctx->pinctrl) {
3343                 dev_err(&pdev->dev, "failed to register pincontrol\n");
3344                 err = -ENODEV;
3345                 goto assert_clk_reset;
3346         }
3347
3348         TRACE();
3349         phy = devm_phy_create(&pdev->dev, &pcie_phy_ops, NULL);
3350         if (IS_ERR(phy)) {
3351                 err = PTR_ERR(phy);
3352                 goto unregister;
3353         }
3354
3355         ctx->pcie_phys[0] = phy;
3356         phy_set_drvdata(phy, ctx);
3357
3358         TRACE();
3359         phy = devm_phy_create(&pdev->dev, &sata_phy_ops, NULL);
3360         if (IS_ERR(phy)) {
3361                 err = PTR_ERR(phy);
3362                 goto unregister;
3363         }
3364
3365         ctx->sata_phys[0] = phy;
3366         phy_set_drvdata(phy, ctx);
3367
3368         TRACE();
3369         phy = devm_phy_create(&pdev->dev, &ufs_phy_ops, NULL);
3370         if (IS_ERR(phy)) {
3371                 err = PTR_ERR(phy);
3372                 goto unregister;
3373         }
3374
3375         ctx->ufs_phys[0] = phy;
3376         phy_set_drvdata(phy, ctx);
3377
3378                 TRACE();
3379                 err = tegra_xusb_setup_usb(ctx);
3380                 TRACE("err %d\n", err);
3381                 if (err)
3382                         goto unregister;
3383
3384         TRACE();
3385         ctx->provider = devm_of_phy_provider_register(&pdev->dev,
3386                                         tegra186_padctl_uphy_xlate);
3387         if (IS_ERR(ctx->provider)) {
3388                 dev_err(&pdev->dev, "failed to register PHYs: %d\n", err);
3389                 goto unregister;
3390         }
3391
3392         TRACE();
3393
3394         ctx->prod_list = tegra_prod_get(&pdev->dev, NULL);
3395         if (IS_ERR_OR_NULL(ctx->prod_list)) {
3396                 dev_warn(&pdev->dev, "uphy: prod list not found %ld\n",
3397                         PTR_ERR(ctx->prod_list));
3398                 ctx->prod_list = NULL;
3399         }
3400
3401         ctx->sata_bypass_fuse =
3402                 of_property_read_bool(np, "nvidia,sata-use-prods");
3403
3404         return 0;
3405
3406 unregister:
3407         TRACE();
3408         pinctrl_unregister(ctx->pinctrl);
3409 assert_clk_reset:
3410         if (tegra_platform_is_silicon())
3411                 tegra_periph_reset_assert(ctx->clk);
3412         return err;
3413 }
3414
3415 static int tegra186_padctl_uphy_remove(struct platform_device *pdev)
3416 {
3417         struct tegra_padctl_uphy *ctx = platform_get_drvdata(pdev);
3418         if (ctx->prod_list)
3419                 tegra_prod_release(&ctx->prod_list);
3420
3421         pinctrl_unregister(ctx->pinctrl);
3422         tegra_periph_reset_assert(ctx->clk);
3423         return 0;
3424 }
3425
3426 static struct platform_driver tegra186_padctl_uphy_driver = {
3427         .driver = {
3428                 .name = "tegra186-padctl-uphy",
3429                 .of_match_table = tegra_padctl_uphy_of_match,
3430         },
3431         .probe = tegra186_padctl_uphy_probe,
3432         .remove = tegra186_padctl_uphy_remove,
3433 };
3434 module_platform_driver(tegra186_padctl_uphy_driver);
3435
3436 MODULE_AUTHOR("JC Kuo <jckuo@nvidia.com>");
3437 MODULE_DESCRIPTION("Tegra 186 XUSB Pad Control and UPHY driver");
3438 MODULE_LICENSE("GPL v2");