usb: dwc3: gadget: remove DWC3_EP_END_TRANSFER_PENDING
[linux-2.6-microblaze.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 - http://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 TEST_J:
50         case TEST_K:
51         case TEST_SE0_NAK:
52         case TEST_PACKET:
53         case TEST_FORCE_EN:
54                 reg |= mode << 1;
55                 break;
56         default:
57                 return -EINVAL;
58         }
59
60         dwc3_writel(dwc->regs, DWC3_DCTL, 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 (dwc->revision >= DWC3_REVISION_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 requested state */
115         reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
117
118         /*
119          * The following code is racy when called from dwc3_gadget_wakeup,
120          * and is not needed, at least on newer versions
121          */
122         if (dwc->revision >= DWC3_REVISION_194A)
123                 return 0;
124
125         /* wait for a change in DSTS */
126         retries = 10000;
127         while (--retries) {
128                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
129
130                 if (DWC3_DSTS_USBLNKST(reg) == state)
131                         return 0;
132
133                 udelay(5);
134         }
135
136         return -ETIMEDOUT;
137 }
138
139 /**
140  * dwc3_ep_inc_trb - increment a trb index.
141  * @index: Pointer to the TRB index to increment.
142  *
143  * The index should never point to the link TRB. After incrementing,
144  * if it is point to the link TRB, wrap around to the beginning. The
145  * link TRB is always at the last TRB entry.
146  */
147 static void dwc3_ep_inc_trb(u8 *index)
148 {
149         (*index)++;
150         if (*index == (DWC3_TRB_NUM - 1))
151                 *index = 0;
152 }
153
154 /**
155  * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156  * @dep: The endpoint whose enqueue pointer we're incrementing
157  */
158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
159 {
160         dwc3_ep_inc_trb(&dep->trb_enqueue);
161 }
162
163 /**
164  * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165  * @dep: The endpoint whose enqueue pointer we're incrementing
166  */
167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
168 {
169         dwc3_ep_inc_trb(&dep->trb_dequeue);
170 }
171
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173                 struct dwc3_request *req, int status)
174 {
175         struct dwc3                     *dwc = dep->dwc;
176
177         list_del(&req->list);
178         req->remaining = 0;
179         req->needs_extra_trb = false;
180
181         if (req->request.status == -EINPROGRESS)
182                 req->request.status = status;
183
184         if (req->trb)
185                 usb_gadget_unmap_request_by_dev(dwc->sysdev,
186                                 &req->request, req->direction);
187
188         req->trb = NULL;
189         trace_dwc3_gadget_giveback(req);
190
191         if (dep->number > 1)
192                 pm_runtime_put(dwc->dev);
193 }
194
195 /**
196  * dwc3_gadget_giveback - call struct usb_request's ->complete callback
197  * @dep: The endpoint to whom the request belongs to
198  * @req: The request we're giving back
199  * @status: completion code for the request
200  *
201  * Must be called with controller's lock held and interrupts disabled. This
202  * function will unmap @req and call its ->complete() callback to notify upper
203  * layers that it has completed.
204  */
205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
206                 int status)
207 {
208         struct dwc3                     *dwc = dep->dwc;
209
210         dwc3_gadget_del_and_unmap_request(dep, req, status);
211         req->status = DWC3_REQUEST_STATUS_COMPLETED;
212
213         spin_unlock(&dwc->lock);
214         usb_gadget_giveback_request(&dep->endpoint, &req->request);
215         spin_lock(&dwc->lock);
216 }
217
218 /**
219  * dwc3_send_gadget_generic_command - issue a generic command for the controller
220  * @dwc: pointer to the controller context
221  * @cmd: the command to be issued
222  * @param: command parameter
223  *
224  * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225  * and wait for its completion.
226  */
227 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
228 {
229         u32             timeout = 500;
230         int             status = 0;
231         int             ret = 0;
232         u32             reg;
233
234         dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235         dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
236
237         do {
238                 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
239                 if (!(reg & DWC3_DGCMD_CMDACT)) {
240                         status = DWC3_DGCMD_STATUS(reg);
241                         if (status)
242                                 ret = -EINVAL;
243                         break;
244                 }
245         } while (--timeout);
246
247         if (!timeout) {
248                 ret = -ETIMEDOUT;
249                 status = -ETIMEDOUT;
250         }
251
252         trace_dwc3_gadget_generic_cmd(cmd, param, status);
253
254         return ret;
255 }
256
257 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
258
259 /**
260  * dwc3_send_gadget_ep_cmd - issue an endpoint command
261  * @dep: the endpoint to which the command is going to be issued
262  * @cmd: the command to be issued
263  * @params: parameters to the command
264  *
265  * Caller should handle locking. This function will issue @cmd with given
266  * @params to @dep and wait for its completion.
267  */
268 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269                 struct dwc3_gadget_ep_cmd_params *params)
270 {
271         const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
272         struct dwc3             *dwc = dep->dwc;
273         u32                     timeout = 1000;
274         u32                     saved_config = 0;
275         u32                     reg;
276
277         int                     cmd_status = 0;
278         int                     ret = -EINVAL;
279
280         /*
281          * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
282          * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
283          * endpoint command.
284          *
285          * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286          * settings. Restore them after the command is completed.
287          *
288          * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
289          */
290         if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291                 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292                 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
293                         saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
294                         reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
295                 }
296
297                 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298                         saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299                         reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
300                 }
301
302                 if (saved_config)
303                         dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
304         }
305
306         if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
307                 int             needs_wakeup;
308
309                 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
310                                 dwc->link_state == DWC3_LINK_STATE_U2 ||
311                                 dwc->link_state == DWC3_LINK_STATE_U3);
312
313                 if (unlikely(needs_wakeup)) {
314                         ret = __dwc3_gadget_wakeup(dwc);
315                         dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316                                         ret);
317                 }
318         }
319
320         dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
321         dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
322         dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
323
324         /*
325          * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
326          * not relying on XferNotReady, we can make use of a special "No
327          * Response Update Transfer" command where we should clear both CmdAct
328          * and CmdIOC bits.
329          *
330          * With this, we don't need to wait for command completion and can
331          * straight away issue further commands to the endpoint.
332          *
333          * NOTICE: We're making an assumption that control endpoints will never
334          * make use of Update Transfer command. This is a safe assumption
335          * because we can never have more than one request at a time with
336          * Control Endpoints. If anybody changes that assumption, this chunk
337          * needs to be updated accordingly.
338          */
339         if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
340                         !usb_endpoint_xfer_isoc(desc))
341                 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
342         else
343                 cmd |= DWC3_DEPCMD_CMDACT;
344
345         dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
346         do {
347                 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
348                 if (!(reg & DWC3_DEPCMD_CMDACT)) {
349                         cmd_status = DWC3_DEPCMD_STATUS(reg);
350
351                         switch (cmd_status) {
352                         case 0:
353                                 ret = 0;
354                                 break;
355                         case DEPEVT_TRANSFER_NO_RESOURCE:
356                                 ret = -EINVAL;
357                                 break;
358                         case DEPEVT_TRANSFER_BUS_EXPIRY:
359                                 /*
360                                  * SW issues START TRANSFER command to
361                                  * isochronous ep with future frame interval. If
362                                  * future interval time has already passed when
363                                  * core receives the command, it will respond
364                                  * with an error status of 'Bus Expiry'.
365                                  *
366                                  * Instead of always returning -EINVAL, let's
367                                  * give a hint to the gadget driver that this is
368                                  * the case by returning -EAGAIN.
369                                  */
370                                 ret = -EAGAIN;
371                                 break;
372                         default:
373                                 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
374                         }
375
376                         break;
377                 }
378         } while (--timeout);
379
380         if (timeout == 0) {
381                 ret = -ETIMEDOUT;
382                 cmd_status = -ETIMEDOUT;
383         }
384
385         trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
386
387         if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
388                 dep->flags |= DWC3_EP_TRANSFER_STARTED;
389                 dwc3_gadget_ep_get_transfer_index(dep);
390         }
391
392         if (saved_config) {
393                 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
394                 reg |= saved_config;
395                 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
396         }
397
398         return ret;
399 }
400
401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
402 {
403         struct dwc3 *dwc = dep->dwc;
404         struct dwc3_gadget_ep_cmd_params params;
405         u32 cmd = DWC3_DEPCMD_CLEARSTALL;
406
407         /*
408          * As of core revision 2.60a the recommended programming model
409          * is to set the ClearPendIN bit when issuing a Clear Stall EP
410          * command for IN endpoints. This is to prevent an issue where
411          * some (non-compliant) hosts may not send ACK TPs for pending
412          * IN transfers due to a mishandled error condition. Synopsys
413          * STAR 9000614252.
414          */
415         if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416             (dwc->gadget.speed >= USB_SPEED_SUPER))
417                 cmd |= DWC3_DEPCMD_CLEARPENDIN;
418
419         memset(&params, 0, sizeof(params));
420
421         return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
422 }
423
424 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
425                 struct dwc3_trb *trb)
426 {
427         u32             offset = (char *) trb - (char *) dep->trb_pool;
428
429         return dep->trb_pool_dma + offset;
430 }
431
432 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
433 {
434         struct dwc3             *dwc = dep->dwc;
435
436         if (dep->trb_pool)
437                 return 0;
438
439         dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
440                         sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
441                         &dep->trb_pool_dma, GFP_KERNEL);
442         if (!dep->trb_pool) {
443                 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
444                                 dep->name);
445                 return -ENOMEM;
446         }
447
448         return 0;
449 }
450
451 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
452 {
453         struct dwc3             *dwc = dep->dwc;
454
455         dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
456                         dep->trb_pool, dep->trb_pool_dma);
457
458         dep->trb_pool = NULL;
459         dep->trb_pool_dma = 0;
460 }
461
462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
463 {
464         struct dwc3_gadget_ep_cmd_params params;
465
466         memset(&params, 0x00, sizeof(params));
467
468         params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
469
470         return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471                         &params);
472 }
473
474 /**
475  * dwc3_gadget_start_config - configure ep resources
476  * @dep: endpoint that is being enabled
477  *
478  * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479  * completion, it will set Transfer Resource for all available endpoints.
480  *
481  * The assignment of transfer resources cannot perfectly follow the data book
482  * due to the fact that the controller driver does not have all knowledge of the
483  * configuration in advance. It is given this information piecemeal by the
484  * composite gadget framework after every SET_CONFIGURATION and
485  * SET_INTERFACE. Trying to follow the databook programming model in this
486  * scenario can cause errors. For two reasons:
487  *
488  * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
489  * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
490  * incorrect in the scenario of multiple interfaces.
491  *
492  * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493  * endpoint on alt setting (8.1.6).
494  *
495  * The following simplified method is used instead:
496  *
497  * All hardware endpoints can be assigned a transfer resource and this setting
498  * will stay persistent until either a core reset or hibernation. So whenever we
499  * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
500  * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
501  * guaranteed that there are as many transfer resources as endpoints.
502  *
503  * This function is called for each endpoint when it is being enabled but is
504  * triggered only when called for EP0-out, which always happens first, and which
505  * should only happen in one of the above conditions.
506  */
507 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
508 {
509         struct dwc3_gadget_ep_cmd_params params;
510         struct dwc3             *dwc;
511         u32                     cmd;
512         int                     i;
513         int                     ret;
514
515         if (dep->number)
516                 return 0;
517
518         memset(&params, 0x00, sizeof(params));
519         cmd = DWC3_DEPCMD_DEPSTARTCFG;
520         dwc = dep->dwc;
521
522         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
523         if (ret)
524                 return ret;
525
526         for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527                 struct dwc3_ep *dep = dwc->eps[i];
528
529                 if (!dep)
530                         continue;
531
532                 ret = dwc3_gadget_set_xfer_resource(dep);
533                 if (ret)
534                         return ret;
535         }
536
537         return 0;
538 }
539
540 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
541 {
542         const struct usb_ss_ep_comp_descriptor *comp_desc;
543         const struct usb_endpoint_descriptor *desc;
544         struct dwc3_gadget_ep_cmd_params params;
545         struct dwc3 *dwc = dep->dwc;
546
547         comp_desc = dep->endpoint.comp_desc;
548         desc = dep->endpoint.desc;
549
550         memset(&params, 0x00, sizeof(params));
551
552         params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
553                 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
554
555         /* Burst size is only needed in SuperSpeed mode */
556         if (dwc->gadget.speed >= USB_SPEED_SUPER) {
557                 u32 burst = dep->endpoint.maxburst;
558                 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
559         }
560
561         params.param0 |= action;
562         if (action == DWC3_DEPCFG_ACTION_RESTORE)
563                 params.param2 |= dep->saved_state;
564
565         if (usb_endpoint_xfer_control(desc))
566                 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
567
568         if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569                 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
570
571         if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
572                 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
573                         | DWC3_DEPCFG_STREAM_EVENT_EN;
574                 dep->stream_capable = true;
575         }
576
577         if (!usb_endpoint_xfer_control(desc))
578                 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
579
580         /*
581          * We are doing 1:1 mapping for endpoints, meaning
582          * Physical Endpoints 2 maps to Logical Endpoint 2 and
583          * so on. We consider the direction bit as part of the physical
584          * endpoint number. So USB endpoint 0x81 is 0x03.
585          */
586         params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
587
588         /*
589          * We must use the lower 16 TX FIFOs even though
590          * HW might have more
591          */
592         if (dep->direction)
593                 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
594
595         if (desc->bInterval) {
596                 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
597                 dep->interval = 1 << (desc->bInterval - 1);
598         }
599
600         return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
601 }
602
603 /**
604  * __dwc3_gadget_ep_enable - initializes a hw endpoint
605  * @dep: endpoint to be initialized
606  * @action: one of INIT, MODIFY or RESTORE
607  *
608  * Caller should take care of locking. Execute all necessary commands to
609  * initialize a HW endpoint so it can be used by a gadget driver.
610  */
611 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
612 {
613         const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
614         struct dwc3             *dwc = dep->dwc;
615
616         u32                     reg;
617         int                     ret;
618
619         if (!(dep->flags & DWC3_EP_ENABLED)) {
620                 ret = dwc3_gadget_start_config(dep);
621                 if (ret)
622                         return ret;
623         }
624
625         ret = dwc3_gadget_set_ep_config(dep, action);
626         if (ret)
627                 return ret;
628
629         if (!(dep->flags & DWC3_EP_ENABLED)) {
630                 struct dwc3_trb *trb_st_hw;
631                 struct dwc3_trb *trb_link;
632
633                 dep->type = usb_endpoint_type(desc);
634                 dep->flags |= DWC3_EP_ENABLED;
635
636                 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
637                 reg |= DWC3_DALEPENA_EP(dep->number);
638                 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
639
640                 if (usb_endpoint_xfer_control(desc))
641                         goto out;
642
643                 /* Initialize the TRB ring */
644                 dep->trb_dequeue = 0;
645                 dep->trb_enqueue = 0;
646                 memset(dep->trb_pool, 0,
647                        sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
648
649                 /* Link TRB. The HWO bit is never reset */
650                 trb_st_hw = &dep->trb_pool[0];
651
652                 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
653                 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654                 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655                 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656                 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
657         }
658
659         /*
660          * Issue StartTransfer here with no-op TRB so we can always rely on No
661          * Response Update Transfer command.
662          */
663         if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
664                         usb_endpoint_xfer_int(desc)) {
665                 struct dwc3_gadget_ep_cmd_params params;
666                 struct dwc3_trb *trb;
667                 dma_addr_t trb_dma;
668                 u32 cmd;
669
670                 memset(&params, 0, sizeof(params));
671                 trb = &dep->trb_pool[0];
672                 trb_dma = dwc3_trb_dma_offset(dep, trb);
673
674                 params.param0 = upper_32_bits(trb_dma);
675                 params.param1 = lower_32_bits(trb_dma);
676
677                 cmd = DWC3_DEPCMD_STARTTRANSFER;
678
679                 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
680                 if (ret < 0)
681                         return ret;
682         }
683
684 out:
685         trace_dwc3_gadget_ep_enable(dep);
686
687         return 0;
688 }
689
690 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
691 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
692 {
693         struct dwc3_request             *req;
694
695         dwc3_stop_active_transfer(dep, true);
696
697         /* - giveback all requests to gadget driver */
698         while (!list_empty(&dep->started_list)) {
699                 req = next_request(&dep->started_list);
700
701                 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
702         }
703
704         while (!list_empty(&dep->pending_list)) {
705                 req = next_request(&dep->pending_list);
706
707                 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
708         }
709 }
710
711 /**
712  * __dwc3_gadget_ep_disable - disables a hw endpoint
713  * @dep: the endpoint to disable
714  *
715  * This function undoes what __dwc3_gadget_ep_enable did and also removes
716  * requests which are currently being processed by the hardware and those which
717  * are not yet scheduled.
718  *
719  * Caller should take care of locking.
720  */
721 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
722 {
723         struct dwc3             *dwc = dep->dwc;
724         u32                     reg;
725
726         trace_dwc3_gadget_ep_disable(dep);
727
728         dwc3_remove_requests(dwc, dep);
729
730         /* make sure HW endpoint isn't stalled */
731         if (dep->flags & DWC3_EP_STALL)
732                 __dwc3_gadget_ep_set_halt(dep, 0, false);
733
734         reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
735         reg &= ~DWC3_DALEPENA_EP(dep->number);
736         dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
737
738         dep->stream_capable = false;
739         dep->type = 0;
740         dep->flags = 0;
741
742         /* Clear out the ep descriptors for non-ep0 */
743         if (dep->number > 1) {
744                 dep->endpoint.comp_desc = NULL;
745                 dep->endpoint.desc = NULL;
746         }
747
748         return 0;
749 }
750
751 /* -------------------------------------------------------------------------- */
752
753 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
754                 const struct usb_endpoint_descriptor *desc)
755 {
756         return -EINVAL;
757 }
758
759 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
760 {
761         return -EINVAL;
762 }
763
764 /* -------------------------------------------------------------------------- */
765
766 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
767                 const struct usb_endpoint_descriptor *desc)
768 {
769         struct dwc3_ep                  *dep;
770         struct dwc3                     *dwc;
771         unsigned long                   flags;
772         int                             ret;
773
774         if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
775                 pr_debug("dwc3: invalid parameters\n");
776                 return -EINVAL;
777         }
778
779         if (!desc->wMaxPacketSize) {
780                 pr_debug("dwc3: missing wMaxPacketSize\n");
781                 return -EINVAL;
782         }
783
784         dep = to_dwc3_ep(ep);
785         dwc = dep->dwc;
786
787         if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
788                                         "%s is already enabled\n",
789                                         dep->name))
790                 return 0;
791
792         spin_lock_irqsave(&dwc->lock, flags);
793         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
794         spin_unlock_irqrestore(&dwc->lock, flags);
795
796         return ret;
797 }
798
799 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
800 {
801         struct dwc3_ep                  *dep;
802         struct dwc3                     *dwc;
803         unsigned long                   flags;
804         int                             ret;
805
806         if (!ep) {
807                 pr_debug("dwc3: invalid parameters\n");
808                 return -EINVAL;
809         }
810
811         dep = to_dwc3_ep(ep);
812         dwc = dep->dwc;
813
814         if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
815                                         "%s is already disabled\n",
816                                         dep->name))
817                 return 0;
818
819         spin_lock_irqsave(&dwc->lock, flags);
820         ret = __dwc3_gadget_ep_disable(dep);
821         spin_unlock_irqrestore(&dwc->lock, flags);
822
823         return ret;
824 }
825
826 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
827                 gfp_t gfp_flags)
828 {
829         struct dwc3_request             *req;
830         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
831
832         req = kzalloc(sizeof(*req), gfp_flags);
833         if (!req)
834                 return NULL;
835
836         req->direction  = dep->direction;
837         req->epnum      = dep->number;
838         req->dep        = dep;
839         req->status     = DWC3_REQUEST_STATUS_UNKNOWN;
840
841         trace_dwc3_alloc_request(req);
842
843         return &req->request;
844 }
845
846 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
847                 struct usb_request *request)
848 {
849         struct dwc3_request             *req = to_dwc3_request(request);
850
851         trace_dwc3_free_request(req);
852         kfree(req);
853 }
854
855 /**
856  * dwc3_ep_prev_trb - returns the previous TRB in the ring
857  * @dep: The endpoint with the TRB ring
858  * @index: The index of the current TRB in the ring
859  *
860  * Returns the TRB prior to the one pointed to by the index. If the
861  * index is 0, we will wrap backwards, skip the link TRB, and return
862  * the one just before that.
863  */
864 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
865 {
866         u8 tmp = index;
867
868         if (!tmp)
869                 tmp = DWC3_TRB_NUM - 1;
870
871         return &dep->trb_pool[tmp - 1];
872 }
873
874 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
875 {
876         struct dwc3_trb         *tmp;
877         u8                      trbs_left;
878
879         /*
880          * If enqueue & dequeue are equal than it is either full or empty.
881          *
882          * One way to know for sure is if the TRB right before us has HWO bit
883          * set or not. If it has, then we're definitely full and can't fit any
884          * more transfers in our ring.
885          */
886         if (dep->trb_enqueue == dep->trb_dequeue) {
887                 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
888                 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
889                         return 0;
890
891                 return DWC3_TRB_NUM - 1;
892         }
893
894         trbs_left = dep->trb_dequeue - dep->trb_enqueue;
895         trbs_left &= (DWC3_TRB_NUM - 1);
896
897         if (dep->trb_dequeue < dep->trb_enqueue)
898                 trbs_left--;
899
900         return trbs_left;
901 }
902
903 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
904                 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
905                 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
906 {
907         struct dwc3             *dwc = dep->dwc;
908         struct usb_gadget       *gadget = &dwc->gadget;
909         enum usb_device_speed   speed = gadget->speed;
910
911         trb->size = DWC3_TRB_SIZE_LENGTH(length);
912         trb->bpl = lower_32_bits(dma);
913         trb->bph = upper_32_bits(dma);
914
915         switch (usb_endpoint_type(dep->endpoint.desc)) {
916         case USB_ENDPOINT_XFER_CONTROL:
917                 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
918                 break;
919
920         case USB_ENDPOINT_XFER_ISOC:
921                 if (!node) {
922                         trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
923
924                         /*
925                          * USB Specification 2.0 Section 5.9.2 states that: "If
926                          * there is only a single transaction in the microframe,
927                          * only a DATA0 data packet PID is used.  If there are
928                          * two transactions per microframe, DATA1 is used for
929                          * the first transaction data packet and DATA0 is used
930                          * for the second transaction data packet.  If there are
931                          * three transactions per microframe, DATA2 is used for
932                          * the first transaction data packet, DATA1 is used for
933                          * the second, and DATA0 is used for the third."
934                          *
935                          * IOW, we should satisfy the following cases:
936                          *
937                          * 1) length <= maxpacket
938                          *      - DATA0
939                          *
940                          * 2) maxpacket < length <= (2 * maxpacket)
941                          *      - DATA1, DATA0
942                          *
943                          * 3) (2 * maxpacket) < length <= (3 * maxpacket)
944                          *      - DATA2, DATA1, DATA0
945                          */
946                         if (speed == USB_SPEED_HIGH) {
947                                 struct usb_ep *ep = &dep->endpoint;
948                                 unsigned int mult = 2;
949                                 unsigned int maxp = usb_endpoint_maxp(ep->desc);
950
951                                 if (length <= (2 * maxp))
952                                         mult--;
953
954                                 if (length <= maxp)
955                                         mult--;
956
957                                 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
958                         }
959                 } else {
960                         trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
961                 }
962
963                 /* always enable Interrupt on Missed ISOC */
964                 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
965                 break;
966
967         case USB_ENDPOINT_XFER_BULK:
968         case USB_ENDPOINT_XFER_INT:
969                 trb->ctrl = DWC3_TRBCTL_NORMAL;
970                 break;
971         default:
972                 /*
973                  * This is only possible with faulty memory because we
974                  * checked it already :)
975                  */
976                 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
977                                 usb_endpoint_type(dep->endpoint.desc));
978         }
979
980         /*
981          * Enable Continue on Short Packet
982          * when endpoint is not a stream capable
983          */
984         if (usb_endpoint_dir_out(dep->endpoint.desc)) {
985                 if (!dep->stream_capable)
986                         trb->ctrl |= DWC3_TRB_CTRL_CSP;
987
988                 if (short_not_ok)
989                         trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
990         }
991
992         if ((!no_interrupt && !chain) ||
993                         (dwc3_calc_trbs_left(dep) == 1))
994                 trb->ctrl |= DWC3_TRB_CTRL_IOC;
995
996         if (chain)
997                 trb->ctrl |= DWC3_TRB_CTRL_CHN;
998
999         if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1000                 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1001
1002         trb->ctrl |= DWC3_TRB_CTRL_HWO;
1003
1004         dwc3_ep_inc_enq(dep);
1005
1006         trace_dwc3_prepare_trb(dep, trb);
1007 }
1008
1009 /**
1010  * dwc3_prepare_one_trb - setup one TRB from one request
1011  * @dep: endpoint for which this request is prepared
1012  * @req: dwc3_request pointer
1013  * @chain: should this TRB be chained to the next?
1014  * @node: only for isochronous endpoints. First TRB needs different type.
1015  */
1016 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1017                 struct dwc3_request *req, unsigned chain, unsigned node)
1018 {
1019         struct dwc3_trb         *trb;
1020         unsigned int            length;
1021         dma_addr_t              dma;
1022         unsigned                stream_id = req->request.stream_id;
1023         unsigned                short_not_ok = req->request.short_not_ok;
1024         unsigned                no_interrupt = req->request.no_interrupt;
1025
1026         if (req->request.num_sgs > 0) {
1027                 length = sg_dma_len(req->start_sg);
1028                 dma = sg_dma_address(req->start_sg);
1029         } else {
1030                 length = req->request.length;
1031                 dma = req->request.dma;
1032         }
1033
1034         trb = &dep->trb_pool[dep->trb_enqueue];
1035
1036         if (!req->trb) {
1037                 dwc3_gadget_move_started_request(req);
1038                 req->trb = trb;
1039                 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1040         }
1041
1042         req->num_trbs++;
1043
1044         __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1045                         stream_id, short_not_ok, no_interrupt);
1046 }
1047
1048 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1049                 struct dwc3_request *req)
1050 {
1051         struct scatterlist *sg = req->start_sg;
1052         struct scatterlist *s;
1053         int             i;
1054
1055         unsigned int remaining = req->request.num_mapped_sgs
1056                 - req->num_queued_sgs;
1057
1058         for_each_sg(sg, s, remaining, i) {
1059                 unsigned int length = req->request.length;
1060                 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1061                 unsigned int rem = length % maxp;
1062                 unsigned chain = true;
1063
1064                 if (sg_is_last(s))
1065                         chain = false;
1066
1067                 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1068                         struct dwc3     *dwc = dep->dwc;
1069                         struct dwc3_trb *trb;
1070
1071                         req->needs_extra_trb = true;
1072
1073                         /* prepare normal TRB */
1074                         dwc3_prepare_one_trb(dep, req, true, i);
1075
1076                         /* Now prepare one extra TRB to align transfer size */
1077                         trb = &dep->trb_pool[dep->trb_enqueue];
1078                         req->num_trbs++;
1079                         __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1080                                         maxp - rem, false, 1,
1081                                         req->request.stream_id,
1082                                         req->request.short_not_ok,
1083                                         req->request.no_interrupt);
1084                 } else {
1085                         dwc3_prepare_one_trb(dep, req, chain, i);
1086                 }
1087
1088                 /*
1089                  * There can be a situation where all sgs in sglist are not
1090                  * queued because of insufficient trb number. To handle this
1091                  * case, update start_sg to next sg to be queued, so that
1092                  * we have free trbs we can continue queuing from where we
1093                  * previously stopped
1094                  */
1095                 if (chain)
1096                         req->start_sg = sg_next(s);
1097
1098                 req->num_queued_sgs++;
1099
1100                 if (!dwc3_calc_trbs_left(dep))
1101                         break;
1102         }
1103 }
1104
1105 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1106                 struct dwc3_request *req)
1107 {
1108         unsigned int length = req->request.length;
1109         unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1110         unsigned int rem = length % maxp;
1111
1112         if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1113                 struct dwc3     *dwc = dep->dwc;
1114                 struct dwc3_trb *trb;
1115
1116                 req->needs_extra_trb = true;
1117
1118                 /* prepare normal TRB */
1119                 dwc3_prepare_one_trb(dep, req, true, 0);
1120
1121                 /* Now prepare one extra TRB to align transfer size */
1122                 trb = &dep->trb_pool[dep->trb_enqueue];
1123                 req->num_trbs++;
1124                 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1125                                 false, 1, req->request.stream_id,
1126                                 req->request.short_not_ok,
1127                                 req->request.no_interrupt);
1128         } else if (req->request.zero && req->request.length &&
1129                    (IS_ALIGNED(req->request.length, maxp))) {
1130                 struct dwc3     *dwc = dep->dwc;
1131                 struct dwc3_trb *trb;
1132
1133                 req->needs_extra_trb = true;
1134
1135                 /* prepare normal TRB */
1136                 dwc3_prepare_one_trb(dep, req, true, 0);
1137
1138                 /* Now prepare one extra TRB to handle ZLP */
1139                 trb = &dep->trb_pool[dep->trb_enqueue];
1140                 req->num_trbs++;
1141                 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1142                                 false, 1, req->request.stream_id,
1143                                 req->request.short_not_ok,
1144                                 req->request.no_interrupt);
1145         } else {
1146                 dwc3_prepare_one_trb(dep, req, false, 0);
1147         }
1148 }
1149
1150 /*
1151  * dwc3_prepare_trbs - setup TRBs from requests
1152  * @dep: endpoint for which requests are being prepared
1153  *
1154  * The function goes through the requests list and sets up TRBs for the
1155  * transfers. The function returns once there are no more TRBs available or
1156  * it runs out of requests.
1157  */
1158 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1159 {
1160         struct dwc3_request     *req, *n;
1161
1162         BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1163
1164         /*
1165          * We can get in a situation where there's a request in the started list
1166          * but there weren't enough TRBs to fully kick it in the first time
1167          * around, so it has been waiting for more TRBs to be freed up.
1168          *
1169          * In that case, we should check if we have a request with pending_sgs
1170          * in the started list and prepare TRBs for that request first,
1171          * otherwise we will prepare TRBs completely out of order and that will
1172          * break things.
1173          */
1174         list_for_each_entry(req, &dep->started_list, list) {
1175                 if (req->num_pending_sgs > 0)
1176                         dwc3_prepare_one_trb_sg(dep, req);
1177
1178                 if (!dwc3_calc_trbs_left(dep))
1179                         return;
1180         }
1181
1182         list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1183                 struct dwc3     *dwc = dep->dwc;
1184                 int             ret;
1185
1186                 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1187                                                     dep->direction);
1188                 if (ret)
1189                         return;
1190
1191                 req->sg                 = req->request.sg;
1192                 req->start_sg           = req->sg;
1193                 req->num_queued_sgs     = 0;
1194                 req->num_pending_sgs    = req->request.num_mapped_sgs;
1195
1196                 if (req->num_pending_sgs > 0)
1197                         dwc3_prepare_one_trb_sg(dep, req);
1198                 else
1199                         dwc3_prepare_one_trb_linear(dep, req);
1200
1201                 if (!dwc3_calc_trbs_left(dep))
1202                         return;
1203         }
1204 }
1205
1206 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1207 {
1208         struct dwc3_gadget_ep_cmd_params params;
1209         struct dwc3_request             *req;
1210         int                             starting;
1211         int                             ret;
1212         u32                             cmd;
1213
1214         if (!dwc3_calc_trbs_left(dep))
1215                 return 0;
1216
1217         starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1218
1219         dwc3_prepare_trbs(dep);
1220         req = next_request(&dep->started_list);
1221         if (!req) {
1222                 dep->flags |= DWC3_EP_PENDING_REQUEST;
1223                 return 0;
1224         }
1225
1226         memset(&params, 0, sizeof(params));
1227
1228         if (starting) {
1229                 params.param0 = upper_32_bits(req->trb_dma);
1230                 params.param1 = lower_32_bits(req->trb_dma);
1231                 cmd = DWC3_DEPCMD_STARTTRANSFER;
1232
1233                 if (dep->stream_capable)
1234                         cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1235
1236                 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1237                         cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1238         } else {
1239                 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1240                         DWC3_DEPCMD_PARAM(dep->resource_index);
1241         }
1242
1243         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1244         if (ret < 0) {
1245                 /*
1246                  * FIXME we need to iterate over the list of requests
1247                  * here and stop, unmap, free and del each of the linked
1248                  * requests instead of what we do now.
1249                  */
1250                 if (req->trb)
1251                         memset(req->trb, 0, sizeof(struct dwc3_trb));
1252                 dwc3_gadget_del_and_unmap_request(dep, req, ret);
1253                 return ret;
1254         }
1255
1256         return 0;
1257 }
1258
1259 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1260 {
1261         u32                     reg;
1262
1263         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1264         return DWC3_DSTS_SOFFN(reg);
1265 }
1266
1267 /**
1268  * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1269  * @dep: isoc endpoint
1270  *
1271  * This function tests for the correct combination of BIT[15:14] from the 16-bit
1272  * microframe number reported by the XferNotReady event for the future frame
1273  * number to start the isoc transfer.
1274  *
1275  * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1276  * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1277  * XferNotReady event are invalid. The driver uses this number to schedule the
1278  * isochronous transfer and passes it to the START TRANSFER command. Because
1279  * this number is invalid, the command may fail. If BIT[15:14] matches the
1280  * internal 16-bit microframe, the START TRANSFER command will pass and the
1281  * transfer will start at the scheduled time, if it is off by 1, the command
1282  * will still pass, but the transfer will start 2 seconds in the future. For all
1283  * other conditions, the START TRANSFER command will fail with bus-expiry.
1284  *
1285  * In order to workaround this issue, we can test for the correct combination of
1286  * BIT[15:14] by sending START TRANSFER commands with different values of
1287  * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1288  * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1289  * As the result, within the 4 possible combinations for BIT[15:14], there will
1290  * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1291  * command status will result in a 2-second delay start. The smaller BIT[15:14]
1292  * value is the correct combination.
1293  *
1294  * Since there are only 4 outcomes and the results are ordered, we can simply
1295  * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1296  * deduce the smaller successful combination.
1297  *
1298  * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1299  * of BIT[15:14]. The correct combination is as follow:
1300  *
1301  * if test0 fails and test1 passes, BIT[15:14] is 'b01
1302  * if test0 fails and test1 fails, BIT[15:14] is 'b10
1303  * if test0 passes and test1 fails, BIT[15:14] is 'b11
1304  * if test0 passes and test1 passes, BIT[15:14] is 'b00
1305  *
1306  * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1307  * endpoints.
1308  */
1309 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1310 {
1311         int cmd_status = 0;
1312         bool test0;
1313         bool test1;
1314
1315         while (dep->combo_num < 2) {
1316                 struct dwc3_gadget_ep_cmd_params params;
1317                 u32 test_frame_number;
1318                 u32 cmd;
1319
1320                 /*
1321                  * Check if we can start isoc transfer on the next interval or
1322                  * 4 uframes in the future with BIT[15:14] as dep->combo_num
1323                  */
1324                 test_frame_number = dep->frame_number & 0x3fff;
1325                 test_frame_number |= dep->combo_num << 14;
1326                 test_frame_number += max_t(u32, 4, dep->interval);
1327
1328                 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1329                 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1330
1331                 cmd = DWC3_DEPCMD_STARTTRANSFER;
1332                 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1333                 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1334
1335                 /* Redo if some other failure beside bus-expiry is received */
1336                 if (cmd_status && cmd_status != -EAGAIN) {
1337                         dep->start_cmd_status = 0;
1338                         dep->combo_num = 0;
1339                         return 0;
1340                 }
1341
1342                 /* Store the first test status */
1343                 if (dep->combo_num == 0)
1344                         dep->start_cmd_status = cmd_status;
1345
1346                 dep->combo_num++;
1347
1348                 /*
1349                  * End the transfer if the START_TRANSFER command is successful
1350                  * to wait for the next XferNotReady to test the command again
1351                  */
1352                 if (cmd_status == 0) {
1353                         dwc3_stop_active_transfer(dep, true);
1354                         return 0;
1355                 }
1356         }
1357
1358         /* test0 and test1 are both completed at this point */
1359         test0 = (dep->start_cmd_status == 0);
1360         test1 = (cmd_status == 0);
1361
1362         if (!test0 && test1)
1363                 dep->combo_num = 1;
1364         else if (!test0 && !test1)
1365                 dep->combo_num = 2;
1366         else if (test0 && !test1)
1367                 dep->combo_num = 3;
1368         else if (test0 && test1)
1369                 dep->combo_num = 0;
1370
1371         dep->frame_number &= 0x3fff;
1372         dep->frame_number |= dep->combo_num << 14;
1373         dep->frame_number += max_t(u32, 4, dep->interval);
1374
1375         /* Reinitialize test variables */
1376         dep->start_cmd_status = 0;
1377         dep->combo_num = 0;
1378
1379         return __dwc3_gadget_kick_transfer(dep);
1380 }
1381
1382 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1383 {
1384         struct dwc3 *dwc = dep->dwc;
1385         int ret;
1386         int i;
1387
1388         if (list_empty(&dep->pending_list)) {
1389                 dep->flags |= DWC3_EP_PENDING_REQUEST;
1390                 return -EAGAIN;
1391         }
1392
1393         if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1394             (dwc->revision <= DWC3_USB31_REVISION_160A ||
1395              (dwc->revision == DWC3_USB31_REVISION_170A &&
1396               dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1397               dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1398
1399                 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1400                         return dwc3_gadget_start_isoc_quirk(dep);
1401         }
1402
1403         for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1404                 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1405
1406                 ret = __dwc3_gadget_kick_transfer(dep);
1407                 if (ret != -EAGAIN)
1408                         break;
1409         }
1410
1411         return ret;
1412 }
1413
1414 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1415 {
1416         struct dwc3             *dwc = dep->dwc;
1417
1418         if (!dep->endpoint.desc) {
1419                 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1420                                 dep->name);
1421                 return -ESHUTDOWN;
1422         }
1423
1424         if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1425                                 &req->request, req->dep->name))
1426                 return -EINVAL;
1427
1428         if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1429                                 "%s: request %pK already in flight\n",
1430                                 dep->name, &req->request))
1431                 return -EINVAL;
1432
1433         pm_runtime_get(dwc->dev);
1434
1435         req->request.actual     = 0;
1436         req->request.status     = -EINPROGRESS;
1437
1438         trace_dwc3_ep_queue(req);
1439
1440         list_add_tail(&req->list, &dep->pending_list);
1441         req->status = DWC3_REQUEST_STATUS_QUEUED;
1442
1443         /*
1444          * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1445          * wait for a XferNotReady event so we will know what's the current
1446          * (micro-)frame number.
1447          *
1448          * Without this trick, we are very, very likely gonna get Bus Expiry
1449          * errors which will force us issue EndTransfer command.
1450          */
1451         if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1452                 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1453                                 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1454                         return 0;
1455
1456                 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1457                         if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1458                                 return __dwc3_gadget_start_isoc(dep);
1459                         }
1460                 }
1461         }
1462
1463         return __dwc3_gadget_kick_transfer(dep);
1464 }
1465
1466 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1467         gfp_t gfp_flags)
1468 {
1469         struct dwc3_request             *req = to_dwc3_request(request);
1470         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1471         struct dwc3                     *dwc = dep->dwc;
1472
1473         unsigned long                   flags;
1474
1475         int                             ret;
1476
1477         spin_lock_irqsave(&dwc->lock, flags);
1478         ret = __dwc3_gadget_ep_queue(dep, req);
1479         spin_unlock_irqrestore(&dwc->lock, flags);
1480
1481         return ret;
1482 }
1483
1484 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1485 {
1486         int i;
1487
1488         /*
1489          * If request was already started, this means we had to
1490          * stop the transfer. With that we also need to ignore
1491          * all TRBs used by the request, however TRBs can only
1492          * be modified after completion of END_TRANSFER
1493          * command. So what we do here is that we wait for
1494          * END_TRANSFER completion and only after that, we jump
1495          * over TRBs by clearing HWO and incrementing dequeue
1496          * pointer.
1497          */
1498         for (i = 0; i < req->num_trbs; i++) {
1499                 struct dwc3_trb *trb;
1500
1501                 trb = req->trb + i;
1502                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1503                 dwc3_ep_inc_deq(dep);
1504         }
1505 }
1506
1507 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1508 {
1509         struct dwc3_request             *req;
1510         struct dwc3_request             *tmp;
1511
1512         list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1513                 dwc3_gadget_ep_skip_trbs(dep, req);
1514                 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1515         }
1516 }
1517
1518 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1519                 struct usb_request *request)
1520 {
1521         struct dwc3_request             *req = to_dwc3_request(request);
1522         struct dwc3_request             *r = NULL;
1523
1524         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1525         struct dwc3                     *dwc = dep->dwc;
1526
1527         unsigned long                   flags;
1528         int                             ret = 0;
1529
1530         trace_dwc3_ep_dequeue(req);
1531
1532         spin_lock_irqsave(&dwc->lock, flags);
1533
1534         list_for_each_entry(r, &dep->pending_list, list) {
1535                 if (r == req)
1536                         break;
1537         }
1538
1539         if (r != req) {
1540                 list_for_each_entry(r, &dep->started_list, list) {
1541                         if (r == req)
1542                                 break;
1543                 }
1544                 if (r == req) {
1545                         /* wait until it is processed */
1546                         dwc3_stop_active_transfer(dep, true);
1547
1548                         if (!r->trb)
1549                                 goto out0;
1550
1551                         dwc3_gadget_move_cancelled_request(req);
1552                         if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1553                                 goto out0;
1554                         else
1555                                 goto out1;
1556                 }
1557                 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1558                                 request, ep->name);
1559                 ret = -EINVAL;
1560                 goto out0;
1561         }
1562
1563 out1:
1564         dwc3_gadget_giveback(dep, req, -ECONNRESET);
1565
1566 out0:
1567         spin_unlock_irqrestore(&dwc->lock, flags);
1568
1569         return ret;
1570 }
1571
1572 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1573 {
1574         struct dwc3_gadget_ep_cmd_params        params;
1575         struct dwc3                             *dwc = dep->dwc;
1576         int                                     ret;
1577
1578         if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1579                 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1580                 return -EINVAL;
1581         }
1582
1583         memset(&params, 0x00, sizeof(params));
1584
1585         if (value) {
1586                 struct dwc3_trb *trb;
1587
1588                 unsigned transfer_in_flight;
1589                 unsigned started;
1590
1591                 if (dep->number > 1)
1592                         trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1593                 else
1594                         trb = &dwc->ep0_trb[dep->trb_enqueue];
1595
1596                 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1597                 started = !list_empty(&dep->started_list);
1598
1599                 if (!protocol && ((dep->direction && transfer_in_flight) ||
1600                                 (!dep->direction && started))) {
1601                         return -EAGAIN;
1602                 }
1603
1604                 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1605                                 &params);
1606                 if (ret)
1607                         dev_err(dwc->dev, "failed to set STALL on %s\n",
1608                                         dep->name);
1609                 else
1610                         dep->flags |= DWC3_EP_STALL;
1611         } else {
1612
1613                 ret = dwc3_send_clear_stall_ep_cmd(dep);
1614                 if (ret)
1615                         dev_err(dwc->dev, "failed to clear STALL on %s\n",
1616                                         dep->name);
1617                 else
1618                         dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1619         }
1620
1621         return ret;
1622 }
1623
1624 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1625 {
1626         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1627         struct dwc3                     *dwc = dep->dwc;
1628
1629         unsigned long                   flags;
1630
1631         int                             ret;
1632
1633         spin_lock_irqsave(&dwc->lock, flags);
1634         ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1635         spin_unlock_irqrestore(&dwc->lock, flags);
1636
1637         return ret;
1638 }
1639
1640 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1641 {
1642         struct dwc3_ep                  *dep = to_dwc3_ep(ep);
1643         struct dwc3                     *dwc = dep->dwc;
1644         unsigned long                   flags;
1645         int                             ret;
1646
1647         spin_lock_irqsave(&dwc->lock, flags);
1648         dep->flags |= DWC3_EP_WEDGE;
1649
1650         if (dep->number == 0 || dep->number == 1)
1651                 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1652         else
1653                 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1654         spin_unlock_irqrestore(&dwc->lock, flags);
1655
1656         return ret;
1657 }
1658
1659 /* -------------------------------------------------------------------------- */
1660
1661 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1662         .bLength        = USB_DT_ENDPOINT_SIZE,
1663         .bDescriptorType = USB_DT_ENDPOINT,
1664         .bmAttributes   = USB_ENDPOINT_XFER_CONTROL,
1665 };
1666
1667 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1668         .enable         = dwc3_gadget_ep0_enable,
1669         .disable        = dwc3_gadget_ep0_disable,
1670         .alloc_request  = dwc3_gadget_ep_alloc_request,
1671         .free_request   = dwc3_gadget_ep_free_request,
1672         .queue          = dwc3_gadget_ep0_queue,
1673         .dequeue        = dwc3_gadget_ep_dequeue,
1674         .set_halt       = dwc3_gadget_ep0_set_halt,
1675         .set_wedge      = dwc3_gadget_ep_set_wedge,
1676 };
1677
1678 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1679         .enable         = dwc3_gadget_ep_enable,
1680         .disable        = dwc3_gadget_ep_disable,
1681         .alloc_request  = dwc3_gadget_ep_alloc_request,
1682         .free_request   = dwc3_gadget_ep_free_request,
1683         .queue          = dwc3_gadget_ep_queue,
1684         .dequeue        = dwc3_gadget_ep_dequeue,
1685         .set_halt       = dwc3_gadget_ep_set_halt,
1686         .set_wedge      = dwc3_gadget_ep_set_wedge,
1687 };
1688
1689 /* -------------------------------------------------------------------------- */
1690
1691 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1692 {
1693         struct dwc3             *dwc = gadget_to_dwc(g);
1694
1695         return __dwc3_gadget_get_frame(dwc);
1696 }
1697
1698 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1699 {
1700         int                     retries;
1701
1702         int                     ret;
1703         u32                     reg;
1704
1705         u8                      link_state;
1706         u8                      speed;
1707
1708         /*
1709          * According to the Databook Remote wakeup request should
1710          * be issued only when the device is in early suspend state.
1711          *
1712          * We can check that via USB Link State bits in DSTS register.
1713          */
1714         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1715
1716         speed = reg & DWC3_DSTS_CONNECTSPD;
1717         if ((speed == DWC3_DSTS_SUPERSPEED) ||
1718             (speed == DWC3_DSTS_SUPERSPEED_PLUS))
1719                 return 0;
1720
1721         link_state = DWC3_DSTS_USBLNKST(reg);
1722
1723         switch (link_state) {
1724         case DWC3_LINK_STATE_RX_DET:    /* in HS, means Early Suspend */
1725         case DWC3_LINK_STATE_U3:        /* in HS, means SUSPEND */
1726                 break;
1727         default:
1728                 return -EINVAL;
1729         }
1730
1731         ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1732         if (ret < 0) {
1733                 dev_err(dwc->dev, "failed to put link in Recovery\n");
1734                 return ret;
1735         }
1736
1737         /* Recent versions do this automatically */
1738         if (dwc->revision < DWC3_REVISION_194A) {
1739                 /* write zeroes to Link Change Request */
1740                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1741                 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1742                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1743         }
1744
1745         /* poll until Link State changes to ON */
1746         retries = 20000;
1747
1748         while (retries--) {
1749                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1750
1751                 /* in HS, means ON */
1752                 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1753                         break;
1754         }
1755
1756         if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1757                 dev_err(dwc->dev, "failed to send remote wakeup\n");
1758                 return -EINVAL;
1759         }
1760
1761         return 0;
1762 }
1763
1764 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1765 {
1766         struct dwc3             *dwc = gadget_to_dwc(g);
1767         unsigned long           flags;
1768         int                     ret;
1769
1770         spin_lock_irqsave(&dwc->lock, flags);
1771         ret = __dwc3_gadget_wakeup(dwc);
1772         spin_unlock_irqrestore(&dwc->lock, flags);
1773
1774         return ret;
1775 }
1776
1777 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1778                 int is_selfpowered)
1779 {
1780         struct dwc3             *dwc = gadget_to_dwc(g);
1781         unsigned long           flags;
1782
1783         spin_lock_irqsave(&dwc->lock, flags);
1784         g->is_selfpowered = !!is_selfpowered;
1785         spin_unlock_irqrestore(&dwc->lock, flags);
1786
1787         return 0;
1788 }
1789
1790 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1791 {
1792         u32                     reg;
1793         u32                     timeout = 500;
1794
1795         if (pm_runtime_suspended(dwc->dev))
1796                 return 0;
1797
1798         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1799         if (is_on) {
1800                 if (dwc->revision <= DWC3_REVISION_187A) {
1801                         reg &= ~DWC3_DCTL_TRGTULST_MASK;
1802                         reg |= DWC3_DCTL_TRGTULST_RX_DET;
1803                 }
1804
1805                 if (dwc->revision >= DWC3_REVISION_194A)
1806                         reg &= ~DWC3_DCTL_KEEP_CONNECT;
1807                 reg |= DWC3_DCTL_RUN_STOP;
1808
1809                 if (dwc->has_hibernation)
1810                         reg |= DWC3_DCTL_KEEP_CONNECT;
1811
1812                 dwc->pullups_connected = true;
1813         } else {
1814                 reg &= ~DWC3_DCTL_RUN_STOP;
1815
1816                 if (dwc->has_hibernation && !suspend)
1817                         reg &= ~DWC3_DCTL_KEEP_CONNECT;
1818
1819                 dwc->pullups_connected = false;
1820         }
1821
1822         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1823
1824         do {
1825                 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1826                 reg &= DWC3_DSTS_DEVCTRLHLT;
1827         } while (--timeout && !(!is_on ^ !reg));
1828
1829         if (!timeout)
1830                 return -ETIMEDOUT;
1831
1832         return 0;
1833 }
1834
1835 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1836 {
1837         struct dwc3             *dwc = gadget_to_dwc(g);
1838         unsigned long           flags;
1839         int                     ret;
1840
1841         is_on = !!is_on;
1842
1843         /*
1844          * Per databook, when we want to stop the gadget, if a control transfer
1845          * is still in process, complete it and get the core into setup phase.
1846          */
1847         if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1848                 reinit_completion(&dwc->ep0_in_setup);
1849
1850                 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1851                                 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1852                 if (ret == 0) {
1853                         dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1854                         return -ETIMEDOUT;
1855                 }
1856         }
1857
1858         spin_lock_irqsave(&dwc->lock, flags);
1859         ret = dwc3_gadget_run_stop(dwc, is_on, false);
1860         spin_unlock_irqrestore(&dwc->lock, flags);
1861
1862         return ret;
1863 }
1864
1865 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1866 {
1867         u32                     reg;
1868
1869         /* Enable all but Start and End of Frame IRQs */
1870         reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1871                         DWC3_DEVTEN_EVNTOVERFLOWEN |
1872                         DWC3_DEVTEN_CMDCMPLTEN |
1873                         DWC3_DEVTEN_ERRTICERREN |
1874                         DWC3_DEVTEN_WKUPEVTEN |
1875                         DWC3_DEVTEN_CONNECTDONEEN |
1876                         DWC3_DEVTEN_USBRSTEN |
1877                         DWC3_DEVTEN_DISCONNEVTEN);
1878
1879         if (dwc->revision < DWC3_REVISION_250A)
1880                 reg |= DWC3_DEVTEN_ULSTCNGEN;
1881
1882         dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1883 }
1884
1885 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1886 {
1887         /* mask all interrupts */
1888         dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1889 }
1890
1891 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1892 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1893
1894 /**
1895  * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1896  * @dwc: pointer to our context structure
1897  *
1898  * The following looks like complex but it's actually very simple. In order to
1899  * calculate the number of packets we can burst at once on OUT transfers, we're
1900  * gonna use RxFIFO size.
1901  *
1902  * To calculate RxFIFO size we need two numbers:
1903  * MDWIDTH = size, in bits, of the internal memory bus
1904  * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1905  *
1906  * Given these two numbers, the formula is simple:
1907  *
1908  * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1909  *
1910  * 24 bytes is for 3x SETUP packets
1911  * 16 bytes is a clock domain crossing tolerance
1912  *
1913  * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1914  */
1915 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1916 {
1917         u32 ram2_depth;
1918         u32 mdwidth;
1919         u32 nump;
1920         u32 reg;
1921
1922         ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1923         mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1924
1925         nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1926         nump = min_t(u32, nump, 16);
1927
1928         /* update NumP */
1929         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1930         reg &= ~DWC3_DCFG_NUMP_MASK;
1931         reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1932         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1933 }
1934
1935 static int __dwc3_gadget_start(struct dwc3 *dwc)
1936 {
1937         struct dwc3_ep          *dep;
1938         int                     ret = 0;
1939         u32                     reg;
1940
1941         /*
1942          * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1943          * the core supports IMOD, disable it.
1944          */
1945         if (dwc->imod_interval) {
1946                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1947                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1948         } else if (dwc3_has_imod(dwc)) {
1949                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1950         }
1951
1952         /*
1953          * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1954          * field instead of letting dwc3 itself calculate that automatically.
1955          *
1956          * This way, we maximize the chances that we'll be able to get several
1957          * bursts of data without going through any sort of endpoint throttling.
1958          */
1959         reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1960         if (dwc3_is_usb31(dwc))
1961                 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1962         else
1963                 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1964
1965         dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1966
1967         dwc3_gadget_setup_nump(dwc);
1968
1969         /* Start with SuperSpeed Default */
1970         dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1971
1972         dep = dwc->eps[0];
1973         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1974         if (ret) {
1975                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1976                 goto err0;
1977         }
1978
1979         dep = dwc->eps[1];
1980         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1981         if (ret) {
1982                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1983                 goto err1;
1984         }
1985
1986         /* begin to receive SETUP packets */
1987         dwc->ep0state = EP0_SETUP_PHASE;
1988         dwc->link_state = DWC3_LINK_STATE_SS_DIS;
1989         dwc3_ep0_out_start(dwc);
1990
1991         dwc3_gadget_enable_irq(dwc);
1992
1993         return 0;
1994
1995 err1:
1996         __dwc3_gadget_ep_disable(dwc->eps[0]);
1997
1998 err0:
1999         return ret;
2000 }
2001
2002 static int dwc3_gadget_start(struct usb_gadget *g,
2003                 struct usb_gadget_driver *driver)
2004 {
2005         struct dwc3             *dwc = gadget_to_dwc(g);
2006         unsigned long           flags;
2007         int                     ret = 0;
2008         int                     irq;
2009
2010         irq = dwc->irq_gadget;
2011         ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2012                         IRQF_SHARED, "dwc3", dwc->ev_buf);
2013         if (ret) {
2014                 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2015                                 irq, ret);
2016                 goto err0;
2017         }
2018
2019         spin_lock_irqsave(&dwc->lock, flags);
2020         if (dwc->gadget_driver) {
2021                 dev_err(dwc->dev, "%s is already bound to %s\n",
2022                                 dwc->gadget.name,
2023                                 dwc->gadget_driver->driver.name);
2024                 ret = -EBUSY;
2025                 goto err1;
2026         }
2027
2028         dwc->gadget_driver      = driver;
2029
2030         if (pm_runtime_active(dwc->dev))
2031                 __dwc3_gadget_start(dwc);
2032
2033         spin_unlock_irqrestore(&dwc->lock, flags);
2034
2035         return 0;
2036
2037 err1:
2038         spin_unlock_irqrestore(&dwc->lock, flags);
2039         free_irq(irq, dwc);
2040
2041 err0:
2042         return ret;
2043 }
2044
2045 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2046 {
2047         dwc3_gadget_disable_irq(dwc);
2048         __dwc3_gadget_ep_disable(dwc->eps[0]);
2049         __dwc3_gadget_ep_disable(dwc->eps[1]);
2050 }
2051
2052 static int dwc3_gadget_stop(struct usb_gadget *g)
2053 {
2054         struct dwc3             *dwc = gadget_to_dwc(g);
2055         unsigned long           flags;
2056
2057         spin_lock_irqsave(&dwc->lock, flags);
2058
2059         if (pm_runtime_suspended(dwc->dev))
2060                 goto out;
2061
2062         __dwc3_gadget_stop(dwc);
2063
2064 out:
2065         dwc->gadget_driver      = NULL;
2066         spin_unlock_irqrestore(&dwc->lock, flags);
2067
2068         free_irq(dwc->irq_gadget, dwc->ev_buf);
2069
2070         return 0;
2071 }
2072
2073 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2074                                   enum usb_device_speed speed)
2075 {
2076         struct dwc3             *dwc = gadget_to_dwc(g);
2077         unsigned long           flags;
2078         u32                     reg;
2079
2080         spin_lock_irqsave(&dwc->lock, flags);
2081         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2082         reg &= ~(DWC3_DCFG_SPEED_MASK);
2083
2084         /*
2085          * WORKAROUND: DWC3 revision < 2.20a have an issue
2086          * which would cause metastability state on Run/Stop
2087          * bit if we try to force the IP to USB2-only mode.
2088          *
2089          * Because of that, we cannot configure the IP to any
2090          * speed other than the SuperSpeed
2091          *
2092          * Refers to:
2093          *
2094          * STAR#9000525659: Clock Domain Crossing on DCTL in
2095          * USB 2.0 Mode
2096          */
2097         if (dwc->revision < DWC3_REVISION_220A &&
2098             !dwc->dis_metastability_quirk) {
2099                 reg |= DWC3_DCFG_SUPERSPEED;
2100         } else {
2101                 switch (speed) {
2102                 case USB_SPEED_LOW:
2103                         reg |= DWC3_DCFG_LOWSPEED;
2104                         break;
2105                 case USB_SPEED_FULL:
2106                         reg |= DWC3_DCFG_FULLSPEED;
2107                         break;
2108                 case USB_SPEED_HIGH:
2109                         reg |= DWC3_DCFG_HIGHSPEED;
2110                         break;
2111                 case USB_SPEED_SUPER:
2112                         reg |= DWC3_DCFG_SUPERSPEED;
2113                         break;
2114                 case USB_SPEED_SUPER_PLUS:
2115                         if (dwc3_is_usb31(dwc))
2116                                 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2117                         else
2118                                 reg |= DWC3_DCFG_SUPERSPEED;
2119                         break;
2120                 default:
2121                         dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2122
2123                         if (dwc->revision & DWC3_REVISION_IS_DWC31)
2124                                 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2125                         else
2126                                 reg |= DWC3_DCFG_SUPERSPEED;
2127                 }
2128         }
2129         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2130
2131         spin_unlock_irqrestore(&dwc->lock, flags);
2132 }
2133
2134 static const struct usb_gadget_ops dwc3_gadget_ops = {
2135         .get_frame              = dwc3_gadget_get_frame,
2136         .wakeup                 = dwc3_gadget_wakeup,
2137         .set_selfpowered        = dwc3_gadget_set_selfpowered,
2138         .pullup                 = dwc3_gadget_pullup,
2139         .udc_start              = dwc3_gadget_start,
2140         .udc_stop               = dwc3_gadget_stop,
2141         .udc_set_speed          = dwc3_gadget_set_speed,
2142 };
2143
2144 /* -------------------------------------------------------------------------- */
2145
2146 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2147 {
2148         struct dwc3 *dwc = dep->dwc;
2149
2150         usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2151         dep->endpoint.maxburst = 1;
2152         dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2153         if (!dep->direction)
2154                 dwc->gadget.ep0 = &dep->endpoint;
2155
2156         dep->endpoint.caps.type_control = true;
2157
2158         return 0;
2159 }
2160
2161 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2162 {
2163         struct dwc3 *dwc = dep->dwc;
2164         int mdwidth;
2165         int kbytes;
2166         int size;
2167
2168         mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2169         /* MDWIDTH is represented in bits, we need it in bytes */
2170         mdwidth /= 8;
2171
2172         size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2173         if (dwc3_is_usb31(dwc))
2174                 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2175         else
2176                 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2177
2178         /* FIFO Depth is in MDWDITH bytes. Multiply */
2179         size *= mdwidth;
2180
2181         kbytes = size / 1024;
2182         if (kbytes == 0)
2183                 kbytes = 1;
2184
2185         /*
2186          * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2187          * internal overhead. We don't really know how these are used,
2188          * but documentation say it exists.
2189          */
2190         size -= mdwidth * (kbytes + 1);
2191         size /= kbytes;
2192
2193         usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2194
2195         dep->endpoint.max_streams = 15;
2196         dep->endpoint.ops = &dwc3_gadget_ep_ops;
2197         list_add_tail(&dep->endpoint.ep_list,
2198                         &dwc->gadget.ep_list);
2199         dep->endpoint.caps.type_iso = true;
2200         dep->endpoint.caps.type_bulk = true;
2201         dep->endpoint.caps.type_int = true;
2202
2203         return dwc3_alloc_trb_pool(dep);
2204 }
2205
2206 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2207 {
2208         struct dwc3 *dwc = dep->dwc;
2209
2210         usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2211         dep->endpoint.max_streams = 15;
2212         dep->endpoint.ops = &dwc3_gadget_ep_ops;
2213         list_add_tail(&dep->endpoint.ep_list,
2214                         &dwc->gadget.ep_list);
2215         dep->endpoint.caps.type_iso = true;
2216         dep->endpoint.caps.type_bulk = true;
2217         dep->endpoint.caps.type_int = true;
2218
2219         return dwc3_alloc_trb_pool(dep);
2220 }
2221
2222 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2223 {
2224         struct dwc3_ep                  *dep;
2225         bool                            direction = epnum & 1;
2226         int                             ret;
2227         u8                              num = epnum >> 1;
2228
2229         dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2230         if (!dep)
2231                 return -ENOMEM;
2232
2233         dep->dwc = dwc;
2234         dep->number = epnum;
2235         dep->direction = direction;
2236         dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2237         dwc->eps[epnum] = dep;
2238         dep->combo_num = 0;
2239         dep->start_cmd_status = 0;
2240
2241         snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2242                         direction ? "in" : "out");
2243
2244         dep->endpoint.name = dep->name;
2245
2246         if (!(dep->number > 1)) {
2247                 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2248                 dep->endpoint.comp_desc = NULL;
2249         }
2250
2251         spin_lock_init(&dep->lock);
2252
2253         if (num == 0)
2254                 ret = dwc3_gadget_init_control_endpoint(dep);
2255         else if (direction)
2256                 ret = dwc3_gadget_init_in_endpoint(dep);
2257         else
2258                 ret = dwc3_gadget_init_out_endpoint(dep);
2259
2260         if (ret)
2261                 return ret;
2262
2263         dep->endpoint.caps.dir_in = direction;
2264         dep->endpoint.caps.dir_out = !direction;
2265
2266         INIT_LIST_HEAD(&dep->pending_list);
2267         INIT_LIST_HEAD(&dep->started_list);
2268         INIT_LIST_HEAD(&dep->cancelled_list);
2269
2270         return 0;
2271 }
2272
2273 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2274 {
2275         u8                              epnum;
2276
2277         INIT_LIST_HEAD(&dwc->gadget.ep_list);
2278
2279         for (epnum = 0; epnum < total; epnum++) {
2280                 int                     ret;
2281
2282                 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2283                 if (ret)
2284                         return ret;
2285         }
2286
2287         return 0;
2288 }
2289
2290 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2291 {
2292         struct dwc3_ep                  *dep;
2293         u8                              epnum;
2294
2295         for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2296                 dep = dwc->eps[epnum];
2297                 if (!dep)
2298                         continue;
2299                 /*
2300                  * Physical endpoints 0 and 1 are special; they form the
2301                  * bi-directional USB endpoint 0.
2302                  *
2303                  * For those two physical endpoints, we don't allocate a TRB
2304                  * pool nor do we add them the endpoints list. Due to that, we
2305                  * shouldn't do these two operations otherwise we would end up
2306                  * with all sorts of bugs when removing dwc3.ko.
2307                  */
2308                 if (epnum != 0 && epnum != 1) {
2309                         dwc3_free_trb_pool(dep);
2310                         list_del(&dep->endpoint.ep_list);
2311                 }
2312
2313                 kfree(dep);
2314         }
2315 }
2316
2317 /* -------------------------------------------------------------------------- */
2318
2319 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2320                 struct dwc3_request *req, struct dwc3_trb *trb,
2321                 const struct dwc3_event_depevt *event, int status, int chain)
2322 {
2323         unsigned int            count;
2324
2325         dwc3_ep_inc_deq(dep);
2326
2327         trace_dwc3_complete_trb(dep, trb);
2328         req->num_trbs--;
2329
2330         /*
2331          * If we're in the middle of series of chained TRBs and we
2332          * receive a short transfer along the way, DWC3 will skip
2333          * through all TRBs including the last TRB in the chain (the
2334          * where CHN bit is zero. DWC3 will also avoid clearing HWO
2335          * bit and SW has to do it manually.
2336          *
2337          * We're going to do that here to avoid problems of HW trying
2338          * to use bogus TRBs for transfers.
2339          */
2340         if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2341                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2342
2343         /*
2344          * For isochronous transfers, the first TRB in a service interval must
2345          * have the Isoc-First type. Track and report its interval frame number.
2346          */
2347         if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2348             (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2349                 unsigned int frame_number;
2350
2351                 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2352                 frame_number &= ~(dep->interval - 1);
2353                 req->request.frame_number = frame_number;
2354         }
2355
2356         /*
2357          * If we're dealing with unaligned size OUT transfer, we will be left
2358          * with one TRB pending in the ring. We need to manually clear HWO bit
2359          * from that TRB.
2360          */
2361
2362         if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
2363                 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2364                 return 1;
2365         }
2366
2367         count = trb->size & DWC3_TRB_SIZE_MASK;
2368         req->remaining += count;
2369
2370         if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2371                 return 1;
2372
2373         if (event->status & DEPEVT_STATUS_SHORT && !chain)
2374                 return 1;
2375
2376         if (event->status & DEPEVT_STATUS_IOC)
2377                 return 1;
2378
2379         return 0;
2380 }
2381
2382 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2383                 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2384                 int status)
2385 {
2386         struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2387         struct scatterlist *sg = req->sg;
2388         struct scatterlist *s;
2389         unsigned int pending = req->num_pending_sgs;
2390         unsigned int i;
2391         int ret = 0;
2392
2393         for_each_sg(sg, s, pending, i) {
2394                 trb = &dep->trb_pool[dep->trb_dequeue];
2395
2396                 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2397                         break;
2398
2399                 req->sg = sg_next(s);
2400                 req->num_pending_sgs--;
2401
2402                 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2403                                 trb, event, status, true);
2404                 if (ret)
2405                         break;
2406         }
2407
2408         return ret;
2409 }
2410
2411 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2412                 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2413                 int status)
2414 {
2415         struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2416
2417         return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2418                         event, status, false);
2419 }
2420
2421 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2422 {
2423         return req->request.actual == req->request.length;
2424 }
2425
2426 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2427                 const struct dwc3_event_depevt *event,
2428                 struct dwc3_request *req, int status)
2429 {
2430         int ret;
2431
2432         if (req->num_pending_sgs)
2433                 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2434                                 status);
2435         else
2436                 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2437                                 status);
2438
2439         if (req->needs_extra_trb) {
2440                 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2441                                 status);
2442                 req->needs_extra_trb = false;
2443         }
2444
2445         req->request.actual = req->request.length - req->remaining;
2446
2447         if (!dwc3_gadget_ep_request_completed(req) &&
2448                         req->num_pending_sgs) {
2449                 __dwc3_gadget_kick_transfer(dep);
2450                 goto out;
2451         }
2452
2453         dwc3_gadget_giveback(dep, req, status);
2454
2455 out:
2456         return ret;
2457 }
2458
2459 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2460                 const struct dwc3_event_depevt *event, int status)
2461 {
2462         struct dwc3_request     *req;
2463         struct dwc3_request     *tmp;
2464
2465         list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2466                 int ret;
2467
2468                 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2469                                 req, status);
2470                 if (ret)
2471                         break;
2472         }
2473 }
2474
2475 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2476                 const struct dwc3_event_depevt *event)
2477 {
2478         dep->frame_number = event->parameters;
2479 }
2480
2481 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2482                 const struct dwc3_event_depevt *event)
2483 {
2484         struct dwc3             *dwc = dep->dwc;
2485         unsigned                status = 0;
2486         bool                    stop = false;
2487
2488         dwc3_gadget_endpoint_frame_from_event(dep, event);
2489
2490         if (event->status & DEPEVT_STATUS_BUSERR)
2491                 status = -ECONNRESET;
2492
2493         if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2494                 status = -EXDEV;
2495
2496                 if (list_empty(&dep->started_list))
2497                         stop = true;
2498         }
2499
2500         dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2501
2502         if (stop) {
2503                 dwc3_stop_active_transfer(dep, true);
2504                 dep->flags = DWC3_EP_ENABLED;
2505         }
2506
2507         /*
2508          * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2509          * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2510          */
2511         if (dwc->revision < DWC3_REVISION_183A) {
2512                 u32             reg;
2513                 int             i;
2514
2515                 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2516                         dep = dwc->eps[i];
2517
2518                         if (!(dep->flags & DWC3_EP_ENABLED))
2519                                 continue;
2520
2521                         if (!list_empty(&dep->started_list))
2522                                 return;
2523                 }
2524
2525                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2526                 reg |= dwc->u1u2;
2527                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2528
2529                 dwc->u1u2 = 0;
2530         }
2531 }
2532
2533 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2534                 const struct dwc3_event_depevt *event)
2535 {
2536         dwc3_gadget_endpoint_frame_from_event(dep, event);
2537         (void) __dwc3_gadget_start_isoc(dep);
2538 }
2539
2540 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2541                 const struct dwc3_event_depevt *event)
2542 {
2543         struct dwc3_ep          *dep;
2544         u8                      epnum = event->endpoint_number;
2545         u8                      cmd;
2546
2547         dep = dwc->eps[epnum];
2548
2549         if (!(dep->flags & DWC3_EP_ENABLED)) {
2550                 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
2551                         return;
2552
2553                 /* Handle only EPCMDCMPLT when EP disabled */
2554                 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2555                         return;
2556         }
2557
2558         if (epnum == 0 || epnum == 1) {
2559                 dwc3_ep0_interrupt(dwc, event);
2560                 return;
2561         }
2562
2563         switch (event->endpoint_event) {
2564         case DWC3_DEPEVT_XFERINPROGRESS:
2565                 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2566                 break;
2567         case DWC3_DEPEVT_XFERNOTREADY:
2568                 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2569                 break;
2570         case DWC3_DEPEVT_EPCMDCMPLT:
2571                 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2572
2573                 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2574                         dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2575                         dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2576                 }
2577                 break;
2578         case DWC3_DEPEVT_STREAMEVT:
2579         case DWC3_DEPEVT_XFERCOMPLETE:
2580         case DWC3_DEPEVT_RXTXFIFOEVT:
2581                 break;
2582         }
2583 }
2584
2585 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2586 {
2587         if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2588                 spin_unlock(&dwc->lock);
2589                 dwc->gadget_driver->disconnect(&dwc->gadget);
2590                 spin_lock(&dwc->lock);
2591         }
2592 }
2593
2594 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2595 {
2596         if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2597                 spin_unlock(&dwc->lock);
2598                 dwc->gadget_driver->suspend(&dwc->gadget);
2599                 spin_lock(&dwc->lock);
2600         }
2601 }
2602
2603 static void dwc3_resume_gadget(struct dwc3 *dwc)
2604 {
2605         if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2606                 spin_unlock(&dwc->lock);
2607                 dwc->gadget_driver->resume(&dwc->gadget);
2608                 spin_lock(&dwc->lock);
2609         }
2610 }
2611
2612 static void dwc3_reset_gadget(struct dwc3 *dwc)
2613 {
2614         if (!dwc->gadget_driver)
2615                 return;
2616
2617         if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2618                 spin_unlock(&dwc->lock);
2619                 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2620                 spin_lock(&dwc->lock);
2621         }
2622 }
2623
2624 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
2625 {
2626         struct dwc3 *dwc = dep->dwc;
2627         struct dwc3_gadget_ep_cmd_params params;
2628         u32 cmd;
2629         int ret;
2630
2631         if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
2632                 return;
2633
2634         /*
2635          * NOTICE: We are violating what the Databook says about the
2636          * EndTransfer command. Ideally we would _always_ wait for the
2637          * EndTransfer Command Completion IRQ, but that's causing too
2638          * much trouble synchronizing between us and gadget driver.
2639          *
2640          * We have discussed this with the IP Provider and it was
2641          * suggested to giveback all requests here, but give HW some
2642          * extra time to synchronize with the interconnect. We're using
2643          * an arbitrary 100us delay for that.
2644          *
2645          * Note also that a similar handling was tested by Synopsys
2646          * (thanks a lot Paul) and nothing bad has come out of it.
2647          * In short, what we're doing is:
2648          *
2649          * - Issue EndTransfer WITH CMDIOC bit set
2650          * - Wait 100us
2651          *
2652          * As of IP version 3.10a of the DWC_usb3 IP, the controller
2653          * supports a mode to work around the above limitation. The
2654          * software can poll the CMDACT bit in the DEPCMD register
2655          * after issuing a EndTransfer command. This mode is enabled
2656          * by writing GUCTL2[14]. This polling is already done in the
2657          * dwc3_send_gadget_ep_cmd() function so if the mode is
2658          * enabled, the EndTransfer command will have completed upon
2659          * returning from this function and we don't need to delay for
2660          * 100us.
2661          *
2662          * This mode is NOT available on the DWC_usb31 IP.
2663          */
2664
2665         cmd = DWC3_DEPCMD_ENDTRANSFER;
2666         cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2667         cmd |= DWC3_DEPCMD_CMDIOC;
2668         cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2669         memset(&params, 0, sizeof(params));
2670         ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
2671         WARN_ON_ONCE(ret);
2672         dep->resource_index = 0;
2673
2674         if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2675                 udelay(100);
2676 }
2677
2678 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2679 {
2680         u32 epnum;
2681
2682         for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2683                 struct dwc3_ep *dep;
2684                 int ret;
2685
2686                 dep = dwc->eps[epnum];
2687                 if (!dep)
2688                         continue;
2689
2690                 if (!(dep->flags & DWC3_EP_STALL))
2691                         continue;
2692
2693                 dep->flags &= ~DWC3_EP_STALL;
2694
2695                 ret = dwc3_send_clear_stall_ep_cmd(dep);
2696                 WARN_ON_ONCE(ret);
2697         }
2698 }
2699
2700 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2701 {
2702         int                     reg;
2703
2704         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2705         reg &= ~DWC3_DCTL_INITU1ENA;
2706         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2707
2708         reg &= ~DWC3_DCTL_INITU2ENA;
2709         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2710
2711         dwc3_disconnect_gadget(dwc);
2712
2713         dwc->gadget.speed = USB_SPEED_UNKNOWN;
2714         dwc->setup_packet_pending = false;
2715         usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2716
2717         dwc->connected = false;
2718 }
2719
2720 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2721 {
2722         u32                     reg;
2723
2724         dwc->connected = true;
2725
2726         /*
2727          * WORKAROUND: DWC3 revisions <1.88a have an issue which
2728          * would cause a missing Disconnect Event if there's a
2729          * pending Setup Packet in the FIFO.
2730          *
2731          * There's no suggested workaround on the official Bug
2732          * report, which states that "unless the driver/application
2733          * is doing any special handling of a disconnect event,
2734          * there is no functional issue".
2735          *
2736          * Unfortunately, it turns out that we _do_ some special
2737          * handling of a disconnect event, namely complete all
2738          * pending transfers, notify gadget driver of the
2739          * disconnection, and so on.
2740          *
2741          * Our suggested workaround is to follow the Disconnect
2742          * Event steps here, instead, based on a setup_packet_pending
2743          * flag. Such flag gets set whenever we have a SETUP_PENDING
2744          * status for EP0 TRBs and gets cleared on XferComplete for the
2745          * same endpoint.
2746          *
2747          * Refers to:
2748          *
2749          * STAR#9000466709: RTL: Device : Disconnect event not
2750          * generated if setup packet pending in FIFO
2751          */
2752         if (dwc->revision < DWC3_REVISION_188A) {
2753                 if (dwc->setup_packet_pending)
2754                         dwc3_gadget_disconnect_interrupt(dwc);
2755         }
2756
2757         dwc3_reset_gadget(dwc);
2758
2759         reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2760         reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2761         dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2762         dwc->test_mode = false;
2763         dwc3_clear_stall_all_ep(dwc);
2764
2765         /* Reset device address to zero */
2766         reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2767         reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2768         dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2769 }
2770
2771 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2772 {
2773         struct dwc3_ep          *dep;
2774         int                     ret;
2775         u32                     reg;
2776         u8                      speed;
2777
2778         reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2779         speed = reg & DWC3_DSTS_CONNECTSPD;
2780         dwc->speed = speed;
2781
2782         /*
2783          * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2784          * each time on Connect Done.
2785          *
2786          * Currently we always use the reset value. If any platform
2787          * wants to set this to a different value, we need to add a
2788          * setting and update GCTL.RAMCLKSEL here.
2789          */
2790
2791         switch (speed) {
2792         case DWC3_DSTS_SUPERSPEED_PLUS:
2793                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2794                 dwc->gadget.ep0->maxpacket = 512;
2795                 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2796                 break;
2797         case DWC3_DSTS_SUPERSPEED:
2798                 /*
2799                  * WORKAROUND: DWC3 revisions <1.90a have an issue which
2800                  * would cause a missing USB3 Reset event.
2801                  *
2802                  * In such situations, we should force a USB3 Reset
2803                  * event by calling our dwc3_gadget_reset_interrupt()
2804                  * routine.
2805                  *
2806                  * Refers to:
2807                  *
2808                  * STAR#9000483510: RTL: SS : USB3 reset event may
2809                  * not be generated always when the link enters poll
2810                  */
2811                 if (dwc->revision < DWC3_REVISION_190A)
2812                         dwc3_gadget_reset_interrupt(dwc);
2813
2814                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2815                 dwc->gadget.ep0->maxpacket = 512;
2816                 dwc->gadget.speed = USB_SPEED_SUPER;
2817                 break;
2818         case DWC3_DSTS_HIGHSPEED:
2819                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2820                 dwc->gadget.ep0->maxpacket = 64;
2821                 dwc->gadget.speed = USB_SPEED_HIGH;
2822                 break;
2823         case DWC3_DSTS_FULLSPEED:
2824                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2825                 dwc->gadget.ep0->maxpacket = 64;
2826                 dwc->gadget.speed = USB_SPEED_FULL;
2827                 break;
2828         case DWC3_DSTS_LOWSPEED:
2829                 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2830                 dwc->gadget.ep0->maxpacket = 8;
2831                 dwc->gadget.speed = USB_SPEED_LOW;
2832                 break;
2833         }
2834
2835         dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2836
2837         /* Enable USB2 LPM Capability */
2838
2839         if ((dwc->revision > DWC3_REVISION_194A) &&
2840             (speed != DWC3_DSTS_SUPERSPEED) &&
2841             (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2842                 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2843                 reg |= DWC3_DCFG_LPM_CAP;
2844                 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2845
2846                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2847                 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2848
2849                 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2850
2851                 /*
2852                  * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2853                  * DCFG.LPMCap is set, core responses with an ACK and the
2854                  * BESL value in the LPM token is less than or equal to LPM
2855                  * NYET threshold.
2856                  */
2857                 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2858                                 && dwc->has_lpm_erratum,
2859                                 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2860
2861                 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2862                         reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2863
2864                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2865         } else {
2866                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2867                 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2868                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2869         }
2870
2871         dep = dwc->eps[0];
2872         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2873         if (ret) {
2874                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2875                 return;
2876         }
2877
2878         dep = dwc->eps[1];
2879         ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2880         if (ret) {
2881                 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2882                 return;
2883         }
2884
2885         /*
2886          * Configure PHY via GUSB3PIPECTLn if required.
2887          *
2888          * Update GTXFIFOSIZn
2889          *
2890          * In both cases reset values should be sufficient.
2891          */
2892 }
2893
2894 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2895 {
2896         /*
2897          * TODO take core out of low power mode when that's
2898          * implemented.
2899          */
2900
2901         if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2902                 spin_unlock(&dwc->lock);
2903                 dwc->gadget_driver->resume(&dwc->gadget);
2904                 spin_lock(&dwc->lock);
2905         }
2906 }
2907
2908 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2909                 unsigned int evtinfo)
2910 {
2911         enum dwc3_link_state    next = evtinfo & DWC3_LINK_STATE_MASK;
2912         unsigned int            pwropt;
2913
2914         /*
2915          * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2916          * Hibernation mode enabled which would show up when device detects
2917          * host-initiated U3 exit.
2918          *
2919          * In that case, device will generate a Link State Change Interrupt
2920          * from U3 to RESUME which is only necessary if Hibernation is
2921          * configured in.
2922          *
2923          * There are no functional changes due to such spurious event and we
2924          * just need to ignore it.
2925          *
2926          * Refers to:
2927          *
2928          * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2929          * operational mode
2930          */
2931         pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2932         if ((dwc->revision < DWC3_REVISION_250A) &&
2933                         (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2934                 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2935                                 (next == DWC3_LINK_STATE_RESUME)) {
2936                         return;
2937                 }
2938         }
2939
2940         /*
2941          * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2942          * on the link partner, the USB session might do multiple entry/exit
2943          * of low power states before a transfer takes place.
2944          *
2945          * Due to this problem, we might experience lower throughput. The
2946          * suggested workaround is to disable DCTL[12:9] bits if we're
2947          * transitioning from U1/U2 to U0 and enable those bits again
2948          * after a transfer completes and there are no pending transfers
2949          * on any of the enabled endpoints.
2950          *
2951          * This is the first half of that workaround.
2952          *
2953          * Refers to:
2954          *
2955          * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2956          * core send LGO_Ux entering U0
2957          */
2958         if (dwc->revision < DWC3_REVISION_183A) {
2959                 if (next == DWC3_LINK_STATE_U0) {
2960                         u32     u1u2;
2961                         u32     reg;
2962
2963                         switch (dwc->link_state) {
2964                         case DWC3_LINK_STATE_U1:
2965                         case DWC3_LINK_STATE_U2:
2966                                 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2967                                 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2968                                                 | DWC3_DCTL_ACCEPTU2ENA
2969                                                 | DWC3_DCTL_INITU1ENA
2970                                                 | DWC3_DCTL_ACCEPTU1ENA);
2971
2972                                 if (!dwc->u1u2)
2973                                         dwc->u1u2 = reg & u1u2;
2974
2975                                 reg &= ~u1u2;
2976
2977                                 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2978                                 break;
2979                         default:
2980                                 /* do nothing */
2981                                 break;
2982                         }
2983                 }
2984         }
2985
2986         switch (next) {
2987         case DWC3_LINK_STATE_U1:
2988                 if (dwc->speed == USB_SPEED_SUPER)
2989                         dwc3_suspend_gadget(dwc);
2990                 break;
2991         case DWC3_LINK_STATE_U2:
2992         case DWC3_LINK_STATE_U3:
2993                 dwc3_suspend_gadget(dwc);
2994                 break;
2995         case DWC3_LINK_STATE_RESUME:
2996                 dwc3_resume_gadget(dwc);
2997                 break;
2998         default:
2999                 /* do nothing */
3000                 break;
3001         }
3002
3003         dwc->link_state = next;
3004 }
3005
3006 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3007                                           unsigned int evtinfo)
3008 {
3009         enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3010
3011         if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3012                 dwc3_suspend_gadget(dwc);
3013
3014         dwc->link_state = next;
3015 }
3016
3017 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3018                 unsigned int evtinfo)
3019 {
3020         unsigned int is_ss = evtinfo & BIT(4);
3021
3022         /*
3023          * WORKAROUND: DWC3 revison 2.20a with hibernation support
3024          * have a known issue which can cause USB CV TD.9.23 to fail
3025          * randomly.
3026          *
3027          * Because of this issue, core could generate bogus hibernation
3028          * events which SW needs to ignore.
3029          *
3030          * Refers to:
3031          *
3032          * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3033          * Device Fallback from SuperSpeed
3034          */
3035         if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3036                 return;
3037
3038         /* enter hibernation here */
3039 }
3040
3041 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3042                 const struct dwc3_event_devt *event)
3043 {
3044         switch (event->type) {
3045         case DWC3_DEVICE_EVENT_DISCONNECT:
3046                 dwc3_gadget_disconnect_interrupt(dwc);
3047                 break;
3048         case DWC3_DEVICE_EVENT_RESET:
3049                 dwc3_gadget_reset_interrupt(dwc);
3050                 break;
3051         case DWC3_DEVICE_EVENT_CONNECT_DONE:
3052                 dwc3_gadget_conndone_interrupt(dwc);
3053                 break;
3054         case DWC3_DEVICE_EVENT_WAKEUP:
3055                 dwc3_gadget_wakeup_interrupt(dwc);
3056                 break;
3057         case DWC3_DEVICE_EVENT_HIBER_REQ:
3058                 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3059                                         "unexpected hibernation event\n"))
3060                         break;
3061
3062                 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3063                 break;
3064         case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3065                 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3066                 break;
3067         case DWC3_DEVICE_EVENT_EOPF:
3068                 /* It changed to be suspend event for version 2.30a and above */
3069                 if (dwc->revision >= DWC3_REVISION_230A) {
3070                         /*
3071                          * Ignore suspend event until the gadget enters into
3072                          * USB_STATE_CONFIGURED state.
3073                          */
3074                         if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3075                                 dwc3_gadget_suspend_interrupt(dwc,
3076                                                 event->event_info);
3077                 }
3078                 break;
3079         case DWC3_DEVICE_EVENT_SOF:
3080         case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3081         case DWC3_DEVICE_EVENT_CMD_CMPL:
3082         case DWC3_DEVICE_EVENT_OVERFLOW:
3083                 break;
3084         default:
3085                 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3086         }
3087 }
3088
3089 static void dwc3_process_event_entry(struct dwc3 *dwc,
3090                 const union dwc3_event *event)
3091 {
3092         trace_dwc3_event(event->raw, dwc);
3093
3094         if (!event->type.is_devspec)
3095                 dwc3_endpoint_interrupt(dwc, &event->depevt);
3096         else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3097                 dwc3_gadget_interrupt(dwc, &event->devt);
3098         else
3099                 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3100 }
3101
3102 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3103 {
3104         struct dwc3 *dwc = evt->dwc;
3105         irqreturn_t ret = IRQ_NONE;
3106         int left;
3107         u32 reg;
3108
3109         left = evt->count;
3110
3111         if (!(evt->flags & DWC3_EVENT_PENDING))
3112                 return IRQ_NONE;
3113
3114         while (left > 0) {
3115                 union dwc3_event event;
3116
3117                 event.raw = *(u32 *) (evt->cache + evt->lpos);
3118
3119                 dwc3_process_event_entry(dwc, &event);
3120
3121                 /*
3122                  * FIXME we wrap around correctly to the next entry as
3123                  * almost all entries are 4 bytes in size. There is one
3124                  * entry which has 12 bytes which is a regular entry
3125                  * followed by 8 bytes data. ATM I don't know how
3126                  * things are organized if we get next to the a
3127                  * boundary so I worry about that once we try to handle
3128                  * that.
3129                  */
3130                 evt->lpos = (evt->lpos + 4) % evt->length;
3131                 left -= 4;
3132         }
3133
3134         evt->count = 0;
3135         evt->flags &= ~DWC3_EVENT_PENDING;
3136         ret = IRQ_HANDLED;
3137
3138         /* Unmask interrupt */
3139         reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3140         reg &= ~DWC3_GEVNTSIZ_INTMASK;
3141         dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3142
3143         if (dwc->imod_interval) {
3144                 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3145                 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3146         }
3147
3148         return ret;
3149 }
3150
3151 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3152 {
3153         struct dwc3_event_buffer *evt = _evt;
3154         struct dwc3 *dwc = evt->dwc;
3155         unsigned long flags;
3156         irqreturn_t ret = IRQ_NONE;
3157
3158         spin_lock_irqsave(&dwc->lock, flags);
3159         ret = dwc3_process_event_buf(evt);
3160         spin_unlock_irqrestore(&dwc->lock, flags);
3161
3162         return ret;
3163 }
3164
3165 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3166 {
3167         struct dwc3 *dwc = evt->dwc;
3168         u32 amount;
3169         u32 count;
3170         u32 reg;
3171
3172         if (pm_runtime_suspended(dwc->dev)) {
3173                 pm_runtime_get(dwc->dev);
3174                 disable_irq_nosync(dwc->irq_gadget);
3175                 dwc->pending_events = true;
3176                 return IRQ_HANDLED;
3177         }
3178
3179         /*
3180          * With PCIe legacy interrupt, test shows that top-half irq handler can
3181          * be called again after HW interrupt deassertion. Check if bottom-half
3182          * irq event handler completes before caching new event to prevent
3183          * losing events.
3184          */
3185         if (evt->flags & DWC3_EVENT_PENDING)
3186                 return IRQ_HANDLED;
3187
3188         count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3189         count &= DWC3_GEVNTCOUNT_MASK;
3190         if (!count)
3191                 return IRQ_NONE;
3192
3193         evt->count = count;
3194         evt->flags |= DWC3_EVENT_PENDING;
3195
3196         /* Mask interrupt */
3197         reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3198         reg |= DWC3_GEVNTSIZ_INTMASK;
3199         dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3200
3201         amount = min(count, evt->length - evt->lpos);
3202         memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3203
3204         if (amount < count)
3205                 memcpy(evt->cache, evt->buf, count - amount);
3206
3207         dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3208
3209         return IRQ_WAKE_THREAD;
3210 }
3211
3212 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3213 {
3214         struct dwc3_event_buffer        *evt = _evt;
3215
3216         return dwc3_check_event_buf(evt);
3217 }
3218
3219 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3220 {
3221         struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3222         int irq;
3223
3224         irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3225         if (irq > 0)
3226                 goto out;
3227
3228         if (irq == -EPROBE_DEFER)
3229                 goto out;
3230
3231         irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3232         if (irq > 0)
3233                 goto out;
3234
3235         if (irq == -EPROBE_DEFER)
3236                 goto out;
3237
3238         irq = platform_get_irq(dwc3_pdev, 0);
3239         if (irq > 0)
3240                 goto out;
3241
3242         if (irq != -EPROBE_DEFER)
3243                 dev_err(dwc->dev, "missing peripheral IRQ\n");
3244
3245         if (!irq)
3246                 irq = -EINVAL;
3247
3248 out:
3249         return irq;
3250 }
3251
3252 /**
3253  * dwc3_gadget_init - initializes gadget related registers
3254  * @dwc: pointer to our controller context structure
3255  *
3256  * Returns 0 on success otherwise negative errno.
3257  */
3258 int dwc3_gadget_init(struct dwc3 *dwc)
3259 {
3260         int ret;
3261         int irq;
3262
3263         irq = dwc3_gadget_get_irq(dwc);
3264         if (irq < 0) {
3265                 ret = irq;
3266                 goto err0;
3267         }
3268
3269         dwc->irq_gadget = irq;
3270
3271         dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3272                                           sizeof(*dwc->ep0_trb) * 2,
3273                                           &dwc->ep0_trb_addr, GFP_KERNEL);
3274         if (!dwc->ep0_trb) {
3275                 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3276                 ret = -ENOMEM;
3277                 goto err0;
3278         }
3279
3280         dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3281         if (!dwc->setup_buf) {
3282                 ret = -ENOMEM;
3283                 goto err1;
3284         }
3285
3286         dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3287                         &dwc->bounce_addr, GFP_KERNEL);
3288         if (!dwc->bounce) {
3289                 ret = -ENOMEM;
3290                 goto err2;
3291         }
3292
3293         init_completion(&dwc->ep0_in_setup);
3294
3295         dwc->gadget.ops                 = &dwc3_gadget_ops;
3296         dwc->gadget.speed               = USB_SPEED_UNKNOWN;
3297         dwc->gadget.sg_supported        = true;
3298         dwc->gadget.name                = "dwc3-gadget";
3299         dwc->gadget.is_otg              = dwc->dr_mode == USB_DR_MODE_OTG;
3300
3301         /*
3302          * FIXME We might be setting max_speed to <SUPER, however versions
3303          * <2.20a of dwc3 have an issue with metastability (documented
3304          * elsewhere in this driver) which tells us we can't set max speed to
3305          * anything lower than SUPER.
3306          *
3307          * Because gadget.max_speed is only used by composite.c and function
3308          * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3309          * to happen so we avoid sending SuperSpeed Capability descriptor
3310          * together with our BOS descriptor as that could confuse host into
3311          * thinking we can handle super speed.
3312          *
3313          * Note that, in fact, we won't even support GetBOS requests when speed
3314          * is less than super speed because we don't have means, yet, to tell
3315          * composite.c that we are USB 2.0 + LPM ECN.
3316          */
3317         if (dwc->revision < DWC3_REVISION_220A &&
3318             !dwc->dis_metastability_quirk)
3319                 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3320                                 dwc->revision);
3321
3322         dwc->gadget.max_speed           = dwc->maximum_speed;
3323
3324         /*
3325          * REVISIT: Here we should clear all pending IRQs to be
3326          * sure we're starting from a well known location.
3327          */
3328
3329         ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3330         if (ret)
3331                 goto err3;
3332
3333         ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3334         if (ret) {
3335                 dev_err(dwc->dev, "failed to register udc\n");
3336                 goto err4;
3337         }
3338
3339         dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3340
3341         return 0;
3342
3343 err4:
3344         dwc3_gadget_free_endpoints(dwc);
3345
3346 err3:
3347         dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3348                         dwc->bounce_addr);
3349
3350 err2:
3351         kfree(dwc->setup_buf);
3352
3353 err1:
3354         dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3355                         dwc->ep0_trb, dwc->ep0_trb_addr);
3356
3357 err0:
3358         return ret;
3359 }
3360
3361 /* -------------------------------------------------------------------------- */
3362
3363 void dwc3_gadget_exit(struct dwc3 *dwc)
3364 {
3365         usb_del_gadget_udc(&dwc->gadget);
3366         dwc3_gadget_free_endpoints(dwc);
3367         dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3368                           dwc->bounce_addr);
3369         kfree(dwc->setup_buf);
3370         dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3371                           dwc->ep0_trb, dwc->ep0_trb_addr);
3372 }
3373
3374 int dwc3_gadget_suspend(struct dwc3 *dwc)
3375 {
3376         if (!dwc->gadget_driver)
3377                 return 0;
3378
3379         dwc3_gadget_run_stop(dwc, false, false);
3380         dwc3_disconnect_gadget(dwc);
3381         __dwc3_gadget_stop(dwc);
3382
3383         synchronize_irq(dwc->irq_gadget);
3384
3385         return 0;
3386 }
3387
3388 int dwc3_gadget_resume(struct dwc3 *dwc)
3389 {
3390         int                     ret;
3391
3392         if (!dwc->gadget_driver)
3393                 return 0;
3394
3395         ret = __dwc3_gadget_start(dwc);
3396         if (ret < 0)
3397                 goto err0;
3398
3399         ret = dwc3_gadget_run_stop(dwc, true, false);
3400         if (ret < 0)
3401                 goto err1;
3402
3403         return 0;
3404
3405 err1:
3406         __dwc3_gadget_stop(dwc);
3407
3408 err0:
3409         return ret;
3410 }
3411
3412 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3413 {
3414         if (dwc->pending_events) {
3415                 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3416                 dwc->pending_events = false;
3417                 enable_irq(dwc->irq_gadget);
3418         }
3419 }