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