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