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