GNU Linux-libre 5.10.217-gnu1
[releases.git] / drivers / usb / dwc3 / gadget.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4  *
5  * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
6  *
7  * Authors: Felipe Balbi <balbi@ti.com>,
8  *          Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/list.h>
20 #include <linux/dma-mapping.h>
21
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24
25 #include "debug.h"
26 #include "core.h"
27 #include "gadget.h"
28 #include "io.h"
29
30 #define DWC3_ALIGN_FRAME(d, n)  (((d)->frame_number + ((d)->interval * (n))) \
31                                         & ~((d)->interval - 1))
32
33 /**
34  * dwc3_gadget_set_test_mode - enables usb2 test modes
35  * @dwc: pointer to our context structure
36  * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37  *
38  * Caller should take care of locking. This function will return 0 on
39  * success or -EINVAL if wrong Test Selector is passed.
40  */
41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42 {
43         u32             reg;
44
45         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46         reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48         switch (mode) {
49         case USB_TEST_J:
50         case USB_TEST_K:
51         case USB_TEST_SE0_NAK:
52         case USB_TEST_PACKET:
53         case USB_TEST_FORCE_ENABLE:
54                 reg |= mode << 1;
55                 break;
56         default:
57                 return -EINVAL;
58         }
59
60         dwc3_gadget_dctl_write_safe(dwc, reg);
61
62         return 0;
63 }
64
65 /**
66  * dwc3_gadget_get_link_state - gets current state of usb link
67  * @dwc: pointer to our context structure
68  *
69  * Caller should take care of locking. This function will
70  * return the link state on success (>= 0) or -ETIMEDOUT.
71  */
72 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73 {
74         u32             reg;
75
76         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78         return DWC3_DSTS_USBLNKST(reg);
79 }
80
81 /**
82  * dwc3_gadget_set_link_state - sets usb link to a particular state
83  * @dwc: pointer to our context structure
84  * @state: the state to put link into
85  *
86  * Caller should take care of locking. This function will
87  * return 0 on success or -ETIMEDOUT.
88  */
89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90 {
91         int             retries = 10000;
92         u32             reg;
93
94         /*
95          * Wait until device controller is ready. Only applies to 1.94a and
96          * later RTL.
97          */
98         if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
99                 while (--retries) {
100                         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101                         if (reg & DWC3_DSTS_DCNRD)
102                                 udelay(5);
103                         else
104                                 break;
105                 }
106
107                 if (retries <= 0)
108                         return -ETIMEDOUT;
109         }
110
111         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112         reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
114         /* set no action before sending new link state change */
115         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
117         /* set requested state */
118         reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120
121         /*
122          * The following code is racy when called from dwc3_gadget_wakeup,
123          * and is not needed, at least on newer versions
124          */
125         if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
126                 return 0;
127
128         /* wait for a change in DSTS */
129         retries = 10000;
130         while (--retries) {
131                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132
133                 if (DWC3_DSTS_USBLNKST(reg) == state)
134                         return 0;
135
136                 udelay(5);
137         }
138
139         return -ETIMEDOUT;
140 }
141
142 /**
143  * dwc3_ep_inc_trb - increment a trb index.
144  * @index: Pointer to the TRB index to increment.
145  *
146  * The index should never point to the link TRB. After incrementing,
147  * if it is point to the link TRB, wrap around to the beginning. The
148  * link TRB is always at the last TRB entry.
149  */
150 static void dwc3_ep_inc_trb(u8 *index)
151 {
152         (*index)++;
153         if (*index == (DWC3_TRB_NUM - 1))
154                 *index = 0;
155 }
156
157 /**
158  * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159  * @dep: The endpoint whose enqueue pointer we're incrementing
160  */
161 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
162 {
163         dwc3_ep_inc_trb(&dep->trb_enqueue);
164 }
165
166 /**
167  * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168  * @dep: The endpoint whose enqueue pointer we're incrementing
169  */
170 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171 {
172         dwc3_ep_inc_trb(&dep->trb_dequeue);
173 }
174
175 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
176                 struct dwc3_request *req, int status)
177 {
178         struct dwc3                     *dwc = dep->dwc;
179
180         list_del(&req->list);
181         req->remaining = 0;
182         req->needs_extra_trb = false;
183         req->num_trbs = 0;
184
185         if (req->request.status == -EINPROGRESS)
186                 req->request.status = status;
187
188         if (req->trb)
189                 usb_gadget_unmap_request_by_dev(dwc->sysdev,
190                                 &req->request, req->direction);
191
192         req->trb = NULL;
193         trace_dwc3_gadget_giveback(req);
194
195         if (dep->number > 1)
196                 pm_runtime_put(dwc->dev);
197 }
198
199 /**
200  * dwc3_gadget_giveback - call struct usb_request's ->complete callback
201  * @dep: The endpoint to whom the request belongs to
202  * @req: The request we're giving back
203  * @status: completion code for the request
204  *
205  * Must be called with controller's lock held and interrupts disabled. This
206  * function will unmap @req and call its ->complete() callback to notify upper
207  * layers that it has completed.
208  */
209 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
210                 int status)
211 {
212         struct dwc3                     *dwc = dep->dwc;
213
214         dwc3_gadget_del_and_unmap_request(dep, req, status);
215         req->status = DWC3_REQUEST_STATUS_COMPLETED;
216
217         spin_unlock(&dwc->lock);
218         usb_gadget_giveback_request(&dep->endpoint, &req->request);
219         spin_lock(&dwc->lock);
220 }
221
222 /**
223  * dwc3_send_gadget_generic_command - issue a generic command for the controller
224  * @dwc: pointer to the controller context
225  * @cmd: the command to be issued
226  * @param: command parameter
227  *
228  * Caller should take care of locking. Issue @cmd with a given @param to @dwc
229  * and wait for its completion.
230  */
231 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
232                 u32 param)
233 {
234         u32             timeout = 500;
235         int             status = 0;
236         int             ret = 0;
237         u32             reg;
238
239         dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
240         dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
241
242         do {
243                 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
244                 if (!(reg & DWC3_DGCMD_CMDACT)) {
245                         status = DWC3_DGCMD_STATUS(reg);
246                         if (status)
247                                 ret = -EINVAL;
248                         break;
249                 }
250         } while (--timeout);
251
252         if (!timeout) {
253                 ret = -ETIMEDOUT;
254                 status = -ETIMEDOUT;
255         }
256
257         trace_dwc3_gadget_generic_cmd(cmd, param, status);
258
259         return ret;
260 }
261
262 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
263
264 /**
265  * dwc3_send_gadget_ep_cmd - issue an endpoint command
266  * @dep: the endpoint to which the command is going to be issued
267  * @cmd: the command to be issued
268  * @params: parameters to the command
269  *
270  * Caller should handle locking. This function will issue @cmd with given
271  * @params to @dep and wait for its completion.
272  */
273 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
274                 struct dwc3_gadget_ep_cmd_params *params)
275 {
276         const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
277         struct dwc3             *dwc = dep->dwc;
278         u32                     timeout = 5000;
279         u32                     saved_config = 0;
280         u32                     reg;
281
282         int                     cmd_status = 0;
283         int                     ret = -EINVAL;
284
285         /*
286          * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
287          * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
288          * endpoint command.
289          *
290          * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
291          * settings. Restore them after the command is completed.
292          *
293          * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
294          */
295         if (dwc->gadget->speed <= USB_SPEED_HIGH ||
296             DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
297                 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
298                 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
299                         saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
300                         reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
301                 }
302
303                 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
304                         saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
305                         reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
306                 }
307
308                 if (saved_config)
309                         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
310         }
311
312         if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
313                 int link_state;
314
315                 /*
316                  * Initiate remote wakeup if the link state is in U3 when
317                  * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
318                  * link state is in U1/U2, no remote wakeup is needed. The Start
319                  * Transfer command will initiate the link recovery.
320                  */
321                 link_state = dwc3_gadget_get_link_state(dwc);
322                 switch (link_state) {
323                 case DWC3_LINK_STATE_U2:
324                         if (dwc->gadget->speed >= USB_SPEED_SUPER)
325                                 break;
326
327                         fallthrough;
328                 case DWC3_LINK_STATE_U3:
329                         ret = __dwc3_gadget_wakeup(dwc);
330                         dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
331                                         ret);
332                         break;
333                 }
334         }
335
336         dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
337         dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
338         dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
339
340         /*
341          * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
342          * not relying on XferNotReady, we can make use of a special "No
343          * Response Update Transfer" command where we should clear both CmdAct
344          * and CmdIOC bits.
345          *
346          * With this, we don't need to wait for command completion and can
347          * straight away issue further commands to the endpoint.
348          *
349          * NOTICE: We're making an assumption that control endpoints will never
350          * make use of Update Transfer command. This is a safe assumption
351          * because we can never have more than one request at a time with
352          * Control Endpoints. If anybody changes that assumption, this chunk
353          * needs to be updated accordingly.
354          */
355         if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
356                         !usb_endpoint_xfer_isoc(desc))
357                 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
358         else
359                 cmd |= DWC3_DEPCMD_CMDACT;
360
361         dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
362         do {
363                 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
364                 if (!(reg & DWC3_DEPCMD_CMDACT)) {
365                         cmd_status = DWC3_DEPCMD_STATUS(reg);
366
367                         switch (cmd_status) {
368                         case 0:
369                                 ret = 0;
370                                 break;
371                         case DEPEVT_TRANSFER_NO_RESOURCE:
372                                 dev_WARN(dwc->dev, "No resource for %s\n",
373                                          dep->name);
374                                 ret = -EINVAL;
375                                 break;
376                         case DEPEVT_TRANSFER_BUS_EXPIRY:
377                                 /*
378                                  * SW issues START TRANSFER command to
379                                  * isochronous ep with future frame interval. If
380                                  * future interval time has already passed when
381                                  * core receives the command, it will respond
382                                  * with an error status of 'Bus Expiry'.
383                                  *
384                                  * Instead of always returning -EINVAL, let's
385                                  * give a hint to the gadget driver that this is
386                                  * the case by returning -EAGAIN.
387                                  */
388                                 ret = -EAGAIN;
389                                 break;
390                         default:
391                                 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
392                         }
393
394                         break;
395                 }
396         } while (--timeout);
397
398         if (timeout == 0) {
399                 ret = -ETIMEDOUT;
400                 cmd_status = -ETIMEDOUT;
401         }
402
403         trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
404
405         if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
406                 if (ret == 0)
407                         dep->flags |= DWC3_EP_TRANSFER_STARTED;
408
409                 if (ret != -ETIMEDOUT)
410                         dwc3_gadget_ep_get_transfer_index(dep);
411         }
412
413         if (saved_config) {
414                 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
415                 reg |= saved_config;
416                 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
417         }
418
419         return ret;
420 }
421
422 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
423 {
424         struct dwc3 *dwc = dep->dwc;
425         struct dwc3_gadget_ep_cmd_params params;
426         u32 cmd = DWC3_DEPCMD_CLEARSTALL;
427
428         /*
429          * As of core revision 2.60a the recommended programming model
430          * is to set the ClearPendIN bit when issuing a Clear Stall EP
431          * command for IN endpoints. This is to prevent an issue where
432          * some (non-compliant) hosts may not send ACK TPs for pending
433          * IN transfers due to a mishandled error condition. Synopsys
434          * STAR 9000614252.
435          */
436         if (dep->direction &&
437             !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
438             (dwc->gadget->speed >= USB_SPEED_SUPER))
439                 cmd |= DWC3_DEPCMD_CLEARPENDIN;
440
441         memset(&params, 0, sizeof(params));
442
443         return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
444 }
445
446 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
447                 struct dwc3_trb *trb)
448 {
449         u32             offset = (char *) trb - (char *) dep->trb_pool;
450
451         return dep->trb_pool_dma + offset;
452 }
453
454 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
455 {
456         struct dwc3             *dwc = dep->dwc;
457
458         if (dep->trb_pool)
459                 return 0;
460
461         dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
462                         sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
463                         &dep->trb_pool_dma, GFP_KERNEL);
464         if (!dep->trb_pool) {
465                 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
466                                 dep->name);
467                 return -ENOMEM;
468         }
469
470         return 0;
471 }
472
473 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
474 {
475         struct dwc3             *dwc = dep->dwc;
476
477         dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
478                         dep->trb_pool, dep->trb_pool_dma);
479
480         dep->trb_pool = NULL;
481         dep->trb_pool_dma = 0;
482 }
483
484 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
485 {
486         struct dwc3_gadget_ep_cmd_params params;
487
488         memset(&params, 0x00, sizeof(params));
489
490         params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
491
492         return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
493                         &params);
494 }
495
496 /**
497  * dwc3_gadget_start_config - configure ep resources
498  * @dep: endpoint that is being enabled
499  *
500  * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
501  * completion, it will set Transfer Resource for all available endpoints.
502  *
503  * The assignment of transfer resources cannot perfectly follow the data book
504  * due to the fact that the controller driver does not have all knowledge of the
505  * configuration in advance. It is given this information piecemeal by the
506  * composite gadget framework after every SET_CONFIGURATION and
507  * SET_INTERFACE. Trying to follow the databook programming model in this
508  * scenario can cause errors. For two reasons:
509  *
510  * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
511  * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
512  * incorrect in the scenario of multiple interfaces.
513  *
514  * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
515  * endpoint on alt setting (8.1.6).
516  *
517  * The following simplified method is used instead:
518  *
519  * All hardware endpoints can be assigned a transfer resource and this setting
520  * will stay persistent until either a core reset or hibernation. So whenever we
521  * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
522  * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
523  * guaranteed that there are as many transfer resources as endpoints.
524  *
525  * This function is called for each endpoint when it is being enabled but is
526  * triggered only when called for EP0-out, which always happens first, and which
527  * should only happen in one of the above conditions.
528  */
529 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
530 {
531         struct dwc3_gadget_ep_cmd_params params;
532         struct dwc3             *dwc;
533         u32                     cmd;
534         int                     i;
535         int                     ret;
536
537         if (dep->number)
538                 return 0;
539
540         memset(&params, 0x00, sizeof(params));
541         cmd = DWC3_DEPCMD_DEPSTARTCFG;
542         dwc = dep->dwc;
543
544         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
545         if (ret)
546                 return ret;
547
548         for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
549                 struct dwc3_ep *dep = dwc->eps[i];
550
551                 if (!dep)
552                         continue;
553
554                 ret = dwc3_gadget_set_xfer_resource(dep);
555                 if (ret)
556                         return ret;
557         }
558
559         return 0;
560 }
561
562 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
563 {
564         const struct usb_ss_ep_comp_descriptor *comp_desc;
565         const struct usb_endpoint_descriptor *desc;
566         struct dwc3_gadget_ep_cmd_params params;
567         struct dwc3 *dwc = dep->dwc;
568
569         comp_desc = dep->endpoint.comp_desc;
570         desc = dep->endpoint.desc;
571
572         memset(&params, 0x00, sizeof(params));
573
574         params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
575                 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
576
577         /* Burst size is only needed in SuperSpeed mode */
578         if (dwc->gadget->speed >= USB_SPEED_SUPER) {
579                 u32 burst = dep->endpoint.maxburst;
580
581                 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
582         }
583
584         params.param0 |= action;
585         if (action == DWC3_DEPCFG_ACTION_RESTORE)
586                 params.param2 |= dep->saved_state;
587
588         if (usb_endpoint_xfer_control(desc))
589                 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
590
591         if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
592                 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
593
594         if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
595                 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
596                         | DWC3_DEPCFG_XFER_COMPLETE_EN
597                         | DWC3_DEPCFG_STREAM_EVENT_EN;
598                 dep->stream_capable = true;
599         }
600
601         if (!usb_endpoint_xfer_control(desc))
602                 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
603
604         /*
605          * We are doing 1:1 mapping for endpoints, meaning
606          * Physical Endpoints 2 maps to Logical Endpoint 2 and
607          * so on. We consider the direction bit as part of the physical
608          * endpoint number. So USB endpoint 0x81 is 0x03.
609          */
610         params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
611
612         /*
613          * We must use the lower 16 TX FIFOs even though
614          * HW might have more
615          */
616         if (dep->direction)
617                 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
618
619         if (desc->bInterval) {
620                 u8 bInterval_m1;
621
622                 /*
623                  * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
624                  *
625                  * NOTE: The programming guide incorrectly stated bInterval_m1
626                  * must be set to 0 when operating in fullspeed. Internally the
627                  * controller does not have this limitation. See DWC_usb3x
628                  * programming guide section 3.2.2.1.
629                  */
630                 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
631
632                 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
633                     dwc->gadget->speed == USB_SPEED_FULL)
634                         dep->interval = desc->bInterval;
635                 else
636                         dep->interval = 1 << (desc->bInterval - 1);
637
638                 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
639         }
640
641         return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
642 }
643
644 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
645                 bool interrupt);
646
647 /**
648  * __dwc3_gadget_ep_enable - initializes a hw endpoint
649  * @dep: endpoint to be initialized
650  * @action: one of INIT, MODIFY or RESTORE
651  *
652  * Caller should take care of locking. Execute all necessary commands to
653  * initialize a HW endpoint so it can be used by a gadget driver.
654  */
655 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
656 {
657         const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
658         struct dwc3             *dwc = dep->dwc;
659
660         u32                     reg;
661         int                     ret;
662
663         if (!(dep->flags & DWC3_EP_ENABLED)) {
664                 ret = dwc3_gadget_start_config(dep);
665                 if (ret)
666                         return ret;
667         }
668
669         ret = dwc3_gadget_set_ep_config(dep, action);
670         if (ret)
671                 return ret;
672
673         if (!(dep->flags & DWC3_EP_ENABLED)) {
674                 struct dwc3_trb *trb_st_hw;
675                 struct dwc3_trb *trb_link;
676
677                 dep->type = usb_endpoint_type(desc);
678                 dep->flags |= DWC3_EP_ENABLED;
679
680                 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
681                 reg |= DWC3_DALEPENA_EP(dep->number);
682                 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
683
684                 if (usb_endpoint_xfer_control(desc))
685                         goto out;
686
687                 /* Initialize the TRB ring */
688                 dep->trb_dequeue = 0;
689                 dep->trb_enqueue = 0;
690                 memset(dep->trb_pool, 0,
691                        sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
692
693                 /* Link TRB. The HWO bit is never reset */
694                 trb_st_hw = &dep->trb_pool[0];
695
696                 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
697                 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
698                 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
699                 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
700                 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
701         }
702
703         /*
704          * Issue StartTransfer here with no-op TRB so we can always rely on No
705          * Response Update Transfer command.
706          */
707         if (usb_endpoint_xfer_bulk(desc) ||
708                         usb_endpoint_xfer_int(desc)) {
709                 struct dwc3_gadget_ep_cmd_params params;
710                 struct dwc3_trb *trb;
711                 dma_addr_t trb_dma;
712                 u32 cmd;
713
714                 memset(&params, 0, sizeof(params));
715                 trb = &dep->trb_pool[0];
716                 trb_dma = dwc3_trb_dma_offset(dep, trb);
717
718                 params.param0 = upper_32_bits(trb_dma);
719                 params.param1 = lower_32_bits(trb_dma);
720
721                 cmd = DWC3_DEPCMD_STARTTRANSFER;
722
723                 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
724                 if (ret < 0)
725                         return ret;
726
727                 if (dep->stream_capable) {
728                         /*
729                          * For streams, at start, there maybe a race where the
730                          * host primes the endpoint before the function driver
731                          * queues a request to initiate a stream. In that case,
732                          * the controller will not see the prime to generate the
733                          * ERDY and start stream. To workaround this, issue a
734                          * no-op TRB as normal, but end it immediately. As a
735                          * result, when the function driver queues the request,
736                          * the next START_TRANSFER command will cause the
737                          * controller to generate an ERDY to initiate the
738                          * stream.
739                          */
740                         dwc3_stop_active_transfer(dep, true, true);
741
742                         /*
743                          * All stream eps will reinitiate stream on NoStream
744                          * rejection until we can determine that the host can
745                          * prime after the first transfer.
746                          */
747                         dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
748                 }
749         }
750
751 out:
752         trace_dwc3_gadget_ep_enable(dep);
753
754         return 0;
755 }
756
757 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
758 {
759         struct dwc3_request             *req;
760
761         dwc3_stop_active_transfer(dep, true, false);
762
763         /* - giveback all requests to gadget driver */
764         while (!list_empty(&dep->started_list)) {
765                 req = next_request(&dep->started_list);
766
767                 dwc3_gadget_giveback(dep, req, status);
768         }
769
770         while (!list_empty(&dep->pending_list)) {
771                 req = next_request(&dep->pending_list);
772
773                 dwc3_gadget_giveback(dep, req, status);
774         }
775
776         while (!list_empty(&dep->cancelled_list)) {
777                 req = next_request(&dep->cancelled_list);
778
779                 dwc3_gadget_giveback(dep, req, status);
780         }
781 }
782
783 /**
784  * __dwc3_gadget_ep_disable - disables a hw endpoint
785  * @dep: the endpoint to disable
786  *
787  * This function undoes what __dwc3_gadget_ep_enable did and also removes
788  * requests which are currently being processed by the hardware and those which
789  * are not yet scheduled.
790  *
791  * Caller should take care of locking.
792  */
793 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
794 {
795         struct dwc3             *dwc = dep->dwc;
796         u32                     reg;
797
798         trace_dwc3_gadget_ep_disable(dep);
799
800         /* make sure HW endpoint isn't stalled */
801         if (dep->flags & DWC3_EP_STALL)
802                 __dwc3_gadget_ep_set_halt(dep, 0, false);
803
804         reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
805         reg &= ~DWC3_DALEPENA_EP(dep->number);
806         dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
807
808         dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
809
810         dep->stream_capable = false;
811         dep->type = 0;
812         dep->flags = 0;
813
814         /* Clear out the ep descriptors for non-ep0 */
815         if (dep->number > 1) {
816                 dep->endpoint.comp_desc = NULL;
817                 dep->endpoint.desc = NULL;
818         }
819
820         return 0;
821 }
822
823 /* -------------------------------------------------------------------------- */
824
825 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
826                 const struct usb_endpoint_descriptor *desc)
827 {
828         return -EINVAL;
829 }
830
831 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
832 {
833         return -EINVAL;
834 }
835
836 /* -------------------------------------------------------------------------- */
837
838 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
839                 const struct usb_endpoint_descriptor *desc)
840 {
841         struct dwc3_ep                  *dep;
842         struct dwc3                     *dwc;
843         unsigned long                   flags;
844         int                             ret;
845
846         if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
847                 pr_debug("dwc3: invalid parameters\n");
848                 return -EINVAL;
849         }
850
851         if (!desc->wMaxPacketSize) {
852                 pr_debug("dwc3: missing wMaxPacketSize\n");
853                 return -EINVAL;
854         }
855
856         dep = to_dwc3_ep(ep);
857         dwc = dep->dwc;
858
859         if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
860                                         "%s is already enabled\n",
861                                         dep->name))
862                 return 0;
863
864         spin_lock_irqsave(&dwc->lock, flags);
865         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
866         spin_unlock_irqrestore(&dwc->lock, flags);
867
868         return ret;
869 }
870
871 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
872 {
873         struct dwc3_ep                  *dep;
874         struct dwc3                     *dwc;
875         unsigned long                   flags;
876         int                             ret;
877
878         if (!ep) {
879                 pr_debug("dwc3: invalid parameters\n");
880                 return -EINVAL;
881         }
882
883         dep = to_dwc3_ep(ep);
884         dwc = dep->dwc;
885
886         if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
887                                         "%s is already disabled\n",
888                                         dep->name))
889                 return 0;
890
891         spin_lock_irqsave(&dwc->lock, flags);
892         ret = __dwc3_gadget_ep_disable(dep);
893         spin_unlock_irqrestore(&dwc->lock, flags);
894
895         return ret;
896 }
897
898 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
899                 gfp_t gfp_flags)
900 {
901         struct dwc3_request             *req;
902         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
903
904         req = kzalloc(sizeof(*req), gfp_flags);
905         if (!req)
906                 return NULL;
907
908         req->direction  = dep->direction;
909         req->epnum      = dep->number;
910         req->dep        = dep;
911         req->status     = DWC3_REQUEST_STATUS_UNKNOWN;
912
913         trace_dwc3_alloc_request(req);
914
915         return &req->request;
916 }
917
918 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
919                 struct usb_request *request)
920 {
921         struct dwc3_request             *req = to_dwc3_request(request);
922
923         trace_dwc3_free_request(req);
924         kfree(req);
925 }
926
927 /**
928  * dwc3_ep_prev_trb - returns the previous TRB in the ring
929  * @dep: The endpoint with the TRB ring
930  * @index: The index of the current TRB in the ring
931  *
932  * Returns the TRB prior to the one pointed to by the index. If the
933  * index is 0, we will wrap backwards, skip the link TRB, and return
934  * the one just before that.
935  */
936 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
937 {
938         u8 tmp = index;
939
940         if (!tmp)
941                 tmp = DWC3_TRB_NUM - 1;
942
943         return &dep->trb_pool[tmp - 1];
944 }
945
946 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
947 {
948         u8                      trbs_left;
949
950         /*
951          * If the enqueue & dequeue are equal then the TRB ring is either full
952          * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
953          * pending to be processed by the driver.
954          */
955         if (dep->trb_enqueue == dep->trb_dequeue) {
956                 /*
957                  * If there is any request remained in the started_list at
958                  * this point, that means there is no TRB available.
959                  */
960                 if (!list_empty(&dep->started_list))
961                         return 0;
962
963                 return DWC3_TRB_NUM - 1;
964         }
965
966         trbs_left = dep->trb_dequeue - dep->trb_enqueue;
967         trbs_left &= (DWC3_TRB_NUM - 1);
968
969         if (dep->trb_dequeue < dep->trb_enqueue)
970                 trbs_left--;
971
972         return trbs_left;
973 }
974
975 /**
976  * dwc3_prepare_one_trb - setup one TRB from one request
977  * @dep: endpoint for which this request is prepared
978  * @req: dwc3_request pointer
979  * @trb_length: buffer size of the TRB
980  * @chain: should this TRB be chained to the next?
981  * @node: only for isochronous endpoints. First TRB needs different type.
982  * @use_bounce_buffer: set to use bounce buffer
983  * @must_interrupt: set to interrupt on TRB completion
984  */
985 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
986                 struct dwc3_request *req, unsigned int trb_length,
987                 unsigned int chain, unsigned int node, bool use_bounce_buffer,
988                 bool must_interrupt)
989 {
990         struct dwc3_trb         *trb;
991         dma_addr_t              dma;
992         unsigned int            stream_id = req->request.stream_id;
993         unsigned int            short_not_ok = req->request.short_not_ok;
994         unsigned int            no_interrupt = req->request.no_interrupt;
995         unsigned int            is_last = req->request.is_last;
996         struct dwc3             *dwc = dep->dwc;
997         struct usb_gadget       *gadget = dwc->gadget;
998         enum usb_device_speed   speed = gadget->speed;
999
1000         if (use_bounce_buffer)
1001                 dma = dep->dwc->bounce_addr;
1002         else if (req->request.num_sgs > 0)
1003                 dma = sg_dma_address(req->start_sg);
1004         else
1005                 dma = req->request.dma;
1006
1007         trb = &dep->trb_pool[dep->trb_enqueue];
1008
1009         if (!req->trb) {
1010                 dwc3_gadget_move_started_request(req);
1011                 req->trb = trb;
1012                 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1013         }
1014
1015         req->num_trbs++;
1016
1017         trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
1018         trb->bpl = lower_32_bits(dma);
1019         trb->bph = upper_32_bits(dma);
1020
1021         switch (usb_endpoint_type(dep->endpoint.desc)) {
1022         case USB_ENDPOINT_XFER_CONTROL:
1023                 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
1024                 break;
1025
1026         case USB_ENDPOINT_XFER_ISOC:
1027                 if (!node) {
1028                         trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
1029
1030                         /*
1031                          * USB Specification 2.0 Section 5.9.2 states that: "If
1032                          * there is only a single transaction in the microframe,
1033                          * only a DATA0 data packet PID is used.  If there are
1034                          * two transactions per microframe, DATA1 is used for
1035                          * the first transaction data packet and DATA0 is used
1036                          * for the second transaction data packet.  If there are
1037                          * three transactions per microframe, DATA2 is used for
1038                          * the first transaction data packet, DATA1 is used for
1039                          * the second, and DATA0 is used for the third."
1040                          *
1041                          * IOW, we should satisfy the following cases:
1042                          *
1043                          * 1) length <= maxpacket
1044                          *      - DATA0
1045                          *
1046                          * 2) maxpacket < length <= (2 * maxpacket)
1047                          *      - DATA1, DATA0
1048                          *
1049                          * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1050                          *      - DATA2, DATA1, DATA0
1051                          */
1052                         if (speed == USB_SPEED_HIGH) {
1053                                 struct usb_ep *ep = &dep->endpoint;
1054                                 unsigned int mult = 2;
1055                                 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1056
1057                                 if (req->request.length <= (2 * maxp))
1058                                         mult--;
1059
1060                                 if (req->request.length <= maxp)
1061                                         mult--;
1062
1063                                 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
1064                         }
1065                 } else {
1066                         trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
1067                 }
1068
1069                 if (!no_interrupt && !chain)
1070                         trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1071                 break;
1072
1073         case USB_ENDPOINT_XFER_BULK:
1074         case USB_ENDPOINT_XFER_INT:
1075                 trb->ctrl = DWC3_TRBCTL_NORMAL;
1076                 break;
1077         default:
1078                 /*
1079                  * This is only possible with faulty memory because we
1080                  * checked it already :)
1081                  */
1082                 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1083                                 usb_endpoint_type(dep->endpoint.desc));
1084         }
1085
1086         /*
1087          * Enable Continue on Short Packet
1088          * when endpoint is not a stream capable
1089          */
1090         if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1091                 if (!dep->stream_capable)
1092                         trb->ctrl |= DWC3_TRB_CTRL_CSP;
1093
1094                 if (short_not_ok)
1095                         trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1096         }
1097
1098         if ((!no_interrupt && !chain) || must_interrupt)
1099                 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1100
1101         if (chain)
1102                 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1103         else if (dep->stream_capable && is_last)
1104                 trb->ctrl |= DWC3_TRB_CTRL_LST;
1105
1106         if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1107                 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1108
1109         /*
1110          * As per data book 4.2.3.2TRB Control Bit Rules section
1111          *
1112          * The controller autonomously checks the HWO field of a TRB to determine if the
1113          * entire TRB is valid. Therefore, software must ensure that the rest of the TRB
1114          * is valid before setting the HWO field to '1'. In most systems, this means that
1115          * software must update the fourth DWORD of a TRB last.
1116          *
1117          * However there is a possibility of CPU re-ordering here which can cause
1118          * controller to observe the HWO bit set prematurely.
1119          * Add a write memory barrier to prevent CPU re-ordering.
1120          */
1121         wmb();
1122         trb->ctrl |= DWC3_TRB_CTRL_HWO;
1123
1124         dwc3_ep_inc_enq(dep);
1125
1126         trace_dwc3_prepare_trb(dep, trb);
1127 }
1128
1129 static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1130 {
1131         unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1132         unsigned int rem = req->request.length % maxp;
1133
1134         if ((req->request.length && req->request.zero && !rem &&
1135                         !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1136                         (!req->direction && rem))
1137                 return true;
1138
1139         return false;
1140 }
1141
1142 /**
1143  * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1144  * @dep: The endpoint that the request belongs to
1145  * @req: The request to prepare
1146  * @entry_length: The last SG entry size
1147  * @node: Indicates whether this is not the first entry (for isoc only)
1148  *
1149  * Return the number of TRBs prepared.
1150  */
1151 static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1152                 struct dwc3_request *req, unsigned int entry_length,
1153                 unsigned int node)
1154 {
1155         unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1156         unsigned int rem = req->request.length % maxp;
1157         unsigned int num_trbs = 1;
1158
1159         if (dwc3_needs_extra_trb(dep, req))
1160                 num_trbs++;
1161
1162         if (dwc3_calc_trbs_left(dep) < num_trbs)
1163                 return 0;
1164
1165         req->needs_extra_trb = num_trbs > 1;
1166
1167         /* Prepare a normal TRB */
1168         if (req->direction || req->request.length)
1169                 dwc3_prepare_one_trb(dep, req, entry_length,
1170                                 req->needs_extra_trb, node, false, false);
1171
1172         /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1173         if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1174                 dwc3_prepare_one_trb(dep, req,
1175                                 req->direction ? 0 : maxp - rem,
1176                                 false, 1, true, false);
1177
1178         return num_trbs;
1179 }
1180
1181 static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
1182                 struct dwc3_request *req)
1183 {
1184         struct scatterlist *sg = req->start_sg;
1185         struct scatterlist *s;
1186         int             i;
1187         unsigned int length = req->request.length;
1188         unsigned int remaining = req->request.num_mapped_sgs
1189                 - req->num_queued_sgs;
1190         unsigned int num_trbs = req->num_trbs;
1191         bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
1192
1193         /*
1194          * If we resume preparing the request, then get the remaining length of
1195          * the request and resume where we left off.
1196          */
1197         for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1198                 length -= sg_dma_len(s);
1199
1200         for_each_sg(sg, s, remaining, i) {
1201                 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
1202                 unsigned int trb_length;
1203                 bool must_interrupt = false;
1204                 bool last_sg = false;
1205
1206                 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1207
1208                 length -= trb_length;
1209
1210                 /*
1211                  * IOMMU driver is coalescing the list of sgs which shares a
1212                  * page boundary into one and giving it to USB driver. With
1213                  * this the number of sgs mapped is not equal to the number of
1214                  * sgs passed. So mark the chain bit to false if it isthe last
1215                  * mapped sg.
1216                  */
1217                 if ((i == remaining - 1) || !length)
1218                         last_sg = true;
1219
1220                 if (!num_trbs_left)
1221                         break;
1222
1223                 if (last_sg) {
1224                         if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1225                                 break;
1226                 } else {
1227                         /*
1228                          * Look ahead to check if we have enough TRBs for the
1229                          * next SG entry. If not, set interrupt on this TRB to
1230                          * resume preparing the next SG entry when more TRBs are
1231                          * free.
1232                          */
1233                         if (num_trbs_left == 1 || (needs_extra_trb &&
1234                                         num_trbs_left <= 2 &&
1235                                         sg_dma_len(sg_next(s)) >= length))
1236                                 must_interrupt = true;
1237
1238                         dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1239                                         must_interrupt);
1240                 }
1241
1242                 /*
1243                  * There can be a situation where all sgs in sglist are not
1244                  * queued because of insufficient trb number. To handle this
1245                  * case, update start_sg to next sg to be queued, so that
1246                  * we have free trbs we can continue queuing from where we
1247                  * previously stopped
1248                  */
1249                 if (!last_sg)
1250                         req->start_sg = sg_next(s);
1251
1252                 req->num_queued_sgs++;
1253                 req->num_pending_sgs--;
1254
1255                 /*
1256                  * The number of pending SG entries may not correspond to the
1257                  * number of mapped SG entries. If all the data are queued, then
1258                  * don't include unused SG entries.
1259                  */
1260                 if (length == 0) {
1261                         req->num_pending_sgs = 0;
1262                         break;
1263                 }
1264
1265                 if (must_interrupt)
1266                         break;
1267         }
1268
1269         return req->num_trbs - num_trbs;
1270 }
1271
1272 static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
1273                 struct dwc3_request *req)
1274 {
1275         return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
1276 }
1277
1278 /*
1279  * dwc3_prepare_trbs - setup TRBs from requests
1280  * @dep: endpoint for which requests are being prepared
1281  *
1282  * The function goes through the requests list and sets up TRBs for the
1283  * transfers. The function returns once there are no more TRBs available or
1284  * it runs out of requests.
1285  *
1286  * Returns the number of TRBs prepared or negative errno.
1287  */
1288 static int dwc3_prepare_trbs(struct dwc3_ep *dep)
1289 {
1290         struct dwc3_request     *req, *n;
1291         int                     ret = 0;
1292
1293         BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1294
1295         /*
1296          * We can get in a situation where there's a request in the started list
1297          * but there weren't enough TRBs to fully kick it in the first time
1298          * around, so it has been waiting for more TRBs to be freed up.
1299          *
1300          * In that case, we should check if we have a request with pending_sgs
1301          * in the started list and prepare TRBs for that request first,
1302          * otherwise we will prepare TRBs completely out of order and that will
1303          * break things.
1304          */
1305         list_for_each_entry(req, &dep->started_list, list) {
1306                 if (req->num_pending_sgs > 0) {
1307                         ret = dwc3_prepare_trbs_sg(dep, req);
1308                         if (!ret || req->num_pending_sgs)
1309                                 return ret;
1310                 }
1311
1312                 if (!dwc3_calc_trbs_left(dep))
1313                         return ret;
1314
1315                 /*
1316                  * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1317                  * burst capability may try to read and use TRBs beyond the
1318                  * active transfer instead of stopping.
1319                  */
1320                 if (dep->stream_capable && req->request.is_last)
1321                         return ret;
1322         }
1323
1324         list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1325                 struct dwc3     *dwc = dep->dwc;
1326
1327                 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1328                                                     dep->direction);
1329                 if (ret)
1330                         return ret;
1331
1332                 req->sg                 = req->request.sg;
1333                 req->start_sg           = req->sg;
1334                 req->num_queued_sgs     = 0;
1335                 req->num_pending_sgs    = req->request.num_mapped_sgs;
1336
1337                 if (req->num_pending_sgs > 0) {
1338                         ret = dwc3_prepare_trbs_sg(dep, req);
1339                         if (req->num_pending_sgs)
1340                                 return ret;
1341                 } else {
1342                         ret = dwc3_prepare_trbs_linear(dep, req);
1343                 }
1344
1345                 if (!ret || !dwc3_calc_trbs_left(dep))
1346                         return ret;
1347
1348                 /*
1349                  * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1350                  * burst capability may try to read and use TRBs beyond the
1351                  * active transfer instead of stopping.
1352                  */
1353                 if (dep->stream_capable && req->request.is_last)
1354                         return ret;
1355         }
1356
1357         return ret;
1358 }
1359
1360 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1361
1362 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1363 {
1364         struct dwc3_gadget_ep_cmd_params params;
1365         struct dwc3_request             *req;
1366         int                             starting;
1367         int                             ret;
1368         u32                             cmd;
1369
1370         /*
1371          * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1372          * This happens when we need to stop and restart a transfer such as in
1373          * the case of reinitiating a stream or retrying an isoc transfer.
1374          */
1375         ret = dwc3_prepare_trbs(dep);
1376         if (ret < 0)
1377                 return ret;
1378
1379         starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1380
1381         /*
1382          * If there's no new TRB prepared and we don't need to restart a
1383          * transfer, there's no need to update the transfer.
1384          */
1385         if (!ret && !starting)
1386                 return ret;
1387
1388         req = next_request(&dep->started_list);
1389         if (!req) {
1390                 dep->flags |= DWC3_EP_PENDING_REQUEST;
1391                 return 0;
1392         }
1393
1394         memset(&params, 0, sizeof(params));
1395
1396         if (starting) {
1397                 params.param0 = upper_32_bits(req->trb_dma);
1398                 params.param1 = lower_32_bits(req->trb_dma);
1399                 cmd = DWC3_DEPCMD_STARTTRANSFER;
1400
1401                 if (dep->stream_capable)
1402                         cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1403
1404                 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1405                         cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1406         } else {
1407                 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1408                         DWC3_DEPCMD_PARAM(dep->resource_index);
1409         }
1410
1411         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1412         if (ret < 0) {
1413                 struct dwc3_request *tmp;
1414
1415                 if (ret == -EAGAIN)
1416                         return ret;
1417
1418                 dwc3_stop_active_transfer(dep, true, true);
1419
1420                 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1421                         dwc3_gadget_move_cancelled_request(req);
1422
1423                 /* If ep isn't started, then there's no end transfer pending */
1424                 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1425                         dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1426
1427                 return ret;
1428         }
1429
1430         if (dep->stream_capable && req->request.is_last)
1431                 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1432
1433         return 0;
1434 }
1435
1436 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1437 {
1438         u32                     reg;
1439
1440         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1441         return DWC3_DSTS_SOFFN(reg);
1442 }
1443
1444 /**
1445  * __dwc3_stop_active_transfer - stop the current active transfer
1446  * @dep: isoc endpoint
1447  * @force: set forcerm bit in the command
1448  * @interrupt: command complete interrupt after End Transfer command
1449  *
1450  * When setting force, the ForceRM bit will be set. In that case
1451  * the controller won't update the TRB progress on command
1452  * completion. It also won't clear the HWO bit in the TRB.
1453  * The command will also not complete immediately in that case.
1454  */
1455 static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1456 {
1457         struct dwc3 *dwc = dep->dwc;
1458         struct dwc3_gadget_ep_cmd_params params;
1459         u32 cmd;
1460         int ret;
1461
1462         cmd = DWC3_DEPCMD_ENDTRANSFER;
1463         cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1464         cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1465         cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1466         memset(&params, 0, sizeof(params));
1467         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1468         WARN_ON_ONCE(ret);
1469         dep->resource_index = 0;
1470
1471         if (!interrupt) {
1472                 if (!DWC3_IP_IS(DWC3) || DWC3_VER_IS_PRIOR(DWC3, 310A))
1473                         mdelay(1);
1474                 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1475         } else if (!ret) {
1476                 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1477         }
1478
1479         return ret;
1480 }
1481
1482 /**
1483  * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1484  * @dep: isoc endpoint
1485  *
1486  * This function tests for the correct combination of BIT[15:14] from the 16-bit
1487  * microframe number reported by the XferNotReady event for the future frame
1488  * number to start the isoc transfer.
1489  *
1490  * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1491  * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1492  * XferNotReady event are invalid. The driver uses this number to schedule the
1493  * isochronous transfer and passes it to the START TRANSFER command. Because
1494  * this number is invalid, the command may fail. If BIT[15:14] matches the
1495  * internal 16-bit microframe, the START TRANSFER command will pass and the
1496  * transfer will start at the scheduled time, if it is off by 1, the command
1497  * will still pass, but the transfer will start 2 seconds in the future. For all
1498  * other conditions, the START TRANSFER command will fail with bus-expiry.
1499  *
1500  * In order to workaround this issue, we can test for the correct combination of
1501  * BIT[15:14] by sending START TRANSFER commands with different values of
1502  * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1503  * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1504  * As the result, within the 4 possible combinations for BIT[15:14], there will
1505  * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1506  * command status will result in a 2-second delay start. The smaller BIT[15:14]
1507  * value is the correct combination.
1508  *
1509  * Since there are only 4 outcomes and the results are ordered, we can simply
1510  * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1511  * deduce the smaller successful combination.
1512  *
1513  * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1514  * of BIT[15:14]. The correct combination is as follow:
1515  *
1516  * if test0 fails and test1 passes, BIT[15:14] is 'b01
1517  * if test0 fails and test1 fails, BIT[15:14] is 'b10
1518  * if test0 passes and test1 fails, BIT[15:14] is 'b11
1519  * if test0 passes and test1 passes, BIT[15:14] is 'b00
1520  *
1521  * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1522  * endpoints.
1523  */
1524 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1525 {
1526         int cmd_status = 0;
1527         bool test0;
1528         bool test1;
1529
1530         while (dep->combo_num < 2) {
1531                 struct dwc3_gadget_ep_cmd_params params;
1532                 u32 test_frame_number;
1533                 u32 cmd;
1534
1535                 /*
1536                  * Check if we can start isoc transfer on the next interval or
1537                  * 4 uframes in the future with BIT[15:14] as dep->combo_num
1538                  */
1539                 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
1540                 test_frame_number |= dep->combo_num << 14;
1541                 test_frame_number += max_t(u32, 4, dep->interval);
1542
1543                 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1544                 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1545
1546                 cmd = DWC3_DEPCMD_STARTTRANSFER;
1547                 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1548                 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1549
1550                 /* Redo if some other failure beside bus-expiry is received */
1551                 if (cmd_status && cmd_status != -EAGAIN) {
1552                         dep->start_cmd_status = 0;
1553                         dep->combo_num = 0;
1554                         return 0;
1555                 }
1556
1557                 /* Store the first test status */
1558                 if (dep->combo_num == 0)
1559                         dep->start_cmd_status = cmd_status;
1560
1561                 dep->combo_num++;
1562
1563                 /*
1564                  * End the transfer if the START_TRANSFER command is successful
1565                  * to wait for the next XferNotReady to test the command again
1566                  */
1567                 if (cmd_status == 0) {
1568                         dwc3_stop_active_transfer(dep, true, true);
1569                         return 0;
1570                 }
1571         }
1572
1573         /* test0 and test1 are both completed at this point */
1574         test0 = (dep->start_cmd_status == 0);
1575         test1 = (cmd_status == 0);
1576
1577         if (!test0 && test1)
1578                 dep->combo_num = 1;
1579         else if (!test0 && !test1)
1580                 dep->combo_num = 2;
1581         else if (test0 && !test1)
1582                 dep->combo_num = 3;
1583         else if (test0 && test1)
1584                 dep->combo_num = 0;
1585
1586         dep->frame_number &= DWC3_FRNUMBER_MASK;
1587         dep->frame_number |= dep->combo_num << 14;
1588         dep->frame_number += max_t(u32, 4, dep->interval);
1589
1590         /* Reinitialize test variables */
1591         dep->start_cmd_status = 0;
1592         dep->combo_num = 0;
1593
1594         return __dwc3_gadget_kick_transfer(dep);
1595 }
1596
1597 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1598 {
1599         const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1600         struct dwc3 *dwc = dep->dwc;
1601         int ret;
1602         int i;
1603
1604         if (list_empty(&dep->pending_list) &&
1605             list_empty(&dep->started_list)) {
1606                 dep->flags |= DWC3_EP_PENDING_REQUEST;
1607                 return -EAGAIN;
1608         }
1609
1610         if (!dwc->dis_start_transfer_quirk &&
1611             (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1612              DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1613                 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
1614                         return dwc3_gadget_start_isoc_quirk(dep);
1615         }
1616
1617         if (desc->bInterval <= 14 &&
1618             dwc->gadget->speed >= USB_SPEED_HIGH) {
1619                 u32 frame = __dwc3_gadget_get_frame(dwc);
1620                 bool rollover = frame <
1621                                 (dep->frame_number & DWC3_FRNUMBER_MASK);
1622
1623                 /*
1624                  * frame_number is set from XferNotReady and may be already
1625                  * out of date. DSTS only provides the lower 14 bit of the
1626                  * current frame number. So add the upper two bits of
1627                  * frame_number and handle a possible rollover.
1628                  * This will provide the correct frame_number unless more than
1629                  * rollover has happened since XferNotReady.
1630                  */
1631
1632                 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1633                                      frame;
1634                 if (rollover)
1635                         dep->frame_number += BIT(14);
1636         }
1637
1638         for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1639                 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1640
1641                 ret = __dwc3_gadget_kick_transfer(dep);
1642                 if (ret != -EAGAIN)
1643                         break;
1644         }
1645
1646         /*
1647          * After a number of unsuccessful start attempts due to bus-expiry
1648          * status, issue END_TRANSFER command and retry on the next XferNotReady
1649          * event.
1650          */
1651         if (ret == -EAGAIN)
1652                 ret = __dwc3_stop_active_transfer(dep, false, true);
1653
1654         return ret;
1655 }
1656
1657 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1658 {
1659         struct dwc3             *dwc = dep->dwc;
1660
1661         if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
1662                 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1663                                 dep->name);
1664                 return -ESHUTDOWN;
1665         }
1666
1667         if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1668                                 &req->request, req->dep->name))
1669                 return -EINVAL;
1670
1671         if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1672                                 "%s: request %pK already in flight\n",
1673                                 dep->name, &req->request))
1674                 return -EINVAL;
1675
1676         pm_runtime_get(dwc->dev);
1677
1678         req->request.actual     = 0;
1679         req->request.status     = -EINPROGRESS;
1680
1681         trace_dwc3_ep_queue(req);
1682
1683         list_add_tail(&req->list, &dep->pending_list);
1684         req->status = DWC3_REQUEST_STATUS_QUEUED;
1685
1686         if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1687                 return 0;
1688
1689         /*
1690          * Start the transfer only after the END_TRANSFER is completed
1691          * and endpoint STALL is cleared.
1692          */
1693         if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1694             (dep->flags & DWC3_EP_WEDGE) ||
1695             (dep->flags & DWC3_EP_STALL)) {
1696                 dep->flags |= DWC3_EP_DELAY_START;
1697                 return 0;
1698         }
1699
1700         /*
1701          * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1702          * wait for a XferNotReady event so we will know what's the current
1703          * (micro-)frame number.
1704          *
1705          * Without this trick, we are very, very likely gonna get Bus Expiry
1706          * errors which will force us issue EndTransfer command.
1707          */
1708         if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1709                 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1710                                 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1711                         return 0;
1712
1713                 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1714                         if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
1715                                 return __dwc3_gadget_start_isoc(dep);
1716                 }
1717         }
1718
1719         __dwc3_gadget_kick_transfer(dep);
1720
1721         return 0;
1722 }
1723
1724 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1725         gfp_t gfp_flags)
1726 {
1727         struct dwc3_request             *req = to_dwc3_request(request);
1728         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1729         struct dwc3                     *dwc = dep->dwc;
1730
1731         unsigned long                   flags;
1732
1733         int                             ret;
1734
1735         spin_lock_irqsave(&dwc->lock, flags);
1736         ret = __dwc3_gadget_ep_queue(dep, req);
1737         spin_unlock_irqrestore(&dwc->lock, flags);
1738
1739         return ret;
1740 }
1741
1742 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1743 {
1744         int i;
1745
1746         /* If req->trb is not set, then the request has not started */
1747         if (!req->trb)
1748                 return;
1749
1750         /*
1751          * If request was already started, this means we had to
1752          * stop the transfer. With that we also need to ignore
1753          * all TRBs used by the request, however TRBs can only
1754          * be modified after completion of END_TRANSFER
1755          * command. So what we do here is that we wait for
1756          * END_TRANSFER completion and only after that, we jump
1757          * over TRBs by clearing HWO and incrementing dequeue
1758          * pointer.
1759          */
1760         for (i = 0; i < req->num_trbs; i++) {
1761                 struct dwc3_trb *trb;
1762
1763                 trb = &dep->trb_pool[dep->trb_dequeue];
1764                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1765                 dwc3_ep_inc_deq(dep);
1766         }
1767
1768         req->num_trbs = 0;
1769 }
1770
1771 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1772 {
1773         struct dwc3_request             *req;
1774         struct dwc3_request             *tmp;
1775
1776         list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1777                 dwc3_gadget_ep_skip_trbs(dep, req);
1778                 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1779         }
1780 }
1781
1782 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1783                 struct usb_request *request)
1784 {
1785         struct dwc3_request             *req = to_dwc3_request(request);
1786         struct dwc3_request             *r = NULL;
1787
1788         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1789         struct dwc3                     *dwc = dep->dwc;
1790
1791         unsigned long                   flags;
1792         int                             ret = 0;
1793
1794         trace_dwc3_ep_dequeue(req);
1795
1796         spin_lock_irqsave(&dwc->lock, flags);
1797
1798         list_for_each_entry(r, &dep->cancelled_list, list) {
1799                 if (r == req)
1800                         goto out;
1801         }
1802
1803         list_for_each_entry(r, &dep->pending_list, list) {
1804                 if (r == req) {
1805                         dwc3_gadget_giveback(dep, req, -ECONNRESET);
1806                         goto out;
1807                 }
1808         }
1809
1810         list_for_each_entry(r, &dep->started_list, list) {
1811                 if (r == req) {
1812                         struct dwc3_request *t;
1813
1814                         /* wait until it is processed */
1815                         dwc3_stop_active_transfer(dep, true, true);
1816
1817                         /*
1818                          * Remove any started request if the transfer is
1819                          * cancelled.
1820                          */
1821                         list_for_each_entry_safe(r, t, &dep->started_list, list)
1822                                 dwc3_gadget_move_cancelled_request(r);
1823
1824                         dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1825
1826                         goto out;
1827                 }
1828         }
1829
1830         dev_err(dwc->dev, "request %pK was not queued to %s\n",
1831                 request, ep->name);
1832         ret = -EINVAL;
1833 out:
1834         spin_unlock_irqrestore(&dwc->lock, flags);
1835
1836         return ret;
1837 }
1838
1839 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1840 {
1841         struct dwc3_gadget_ep_cmd_params        params;
1842         struct dwc3                             *dwc = dep->dwc;
1843         struct dwc3_request                     *req;
1844         struct dwc3_request                     *tmp;
1845         int                                     ret;
1846
1847         if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1848                 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1849                 return -EINVAL;
1850         }
1851
1852         memset(&params, 0x00, sizeof(params));
1853
1854         if (value) {
1855                 struct dwc3_trb *trb;
1856
1857                 unsigned int transfer_in_flight;
1858                 unsigned int started;
1859
1860                 if (dep->number > 1)
1861                         trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1862                 else
1863                         trb = &dwc->ep0_trb[dep->trb_enqueue];
1864
1865                 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1866                 started = !list_empty(&dep->started_list);
1867
1868                 if (!protocol && ((dep->direction && transfer_in_flight) ||
1869                                 (!dep->direction && started))) {
1870                         return -EAGAIN;
1871                 }
1872
1873                 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1874                                 &params);
1875                 if (ret)
1876                         dev_err(dwc->dev, "failed to set STALL on %s\n",
1877                                         dep->name);
1878                 else
1879                         dep->flags |= DWC3_EP_STALL;
1880         } else {
1881                 /*
1882                  * Don't issue CLEAR_STALL command to control endpoints. The
1883                  * controller automatically clears the STALL when it receives
1884                  * the SETUP token.
1885                  */
1886                 if (dep->number <= 1) {
1887                         dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1888                         return 0;
1889                 }
1890
1891                 dwc3_stop_active_transfer(dep, true, true);
1892
1893                 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1894                         dwc3_gadget_move_cancelled_request(req);
1895
1896                 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1897                         dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1898                         return 0;
1899                 }
1900
1901                 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1902
1903                 ret = dwc3_send_clear_stall_ep_cmd(dep);
1904                 if (ret) {
1905                         dev_err(dwc->dev, "failed to clear STALL on %s\n",
1906                                         dep->name);
1907                         return ret;
1908                 }
1909
1910                 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1911
1912                 if ((dep->flags & DWC3_EP_DELAY_START) &&
1913                     !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1914                         __dwc3_gadget_kick_transfer(dep);
1915
1916                 dep->flags &= ~DWC3_EP_DELAY_START;
1917         }
1918
1919         return ret;
1920 }
1921
1922 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1923 {
1924         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1925         struct dwc3                     *dwc = dep->dwc;
1926
1927         unsigned long                   flags;
1928
1929         int                             ret;
1930
1931         spin_lock_irqsave(&dwc->lock, flags);
1932         ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1933         spin_unlock_irqrestore(&dwc->lock, flags);
1934
1935         return ret;
1936 }
1937
1938 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1939 {
1940         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1941         struct dwc3                     *dwc = dep->dwc;
1942         unsigned long                   flags;
1943         int                             ret;
1944
1945         spin_lock_irqsave(&dwc->lock, flags);
1946         dep->flags |= DWC3_EP_WEDGE;
1947
1948         if (dep->number == 0 || dep->number == 1)
1949                 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1950         else
1951                 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1952         spin_unlock_irqrestore(&dwc->lock, flags);
1953
1954         return ret;
1955 }
1956
1957 /* -------------------------------------------------------------------------- */
1958
1959 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1960         .bLength        = USB_DT_ENDPOINT_SIZE,
1961         .bDescriptorType = USB_DT_ENDPOINT,
1962         .bmAttributes   = USB_ENDPOINT_XFER_CONTROL,
1963 };
1964
1965 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1966         .enable         = dwc3_gadget_ep0_enable,
1967         .disable        = dwc3_gadget_ep0_disable,
1968         .alloc_request  = dwc3_gadget_ep_alloc_request,
1969         .free_request   = dwc3_gadget_ep_free_request,
1970         .queue          = dwc3_gadget_ep0_queue,
1971         .dequeue        = dwc3_gadget_ep_dequeue,
1972         .set_halt       = dwc3_gadget_ep0_set_halt,
1973         .set_wedge      = dwc3_gadget_ep_set_wedge,
1974 };
1975
1976 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1977         .enable         = dwc3_gadget_ep_enable,
1978         .disable        = dwc3_gadget_ep_disable,
1979         .alloc_request  = dwc3_gadget_ep_alloc_request,
1980         .free_request   = dwc3_gadget_ep_free_request,
1981         .queue          = dwc3_gadget_ep_queue,
1982         .dequeue        = dwc3_gadget_ep_dequeue,
1983         .set_halt       = dwc3_gadget_ep_set_halt,
1984         .set_wedge      = dwc3_gadget_ep_set_wedge,
1985 };
1986
1987 /* -------------------------------------------------------------------------- */
1988
1989 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1990 {
1991         struct dwc3             *dwc = gadget_to_dwc(g);
1992
1993         return __dwc3_gadget_get_frame(dwc);
1994 }
1995
1996 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1997 {
1998         int                     retries;
1999
2000         int                     ret;
2001         u32                     reg;
2002
2003         u8                      link_state;
2004
2005         /*
2006          * According to the Databook Remote wakeup request should
2007          * be issued only when the device is in early suspend state.
2008          *
2009          * We can check that via USB Link State bits in DSTS register.
2010          */
2011         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2012
2013         link_state = DWC3_DSTS_USBLNKST(reg);
2014
2015         switch (link_state) {
2016         case DWC3_LINK_STATE_RESET:
2017         case DWC3_LINK_STATE_RX_DET:    /* in HS, means Early Suspend */
2018         case DWC3_LINK_STATE_U3:        /* in HS, means SUSPEND */
2019         case DWC3_LINK_STATE_U2:        /* in HS, means Sleep (L1) */
2020         case DWC3_LINK_STATE_U1:
2021         case DWC3_LINK_STATE_RESUME:
2022                 break;
2023         default:
2024                 return -EINVAL;
2025         }
2026
2027         ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2028         if (ret < 0) {
2029                 dev_err(dwc->dev, "failed to put link in Recovery\n");
2030                 return ret;
2031         }
2032
2033         /* Recent versions do this automatically */
2034         if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
2035                 /* write zeroes to Link Change Request */
2036                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2037                 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2038                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2039         }
2040
2041         /* poll until Link State changes to ON */
2042         retries = 20000;
2043
2044         while (retries--) {
2045                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2046
2047                 /* in HS, means ON */
2048                 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2049                         break;
2050         }
2051
2052         if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2053                 dev_err(dwc->dev, "failed to send remote wakeup\n");
2054                 return -EINVAL;
2055         }
2056
2057         return 0;
2058 }
2059
2060 static int dwc3_gadget_wakeup(struct usb_gadget *g)
2061 {
2062         struct dwc3             *dwc = gadget_to_dwc(g);
2063         unsigned long           flags;
2064         int                     ret;
2065
2066         spin_lock_irqsave(&dwc->lock, flags);
2067         ret = __dwc3_gadget_wakeup(dwc);
2068         spin_unlock_irqrestore(&dwc->lock, flags);
2069
2070         return ret;
2071 }
2072
2073 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2074                 int is_selfpowered)
2075 {
2076         struct dwc3             *dwc = gadget_to_dwc(g);
2077         unsigned long           flags;
2078
2079         spin_lock_irqsave(&dwc->lock, flags);
2080         g->is_selfpowered = !!is_selfpowered;
2081         spin_unlock_irqrestore(&dwc->lock, flags);
2082
2083         return 0;
2084 }
2085
2086 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2087 {
2088         u32 epnum;
2089
2090         for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2091                 struct dwc3_ep *dep;
2092
2093                 dep = dwc->eps[epnum];
2094                 if (!dep)
2095                         continue;
2096
2097                 dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2098         }
2099 }
2100
2101 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
2102 {
2103         u32                     reg;
2104         u32                     timeout = 500;
2105
2106         if (pm_runtime_suspended(dwc->dev))
2107                 return 0;
2108
2109         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2110         if (is_on) {
2111                 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
2112                         reg &= ~DWC3_DCTL_TRGTULST_MASK;
2113                         reg |= DWC3_DCTL_TRGTULST_RX_DET;
2114                 }
2115
2116                 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
2117                         reg &= ~DWC3_DCTL_KEEP_CONNECT;
2118                 reg |= DWC3_DCTL_RUN_STOP;
2119
2120                 if (dwc->has_hibernation)
2121                         reg |= DWC3_DCTL_KEEP_CONNECT;
2122
2123                 dwc->pullups_connected = true;
2124         } else {
2125                 reg &= ~DWC3_DCTL_RUN_STOP;
2126
2127                 if (dwc->has_hibernation && !suspend)
2128                         reg &= ~DWC3_DCTL_KEEP_CONNECT;
2129
2130                 dwc->pullups_connected = false;
2131         }
2132
2133         dwc3_gadget_dctl_write_safe(dwc, reg);
2134
2135         do {
2136                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2137                 reg &= DWC3_DSTS_DEVCTRLHLT;
2138         } while (--timeout && !(!is_on ^ !reg));
2139
2140         if (!timeout)
2141                 return -ETIMEDOUT;
2142
2143         return 0;
2144 }
2145
2146 static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2147 static void __dwc3_gadget_stop(struct dwc3 *dwc);
2148 static int __dwc3_gadget_start(struct dwc3 *dwc);
2149
2150 static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2151 {
2152         unsigned long flags;
2153
2154         spin_lock_irqsave(&dwc->lock, flags);
2155         dwc->connected = false;
2156
2157         /*
2158          * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2159          * Section 4.1.8 Table 4-7, it states that for a device-initiated
2160          * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2161          * command for any active transfers" before clearing the RunStop
2162          * bit.
2163          */
2164         dwc3_stop_active_transfers(dwc);
2165         __dwc3_gadget_stop(dwc);
2166         spin_unlock_irqrestore(&dwc->lock, flags);
2167
2168         /*
2169          * Note: if the GEVNTCOUNT indicates events in the event buffer, the
2170          * driver needs to acknowledge them before the controller can halt.
2171          * Simply let the interrupt handler acknowledges and handle the
2172          * remaining event generated by the controller while polling for
2173          * DSTS.DEVCTLHLT.
2174          */
2175         return dwc3_gadget_run_stop(dwc, false, false);
2176 }
2177
2178 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2179 {
2180         struct dwc3             *dwc = gadget_to_dwc(g);
2181         int                     ret;
2182
2183         is_on = !!is_on;
2184
2185         dwc->softconnect = is_on;
2186         /*
2187          * Per databook, when we want to stop the gadget, if a control transfer
2188          * is still in process, complete it and get the core into setup phase.
2189          */
2190         if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2191                 reinit_completion(&dwc->ep0_in_setup);
2192
2193                 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2194                                 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2195                 if (ret == 0)
2196                         dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
2197         }
2198
2199         /*
2200          * Avoid issuing a runtime resume if the device is already in the
2201          * suspended state during gadget disconnect.  DWC3 gadget was already
2202          * halted/stopped during runtime suspend.
2203          */
2204         if (!is_on) {
2205                 pm_runtime_barrier(dwc->dev);
2206                 if (pm_runtime_suspended(dwc->dev))
2207                         return 0;
2208         }
2209
2210         /*
2211          * Check the return value for successful resume, or error.  For a
2212          * successful resume, the DWC3 runtime PM resume routine will handle
2213          * the run stop sequence, so avoid duplicate operations here.
2214          */
2215         ret = pm_runtime_get_sync(dwc->dev);
2216         if (!ret || ret < 0) {
2217                 pm_runtime_put(dwc->dev);
2218                 if (ret < 0)
2219                         pm_runtime_set_suspended(dwc->dev);
2220                 return ret;
2221         }
2222
2223         if (dwc->pullups_connected == is_on) {
2224                 pm_runtime_put(dwc->dev);
2225                 return 0;
2226         }
2227
2228         if (!is_on) {
2229                 ret = dwc3_gadget_soft_disconnect(dwc);
2230         } else {
2231                 /*
2232                  * In the Synopsys DWC_usb31 1.90a programming guide section
2233                  * 4.1.9, it specifies that for a reconnect after a
2234                  * device-initiated disconnect requires a core soft reset
2235                  * (DCTL.CSftRst) before enabling the run/stop bit.
2236                  */
2237                 dwc3_core_soft_reset(dwc);
2238
2239                 dwc3_event_buffers_setup(dwc);
2240                 __dwc3_gadget_start(dwc);
2241                 ret = dwc3_gadget_run_stop(dwc, true, false);
2242         }
2243
2244         pm_runtime_put(dwc->dev);
2245
2246         return ret;
2247 }
2248
2249 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2250 {
2251         u32                     reg;
2252
2253         /* Enable all but Start and End of Frame IRQs */
2254         reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2255                         DWC3_DEVTEN_EVNTOVERFLOWEN |
2256                         DWC3_DEVTEN_CMDCMPLTEN |
2257                         DWC3_DEVTEN_ERRTICERREN |
2258                         DWC3_DEVTEN_WKUPEVTEN |
2259                         DWC3_DEVTEN_CONNECTDONEEN |
2260                         DWC3_DEVTEN_USBRSTEN |
2261                         DWC3_DEVTEN_DISCONNEVTEN);
2262
2263         if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2264                 reg |= DWC3_DEVTEN_ULSTCNGEN;
2265
2266         /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2267         if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2268                 reg |= DWC3_DEVTEN_EOPFEN;
2269
2270         dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2271 }
2272
2273 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2274 {
2275         /* mask all interrupts */
2276         dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2277 }
2278
2279 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2280 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
2281
2282 /**
2283  * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2284  * @dwc: pointer to our context structure
2285  *
2286  * The following looks like complex but it's actually very simple. In order to
2287  * calculate the number of packets we can burst at once on OUT transfers, we're
2288  * gonna use RxFIFO size.
2289  *
2290  * To calculate RxFIFO size we need two numbers:
2291  * MDWIDTH = size, in bits, of the internal memory bus
2292  * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2293  *
2294  * Given these two numbers, the formula is simple:
2295  *
2296  * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2297  *
2298  * 24 bytes is for 3x SETUP packets
2299  * 16 bytes is a clock domain crossing tolerance
2300  *
2301  * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2302  */
2303 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2304 {
2305         u32 ram2_depth;
2306         u32 mdwidth;
2307         u32 nump;
2308         u32 reg;
2309
2310         ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2311         mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2312         if (DWC3_IP_IS(DWC32))
2313                 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2314
2315         nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2316         nump = min_t(u32, nump, 16);
2317
2318         /* update NumP */
2319         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2320         reg &= ~DWC3_DCFG_NUMP_MASK;
2321         reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2322         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2323 }
2324
2325 static int __dwc3_gadget_start(struct dwc3 *dwc)
2326 {
2327         struct dwc3_ep          *dep;
2328         int                     ret = 0;
2329         u32                     reg;
2330
2331         /*
2332          * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2333          * the core supports IMOD, disable it.
2334          */
2335         if (dwc->imod_interval) {
2336                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2337                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2338         } else if (dwc3_has_imod(dwc)) {
2339                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2340         }
2341
2342         /*
2343          * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2344          * field instead of letting dwc3 itself calculate that automatically.
2345          *
2346          * This way, we maximize the chances that we'll be able to get several
2347          * bursts of data without going through any sort of endpoint throttling.
2348          */
2349         reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2350         if (DWC3_IP_IS(DWC3))
2351                 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2352         else
2353                 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2354
2355         dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2356
2357         dwc3_gadget_setup_nump(dwc);
2358
2359         /* Start with SuperSpeed Default */
2360         dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2361
2362         dep = dwc->eps[0];
2363         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2364         if (ret) {
2365                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2366                 goto err0;
2367         }
2368
2369         dep = dwc->eps[1];
2370         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2371         if (ret) {
2372                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2373                 goto err1;
2374         }
2375
2376         /* begin to receive SETUP packets */
2377         dwc->ep0state = EP0_SETUP_PHASE;
2378         dwc->link_state = DWC3_LINK_STATE_SS_DIS;
2379         dwc->delayed_status = false;
2380         dwc3_ep0_out_start(dwc);
2381
2382         dwc3_gadget_enable_irq(dwc);
2383         dwc3_enable_susphy(dwc, true);
2384
2385         return 0;
2386
2387 err1:
2388         __dwc3_gadget_ep_disable(dwc->eps[0]);
2389
2390 err0:
2391         return ret;
2392 }
2393
2394 static int dwc3_gadget_start(struct usb_gadget *g,
2395                 struct usb_gadget_driver *driver)
2396 {
2397         struct dwc3             *dwc = gadget_to_dwc(g);
2398         unsigned long           flags;
2399         int                     ret = 0;
2400         int                     irq;
2401
2402         irq = dwc->irq_gadget;
2403         ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2404                         IRQF_SHARED, "dwc3", dwc->ev_buf);
2405         if (ret) {
2406                 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2407                                 irq, ret);
2408                 goto err0;
2409         }
2410
2411         spin_lock_irqsave(&dwc->lock, flags);
2412         if (dwc->gadget_driver) {
2413                 dev_err(dwc->dev, "%s is already bound to %s\n",
2414                                 dwc->gadget->name,
2415                                 dwc->gadget_driver->driver.name);
2416                 ret = -EBUSY;
2417                 goto err1;
2418         }
2419
2420         dwc->gadget_driver      = driver;
2421         spin_unlock_irqrestore(&dwc->lock, flags);
2422
2423         return 0;
2424
2425 err1:
2426         spin_unlock_irqrestore(&dwc->lock, flags);
2427         free_irq(irq, dwc);
2428
2429 err0:
2430         return ret;
2431 }
2432
2433 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2434 {
2435         dwc3_gadget_disable_irq(dwc);
2436         __dwc3_gadget_ep_disable(dwc->eps[0]);
2437         __dwc3_gadget_ep_disable(dwc->eps[1]);
2438 }
2439
2440 static int dwc3_gadget_stop(struct usb_gadget *g)
2441 {
2442         struct dwc3             *dwc = gadget_to_dwc(g);
2443         unsigned long           flags;
2444
2445         spin_lock_irqsave(&dwc->lock, flags);
2446         dwc->gadget_driver      = NULL;
2447         spin_unlock_irqrestore(&dwc->lock, flags);
2448
2449         free_irq(dwc->irq_gadget, dwc->ev_buf);
2450
2451         return 0;
2452 }
2453
2454 static void dwc3_gadget_config_params(struct usb_gadget *g,
2455                                       struct usb_dcd_config_params *params)
2456 {
2457         struct dwc3             *dwc = gadget_to_dwc(g);
2458
2459         params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2460         params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2461
2462         /* Recommended BESL */
2463         if (!dwc->dis_enblslpm_quirk) {
2464                 /*
2465                  * If the recommended BESL baseline is 0 or if the BESL deep is
2466                  * less than 2, Microsoft's Windows 10 host usb stack will issue
2467                  * a usb reset immediately after it receives the extended BOS
2468                  * descriptor and the enumeration will fail. To maintain
2469                  * compatibility with the Windows' usb stack, let's set the
2470                  * recommended BESL baseline to 1 and clamp the BESL deep to be
2471                  * within 2 to 15.
2472                  */
2473                 params->besl_baseline = 1;
2474                 if (dwc->is_utmi_l1_suspend)
2475                         params->besl_deep =
2476                                 clamp_t(u8, dwc->hird_threshold, 2, 15);
2477         }
2478
2479         /* U1 Device exit Latency */
2480         if (dwc->dis_u1_entry_quirk)
2481                 params->bU1devExitLat = 0;
2482         else
2483                 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2484
2485         /* U2 Device exit Latency */
2486         if (dwc->dis_u2_entry_quirk)
2487                 params->bU2DevExitLat = 0;
2488         else
2489                 params->bU2DevExitLat =
2490                                 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2491 }
2492
2493 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2494                                   enum usb_device_speed speed)
2495 {
2496         struct dwc3             *dwc = gadget_to_dwc(g);
2497         unsigned long           flags;
2498         u32                     reg;
2499
2500         spin_lock_irqsave(&dwc->lock, flags);
2501         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2502         reg &= ~(DWC3_DCFG_SPEED_MASK);
2503
2504         /*
2505          * WORKAROUND: DWC3 revision < 2.20a have an issue
2506          * which would cause metastability state on Run/Stop
2507          * bit if we try to force the IP to USB2-only mode.
2508          *
2509          * Because of that, we cannot configure the IP to any
2510          * speed other than the SuperSpeed
2511          *
2512          * Refers to:
2513          *
2514          * STAR#9000525659: Clock Domain Crossing on DCTL in
2515          * USB 2.0 Mode
2516          */
2517         if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2518             !dwc->dis_metastability_quirk) {
2519                 reg |= DWC3_DCFG_SUPERSPEED;
2520         } else {
2521                 switch (speed) {
2522                 case USB_SPEED_LOW:
2523                         reg |= DWC3_DCFG_LOWSPEED;
2524                         break;
2525                 case USB_SPEED_FULL:
2526                         reg |= DWC3_DCFG_FULLSPEED;
2527                         break;
2528                 case USB_SPEED_HIGH:
2529                         reg |= DWC3_DCFG_HIGHSPEED;
2530                         break;
2531                 case USB_SPEED_SUPER:
2532                         reg |= DWC3_DCFG_SUPERSPEED;
2533                         break;
2534                 case USB_SPEED_SUPER_PLUS:
2535                         if (DWC3_IP_IS(DWC3))
2536                                 reg |= DWC3_DCFG_SUPERSPEED;
2537                         else
2538                                 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2539                         break;
2540                 default:
2541                         dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2542
2543                         if (DWC3_IP_IS(DWC3))
2544                                 reg |= DWC3_DCFG_SUPERSPEED;
2545                         else
2546                                 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2547                 }
2548         }
2549         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2550
2551         spin_unlock_irqrestore(&dwc->lock, flags);
2552 }
2553
2554 static const struct usb_gadget_ops dwc3_gadget_ops = {
2555         .get_frame              = dwc3_gadget_get_frame,
2556         .wakeup                 = dwc3_gadget_wakeup,
2557         .set_selfpowered        = dwc3_gadget_set_selfpowered,
2558         .pullup                 = dwc3_gadget_pullup,
2559         .udc_start              = dwc3_gadget_start,
2560         .udc_stop               = dwc3_gadget_stop,
2561         .udc_set_speed          = dwc3_gadget_set_speed,
2562         .get_config_params      = dwc3_gadget_config_params,
2563 };
2564
2565 /* -------------------------------------------------------------------------- */
2566
2567 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2568 {
2569         struct dwc3 *dwc = dep->dwc;
2570
2571         usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2572         dep->endpoint.maxburst = 1;
2573         dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2574         if (!dep->direction)
2575                 dwc->gadget->ep0 = &dep->endpoint;
2576
2577         dep->endpoint.caps.type_control = true;
2578
2579         return 0;
2580 }
2581
2582 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2583 {
2584         struct dwc3 *dwc = dep->dwc;
2585         int mdwidth;
2586         int size;
2587
2588         mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2589         if (DWC3_IP_IS(DWC32))
2590                 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2591
2592         /* MDWIDTH is represented in bits, we need it in bytes */
2593         mdwidth /= 8;
2594
2595         size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2596         if (DWC3_IP_IS(DWC3))
2597                 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
2598         else
2599                 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
2600
2601         /* FIFO Depth is in MDWDITH bytes. Multiply */
2602         size *= mdwidth;
2603
2604         /*
2605          * To meet performance requirement, a minimum TxFIFO size of 3x
2606          * MaxPacketSize is recommended for endpoints that support burst and a
2607          * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2608          * support burst. Use those numbers and we can calculate the max packet
2609          * limit as below.
2610          */
2611         if (dwc->maximum_speed >= USB_SPEED_SUPER)
2612                 size /= 3;
2613         else
2614                 size /= 2;
2615
2616         usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2617
2618         dep->endpoint.max_streams = 16;
2619         dep->endpoint.ops = &dwc3_gadget_ep_ops;
2620         list_add_tail(&dep->endpoint.ep_list,
2621                         &dwc->gadget->ep_list);
2622         dep->endpoint.caps.type_iso = true;
2623         dep->endpoint.caps.type_bulk = true;
2624         dep->endpoint.caps.type_int = true;
2625
2626         return dwc3_alloc_trb_pool(dep);
2627 }
2628
2629 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2630 {
2631         struct dwc3 *dwc = dep->dwc;
2632         int mdwidth;
2633         int size;
2634
2635         mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2636         if (DWC3_IP_IS(DWC32))
2637                 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2638
2639         /* MDWIDTH is represented in bits, convert to bytes */
2640         mdwidth /= 8;
2641
2642         /* All OUT endpoints share a single RxFIFO space */
2643         size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2644         if (DWC3_IP_IS(DWC3))
2645                 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
2646         else
2647                 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2648
2649         /* FIFO depth is in MDWDITH bytes */
2650         size *= mdwidth;
2651
2652         /*
2653          * To meet performance requirement, a minimum recommended RxFIFO size
2654          * is defined as follow:
2655          * RxFIFO size >= (3 x MaxPacketSize) +
2656          * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2657          *
2658          * Then calculate the max packet limit as below.
2659          */
2660         size -= (3 * 8) + 16;
2661         if (size < 0)
2662                 size = 0;
2663         else
2664                 size /= 3;
2665
2666         usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2667         dep->endpoint.max_streams = 16;
2668         dep->endpoint.ops = &dwc3_gadget_ep_ops;
2669         list_add_tail(&dep->endpoint.ep_list,
2670                         &dwc->gadget->ep_list);
2671         dep->endpoint.caps.type_iso = true;
2672         dep->endpoint.caps.type_bulk = true;
2673         dep->endpoint.caps.type_int = true;
2674
2675         return dwc3_alloc_trb_pool(dep);
2676 }
2677
2678 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2679 {
2680         struct dwc3_ep                  *dep;
2681         bool                            direction = epnum & 1;
2682         int                             ret;
2683         u8                              num = epnum >> 1;
2684
2685         dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2686         if (!dep)
2687                 return -ENOMEM;
2688
2689         dep->dwc = dwc;
2690         dep->number = epnum;
2691         dep->direction = direction;
2692         dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2693         dwc->eps[epnum] = dep;
2694         dep->combo_num = 0;
2695         dep->start_cmd_status = 0;
2696
2697         snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2698                         direction ? "in" : "out");
2699
2700         dep->endpoint.name = dep->name;
2701
2702         if (!(dep->number > 1)) {
2703                 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2704                 dep->endpoint.comp_desc = NULL;
2705         }
2706
2707         if (num == 0)
2708                 ret = dwc3_gadget_init_control_endpoint(dep);
2709         else if (direction)
2710                 ret = dwc3_gadget_init_in_endpoint(dep);
2711         else
2712                 ret = dwc3_gadget_init_out_endpoint(dep);
2713
2714         if (ret)
2715                 return ret;
2716
2717         dep->endpoint.caps.dir_in = direction;
2718         dep->endpoint.caps.dir_out = !direction;
2719
2720         INIT_LIST_HEAD(&dep->pending_list);
2721         INIT_LIST_HEAD(&dep->started_list);
2722         INIT_LIST_HEAD(&dep->cancelled_list);
2723
2724         dwc3_debugfs_create_endpoint_dir(dep);
2725
2726         return 0;
2727 }
2728
2729 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2730 {
2731         u8                              epnum;
2732
2733         INIT_LIST_HEAD(&dwc->gadget->ep_list);
2734
2735         for (epnum = 0; epnum < total; epnum++) {
2736                 int                     ret;
2737
2738                 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2739                 if (ret)
2740                         return ret;
2741         }
2742
2743         return 0;
2744 }
2745
2746 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2747 {
2748         struct dwc3_ep                  *dep;
2749         u8                              epnum;
2750
2751         for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2752                 dep = dwc->eps[epnum];
2753                 if (!dep)
2754                         continue;
2755                 /*
2756                  * Physical endpoints 0 and 1 are special; they form the
2757                  * bi-directional USB endpoint 0.
2758                  *
2759                  * For those two physical endpoints, we don't allocate a TRB
2760                  * pool nor do we add them the endpoints list. Due to that, we
2761                  * shouldn't do these two operations otherwise we would end up
2762                  * with all sorts of bugs when removing dwc3.ko.
2763                  */
2764                 if (epnum != 0 && epnum != 1) {
2765                         dwc3_free_trb_pool(dep);
2766                         list_del(&dep->endpoint.ep_list);
2767                 }
2768
2769                 debugfs_remove_recursive(debugfs_lookup(dep->name, dwc->root));
2770                 kfree(dep);
2771         }
2772 }
2773
2774 /* -------------------------------------------------------------------------- */
2775
2776 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2777                 struct dwc3_request *req, struct dwc3_trb *trb,
2778                 const struct dwc3_event_depevt *event, int status, int chain)
2779 {
2780         unsigned int            count;
2781
2782         dwc3_ep_inc_deq(dep);
2783
2784         trace_dwc3_complete_trb(dep, trb);
2785         req->num_trbs--;
2786
2787         /*
2788          * If we're in the middle of series of chained TRBs and we
2789          * receive a short transfer along the way, DWC3 will skip
2790          * through all TRBs including the last TRB in the chain (the
2791          * where CHN bit is zero. DWC3 will also avoid clearing HWO
2792          * bit and SW has to do it manually.
2793          *
2794          * We're going to do that here to avoid problems of HW trying
2795          * to use bogus TRBs for transfers.
2796          */
2797         if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2798                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2799
2800         /*
2801          * For isochronous transfers, the first TRB in a service interval must
2802          * have the Isoc-First type. Track and report its interval frame number.
2803          */
2804         if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2805             (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2806                 unsigned int frame_number;
2807
2808                 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2809                 frame_number &= ~(dep->interval - 1);
2810                 req->request.frame_number = frame_number;
2811         }
2812
2813         /*
2814          * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2815          * this TRB points to the bounce buffer address, it's a MPS alignment
2816          * TRB. Don't add it to req->remaining calculation.
2817          */
2818         if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2819             trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
2820                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2821                 return 1;
2822         }
2823
2824         count = trb->size & DWC3_TRB_SIZE_MASK;
2825         req->remaining += count;
2826
2827         if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2828                 return 1;
2829
2830         if (event->status & DEPEVT_STATUS_SHORT && !chain)
2831                 return 1;
2832
2833         if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
2834             DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
2835                 return 1;
2836
2837         if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2838             (trb->ctrl & DWC3_TRB_CTRL_LST))
2839                 return 1;
2840
2841         return 0;
2842 }
2843
2844 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2845                 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2846                 int status)
2847 {
2848         struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2849         struct scatterlist *sg = req->sg;
2850         struct scatterlist *s;
2851         unsigned int num_queued = req->num_queued_sgs;
2852         unsigned int i;
2853         int ret = 0;
2854
2855         for_each_sg(sg, s, num_queued, i) {
2856                 trb = &dep->trb_pool[dep->trb_dequeue];
2857
2858                 req->sg = sg_next(s);
2859                 req->num_queued_sgs--;
2860
2861                 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2862                                 trb, event, status, true);
2863                 if (ret)
2864                         break;
2865         }
2866
2867         return ret;
2868 }
2869
2870 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2871                 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2872                 int status)
2873 {
2874         struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2875
2876         return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2877                         event, status, false);
2878 }
2879
2880 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2881 {
2882         return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
2883 }
2884
2885 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2886                 const struct dwc3_event_depevt *event,
2887                 struct dwc3_request *req, int status)
2888 {
2889         int request_status;
2890         int ret;
2891
2892         if (req->request.num_mapped_sgs)
2893                 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2894                                 status);
2895         else
2896                 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2897                                 status);
2898
2899         req->request.actual = req->request.length - req->remaining;
2900
2901         if (!dwc3_gadget_ep_request_completed(req))
2902                 goto out;
2903
2904         if (req->needs_extra_trb) {
2905                 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2906                                 status);
2907                 req->needs_extra_trb = false;
2908         }
2909
2910         /*
2911          * The event status only reflects the status of the TRB with IOC set.
2912          * For the requests that don't set interrupt on completion, the driver
2913          * needs to check and return the status of the completed TRBs associated
2914          * with the request. Use the status of the last TRB of the request.
2915          */
2916         if (req->request.no_interrupt) {
2917                 struct dwc3_trb *trb;
2918
2919                 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
2920                 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
2921                 case DWC3_TRBSTS_MISSED_ISOC:
2922                         /* Isoc endpoint only */
2923                         request_status = -EXDEV;
2924                         break;
2925                 case DWC3_TRB_STS_XFER_IN_PROG:
2926                         /* Applicable when End Transfer with ForceRM=0 */
2927                 case DWC3_TRBSTS_SETUP_PENDING:
2928                         /* Control endpoint only */
2929                 case DWC3_TRBSTS_OK:
2930                 default:
2931                         request_status = 0;
2932                         break;
2933                 }
2934         } else {
2935                 request_status = status;
2936         }
2937
2938         dwc3_gadget_giveback(dep, req, request_status);
2939
2940 out:
2941         return ret;
2942 }
2943
2944 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2945                 const struct dwc3_event_depevt *event, int status)
2946 {
2947         struct dwc3_request     *req;
2948         struct dwc3_request     *tmp;
2949
2950         list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2951                 int ret;
2952
2953                 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2954                                 req, status);
2955                 if (ret)
2956                         break;
2957         }
2958 }
2959
2960 static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2961 {
2962         struct dwc3_request     *req;
2963
2964         if (!list_empty(&dep->pending_list))
2965                 return true;
2966
2967         /*
2968          * We only need to check the first entry of the started list. We can
2969          * assume the completed requests are removed from the started list.
2970          */
2971         req = next_request(&dep->started_list);
2972         if (!req)
2973                 return false;
2974
2975         return !dwc3_gadget_ep_request_completed(req);
2976 }
2977
2978 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2979                 const struct dwc3_event_depevt *event)
2980 {
2981         dep->frame_number = event->parameters;
2982 }
2983
2984 static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2985                 const struct dwc3_event_depevt *event, int status)
2986 {
2987         struct dwc3             *dwc = dep->dwc;
2988         bool                    no_started_trb = true;
2989
2990         dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2991
2992         if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2993                 goto out;
2994
2995         if (!dep->endpoint.desc)
2996                 return no_started_trb;
2997
2998         if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2999                 list_empty(&dep->started_list) &&
3000                 (list_empty(&dep->pending_list) || status == -EXDEV))
3001                 dwc3_stop_active_transfer(dep, true, true);
3002         else if (dwc3_gadget_ep_should_continue(dep))
3003                 if (__dwc3_gadget_kick_transfer(dep) == 0)
3004                         no_started_trb = false;
3005
3006 out:
3007         /*
3008          * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3009          * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3010          */
3011         if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3012                 u32             reg;
3013                 int             i;
3014
3015                 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
3016                         dep = dwc->eps[i];
3017
3018                         if (!(dep->flags & DWC3_EP_ENABLED))
3019                                 continue;
3020
3021                         if (!list_empty(&dep->started_list))
3022                                 return no_started_trb;
3023                 }
3024
3025                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3026                 reg |= dwc->u1u2;
3027                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3028
3029                 dwc->u1u2 = 0;
3030         }
3031
3032         return no_started_trb;
3033 }
3034
3035 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3036                 const struct dwc3_event_depevt *event)
3037 {
3038         int status = 0;
3039
3040         if (!dep->endpoint.desc)
3041                 return;
3042
3043         if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3044                 dwc3_gadget_endpoint_frame_from_event(dep, event);
3045
3046         if (event->status & DEPEVT_STATUS_BUSERR)
3047                 status = -ECONNRESET;
3048
3049         if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3050                 status = -EXDEV;
3051
3052         dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3053 }
3054
3055 static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3056                 const struct dwc3_event_depevt *event)
3057 {
3058         int status = 0;
3059
3060         dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3061
3062         if (event->status & DEPEVT_STATUS_BUSERR)
3063                 status = -ECONNRESET;
3064
3065         if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3066                 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
3067 }
3068
3069 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3070                 const struct dwc3_event_depevt *event)
3071 {
3072         dwc3_gadget_endpoint_frame_from_event(dep, event);
3073
3074         /*
3075          * The XferNotReady event is generated only once before the endpoint
3076          * starts. It will be generated again when END_TRANSFER command is
3077          * issued. For some controller versions, the XferNotReady event may be
3078          * generated while the END_TRANSFER command is still in process. Ignore
3079          * it and wait for the next XferNotReady event after the command is
3080          * completed.
3081          */
3082         if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3083                 return;
3084
3085         (void) __dwc3_gadget_start_isoc(dep);
3086 }
3087
3088 static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3089                 const struct dwc3_event_depevt *event)
3090 {
3091         u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3092
3093         if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3094                 return;
3095
3096         /*
3097          * The END_TRANSFER command will cause the controller to generate a
3098          * NoStream Event, and it's not due to the host DP NoStream rejection.
3099          * Ignore the next NoStream event.
3100          */
3101         if (dep->stream_capable)
3102                 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3103
3104         dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3105         dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3106         dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3107
3108         if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3109                 struct dwc3 *dwc = dep->dwc;
3110
3111                 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3112                 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3113                         struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3114
3115                         dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3116                         if (dwc->delayed_status)
3117                                 __dwc3_gadget_ep0_set_halt(ep0, 1);
3118                         return;
3119                 }
3120
3121                 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3122                 if (dwc->delayed_status)
3123                         dwc3_ep0_send_delayed_status(dwc);
3124         }
3125
3126         if ((dep->flags & DWC3_EP_DELAY_START) &&
3127             !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3128                 __dwc3_gadget_kick_transfer(dep);
3129
3130         dep->flags &= ~DWC3_EP_DELAY_START;
3131 }
3132
3133 static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3134                 const struct dwc3_event_depevt *event)
3135 {
3136         struct dwc3 *dwc = dep->dwc;
3137
3138         if (event->status == DEPEVT_STREAMEVT_FOUND) {
3139                 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3140                 goto out;
3141         }
3142
3143         /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3144         switch (event->parameters) {
3145         case DEPEVT_STREAM_PRIME:
3146                 /*
3147                  * If the host can properly transition the endpoint state from
3148                  * idle to prime after a NoStream rejection, there's no need to
3149                  * force restarting the endpoint to reinitiate the stream. To
3150                  * simplify the check, assume the host follows the USB spec if
3151                  * it primed the endpoint more than once.
3152                  */
3153                 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3154                         if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3155                                 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3156                         else
3157                                 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3158                 }
3159
3160                 break;
3161         case DEPEVT_STREAM_NOSTREAM:
3162                 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3163                     !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3164                     !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3165                         break;
3166
3167                 /*
3168                  * If the host rejects a stream due to no active stream, by the
3169                  * USB and xHCI spec, the endpoint will be put back to idle
3170                  * state. When the host is ready (buffer added/updated), it will
3171                  * prime the endpoint to inform the usb device controller. This
3172                  * triggers the device controller to issue ERDY to restart the
3173                  * stream. However, some hosts don't follow this and keep the
3174                  * endpoint in the idle state. No prime will come despite host
3175                  * streams are updated, and the device controller will not be
3176                  * triggered to generate ERDY to move the next stream data. To
3177                  * workaround this and maintain compatibility with various
3178                  * hosts, force to reinitate the stream until the host is ready
3179                  * instead of waiting for the host to prime the endpoint.
3180                  */
3181                 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3182                         unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3183
3184                         dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3185                 } else {
3186                         dep->flags |= DWC3_EP_DELAY_START;
3187                         dwc3_stop_active_transfer(dep, true, true);
3188                         return;
3189                 }
3190                 break;
3191         }
3192
3193 out:
3194         dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3195 }
3196
3197 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3198                 const struct dwc3_event_depevt *event)
3199 {
3200         struct dwc3_ep          *dep;
3201         u8                      epnum = event->endpoint_number;
3202
3203         dep = dwc->eps[epnum];
3204
3205         if (!(dep->flags & DWC3_EP_ENABLED)) {
3206                 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
3207                         return;
3208
3209                 /* Handle only EPCMDCMPLT when EP disabled */
3210                 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3211                         return;
3212         }
3213
3214         if (epnum == 0 || epnum == 1) {
3215                 dwc3_ep0_interrupt(dwc, event);
3216                 return;
3217         }
3218
3219         switch (event->endpoint_event) {
3220         case DWC3_DEPEVT_XFERINPROGRESS:
3221                 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
3222                 break;
3223         case DWC3_DEPEVT_XFERNOTREADY:
3224                 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
3225                 break;
3226         case DWC3_DEPEVT_EPCMDCMPLT:
3227                 dwc3_gadget_endpoint_command_complete(dep, event);
3228                 break;
3229         case DWC3_DEPEVT_XFERCOMPLETE:
3230                 dwc3_gadget_endpoint_transfer_complete(dep, event);
3231                 break;
3232         case DWC3_DEPEVT_STREAMEVT:
3233                 dwc3_gadget_endpoint_stream_event(dep, event);
3234                 break;
3235         case DWC3_DEPEVT_RXTXFIFOEVT:
3236                 break;
3237         }
3238 }
3239
3240 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3241 {
3242         if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3243                 spin_unlock(&dwc->lock);
3244                 dwc->gadget_driver->disconnect(dwc->gadget);
3245                 spin_lock(&dwc->lock);
3246         }
3247 }
3248
3249 static void dwc3_suspend_gadget(struct dwc3 *dwc)
3250 {
3251         if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
3252                 spin_unlock(&dwc->lock);
3253                 dwc->gadget_driver->suspend(dwc->gadget);
3254                 spin_lock(&dwc->lock);
3255         }
3256 }
3257
3258 static void dwc3_resume_gadget(struct dwc3 *dwc)
3259 {
3260         if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3261                 spin_unlock(&dwc->lock);
3262                 dwc->gadget_driver->resume(dwc->gadget);
3263                 spin_lock(&dwc->lock);
3264         }
3265 }
3266
3267 static void dwc3_reset_gadget(struct dwc3 *dwc)
3268 {
3269         if (!dwc->gadget_driver)
3270                 return;
3271
3272         if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
3273                 spin_unlock(&dwc->lock);
3274                 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
3275                 spin_lock(&dwc->lock);
3276         }
3277 }
3278
3279 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3280         bool interrupt)
3281 {
3282         if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3283             (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
3284                 return;
3285
3286         /*
3287          * NOTICE: We are violating what the Databook says about the
3288          * EndTransfer command. Ideally we would _always_ wait for the
3289          * EndTransfer Command Completion IRQ, but that's causing too
3290          * much trouble synchronizing between us and gadget driver.
3291          *
3292          * We have discussed this with the IP Provider and it was
3293          * suggested to giveback all requests here.
3294          *
3295          * Note also that a similar handling was tested by Synopsys
3296          * (thanks a lot Paul) and nothing bad has come out of it.
3297          * In short, what we're doing is issuing EndTransfer with
3298          * CMDIOC bit set and delay kicking transfer until the
3299          * EndTransfer command had completed.
3300          *
3301          * As of IP version 3.10a of the DWC_usb3 IP, the controller
3302          * supports a mode to work around the above limitation. The
3303          * software can poll the CMDACT bit in the DEPCMD register
3304          * after issuing a EndTransfer command. This mode is enabled
3305          * by writing GUCTL2[14]. This polling is already done in the
3306          * dwc3_send_gadget_ep_cmd() function so if the mode is
3307          * enabled, the EndTransfer command will have completed upon
3308          * returning from this function.
3309          *
3310          * This mode is NOT available on the DWC_usb31 IP.  In this
3311          * case, if the IOC bit is not set, then delay by 1ms
3312          * after issuing the EndTransfer command.  This allows for the
3313          * controller to handle the command completely before DWC3
3314          * remove requests attempts to unmap USB request buffers.
3315          */
3316
3317         __dwc3_stop_active_transfer(dep, force, interrupt);
3318 }
3319
3320 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3321 {
3322         u32 epnum;
3323
3324         for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3325                 struct dwc3_ep *dep;
3326                 int ret;
3327
3328                 dep = dwc->eps[epnum];
3329                 if (!dep)
3330                         continue;
3331
3332                 if (!(dep->flags & DWC3_EP_STALL))
3333                         continue;
3334
3335                 dep->flags &= ~DWC3_EP_STALL;
3336
3337                 ret = dwc3_send_clear_stall_ep_cmd(dep);
3338                 WARN_ON_ONCE(ret);
3339         }
3340 }
3341
3342 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3343 {
3344         int                     reg;
3345
3346         dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3347
3348         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3349         reg &= ~DWC3_DCTL_INITU1ENA;
3350         reg &= ~DWC3_DCTL_INITU2ENA;
3351         dwc3_gadget_dctl_write_safe(dwc, reg);
3352
3353         dwc3_disconnect_gadget(dwc);
3354
3355         dwc->gadget->speed = USB_SPEED_UNKNOWN;
3356         dwc->setup_packet_pending = false;
3357         usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
3358
3359         dwc->connected = false;
3360 }
3361
3362 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3363 {
3364         u32                     reg;
3365
3366         /*
3367          * Ideally, dwc3_reset_gadget() would trigger the function
3368          * drivers to stop any active transfers through ep disable.
3369          * However, for functions which defer ep disable, such as mass
3370          * storage, we will need to rely on the call to stop active
3371          * transfers here, and avoid allowing of request queuing.
3372          */
3373         dwc->connected = false;
3374
3375         /*
3376          * WORKAROUND: DWC3 revisions <1.88a have an issue which
3377          * would cause a missing Disconnect Event if there's a
3378          * pending Setup Packet in the FIFO.
3379          *
3380          * There's no suggested workaround on the official Bug
3381          * report, which states that "unless the driver/application
3382          * is doing any special handling of a disconnect event,
3383          * there is no functional issue".
3384          *
3385          * Unfortunately, it turns out that we _do_ some special
3386          * handling of a disconnect event, namely complete all
3387          * pending transfers, notify gadget driver of the
3388          * disconnection, and so on.
3389          *
3390          * Our suggested workaround is to follow the Disconnect
3391          * Event steps here, instead, based on a setup_packet_pending
3392          * flag. Such flag gets set whenever we have a SETUP_PENDING
3393          * status for EP0 TRBs and gets cleared on XferComplete for the
3394          * same endpoint.
3395          *
3396          * Refers to:
3397          *
3398          * STAR#9000466709: RTL: Device : Disconnect event not
3399          * generated if setup packet pending in FIFO
3400          */
3401         if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
3402                 if (dwc->setup_packet_pending)
3403                         dwc3_gadget_disconnect_interrupt(dwc);
3404         }
3405
3406         dwc3_reset_gadget(dwc);
3407         /*
3408          * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3409          * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3410          * needs to ensure that it sends "a DEPENDXFER command for any active
3411          * transfers."
3412          */
3413         dwc3_stop_active_transfers(dwc);
3414         dwc->connected = true;
3415
3416         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3417         reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3418         dwc3_gadget_dctl_write_safe(dwc, reg);
3419         dwc->test_mode = false;
3420         dwc3_clear_stall_all_ep(dwc);
3421
3422         /* Reset device address to zero */
3423         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3424         reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3425         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3426 }
3427
3428 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3429 {
3430         struct dwc3_ep          *dep;
3431         int                     ret;
3432         u32                     reg;
3433         u8                      speed;
3434
3435         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3436         speed = reg & DWC3_DSTS_CONNECTSPD;
3437         dwc->speed = speed;
3438
3439         /*
3440          * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3441          * each time on Connect Done.
3442          *
3443          * Currently we always use the reset value. If any platform
3444          * wants to set this to a different value, we need to add a
3445          * setting and update GCTL.RAMCLKSEL here.
3446          */
3447
3448         switch (speed) {
3449         case DWC3_DSTS_SUPERSPEED_PLUS:
3450                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3451                 dwc->gadget->ep0->maxpacket = 512;
3452                 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3453                 break;
3454         case DWC3_DSTS_SUPERSPEED:
3455                 /*
3456                  * WORKAROUND: DWC3 revisions <1.90a have an issue which
3457                  * would cause a missing USB3 Reset event.
3458                  *
3459                  * In such situations, we should force a USB3 Reset
3460                  * event by calling our dwc3_gadget_reset_interrupt()
3461                  * routine.
3462                  *
3463                  * Refers to:
3464                  *
3465                  * STAR#9000483510: RTL: SS : USB3 reset event may
3466                  * not be generated always when the link enters poll
3467                  */
3468                 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
3469                         dwc3_gadget_reset_interrupt(dwc);
3470
3471                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3472                 dwc->gadget->ep0->maxpacket = 512;
3473                 dwc->gadget->speed = USB_SPEED_SUPER;
3474                 break;
3475         case DWC3_DSTS_HIGHSPEED:
3476                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3477                 dwc->gadget->ep0->maxpacket = 64;
3478                 dwc->gadget->speed = USB_SPEED_HIGH;
3479                 break;
3480         case DWC3_DSTS_FULLSPEED:
3481                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3482                 dwc->gadget->ep0->maxpacket = 64;
3483                 dwc->gadget->speed = USB_SPEED_FULL;
3484                 break;
3485         case DWC3_DSTS_LOWSPEED:
3486                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3487                 dwc->gadget->ep0->maxpacket = 8;
3488                 dwc->gadget->speed = USB_SPEED_LOW;
3489                 break;
3490         }
3491
3492         dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
3493
3494         /* Enable USB2 LPM Capability */
3495
3496         if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
3497             !dwc->usb2_gadget_lpm_disable &&
3498             (speed != DWC3_DSTS_SUPERSPEED) &&
3499             (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
3500                 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3501                 reg |= DWC3_DCFG_LPM_CAP;
3502                 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3503
3504                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3505                 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3506
3507                 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3508                                             (dwc->is_utmi_l1_suspend << 4));
3509
3510                 /*
3511                  * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3512                  * DCFG.LPMCap is set, core responses with an ACK and the
3513                  * BESL value in the LPM token is less than or equal to LPM
3514                  * NYET threshold.
3515                  */
3516                 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
3517                                 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
3518
3519                 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
3520                         reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
3521
3522                 dwc3_gadget_dctl_write_safe(dwc, reg);
3523         } else {
3524                 if (dwc->usb2_gadget_lpm_disable) {
3525                         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3526                         reg &= ~DWC3_DCFG_LPM_CAP;
3527                         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3528                 }
3529
3530                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3531                 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3532                 dwc3_gadget_dctl_write_safe(dwc, reg);
3533         }
3534
3535         dep = dwc->eps[0];
3536         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3537         if (ret) {
3538                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3539                 return;
3540         }
3541
3542         dep = dwc->eps[1];
3543         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
3544         if (ret) {
3545                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3546                 return;
3547         }
3548
3549         /*
3550          * Configure PHY via GUSB3PIPECTLn if required.
3551          *
3552          * Update GTXFIFOSIZn
3553          *
3554          * In both cases reset values should be sufficient.
3555          */
3556 }
3557
3558 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3559 {
3560         /*
3561          * TODO take core out of low power mode when that's
3562          * implemented.
3563          */
3564
3565         if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3566                 spin_unlock(&dwc->lock);
3567                 dwc->gadget_driver->resume(dwc->gadget);
3568                 spin_lock(&dwc->lock);
3569         }
3570 }
3571
3572 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3573                 unsigned int evtinfo)
3574 {
3575         enum dwc3_link_state    next = evtinfo & DWC3_LINK_STATE_MASK;
3576         unsigned int            pwropt;
3577
3578         /*
3579          * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3580          * Hibernation mode enabled which would show up when device detects
3581          * host-initiated U3 exit.
3582          *
3583          * In that case, device will generate a Link State Change Interrupt
3584          * from U3 to RESUME which is only necessary if Hibernation is
3585          * configured in.
3586          *
3587          * There are no functional changes due to such spurious event and we
3588          * just need to ignore it.
3589          *
3590          * Refers to:
3591          *
3592          * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3593          * operational mode
3594          */
3595         pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3596         if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
3597                         (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3598                 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3599                                 (next == DWC3_LINK_STATE_RESUME)) {
3600                         return;
3601                 }
3602         }
3603
3604         /*
3605          * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3606          * on the link partner, the USB session might do multiple entry/exit
3607          * of low power states before a transfer takes place.
3608          *
3609          * Due to this problem, we might experience lower throughput. The
3610          * suggested workaround is to disable DCTL[12:9] bits if we're
3611          * transitioning from U1/U2 to U0 and enable those bits again
3612          * after a transfer completes and there are no pending transfers
3613          * on any of the enabled endpoints.
3614          *
3615          * This is the first half of that workaround.
3616          *
3617          * Refers to:
3618          *
3619          * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3620          * core send LGO_Ux entering U0
3621          */
3622         if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3623                 if (next == DWC3_LINK_STATE_U0) {
3624                         u32     u1u2;
3625                         u32     reg;
3626
3627                         switch (dwc->link_state) {
3628                         case DWC3_LINK_STATE_U1:
3629                         case DWC3_LINK_STATE_U2:
3630                                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3631                                 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3632                                                 | DWC3_DCTL_ACCEPTU2ENA
3633                                                 | DWC3_DCTL_INITU1ENA
3634                                                 | DWC3_DCTL_ACCEPTU1ENA);
3635
3636                                 if (!dwc->u1u2)
3637                                         dwc->u1u2 = reg & u1u2;
3638
3639                                 reg &= ~u1u2;
3640
3641                                 dwc3_gadget_dctl_write_safe(dwc, reg);
3642                                 break;
3643                         default:
3644                                 /* do nothing */
3645                                 break;
3646                         }
3647                 }
3648         }
3649
3650         switch (next) {
3651         case DWC3_LINK_STATE_U1:
3652                 if (dwc->speed == USB_SPEED_SUPER)
3653                         dwc3_suspend_gadget(dwc);
3654                 break;
3655         case DWC3_LINK_STATE_U2:
3656         case DWC3_LINK_STATE_U3:
3657                 dwc3_suspend_gadget(dwc);
3658                 break;
3659         case DWC3_LINK_STATE_RESUME:
3660                 dwc3_resume_gadget(dwc);
3661                 break;
3662         default:
3663                 /* do nothing */
3664                 break;
3665         }
3666
3667         dwc->link_state = next;
3668 }
3669
3670 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3671                                           unsigned int evtinfo)
3672 {
3673         enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3674
3675         if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3676                 dwc3_suspend_gadget(dwc);
3677
3678         dwc->link_state = next;
3679 }
3680
3681 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3682                 unsigned int evtinfo)
3683 {
3684         unsigned int is_ss = evtinfo & BIT(4);
3685
3686         /*
3687          * WORKAROUND: DWC3 revison 2.20a with hibernation support
3688          * have a known issue which can cause USB CV TD.9.23 to fail
3689          * randomly.
3690          *
3691          * Because of this issue, core could generate bogus hibernation
3692          * events which SW needs to ignore.
3693          *
3694          * Refers to:
3695          *
3696          * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3697          * Device Fallback from SuperSpeed
3698          */
3699         if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3700                 return;
3701
3702         /* enter hibernation here */
3703 }
3704
3705 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3706                 const struct dwc3_event_devt *event)
3707 {
3708         switch (event->type) {
3709         case DWC3_DEVICE_EVENT_DISCONNECT:
3710                 dwc3_gadget_disconnect_interrupt(dwc);
3711                 break;
3712         case DWC3_DEVICE_EVENT_RESET:
3713                 dwc3_gadget_reset_interrupt(dwc);
3714                 break;
3715         case DWC3_DEVICE_EVENT_CONNECT_DONE:
3716                 dwc3_gadget_conndone_interrupt(dwc);
3717                 break;
3718         case DWC3_DEVICE_EVENT_WAKEUP:
3719                 dwc3_gadget_wakeup_interrupt(dwc);
3720                 break;
3721         case DWC3_DEVICE_EVENT_HIBER_REQ:
3722                 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3723                                         "unexpected hibernation event\n"))
3724                         break;
3725
3726                 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3727                 break;
3728         case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3729                 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3730                 break;
3731         case DWC3_DEVICE_EVENT_EOPF:
3732                 /* It changed to be suspend event for version 2.30a and above */
3733                 if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
3734                         dwc3_gadget_suspend_interrupt(dwc, event->event_info);
3735                 break;
3736         case DWC3_DEVICE_EVENT_SOF:
3737         case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3738         case DWC3_DEVICE_EVENT_CMD_CMPL:
3739         case DWC3_DEVICE_EVENT_OVERFLOW:
3740                 break;
3741         default:
3742                 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3743         }
3744 }
3745
3746 static void dwc3_process_event_entry(struct dwc3 *dwc,
3747                 const union dwc3_event *event)
3748 {
3749         trace_dwc3_event(event->raw, dwc);
3750
3751         if (!event->type.is_devspec)
3752                 dwc3_endpoint_interrupt(dwc, &event->depevt);
3753         else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3754                 dwc3_gadget_interrupt(dwc, &event->devt);
3755         else
3756                 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3757 }
3758
3759 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3760 {
3761         struct dwc3 *dwc = evt->dwc;
3762         irqreturn_t ret = IRQ_NONE;
3763         int left;
3764         u32 reg;
3765
3766         left = evt->count;
3767
3768         if (!(evt->flags & DWC3_EVENT_PENDING))
3769                 return IRQ_NONE;
3770
3771         while (left > 0) {
3772                 union dwc3_event event;
3773
3774                 event.raw = *(u32 *) (evt->cache + evt->lpos);
3775
3776                 dwc3_process_event_entry(dwc, &event);
3777
3778                 /*
3779                  * FIXME we wrap around correctly to the next entry as
3780                  * almost all entries are 4 bytes in size. There is one
3781                  * entry which has 12 bytes which is a regular entry
3782                  * followed by 8 bytes data. ATM I don't know how
3783                  * things are organized if we get next to the a
3784                  * boundary so I worry about that once we try to handle
3785                  * that.
3786                  */
3787                 evt->lpos = (evt->lpos + 4) % evt->length;
3788                 left -= 4;
3789         }
3790
3791         evt->count = 0;
3792         ret = IRQ_HANDLED;
3793
3794         /* Unmask interrupt */
3795         reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3796         reg &= ~DWC3_GEVNTSIZ_INTMASK;
3797         dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3798
3799         if (dwc->imod_interval) {
3800                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3801                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3802         }
3803
3804         /* Keep the clearing of DWC3_EVENT_PENDING at the end */
3805         evt->flags &= ~DWC3_EVENT_PENDING;
3806
3807         return ret;
3808 }
3809
3810 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3811 {
3812         struct dwc3_event_buffer *evt = _evt;
3813         struct dwc3 *dwc = evt->dwc;
3814         unsigned long flags;
3815         irqreturn_t ret = IRQ_NONE;
3816
3817         local_bh_disable();
3818         spin_lock_irqsave(&dwc->lock, flags);
3819         ret = dwc3_process_event_buf(evt);
3820         spin_unlock_irqrestore(&dwc->lock, flags);
3821         local_bh_enable();
3822
3823         return ret;
3824 }
3825
3826 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3827 {
3828         struct dwc3 *dwc = evt->dwc;
3829         u32 amount;
3830         u32 count;
3831         u32 reg;
3832
3833         if (pm_runtime_suspended(dwc->dev)) {
3834                 dwc->pending_events = true;
3835                 /*
3836                  * Trigger runtime resume. The get() function will be balanced
3837                  * after processing the pending events in dwc3_process_pending
3838                  * events().
3839                  */
3840                 pm_runtime_get(dwc->dev);
3841                 disable_irq_nosync(dwc->irq_gadget);
3842                 return IRQ_HANDLED;
3843         }
3844
3845         /*
3846          * With PCIe legacy interrupt, test shows that top-half irq handler can
3847          * be called again after HW interrupt deassertion. Check if bottom-half
3848          * irq event handler completes before caching new event to prevent
3849          * losing events.
3850          */
3851         if (evt->flags & DWC3_EVENT_PENDING)
3852                 return IRQ_HANDLED;
3853
3854         count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3855         count &= DWC3_GEVNTCOUNT_MASK;
3856         if (!count)
3857                 return IRQ_NONE;
3858
3859         evt->count = count;
3860         evt->flags |= DWC3_EVENT_PENDING;
3861
3862         /* Mask interrupt */
3863         reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3864         reg |= DWC3_GEVNTSIZ_INTMASK;
3865         dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3866
3867         amount = min(count, evt->length - evt->lpos);
3868         memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3869
3870         if (amount < count)
3871                 memcpy(evt->cache, evt->buf, count - amount);
3872
3873         dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3874
3875         return IRQ_WAKE_THREAD;
3876 }
3877
3878 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3879 {
3880         struct dwc3_event_buffer        *evt = _evt;
3881
3882         return dwc3_check_event_buf(evt);
3883 }
3884
3885 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3886 {
3887         struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3888         int irq;
3889
3890         irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
3891         if (irq > 0)
3892                 goto out;
3893
3894         if (irq == -EPROBE_DEFER)
3895                 goto out;
3896
3897         irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
3898         if (irq > 0)
3899                 goto out;
3900
3901         if (irq == -EPROBE_DEFER)
3902                 goto out;
3903
3904         irq = platform_get_irq(dwc3_pdev, 0);
3905         if (irq > 0)
3906                 goto out;
3907
3908         if (!irq)
3909                 irq = -EINVAL;
3910
3911 out:
3912         return irq;
3913 }
3914
3915 static void dwc_gadget_release(struct device *dev)
3916 {
3917         struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3918
3919         kfree(gadget);
3920 }
3921
3922 /**
3923  * dwc3_gadget_init - initializes gadget related registers
3924  * @dwc: pointer to our controller context structure
3925  *
3926  * Returns 0 on success otherwise negative errno.
3927  */
3928 int dwc3_gadget_init(struct dwc3 *dwc)
3929 {
3930         int ret;
3931         int irq;
3932         struct device *dev;
3933
3934         irq = dwc3_gadget_get_irq(dwc);
3935         if (irq < 0) {
3936                 ret = irq;
3937                 goto err0;
3938         }
3939
3940         dwc->irq_gadget = irq;
3941
3942         dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3943                                           sizeof(*dwc->ep0_trb) * 2,
3944                                           &dwc->ep0_trb_addr, GFP_KERNEL);
3945         if (!dwc->ep0_trb) {
3946                 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3947                 ret = -ENOMEM;
3948                 goto err0;
3949         }
3950
3951         dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3952         if (!dwc->setup_buf) {
3953                 ret = -ENOMEM;
3954                 goto err1;
3955         }
3956
3957         dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3958                         &dwc->bounce_addr, GFP_KERNEL);
3959         if (!dwc->bounce) {
3960                 ret = -ENOMEM;
3961                 goto err2;
3962         }
3963
3964         init_completion(&dwc->ep0_in_setup);
3965         dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3966         if (!dwc->gadget) {
3967                 ret = -ENOMEM;
3968                 goto err3;
3969         }
3970
3971
3972         usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3973         dev                             = &dwc->gadget->dev;
3974         dev->platform_data              = dwc;
3975         dwc->gadget->ops                = &dwc3_gadget_ops;
3976         dwc->gadget->speed              = USB_SPEED_UNKNOWN;
3977         dwc->gadget->sg_supported       = true;
3978         dwc->gadget->name               = "dwc3-gadget";
3979         dwc->gadget->lpm_capable        = !dwc->usb2_gadget_lpm_disable;
3980
3981         /*
3982          * FIXME We might be setting max_speed to <SUPER, however versions
3983          * <2.20a of dwc3 have an issue with metastability (documented
3984          * elsewhere in this driver) which tells us we can't set max speed to
3985          * anything lower than SUPER.
3986          *
3987          * Because gadget.max_speed is only used by composite.c and function
3988          * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3989          * to happen so we avoid sending SuperSpeed Capability descriptor
3990          * together with our BOS descriptor as that could confuse host into
3991          * thinking we can handle super speed.
3992          *
3993          * Note that, in fact, we won't even support GetBOS requests when speed
3994          * is less than super speed because we don't have means, yet, to tell
3995          * composite.c that we are USB 2.0 + LPM ECN.
3996          */
3997         if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
3998             !dwc->dis_metastability_quirk)
3999                 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
4000                                 dwc->revision);
4001
4002         dwc->gadget->max_speed          = dwc->maximum_speed;
4003
4004         /*
4005          * REVISIT: Here we should clear all pending IRQs to be
4006          * sure we're starting from a well known location.
4007          */
4008
4009         ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
4010         if (ret)
4011                 goto err4;
4012
4013         ret = usb_add_gadget(dwc->gadget);
4014         if (ret) {
4015                 dev_err(dwc->dev, "failed to add gadget\n");
4016                 goto err5;
4017         }
4018
4019         dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
4020
4021         return 0;
4022
4023 err5:
4024         dwc3_gadget_free_endpoints(dwc);
4025 err4:
4026         usb_put_gadget(dwc->gadget);
4027         dwc->gadget = NULL;
4028 err3:
4029         dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4030                         dwc->bounce_addr);
4031
4032 err2:
4033         kfree(dwc->setup_buf);
4034
4035 err1:
4036         dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4037                         dwc->ep0_trb, dwc->ep0_trb_addr);
4038
4039 err0:
4040         return ret;
4041 }
4042
4043 /* -------------------------------------------------------------------------- */
4044
4045 void dwc3_gadget_exit(struct dwc3 *dwc)
4046 {
4047         if (!dwc->gadget)
4048                 return;
4049
4050         dwc3_enable_susphy(dwc, false);
4051         usb_del_gadget(dwc->gadget);
4052         dwc3_gadget_free_endpoints(dwc);
4053         usb_put_gadget(dwc->gadget);
4054         dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4055                           dwc->bounce_addr);
4056         kfree(dwc->setup_buf);
4057         dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4058                           dwc->ep0_trb, dwc->ep0_trb_addr);
4059 }
4060
4061 int dwc3_gadget_suspend(struct dwc3 *dwc)
4062 {
4063         if (!dwc->gadget_driver)
4064                 return 0;
4065
4066         dwc3_gadget_run_stop(dwc, false, false);
4067         dwc3_disconnect_gadget(dwc);
4068         __dwc3_gadget_stop(dwc);
4069
4070         return 0;
4071 }
4072
4073 int dwc3_gadget_resume(struct dwc3 *dwc)
4074 {
4075         int                     ret;
4076
4077         if (!dwc->gadget_driver || !dwc->softconnect)
4078                 return 0;
4079
4080         ret = __dwc3_gadget_start(dwc);
4081         if (ret < 0)
4082                 goto err0;
4083
4084         ret = dwc3_gadget_run_stop(dwc, true, false);
4085         if (ret < 0)
4086                 goto err1;
4087
4088         return 0;
4089
4090 err1:
4091         __dwc3_gadget_stop(dwc);
4092
4093 err0:
4094         return ret;
4095 }
4096
4097 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4098 {
4099         if (dwc->pending_events) {
4100                 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4101                 dwc3_thread_interrupt(dwc->irq_gadget, dwc->ev_buf);
4102                 pm_runtime_put(dwc->dev);
4103                 dwc->pending_events = false;
4104                 enable_irq(dwc->irq_gadget);
4105         }
4106 }