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