xref: /linux/drivers/usb/gadget/udc/tegra-xudc.c (revision e3966940559d52aa1800a008dcfeec218dd31f88)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * NVIDIA Tegra XUSB device mode controller
4  *
5  * Copyright (c) 2013-2022, NVIDIA CORPORATION.  All rights reserved.
6  * Copyright (c) 2015, Google Inc.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/completion.h>
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/interrupt.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/phy/phy.h>
20 #include <linux/phy/tegra/xusb.h>
21 #include <linux/pm_domain.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/reset.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
28 #include <linux/usb/otg.h>
29 #include <linux/usb/role.h>
30 #include <linux/usb/phy.h>
31 #include <linux/workqueue.h>
32 
33 /* XUSB_DEV registers */
34 #define DB 0x004
35 #define  DB_TARGET_MASK GENMASK(15, 8)
36 #define  DB_TARGET(x) (((x) << 8) & DB_TARGET_MASK)
37 #define  DB_STREAMID_MASK GENMASK(31, 16)
38 #define  DB_STREAMID(x) (((x) << 16) & DB_STREAMID_MASK)
39 #define ERSTSZ 0x008
40 #define  ERSTSZ_ERSTXSZ_SHIFT(x) ((x) * 16)
41 #define  ERSTSZ_ERSTXSZ_MASK GENMASK(15, 0)
42 #define ERSTXBALO(x) (0x010 + 8 * (x))
43 #define ERSTXBAHI(x) (0x014 + 8 * (x))
44 #define ERDPLO 0x020
45 #define  ERDPLO_EHB BIT(3)
46 #define ERDPHI 0x024
47 #define EREPLO 0x028
48 #define  EREPLO_ECS BIT(0)
49 #define  EREPLO_SEGI BIT(1)
50 #define EREPHI 0x02c
51 #define CTRL 0x030
52 #define  CTRL_RUN BIT(0)
53 #define  CTRL_LSE BIT(1)
54 #define  CTRL_IE BIT(4)
55 #define  CTRL_SMI_EVT BIT(5)
56 #define  CTRL_SMI_DSE BIT(6)
57 #define  CTRL_EWE BIT(7)
58 #define  CTRL_DEVADDR_MASK GENMASK(30, 24)
59 #define  CTRL_DEVADDR(x) (((x) << 24) & CTRL_DEVADDR_MASK)
60 #define  CTRL_ENABLE BIT(31)
61 #define ST 0x034
62 #define  ST_RC BIT(0)
63 #define  ST_IP BIT(4)
64 #define RT_IMOD	0x038
65 #define  RT_IMOD_IMODI_MASK GENMASK(15, 0)
66 #define  RT_IMOD_IMODI(x) ((x) & RT_IMOD_IMODI_MASK)
67 #define  RT_IMOD_IMODC_MASK GENMASK(31, 16)
68 #define  RT_IMOD_IMODC(x) (((x) << 16) & RT_IMOD_IMODC_MASK)
69 #define PORTSC 0x03c
70 #define  PORTSC_CCS BIT(0)
71 #define  PORTSC_PED BIT(1)
72 #define  PORTSC_PR BIT(4)
73 #define  PORTSC_PLS_SHIFT 5
74 #define  PORTSC_PLS_MASK GENMASK(8, 5)
75 #define  PORTSC_PLS_U0 0x0
76 #define  PORTSC_PLS_U2 0x2
77 #define  PORTSC_PLS_U3 0x3
78 #define  PORTSC_PLS_DISABLED 0x4
79 #define  PORTSC_PLS_RXDETECT 0x5
80 #define  PORTSC_PLS_INACTIVE 0x6
81 #define  PORTSC_PLS_RESUME 0xf
82 #define  PORTSC_PLS(x) (((x) << PORTSC_PLS_SHIFT) & PORTSC_PLS_MASK)
83 #define  PORTSC_PS_SHIFT 10
84 #define  PORTSC_PS_MASK GENMASK(13, 10)
85 #define  PORTSC_PS_UNDEFINED 0x0
86 #define  PORTSC_PS_FS 0x1
87 #define  PORTSC_PS_LS 0x2
88 #define  PORTSC_PS_HS 0x3
89 #define  PORTSC_PS_SS 0x4
90 #define  PORTSC_LWS BIT(16)
91 #define  PORTSC_CSC BIT(17)
92 #define  PORTSC_WRC BIT(19)
93 #define  PORTSC_PRC BIT(21)
94 #define  PORTSC_PLC BIT(22)
95 #define  PORTSC_CEC BIT(23)
96 #define  PORTSC_WPR BIT(30)
97 #define  PORTSC_CHANGE_MASK (PORTSC_CSC | PORTSC_WRC | PORTSC_PRC | \
98 			     PORTSC_PLC | PORTSC_CEC)
99 #define ECPLO 0x040
100 #define ECPHI 0x044
101 #define MFINDEX 0x048
102 #define  MFINDEX_FRAME_SHIFT 3
103 #define  MFINDEX_FRAME_MASK GENMASK(13, 3)
104 #define PORTPM 0x04c
105 #define  PORTPM_L1S_MASK GENMASK(1, 0)
106 #define  PORTPM_L1S_DROP 0x0
107 #define  PORTPM_L1S_ACCEPT 0x1
108 #define  PORTPM_L1S_NYET 0x2
109 #define  PORTPM_L1S_STALL 0x3
110 #define  PORTPM_L1S(x) ((x) & PORTPM_L1S_MASK)
111 #define  PORTPM_RWE BIT(3)
112 #define  PORTPM_U2TIMEOUT_MASK GENMASK(15, 8)
113 #define  PORTPM_U1TIMEOUT_MASK GENMASK(23, 16)
114 #define  PORTPM_FLA BIT(24)
115 #define  PORTPM_VBA BIT(25)
116 #define  PORTPM_WOC BIT(26)
117 #define  PORTPM_WOD BIT(27)
118 #define  PORTPM_U1E BIT(28)
119 #define  PORTPM_U2E BIT(29)
120 #define  PORTPM_FRWE BIT(30)
121 #define  PORTPM_PNG_CYA BIT(31)
122 #define EP_HALT 0x050
123 #define EP_PAUSE 0x054
124 #define EP_RELOAD 0x058
125 #define EP_STCHG 0x05c
126 #define DEVNOTIF_LO 0x064
127 #define  DEVNOTIF_LO_TRIG BIT(0)
128 #define  DEVNOTIF_LO_TYPE_MASK GENMASK(7, 4)
129 #define  DEVNOTIF_LO_TYPE(x) (((x) << 4)  & DEVNOTIF_LO_TYPE_MASK)
130 #define  DEVNOTIF_LO_TYPE_FUNCTION_WAKE 0x1
131 #define DEVNOTIF_HI 0x068
132 #define PORTHALT 0x06c
133 #define  PORTHALT_HALT_LTSSM BIT(0)
134 #define  PORTHALT_HALT_REJECT BIT(1)
135 #define  PORTHALT_STCHG_REQ BIT(20)
136 #define  PORTHALT_STCHG_INTR_EN BIT(24)
137 #define PORT_TM	0x070
138 #define EP_THREAD_ACTIVE 0x074
139 #define EP_STOPPED 0x078
140 #define HSFSPI_COUNT0 0x100
141 #define HSFSPI_COUNT13 0x134
142 #define  HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK GENMASK(29, 0)
143 #define  HSFSPI_COUNT13_U2_RESUME_K_DURATION(x) ((x) & \
144 				HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK)
145 #define BLCG 0x840
146 #define SSPX_CORE_CNT0 0x610
147 #define  SSPX_CORE_CNT0_PING_TBURST_MASK GENMASK(7, 0)
148 #define  SSPX_CORE_CNT0_PING_TBURST(x) ((x) & SSPX_CORE_CNT0_PING_TBURST_MASK)
149 #define SSPX_CORE_CNT30 0x688
150 #define  SSPX_CORE_CNT30_LMPITP_TIMER_MASK GENMASK(19, 0)
151 #define  SSPX_CORE_CNT30_LMPITP_TIMER(x) ((x) & \
152 					SSPX_CORE_CNT30_LMPITP_TIMER_MASK)
153 #define SSPX_CORE_CNT32 0x690
154 #define  SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK GENMASK(7, 0)
155 #define  SSPX_CORE_CNT32_POLL_TBURST_MAX(x) ((x) & \
156 					SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK)
157 #define SSPX_CORE_CNT56 0x6fc
158 #define  SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK GENMASK(19, 0)
159 #define  SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(x) ((x) & \
160 				SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK)
161 #define SSPX_CORE_CNT57 0x700
162 #define  SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK GENMASK(19, 0)
163 #define  SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(x) ((x) & \
164 				SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK)
165 #define SSPX_CORE_CNT65 0x720
166 #define  SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK GENMASK(19, 0)
167 #define  SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(x) ((x) & \
168 				SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK)
169 #define SSPX_CORE_CNT66 0x724
170 #define  SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK GENMASK(19, 0)
171 #define  SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(x) ((x) & \
172 				SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK)
173 #define SSPX_CORE_CNT67 0x728
174 #define  SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK GENMASK(19, 0)
175 #define  SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(x) ((x) & \
176 				SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK)
177 #define SSPX_CORE_CNT72 0x73c
178 #define  SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK GENMASK(19, 0)
179 #define  SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(x) ((x) & \
180 				SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK)
181 #define SSPX_CORE_PADCTL4 0x750
182 #define  SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK GENMASK(19, 0)
183 #define  SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(x) ((x) & \
184 				SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK)
185 #define  BLCG_DFPCI BIT(0)
186 #define  BLCG_UFPCI BIT(1)
187 #define  BLCG_FE BIT(2)
188 #define  BLCG_COREPLL_PWRDN BIT(8)
189 #define  BLCG_IOPLL_0_PWRDN BIT(9)
190 #define  BLCG_IOPLL_1_PWRDN BIT(10)
191 #define  BLCG_IOPLL_2_PWRDN BIT(11)
192 #define  BLCG_ALL 0x1ff
193 #define CFG_DEV_SSPI_XFER 0x858
194 #define  CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK GENMASK(31, 0)
195 #define  CFG_DEV_SSPI_XFER_ACKTIMEOUT(x) ((x) & \
196 					CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK)
197 #define CFG_DEV_FE 0x85c
198 #define  CFG_DEV_FE_PORTREGSEL_MASK GENMASK(1, 0)
199 #define  CFG_DEV_FE_PORTREGSEL_SS_PI 1
200 #define  CFG_DEV_FE_PORTREGSEL_HSFS_PI 2
201 #define  CFG_DEV_FE_PORTREGSEL(x) ((x) & CFG_DEV_FE_PORTREGSEL_MASK)
202 #define  CFG_DEV_FE_INFINITE_SS_RETRY BIT(29)
203 
204 /* FPCI registers */
205 #define XUSB_DEV_CFG_1 0x004
206 #define  XUSB_DEV_CFG_1_IO_SPACE_EN BIT(0)
207 #define  XUSB_DEV_CFG_1_MEMORY_SPACE_EN BIT(1)
208 #define  XUSB_DEV_CFG_1_BUS_MASTER_EN BIT(2)
209 #define XUSB_DEV_CFG_4 0x010
210 #define  XUSB_DEV_CFG_4_BASE_ADDR_MASK GENMASK(31, 15)
211 #define XUSB_DEV_CFG_5 0x014
212 
213 /* IPFS registers */
214 #define XUSB_DEV_CONFIGURATION_0 0x180
215 #define  XUSB_DEV_CONFIGURATION_0_EN_FPCI BIT(0)
216 #define XUSB_DEV_INTR_MASK_0 0x188
217 #define  XUSB_DEV_INTR_MASK_0_IP_INT_MASK BIT(16)
218 
219 struct tegra_xudc_ep_context {
220 	__le32 info0;
221 	__le32 info1;
222 	__le32 deq_lo;
223 	__le32 deq_hi;
224 	__le32 tx_info;
225 	__le32 rsvd[11];
226 };
227 
228 #define EP_STATE_DISABLED 0
229 #define EP_STATE_RUNNING 1
230 #define EP_STATE_HALTED 2
231 #define EP_STATE_STOPPED 3
232 #define EP_STATE_ERROR 4
233 
234 #define EP_TYPE_INVALID 0
235 #define EP_TYPE_ISOCH_OUT 1
236 #define EP_TYPE_BULK_OUT 2
237 #define EP_TYPE_INTERRUPT_OUT 3
238 #define EP_TYPE_CONTROL 4
239 #define EP_TYPE_ISCOH_IN 5
240 #define EP_TYPE_BULK_IN 6
241 #define EP_TYPE_INTERRUPT_IN 7
242 
243 #define BUILD_EP_CONTEXT_RW(name, member, shift, mask)			\
244 static inline u32 ep_ctx_read_##name(struct tegra_xudc_ep_context *ctx)	\
245 {									\
246 	return (le32_to_cpu(ctx->member) >> (shift)) & (mask);		\
247 }									\
248 static inline void							\
249 ep_ctx_write_##name(struct tegra_xudc_ep_context *ctx, u32 val)		\
250 {									\
251 	u32 tmp;							\
252 									\
253 	tmp = le32_to_cpu(ctx->member) & ~((mask) << (shift));		\
254 	tmp |= (val & (mask)) << (shift);				\
255 	ctx->member = cpu_to_le32(tmp);					\
256 }
257 
258 BUILD_EP_CONTEXT_RW(state, info0, 0, 0x7)
259 BUILD_EP_CONTEXT_RW(mult, info0, 8, 0x3)
260 BUILD_EP_CONTEXT_RW(max_pstreams, info0, 10, 0x1f)
261 BUILD_EP_CONTEXT_RW(lsa, info0, 15, 0x1)
262 BUILD_EP_CONTEXT_RW(interval, info0, 16, 0xff)
263 BUILD_EP_CONTEXT_RW(cerr, info1, 1, 0x3)
264 BUILD_EP_CONTEXT_RW(type, info1, 3, 0x7)
265 BUILD_EP_CONTEXT_RW(hid, info1, 7, 0x1)
266 BUILD_EP_CONTEXT_RW(max_burst_size, info1, 8, 0xff)
267 BUILD_EP_CONTEXT_RW(max_packet_size, info1, 16, 0xffff)
268 BUILD_EP_CONTEXT_RW(dcs, deq_lo, 0, 0x1)
269 BUILD_EP_CONTEXT_RW(deq_lo, deq_lo, 4, 0xfffffff)
270 BUILD_EP_CONTEXT_RW(deq_hi, deq_hi, 0, 0xffffffff)
271 BUILD_EP_CONTEXT_RW(avg_trb_len, tx_info, 0, 0xffff)
272 BUILD_EP_CONTEXT_RW(max_esit_payload, tx_info, 16, 0xffff)
273 BUILD_EP_CONTEXT_RW(edtla, rsvd[0], 0, 0xffffff)
274 BUILD_EP_CONTEXT_RW(rsvd, rsvd[0], 24, 0x1)
275 BUILD_EP_CONTEXT_RW(partial_td, rsvd[0], 25, 0x1)
276 BUILD_EP_CONTEXT_RW(splitxstate, rsvd[0], 26, 0x1)
277 BUILD_EP_CONTEXT_RW(seq_num, rsvd[0], 27, 0x1f)
278 BUILD_EP_CONTEXT_RW(cerrcnt, rsvd[1], 18, 0x3)
279 BUILD_EP_CONTEXT_RW(data_offset, rsvd[2], 0, 0x1ffff)
280 BUILD_EP_CONTEXT_RW(numtrbs, rsvd[2], 22, 0x1f)
281 BUILD_EP_CONTEXT_RW(devaddr, rsvd[6], 0, 0x7f)
282 
283 static inline u64 ep_ctx_read_deq_ptr(struct tegra_xudc_ep_context *ctx)
284 {
285 	return ((u64)ep_ctx_read_deq_hi(ctx) << 32) |
286 		(ep_ctx_read_deq_lo(ctx) << 4);
287 }
288 
289 static inline void
290 ep_ctx_write_deq_ptr(struct tegra_xudc_ep_context *ctx, u64 addr)
291 {
292 	ep_ctx_write_deq_lo(ctx, lower_32_bits(addr) >> 4);
293 	ep_ctx_write_deq_hi(ctx, upper_32_bits(addr));
294 }
295 
296 struct tegra_xudc_trb {
297 	__le32 data_lo;
298 	__le32 data_hi;
299 	__le32 status;
300 	__le32 control;
301 };
302 
303 #define TRB_TYPE_RSVD 0
304 #define TRB_TYPE_NORMAL 1
305 #define TRB_TYPE_SETUP_STAGE 2
306 #define TRB_TYPE_DATA_STAGE 3
307 #define TRB_TYPE_STATUS_STAGE 4
308 #define TRB_TYPE_ISOCH 5
309 #define TRB_TYPE_LINK 6
310 #define TRB_TYPE_TRANSFER_EVENT 32
311 #define TRB_TYPE_PORT_STATUS_CHANGE_EVENT 34
312 #define TRB_TYPE_STREAM 48
313 #define TRB_TYPE_SETUP_PACKET_EVENT 63
314 
315 #define TRB_CMPL_CODE_INVALID 0
316 #define TRB_CMPL_CODE_SUCCESS 1
317 #define TRB_CMPL_CODE_DATA_BUFFER_ERR 2
318 #define TRB_CMPL_CODE_BABBLE_DETECTED_ERR 3
319 #define TRB_CMPL_CODE_USB_TRANS_ERR 4
320 #define TRB_CMPL_CODE_TRB_ERR 5
321 #define TRB_CMPL_CODE_STALL 6
322 #define TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR 10
323 #define TRB_CMPL_CODE_SHORT_PACKET 13
324 #define TRB_CMPL_CODE_RING_UNDERRUN 14
325 #define TRB_CMPL_CODE_RING_OVERRUN 15
326 #define TRB_CMPL_CODE_EVENT_RING_FULL_ERR 21
327 #define TRB_CMPL_CODE_STOPPED 26
328 #define TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN 31
329 #define TRB_CMPL_CODE_STREAM_NUMP_ERROR 219
330 #define TRB_CMPL_CODE_PRIME_PIPE_RECEIVED 220
331 #define TRB_CMPL_CODE_HOST_REJECTED 221
332 #define TRB_CMPL_CODE_CTRL_DIR_ERR 222
333 #define TRB_CMPL_CODE_CTRL_SEQNUM_ERR 223
334 
335 #define BUILD_TRB_RW(name, member, shift, mask)				\
336 static inline u32 trb_read_##name(struct tegra_xudc_trb *trb)		\
337 {									\
338 	return (le32_to_cpu(trb->member) >> (shift)) & (mask);		\
339 }									\
340 static inline void							\
341 trb_write_##name(struct tegra_xudc_trb *trb, u32 val)			\
342 {									\
343 	u32 tmp;							\
344 									\
345 	tmp = le32_to_cpu(trb->member) & ~((mask) << (shift));		\
346 	tmp |= (val & (mask)) << (shift);				\
347 	trb->member = cpu_to_le32(tmp);					\
348 }
349 
350 BUILD_TRB_RW(data_lo, data_lo, 0, 0xffffffff)
351 BUILD_TRB_RW(data_hi, data_hi, 0, 0xffffffff)
352 BUILD_TRB_RW(seq_num, status, 0, 0xffff)
353 BUILD_TRB_RW(transfer_len, status, 0, 0xffffff)
354 BUILD_TRB_RW(td_size, status, 17, 0x1f)
355 BUILD_TRB_RW(cmpl_code, status, 24, 0xff)
356 BUILD_TRB_RW(cycle, control, 0, 0x1)
357 BUILD_TRB_RW(toggle_cycle, control, 1, 0x1)
358 BUILD_TRB_RW(isp, control, 2, 0x1)
359 BUILD_TRB_RW(chain, control, 4, 0x1)
360 BUILD_TRB_RW(ioc, control, 5, 0x1)
361 BUILD_TRB_RW(type, control, 10, 0x3f)
362 BUILD_TRB_RW(stream_id, control, 16, 0xffff)
363 BUILD_TRB_RW(endpoint_id, control, 16, 0x1f)
364 BUILD_TRB_RW(tlbpc, control, 16, 0xf)
365 BUILD_TRB_RW(data_stage_dir, control, 16, 0x1)
366 BUILD_TRB_RW(frame_id, control, 20, 0x7ff)
367 BUILD_TRB_RW(sia, control, 31, 0x1)
368 
369 static inline u64 trb_read_data_ptr(struct tegra_xudc_trb *trb)
370 {
371 	return ((u64)trb_read_data_hi(trb) << 32) |
372 		trb_read_data_lo(trb);
373 }
374 
375 static inline void trb_write_data_ptr(struct tegra_xudc_trb *trb, u64 addr)
376 {
377 	trb_write_data_lo(trb, lower_32_bits(addr));
378 	trb_write_data_hi(trb, upper_32_bits(addr));
379 }
380 
381 struct tegra_xudc_request {
382 	struct usb_request usb_req;
383 
384 	size_t buf_queued;
385 	unsigned int trbs_queued;
386 	unsigned int trbs_needed;
387 	bool need_zlp;
388 
389 	struct tegra_xudc_trb *first_trb;
390 	struct tegra_xudc_trb *last_trb;
391 
392 	struct list_head list;
393 };
394 
395 struct tegra_xudc_ep {
396 	struct tegra_xudc *xudc;
397 	struct usb_ep usb_ep;
398 	unsigned int index;
399 	char name[8];
400 
401 	struct tegra_xudc_ep_context *context;
402 
403 #define XUDC_TRANSFER_RING_SIZE 64
404 	struct tegra_xudc_trb *transfer_ring;
405 	dma_addr_t transfer_ring_phys;
406 
407 	unsigned int enq_ptr;
408 	unsigned int deq_ptr;
409 	bool pcs;
410 	bool ring_full;
411 	bool stream_rejected;
412 
413 	struct list_head queue;
414 	const struct usb_endpoint_descriptor *desc;
415 	const struct usb_ss_ep_comp_descriptor *comp_desc;
416 };
417 
418 struct tegra_xudc_sel_timing {
419 	__u8 u1sel;
420 	__u8 u1pel;
421 	__le16 u2sel;
422 	__le16 u2pel;
423 };
424 
425 enum tegra_xudc_setup_state {
426 	WAIT_FOR_SETUP,
427 	DATA_STAGE_XFER,
428 	DATA_STAGE_RECV,
429 	STATUS_STAGE_XFER,
430 	STATUS_STAGE_RECV,
431 };
432 
433 struct tegra_xudc_setup_packet {
434 	struct usb_ctrlrequest ctrl_req;
435 	unsigned int seq_num;
436 };
437 
438 struct tegra_xudc_save_regs {
439 	u32 ctrl;
440 	u32 portpm;
441 };
442 
443 struct tegra_xudc {
444 	struct device *dev;
445 	const struct tegra_xudc_soc *soc;
446 	struct tegra_xusb_padctl *padctl;
447 
448 	spinlock_t lock;
449 
450 	struct usb_gadget gadget;
451 	struct usb_gadget_driver *driver;
452 
453 #define XUDC_NR_EVENT_RINGS 2
454 #define XUDC_EVENT_RING_SIZE 4096
455 	struct tegra_xudc_trb *event_ring[XUDC_NR_EVENT_RINGS];
456 	dma_addr_t event_ring_phys[XUDC_NR_EVENT_RINGS];
457 	unsigned int event_ring_index;
458 	unsigned int event_ring_deq_ptr;
459 	bool ccs;
460 
461 #define XUDC_NR_EPS 32
462 	struct tegra_xudc_ep ep[XUDC_NR_EPS];
463 	struct tegra_xudc_ep_context *ep_context;
464 	dma_addr_t ep_context_phys;
465 
466 	struct device *genpd_dev_device;
467 	struct device *genpd_dev_ss;
468 	struct device_link *genpd_dl_device;
469 	struct device_link *genpd_dl_ss;
470 
471 	struct dma_pool *transfer_ring_pool;
472 
473 	bool queued_setup_packet;
474 	struct tegra_xudc_setup_packet setup_packet;
475 	enum tegra_xudc_setup_state setup_state;
476 	u16 setup_seq_num;
477 
478 	u16 dev_addr;
479 	u16 isoch_delay;
480 	struct tegra_xudc_sel_timing sel_timing;
481 	u8 test_mode_pattern;
482 	u16 status_buf;
483 	struct tegra_xudc_request *ep0_req;
484 
485 	bool pullup;
486 
487 	unsigned int nr_enabled_eps;
488 	unsigned int nr_isoch_eps;
489 
490 	unsigned int device_state;
491 	unsigned int resume_state;
492 
493 	int irq;
494 
495 	void __iomem *base;
496 	resource_size_t phys_base;
497 	void __iomem *ipfs;
498 	void __iomem *fpci;
499 
500 	struct regulator_bulk_data *supplies;
501 
502 	struct clk_bulk_data *clks;
503 
504 	bool device_mode;
505 	bool current_device_mode;
506 	struct work_struct usb_role_sw_work;
507 
508 	struct phy **usb3_phy;
509 	struct phy *curr_usb3_phy;
510 	struct phy **utmi_phy;
511 	struct phy *curr_utmi_phy;
512 
513 	struct tegra_xudc_save_regs saved_regs;
514 	bool suspended;
515 	bool powergated;
516 
517 	struct usb_phy **usbphy;
518 	struct usb_phy *curr_usbphy;
519 	struct notifier_block vbus_nb;
520 
521 	struct completion disconnect_complete;
522 
523 	bool selfpowered;
524 
525 #define TOGGLE_VBUS_WAIT_MS 100
526 	struct delayed_work plc_reset_work;
527 	bool wait_csc;
528 
529 	struct delayed_work port_reset_war_work;
530 	bool wait_for_sec_prc;
531 };
532 
533 #define XUDC_TRB_MAX_BUFFER_SIZE 65536
534 #define XUDC_MAX_ISOCH_EPS 4
535 #define XUDC_INTERRUPT_MODERATION_US 0
536 
537 static struct usb_endpoint_descriptor tegra_xudc_ep0_desc = {
538 	.bLength = USB_DT_ENDPOINT_SIZE,
539 	.bDescriptorType = USB_DT_ENDPOINT,
540 	.bEndpointAddress = 0,
541 	.bmAttributes = USB_ENDPOINT_XFER_CONTROL,
542 	.wMaxPacketSize = cpu_to_le16(64),
543 };
544 
545 struct tegra_xudc_soc {
546 	const char * const *supply_names;
547 	unsigned int num_supplies;
548 	const char * const *clock_names;
549 	unsigned int num_clks;
550 	unsigned int num_phys;
551 	bool u1_enable;
552 	bool u2_enable;
553 	bool lpm_enable;
554 	bool invalid_seq_num;
555 	bool pls_quirk;
556 	bool port_reset_quirk;
557 	bool port_speed_quirk;
558 	bool has_ipfs;
559 };
560 
561 static inline u32 fpci_readl(struct tegra_xudc *xudc, unsigned int offset)
562 {
563 	return readl(xudc->fpci + offset);
564 }
565 
566 static inline void fpci_writel(struct tegra_xudc *xudc, u32 val,
567 			       unsigned int offset)
568 {
569 	writel(val, xudc->fpci + offset);
570 }
571 
572 static inline u32 ipfs_readl(struct tegra_xudc *xudc, unsigned int offset)
573 {
574 	return readl(xudc->ipfs + offset);
575 }
576 
577 static inline void ipfs_writel(struct tegra_xudc *xudc, u32 val,
578 			       unsigned int offset)
579 {
580 	writel(val, xudc->ipfs + offset);
581 }
582 
583 static inline u32 xudc_readl(struct tegra_xudc *xudc, unsigned int offset)
584 {
585 	return readl(xudc->base + offset);
586 }
587 
588 static inline void xudc_writel(struct tegra_xudc *xudc, u32 val,
589 			       unsigned int offset)
590 {
591 	writel(val, xudc->base + offset);
592 }
593 
594 static inline int xudc_readl_poll(struct tegra_xudc *xudc,
595 				  unsigned int offset, u32 mask, u32 val)
596 {
597 	u32 regval;
598 
599 	return readl_poll_timeout_atomic(xudc->base + offset, regval,
600 					 (regval & mask) == val, 1, 100);
601 }
602 
603 static inline struct tegra_xudc *to_xudc(struct usb_gadget *gadget)
604 {
605 	return container_of(gadget, struct tegra_xudc, gadget);
606 }
607 
608 static inline struct tegra_xudc_ep *to_xudc_ep(struct usb_ep *ep)
609 {
610 	return container_of(ep, struct tegra_xudc_ep, usb_ep);
611 }
612 
613 static inline struct tegra_xudc_request *to_xudc_req(struct usb_request *req)
614 {
615 	return container_of(req, struct tegra_xudc_request, usb_req);
616 }
617 
618 static inline void dump_trb(struct tegra_xudc *xudc, const char *type,
619 			    struct tegra_xudc_trb *trb)
620 {
621 	dev_dbg(xudc->dev,
622 		"%s: %p, lo = %#x, hi = %#x, status = %#x, control = %#x\n",
623 		type, trb, trb->data_lo, trb->data_hi, trb->status,
624 		trb->control);
625 }
626 
627 static void tegra_xudc_limit_port_speed(struct tegra_xudc *xudc)
628 {
629 	u32 val;
630 
631 	/* limit port speed to gen 1 */
632 	val = xudc_readl(xudc, SSPX_CORE_CNT56);
633 	val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
634 	val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x260);
635 	xudc_writel(xudc, val, SSPX_CORE_CNT56);
636 
637 	val = xudc_readl(xudc, SSPX_CORE_CNT57);
638 	val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
639 	val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x6D6);
640 	xudc_writel(xudc, val, SSPX_CORE_CNT57);
641 
642 	val = xudc_readl(xudc, SSPX_CORE_CNT65);
643 	val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
644 	val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0x4B0);
645 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
646 
647 	val = xudc_readl(xudc, SSPX_CORE_CNT66);
648 	val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
649 	val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x4B0);
650 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
651 
652 	val = xudc_readl(xudc, SSPX_CORE_CNT67);
653 	val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
654 	val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x4B0);
655 	xudc_writel(xudc, val, SSPX_CORE_CNT67);
656 
657 	val = xudc_readl(xudc, SSPX_CORE_CNT72);
658 	val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
659 	val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x10);
660 	xudc_writel(xudc, val, SSPX_CORE_CNT72);
661 }
662 
663 static void tegra_xudc_restore_port_speed(struct tegra_xudc *xudc)
664 {
665 	u32 val;
666 
667 	/* restore port speed to gen2 */
668 	val = xudc_readl(xudc, SSPX_CORE_CNT56);
669 	val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
670 	val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x438);
671 	xudc_writel(xudc, val, SSPX_CORE_CNT56);
672 
673 	val = xudc_readl(xudc, SSPX_CORE_CNT57);
674 	val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
675 	val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x528);
676 	xudc_writel(xudc, val, SSPX_CORE_CNT57);
677 
678 	val = xudc_readl(xudc, SSPX_CORE_CNT65);
679 	val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
680 	val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0xE10);
681 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
682 
683 	val = xudc_readl(xudc, SSPX_CORE_CNT66);
684 	val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
685 	val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x348);
686 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
687 
688 	val = xudc_readl(xudc, SSPX_CORE_CNT67);
689 	val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
690 	val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x5a0);
691 	xudc_writel(xudc, val, SSPX_CORE_CNT67);
692 
693 	val = xudc_readl(xudc, SSPX_CORE_CNT72);
694 	val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
695 	val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x1c21);
696 	xudc_writel(xudc, val, SSPX_CORE_CNT72);
697 }
698 
699 static void tegra_xudc_device_mode_on(struct tegra_xudc *xudc)
700 {
701 	int err;
702 
703 	pm_runtime_get_sync(xudc->dev);
704 
705 	tegra_phy_xusb_utmi_pad_power_on(xudc->curr_utmi_phy);
706 
707 	err = phy_power_on(xudc->curr_utmi_phy);
708 	if (err < 0)
709 		dev_err(xudc->dev, "UTMI power on failed: %d\n", err);
710 
711 	err = phy_power_on(xudc->curr_usb3_phy);
712 	if (err < 0)
713 		dev_err(xudc->dev, "USB3 PHY power on failed: %d\n", err);
714 
715 	dev_dbg(xudc->dev, "device mode on\n");
716 
717 	phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
718 			 USB_ROLE_DEVICE);
719 
720 	xudc->current_device_mode = true;
721 }
722 
723 static void tegra_xudc_device_mode_off(struct tegra_xudc *xudc)
724 {
725 	bool connected = false;
726 	u32 pls, val;
727 	int err;
728 
729 	dev_dbg(xudc->dev, "device mode off\n");
730 
731 	xudc->current_device_mode = false;
732 
733 	connected = !!(xudc_readl(xudc, PORTSC) & PORTSC_CCS);
734 
735 	reinit_completion(&xudc->disconnect_complete);
736 
737 	if (xudc->soc->port_speed_quirk)
738 		tegra_xudc_restore_port_speed(xudc);
739 
740 	phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
741 
742 	pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
743 		PORTSC_PLS_SHIFT;
744 
745 	/* Direct link to U0 if disconnected in RESUME or U2. */
746 	if (xudc->soc->pls_quirk && xudc->gadget.speed == USB_SPEED_SUPER &&
747 	    (pls == PORTSC_PLS_RESUME || pls == PORTSC_PLS_U2)) {
748 		val = xudc_readl(xudc, PORTPM);
749 		val |= PORTPM_FRWE;
750 		xudc_writel(xudc, val, PORTPM);
751 
752 		val = xudc_readl(xudc, PORTSC);
753 		val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
754 		val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
755 		xudc_writel(xudc, val, PORTSC);
756 	}
757 
758 	/* Wait for disconnect event. */
759 	if (connected)
760 		wait_for_completion(&xudc->disconnect_complete);
761 
762 	/* Make sure interrupt handler has completed before powergating. */
763 	synchronize_irq(xudc->irq);
764 
765 	tegra_phy_xusb_utmi_pad_power_down(xudc->curr_utmi_phy);
766 
767 	err = phy_power_off(xudc->curr_utmi_phy);
768 	if (err < 0)
769 		dev_err(xudc->dev, "UTMI PHY power off failed: %d\n", err);
770 
771 	err = phy_power_off(xudc->curr_usb3_phy);
772 	if (err < 0)
773 		dev_err(xudc->dev, "USB3 PHY power off failed: %d\n", err);
774 
775 	pm_runtime_put(xudc->dev);
776 }
777 
778 static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
779 {
780 	struct tegra_xudc *xudc = container_of(work, struct tegra_xudc,
781 					       usb_role_sw_work);
782 
783 	if (xudc->device_mode)
784 		tegra_xudc_device_mode_on(xudc);
785 	else
786 		tegra_xudc_device_mode_off(xudc);
787 }
788 
789 static int tegra_xudc_get_phy_index(struct tegra_xudc *xudc,
790 					      struct usb_phy *usbphy)
791 {
792 	unsigned int i;
793 
794 	for (i = 0; i < xudc->soc->num_phys; i++) {
795 		if (xudc->usbphy[i] && usbphy == xudc->usbphy[i])
796 			return i;
797 	}
798 
799 	dev_info(xudc->dev, "phy index could not be found for shared USB PHY");
800 	return -1;
801 }
802 
803 static void tegra_xudc_update_data_role(struct tegra_xudc *xudc,
804 					      struct usb_phy *usbphy)
805 {
806 	int phy_index;
807 
808 	if ((xudc->device_mode && usbphy->last_event == USB_EVENT_VBUS) ||
809 	    (!xudc->device_mode && usbphy->last_event != USB_EVENT_VBUS)) {
810 		dev_dbg(xudc->dev, "Same role(%d) received. Ignore",
811 			xudc->device_mode);
812 		return;
813 	}
814 
815 	xudc->device_mode = usbphy->last_event == USB_EVENT_VBUS;
816 
817 	phy_index = tegra_xudc_get_phy_index(xudc, usbphy);
818 	dev_dbg(xudc->dev, "%s(): current phy index is %d\n", __func__,
819 		phy_index);
820 
821 	if (!xudc->suspended && phy_index != -1) {
822 		xudc->curr_utmi_phy = xudc->utmi_phy[phy_index];
823 		xudc->curr_usb3_phy = xudc->usb3_phy[phy_index];
824 		xudc->curr_usbphy = usbphy;
825 		schedule_work(&xudc->usb_role_sw_work);
826 	}
827 }
828 
829 static int tegra_xudc_vbus_notify(struct notifier_block *nb,
830 					 unsigned long action, void *data)
831 {
832 	struct tegra_xudc *xudc = container_of(nb, struct tegra_xudc,
833 					       vbus_nb);
834 	struct usb_phy *usbphy = (struct usb_phy *)data;
835 
836 	dev_dbg(xudc->dev, "%s(): event is %d\n", __func__, usbphy->last_event);
837 
838 	tegra_xudc_update_data_role(xudc, usbphy);
839 
840 	return NOTIFY_OK;
841 }
842 
843 static void tegra_xudc_plc_reset_work(struct work_struct *work)
844 {
845 	struct delayed_work *dwork = to_delayed_work(work);
846 	struct tegra_xudc *xudc = container_of(dwork, struct tegra_xudc,
847 					       plc_reset_work);
848 	unsigned long flags;
849 
850 	spin_lock_irqsave(&xudc->lock, flags);
851 
852 	if (xudc->wait_csc) {
853 		u32 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
854 			PORTSC_PLS_SHIFT;
855 
856 		if (pls == PORTSC_PLS_INACTIVE) {
857 			dev_info(xudc->dev, "PLS = Inactive. Toggle VBUS\n");
858 			phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
859 					 USB_ROLE_NONE);
860 			phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
861 					 USB_ROLE_DEVICE);
862 
863 			xudc->wait_csc = false;
864 		}
865 	}
866 
867 	spin_unlock_irqrestore(&xudc->lock, flags);
868 }
869 
870 static void tegra_xudc_port_reset_war_work(struct work_struct *work)
871 {
872 	struct delayed_work *dwork = to_delayed_work(work);
873 	struct tegra_xudc *xudc =
874 		container_of(dwork, struct tegra_xudc, port_reset_war_work);
875 	unsigned long flags;
876 	u32 pls;
877 	int ret;
878 
879 	spin_lock_irqsave(&xudc->lock, flags);
880 
881 	if (xudc->device_mode && xudc->wait_for_sec_prc) {
882 		pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
883 			PORTSC_PLS_SHIFT;
884 		dev_dbg(xudc->dev, "pls = %x\n", pls);
885 
886 		if (pls == PORTSC_PLS_DISABLED) {
887 			dev_dbg(xudc->dev, "toggle vbus\n");
888 			/* PRC doesn't complete in 100ms, toggle the vbus */
889 			ret = tegra_phy_xusb_utmi_port_reset(
890 				xudc->curr_utmi_phy);
891 			if (ret == 1)
892 				xudc->wait_for_sec_prc = 0;
893 		}
894 	}
895 
896 	spin_unlock_irqrestore(&xudc->lock, flags);
897 }
898 
899 static dma_addr_t trb_virt_to_phys(struct tegra_xudc_ep *ep,
900 				   struct tegra_xudc_trb *trb)
901 {
902 	unsigned int index;
903 
904 	index = trb - ep->transfer_ring;
905 
906 	if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
907 		return 0;
908 
909 	return (ep->transfer_ring_phys + index * sizeof(*trb));
910 }
911 
912 static struct tegra_xudc_trb *trb_phys_to_virt(struct tegra_xudc_ep *ep,
913 					       dma_addr_t addr)
914 {
915 	struct tegra_xudc_trb *trb;
916 	unsigned int index;
917 
918 	index = (addr - ep->transfer_ring_phys) / sizeof(*trb);
919 
920 	if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
921 		return NULL;
922 
923 	trb = &ep->transfer_ring[index];
924 
925 	return trb;
926 }
927 
928 static void ep_reload(struct tegra_xudc *xudc, unsigned int ep)
929 {
930 	xudc_writel(xudc, BIT(ep), EP_RELOAD);
931 	xudc_readl_poll(xudc, EP_RELOAD, BIT(ep), 0);
932 }
933 
934 static void ep_pause(struct tegra_xudc *xudc, unsigned int ep)
935 {
936 	u32 val;
937 
938 	val = xudc_readl(xudc, EP_PAUSE);
939 	if (val & BIT(ep))
940 		return;
941 	val |= BIT(ep);
942 
943 	xudc_writel(xudc, val, EP_PAUSE);
944 
945 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
946 
947 	xudc_writel(xudc, BIT(ep), EP_STCHG);
948 }
949 
950 static void ep_unpause(struct tegra_xudc *xudc, unsigned int ep)
951 {
952 	u32 val;
953 
954 	val = xudc_readl(xudc, EP_PAUSE);
955 	if (!(val & BIT(ep)))
956 		return;
957 	val &= ~BIT(ep);
958 
959 	xudc_writel(xudc, val, EP_PAUSE);
960 
961 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
962 
963 	xudc_writel(xudc, BIT(ep), EP_STCHG);
964 }
965 
966 static void ep_unpause_all(struct tegra_xudc *xudc)
967 {
968 	u32 val;
969 
970 	val = xudc_readl(xudc, EP_PAUSE);
971 
972 	xudc_writel(xudc, 0, EP_PAUSE);
973 
974 	xudc_readl_poll(xudc, EP_STCHG, val, val);
975 
976 	xudc_writel(xudc, val, EP_STCHG);
977 }
978 
979 static void ep_halt(struct tegra_xudc *xudc, unsigned int ep)
980 {
981 	u32 val;
982 
983 	val = xudc_readl(xudc, EP_HALT);
984 	if (val & BIT(ep))
985 		return;
986 	val |= BIT(ep);
987 	xudc_writel(xudc, val, EP_HALT);
988 
989 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
990 
991 	xudc_writel(xudc, BIT(ep), EP_STCHG);
992 }
993 
994 static void ep_unhalt(struct tegra_xudc *xudc, unsigned int ep)
995 {
996 	u32 val;
997 
998 	val = xudc_readl(xudc, EP_HALT);
999 	if (!(val & BIT(ep)))
1000 		return;
1001 	val &= ~BIT(ep);
1002 	xudc_writel(xudc, val, EP_HALT);
1003 
1004 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
1005 
1006 	xudc_writel(xudc, BIT(ep), EP_STCHG);
1007 }
1008 
1009 static void ep_unhalt_all(struct tegra_xudc *xudc)
1010 {
1011 	u32 val;
1012 
1013 	val = xudc_readl(xudc, EP_HALT);
1014 	if (!val)
1015 		return;
1016 	xudc_writel(xudc, 0, EP_HALT);
1017 
1018 	xudc_readl_poll(xudc, EP_STCHG, val, val);
1019 
1020 	xudc_writel(xudc, val, EP_STCHG);
1021 }
1022 
1023 static void ep_wait_for_stopped(struct tegra_xudc *xudc, unsigned int ep)
1024 {
1025 	xudc_readl_poll(xudc, EP_STOPPED, BIT(ep), BIT(ep));
1026 	xudc_writel(xudc, BIT(ep), EP_STOPPED);
1027 }
1028 
1029 static void ep_wait_for_inactive(struct tegra_xudc *xudc, unsigned int ep)
1030 {
1031 	xudc_readl_poll(xudc, EP_THREAD_ACTIVE, BIT(ep), 0);
1032 }
1033 
1034 static void tegra_xudc_req_done(struct tegra_xudc_ep *ep,
1035 				struct tegra_xudc_request *req, int status)
1036 {
1037 	struct tegra_xudc *xudc = ep->xudc;
1038 
1039 	dev_dbg(xudc->dev, "completing request %p on EP %u with status %d\n",
1040 		 req, ep->index, status);
1041 
1042 	if (likely(req->usb_req.status == -EINPROGRESS))
1043 		req->usb_req.status = status;
1044 
1045 	list_del_init(&req->list);
1046 
1047 	if (usb_endpoint_xfer_control(ep->desc)) {
1048 		usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
1049 					 (xudc->setup_state ==
1050 					  DATA_STAGE_XFER));
1051 	} else {
1052 		usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
1053 					 usb_endpoint_dir_in(ep->desc));
1054 	}
1055 
1056 	spin_unlock(&xudc->lock);
1057 	usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
1058 	spin_lock(&xudc->lock);
1059 }
1060 
1061 static void tegra_xudc_ep_nuke(struct tegra_xudc_ep *ep, int status)
1062 {
1063 	struct tegra_xudc_request *req;
1064 
1065 	while (!list_empty(&ep->queue)) {
1066 		req = list_first_entry(&ep->queue, struct tegra_xudc_request,
1067 				       list);
1068 		tegra_xudc_req_done(ep, req, status);
1069 	}
1070 }
1071 
1072 static unsigned int ep_available_trbs(struct tegra_xudc_ep *ep)
1073 {
1074 	if (ep->ring_full)
1075 		return 0;
1076 
1077 	if (ep->deq_ptr > ep->enq_ptr)
1078 		return ep->deq_ptr - ep->enq_ptr - 1;
1079 
1080 	return XUDC_TRANSFER_RING_SIZE - (ep->enq_ptr - ep->deq_ptr) - 2;
1081 }
1082 
1083 static void tegra_xudc_queue_one_trb(struct tegra_xudc_ep *ep,
1084 				     struct tegra_xudc_request *req,
1085 				     struct tegra_xudc_trb *trb,
1086 				     bool ioc)
1087 {
1088 	struct tegra_xudc *xudc = ep->xudc;
1089 	dma_addr_t buf_addr;
1090 	size_t len;
1091 
1092 	len = min_t(size_t, XUDC_TRB_MAX_BUFFER_SIZE, req->usb_req.length -
1093 		    req->buf_queued);
1094 	if (len > 0)
1095 		buf_addr = req->usb_req.dma + req->buf_queued;
1096 	else
1097 		buf_addr = 0;
1098 
1099 	trb_write_data_ptr(trb, buf_addr);
1100 
1101 	trb_write_transfer_len(trb, len);
1102 	trb_write_td_size(trb, req->trbs_needed - req->trbs_queued - 1);
1103 
1104 	if (req->trbs_queued == req->trbs_needed - 1 ||
1105 		(req->need_zlp && req->trbs_queued == req->trbs_needed - 2))
1106 		trb_write_chain(trb, 0);
1107 	else
1108 		trb_write_chain(trb, 1);
1109 
1110 	trb_write_ioc(trb, ioc);
1111 
1112 	if (usb_endpoint_dir_out(ep->desc) ||
1113 	    (usb_endpoint_xfer_control(ep->desc) &&
1114 	     (xudc->setup_state == DATA_STAGE_RECV)))
1115 		trb_write_isp(trb, 1);
1116 	else
1117 		trb_write_isp(trb, 0);
1118 
1119 	if (usb_endpoint_xfer_control(ep->desc)) {
1120 		if (xudc->setup_state == DATA_STAGE_XFER ||
1121 		    xudc->setup_state == DATA_STAGE_RECV)
1122 			trb_write_type(trb, TRB_TYPE_DATA_STAGE);
1123 		else
1124 			trb_write_type(trb, TRB_TYPE_STATUS_STAGE);
1125 
1126 		if (xudc->setup_state == DATA_STAGE_XFER ||
1127 		    xudc->setup_state == STATUS_STAGE_XFER)
1128 			trb_write_data_stage_dir(trb, 1);
1129 		else
1130 			trb_write_data_stage_dir(trb, 0);
1131 	} else if (usb_endpoint_xfer_isoc(ep->desc)) {
1132 		trb_write_type(trb, TRB_TYPE_ISOCH);
1133 		trb_write_sia(trb, 1);
1134 		trb_write_frame_id(trb, 0);
1135 		trb_write_tlbpc(trb, 0);
1136 	} else if (usb_ss_max_streams(ep->comp_desc)) {
1137 		trb_write_type(trb, TRB_TYPE_STREAM);
1138 		trb_write_stream_id(trb, req->usb_req.stream_id);
1139 	} else {
1140 		trb_write_type(trb, TRB_TYPE_NORMAL);
1141 		trb_write_stream_id(trb, 0);
1142 	}
1143 
1144 	trb_write_cycle(trb, ep->pcs);
1145 
1146 	req->trbs_queued++;
1147 	req->buf_queued += len;
1148 
1149 	dump_trb(xudc, "TRANSFER", trb);
1150 }
1151 
1152 static unsigned int tegra_xudc_queue_trbs(struct tegra_xudc_ep *ep,
1153 					  struct tegra_xudc_request *req)
1154 {
1155 	unsigned int i, count, available;
1156 	bool wait_td = false;
1157 
1158 	available = ep_available_trbs(ep);
1159 	count = req->trbs_needed - req->trbs_queued;
1160 	if (available < count) {
1161 		count = available;
1162 		ep->ring_full = true;
1163 	}
1164 
1165 	/*
1166 	 * To generate zero-length packet on USB bus, SW needs schedule a
1167 	 * standalone zero-length TD. According to HW's behavior, SW needs
1168 	 * to schedule TDs in different ways for different endpoint types.
1169 	 *
1170 	 * For control endpoint:
1171 	 * - Data stage TD (IOC = 1, CH = 0)
1172 	 * - Ring doorbell and wait transfer event
1173 	 * - Data stage TD for ZLP (IOC = 1, CH = 0)
1174 	 * - Ring doorbell
1175 	 *
1176 	 * For bulk and interrupt endpoints:
1177 	 * - Normal transfer TD (IOC = 0, CH = 0)
1178 	 * - Normal transfer TD for ZLP (IOC = 1, CH = 0)
1179 	 * - Ring doorbell
1180 	 */
1181 
1182 	if (req->need_zlp && usb_endpoint_xfer_control(ep->desc) && count > 1)
1183 		wait_td = true;
1184 
1185 	if (!req->first_trb)
1186 		req->first_trb = &ep->transfer_ring[ep->enq_ptr];
1187 
1188 	for (i = 0; i < count; i++) {
1189 		struct tegra_xudc_trb *trb = &ep->transfer_ring[ep->enq_ptr];
1190 		bool ioc = false;
1191 
1192 		if ((i == count - 1) || (wait_td && i == count - 2))
1193 			ioc = true;
1194 
1195 		tegra_xudc_queue_one_trb(ep, req, trb, ioc);
1196 		req->last_trb = trb;
1197 
1198 		ep->enq_ptr++;
1199 		if (ep->enq_ptr == XUDC_TRANSFER_RING_SIZE - 1) {
1200 			trb = &ep->transfer_ring[ep->enq_ptr];
1201 			trb_write_cycle(trb, ep->pcs);
1202 			ep->pcs = !ep->pcs;
1203 			ep->enq_ptr = 0;
1204 		}
1205 
1206 		if (ioc)
1207 			break;
1208 	}
1209 
1210 	return count;
1211 }
1212 
1213 static void tegra_xudc_ep_ring_doorbell(struct tegra_xudc_ep *ep)
1214 {
1215 	struct tegra_xudc *xudc = ep->xudc;
1216 	u32 val;
1217 
1218 	if (list_empty(&ep->queue))
1219 		return;
1220 
1221 	val = DB_TARGET(ep->index);
1222 	if (usb_endpoint_xfer_control(ep->desc)) {
1223 		val |= DB_STREAMID(xudc->setup_seq_num);
1224 	} else if (usb_ss_max_streams(ep->comp_desc) > 0) {
1225 		struct tegra_xudc_request *req;
1226 
1227 		/* Don't ring doorbell if the stream has been rejected. */
1228 		if (ep->stream_rejected)
1229 			return;
1230 
1231 		req = list_first_entry(&ep->queue, struct tegra_xudc_request,
1232 				       list);
1233 		val |= DB_STREAMID(req->usb_req.stream_id);
1234 	}
1235 
1236 	dev_dbg(xudc->dev, "ring doorbell: %#x\n", val);
1237 	xudc_writel(xudc, val, DB);
1238 }
1239 
1240 static void tegra_xudc_ep_kick_queue(struct tegra_xudc_ep *ep)
1241 {
1242 	struct tegra_xudc_request *req;
1243 	bool trbs_queued = false;
1244 
1245 	list_for_each_entry(req, &ep->queue, list) {
1246 		if (ep->ring_full)
1247 			break;
1248 
1249 		if (tegra_xudc_queue_trbs(ep, req) > 0)
1250 			trbs_queued = true;
1251 	}
1252 
1253 	if (trbs_queued)
1254 		tegra_xudc_ep_ring_doorbell(ep);
1255 }
1256 
1257 static int
1258 __tegra_xudc_ep_queue(struct tegra_xudc_ep *ep, struct tegra_xudc_request *req)
1259 {
1260 	struct tegra_xudc *xudc = ep->xudc;
1261 	int err;
1262 
1263 	if (usb_endpoint_xfer_control(ep->desc) && !list_empty(&ep->queue)) {
1264 		dev_err(xudc->dev, "control EP has pending transfers\n");
1265 		return -EINVAL;
1266 	}
1267 
1268 	if (usb_endpoint_xfer_control(ep->desc)) {
1269 		err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
1270 					     (xudc->setup_state ==
1271 					      DATA_STAGE_XFER));
1272 	} else {
1273 		err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
1274 					     usb_endpoint_dir_in(ep->desc));
1275 	}
1276 
1277 	if (err < 0) {
1278 		dev_err(xudc->dev, "failed to map request: %d\n", err);
1279 		return err;
1280 	}
1281 
1282 	req->first_trb = NULL;
1283 	req->last_trb = NULL;
1284 	req->buf_queued = 0;
1285 	req->trbs_queued = 0;
1286 	req->need_zlp = false;
1287 	req->trbs_needed = DIV_ROUND_UP(req->usb_req.length,
1288 					XUDC_TRB_MAX_BUFFER_SIZE);
1289 	if (req->usb_req.length == 0)
1290 		req->trbs_needed++;
1291 
1292 	if (!usb_endpoint_xfer_isoc(ep->desc) &&
1293 	    req->usb_req.zero && req->usb_req.length &&
1294 	    ((req->usb_req.length % ep->usb_ep.maxpacket) == 0)) {
1295 		req->trbs_needed++;
1296 		req->need_zlp = true;
1297 	}
1298 
1299 	req->usb_req.status = -EINPROGRESS;
1300 	req->usb_req.actual = 0;
1301 
1302 	list_add_tail(&req->list, &ep->queue);
1303 
1304 	tegra_xudc_ep_kick_queue(ep);
1305 
1306 	return 0;
1307 }
1308 
1309 static int
1310 tegra_xudc_ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
1311 		    gfp_t gfp)
1312 {
1313 	struct tegra_xudc_request *req;
1314 	struct tegra_xudc_ep *ep;
1315 	struct tegra_xudc *xudc;
1316 	unsigned long flags;
1317 	int ret;
1318 
1319 	if (!usb_ep || !usb_req)
1320 		return -EINVAL;
1321 
1322 	ep = to_xudc_ep(usb_ep);
1323 	req = to_xudc_req(usb_req);
1324 	xudc = ep->xudc;
1325 
1326 	spin_lock_irqsave(&xudc->lock, flags);
1327 	if (xudc->powergated || !ep->desc) {
1328 		ret = -ESHUTDOWN;
1329 		goto unlock;
1330 	}
1331 
1332 	ret = __tegra_xudc_ep_queue(ep, req);
1333 unlock:
1334 	spin_unlock_irqrestore(&xudc->lock, flags);
1335 
1336 	return ret;
1337 }
1338 
1339 static void squeeze_transfer_ring(struct tegra_xudc_ep *ep,
1340 				  struct tegra_xudc_request *req)
1341 {
1342 	struct tegra_xudc_trb *trb = req->first_trb;
1343 	bool pcs_enq = trb_read_cycle(trb);
1344 	bool pcs;
1345 
1346 	/*
1347 	 * Clear out all the TRBs part of or after the cancelled request,
1348 	 * and must correct trb cycle bit to the last un-enqueued state.
1349 	 */
1350 	while (trb != &ep->transfer_ring[ep->enq_ptr]) {
1351 		pcs = trb_read_cycle(trb);
1352 		memset(trb, 0, sizeof(*trb));
1353 		trb_write_cycle(trb, !pcs);
1354 		trb++;
1355 
1356 		if (trb_read_type(trb) == TRB_TYPE_LINK)
1357 			trb = ep->transfer_ring;
1358 	}
1359 
1360 	/* Requests will be re-queued at the start of the cancelled request. */
1361 	ep->enq_ptr = req->first_trb - ep->transfer_ring;
1362 	/*
1363 	 * Retrieve the correct cycle bit state from the first trb of
1364 	 * the cancelled request.
1365 	 */
1366 	ep->pcs = pcs_enq;
1367 	ep->ring_full = false;
1368 	list_for_each_entry_continue(req, &ep->queue, list) {
1369 		req->usb_req.status = -EINPROGRESS;
1370 		req->usb_req.actual = 0;
1371 
1372 		req->first_trb = NULL;
1373 		req->last_trb = NULL;
1374 		req->buf_queued = 0;
1375 		req->trbs_queued = 0;
1376 	}
1377 }
1378 
1379 /*
1380  * Determine if the given TRB is in the range [first trb, last trb] for the
1381  * given request.
1382  */
1383 static bool trb_in_request(struct tegra_xudc_ep *ep,
1384 			   struct tegra_xudc_request *req,
1385 			   struct tegra_xudc_trb *trb)
1386 {
1387 	dev_dbg(ep->xudc->dev, "%s: request %p -> %p; trb %p\n", __func__,
1388 		req->first_trb, req->last_trb, trb);
1389 
1390 	if (trb >= req->first_trb && (trb <= req->last_trb ||
1391 				      req->last_trb < req->first_trb))
1392 		return true;
1393 
1394 	if (trb < req->first_trb && trb <= req->last_trb &&
1395 	    req->last_trb < req->first_trb)
1396 		return true;
1397 
1398 	return false;
1399 }
1400 
1401 /*
1402  * Determine if the given TRB is in the range [EP enqueue pointer, first TRB)
1403  * for the given endpoint and request.
1404  */
1405 static bool trb_before_request(struct tegra_xudc_ep *ep,
1406 			       struct tegra_xudc_request *req,
1407 			       struct tegra_xudc_trb *trb)
1408 {
1409 	struct tegra_xudc_trb *enq_trb = &ep->transfer_ring[ep->enq_ptr];
1410 
1411 	dev_dbg(ep->xudc->dev, "%s: request %p -> %p; enq ptr: %p; trb %p\n",
1412 		__func__, req->first_trb, req->last_trb, enq_trb, trb);
1413 
1414 	if (trb < req->first_trb && (enq_trb <= trb ||
1415 				     req->first_trb < enq_trb))
1416 		return true;
1417 
1418 	if (trb > req->first_trb && req->first_trb < enq_trb && enq_trb <= trb)
1419 		return true;
1420 
1421 	return false;
1422 }
1423 
1424 static int
1425 __tegra_xudc_ep_dequeue(struct tegra_xudc_ep *ep,
1426 			struct tegra_xudc_request *req)
1427 {
1428 	struct tegra_xudc *xudc = ep->xudc;
1429 	struct tegra_xudc_request *r = NULL, *iter;
1430 	struct tegra_xudc_trb *deq_trb;
1431 	bool busy, kick_queue = false;
1432 	int ret = 0;
1433 
1434 	/* Make sure the request is actually queued to this endpoint. */
1435 	list_for_each_entry(iter, &ep->queue, list) {
1436 		if (iter != req)
1437 			continue;
1438 		r = iter;
1439 		break;
1440 	}
1441 
1442 	if (!r)
1443 		return -EINVAL;
1444 
1445 	/* Request hasn't been queued in the transfer ring yet. */
1446 	if (!req->trbs_queued) {
1447 		tegra_xudc_req_done(ep, req, -ECONNRESET);
1448 		return 0;
1449 	}
1450 
1451 	/* Halt DMA for this endpoint. */
1452 	if (ep_ctx_read_state(ep->context) == EP_STATE_RUNNING) {
1453 		ep_pause(xudc, ep->index);
1454 		ep_wait_for_inactive(xudc, ep->index);
1455 	}
1456 
1457 	deq_trb = trb_phys_to_virt(ep, ep_ctx_read_deq_ptr(ep->context));
1458 	/* Is the hardware processing the TRB at the dequeue pointer? */
1459 	busy = (trb_read_cycle(deq_trb) == ep_ctx_read_dcs(ep->context));
1460 
1461 	if (trb_in_request(ep, req, deq_trb) && busy) {
1462 		/*
1463 		 * Request has been partially completed or it hasn't
1464 		 * started processing yet.
1465 		 */
1466 		dma_addr_t deq_ptr;
1467 
1468 		squeeze_transfer_ring(ep, req);
1469 
1470 		req->usb_req.actual = ep_ctx_read_edtla(ep->context);
1471 		tegra_xudc_req_done(ep, req, -ECONNRESET);
1472 		kick_queue = true;
1473 
1474 		/* EDTLA is > 0: request has been partially completed */
1475 		if (req->usb_req.actual > 0) {
1476 			/*
1477 			 * Abort the pending transfer and update the dequeue
1478 			 * pointer
1479 			 */
1480 			ep_ctx_write_edtla(ep->context, 0);
1481 			ep_ctx_write_partial_td(ep->context, 0);
1482 			ep_ctx_write_data_offset(ep->context, 0);
1483 
1484 			deq_ptr = trb_virt_to_phys(ep,
1485 					&ep->transfer_ring[ep->enq_ptr]);
1486 
1487 			if (dma_mapping_error(xudc->dev, deq_ptr)) {
1488 				ret = -EINVAL;
1489 			} else {
1490 				ep_ctx_write_deq_ptr(ep->context, deq_ptr);
1491 				ep_ctx_write_dcs(ep->context, ep->pcs);
1492 				ep_reload(xudc, ep->index);
1493 			}
1494 		}
1495 	} else if (trb_before_request(ep, req, deq_trb) && busy) {
1496 		/* Request hasn't started processing yet. */
1497 		squeeze_transfer_ring(ep, req);
1498 
1499 		tegra_xudc_req_done(ep, req, -ECONNRESET);
1500 		kick_queue = true;
1501 	} else {
1502 		/*
1503 		 * Request has completed, but we haven't processed the
1504 		 * completion event yet.
1505 		 */
1506 		tegra_xudc_req_done(ep, req, -ECONNRESET);
1507 		ret = -EINVAL;
1508 	}
1509 
1510 	/* Resume the endpoint. */
1511 	ep_unpause(xudc, ep->index);
1512 
1513 	if (kick_queue)
1514 		tegra_xudc_ep_kick_queue(ep);
1515 
1516 	return ret;
1517 }
1518 
1519 static int
1520 tegra_xudc_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
1521 {
1522 	struct tegra_xudc_request *req;
1523 	struct tegra_xudc_ep *ep;
1524 	struct tegra_xudc *xudc;
1525 	unsigned long flags;
1526 	int ret;
1527 
1528 	if (!usb_ep || !usb_req)
1529 		return -EINVAL;
1530 
1531 	ep = to_xudc_ep(usb_ep);
1532 	req = to_xudc_req(usb_req);
1533 	xudc = ep->xudc;
1534 
1535 	spin_lock_irqsave(&xudc->lock, flags);
1536 
1537 	if (xudc->powergated || !ep->desc) {
1538 		ret = -ESHUTDOWN;
1539 		goto unlock;
1540 	}
1541 
1542 	ret = __tegra_xudc_ep_dequeue(ep, req);
1543 unlock:
1544 	spin_unlock_irqrestore(&xudc->lock, flags);
1545 
1546 	return ret;
1547 }
1548 
1549 static int __tegra_xudc_ep_set_halt(struct tegra_xudc_ep *ep, bool halt)
1550 {
1551 	struct tegra_xudc *xudc = ep->xudc;
1552 
1553 	if (!ep->desc)
1554 		return -EINVAL;
1555 
1556 	if (usb_endpoint_xfer_isoc(ep->desc)) {
1557 		dev_err(xudc->dev, "can't halt isochronous EP\n");
1558 		return -ENOTSUPP;
1559 	}
1560 
1561 	if (!!(xudc_readl(xudc, EP_HALT) & BIT(ep->index)) == halt) {
1562 		dev_dbg(xudc->dev, "EP %u already %s\n", ep->index,
1563 			halt ? "halted" : "not halted");
1564 		return 0;
1565 	}
1566 
1567 	if (halt) {
1568 		ep_halt(xudc, ep->index);
1569 	} else {
1570 		ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
1571 
1572 		ep_reload(xudc, ep->index);
1573 
1574 		ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
1575 		ep_ctx_write_rsvd(ep->context, 0);
1576 		ep_ctx_write_partial_td(ep->context, 0);
1577 		ep_ctx_write_splitxstate(ep->context, 0);
1578 		ep_ctx_write_seq_num(ep->context, 0);
1579 
1580 		ep_reload(xudc, ep->index);
1581 		ep_unpause(xudc, ep->index);
1582 		ep_unhalt(xudc, ep->index);
1583 
1584 		tegra_xudc_ep_ring_doorbell(ep);
1585 	}
1586 
1587 	return 0;
1588 }
1589 
1590 static int tegra_xudc_ep_set_halt(struct usb_ep *usb_ep, int value)
1591 {
1592 	struct tegra_xudc_ep *ep;
1593 	struct tegra_xudc *xudc;
1594 	unsigned long flags;
1595 	int ret;
1596 
1597 	if (!usb_ep)
1598 		return -EINVAL;
1599 
1600 	ep = to_xudc_ep(usb_ep);
1601 	xudc = ep->xudc;
1602 
1603 	spin_lock_irqsave(&xudc->lock, flags);
1604 	if (xudc->powergated) {
1605 		ret = -ESHUTDOWN;
1606 		goto unlock;
1607 	}
1608 
1609 	if (value && usb_endpoint_dir_in(ep->desc) &&
1610 	    !list_empty(&ep->queue)) {
1611 		dev_err(xudc->dev, "can't halt EP with requests pending\n");
1612 		ret = -EAGAIN;
1613 		goto unlock;
1614 	}
1615 
1616 	ret = __tegra_xudc_ep_set_halt(ep, value);
1617 unlock:
1618 	spin_unlock_irqrestore(&xudc->lock, flags);
1619 
1620 	return ret;
1621 }
1622 
1623 static void tegra_xudc_ep_context_setup(struct tegra_xudc_ep *ep)
1624 {
1625 	const struct usb_endpoint_descriptor *desc = ep->desc;
1626 	const struct usb_ss_ep_comp_descriptor *comp_desc = ep->comp_desc;
1627 	struct tegra_xudc *xudc = ep->xudc;
1628 	u16 maxpacket, maxburst = 0, esit = 0;
1629 	u32 val;
1630 
1631 	maxpacket = usb_endpoint_maxp(desc);
1632 	if (xudc->gadget.speed == USB_SPEED_SUPER) {
1633 		if (!usb_endpoint_xfer_control(desc))
1634 			maxburst = comp_desc->bMaxBurst;
1635 
1636 		if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc))
1637 			esit = le16_to_cpu(comp_desc->wBytesPerInterval);
1638 	} else if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
1639 		   (usb_endpoint_xfer_int(desc) ||
1640 		    usb_endpoint_xfer_isoc(desc))) {
1641 		if (xudc->gadget.speed == USB_SPEED_HIGH) {
1642 			maxburst = usb_endpoint_maxp_mult(desc) - 1;
1643 			if (maxburst == 0x3) {
1644 				dev_warn(xudc->dev,
1645 					 "invalid endpoint maxburst\n");
1646 				maxburst = 0x2;
1647 			}
1648 		}
1649 		esit = maxpacket * (maxburst + 1);
1650 	}
1651 
1652 	memset(ep->context, 0, sizeof(*ep->context));
1653 
1654 	ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
1655 	ep_ctx_write_interval(ep->context, desc->bInterval);
1656 	if (xudc->gadget.speed == USB_SPEED_SUPER) {
1657 		if (usb_endpoint_xfer_isoc(desc)) {
1658 			ep_ctx_write_mult(ep->context,
1659 					  comp_desc->bmAttributes & 0x3);
1660 		}
1661 
1662 		if (usb_endpoint_xfer_bulk(desc)) {
1663 			ep_ctx_write_max_pstreams(ep->context,
1664 						  comp_desc->bmAttributes &
1665 						  0x1f);
1666 			ep_ctx_write_lsa(ep->context, 1);
1667 		}
1668 	}
1669 
1670 	if (!usb_endpoint_xfer_control(desc) && usb_endpoint_dir_out(desc))
1671 		val = usb_endpoint_type(desc);
1672 	else
1673 		val = usb_endpoint_type(desc) + EP_TYPE_CONTROL;
1674 
1675 	ep_ctx_write_type(ep->context, val);
1676 	ep_ctx_write_cerr(ep->context, 0x3);
1677 	ep_ctx_write_max_packet_size(ep->context, maxpacket);
1678 	ep_ctx_write_max_burst_size(ep->context, maxburst);
1679 
1680 	ep_ctx_write_deq_ptr(ep->context, ep->transfer_ring_phys);
1681 	ep_ctx_write_dcs(ep->context, ep->pcs);
1682 
1683 	/* Select a reasonable average TRB length based on endpoint type. */
1684 	switch (usb_endpoint_type(desc)) {
1685 	case USB_ENDPOINT_XFER_CONTROL:
1686 		val = 8;
1687 		break;
1688 	case USB_ENDPOINT_XFER_INT:
1689 		val = 1024;
1690 		break;
1691 	case USB_ENDPOINT_XFER_BULK:
1692 	case USB_ENDPOINT_XFER_ISOC:
1693 	default:
1694 		val = 3072;
1695 		break;
1696 	}
1697 
1698 	ep_ctx_write_avg_trb_len(ep->context, val);
1699 	ep_ctx_write_max_esit_payload(ep->context, esit);
1700 
1701 	ep_ctx_write_cerrcnt(ep->context, 0x3);
1702 }
1703 
1704 static void setup_link_trb(struct tegra_xudc_ep *ep,
1705 			   struct tegra_xudc_trb *trb)
1706 {
1707 	trb_write_data_ptr(trb, ep->transfer_ring_phys);
1708 	trb_write_type(trb, TRB_TYPE_LINK);
1709 	trb_write_toggle_cycle(trb, 1);
1710 }
1711 
1712 static int __tegra_xudc_ep_disable(struct tegra_xudc_ep *ep)
1713 {
1714 	struct tegra_xudc *xudc = ep->xudc;
1715 
1716 	if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
1717 		dev_err(xudc->dev, "endpoint %u already disabled\n",
1718 			ep->index);
1719 		return -EINVAL;
1720 	}
1721 
1722 	ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
1723 
1724 	ep_reload(xudc, ep->index);
1725 
1726 	tegra_xudc_ep_nuke(ep, -ESHUTDOWN);
1727 
1728 	xudc->nr_enabled_eps--;
1729 	if (usb_endpoint_xfer_isoc(ep->desc))
1730 		xudc->nr_isoch_eps--;
1731 
1732 	ep->desc = NULL;
1733 	ep->comp_desc = NULL;
1734 
1735 	memset(ep->context, 0, sizeof(*ep->context));
1736 
1737 	ep_unpause(xudc, ep->index);
1738 	ep_unhalt(xudc, ep->index);
1739 	if (xudc_readl(xudc, EP_STOPPED) & BIT(ep->index))
1740 		xudc_writel(xudc, BIT(ep->index), EP_STOPPED);
1741 
1742 	/*
1743 	 * If this is the last endpoint disabled in a de-configure request,
1744 	 * switch back to address state.
1745 	 */
1746 	if ((xudc->device_state == USB_STATE_CONFIGURED) &&
1747 	    (xudc->nr_enabled_eps == 1)) {
1748 		u32 val;
1749 
1750 		xudc->device_state = USB_STATE_ADDRESS;
1751 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1752 
1753 		val = xudc_readl(xudc, CTRL);
1754 		val &= ~CTRL_RUN;
1755 		xudc_writel(xudc, val, CTRL);
1756 
1757 		val = xudc_readl(xudc, ST);
1758 		if (val & ST_RC)
1759 			xudc_writel(xudc, ST_RC, ST);
1760 	}
1761 
1762 	dev_info(xudc->dev, "ep %u disabled\n", ep->index);
1763 
1764 	return 0;
1765 }
1766 
1767 static int tegra_xudc_ep_disable(struct usb_ep *usb_ep)
1768 {
1769 	struct tegra_xudc_ep *ep;
1770 	struct tegra_xudc *xudc;
1771 	unsigned long flags;
1772 	int ret;
1773 
1774 	if (!usb_ep)
1775 		return -EINVAL;
1776 
1777 	ep = to_xudc_ep(usb_ep);
1778 	xudc = ep->xudc;
1779 
1780 	spin_lock_irqsave(&xudc->lock, flags);
1781 	if (xudc->powergated) {
1782 		ret = -ESHUTDOWN;
1783 		goto unlock;
1784 	}
1785 
1786 	ret = __tegra_xudc_ep_disable(ep);
1787 unlock:
1788 	spin_unlock_irqrestore(&xudc->lock, flags);
1789 
1790 	return ret;
1791 }
1792 
1793 static int __tegra_xudc_ep_enable(struct tegra_xudc_ep *ep,
1794 				  const struct usb_endpoint_descriptor *desc)
1795 {
1796 	struct tegra_xudc *xudc = ep->xudc;
1797 	unsigned int i;
1798 	u32 val;
1799 
1800 	if (xudc->gadget.speed == USB_SPEED_SUPER &&
1801 		!usb_endpoint_xfer_control(desc) && !ep->usb_ep.comp_desc)
1802 		return -EINVAL;
1803 
1804 	/* Disable the EP if it is not disabled */
1805 	if (ep_ctx_read_state(ep->context) != EP_STATE_DISABLED)
1806 		__tegra_xudc_ep_disable(ep);
1807 
1808 	ep->desc = desc;
1809 	ep->comp_desc = ep->usb_ep.comp_desc;
1810 
1811 	if (usb_endpoint_xfer_isoc(desc)) {
1812 		if (xudc->nr_isoch_eps > XUDC_MAX_ISOCH_EPS) {
1813 			dev_err(xudc->dev, "too many isochronous endpoints\n");
1814 			return -EBUSY;
1815 		}
1816 		xudc->nr_isoch_eps++;
1817 	}
1818 
1819 	memset(ep->transfer_ring, 0, XUDC_TRANSFER_RING_SIZE *
1820 	       sizeof(*ep->transfer_ring));
1821 	setup_link_trb(ep, &ep->transfer_ring[XUDC_TRANSFER_RING_SIZE - 1]);
1822 
1823 	ep->enq_ptr = 0;
1824 	ep->deq_ptr = 0;
1825 	ep->pcs = true;
1826 	ep->ring_full = false;
1827 	xudc->nr_enabled_eps++;
1828 
1829 	tegra_xudc_ep_context_setup(ep);
1830 
1831 	/*
1832 	 * No need to reload and un-halt EP0.  This will be done automatically
1833 	 * once a valid SETUP packet is received.
1834 	 */
1835 	if (usb_endpoint_xfer_control(desc))
1836 		goto out;
1837 
1838 	/*
1839 	 * Transition to configured state once the first non-control
1840 	 * endpoint is enabled.
1841 	 */
1842 	if (xudc->device_state == USB_STATE_ADDRESS) {
1843 		val = xudc_readl(xudc, CTRL);
1844 		val |= CTRL_RUN;
1845 		xudc_writel(xudc, val, CTRL);
1846 
1847 		xudc->device_state = USB_STATE_CONFIGURED;
1848 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1849 	}
1850 
1851 	if (usb_endpoint_xfer_isoc(desc)) {
1852 		/*
1853 		 * Pause all bulk endpoints when enabling an isoch endpoint
1854 		 * to ensure the isoch endpoint is allocated enough bandwidth.
1855 		 */
1856 		for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
1857 			if (xudc->ep[i].desc &&
1858 			    usb_endpoint_xfer_bulk(xudc->ep[i].desc))
1859 				ep_pause(xudc, i);
1860 		}
1861 	}
1862 
1863 	ep_reload(xudc, ep->index);
1864 	ep_unpause(xudc, ep->index);
1865 	ep_unhalt(xudc, ep->index);
1866 
1867 	if (usb_endpoint_xfer_isoc(desc)) {
1868 		for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
1869 			if (xudc->ep[i].desc &&
1870 			    usb_endpoint_xfer_bulk(xudc->ep[i].desc))
1871 				ep_unpause(xudc, i);
1872 		}
1873 	}
1874 
1875 out:
1876 	dev_info(xudc->dev, "EP %u (type: %s, dir: %s) enabled\n", ep->index,
1877 		 usb_ep_type_string(usb_endpoint_type(ep->desc)),
1878 		 usb_endpoint_dir_in(ep->desc) ? "in" : "out");
1879 
1880 	return 0;
1881 }
1882 
1883 static int tegra_xudc_ep_enable(struct usb_ep *usb_ep,
1884 				const struct usb_endpoint_descriptor *desc)
1885 {
1886 	struct tegra_xudc_ep *ep;
1887 	struct tegra_xudc *xudc;
1888 	unsigned long flags;
1889 	int ret;
1890 
1891 	if  (!usb_ep || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT))
1892 		return -EINVAL;
1893 
1894 	ep = to_xudc_ep(usb_ep);
1895 	xudc = ep->xudc;
1896 
1897 	spin_lock_irqsave(&xudc->lock, flags);
1898 	if (xudc->powergated) {
1899 		ret = -ESHUTDOWN;
1900 		goto unlock;
1901 	}
1902 
1903 	ret = __tegra_xudc_ep_enable(ep, desc);
1904 unlock:
1905 	spin_unlock_irqrestore(&xudc->lock, flags);
1906 
1907 	return ret;
1908 }
1909 
1910 static struct usb_request *
1911 tegra_xudc_ep_alloc_request(struct usb_ep *usb_ep, gfp_t gfp)
1912 {
1913 	struct tegra_xudc_request *req;
1914 
1915 	req = kzalloc(sizeof(*req), gfp);
1916 	if (!req)
1917 		return NULL;
1918 
1919 	INIT_LIST_HEAD(&req->list);
1920 
1921 	return &req->usb_req;
1922 }
1923 
1924 static void tegra_xudc_ep_free_request(struct usb_ep *usb_ep,
1925 				       struct usb_request *usb_req)
1926 {
1927 	struct tegra_xudc_request *req = to_xudc_req(usb_req);
1928 
1929 	kfree(req);
1930 }
1931 
1932 static const struct usb_ep_ops tegra_xudc_ep_ops = {
1933 	.enable = tegra_xudc_ep_enable,
1934 	.disable = tegra_xudc_ep_disable,
1935 	.alloc_request = tegra_xudc_ep_alloc_request,
1936 	.free_request = tegra_xudc_ep_free_request,
1937 	.queue = tegra_xudc_ep_queue,
1938 	.dequeue = tegra_xudc_ep_dequeue,
1939 	.set_halt = tegra_xudc_ep_set_halt,
1940 };
1941 
1942 static int tegra_xudc_ep0_enable(struct usb_ep *usb_ep,
1943 				 const struct usb_endpoint_descriptor *desc)
1944 {
1945 	return -EBUSY;
1946 }
1947 
1948 static int tegra_xudc_ep0_disable(struct usb_ep *usb_ep)
1949 {
1950 	return -EBUSY;
1951 }
1952 
1953 static const struct usb_ep_ops tegra_xudc_ep0_ops = {
1954 	.enable = tegra_xudc_ep0_enable,
1955 	.disable = tegra_xudc_ep0_disable,
1956 	.alloc_request = tegra_xudc_ep_alloc_request,
1957 	.free_request = tegra_xudc_ep_free_request,
1958 	.queue = tegra_xudc_ep_queue,
1959 	.dequeue = tegra_xudc_ep_dequeue,
1960 	.set_halt = tegra_xudc_ep_set_halt,
1961 };
1962 
1963 static int tegra_xudc_gadget_get_frame(struct usb_gadget *gadget)
1964 {
1965 	struct tegra_xudc *xudc = to_xudc(gadget);
1966 	unsigned long flags;
1967 	int ret;
1968 
1969 	spin_lock_irqsave(&xudc->lock, flags);
1970 	if (xudc->powergated) {
1971 		ret = -ESHUTDOWN;
1972 		goto unlock;
1973 	}
1974 
1975 	ret = (xudc_readl(xudc, MFINDEX) & MFINDEX_FRAME_MASK) >>
1976 		MFINDEX_FRAME_SHIFT;
1977 unlock:
1978 	spin_unlock_irqrestore(&xudc->lock, flags);
1979 
1980 	return ret;
1981 }
1982 
1983 static void tegra_xudc_resume_device_state(struct tegra_xudc *xudc)
1984 {
1985 	unsigned int i;
1986 	u32 val;
1987 
1988 	ep_unpause_all(xudc);
1989 
1990 	/* Direct link to U0. */
1991 	val = xudc_readl(xudc, PORTSC);
1992 	if (((val & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT) != PORTSC_PLS_U0) {
1993 		val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
1994 		val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
1995 		xudc_writel(xudc, val, PORTSC);
1996 	}
1997 
1998 	if (xudc->device_state == USB_STATE_SUSPENDED) {
1999 		xudc->device_state = xudc->resume_state;
2000 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2001 		xudc->resume_state = 0;
2002 	}
2003 
2004 	/*
2005 	 * Doorbells may be dropped if they are sent too soon (< ~200ns)
2006 	 * after unpausing the endpoint.  Wait for 500ns just to be safe.
2007 	 */
2008 	ndelay(500);
2009 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
2010 		tegra_xudc_ep_ring_doorbell(&xudc->ep[i]);
2011 }
2012 
2013 static int tegra_xudc_gadget_wakeup(struct usb_gadget *gadget)
2014 {
2015 	struct tegra_xudc *xudc = to_xudc(gadget);
2016 	unsigned long flags;
2017 	int ret = 0;
2018 	u32 val;
2019 
2020 	spin_lock_irqsave(&xudc->lock, flags);
2021 
2022 	if (xudc->powergated) {
2023 		ret = -ESHUTDOWN;
2024 		goto unlock;
2025 	}
2026 	val = xudc_readl(xudc, PORTPM);
2027 	dev_dbg(xudc->dev, "%s: PORTPM=%#x, speed=%x\n", __func__,
2028 			val, gadget->speed);
2029 
2030 	if (((xudc->gadget.speed <= USB_SPEED_HIGH) &&
2031 	     (val & PORTPM_RWE)) ||
2032 	    ((xudc->gadget.speed == USB_SPEED_SUPER) &&
2033 	     (val & PORTPM_FRWE))) {
2034 		tegra_xudc_resume_device_state(xudc);
2035 
2036 		/* Send Device Notification packet. */
2037 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
2038 			val = DEVNOTIF_LO_TYPE(DEVNOTIF_LO_TYPE_FUNCTION_WAKE)
2039 					     | DEVNOTIF_LO_TRIG;
2040 			xudc_writel(xudc, 0, DEVNOTIF_HI);
2041 			xudc_writel(xudc, val, DEVNOTIF_LO);
2042 		}
2043 	}
2044 
2045 unlock:
2046 	dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
2047 	spin_unlock_irqrestore(&xudc->lock, flags);
2048 
2049 	return ret;
2050 }
2051 
2052 static int tegra_xudc_gadget_pullup(struct usb_gadget *gadget, int is_on)
2053 {
2054 	struct tegra_xudc *xudc = to_xudc(gadget);
2055 	unsigned long flags;
2056 	u32 val;
2057 
2058 	pm_runtime_get_sync(xudc->dev);
2059 
2060 	spin_lock_irqsave(&xudc->lock, flags);
2061 
2062 	if (is_on != xudc->pullup) {
2063 		val = xudc_readl(xudc, CTRL);
2064 		if (is_on)
2065 			val |= CTRL_ENABLE;
2066 		else
2067 			val &= ~CTRL_ENABLE;
2068 		xudc_writel(xudc, val, CTRL);
2069 	}
2070 
2071 	xudc->pullup = is_on;
2072 	dev_dbg(xudc->dev, "%s: pullup:%d", __func__, is_on);
2073 
2074 	spin_unlock_irqrestore(&xudc->lock, flags);
2075 
2076 	pm_runtime_put(xudc->dev);
2077 
2078 	return 0;
2079 }
2080 
2081 static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
2082 				   struct usb_gadget_driver *driver)
2083 {
2084 	struct tegra_xudc *xudc = to_xudc(gadget);
2085 	unsigned long flags;
2086 	u32 val;
2087 	int ret;
2088 	unsigned int i;
2089 
2090 	if (!driver)
2091 		return -EINVAL;
2092 
2093 	pm_runtime_get_sync(xudc->dev);
2094 
2095 	spin_lock_irqsave(&xudc->lock, flags);
2096 
2097 	if (xudc->driver) {
2098 		ret = -EBUSY;
2099 		goto unlock;
2100 	}
2101 
2102 	xudc->setup_state = WAIT_FOR_SETUP;
2103 	xudc->device_state = USB_STATE_DEFAULT;
2104 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2105 
2106 	ret = __tegra_xudc_ep_enable(&xudc->ep[0], &tegra_xudc_ep0_desc);
2107 	if (ret < 0)
2108 		goto unlock;
2109 
2110 	val = xudc_readl(xudc, CTRL);
2111 	val |= CTRL_IE | CTRL_LSE;
2112 	xudc_writel(xudc, val, CTRL);
2113 
2114 	val = xudc_readl(xudc, PORTHALT);
2115 	val |= PORTHALT_STCHG_INTR_EN;
2116 	xudc_writel(xudc, val, PORTHALT);
2117 
2118 	if (xudc->pullup) {
2119 		val = xudc_readl(xudc, CTRL);
2120 		val |= CTRL_ENABLE;
2121 		xudc_writel(xudc, val, CTRL);
2122 	}
2123 
2124 	for (i = 0; i < xudc->soc->num_phys; i++)
2125 		if (xudc->usbphy[i])
2126 			otg_set_peripheral(xudc->usbphy[i]->otg, gadget);
2127 
2128 	xudc->driver = driver;
2129 unlock:
2130 	dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
2131 	spin_unlock_irqrestore(&xudc->lock, flags);
2132 
2133 	pm_runtime_put(xudc->dev);
2134 
2135 	return ret;
2136 }
2137 
2138 static int tegra_xudc_gadget_stop(struct usb_gadget *gadget)
2139 {
2140 	struct tegra_xudc *xudc = to_xudc(gadget);
2141 	unsigned long flags;
2142 	u32 val;
2143 	unsigned int i;
2144 
2145 	pm_runtime_get_sync(xudc->dev);
2146 
2147 	spin_lock_irqsave(&xudc->lock, flags);
2148 
2149 	for (i = 0; i < xudc->soc->num_phys; i++)
2150 		if (xudc->usbphy[i])
2151 			otg_set_peripheral(xudc->usbphy[i]->otg, NULL);
2152 
2153 	val = xudc_readl(xudc, CTRL);
2154 	val &= ~(CTRL_IE | CTRL_ENABLE);
2155 	xudc_writel(xudc, val, CTRL);
2156 
2157 	__tegra_xudc_ep_disable(&xudc->ep[0]);
2158 
2159 	xudc->driver = NULL;
2160 	dev_dbg(xudc->dev, "Gadget stopped");
2161 
2162 	spin_unlock_irqrestore(&xudc->lock, flags);
2163 
2164 	pm_runtime_put(xudc->dev);
2165 
2166 	return 0;
2167 }
2168 
2169 static int tegra_xudc_gadget_vbus_draw(struct usb_gadget *gadget,
2170 						unsigned int m_a)
2171 {
2172 	struct tegra_xudc *xudc = to_xudc(gadget);
2173 
2174 	dev_dbg(xudc->dev, "%s: %u mA\n", __func__, m_a);
2175 
2176 	if (xudc->curr_usbphy && xudc->curr_usbphy->chg_type == SDP_TYPE)
2177 		return usb_phy_set_power(xudc->curr_usbphy, m_a);
2178 
2179 	return 0;
2180 }
2181 
2182 static int tegra_xudc_set_selfpowered(struct usb_gadget *gadget, int is_on)
2183 {
2184 	struct tegra_xudc *xudc = to_xudc(gadget);
2185 
2186 	dev_dbg(xudc->dev, "%s: %d\n", __func__, is_on);
2187 	xudc->selfpowered = !!is_on;
2188 
2189 	return 0;
2190 }
2191 
2192 static const struct usb_gadget_ops tegra_xudc_gadget_ops = {
2193 	.get_frame = tegra_xudc_gadget_get_frame,
2194 	.wakeup = tegra_xudc_gadget_wakeup,
2195 	.pullup = tegra_xudc_gadget_pullup,
2196 	.udc_start = tegra_xudc_gadget_start,
2197 	.udc_stop = tegra_xudc_gadget_stop,
2198 	.vbus_draw = tegra_xudc_gadget_vbus_draw,
2199 	.set_selfpowered = tegra_xudc_set_selfpowered,
2200 };
2201 
2202 static void no_op_complete(struct usb_ep *ep, struct usb_request *req)
2203 {
2204 }
2205 
2206 static int
2207 tegra_xudc_ep0_queue_status(struct tegra_xudc *xudc,
2208 		void (*cmpl)(struct usb_ep *, struct usb_request *))
2209 {
2210 	xudc->ep0_req->usb_req.buf = NULL;
2211 	xudc->ep0_req->usb_req.dma = 0;
2212 	xudc->ep0_req->usb_req.length = 0;
2213 	xudc->ep0_req->usb_req.complete = cmpl;
2214 	xudc->ep0_req->usb_req.context = xudc;
2215 
2216 	return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
2217 }
2218 
2219 static int
2220 tegra_xudc_ep0_queue_data(struct tegra_xudc *xudc, void *buf, size_t len,
2221 		void (*cmpl)(struct usb_ep *, struct usb_request *))
2222 {
2223 	xudc->ep0_req->usb_req.buf = buf;
2224 	xudc->ep0_req->usb_req.length = len;
2225 	xudc->ep0_req->usb_req.complete = cmpl;
2226 	xudc->ep0_req->usb_req.context = xudc;
2227 
2228 	return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
2229 }
2230 
2231 static void tegra_xudc_ep0_req_done(struct tegra_xudc *xudc)
2232 {
2233 	switch (xudc->setup_state) {
2234 	case DATA_STAGE_XFER:
2235 		xudc->setup_state = STATUS_STAGE_RECV;
2236 		tegra_xudc_ep0_queue_status(xudc, no_op_complete);
2237 		break;
2238 	case DATA_STAGE_RECV:
2239 		xudc->setup_state = STATUS_STAGE_XFER;
2240 		tegra_xudc_ep0_queue_status(xudc, no_op_complete);
2241 		break;
2242 	default:
2243 		xudc->setup_state = WAIT_FOR_SETUP;
2244 		break;
2245 	}
2246 }
2247 
2248 static int tegra_xudc_ep0_delegate_req(struct tegra_xudc *xudc,
2249 				       struct usb_ctrlrequest *ctrl)
2250 {
2251 	int ret;
2252 
2253 	spin_unlock(&xudc->lock);
2254 	ret = xudc->driver->setup(&xudc->gadget, ctrl);
2255 	spin_lock(&xudc->lock);
2256 
2257 	return ret;
2258 }
2259 
2260 static void set_feature_complete(struct usb_ep *ep, struct usb_request *req)
2261 {
2262 	struct tegra_xudc *xudc = req->context;
2263 
2264 	if (xudc->test_mode_pattern) {
2265 		xudc_writel(xudc, xudc->test_mode_pattern, PORT_TM);
2266 		xudc->test_mode_pattern = 0;
2267 	}
2268 }
2269 
2270 static int tegra_xudc_ep0_set_feature(struct tegra_xudc *xudc,
2271 				      struct usb_ctrlrequest *ctrl)
2272 {
2273 	bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
2274 	u32 feature = le16_to_cpu(ctrl->wValue);
2275 	u32 index = le16_to_cpu(ctrl->wIndex);
2276 	u32 val, ep;
2277 	int ret;
2278 
2279 	if (le16_to_cpu(ctrl->wLength) != 0)
2280 		return -EINVAL;
2281 
2282 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
2283 	case USB_RECIP_DEVICE:
2284 		switch (feature) {
2285 		case USB_DEVICE_REMOTE_WAKEUP:
2286 			if ((xudc->gadget.speed == USB_SPEED_SUPER) ||
2287 			    (xudc->device_state == USB_STATE_DEFAULT))
2288 				return -EINVAL;
2289 
2290 			val = xudc_readl(xudc, PORTPM);
2291 			if (set)
2292 				val |= PORTPM_RWE;
2293 			else
2294 				val &= ~PORTPM_RWE;
2295 
2296 			xudc_writel(xudc, val, PORTPM);
2297 			break;
2298 		case USB_DEVICE_U1_ENABLE:
2299 		case USB_DEVICE_U2_ENABLE:
2300 			if ((xudc->device_state != USB_STATE_CONFIGURED) ||
2301 			    (xudc->gadget.speed != USB_SPEED_SUPER))
2302 				return -EINVAL;
2303 
2304 			val = xudc_readl(xudc, PORTPM);
2305 			if ((feature == USB_DEVICE_U1_ENABLE) &&
2306 			     xudc->soc->u1_enable) {
2307 				if (set)
2308 					val |= PORTPM_U1E;
2309 				else
2310 					val &= ~PORTPM_U1E;
2311 			}
2312 
2313 			if ((feature == USB_DEVICE_U2_ENABLE) &&
2314 			     xudc->soc->u2_enable) {
2315 				if (set)
2316 					val |= PORTPM_U2E;
2317 				else
2318 					val &= ~PORTPM_U2E;
2319 			}
2320 
2321 			xudc_writel(xudc, val, PORTPM);
2322 			break;
2323 		case USB_DEVICE_TEST_MODE:
2324 			if (xudc->gadget.speed != USB_SPEED_HIGH)
2325 				return -EINVAL;
2326 
2327 			if (!set)
2328 				return -EINVAL;
2329 
2330 			xudc->test_mode_pattern = index >> 8;
2331 			break;
2332 		default:
2333 			return -EINVAL;
2334 		}
2335 
2336 		break;
2337 	case USB_RECIP_INTERFACE:
2338 		if (xudc->device_state != USB_STATE_CONFIGURED)
2339 			return -EINVAL;
2340 
2341 		switch (feature) {
2342 		case USB_INTRF_FUNC_SUSPEND:
2343 			if (set) {
2344 				val = xudc_readl(xudc, PORTPM);
2345 
2346 				if (index & USB_INTRF_FUNC_SUSPEND_RW)
2347 					val |= PORTPM_FRWE;
2348 				else
2349 					val &= ~PORTPM_FRWE;
2350 
2351 				xudc_writel(xudc, val, PORTPM);
2352 			}
2353 
2354 			return tegra_xudc_ep0_delegate_req(xudc, ctrl);
2355 		default:
2356 			return -EINVAL;
2357 		}
2358 
2359 		break;
2360 	case USB_RECIP_ENDPOINT:
2361 		ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
2362 			((index & USB_DIR_IN) ? 1 : 0);
2363 
2364 		if ((xudc->device_state == USB_STATE_DEFAULT) ||
2365 		    ((xudc->device_state == USB_STATE_ADDRESS) &&
2366 		     (index != 0)))
2367 			return -EINVAL;
2368 
2369 		ret = __tegra_xudc_ep_set_halt(&xudc->ep[ep], set);
2370 		if (ret < 0)
2371 			return ret;
2372 		break;
2373 	default:
2374 		return -EINVAL;
2375 	}
2376 
2377 	return tegra_xudc_ep0_queue_status(xudc, set_feature_complete);
2378 }
2379 
2380 static int tegra_xudc_ep0_get_status(struct tegra_xudc *xudc,
2381 				     struct usb_ctrlrequest *ctrl)
2382 {
2383 	struct tegra_xudc_ep_context *ep_ctx;
2384 	u32 val, ep, index = le16_to_cpu(ctrl->wIndex);
2385 	u16 status = 0;
2386 
2387 	if (!(ctrl->bRequestType & USB_DIR_IN))
2388 		return -EINVAL;
2389 
2390 	if ((le16_to_cpu(ctrl->wValue) != 0) ||
2391 	    (le16_to_cpu(ctrl->wLength) != 2))
2392 		return -EINVAL;
2393 
2394 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
2395 	case USB_RECIP_DEVICE:
2396 		val = xudc_readl(xudc, PORTPM);
2397 
2398 		if (xudc->selfpowered)
2399 			status |= BIT(USB_DEVICE_SELF_POWERED);
2400 
2401 		if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
2402 		    (val & PORTPM_RWE))
2403 			status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
2404 
2405 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
2406 			if (val & PORTPM_U1E)
2407 				status |= BIT(USB_DEV_STAT_U1_ENABLED);
2408 			if (val & PORTPM_U2E)
2409 				status |= BIT(USB_DEV_STAT_U2_ENABLED);
2410 		}
2411 		break;
2412 	case USB_RECIP_INTERFACE:
2413 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
2414 			status |= USB_INTRF_STAT_FUNC_RW_CAP;
2415 			val = xudc_readl(xudc, PORTPM);
2416 			if (val & PORTPM_FRWE)
2417 				status |= USB_INTRF_STAT_FUNC_RW;
2418 		}
2419 		break;
2420 	case USB_RECIP_ENDPOINT:
2421 		ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
2422 			((index & USB_DIR_IN) ? 1 : 0);
2423 		ep_ctx = &xudc->ep_context[ep];
2424 
2425 		if ((xudc->device_state != USB_STATE_CONFIGURED) &&
2426 		    ((xudc->device_state != USB_STATE_ADDRESS) || (ep != 0)))
2427 			return -EINVAL;
2428 
2429 		if (ep_ctx_read_state(ep_ctx) == EP_STATE_DISABLED)
2430 			return -EINVAL;
2431 
2432 		if (xudc_readl(xudc, EP_HALT) & BIT(ep))
2433 			status |= BIT(USB_ENDPOINT_HALT);
2434 		break;
2435 	default:
2436 		return -EINVAL;
2437 	}
2438 
2439 	xudc->status_buf = cpu_to_le16(status);
2440 	return tegra_xudc_ep0_queue_data(xudc, &xudc->status_buf,
2441 					 sizeof(xudc->status_buf),
2442 					 no_op_complete);
2443 }
2444 
2445 static void set_sel_complete(struct usb_ep *ep, struct usb_request *req)
2446 {
2447 	/* Nothing to do with SEL values */
2448 }
2449 
2450 static int tegra_xudc_ep0_set_sel(struct tegra_xudc *xudc,
2451 				  struct usb_ctrlrequest *ctrl)
2452 {
2453 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2454 				     USB_TYPE_STANDARD))
2455 		return -EINVAL;
2456 
2457 	if (xudc->device_state == USB_STATE_DEFAULT)
2458 		return -EINVAL;
2459 
2460 	if ((le16_to_cpu(ctrl->wIndex) != 0) ||
2461 	    (le16_to_cpu(ctrl->wValue) != 0) ||
2462 	    (le16_to_cpu(ctrl->wLength) != 6))
2463 		return -EINVAL;
2464 
2465 	return tegra_xudc_ep0_queue_data(xudc, &xudc->sel_timing,
2466 					 sizeof(xudc->sel_timing),
2467 					 set_sel_complete);
2468 }
2469 
2470 static void set_isoch_delay_complete(struct usb_ep *ep, struct usb_request *req)
2471 {
2472 	/* Nothing to do with isoch delay */
2473 }
2474 
2475 static int tegra_xudc_ep0_set_isoch_delay(struct tegra_xudc *xudc,
2476 					  struct usb_ctrlrequest *ctrl)
2477 {
2478 	u32 delay = le16_to_cpu(ctrl->wValue);
2479 
2480 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2481 				   USB_TYPE_STANDARD))
2482 		return -EINVAL;
2483 
2484 	if ((delay > 65535) || (le16_to_cpu(ctrl->wIndex) != 0) ||
2485 	    (le16_to_cpu(ctrl->wLength) != 0))
2486 		return -EINVAL;
2487 
2488 	xudc->isoch_delay = delay;
2489 
2490 	return tegra_xudc_ep0_queue_status(xudc, set_isoch_delay_complete);
2491 }
2492 
2493 static void set_address_complete(struct usb_ep *ep, struct usb_request *req)
2494 {
2495 	struct tegra_xudc *xudc = req->context;
2496 
2497 	if ((xudc->device_state == USB_STATE_DEFAULT) &&
2498 	    (xudc->dev_addr != 0)) {
2499 		xudc->device_state = USB_STATE_ADDRESS;
2500 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2501 	} else if ((xudc->device_state == USB_STATE_ADDRESS) &&
2502 		   (xudc->dev_addr == 0)) {
2503 		xudc->device_state = USB_STATE_DEFAULT;
2504 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2505 	}
2506 }
2507 
2508 static int tegra_xudc_ep0_set_address(struct tegra_xudc *xudc,
2509 				      struct usb_ctrlrequest *ctrl)
2510 {
2511 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2512 	u32 val, addr = le16_to_cpu(ctrl->wValue);
2513 
2514 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2515 				     USB_TYPE_STANDARD))
2516 		return -EINVAL;
2517 
2518 	if ((addr > 127) || (le16_to_cpu(ctrl->wIndex) != 0) ||
2519 	    (le16_to_cpu(ctrl->wLength) != 0))
2520 		return -EINVAL;
2521 
2522 	if (xudc->device_state == USB_STATE_CONFIGURED)
2523 		return -EINVAL;
2524 
2525 	dev_dbg(xudc->dev, "set address: %u\n", addr);
2526 
2527 	xudc->dev_addr = addr;
2528 	val = xudc_readl(xudc, CTRL);
2529 	val &= ~(CTRL_DEVADDR_MASK);
2530 	val |= CTRL_DEVADDR(addr);
2531 	xudc_writel(xudc, val, CTRL);
2532 
2533 	ep_ctx_write_devaddr(ep0->context, addr);
2534 
2535 	return tegra_xudc_ep0_queue_status(xudc, set_address_complete);
2536 }
2537 
2538 static int tegra_xudc_ep0_standard_req(struct tegra_xudc *xudc,
2539 				      struct usb_ctrlrequest *ctrl)
2540 {
2541 	int ret;
2542 
2543 	switch (ctrl->bRequest) {
2544 	case USB_REQ_GET_STATUS:
2545 		dev_dbg(xudc->dev, "USB_REQ_GET_STATUS\n");
2546 		ret = tegra_xudc_ep0_get_status(xudc, ctrl);
2547 		break;
2548 	case USB_REQ_SET_ADDRESS:
2549 		dev_dbg(xudc->dev, "USB_REQ_SET_ADDRESS\n");
2550 		ret = tegra_xudc_ep0_set_address(xudc, ctrl);
2551 		break;
2552 	case USB_REQ_SET_SEL:
2553 		dev_dbg(xudc->dev, "USB_REQ_SET_SEL\n");
2554 		ret = tegra_xudc_ep0_set_sel(xudc, ctrl);
2555 		break;
2556 	case USB_REQ_SET_ISOCH_DELAY:
2557 		dev_dbg(xudc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
2558 		ret = tegra_xudc_ep0_set_isoch_delay(xudc, ctrl);
2559 		break;
2560 	case USB_REQ_CLEAR_FEATURE:
2561 	case USB_REQ_SET_FEATURE:
2562 		dev_dbg(xudc->dev, "USB_REQ_CLEAR/SET_FEATURE\n");
2563 		ret = tegra_xudc_ep0_set_feature(xudc, ctrl);
2564 		break;
2565 	case USB_REQ_SET_CONFIGURATION:
2566 		dev_dbg(xudc->dev, "USB_REQ_SET_CONFIGURATION\n");
2567 		/*
2568 		 * In theory we need to clear RUN bit before status stage of
2569 		 * deconfig request sent, but this seems to be causing problems.
2570 		 * Clear RUN once all endpoints are disabled instead.
2571 		 */
2572 		fallthrough;
2573 	default:
2574 		ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
2575 		break;
2576 	}
2577 
2578 	return ret;
2579 }
2580 
2581 static void tegra_xudc_handle_ep0_setup_packet(struct tegra_xudc *xudc,
2582 					       struct usb_ctrlrequest *ctrl,
2583 					       u16 seq_num)
2584 {
2585 	int ret;
2586 
2587 	xudc->setup_seq_num = seq_num;
2588 
2589 	/* Ensure EP0 is unhalted. */
2590 	ep_unhalt(xudc, 0);
2591 
2592 	/*
2593 	 * On Tegra210, setup packets with sequence numbers 0xfffe or 0xffff
2594 	 * are invalid.  Halt EP0 until we get a valid packet.
2595 	 */
2596 	if (xudc->soc->invalid_seq_num &&
2597 	    (seq_num == 0xfffe || seq_num == 0xffff)) {
2598 		dev_warn(xudc->dev, "invalid sequence number detected\n");
2599 		ep_halt(xudc, 0);
2600 		return;
2601 	}
2602 
2603 	if (ctrl->wLength)
2604 		xudc->setup_state = (ctrl->bRequestType & USB_DIR_IN) ?
2605 			DATA_STAGE_XFER :  DATA_STAGE_RECV;
2606 	else
2607 		xudc->setup_state = STATUS_STAGE_XFER;
2608 
2609 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
2610 		ret = tegra_xudc_ep0_standard_req(xudc, ctrl);
2611 	else
2612 		ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
2613 
2614 	if (ret < 0) {
2615 		dev_warn(xudc->dev, "setup request failed: %d\n", ret);
2616 		xudc->setup_state = WAIT_FOR_SETUP;
2617 		ep_halt(xudc, 0);
2618 	}
2619 }
2620 
2621 static void tegra_xudc_handle_ep0_event(struct tegra_xudc *xudc,
2622 					struct tegra_xudc_trb *event)
2623 {
2624 	struct usb_ctrlrequest *ctrl = (struct usb_ctrlrequest *)event;
2625 	u16 seq_num = trb_read_seq_num(event);
2626 
2627 	if (xudc->setup_state != WAIT_FOR_SETUP) {
2628 		/*
2629 		 * The controller is in the process of handling another
2630 		 * setup request.  Queue subsequent requests and handle
2631 		 * the last one once the controller reports a sequence
2632 		 * number error.
2633 		 */
2634 		memcpy(&xudc->setup_packet.ctrl_req, ctrl, sizeof(*ctrl));
2635 		xudc->setup_packet.seq_num = seq_num;
2636 		xudc->queued_setup_packet = true;
2637 	} else {
2638 		tegra_xudc_handle_ep0_setup_packet(xudc, ctrl, seq_num);
2639 	}
2640 }
2641 
2642 static struct tegra_xudc_request *
2643 trb_to_request(struct tegra_xudc_ep *ep, struct tegra_xudc_trb *trb)
2644 {
2645 	struct tegra_xudc_request *req;
2646 
2647 	list_for_each_entry(req, &ep->queue, list) {
2648 		if (!req->trbs_queued)
2649 			break;
2650 
2651 		if (trb_in_request(ep, req, trb))
2652 			return req;
2653 	}
2654 
2655 	return NULL;
2656 }
2657 
2658 static void tegra_xudc_handle_transfer_completion(struct tegra_xudc *xudc,
2659 						  struct tegra_xudc_ep *ep,
2660 						  struct tegra_xudc_trb *event)
2661 {
2662 	struct tegra_xudc_request *req;
2663 	struct tegra_xudc_trb *trb;
2664 	bool short_packet;
2665 
2666 	short_packet = (trb_read_cmpl_code(event) ==
2667 			TRB_CMPL_CODE_SHORT_PACKET);
2668 
2669 	trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
2670 	req = trb_to_request(ep, trb);
2671 
2672 	/*
2673 	 * TDs are complete on short packet or when the completed TRB is the
2674 	 * last TRB in the TD (the CHAIN bit is unset).
2675 	 */
2676 	if (req && (short_packet || (!trb_read_chain(trb) &&
2677 		(req->trbs_needed == req->trbs_queued)))) {
2678 		struct tegra_xudc_trb *last = req->last_trb;
2679 		unsigned int residual;
2680 
2681 		residual = trb_read_transfer_len(event);
2682 		req->usb_req.actual = req->usb_req.length - residual;
2683 
2684 		dev_dbg(xudc->dev, "bytes transferred %u / %u\n",
2685 			req->usb_req.actual, req->usb_req.length);
2686 
2687 		tegra_xudc_req_done(ep, req, 0);
2688 
2689 		if (ep->desc && usb_endpoint_xfer_control(ep->desc))
2690 			tegra_xudc_ep0_req_done(xudc);
2691 
2692 		/*
2693 		 * Advance the dequeue pointer past the end of the current TD
2694 		 * on short packet completion.
2695 		 */
2696 		if (short_packet) {
2697 			ep->deq_ptr = (last - ep->transfer_ring) + 1;
2698 			if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
2699 				ep->deq_ptr = 0;
2700 		}
2701 	} else if (!req) {
2702 		dev_warn(xudc->dev, "transfer event on dequeued request\n");
2703 	}
2704 
2705 	if (ep->desc)
2706 		tegra_xudc_ep_kick_queue(ep);
2707 }
2708 
2709 static void tegra_xudc_handle_transfer_event(struct tegra_xudc *xudc,
2710 					     struct tegra_xudc_trb *event)
2711 {
2712 	unsigned int ep_index = trb_read_endpoint_id(event);
2713 	struct tegra_xudc_ep *ep = &xudc->ep[ep_index];
2714 	struct tegra_xudc_trb *trb;
2715 	u16 comp_code;
2716 
2717 	if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
2718 		dev_warn(xudc->dev, "transfer event on disabled EP %u\n",
2719 			 ep_index);
2720 		return;
2721 	}
2722 
2723 	/* Update transfer ring dequeue pointer. */
2724 	trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
2725 	comp_code = trb_read_cmpl_code(event);
2726 	if (comp_code != TRB_CMPL_CODE_BABBLE_DETECTED_ERR) {
2727 		ep->deq_ptr = (trb - ep->transfer_ring) + 1;
2728 
2729 		if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
2730 			ep->deq_ptr = 0;
2731 		ep->ring_full = false;
2732 	}
2733 
2734 	switch (comp_code) {
2735 	case TRB_CMPL_CODE_SUCCESS:
2736 	case TRB_CMPL_CODE_SHORT_PACKET:
2737 		tegra_xudc_handle_transfer_completion(xudc, ep, event);
2738 		break;
2739 	case TRB_CMPL_CODE_HOST_REJECTED:
2740 		dev_info(xudc->dev, "stream rejected on EP %u\n", ep_index);
2741 
2742 		ep->stream_rejected = true;
2743 		break;
2744 	case TRB_CMPL_CODE_PRIME_PIPE_RECEIVED:
2745 		dev_info(xudc->dev, "prime pipe received on EP %u\n", ep_index);
2746 
2747 		if (ep->stream_rejected) {
2748 			ep->stream_rejected = false;
2749 			/*
2750 			 * An EP is stopped when a stream is rejected.  Wait
2751 			 * for the EP to report that it is stopped and then
2752 			 * un-stop it.
2753 			 */
2754 			ep_wait_for_stopped(xudc, ep_index);
2755 		}
2756 		tegra_xudc_ep_ring_doorbell(ep);
2757 		break;
2758 	case TRB_CMPL_CODE_BABBLE_DETECTED_ERR:
2759 		/*
2760 		 * Wait for the EP to be stopped so the controller stops
2761 		 * processing doorbells.
2762 		 */
2763 		ep_wait_for_stopped(xudc, ep_index);
2764 		ep->enq_ptr = ep->deq_ptr;
2765 		tegra_xudc_ep_nuke(ep, -EIO);
2766 		fallthrough;
2767 	case TRB_CMPL_CODE_STREAM_NUMP_ERROR:
2768 	case TRB_CMPL_CODE_CTRL_DIR_ERR:
2769 	case TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR:
2770 	case TRB_CMPL_CODE_RING_UNDERRUN:
2771 	case TRB_CMPL_CODE_RING_OVERRUN:
2772 	case TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN:
2773 	case TRB_CMPL_CODE_USB_TRANS_ERR:
2774 	case TRB_CMPL_CODE_TRB_ERR:
2775 		dev_err(xudc->dev, "completion error %#x on EP %u\n",
2776 			comp_code, ep_index);
2777 
2778 		ep_halt(xudc, ep_index);
2779 		break;
2780 	case TRB_CMPL_CODE_CTRL_SEQNUM_ERR:
2781 		dev_info(xudc->dev, "sequence number error\n");
2782 
2783 		/*
2784 		 * Kill any queued control request and skip to the last
2785 		 * setup packet we received.
2786 		 */
2787 		tegra_xudc_ep_nuke(ep, -EINVAL);
2788 		xudc->setup_state = WAIT_FOR_SETUP;
2789 		if (!xudc->queued_setup_packet)
2790 			break;
2791 
2792 		tegra_xudc_handle_ep0_setup_packet(xudc,
2793 						   &xudc->setup_packet.ctrl_req,
2794 						   xudc->setup_packet.seq_num);
2795 		xudc->queued_setup_packet = false;
2796 		break;
2797 	case TRB_CMPL_CODE_STOPPED:
2798 		dev_dbg(xudc->dev, "stop completion code on EP %u\n",
2799 			ep_index);
2800 
2801 		/* Disconnected. */
2802 		tegra_xudc_ep_nuke(ep, -ECONNREFUSED);
2803 		break;
2804 	default:
2805 		dev_dbg(xudc->dev, "completion event %#x on EP %u\n",
2806 			comp_code, ep_index);
2807 		break;
2808 	}
2809 }
2810 
2811 static void tegra_xudc_reset(struct tegra_xudc *xudc)
2812 {
2813 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2814 	dma_addr_t deq_ptr;
2815 	unsigned int i;
2816 
2817 	xudc->setup_state = WAIT_FOR_SETUP;
2818 	xudc->device_state = USB_STATE_DEFAULT;
2819 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2820 
2821 	ep_unpause_all(xudc);
2822 
2823 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
2824 		tegra_xudc_ep_nuke(&xudc->ep[i], -ESHUTDOWN);
2825 
2826 	/*
2827 	 * Reset sequence number and dequeue pointer to flush the transfer
2828 	 * ring.
2829 	 */
2830 	ep0->deq_ptr = ep0->enq_ptr;
2831 	ep0->ring_full = false;
2832 
2833 	xudc->setup_seq_num = 0;
2834 	xudc->queued_setup_packet = false;
2835 
2836 	ep_ctx_write_rsvd(ep0->context, 0);
2837 	ep_ctx_write_partial_td(ep0->context, 0);
2838 	ep_ctx_write_splitxstate(ep0->context, 0);
2839 	ep_ctx_write_seq_num(ep0->context, 0);
2840 
2841 	deq_ptr = trb_virt_to_phys(ep0, &ep0->transfer_ring[ep0->deq_ptr]);
2842 
2843 	if (!dma_mapping_error(xudc->dev, deq_ptr)) {
2844 		ep_ctx_write_deq_ptr(ep0->context, deq_ptr);
2845 		ep_ctx_write_dcs(ep0->context, ep0->pcs);
2846 	}
2847 
2848 	ep_unhalt_all(xudc);
2849 	ep_reload(xudc, 0);
2850 	ep_unpause(xudc, 0);
2851 }
2852 
2853 static void tegra_xudc_port_connect(struct tegra_xudc *xudc)
2854 {
2855 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2856 	u16 maxpacket;
2857 	u32 val;
2858 
2859 	val = (xudc_readl(xudc, PORTSC) & PORTSC_PS_MASK) >> PORTSC_PS_SHIFT;
2860 	switch (val) {
2861 	case PORTSC_PS_LS:
2862 		xudc->gadget.speed = USB_SPEED_LOW;
2863 		break;
2864 	case PORTSC_PS_FS:
2865 		xudc->gadget.speed = USB_SPEED_FULL;
2866 		break;
2867 	case PORTSC_PS_HS:
2868 		xudc->gadget.speed = USB_SPEED_HIGH;
2869 		break;
2870 	case PORTSC_PS_SS:
2871 		xudc->gadget.speed = USB_SPEED_SUPER;
2872 		break;
2873 	default:
2874 		xudc->gadget.speed = USB_SPEED_UNKNOWN;
2875 		break;
2876 	}
2877 
2878 	xudc->device_state = USB_STATE_DEFAULT;
2879 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2880 
2881 	xudc->setup_state = WAIT_FOR_SETUP;
2882 
2883 	if (xudc->gadget.speed == USB_SPEED_SUPER)
2884 		maxpacket = 512;
2885 	else
2886 		maxpacket = 64;
2887 
2888 	ep_ctx_write_max_packet_size(ep0->context, maxpacket);
2889 	tegra_xudc_ep0_desc.wMaxPacketSize = cpu_to_le16(maxpacket);
2890 	usb_ep_set_maxpacket_limit(&ep0->usb_ep, maxpacket);
2891 
2892 	if (!xudc->soc->u1_enable) {
2893 		val = xudc_readl(xudc, PORTPM);
2894 		val &= ~(PORTPM_U1TIMEOUT_MASK);
2895 		xudc_writel(xudc, val, PORTPM);
2896 	}
2897 
2898 	if (!xudc->soc->u2_enable) {
2899 		val = xudc_readl(xudc, PORTPM);
2900 		val &= ~(PORTPM_U2TIMEOUT_MASK);
2901 		xudc_writel(xudc, val, PORTPM);
2902 	}
2903 
2904 	if (xudc->gadget.speed <= USB_SPEED_HIGH) {
2905 		val = xudc_readl(xudc, PORTPM);
2906 		val &= ~(PORTPM_L1S_MASK);
2907 		if (xudc->soc->lpm_enable)
2908 			val |= PORTPM_L1S(PORTPM_L1S_ACCEPT);
2909 		else
2910 			val |= PORTPM_L1S(PORTPM_L1S_NYET);
2911 		xudc_writel(xudc, val, PORTPM);
2912 	}
2913 
2914 	val = xudc_readl(xudc, ST);
2915 	if (val & ST_RC)
2916 		xudc_writel(xudc, ST_RC, ST);
2917 }
2918 
2919 static void tegra_xudc_port_disconnect(struct tegra_xudc *xudc)
2920 {
2921 	tegra_xudc_reset(xudc);
2922 
2923 	if (xudc->driver && xudc->driver->disconnect) {
2924 		spin_unlock(&xudc->lock);
2925 		xudc->driver->disconnect(&xudc->gadget);
2926 		spin_lock(&xudc->lock);
2927 	}
2928 
2929 	xudc->device_state = USB_STATE_NOTATTACHED;
2930 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2931 
2932 	complete(&xudc->disconnect_complete);
2933 }
2934 
2935 static void tegra_xudc_port_reset(struct tegra_xudc *xudc)
2936 {
2937 	tegra_xudc_reset(xudc);
2938 
2939 	if (xudc->driver) {
2940 		spin_unlock(&xudc->lock);
2941 		usb_gadget_udc_reset(&xudc->gadget, xudc->driver);
2942 		spin_lock(&xudc->lock);
2943 	}
2944 
2945 	tegra_xudc_port_connect(xudc);
2946 }
2947 
2948 static void tegra_xudc_port_suspend(struct tegra_xudc *xudc)
2949 {
2950 	dev_dbg(xudc->dev, "port suspend\n");
2951 
2952 	xudc->resume_state = xudc->device_state;
2953 	xudc->device_state = USB_STATE_SUSPENDED;
2954 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2955 
2956 	if (xudc->driver->suspend) {
2957 		spin_unlock(&xudc->lock);
2958 		xudc->driver->suspend(&xudc->gadget);
2959 		spin_lock(&xudc->lock);
2960 	}
2961 }
2962 
2963 static void tegra_xudc_port_resume(struct tegra_xudc *xudc)
2964 {
2965 	dev_dbg(xudc->dev, "port resume\n");
2966 
2967 	tegra_xudc_resume_device_state(xudc);
2968 
2969 	if (xudc->driver->resume) {
2970 		spin_unlock(&xudc->lock);
2971 		xudc->driver->resume(&xudc->gadget);
2972 		spin_lock(&xudc->lock);
2973 	}
2974 }
2975 
2976 static inline void clear_port_change(struct tegra_xudc *xudc, u32 flag)
2977 {
2978 	u32 val;
2979 
2980 	val = xudc_readl(xudc, PORTSC);
2981 	val &= ~PORTSC_CHANGE_MASK;
2982 	val |= flag;
2983 	xudc_writel(xudc, val, PORTSC);
2984 }
2985 
2986 static void __tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
2987 {
2988 	u32 portsc, porthalt;
2989 
2990 	porthalt = xudc_readl(xudc, PORTHALT);
2991 	if ((porthalt & PORTHALT_STCHG_REQ) &&
2992 	    (porthalt & PORTHALT_HALT_LTSSM)) {
2993 		dev_dbg(xudc->dev, "STCHG_REQ, PORTHALT = %#x\n", porthalt);
2994 		porthalt &= ~PORTHALT_HALT_LTSSM;
2995 		xudc_writel(xudc, porthalt, PORTHALT);
2996 	}
2997 
2998 	portsc = xudc_readl(xudc, PORTSC);
2999 	if ((portsc & PORTSC_PRC) && (portsc & PORTSC_PR)) {
3000 		dev_dbg(xudc->dev, "PRC, PR, PORTSC = %#x\n", portsc);
3001 		clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
3002 #define TOGGLE_VBUS_WAIT_MS 100
3003 		if (xudc->soc->port_reset_quirk) {
3004 			schedule_delayed_work(&xudc->port_reset_war_work,
3005 				msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
3006 			xudc->wait_for_sec_prc = 1;
3007 		}
3008 	}
3009 
3010 	if ((portsc & PORTSC_PRC) && !(portsc & PORTSC_PR)) {
3011 		dev_dbg(xudc->dev, "PRC, Not PR, PORTSC = %#x\n", portsc);
3012 		clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
3013 		tegra_xudc_port_reset(xudc);
3014 		cancel_delayed_work(&xudc->port_reset_war_work);
3015 		xudc->wait_for_sec_prc = 0;
3016 	}
3017 
3018 	portsc = xudc_readl(xudc, PORTSC);
3019 	if (portsc & PORTSC_WRC) {
3020 		dev_dbg(xudc->dev, "WRC, PORTSC = %#x\n", portsc);
3021 		clear_port_change(xudc, PORTSC_WRC | PORTSC_PED);
3022 		if (!(xudc_readl(xudc, PORTSC) & PORTSC_WPR))
3023 			tegra_xudc_port_reset(xudc);
3024 	}
3025 
3026 	portsc = xudc_readl(xudc, PORTSC);
3027 	if (portsc & PORTSC_CSC) {
3028 		dev_dbg(xudc->dev, "CSC, PORTSC = %#x\n", portsc);
3029 		clear_port_change(xudc, PORTSC_CSC);
3030 
3031 		if (portsc & PORTSC_CCS)
3032 			tegra_xudc_port_connect(xudc);
3033 		else
3034 			tegra_xudc_port_disconnect(xudc);
3035 
3036 		if (xudc->wait_csc) {
3037 			cancel_delayed_work(&xudc->plc_reset_work);
3038 			xudc->wait_csc = false;
3039 		}
3040 	}
3041 
3042 	portsc = xudc_readl(xudc, PORTSC);
3043 	if (portsc & PORTSC_PLC) {
3044 		u32 pls = (portsc & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT;
3045 
3046 		dev_dbg(xudc->dev, "PLC, PORTSC = %#x\n", portsc);
3047 		clear_port_change(xudc, PORTSC_PLC);
3048 		switch (pls) {
3049 		case PORTSC_PLS_U3:
3050 			tegra_xudc_port_suspend(xudc);
3051 			break;
3052 		case PORTSC_PLS_U0:
3053 			if (xudc->gadget.speed < USB_SPEED_SUPER)
3054 				tegra_xudc_port_resume(xudc);
3055 			break;
3056 		case PORTSC_PLS_RESUME:
3057 			if (xudc->gadget.speed == USB_SPEED_SUPER)
3058 				tegra_xudc_port_resume(xudc);
3059 			break;
3060 		case PORTSC_PLS_INACTIVE:
3061 			schedule_delayed_work(&xudc->plc_reset_work,
3062 					msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
3063 			xudc->wait_csc = true;
3064 			break;
3065 		default:
3066 			break;
3067 		}
3068 	}
3069 
3070 	if (portsc & PORTSC_CEC) {
3071 		dev_warn(xudc->dev, "CEC, PORTSC = %#x\n", portsc);
3072 		clear_port_change(xudc, PORTSC_CEC);
3073 	}
3074 
3075 	dev_dbg(xudc->dev, "PORTSC = %#x\n", xudc_readl(xudc, PORTSC));
3076 }
3077 
3078 static void tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
3079 {
3080 	while ((xudc_readl(xudc, PORTSC) & PORTSC_CHANGE_MASK) ||
3081 	       (xudc_readl(xudc, PORTHALT) & PORTHALT_STCHG_REQ))
3082 		__tegra_xudc_handle_port_status(xudc);
3083 }
3084 
3085 static void tegra_xudc_handle_event(struct tegra_xudc *xudc,
3086 				    struct tegra_xudc_trb *event)
3087 {
3088 	u32 type = trb_read_type(event);
3089 
3090 	dump_trb(xudc, "EVENT", event);
3091 
3092 	switch (type) {
3093 	case TRB_TYPE_PORT_STATUS_CHANGE_EVENT:
3094 		tegra_xudc_handle_port_status(xudc);
3095 		break;
3096 	case TRB_TYPE_TRANSFER_EVENT:
3097 		tegra_xudc_handle_transfer_event(xudc, event);
3098 		break;
3099 	case TRB_TYPE_SETUP_PACKET_EVENT:
3100 		tegra_xudc_handle_ep0_event(xudc, event);
3101 		break;
3102 	default:
3103 		dev_info(xudc->dev, "Unrecognized TRB type = %#x\n", type);
3104 		break;
3105 	}
3106 }
3107 
3108 static void tegra_xudc_process_event_ring(struct tegra_xudc *xudc)
3109 {
3110 	struct tegra_xudc_trb *event;
3111 	dma_addr_t erdp;
3112 
3113 	while (true) {
3114 		event = xudc->event_ring[xudc->event_ring_index] +
3115 			xudc->event_ring_deq_ptr;
3116 
3117 		if (trb_read_cycle(event) != xudc->ccs)
3118 			break;
3119 
3120 		tegra_xudc_handle_event(xudc, event);
3121 
3122 		xudc->event_ring_deq_ptr++;
3123 		if (xudc->event_ring_deq_ptr == XUDC_EVENT_RING_SIZE) {
3124 			xudc->event_ring_deq_ptr = 0;
3125 			xudc->event_ring_index++;
3126 		}
3127 
3128 		if (xudc->event_ring_index == XUDC_NR_EVENT_RINGS) {
3129 			xudc->event_ring_index = 0;
3130 			xudc->ccs = !xudc->ccs;
3131 		}
3132 	}
3133 
3134 	erdp = xudc->event_ring_phys[xudc->event_ring_index] +
3135 		xudc->event_ring_deq_ptr * sizeof(*event);
3136 
3137 	xudc_writel(xudc, upper_32_bits(erdp), ERDPHI);
3138 	xudc_writel(xudc, lower_32_bits(erdp) | ERDPLO_EHB, ERDPLO);
3139 }
3140 
3141 static irqreturn_t tegra_xudc_irq(int irq, void *data)
3142 {
3143 	struct tegra_xudc *xudc = data;
3144 	unsigned long flags;
3145 	u32 val;
3146 
3147 	val = xudc_readl(xudc, ST);
3148 	if (!(val & ST_IP))
3149 		return IRQ_NONE;
3150 	xudc_writel(xudc, ST_IP, ST);
3151 
3152 	spin_lock_irqsave(&xudc->lock, flags);
3153 	tegra_xudc_process_event_ring(xudc);
3154 	spin_unlock_irqrestore(&xudc->lock, flags);
3155 
3156 	return IRQ_HANDLED;
3157 }
3158 
3159 static int tegra_xudc_alloc_ep(struct tegra_xudc *xudc, unsigned int index)
3160 {
3161 	struct tegra_xudc_ep *ep = &xudc->ep[index];
3162 
3163 	ep->xudc = xudc;
3164 	ep->index = index;
3165 	ep->context = &xudc->ep_context[index];
3166 	INIT_LIST_HEAD(&ep->queue);
3167 
3168 	/*
3169 	 * EP1 would be the input endpoint corresponding to EP0, but since
3170 	 * EP0 is bi-directional, EP1 is unused.
3171 	 */
3172 	if (index == 1)
3173 		return 0;
3174 
3175 	ep->transfer_ring = dma_pool_alloc(xudc->transfer_ring_pool,
3176 					   GFP_KERNEL,
3177 					   &ep->transfer_ring_phys);
3178 	if (!ep->transfer_ring)
3179 		return -ENOMEM;
3180 
3181 	if (index) {
3182 		snprintf(ep->name, sizeof(ep->name), "ep%u%s", index / 2,
3183 			 (index % 2 == 0) ? "out" : "in");
3184 		ep->usb_ep.name = ep->name;
3185 		usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
3186 		ep->usb_ep.max_streams = 16;
3187 		ep->usb_ep.ops = &tegra_xudc_ep_ops;
3188 		ep->usb_ep.caps.type_bulk = true;
3189 		ep->usb_ep.caps.type_int = true;
3190 		if (index & 1)
3191 			ep->usb_ep.caps.dir_in = true;
3192 		else
3193 			ep->usb_ep.caps.dir_out = true;
3194 		list_add_tail(&ep->usb_ep.ep_list, &xudc->gadget.ep_list);
3195 	} else {
3196 		strscpy(ep->name, "ep0", 3);
3197 		ep->usb_ep.name = ep->name;
3198 		usb_ep_set_maxpacket_limit(&ep->usb_ep, 512);
3199 		ep->usb_ep.ops = &tegra_xudc_ep0_ops;
3200 		ep->usb_ep.caps.type_control = true;
3201 		ep->usb_ep.caps.dir_in = true;
3202 		ep->usb_ep.caps.dir_out = true;
3203 	}
3204 
3205 	return 0;
3206 }
3207 
3208 static void tegra_xudc_free_ep(struct tegra_xudc *xudc, unsigned int index)
3209 {
3210 	struct tegra_xudc_ep *ep = &xudc->ep[index];
3211 
3212 	/*
3213 	 * EP1 would be the input endpoint corresponding to EP0, but since
3214 	 * EP0 is bi-directional, EP1 is unused.
3215 	 */
3216 	if (index == 1)
3217 		return;
3218 
3219 	dma_pool_free(xudc->transfer_ring_pool, ep->transfer_ring,
3220 		      ep->transfer_ring_phys);
3221 }
3222 
3223 static int tegra_xudc_alloc_eps(struct tegra_xudc *xudc)
3224 {
3225 	struct usb_request *req;
3226 	unsigned int i;
3227 	int err;
3228 
3229 	xudc->ep_context =
3230 		dma_alloc_coherent(xudc->dev, XUDC_NR_EPS *
3231 				    sizeof(*xudc->ep_context),
3232 				    &xudc->ep_context_phys, GFP_KERNEL);
3233 	if (!xudc->ep_context)
3234 		return -ENOMEM;
3235 
3236 	xudc->transfer_ring_pool =
3237 		dmam_pool_create(dev_name(xudc->dev), xudc->dev,
3238 				 XUDC_TRANSFER_RING_SIZE *
3239 				 sizeof(struct tegra_xudc_trb),
3240 				 sizeof(struct tegra_xudc_trb), 0);
3241 	if (!xudc->transfer_ring_pool) {
3242 		err = -ENOMEM;
3243 		goto free_ep_context;
3244 	}
3245 
3246 	INIT_LIST_HEAD(&xudc->gadget.ep_list);
3247 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
3248 		err = tegra_xudc_alloc_ep(xudc, i);
3249 		if (err < 0)
3250 			goto free_eps;
3251 	}
3252 
3253 	req = tegra_xudc_ep_alloc_request(&xudc->ep[0].usb_ep, GFP_KERNEL);
3254 	if (!req) {
3255 		err = -ENOMEM;
3256 		goto free_eps;
3257 	}
3258 	xudc->ep0_req = to_xudc_req(req);
3259 
3260 	return 0;
3261 
3262 free_eps:
3263 	for (; i > 0; i--)
3264 		tegra_xudc_free_ep(xudc, i - 1);
3265 free_ep_context:
3266 	dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
3267 			  xudc->ep_context, xudc->ep_context_phys);
3268 	return err;
3269 }
3270 
3271 static void tegra_xudc_init_eps(struct tegra_xudc *xudc)
3272 {
3273 	xudc_writel(xudc, lower_32_bits(xudc->ep_context_phys), ECPLO);
3274 	xudc_writel(xudc, upper_32_bits(xudc->ep_context_phys), ECPHI);
3275 }
3276 
3277 static void tegra_xudc_free_eps(struct tegra_xudc *xudc)
3278 {
3279 	unsigned int i;
3280 
3281 	tegra_xudc_ep_free_request(&xudc->ep[0].usb_ep,
3282 				   &xudc->ep0_req->usb_req);
3283 
3284 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
3285 		tegra_xudc_free_ep(xudc, i);
3286 
3287 	dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
3288 			  xudc->ep_context, xudc->ep_context_phys);
3289 }
3290 
3291 static int tegra_xudc_alloc_event_ring(struct tegra_xudc *xudc)
3292 {
3293 	unsigned int i;
3294 
3295 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3296 		xudc->event_ring[i] =
3297 			dma_alloc_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3298 					   sizeof(*xudc->event_ring[i]),
3299 					   &xudc->event_ring_phys[i],
3300 					   GFP_KERNEL);
3301 		if (!xudc->event_ring[i])
3302 			goto free_dma;
3303 	}
3304 
3305 	return 0;
3306 
3307 free_dma:
3308 	for (; i > 0; i--) {
3309 		dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3310 				  sizeof(*xudc->event_ring[i - 1]),
3311 				  xudc->event_ring[i - 1],
3312 				  xudc->event_ring_phys[i - 1]);
3313 	}
3314 	return -ENOMEM;
3315 }
3316 
3317 static void tegra_xudc_init_event_ring(struct tegra_xudc *xudc)
3318 {
3319 	unsigned int i;
3320 	u32 val;
3321 
3322 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3323 		memset(xudc->event_ring[i], 0, XUDC_EVENT_RING_SIZE *
3324 		       sizeof(*xudc->event_ring[i]));
3325 
3326 		val = xudc_readl(xudc, ERSTSZ);
3327 		val &= ~(ERSTSZ_ERSTXSZ_MASK << ERSTSZ_ERSTXSZ_SHIFT(i));
3328 		val |= XUDC_EVENT_RING_SIZE << ERSTSZ_ERSTXSZ_SHIFT(i);
3329 		xudc_writel(xudc, val, ERSTSZ);
3330 
3331 		xudc_writel(xudc, lower_32_bits(xudc->event_ring_phys[i]),
3332 			    ERSTXBALO(i));
3333 		xudc_writel(xudc, upper_32_bits(xudc->event_ring_phys[i]),
3334 			    ERSTXBAHI(i));
3335 	}
3336 
3337 	val = lower_32_bits(xudc->event_ring_phys[0]);
3338 	xudc_writel(xudc, val, ERDPLO);
3339 	val |= EREPLO_ECS;
3340 	xudc_writel(xudc, val, EREPLO);
3341 
3342 	val = upper_32_bits(xudc->event_ring_phys[0]);
3343 	xudc_writel(xudc, val, ERDPHI);
3344 	xudc_writel(xudc, val, EREPHI);
3345 
3346 	xudc->ccs = true;
3347 	xudc->event_ring_index = 0;
3348 	xudc->event_ring_deq_ptr = 0;
3349 }
3350 
3351 static void tegra_xudc_free_event_ring(struct tegra_xudc *xudc)
3352 {
3353 	unsigned int i;
3354 
3355 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3356 		dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3357 				  sizeof(*xudc->event_ring[i]),
3358 				  xudc->event_ring[i],
3359 				  xudc->event_ring_phys[i]);
3360 	}
3361 }
3362 
3363 static void tegra_xudc_fpci_ipfs_init(struct tegra_xudc *xudc)
3364 {
3365 	u32 val;
3366 
3367 	if (xudc->soc->has_ipfs) {
3368 		val = ipfs_readl(xudc, XUSB_DEV_CONFIGURATION_0);
3369 		val |= XUSB_DEV_CONFIGURATION_0_EN_FPCI;
3370 		ipfs_writel(xudc, val, XUSB_DEV_CONFIGURATION_0);
3371 		usleep_range(10, 15);
3372 	}
3373 
3374 	/* Enable bus master */
3375 	val = XUSB_DEV_CFG_1_IO_SPACE_EN | XUSB_DEV_CFG_1_MEMORY_SPACE_EN |
3376 		XUSB_DEV_CFG_1_BUS_MASTER_EN;
3377 	fpci_writel(xudc, val, XUSB_DEV_CFG_1);
3378 
3379 	/* Program BAR0 space */
3380 	val = fpci_readl(xudc, XUSB_DEV_CFG_4);
3381 	val &= ~(XUSB_DEV_CFG_4_BASE_ADDR_MASK);
3382 	val |= xudc->phys_base & (XUSB_DEV_CFG_4_BASE_ADDR_MASK);
3383 
3384 	fpci_writel(xudc, val, XUSB_DEV_CFG_4);
3385 	fpci_writel(xudc, upper_32_bits(xudc->phys_base), XUSB_DEV_CFG_5);
3386 
3387 	usleep_range(100, 200);
3388 
3389 	if (xudc->soc->has_ipfs) {
3390 		/* Enable interrupt assertion */
3391 		val = ipfs_readl(xudc, XUSB_DEV_INTR_MASK_0);
3392 		val |= XUSB_DEV_INTR_MASK_0_IP_INT_MASK;
3393 		ipfs_writel(xudc, val, XUSB_DEV_INTR_MASK_0);
3394 	}
3395 }
3396 
3397 static void tegra_xudc_device_params_init(struct tegra_xudc *xudc)
3398 {
3399 	u32 val, imod;
3400 
3401 	if (xudc->soc->has_ipfs) {
3402 		val = xudc_readl(xudc, BLCG);
3403 		val |= BLCG_ALL;
3404 		val &= ~(BLCG_DFPCI | BLCG_UFPCI | BLCG_FE |
3405 				BLCG_COREPLL_PWRDN);
3406 		val |= BLCG_IOPLL_0_PWRDN;
3407 		val |= BLCG_IOPLL_1_PWRDN;
3408 		val |= BLCG_IOPLL_2_PWRDN;
3409 
3410 		xudc_writel(xudc, val, BLCG);
3411 	}
3412 
3413 	if (xudc->soc->port_speed_quirk)
3414 		tegra_xudc_limit_port_speed(xudc);
3415 
3416 	/* Set a reasonable U3 exit timer value. */
3417 	val = xudc_readl(xudc, SSPX_CORE_PADCTL4);
3418 	val &= ~(SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK);
3419 	val |= SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(0x5dc0);
3420 	xudc_writel(xudc, val, SSPX_CORE_PADCTL4);
3421 
3422 	/* Default ping LFPS tBurst is too large. */
3423 	val = xudc_readl(xudc, SSPX_CORE_CNT0);
3424 	val &= ~(SSPX_CORE_CNT0_PING_TBURST_MASK);
3425 	val |= SSPX_CORE_CNT0_PING_TBURST(0xa);
3426 	xudc_writel(xudc, val, SSPX_CORE_CNT0);
3427 
3428 	/* Default tPortConfiguration timeout is too small. */
3429 	val = xudc_readl(xudc, SSPX_CORE_CNT30);
3430 	val &= ~(SSPX_CORE_CNT30_LMPITP_TIMER_MASK);
3431 	val |= SSPX_CORE_CNT30_LMPITP_TIMER(0x978);
3432 	xudc_writel(xudc, val, SSPX_CORE_CNT30);
3433 
3434 	if (xudc->soc->lpm_enable) {
3435 		/* Set L1 resume duration to 95 us. */
3436 		val = xudc_readl(xudc, HSFSPI_COUNT13);
3437 		val &= ~(HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK);
3438 		val |= HSFSPI_COUNT13_U2_RESUME_K_DURATION(0x2c88);
3439 		xudc_writel(xudc, val, HSFSPI_COUNT13);
3440 	}
3441 
3442 	/*
3443 	 * Compliance suite appears to be violating polling LFPS tBurst max
3444 	 * of 1.4us.  Send 1.45us instead.
3445 	 */
3446 	val = xudc_readl(xudc, SSPX_CORE_CNT32);
3447 	val &= ~(SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK);
3448 	val |= SSPX_CORE_CNT32_POLL_TBURST_MAX(0xb0);
3449 	xudc_writel(xudc, val, SSPX_CORE_CNT32);
3450 
3451 	/* Direct HS/FS port instance to RxDetect. */
3452 	val = xudc_readl(xudc, CFG_DEV_FE);
3453 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3454 	val |= CFG_DEV_FE_PORTREGSEL(CFG_DEV_FE_PORTREGSEL_HSFS_PI);
3455 	xudc_writel(xudc, val, CFG_DEV_FE);
3456 
3457 	val = xudc_readl(xudc, PORTSC);
3458 	val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
3459 	val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
3460 	xudc_writel(xudc, val, PORTSC);
3461 
3462 	/* Direct SS port instance to RxDetect. */
3463 	val = xudc_readl(xudc, CFG_DEV_FE);
3464 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3465 	val |= CFG_DEV_FE_PORTREGSEL_SS_PI & CFG_DEV_FE_PORTREGSEL_MASK;
3466 	xudc_writel(xudc, val, CFG_DEV_FE);
3467 
3468 	val = xudc_readl(xudc, PORTSC);
3469 	val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
3470 	val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
3471 	xudc_writel(xudc, val, PORTSC);
3472 
3473 	/* Restore port instance. */
3474 	val = xudc_readl(xudc, CFG_DEV_FE);
3475 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3476 	xudc_writel(xudc, val, CFG_DEV_FE);
3477 
3478 	/*
3479 	 * Enable INFINITE_SS_RETRY to prevent device from entering
3480 	 * Disabled.Error when attached to buggy SuperSpeed hubs.
3481 	 */
3482 	val = xudc_readl(xudc, CFG_DEV_FE);
3483 	val |= CFG_DEV_FE_INFINITE_SS_RETRY;
3484 	xudc_writel(xudc, val, CFG_DEV_FE);
3485 
3486 	/* Set interrupt moderation. */
3487 	imod = XUDC_INTERRUPT_MODERATION_US * 4;
3488 	val = xudc_readl(xudc, RT_IMOD);
3489 	val &= ~((RT_IMOD_IMODI_MASK) | (RT_IMOD_IMODC_MASK));
3490 	val |= (RT_IMOD_IMODI(imod) | RT_IMOD_IMODC(imod));
3491 	xudc_writel(xudc, val, RT_IMOD);
3492 
3493 	/* increase SSPI transaction timeout from 32us to 512us */
3494 	val = xudc_readl(xudc, CFG_DEV_SSPI_XFER);
3495 	val &= ~(CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK);
3496 	val |= CFG_DEV_SSPI_XFER_ACKTIMEOUT(0xf000);
3497 	xudc_writel(xudc, val, CFG_DEV_SSPI_XFER);
3498 }
3499 
3500 static int tegra_xudc_phy_get(struct tegra_xudc *xudc)
3501 {
3502 	int err = 0, usb3_companion_port;
3503 	unsigned int i, j;
3504 
3505 	xudc->utmi_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3506 					   sizeof(*xudc->utmi_phy), GFP_KERNEL);
3507 	if (!xudc->utmi_phy)
3508 		return -ENOMEM;
3509 
3510 	xudc->usb3_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3511 					   sizeof(*xudc->usb3_phy), GFP_KERNEL);
3512 	if (!xudc->usb3_phy)
3513 		return -ENOMEM;
3514 
3515 	xudc->usbphy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3516 					   sizeof(*xudc->usbphy), GFP_KERNEL);
3517 	if (!xudc->usbphy)
3518 		return -ENOMEM;
3519 
3520 	xudc->vbus_nb.notifier_call = tegra_xudc_vbus_notify;
3521 
3522 	for (i = 0; i < xudc->soc->num_phys; i++) {
3523 		char phy_name[] = "usb.-.";
3524 
3525 		/* Get USB2 phy */
3526 		snprintf(phy_name, sizeof(phy_name), "usb2-%d", i);
3527 		xudc->utmi_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
3528 		if (IS_ERR(xudc->utmi_phy[i])) {
3529 			err = PTR_ERR(xudc->utmi_phy[i]);
3530 			dev_err_probe(xudc->dev, err,
3531 				"failed to get PHY for phy-name usb2-%d\n", i);
3532 			goto clean_up;
3533 		} else if (xudc->utmi_phy[i]) {
3534 			/* Get usb-phy, if utmi phy is available */
3535 			xudc->usbphy[i] = devm_usb_get_phy_by_node(xudc->dev,
3536 						xudc->utmi_phy[i]->dev.of_node,
3537 						NULL);
3538 			if (IS_ERR(xudc->usbphy[i])) {
3539 				err = PTR_ERR(xudc->usbphy[i]);
3540 				dev_err_probe(xudc->dev, err,
3541 					      "failed to get usbphy-%d\n", i);
3542 				goto clean_up;
3543 			}
3544 		} else if (!xudc->utmi_phy[i]) {
3545 			/* if utmi phy is not available, ignore USB3 phy get */
3546 			continue;
3547 		}
3548 
3549 		/* Get USB3 phy */
3550 		usb3_companion_port = tegra_xusb_padctl_get_usb3_companion(xudc->padctl, i);
3551 		if (usb3_companion_port < 0)
3552 			continue;
3553 
3554 		for (j = 0; j < xudc->soc->num_phys; j++) {
3555 			snprintf(phy_name, sizeof(phy_name), "usb3-%d", j);
3556 			xudc->usb3_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
3557 			if (IS_ERR(xudc->usb3_phy[i])) {
3558 				err = PTR_ERR(xudc->usb3_phy[i]);
3559 				dev_err_probe(xudc->dev, err,
3560 					"failed to get PHY for phy-name usb3-%d\n", j);
3561 				goto clean_up;
3562 			} else if (xudc->usb3_phy[i]) {
3563 				int usb2_port =
3564 					tegra_xusb_padctl_get_port_number(xudc->utmi_phy[i]);
3565 				int usb3_port =
3566 					tegra_xusb_padctl_get_port_number(xudc->usb3_phy[i]);
3567 				if (usb3_port == usb3_companion_port) {
3568 					dev_dbg(xudc->dev, "USB2 port %d is paired with USB3 port %d for device mode port %d\n",
3569 					 usb2_port, usb3_port, i);
3570 					break;
3571 				}
3572 			}
3573 		}
3574 	}
3575 
3576 	return err;
3577 
3578 clean_up:
3579 	for (i = 0; i < xudc->soc->num_phys; i++) {
3580 		xudc->usb3_phy[i] = NULL;
3581 		xudc->utmi_phy[i] = NULL;
3582 		xudc->usbphy[i] = NULL;
3583 	}
3584 
3585 	return err;
3586 }
3587 
3588 static void tegra_xudc_phy_exit(struct tegra_xudc *xudc)
3589 {
3590 	unsigned int i;
3591 
3592 	for (i = 0; i < xudc->soc->num_phys; i++) {
3593 		phy_exit(xudc->usb3_phy[i]);
3594 		phy_exit(xudc->utmi_phy[i]);
3595 	}
3596 }
3597 
3598 static int tegra_xudc_phy_init(struct tegra_xudc *xudc)
3599 {
3600 	int err;
3601 	unsigned int i;
3602 
3603 	for (i = 0; i < xudc->soc->num_phys; i++) {
3604 		err = phy_init(xudc->utmi_phy[i]);
3605 		if (err < 0) {
3606 			dev_err(xudc->dev, "UTMI PHY #%u initialization failed: %d\n", i, err);
3607 			goto exit_phy;
3608 		}
3609 
3610 		err = phy_init(xudc->usb3_phy[i]);
3611 		if (err < 0) {
3612 			dev_err(xudc->dev, "USB3 PHY #%u initialization failed: %d\n", i, err);
3613 			goto exit_phy;
3614 		}
3615 	}
3616 	return 0;
3617 
3618 exit_phy:
3619 	tegra_xudc_phy_exit(xudc);
3620 	return err;
3621 }
3622 
3623 static const char * const tegra210_xudc_supply_names[] = {
3624 	"hvdd-usb",
3625 	"avddio-usb",
3626 };
3627 
3628 static const char * const tegra210_xudc_clock_names[] = {
3629 	"dev",
3630 	"ss",
3631 	"ss_src",
3632 	"hs_src",
3633 	"fs_src",
3634 };
3635 
3636 static const char * const tegra186_xudc_clock_names[] = {
3637 	"dev",
3638 	"ss",
3639 	"ss_src",
3640 	"fs_src",
3641 };
3642 
3643 static struct tegra_xudc_soc tegra210_xudc_soc_data = {
3644 	.supply_names = tegra210_xudc_supply_names,
3645 	.num_supplies = ARRAY_SIZE(tegra210_xudc_supply_names),
3646 	.clock_names = tegra210_xudc_clock_names,
3647 	.num_clks = ARRAY_SIZE(tegra210_xudc_clock_names),
3648 	.num_phys = 4,
3649 	.u1_enable = false,
3650 	.u2_enable = true,
3651 	.lpm_enable = false,
3652 	.invalid_seq_num = true,
3653 	.pls_quirk = true,
3654 	.port_reset_quirk = true,
3655 	.port_speed_quirk = false,
3656 	.has_ipfs = true,
3657 };
3658 
3659 static struct tegra_xudc_soc tegra186_xudc_soc_data = {
3660 	.clock_names = tegra186_xudc_clock_names,
3661 	.num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
3662 	.num_phys = 4,
3663 	.u1_enable = true,
3664 	.u2_enable = true,
3665 	.lpm_enable = false,
3666 	.invalid_seq_num = false,
3667 	.pls_quirk = false,
3668 	.port_reset_quirk = false,
3669 	.port_speed_quirk = false,
3670 	.has_ipfs = false,
3671 };
3672 
3673 static struct tegra_xudc_soc tegra194_xudc_soc_data = {
3674 	.clock_names = tegra186_xudc_clock_names,
3675 	.num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
3676 	.num_phys = 4,
3677 	.u1_enable = true,
3678 	.u2_enable = true,
3679 	.lpm_enable = true,
3680 	.invalid_seq_num = false,
3681 	.pls_quirk = false,
3682 	.port_reset_quirk = false,
3683 	.port_speed_quirk = true,
3684 	.has_ipfs = false,
3685 };
3686 
3687 static struct tegra_xudc_soc tegra234_xudc_soc_data = {
3688 	.clock_names = tegra186_xudc_clock_names,
3689 	.num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
3690 	.num_phys = 4,
3691 	.u1_enable = true,
3692 	.u2_enable = true,
3693 	.lpm_enable = true,
3694 	.invalid_seq_num = false,
3695 	.pls_quirk = false,
3696 	.port_reset_quirk = false,
3697 	.has_ipfs = false,
3698 };
3699 
3700 static const struct of_device_id tegra_xudc_of_match[] = {
3701 	{
3702 		.compatible = "nvidia,tegra210-xudc",
3703 		.data = &tegra210_xudc_soc_data
3704 	},
3705 	{
3706 		.compatible = "nvidia,tegra186-xudc",
3707 		.data = &tegra186_xudc_soc_data
3708 	},
3709 	{
3710 		.compatible = "nvidia,tegra194-xudc",
3711 		.data = &tegra194_xudc_soc_data
3712 	},
3713 	{
3714 		.compatible = "nvidia,tegra234-xudc",
3715 		.data = &tegra234_xudc_soc_data
3716 	},
3717 	{ }
3718 };
3719 MODULE_DEVICE_TABLE(of, tegra_xudc_of_match);
3720 
3721 static void tegra_xudc_powerdomain_remove(struct tegra_xudc *xudc)
3722 {
3723 	if (xudc->genpd_dl_ss)
3724 		device_link_del(xudc->genpd_dl_ss);
3725 	if (xudc->genpd_dl_device)
3726 		device_link_del(xudc->genpd_dl_device);
3727 	if (xudc->genpd_dev_ss)
3728 		dev_pm_domain_detach(xudc->genpd_dev_ss, true);
3729 	if (xudc->genpd_dev_device)
3730 		dev_pm_domain_detach(xudc->genpd_dev_device, true);
3731 }
3732 
3733 static int tegra_xudc_powerdomain_init(struct tegra_xudc *xudc)
3734 {
3735 	struct device *dev = xudc->dev;
3736 	int err;
3737 
3738 	xudc->genpd_dev_device = dev_pm_domain_attach_by_name(dev, "dev");
3739 	if (IS_ERR(xudc->genpd_dev_device)) {
3740 		err = PTR_ERR(xudc->genpd_dev_device);
3741 		dev_err(dev, "failed to get device power domain: %d\n", err);
3742 		return err;
3743 	}
3744 
3745 	xudc->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "ss");
3746 	if (IS_ERR(xudc->genpd_dev_ss)) {
3747 		err = PTR_ERR(xudc->genpd_dev_ss);
3748 		dev_err(dev, "failed to get SuperSpeed power domain: %d\n", err);
3749 		return err;
3750 	}
3751 
3752 	xudc->genpd_dl_device = device_link_add(dev, xudc->genpd_dev_device,
3753 						DL_FLAG_PM_RUNTIME |
3754 						DL_FLAG_STATELESS);
3755 	if (!xudc->genpd_dl_device) {
3756 		dev_err(dev, "failed to add USB device link\n");
3757 		return -ENODEV;
3758 	}
3759 
3760 	xudc->genpd_dl_ss = device_link_add(dev, xudc->genpd_dev_ss,
3761 					    DL_FLAG_PM_RUNTIME |
3762 					    DL_FLAG_STATELESS);
3763 	if (!xudc->genpd_dl_ss) {
3764 		dev_err(dev, "failed to add SuperSpeed device link\n");
3765 		return -ENODEV;
3766 	}
3767 
3768 	return 0;
3769 }
3770 
3771 static int tegra_xudc_probe(struct platform_device *pdev)
3772 {
3773 	struct tegra_xudc *xudc;
3774 	struct resource *res;
3775 	unsigned int i;
3776 	int err;
3777 
3778 	xudc = devm_kzalloc(&pdev->dev, sizeof(*xudc), GFP_KERNEL);
3779 	if (!xudc)
3780 		return -ENOMEM;
3781 
3782 	xudc->dev = &pdev->dev;
3783 	platform_set_drvdata(pdev, xudc);
3784 
3785 	xudc->soc = of_device_get_match_data(&pdev->dev);
3786 	if (!xudc->soc)
3787 		return -ENODEV;
3788 
3789 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3790 	xudc->base = devm_ioremap_resource(&pdev->dev, res);
3791 	if (IS_ERR(xudc->base))
3792 		return PTR_ERR(xudc->base);
3793 	xudc->phys_base = res->start;
3794 
3795 	xudc->fpci = devm_platform_ioremap_resource_byname(pdev, "fpci");
3796 	if (IS_ERR(xudc->fpci))
3797 		return PTR_ERR(xudc->fpci);
3798 
3799 	if (xudc->soc->has_ipfs) {
3800 		xudc->ipfs = devm_platform_ioremap_resource_byname(pdev, "ipfs");
3801 		if (IS_ERR(xudc->ipfs))
3802 			return PTR_ERR(xudc->ipfs);
3803 	}
3804 
3805 	xudc->irq = platform_get_irq(pdev, 0);
3806 	if (xudc->irq < 0)
3807 		return xudc->irq;
3808 
3809 	err = devm_request_irq(&pdev->dev, xudc->irq, tegra_xudc_irq, 0,
3810 			       dev_name(&pdev->dev), xudc);
3811 	if (err < 0) {
3812 		dev_err(xudc->dev, "failed to claim IRQ#%u: %d\n", xudc->irq,
3813 			err);
3814 		return err;
3815 	}
3816 
3817 	xudc->clks = devm_kcalloc(&pdev->dev, xudc->soc->num_clks, sizeof(*xudc->clks),
3818 				  GFP_KERNEL);
3819 	if (!xudc->clks)
3820 		return -ENOMEM;
3821 
3822 	for (i = 0; i < xudc->soc->num_clks; i++)
3823 		xudc->clks[i].id = xudc->soc->clock_names[i];
3824 
3825 	err = devm_clk_bulk_get(&pdev->dev, xudc->soc->num_clks, xudc->clks);
3826 	if (err) {
3827 		dev_err_probe(xudc->dev, err, "failed to request clocks\n");
3828 		return err;
3829 	}
3830 
3831 	xudc->supplies = devm_kcalloc(&pdev->dev, xudc->soc->num_supplies,
3832 				      sizeof(*xudc->supplies), GFP_KERNEL);
3833 	if (!xudc->supplies)
3834 		return -ENOMEM;
3835 
3836 	for (i = 0; i < xudc->soc->num_supplies; i++)
3837 		xudc->supplies[i].supply = xudc->soc->supply_names[i];
3838 
3839 	err = devm_regulator_bulk_get(&pdev->dev, xudc->soc->num_supplies,
3840 				      xudc->supplies);
3841 	if (err) {
3842 		dev_err_probe(xudc->dev, err, "failed to request regulators\n");
3843 		return err;
3844 	}
3845 
3846 	xudc->padctl = tegra_xusb_padctl_get(&pdev->dev);
3847 	if (IS_ERR(xudc->padctl))
3848 		return PTR_ERR(xudc->padctl);
3849 
3850 	err = regulator_bulk_enable(xudc->soc->num_supplies, xudc->supplies);
3851 	if (err) {
3852 		dev_err(xudc->dev, "failed to enable regulators: %d\n", err);
3853 		goto put_padctl;
3854 	}
3855 
3856 	err = tegra_xudc_phy_get(xudc);
3857 	if (err)
3858 		goto disable_regulator;
3859 
3860 	err = tegra_xudc_powerdomain_init(xudc);
3861 	if (err)
3862 		goto put_powerdomains;
3863 
3864 	err = tegra_xudc_phy_init(xudc);
3865 	if (err)
3866 		goto put_powerdomains;
3867 
3868 	err = tegra_xudc_alloc_event_ring(xudc);
3869 	if (err)
3870 		goto disable_phy;
3871 
3872 	err = tegra_xudc_alloc_eps(xudc);
3873 	if (err)
3874 		goto free_event_ring;
3875 
3876 	spin_lock_init(&xudc->lock);
3877 
3878 	init_completion(&xudc->disconnect_complete);
3879 
3880 	INIT_WORK(&xudc->usb_role_sw_work, tegra_xudc_usb_role_sw_work);
3881 
3882 	INIT_DELAYED_WORK(&xudc->plc_reset_work, tegra_xudc_plc_reset_work);
3883 
3884 	INIT_DELAYED_WORK(&xudc->port_reset_war_work,
3885 				tegra_xudc_port_reset_war_work);
3886 
3887 	pm_runtime_enable(&pdev->dev);
3888 
3889 	xudc->gadget.ops = &tegra_xudc_gadget_ops;
3890 	xudc->gadget.ep0 = &xudc->ep[0].usb_ep;
3891 	xudc->gadget.name = "tegra-xudc";
3892 	xudc->gadget.max_speed = USB_SPEED_SUPER;
3893 
3894 	err = usb_add_gadget_udc(&pdev->dev, &xudc->gadget);
3895 	if (err) {
3896 		dev_err(&pdev->dev, "failed to add USB gadget: %d\n", err);
3897 		goto free_eps;
3898 	}
3899 
3900 	for (i = 0; i < xudc->soc->num_phys; i++) {
3901 		if (!xudc->usbphy[i])
3902 			continue;
3903 
3904 		usb_register_notifier(xudc->usbphy[i], &xudc->vbus_nb);
3905 		tegra_xudc_update_data_role(xudc, xudc->usbphy[i]);
3906 	}
3907 
3908 	return 0;
3909 
3910 free_eps:
3911 	pm_runtime_disable(&pdev->dev);
3912 	tegra_xudc_free_eps(xudc);
3913 free_event_ring:
3914 	tegra_xudc_free_event_ring(xudc);
3915 disable_phy:
3916 	tegra_xudc_phy_exit(xudc);
3917 put_powerdomains:
3918 	tegra_xudc_powerdomain_remove(xudc);
3919 disable_regulator:
3920 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3921 put_padctl:
3922 	tegra_xusb_padctl_put(xudc->padctl);
3923 
3924 	return err;
3925 }
3926 
3927 static void tegra_xudc_remove(struct platform_device *pdev)
3928 {
3929 	struct tegra_xudc *xudc = platform_get_drvdata(pdev);
3930 	unsigned int i;
3931 
3932 	pm_runtime_get_sync(xudc->dev);
3933 
3934 	cancel_delayed_work_sync(&xudc->plc_reset_work);
3935 	cancel_work_sync(&xudc->usb_role_sw_work);
3936 
3937 	usb_del_gadget_udc(&xudc->gadget);
3938 
3939 	tegra_xudc_free_eps(xudc);
3940 	tegra_xudc_free_event_ring(xudc);
3941 
3942 	tegra_xudc_powerdomain_remove(xudc);
3943 
3944 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3945 
3946 	for (i = 0; i < xudc->soc->num_phys; i++) {
3947 		phy_power_off(xudc->utmi_phy[i]);
3948 		phy_power_off(xudc->usb3_phy[i]);
3949 	}
3950 
3951 	tegra_xudc_phy_exit(xudc);
3952 
3953 	pm_runtime_disable(xudc->dev);
3954 	pm_runtime_put(xudc->dev);
3955 
3956 	tegra_xusb_padctl_put(xudc->padctl);
3957 }
3958 
3959 static int __maybe_unused tegra_xudc_powergate(struct tegra_xudc *xudc)
3960 {
3961 	unsigned long flags;
3962 
3963 	dev_dbg(xudc->dev, "entering ELPG\n");
3964 
3965 	spin_lock_irqsave(&xudc->lock, flags);
3966 
3967 	xudc->powergated = true;
3968 	xudc->saved_regs.ctrl = xudc_readl(xudc, CTRL);
3969 	xudc->saved_regs.portpm = xudc_readl(xudc, PORTPM);
3970 	xudc_writel(xudc, 0, CTRL);
3971 
3972 	spin_unlock_irqrestore(&xudc->lock, flags);
3973 
3974 	clk_bulk_disable_unprepare(xudc->soc->num_clks, xudc->clks);
3975 
3976 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3977 
3978 	dev_dbg(xudc->dev, "entering ELPG done\n");
3979 	return 0;
3980 }
3981 
3982 static int __maybe_unused tegra_xudc_unpowergate(struct tegra_xudc *xudc)
3983 {
3984 	unsigned long flags;
3985 	int err;
3986 
3987 	dev_dbg(xudc->dev, "exiting ELPG\n");
3988 
3989 	err = regulator_bulk_enable(xudc->soc->num_supplies,
3990 			xudc->supplies);
3991 	if (err < 0)
3992 		return err;
3993 
3994 	err = clk_bulk_prepare_enable(xudc->soc->num_clks, xudc->clks);
3995 	if (err < 0)
3996 		return err;
3997 
3998 	tegra_xudc_fpci_ipfs_init(xudc);
3999 
4000 	tegra_xudc_device_params_init(xudc);
4001 
4002 	tegra_xudc_init_event_ring(xudc);
4003 
4004 	tegra_xudc_init_eps(xudc);
4005 
4006 	xudc_writel(xudc, xudc->saved_regs.portpm, PORTPM);
4007 	xudc_writel(xudc, xudc->saved_regs.ctrl, CTRL);
4008 
4009 	spin_lock_irqsave(&xudc->lock, flags);
4010 	xudc->powergated = false;
4011 	spin_unlock_irqrestore(&xudc->lock, flags);
4012 
4013 	dev_dbg(xudc->dev, "exiting ELPG done\n");
4014 	return 0;
4015 }
4016 
4017 static int __maybe_unused tegra_xudc_suspend(struct device *dev)
4018 {
4019 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
4020 	unsigned long flags;
4021 
4022 	spin_lock_irqsave(&xudc->lock, flags);
4023 	xudc->suspended = true;
4024 	spin_unlock_irqrestore(&xudc->lock, flags);
4025 
4026 	flush_work(&xudc->usb_role_sw_work);
4027 
4028 	if (!pm_runtime_status_suspended(dev)) {
4029 		/* Forcibly disconnect before powergating. */
4030 		tegra_xudc_device_mode_off(xudc);
4031 		tegra_xudc_powergate(xudc);
4032 	}
4033 
4034 	pm_runtime_disable(dev);
4035 
4036 	return 0;
4037 }
4038 
4039 static int __maybe_unused tegra_xudc_resume(struct device *dev)
4040 {
4041 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
4042 	unsigned long flags;
4043 	int err;
4044 
4045 	err = tegra_xudc_unpowergate(xudc);
4046 	if (err < 0)
4047 		return err;
4048 
4049 	spin_lock_irqsave(&xudc->lock, flags);
4050 	xudc->suspended = false;
4051 	if (xudc->device_mode != xudc->current_device_mode)
4052 		schedule_work(&xudc->usb_role_sw_work);
4053 	spin_unlock_irqrestore(&xudc->lock, flags);
4054 
4055 	pm_runtime_enable(dev);
4056 
4057 	return 0;
4058 }
4059 
4060 static int __maybe_unused tegra_xudc_runtime_suspend(struct device *dev)
4061 {
4062 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
4063 
4064 	return tegra_xudc_powergate(xudc);
4065 }
4066 
4067 static int __maybe_unused tegra_xudc_runtime_resume(struct device *dev)
4068 {
4069 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
4070 
4071 	return tegra_xudc_unpowergate(xudc);
4072 }
4073 
4074 static const struct dev_pm_ops tegra_xudc_pm_ops = {
4075 	SET_SYSTEM_SLEEP_PM_OPS(tegra_xudc_suspend, tegra_xudc_resume)
4076 	SET_RUNTIME_PM_OPS(tegra_xudc_runtime_suspend,
4077 			   tegra_xudc_runtime_resume, NULL)
4078 };
4079 
4080 static struct platform_driver tegra_xudc_driver = {
4081 	.probe = tegra_xudc_probe,
4082 	.remove = tegra_xudc_remove,
4083 	.driver = {
4084 		.name = "tegra-xudc",
4085 		.pm = &tegra_xudc_pm_ops,
4086 		.of_match_table = tegra_xudc_of_match,
4087 	},
4088 };
4089 module_platform_driver(tegra_xudc_driver);
4090 
4091 MODULE_DESCRIPTION("NVIDIA Tegra XUSB Device Controller");
4092 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
4093 MODULE_AUTHOR("Hui Fu <hfu@nvidia.com>");
4094 MODULE_AUTHOR("Nagarjuna Kristam <nkristam@nvidia.com>");
4095 MODULE_LICENSE("GPL v2");
4096