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