Linux 4.19.133
[linux/fpc-iii.git] / drivers / usb / dwc3 / gadget.c
blob2f5f4ca5c0d04f04cbad61e57da1edacfebeaa99
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
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>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
25 #include "debug.h"
26 #include "core.h"
27 #include "gadget.h"
28 #include "io.h"
30 #define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \
31 & ~((d)->interval - 1))
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)
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
43 u32 reg;
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
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;
60 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
62 return 0;
65 /**
66 * dwc3_gadget_get_link_state - gets current state of usb link
67 * @dwc: pointer to our context structure
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
72 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
74 u32 reg;
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
78 return DWC3_DSTS_USBLNKST(reg);
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
86 * Caller should take care of locking. This function will
87 * return 0 on success or -ETIMEDOUT.
89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
91 int retries = 10000;
92 u32 reg;
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
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;
107 if (retries <= 0)
108 return -ETIMEDOUT;
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
114 /* set requested state */
115 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
122 if (dwc->revision >= DWC3_REVISION_194A)
123 return 0;
125 /* wait for a change in DSTS */
126 retries = 10000;
127 while (--retries) {
128 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
130 if (DWC3_DSTS_USBLNKST(reg) == state)
131 return 0;
133 udelay(5);
136 return -ETIMEDOUT;
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
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.
147 static void dwc3_ep_inc_trb(u8 *index)
149 (*index)++;
150 if (*index == (DWC3_TRB_NUM - 1))
151 *index = 0;
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
160 dwc3_ep_inc_trb(&dep->trb_enqueue);
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
169 dwc3_ep_inc_trb(&dep->trb_dequeue);
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173 struct dwc3_request *req, int status)
175 struct dwc3 *dwc = dep->dwc;
177 req->started = false;
178 list_del(&req->list);
179 req->remaining = 0;
180 req->needs_extra_trb = false;
182 if (req->request.status == -EINPROGRESS)
183 req->request.status = status;
185 if (req->trb)
186 usb_gadget_unmap_request_by_dev(dwc->sysdev,
187 &req->request, req->direction);
189 req->trb = NULL;
190 trace_dwc3_gadget_giveback(req);
192 if (dep->number > 1)
193 pm_runtime_put(dwc->dev);
197 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
198 * @dep: The endpoint to whom the request belongs to
199 * @req: The request we're giving back
200 * @status: completion code for the request
202 * Must be called with controller's lock held and interrupts disabled. This
203 * function will unmap @req and call its ->complete() callback to notify upper
204 * layers that it has completed.
206 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
207 int status)
209 struct dwc3 *dwc = dep->dwc;
211 dwc3_gadget_del_and_unmap_request(dep, req, status);
213 spin_unlock(&dwc->lock);
214 usb_gadget_giveback_request(&dep->endpoint, &req->request);
215 spin_lock(&dwc->lock);
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
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
227 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
229 u32 timeout = 500;
230 int status = 0;
231 int ret = 0;
232 u32 reg;
234 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
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;
245 } while (--timeout);
247 if (!timeout) {
248 ret = -ETIMEDOUT;
249 status = -ETIMEDOUT;
252 trace_dwc3_gadget_generic_cmd(cmd, param, status);
254 return ret;
257 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
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
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
268 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269 struct dwc3_gadget_ep_cmd_params *params)
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;
277 int cmd_status = 0;
278 int ret = -EINVAL;
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.
285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
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;
297 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
302 if (saved_config)
303 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
306 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
307 int needs_wakeup;
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);
313 if (unlikely(needs_wakeup)) {
314 ret = __dwc3_gadget_wakeup(dwc);
315 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316 ret);
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);
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.
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
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.
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;
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);
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:
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'.
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.
370 ret = -EAGAIN;
371 break;
372 default:
373 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
376 break;
378 } while (--timeout);
380 if (timeout == 0) {
381 ret = -ETIMEDOUT;
382 cmd_status = -ETIMEDOUT;
385 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
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);
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);
398 return ret;
401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
403 struct dwc3 *dwc = dep->dwc;
404 struct dwc3_gadget_ep_cmd_params params;
405 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
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.
415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416 (dwc->gadget.speed >= USB_SPEED_SUPER))
417 cmd |= DWC3_DEPCMD_CLEARPENDIN;
419 memset(&params, 0, sizeof(params));
421 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
424 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
425 struct dwc3_trb *trb)
427 u32 offset = (char *) trb - (char *) dep->trb_pool;
429 return dep->trb_pool_dma + offset;
432 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
434 struct dwc3 *dwc = dep->dwc;
436 if (dep->trb_pool)
437 return 0;
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;
448 return 0;
451 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
453 struct dwc3 *dwc = dep->dwc;
455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
456 dep->trb_pool, dep->trb_pool_dma);
458 dep->trb_pool = NULL;
459 dep->trb_pool_dma = 0;
462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
464 struct dwc3_gadget_ep_cmd_params params;
466 memset(&params, 0x00, sizeof(params));
468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471 &params);
475 * dwc3_gadget_start_config - configure ep resources
476 * @dep: endpoint that is being enabled
478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
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:
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.
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493 * endpoint on alt setting (8.1.6).
495 * The following simplified method is used instead:
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.
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.
507 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
509 struct dwc3_gadget_ep_cmd_params params;
510 struct dwc3 *dwc;
511 u32 cmd;
512 int i;
513 int ret;
515 if (dep->number)
516 return 0;
518 memset(&params, 0x00, sizeof(params));
519 cmd = DWC3_DEPCMD_DEPSTARTCFG;
520 dwc = dep->dwc;
522 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
523 if (ret)
524 return ret;
526 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527 struct dwc3_ep *dep = dwc->eps[i];
529 if (!dep)
530 continue;
532 ret = dwc3_gadget_set_xfer_resource(dep);
533 if (ret)
534 return ret;
537 return 0;
540 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
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;
547 comp_desc = dep->endpoint.comp_desc;
548 desc = dep->endpoint.desc;
550 memset(&params, 0x00, sizeof(params));
552 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
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);
561 params.param0 |= action;
562 if (action == DWC3_DEPCFG_ACTION_RESTORE)
563 params.param2 |= dep->saved_state;
565 if (usb_endpoint_xfer_control(desc))
566 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
568 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
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;
577 if (!usb_endpoint_xfer_control(desc))
578 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
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.
586 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
589 * We must use the lower 16 TX FIFOs even though
590 * HW might have more
592 if (dep->direction)
593 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
595 if (desc->bInterval) {
596 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
597 dep->interval = 1 << (desc->bInterval - 1);
600 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
605 * @dep: endpoint to be initialized
606 * @action: one of INIT, MODIFY or RESTORE
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.
611 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
613 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
614 struct dwc3 *dwc = dep->dwc;
616 u32 reg;
617 int ret;
619 if (!(dep->flags & DWC3_EP_ENABLED)) {
620 ret = dwc3_gadget_start_config(dep);
621 if (ret)
622 return ret;
625 ret = dwc3_gadget_set_ep_config(dep, action);
626 if (ret)
627 return ret;
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
630 struct dwc3_trb *trb_st_hw;
631 struct dwc3_trb *trb_link;
633 dep->type = usb_endpoint_type(desc);
634 dep->flags |= DWC3_EP_ENABLED;
635 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
637 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
638 reg |= DWC3_DALEPENA_EP(dep->number);
639 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
641 if (usb_endpoint_xfer_control(desc))
642 goto out;
644 /* Initialize the TRB ring */
645 dep->trb_dequeue = 0;
646 dep->trb_enqueue = 0;
647 memset(dep->trb_pool, 0,
648 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
650 /* Link TRB. The HWO bit is never reset */
651 trb_st_hw = &dep->trb_pool[0];
653 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
654 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
656 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
657 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
661 * Issue StartTransfer here with no-op TRB so we can always rely on No
662 * Response Update Transfer command.
664 if (usb_endpoint_xfer_bulk(desc) ||
665 usb_endpoint_xfer_int(desc)) {
666 struct dwc3_gadget_ep_cmd_params params;
667 struct dwc3_trb *trb;
668 dma_addr_t trb_dma;
669 u32 cmd;
671 memset(&params, 0, sizeof(params));
672 trb = &dep->trb_pool[0];
673 trb_dma = dwc3_trb_dma_offset(dep, trb);
675 params.param0 = upper_32_bits(trb_dma);
676 params.param1 = lower_32_bits(trb_dma);
678 cmd = DWC3_DEPCMD_STARTTRANSFER;
680 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
681 if (ret < 0)
682 return ret;
685 out:
686 trace_dwc3_gadget_ep_enable(dep);
688 return 0;
691 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
692 bool interrupt);
693 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
695 struct dwc3_request *req;
697 dwc3_stop_active_transfer(dep, true, false);
699 /* - giveback all requests to gadget driver */
700 while (!list_empty(&dep->started_list)) {
701 req = next_request(&dep->started_list);
703 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
706 while (!list_empty(&dep->pending_list)) {
707 req = next_request(&dep->pending_list);
709 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
712 while (!list_empty(&dep->cancelled_list)) {
713 req = next_request(&dep->cancelled_list);
715 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
720 * __dwc3_gadget_ep_disable - disables a hw endpoint
721 * @dep: the endpoint to disable
723 * This function undoes what __dwc3_gadget_ep_enable did and also removes
724 * requests which are currently being processed by the hardware and those which
725 * are not yet scheduled.
727 * Caller should take care of locking.
729 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
731 struct dwc3 *dwc = dep->dwc;
732 u32 reg;
734 trace_dwc3_gadget_ep_disable(dep);
736 dwc3_remove_requests(dwc, dep);
738 /* make sure HW endpoint isn't stalled */
739 if (dep->flags & DWC3_EP_STALL)
740 __dwc3_gadget_ep_set_halt(dep, 0, false);
742 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
743 reg &= ~DWC3_DALEPENA_EP(dep->number);
744 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
746 dep->stream_capable = false;
747 dep->type = 0;
748 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
750 /* Clear out the ep descriptors for non-ep0 */
751 if (dep->number > 1) {
752 dep->endpoint.comp_desc = NULL;
753 dep->endpoint.desc = NULL;
756 return 0;
759 /* -------------------------------------------------------------------------- */
761 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
762 const struct usb_endpoint_descriptor *desc)
764 return -EINVAL;
767 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
769 return -EINVAL;
772 /* -------------------------------------------------------------------------- */
774 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
775 const struct usb_endpoint_descriptor *desc)
777 struct dwc3_ep *dep;
778 struct dwc3 *dwc;
779 unsigned long flags;
780 int ret;
782 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
783 pr_debug("dwc3: invalid parameters\n");
784 return -EINVAL;
787 if (!desc->wMaxPacketSize) {
788 pr_debug("dwc3: missing wMaxPacketSize\n");
789 return -EINVAL;
792 dep = to_dwc3_ep(ep);
793 dwc = dep->dwc;
795 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
796 "%s is already enabled\n",
797 dep->name))
798 return 0;
800 spin_lock_irqsave(&dwc->lock, flags);
801 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
802 spin_unlock_irqrestore(&dwc->lock, flags);
804 return ret;
807 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
809 struct dwc3_ep *dep;
810 struct dwc3 *dwc;
811 unsigned long flags;
812 int ret;
814 if (!ep) {
815 pr_debug("dwc3: invalid parameters\n");
816 return -EINVAL;
819 dep = to_dwc3_ep(ep);
820 dwc = dep->dwc;
822 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
823 "%s is already disabled\n",
824 dep->name))
825 return 0;
827 spin_lock_irqsave(&dwc->lock, flags);
828 ret = __dwc3_gadget_ep_disable(dep);
829 spin_unlock_irqrestore(&dwc->lock, flags);
831 return ret;
834 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
835 gfp_t gfp_flags)
837 struct dwc3_request *req;
838 struct dwc3_ep *dep = to_dwc3_ep(ep);
840 req = kzalloc(sizeof(*req), gfp_flags);
841 if (!req)
842 return NULL;
844 req->direction = dep->direction;
845 req->epnum = dep->number;
846 req->dep = dep;
848 trace_dwc3_alloc_request(req);
850 return &req->request;
853 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
854 struct usb_request *request)
856 struct dwc3_request *req = to_dwc3_request(request);
858 trace_dwc3_free_request(req);
859 kfree(req);
863 * dwc3_ep_prev_trb - returns the previous TRB in the ring
864 * @dep: The endpoint with the TRB ring
865 * @index: The index of the current TRB in the ring
867 * Returns the TRB prior to the one pointed to by the index. If the
868 * index is 0, we will wrap backwards, skip the link TRB, and return
869 * the one just before that.
871 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
873 u8 tmp = index;
875 if (!tmp)
876 tmp = DWC3_TRB_NUM - 1;
878 return &dep->trb_pool[tmp - 1];
881 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
883 struct dwc3_trb *tmp;
884 u8 trbs_left;
887 * If enqueue & dequeue are equal than it is either full or empty.
889 * One way to know for sure is if the TRB right before us has HWO bit
890 * set or not. If it has, then we're definitely full and can't fit any
891 * more transfers in our ring.
893 if (dep->trb_enqueue == dep->trb_dequeue) {
894 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
895 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
896 return 0;
898 return DWC3_TRB_NUM - 1;
901 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
902 trbs_left &= (DWC3_TRB_NUM - 1);
904 if (dep->trb_dequeue < dep->trb_enqueue)
905 trbs_left--;
907 return trbs_left;
910 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
911 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
912 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
914 struct dwc3 *dwc = dep->dwc;
915 struct usb_gadget *gadget = &dwc->gadget;
916 enum usb_device_speed speed = gadget->speed;
918 trb->size = DWC3_TRB_SIZE_LENGTH(length);
919 trb->bpl = lower_32_bits(dma);
920 trb->bph = upper_32_bits(dma);
922 switch (usb_endpoint_type(dep->endpoint.desc)) {
923 case USB_ENDPOINT_XFER_CONTROL:
924 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
925 break;
927 case USB_ENDPOINT_XFER_ISOC:
928 if (!node) {
929 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
932 * USB Specification 2.0 Section 5.9.2 states that: "If
933 * there is only a single transaction in the microframe,
934 * only a DATA0 data packet PID is used. If there are
935 * two transactions per microframe, DATA1 is used for
936 * the first transaction data packet and DATA0 is used
937 * for the second transaction data packet. If there are
938 * three transactions per microframe, DATA2 is used for
939 * the first transaction data packet, DATA1 is used for
940 * the second, and DATA0 is used for the third."
942 * IOW, we should satisfy the following cases:
944 * 1) length <= maxpacket
945 * - DATA0
947 * 2) maxpacket < length <= (2 * maxpacket)
948 * - DATA1, DATA0
950 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
951 * - DATA2, DATA1, DATA0
953 if (speed == USB_SPEED_HIGH) {
954 struct usb_ep *ep = &dep->endpoint;
955 unsigned int mult = 2;
956 unsigned int maxp = usb_endpoint_maxp(ep->desc);
958 if (length <= (2 * maxp))
959 mult--;
961 if (length <= maxp)
962 mult--;
964 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
966 } else {
967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
970 /* always enable Interrupt on Missed ISOC */
971 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
972 break;
974 case USB_ENDPOINT_XFER_BULK:
975 case USB_ENDPOINT_XFER_INT:
976 trb->ctrl = DWC3_TRBCTL_NORMAL;
977 break;
978 default:
980 * This is only possible with faulty memory because we
981 * checked it already :)
983 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
984 usb_endpoint_type(dep->endpoint.desc));
988 * Enable Continue on Short Packet
989 * when endpoint is not a stream capable
991 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
992 if (!dep->stream_capable)
993 trb->ctrl |= DWC3_TRB_CTRL_CSP;
995 if (short_not_ok)
996 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
999 if ((!no_interrupt && !chain) ||
1000 (dwc3_calc_trbs_left(dep) == 1))
1001 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1003 if (chain)
1004 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1006 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1007 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1009 trb->ctrl |= DWC3_TRB_CTRL_HWO;
1011 dwc3_ep_inc_enq(dep);
1013 trace_dwc3_prepare_trb(dep, trb);
1017 * dwc3_prepare_one_trb - setup one TRB from one request
1018 * @dep: endpoint for which this request is prepared
1019 * @req: dwc3_request pointer
1020 * @chain: should this TRB be chained to the next?
1021 * @node: only for isochronous endpoints. First TRB needs different type.
1023 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1024 struct dwc3_request *req, unsigned chain, unsigned node)
1026 struct dwc3_trb *trb;
1027 unsigned int length;
1028 dma_addr_t dma;
1029 unsigned stream_id = req->request.stream_id;
1030 unsigned short_not_ok = req->request.short_not_ok;
1031 unsigned no_interrupt = req->request.no_interrupt;
1033 if (req->request.num_sgs > 0) {
1034 length = sg_dma_len(req->start_sg);
1035 dma = sg_dma_address(req->start_sg);
1036 } else {
1037 length = req->request.length;
1038 dma = req->request.dma;
1041 trb = &dep->trb_pool[dep->trb_enqueue];
1043 if (!req->trb) {
1044 dwc3_gadget_move_started_request(req);
1045 req->trb = trb;
1046 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1049 req->num_trbs++;
1051 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1052 stream_id, short_not_ok, no_interrupt);
1055 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1056 struct dwc3_request *req)
1058 struct scatterlist *sg = req->start_sg;
1059 struct scatterlist *s;
1060 int i;
1062 unsigned int remaining = req->request.num_mapped_sgs
1063 - req->num_queued_sgs;
1065 for_each_sg(sg, s, remaining, i) {
1066 unsigned int length = req->request.length;
1067 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1068 unsigned int rem = length % maxp;
1069 unsigned chain = true;
1072 * IOMMU driver is coalescing the list of sgs which shares a
1073 * page boundary into one and giving it to USB driver. With
1074 * this the number of sgs mapped is not equal to the number of
1075 * sgs passed. So mark the chain bit to false if it isthe last
1076 * mapped sg.
1078 if (i == remaining - 1)
1079 chain = false;
1081 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1082 struct dwc3 *dwc = dep->dwc;
1083 struct dwc3_trb *trb;
1085 req->needs_extra_trb = true;
1087 /* prepare normal TRB */
1088 dwc3_prepare_one_trb(dep, req, true, i);
1090 /* Now prepare one extra TRB to align transfer size */
1091 trb = &dep->trb_pool[dep->trb_enqueue];
1092 req->num_trbs++;
1093 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1094 maxp - rem, false, 1,
1095 req->request.stream_id,
1096 req->request.short_not_ok,
1097 req->request.no_interrupt);
1098 } else {
1099 dwc3_prepare_one_trb(dep, req, chain, i);
1103 * There can be a situation where all sgs in sglist are not
1104 * queued because of insufficient trb number. To handle this
1105 * case, update start_sg to next sg to be queued, so that
1106 * we have free trbs we can continue queuing from where we
1107 * previously stopped
1109 if (chain)
1110 req->start_sg = sg_next(s);
1112 req->num_queued_sgs++;
1114 if (!dwc3_calc_trbs_left(dep))
1115 break;
1119 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1120 struct dwc3_request *req)
1122 unsigned int length = req->request.length;
1123 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1124 unsigned int rem = length % maxp;
1126 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
1127 struct dwc3 *dwc = dep->dwc;
1128 struct dwc3_trb *trb;
1130 req->needs_extra_trb = true;
1132 /* prepare normal TRB */
1133 dwc3_prepare_one_trb(dep, req, true, 0);
1135 /* Now prepare one extra TRB to align transfer size */
1136 trb = &dep->trb_pool[dep->trb_enqueue];
1137 req->num_trbs++;
1138 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1139 false, 1, req->request.stream_id,
1140 req->request.short_not_ok,
1141 req->request.no_interrupt);
1142 } else if (req->request.zero && req->request.length &&
1143 (IS_ALIGNED(req->request.length, maxp))) {
1144 struct dwc3 *dwc = dep->dwc;
1145 struct dwc3_trb *trb;
1147 req->needs_extra_trb = true;
1149 /* prepare normal TRB */
1150 dwc3_prepare_one_trb(dep, req, true, 0);
1152 /* Now prepare one extra TRB to handle ZLP */
1153 trb = &dep->trb_pool[dep->trb_enqueue];
1154 req->num_trbs++;
1155 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1156 false, 1, req->request.stream_id,
1157 req->request.short_not_ok,
1158 req->request.no_interrupt);
1159 } else {
1160 dwc3_prepare_one_trb(dep, req, false, 0);
1165 * dwc3_prepare_trbs - setup TRBs from requests
1166 * @dep: endpoint for which requests are being prepared
1168 * The function goes through the requests list and sets up TRBs for the
1169 * transfers. The function returns once there are no more TRBs available or
1170 * it runs out of requests.
1172 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1174 struct dwc3_request *req, *n;
1176 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1179 * We can get in a situation where there's a request in the started list
1180 * but there weren't enough TRBs to fully kick it in the first time
1181 * around, so it has been waiting for more TRBs to be freed up.
1183 * In that case, we should check if we have a request with pending_sgs
1184 * in the started list and prepare TRBs for that request first,
1185 * otherwise we will prepare TRBs completely out of order and that will
1186 * break things.
1188 list_for_each_entry(req, &dep->started_list, list) {
1189 if (req->num_pending_sgs > 0)
1190 dwc3_prepare_one_trb_sg(dep, req);
1192 if (!dwc3_calc_trbs_left(dep))
1193 return;
1196 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1197 struct dwc3 *dwc = dep->dwc;
1198 int ret;
1200 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1201 dep->direction);
1202 if (ret)
1203 return;
1205 req->sg = req->request.sg;
1206 req->start_sg = req->sg;
1207 req->num_queued_sgs = 0;
1208 req->num_pending_sgs = req->request.num_mapped_sgs;
1210 if (req->num_pending_sgs > 0)
1211 dwc3_prepare_one_trb_sg(dep, req);
1212 else
1213 dwc3_prepare_one_trb_linear(dep, req);
1215 if (!dwc3_calc_trbs_left(dep))
1216 return;
1220 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1222 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1224 struct dwc3_gadget_ep_cmd_params params;
1225 struct dwc3_request *req;
1226 int starting;
1227 int ret;
1228 u32 cmd;
1230 if (!dwc3_calc_trbs_left(dep))
1231 return 0;
1233 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1235 dwc3_prepare_trbs(dep);
1236 req = next_request(&dep->started_list);
1237 if (!req) {
1238 dep->flags |= DWC3_EP_PENDING_REQUEST;
1239 return 0;
1242 memset(&params, 0, sizeof(params));
1244 if (starting) {
1245 params.param0 = upper_32_bits(req->trb_dma);
1246 params.param1 = lower_32_bits(req->trb_dma);
1247 cmd = DWC3_DEPCMD_STARTTRANSFER;
1249 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1250 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1251 } else {
1252 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1253 DWC3_DEPCMD_PARAM(dep->resource_index);
1256 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1257 if (ret < 0) {
1258 struct dwc3_request *tmp;
1260 if (ret == -EAGAIN)
1261 return ret;
1263 dwc3_stop_active_transfer(dep, true, true);
1265 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1266 dwc3_gadget_move_cancelled_request(req);
1268 /* If ep isn't started, then there's no end transfer pending */
1269 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1270 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1272 return ret;
1275 return 0;
1278 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1280 u32 reg;
1282 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1283 return DWC3_DSTS_SOFFN(reg);
1286 static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1288 if (list_empty(&dep->pending_list)) {
1289 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
1290 dep->name);
1291 dep->flags |= DWC3_EP_PENDING_REQUEST;
1292 return;
1295 dep->frame_number = DWC3_ALIGN_FRAME(dep);
1296 __dwc3_gadget_kick_transfer(dep);
1299 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1301 struct dwc3 *dwc = dep->dwc;
1303 if (!dep->endpoint.desc) {
1304 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1305 dep->name);
1306 return -ESHUTDOWN;
1309 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1310 &req->request, req->dep->name))
1311 return -EINVAL;
1313 pm_runtime_get(dwc->dev);
1315 req->request.actual = 0;
1316 req->request.status = -EINPROGRESS;
1318 trace_dwc3_ep_queue(req);
1320 list_add_tail(&req->list, &dep->pending_list);
1323 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1324 * wait for a XferNotReady event so we will know what's the current
1325 * (micro-)frame number.
1327 * Without this trick, we are very, very likely gonna get Bus Expiry
1328 * errors which will force us issue EndTransfer command.
1330 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1331 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1332 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1333 return 0;
1335 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1336 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1337 __dwc3_gadget_start_isoc(dep);
1338 return 0;
1343 return __dwc3_gadget_kick_transfer(dep);
1346 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1347 gfp_t gfp_flags)
1349 struct dwc3_request *req = to_dwc3_request(request);
1350 struct dwc3_ep *dep = to_dwc3_ep(ep);
1351 struct dwc3 *dwc = dep->dwc;
1353 unsigned long flags;
1355 int ret;
1357 spin_lock_irqsave(&dwc->lock, flags);
1358 ret = __dwc3_gadget_ep_queue(dep, req);
1359 spin_unlock_irqrestore(&dwc->lock, flags);
1361 return ret;
1364 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1366 int i;
1369 * If request was already started, this means we had to
1370 * stop the transfer. With that we also need to ignore
1371 * all TRBs used by the request, however TRBs can only
1372 * be modified after completion of END_TRANSFER
1373 * command. So what we do here is that we wait for
1374 * END_TRANSFER completion and only after that, we jump
1375 * over TRBs by clearing HWO and incrementing dequeue
1376 * pointer.
1378 for (i = 0; i < req->num_trbs; i++) {
1379 struct dwc3_trb *trb;
1381 trb = &dep->trb_pool[dep->trb_dequeue];
1382 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1383 dwc3_ep_inc_deq(dep);
1386 req->num_trbs = 0;
1389 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1391 struct dwc3_request *req;
1392 struct dwc3_request *tmp;
1394 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1395 dwc3_gadget_ep_skip_trbs(dep, req);
1396 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1400 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1401 struct usb_request *request)
1403 struct dwc3_request *req = to_dwc3_request(request);
1404 struct dwc3_request *r = NULL;
1406 struct dwc3_ep *dep = to_dwc3_ep(ep);
1407 struct dwc3 *dwc = dep->dwc;
1409 unsigned long flags;
1410 int ret = 0;
1412 trace_dwc3_ep_dequeue(req);
1414 spin_lock_irqsave(&dwc->lock, flags);
1416 list_for_each_entry(r, &dep->pending_list, list) {
1417 if (r == req)
1418 break;
1421 if (r != req) {
1422 list_for_each_entry(r, &dep->started_list, list) {
1423 if (r == req)
1424 break;
1426 if (r == req) {
1427 /* wait until it is processed */
1428 dwc3_stop_active_transfer(dep, true, true);
1430 if (!r->trb)
1431 goto out0;
1433 dwc3_gadget_move_cancelled_request(req);
1434 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1435 goto out0;
1436 else
1437 goto out1;
1439 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1440 request, ep->name);
1441 ret = -EINVAL;
1442 goto out0;
1445 out1:
1446 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1448 out0:
1449 spin_unlock_irqrestore(&dwc->lock, flags);
1451 return ret;
1454 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1456 struct dwc3_gadget_ep_cmd_params params;
1457 struct dwc3 *dwc = dep->dwc;
1458 int ret;
1460 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1461 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1462 return -EINVAL;
1465 memset(&params, 0x00, sizeof(params));
1467 if (value) {
1468 struct dwc3_trb *trb;
1470 unsigned transfer_in_flight;
1471 unsigned started;
1473 if (dep->number > 1)
1474 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1475 else
1476 trb = &dwc->ep0_trb[dep->trb_enqueue];
1478 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1479 started = !list_empty(&dep->started_list);
1481 if (!protocol && ((dep->direction && transfer_in_flight) ||
1482 (!dep->direction && started))) {
1483 return -EAGAIN;
1486 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1487 &params);
1488 if (ret)
1489 dev_err(dwc->dev, "failed to set STALL on %s\n",
1490 dep->name);
1491 else
1492 dep->flags |= DWC3_EP_STALL;
1493 } else {
1495 ret = dwc3_send_clear_stall_ep_cmd(dep);
1496 if (ret)
1497 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1498 dep->name);
1499 else
1500 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1503 return ret;
1506 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1508 struct dwc3_ep *dep = to_dwc3_ep(ep);
1509 struct dwc3 *dwc = dep->dwc;
1511 unsigned long flags;
1513 int ret;
1515 spin_lock_irqsave(&dwc->lock, flags);
1516 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1517 spin_unlock_irqrestore(&dwc->lock, flags);
1519 return ret;
1522 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1524 struct dwc3_ep *dep = to_dwc3_ep(ep);
1525 struct dwc3 *dwc = dep->dwc;
1526 unsigned long flags;
1527 int ret;
1529 spin_lock_irqsave(&dwc->lock, flags);
1530 dep->flags |= DWC3_EP_WEDGE;
1532 if (dep->number == 0 || dep->number == 1)
1533 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1534 else
1535 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1536 spin_unlock_irqrestore(&dwc->lock, flags);
1538 return ret;
1541 /* -------------------------------------------------------------------------- */
1543 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1544 .bLength = USB_DT_ENDPOINT_SIZE,
1545 .bDescriptorType = USB_DT_ENDPOINT,
1546 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1549 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1550 .enable = dwc3_gadget_ep0_enable,
1551 .disable = dwc3_gadget_ep0_disable,
1552 .alloc_request = dwc3_gadget_ep_alloc_request,
1553 .free_request = dwc3_gadget_ep_free_request,
1554 .queue = dwc3_gadget_ep0_queue,
1555 .dequeue = dwc3_gadget_ep_dequeue,
1556 .set_halt = dwc3_gadget_ep0_set_halt,
1557 .set_wedge = dwc3_gadget_ep_set_wedge,
1560 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1561 .enable = dwc3_gadget_ep_enable,
1562 .disable = dwc3_gadget_ep_disable,
1563 .alloc_request = dwc3_gadget_ep_alloc_request,
1564 .free_request = dwc3_gadget_ep_free_request,
1565 .queue = dwc3_gadget_ep_queue,
1566 .dequeue = dwc3_gadget_ep_dequeue,
1567 .set_halt = dwc3_gadget_ep_set_halt,
1568 .set_wedge = dwc3_gadget_ep_set_wedge,
1571 /* -------------------------------------------------------------------------- */
1573 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1575 struct dwc3 *dwc = gadget_to_dwc(g);
1577 return __dwc3_gadget_get_frame(dwc);
1580 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1582 int retries;
1584 int ret;
1585 u32 reg;
1587 u8 link_state;
1590 * According to the Databook Remote wakeup request should
1591 * be issued only when the device is in early suspend state.
1593 * We can check that via USB Link State bits in DSTS register.
1595 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1597 link_state = DWC3_DSTS_USBLNKST(reg);
1599 switch (link_state) {
1600 case DWC3_LINK_STATE_RESET:
1601 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1602 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1603 case DWC3_LINK_STATE_RESUME:
1604 break;
1605 default:
1606 return -EINVAL;
1609 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1610 if (ret < 0) {
1611 dev_err(dwc->dev, "failed to put link in Recovery\n");
1612 return ret;
1615 /* Recent versions do this automatically */
1616 if (dwc->revision < DWC3_REVISION_194A) {
1617 /* write zeroes to Link Change Request */
1618 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1619 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1620 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1623 /* poll until Link State changes to ON */
1624 retries = 20000;
1626 while (retries--) {
1627 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1629 /* in HS, means ON */
1630 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1631 break;
1634 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1635 dev_err(dwc->dev, "failed to send remote wakeup\n");
1636 return -EINVAL;
1639 return 0;
1642 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1644 struct dwc3 *dwc = gadget_to_dwc(g);
1645 unsigned long flags;
1646 int ret;
1648 spin_lock_irqsave(&dwc->lock, flags);
1649 ret = __dwc3_gadget_wakeup(dwc);
1650 spin_unlock_irqrestore(&dwc->lock, flags);
1652 return ret;
1655 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1656 int is_selfpowered)
1658 struct dwc3 *dwc = gadget_to_dwc(g);
1659 unsigned long flags;
1661 spin_lock_irqsave(&dwc->lock, flags);
1662 g->is_selfpowered = !!is_selfpowered;
1663 spin_unlock_irqrestore(&dwc->lock, flags);
1665 return 0;
1668 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1670 u32 reg;
1671 u32 timeout = 500;
1673 if (pm_runtime_suspended(dwc->dev))
1674 return 0;
1676 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1677 if (is_on) {
1678 if (dwc->revision <= DWC3_REVISION_187A) {
1679 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1680 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1683 if (dwc->revision >= DWC3_REVISION_194A)
1684 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1685 reg |= DWC3_DCTL_RUN_STOP;
1687 if (dwc->has_hibernation)
1688 reg |= DWC3_DCTL_KEEP_CONNECT;
1690 dwc->pullups_connected = true;
1691 } else {
1692 reg &= ~DWC3_DCTL_RUN_STOP;
1694 if (dwc->has_hibernation && !suspend)
1695 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1697 dwc->pullups_connected = false;
1700 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1702 do {
1703 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1704 reg &= DWC3_DSTS_DEVCTRLHLT;
1705 } while (--timeout && !(!is_on ^ !reg));
1707 if (!timeout)
1708 return -ETIMEDOUT;
1710 return 0;
1713 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1715 struct dwc3 *dwc = gadget_to_dwc(g);
1716 unsigned long flags;
1717 int ret;
1719 is_on = !!is_on;
1722 * Per databook, when we want to stop the gadget, if a control transfer
1723 * is still in process, complete it and get the core into setup phase.
1725 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1726 reinit_completion(&dwc->ep0_in_setup);
1728 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1729 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1730 if (ret == 0) {
1731 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1732 return -ETIMEDOUT;
1736 spin_lock_irqsave(&dwc->lock, flags);
1737 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1738 spin_unlock_irqrestore(&dwc->lock, flags);
1740 return ret;
1743 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1745 u32 reg;
1747 /* Enable all but Start and End of Frame IRQs */
1748 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1749 DWC3_DEVTEN_EVNTOVERFLOWEN |
1750 DWC3_DEVTEN_CMDCMPLTEN |
1751 DWC3_DEVTEN_ERRTICERREN |
1752 DWC3_DEVTEN_WKUPEVTEN |
1753 DWC3_DEVTEN_CONNECTDONEEN |
1754 DWC3_DEVTEN_USBRSTEN |
1755 DWC3_DEVTEN_DISCONNEVTEN);
1757 if (dwc->revision < DWC3_REVISION_250A)
1758 reg |= DWC3_DEVTEN_ULSTCNGEN;
1760 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1763 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1765 /* mask all interrupts */
1766 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1769 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1770 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1773 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1774 * @dwc: pointer to our context structure
1776 * The following looks like complex but it's actually very simple. In order to
1777 * calculate the number of packets we can burst at once on OUT transfers, we're
1778 * gonna use RxFIFO size.
1780 * To calculate RxFIFO size we need two numbers:
1781 * MDWIDTH = size, in bits, of the internal memory bus
1782 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1784 * Given these two numbers, the formula is simple:
1786 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1788 * 24 bytes is for 3x SETUP packets
1789 * 16 bytes is a clock domain crossing tolerance
1791 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1793 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1795 u32 ram2_depth;
1796 u32 mdwidth;
1797 u32 nump;
1798 u32 reg;
1800 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1801 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1803 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1804 nump = min_t(u32, nump, 16);
1806 /* update NumP */
1807 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1808 reg &= ~DWC3_DCFG_NUMP_MASK;
1809 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1810 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1813 static int __dwc3_gadget_start(struct dwc3 *dwc)
1815 struct dwc3_ep *dep;
1816 int ret = 0;
1817 u32 reg;
1820 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1821 * the core supports IMOD, disable it.
1823 if (dwc->imod_interval) {
1824 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1825 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1826 } else if (dwc3_has_imod(dwc)) {
1827 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1831 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1832 * field instead of letting dwc3 itself calculate that automatically.
1834 * This way, we maximize the chances that we'll be able to get several
1835 * bursts of data without going through any sort of endpoint throttling.
1837 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1838 if (dwc3_is_usb31(dwc))
1839 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1840 else
1841 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1843 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1845 dwc3_gadget_setup_nump(dwc);
1847 /* Start with SuperSpeed Default */
1848 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1850 dep = dwc->eps[0];
1851 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1852 if (ret) {
1853 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1854 goto err0;
1857 dep = dwc->eps[1];
1858 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1859 if (ret) {
1860 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1861 goto err1;
1864 /* begin to receive SETUP packets */
1865 dwc->ep0state = EP0_SETUP_PHASE;
1866 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
1867 dwc3_ep0_out_start(dwc);
1869 dwc3_gadget_enable_irq(dwc);
1871 return 0;
1873 err1:
1874 __dwc3_gadget_ep_disable(dwc->eps[0]);
1876 err0:
1877 return ret;
1880 static int dwc3_gadget_start(struct usb_gadget *g,
1881 struct usb_gadget_driver *driver)
1883 struct dwc3 *dwc = gadget_to_dwc(g);
1884 unsigned long flags;
1885 int ret = 0;
1886 int irq;
1888 irq = dwc->irq_gadget;
1889 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1890 IRQF_SHARED, "dwc3", dwc->ev_buf);
1891 if (ret) {
1892 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1893 irq, ret);
1894 goto err0;
1897 spin_lock_irqsave(&dwc->lock, flags);
1898 if (dwc->gadget_driver) {
1899 dev_err(dwc->dev, "%s is already bound to %s\n",
1900 dwc->gadget.name,
1901 dwc->gadget_driver->driver.name);
1902 ret = -EBUSY;
1903 goto err1;
1906 dwc->gadget_driver = driver;
1908 if (pm_runtime_active(dwc->dev))
1909 __dwc3_gadget_start(dwc);
1911 spin_unlock_irqrestore(&dwc->lock, flags);
1913 return 0;
1915 err1:
1916 spin_unlock_irqrestore(&dwc->lock, flags);
1917 free_irq(irq, dwc);
1919 err0:
1920 return ret;
1923 static void __dwc3_gadget_stop(struct dwc3 *dwc)
1925 dwc3_gadget_disable_irq(dwc);
1926 __dwc3_gadget_ep_disable(dwc->eps[0]);
1927 __dwc3_gadget_ep_disable(dwc->eps[1]);
1930 static int dwc3_gadget_stop(struct usb_gadget *g)
1932 struct dwc3 *dwc = gadget_to_dwc(g);
1933 unsigned long flags;
1935 spin_lock_irqsave(&dwc->lock, flags);
1937 if (pm_runtime_suspended(dwc->dev))
1938 goto out;
1940 __dwc3_gadget_stop(dwc);
1942 out:
1943 dwc->gadget_driver = NULL;
1944 spin_unlock_irqrestore(&dwc->lock, flags);
1946 free_irq(dwc->irq_gadget, dwc->ev_buf);
1948 return 0;
1951 static void dwc3_gadget_set_speed(struct usb_gadget *g,
1952 enum usb_device_speed speed)
1954 struct dwc3 *dwc = gadget_to_dwc(g);
1955 unsigned long flags;
1956 u32 reg;
1958 spin_lock_irqsave(&dwc->lock, flags);
1959 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1960 reg &= ~(DWC3_DCFG_SPEED_MASK);
1963 * WORKAROUND: DWC3 revision < 2.20a have an issue
1964 * which would cause metastability state on Run/Stop
1965 * bit if we try to force the IP to USB2-only mode.
1967 * Because of that, we cannot configure the IP to any
1968 * speed other than the SuperSpeed
1970 * Refers to:
1972 * STAR#9000525659: Clock Domain Crossing on DCTL in
1973 * USB 2.0 Mode
1975 if (dwc->revision < DWC3_REVISION_220A &&
1976 !dwc->dis_metastability_quirk) {
1977 reg |= DWC3_DCFG_SUPERSPEED;
1978 } else {
1979 switch (speed) {
1980 case USB_SPEED_LOW:
1981 reg |= DWC3_DCFG_LOWSPEED;
1982 break;
1983 case USB_SPEED_FULL:
1984 reg |= DWC3_DCFG_FULLSPEED;
1985 break;
1986 case USB_SPEED_HIGH:
1987 reg |= DWC3_DCFG_HIGHSPEED;
1988 break;
1989 case USB_SPEED_SUPER:
1990 reg |= DWC3_DCFG_SUPERSPEED;
1991 break;
1992 case USB_SPEED_SUPER_PLUS:
1993 if (dwc3_is_usb31(dwc))
1994 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
1995 else
1996 reg |= DWC3_DCFG_SUPERSPEED;
1997 break;
1998 default:
1999 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2001 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2002 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2003 else
2004 reg |= DWC3_DCFG_SUPERSPEED;
2007 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2009 spin_unlock_irqrestore(&dwc->lock, flags);
2012 static const struct usb_gadget_ops dwc3_gadget_ops = {
2013 .get_frame = dwc3_gadget_get_frame,
2014 .wakeup = dwc3_gadget_wakeup,
2015 .set_selfpowered = dwc3_gadget_set_selfpowered,
2016 .pullup = dwc3_gadget_pullup,
2017 .udc_start = dwc3_gadget_start,
2018 .udc_stop = dwc3_gadget_stop,
2019 .udc_set_speed = dwc3_gadget_set_speed,
2022 /* -------------------------------------------------------------------------- */
2024 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2026 struct dwc3 *dwc = dep->dwc;
2028 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2029 dep->endpoint.maxburst = 1;
2030 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2031 if (!dep->direction)
2032 dwc->gadget.ep0 = &dep->endpoint;
2034 dep->endpoint.caps.type_control = true;
2036 return 0;
2039 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2041 struct dwc3 *dwc = dep->dwc;
2042 int mdwidth;
2043 int size;
2045 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2046 /* MDWIDTH is represented in bits, we need it in bytes */
2047 mdwidth /= 8;
2049 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2050 if (dwc3_is_usb31(dwc))
2051 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2052 else
2053 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2055 /* FIFO Depth is in MDWDITH bytes. Multiply */
2056 size *= mdwidth;
2059 * To meet performance requirement, a minimum TxFIFO size of 3x
2060 * MaxPacketSize is recommended for endpoints that support burst and a
2061 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2062 * support burst. Use those numbers and we can calculate the max packet
2063 * limit as below.
2065 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2066 size /= 3;
2067 else
2068 size /= 2;
2070 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2072 dep->endpoint.max_streams = 15;
2073 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2074 list_add_tail(&dep->endpoint.ep_list,
2075 &dwc->gadget.ep_list);
2076 dep->endpoint.caps.type_iso = true;
2077 dep->endpoint.caps.type_bulk = true;
2078 dep->endpoint.caps.type_int = true;
2080 return dwc3_alloc_trb_pool(dep);
2083 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2085 struct dwc3 *dwc = dep->dwc;
2086 int mdwidth;
2087 int size;
2089 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2091 /* MDWIDTH is represented in bits, convert to bytes */
2092 mdwidth /= 8;
2094 /* All OUT endpoints share a single RxFIFO space */
2095 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2096 if (dwc3_is_usb31(dwc))
2097 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2098 else
2099 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
2101 /* FIFO depth is in MDWDITH bytes */
2102 size *= mdwidth;
2105 * To meet performance requirement, a minimum recommended RxFIFO size
2106 * is defined as follow:
2107 * RxFIFO size >= (3 x MaxPacketSize) +
2108 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2110 * Then calculate the max packet limit as below.
2112 size -= (3 * 8) + 16;
2113 if (size < 0)
2114 size = 0;
2115 else
2116 size /= 3;
2118 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2119 dep->endpoint.max_streams = 15;
2120 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2121 list_add_tail(&dep->endpoint.ep_list,
2122 &dwc->gadget.ep_list);
2123 dep->endpoint.caps.type_iso = true;
2124 dep->endpoint.caps.type_bulk = true;
2125 dep->endpoint.caps.type_int = true;
2127 return dwc3_alloc_trb_pool(dep);
2130 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2132 struct dwc3_ep *dep;
2133 bool direction = epnum & 1;
2134 int ret;
2135 u8 num = epnum >> 1;
2137 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2138 if (!dep)
2139 return -ENOMEM;
2141 dep->dwc = dwc;
2142 dep->number = epnum;
2143 dep->direction = direction;
2144 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2145 dwc->eps[epnum] = dep;
2147 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2148 direction ? "in" : "out");
2150 dep->endpoint.name = dep->name;
2152 if (!(dep->number > 1)) {
2153 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2154 dep->endpoint.comp_desc = NULL;
2157 spin_lock_init(&dep->lock);
2159 if (num == 0)
2160 ret = dwc3_gadget_init_control_endpoint(dep);
2161 else if (direction)
2162 ret = dwc3_gadget_init_in_endpoint(dep);
2163 else
2164 ret = dwc3_gadget_init_out_endpoint(dep);
2166 if (ret)
2167 return ret;
2169 dep->endpoint.caps.dir_in = direction;
2170 dep->endpoint.caps.dir_out = !direction;
2172 INIT_LIST_HEAD(&dep->pending_list);
2173 INIT_LIST_HEAD(&dep->started_list);
2174 INIT_LIST_HEAD(&dep->cancelled_list);
2176 return 0;
2179 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2181 u8 epnum;
2183 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2185 for (epnum = 0; epnum < total; epnum++) {
2186 int ret;
2188 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2189 if (ret)
2190 return ret;
2193 return 0;
2196 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2198 struct dwc3_ep *dep;
2199 u8 epnum;
2201 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2202 dep = dwc->eps[epnum];
2203 if (!dep)
2204 continue;
2206 * Physical endpoints 0 and 1 are special; they form the
2207 * bi-directional USB endpoint 0.
2209 * For those two physical endpoints, we don't allocate a TRB
2210 * pool nor do we add them the endpoints list. Due to that, we
2211 * shouldn't do these two operations otherwise we would end up
2212 * with all sorts of bugs when removing dwc3.ko.
2214 if (epnum != 0 && epnum != 1) {
2215 dwc3_free_trb_pool(dep);
2216 list_del(&dep->endpoint.ep_list);
2219 kfree(dep);
2223 /* -------------------------------------------------------------------------- */
2225 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2226 struct dwc3_request *req, struct dwc3_trb *trb,
2227 const struct dwc3_event_depevt *event, int status, int chain)
2229 unsigned int count;
2231 dwc3_ep_inc_deq(dep);
2233 trace_dwc3_complete_trb(dep, trb);
2234 req->num_trbs--;
2237 * If we're in the middle of series of chained TRBs and we
2238 * receive a short transfer along the way, DWC3 will skip
2239 * through all TRBs including the last TRB in the chain (the
2240 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2241 * bit and SW has to do it manually.
2243 * We're going to do that here to avoid problems of HW trying
2244 * to use bogus TRBs for transfers.
2246 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2247 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2250 * If we're dealing with unaligned size OUT transfer, we will be left
2251 * with one TRB pending in the ring. We need to manually clear HWO bit
2252 * from that TRB.
2255 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
2256 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2257 return 1;
2260 count = trb->size & DWC3_TRB_SIZE_MASK;
2261 req->remaining += count;
2263 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2264 return 1;
2266 if (event->status & DEPEVT_STATUS_SHORT && !chain)
2267 return 1;
2269 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2270 (trb->ctrl & DWC3_TRB_CTRL_LST))
2271 return 1;
2273 return 0;
2276 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2277 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2278 int status)
2280 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2281 struct scatterlist *sg = req->sg;
2282 struct scatterlist *s;
2283 unsigned int pending = req->num_pending_sgs;
2284 unsigned int i;
2285 int ret = 0;
2287 for_each_sg(sg, s, pending, i) {
2288 trb = &dep->trb_pool[dep->trb_dequeue];
2290 req->sg = sg_next(s);
2291 req->num_pending_sgs--;
2293 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2294 trb, event, status, true);
2295 if (ret)
2296 break;
2299 return ret;
2302 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2303 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2304 int status)
2306 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2308 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2309 event, status, false);
2312 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2314 return req->num_pending_sgs == 0;
2317 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2318 const struct dwc3_event_depevt *event,
2319 struct dwc3_request *req, int status)
2321 int ret;
2323 if (req->num_pending_sgs)
2324 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2325 status);
2326 else
2327 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2328 status);
2330 if (req->needs_extra_trb) {
2331 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2332 status);
2333 req->needs_extra_trb = false;
2336 req->request.actual = req->request.length - req->remaining;
2338 if (!dwc3_gadget_ep_request_completed(req)) {
2339 __dwc3_gadget_kick_transfer(dep);
2340 goto out;
2343 dwc3_gadget_giveback(dep, req, status);
2345 out:
2346 return ret;
2349 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2350 const struct dwc3_event_depevt *event, int status)
2352 struct dwc3_request *req;
2353 struct dwc3_request *tmp;
2355 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2356 int ret;
2358 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2359 req, status);
2360 if (ret)
2361 break;
2365 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2366 const struct dwc3_event_depevt *event)
2368 dep->frame_number = event->parameters;
2371 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2372 const struct dwc3_event_depevt *event)
2374 struct dwc3 *dwc = dep->dwc;
2375 unsigned status = 0;
2376 bool stop = false;
2378 dwc3_gadget_endpoint_frame_from_event(dep, event);
2380 if (event->status & DEPEVT_STATUS_BUSERR)
2381 status = -ECONNRESET;
2383 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2384 status = -EXDEV;
2386 if (list_empty(&dep->started_list))
2387 stop = true;
2390 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2392 if (stop)
2393 dwc3_stop_active_transfer(dep, true, true);
2396 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2397 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2399 if (dwc->revision < DWC3_REVISION_183A) {
2400 u32 reg;
2401 int i;
2403 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2404 dep = dwc->eps[i];
2406 if (!(dep->flags & DWC3_EP_ENABLED))
2407 continue;
2409 if (!list_empty(&dep->started_list))
2410 return;
2413 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2414 reg |= dwc->u1u2;
2415 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2417 dwc->u1u2 = 0;
2421 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2422 const struct dwc3_event_depevt *event)
2424 dwc3_gadget_endpoint_frame_from_event(dep, event);
2425 __dwc3_gadget_start_isoc(dep);
2428 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2429 const struct dwc3_event_depevt *event)
2431 struct dwc3_ep *dep;
2432 u8 epnum = event->endpoint_number;
2433 u8 cmd;
2435 dep = dwc->eps[epnum];
2437 if (!(dep->flags & DWC3_EP_ENABLED)) {
2438 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2439 return;
2441 /* Handle only EPCMDCMPLT when EP disabled */
2442 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2443 return;
2446 if (epnum == 0 || epnum == 1) {
2447 dwc3_ep0_interrupt(dwc, event);
2448 return;
2451 switch (event->endpoint_event) {
2452 case DWC3_DEPEVT_XFERINPROGRESS:
2453 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2454 break;
2455 case DWC3_DEPEVT_XFERNOTREADY:
2456 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2457 break;
2458 case DWC3_DEPEVT_EPCMDCMPLT:
2459 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2461 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2462 dep->flags &= ~(DWC3_EP_END_TRANSFER_PENDING |
2463 DWC3_EP_TRANSFER_STARTED);
2464 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2466 break;
2467 case DWC3_DEPEVT_STREAMEVT:
2468 case DWC3_DEPEVT_XFERCOMPLETE:
2469 case DWC3_DEPEVT_RXTXFIFOEVT:
2470 break;
2474 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2476 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2477 spin_unlock(&dwc->lock);
2478 dwc->gadget_driver->disconnect(&dwc->gadget);
2479 spin_lock(&dwc->lock);
2483 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2485 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2486 spin_unlock(&dwc->lock);
2487 dwc->gadget_driver->suspend(&dwc->gadget);
2488 spin_lock(&dwc->lock);
2492 static void dwc3_resume_gadget(struct dwc3 *dwc)
2494 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2495 spin_unlock(&dwc->lock);
2496 dwc->gadget_driver->resume(&dwc->gadget);
2497 spin_lock(&dwc->lock);
2501 static void dwc3_reset_gadget(struct dwc3 *dwc)
2503 if (!dwc->gadget_driver)
2504 return;
2506 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2507 spin_unlock(&dwc->lock);
2508 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2509 spin_lock(&dwc->lock);
2513 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
2514 bool interrupt)
2516 struct dwc3 *dwc = dep->dwc;
2517 struct dwc3_gadget_ep_cmd_params params;
2518 u32 cmd;
2519 int ret;
2521 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2522 !dep->resource_index)
2523 return;
2526 * NOTICE: We are violating what the Databook says about the
2527 * EndTransfer command. Ideally we would _always_ wait for the
2528 * EndTransfer Command Completion IRQ, but that's causing too
2529 * much trouble synchronizing between us and gadget driver.
2531 * We have discussed this with the IP Provider and it was
2532 * suggested to giveback all requests here, but give HW some
2533 * extra time to synchronize with the interconnect. We're using
2534 * an arbitrary 100us delay for that.
2536 * Note also that a similar handling was tested by Synopsys
2537 * (thanks a lot Paul) and nothing bad has come out of it.
2538 * In short, what we're doing is:
2540 * - Issue EndTransfer WITH CMDIOC bit set
2541 * - Wait 100us
2543 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2544 * supports a mode to work around the above limitation. The
2545 * software can poll the CMDACT bit in the DEPCMD register
2546 * after issuing a EndTransfer command. This mode is enabled
2547 * by writing GUCTL2[14]. This polling is already done in the
2548 * dwc3_send_gadget_ep_cmd() function so if the mode is
2549 * enabled, the EndTransfer command will have completed upon
2550 * returning from this function and we don't need to delay for
2551 * 100us.
2553 * This mode is NOT available on the DWC_usb31 IP.
2556 cmd = DWC3_DEPCMD_ENDTRANSFER;
2557 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2558 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
2559 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2560 memset(&params, 0, sizeof(params));
2561 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
2562 WARN_ON_ONCE(ret);
2563 dep->resource_index = 0;
2565 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2566 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2567 udelay(100);
2571 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2573 u32 epnum;
2575 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2576 struct dwc3_ep *dep;
2577 int ret;
2579 dep = dwc->eps[epnum];
2580 if (!dep)
2581 continue;
2583 if (!(dep->flags & DWC3_EP_STALL))
2584 continue;
2586 dep->flags &= ~DWC3_EP_STALL;
2588 ret = dwc3_send_clear_stall_ep_cmd(dep);
2589 WARN_ON_ONCE(ret);
2593 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2595 int reg;
2597 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2598 reg &= ~DWC3_DCTL_INITU1ENA;
2599 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2601 reg &= ~DWC3_DCTL_INITU2ENA;
2602 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2604 dwc3_disconnect_gadget(dwc);
2606 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2607 dwc->setup_packet_pending = false;
2608 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2610 dwc->connected = false;
2613 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2615 u32 reg;
2617 dwc->connected = true;
2620 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2621 * would cause a missing Disconnect Event if there's a
2622 * pending Setup Packet in the FIFO.
2624 * There's no suggested workaround on the official Bug
2625 * report, which states that "unless the driver/application
2626 * is doing any special handling of a disconnect event,
2627 * there is no functional issue".
2629 * Unfortunately, it turns out that we _do_ some special
2630 * handling of a disconnect event, namely complete all
2631 * pending transfers, notify gadget driver of the
2632 * disconnection, and so on.
2634 * Our suggested workaround is to follow the Disconnect
2635 * Event steps here, instead, based on a setup_packet_pending
2636 * flag. Such flag gets set whenever we have a SETUP_PENDING
2637 * status for EP0 TRBs and gets cleared on XferComplete for the
2638 * same endpoint.
2640 * Refers to:
2642 * STAR#9000466709: RTL: Device : Disconnect event not
2643 * generated if setup packet pending in FIFO
2645 if (dwc->revision < DWC3_REVISION_188A) {
2646 if (dwc->setup_packet_pending)
2647 dwc3_gadget_disconnect_interrupt(dwc);
2650 dwc3_reset_gadget(dwc);
2652 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2653 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2654 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2655 dwc->test_mode = false;
2656 dwc3_clear_stall_all_ep(dwc);
2658 /* Reset device address to zero */
2659 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2660 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2661 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2664 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2666 struct dwc3_ep *dep;
2667 int ret;
2668 u32 reg;
2669 u8 speed;
2671 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2672 speed = reg & DWC3_DSTS_CONNECTSPD;
2673 dwc->speed = speed;
2676 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2677 * each time on Connect Done.
2679 * Currently we always use the reset value. If any platform
2680 * wants to set this to a different value, we need to add a
2681 * setting and update GCTL.RAMCLKSEL here.
2684 switch (speed) {
2685 case DWC3_DSTS_SUPERSPEED_PLUS:
2686 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2687 dwc->gadget.ep0->maxpacket = 512;
2688 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2689 break;
2690 case DWC3_DSTS_SUPERSPEED:
2692 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2693 * would cause a missing USB3 Reset event.
2695 * In such situations, we should force a USB3 Reset
2696 * event by calling our dwc3_gadget_reset_interrupt()
2697 * routine.
2699 * Refers to:
2701 * STAR#9000483510: RTL: SS : USB3 reset event may
2702 * not be generated always when the link enters poll
2704 if (dwc->revision < DWC3_REVISION_190A)
2705 dwc3_gadget_reset_interrupt(dwc);
2707 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2708 dwc->gadget.ep0->maxpacket = 512;
2709 dwc->gadget.speed = USB_SPEED_SUPER;
2710 break;
2711 case DWC3_DSTS_HIGHSPEED:
2712 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2713 dwc->gadget.ep0->maxpacket = 64;
2714 dwc->gadget.speed = USB_SPEED_HIGH;
2715 break;
2716 case DWC3_DSTS_FULLSPEED:
2717 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2718 dwc->gadget.ep0->maxpacket = 64;
2719 dwc->gadget.speed = USB_SPEED_FULL;
2720 break;
2721 case DWC3_DSTS_LOWSPEED:
2722 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2723 dwc->gadget.ep0->maxpacket = 8;
2724 dwc->gadget.speed = USB_SPEED_LOW;
2725 break;
2728 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2730 /* Enable USB2 LPM Capability */
2732 if ((dwc->revision > DWC3_REVISION_194A) &&
2733 (speed != DWC3_DSTS_SUPERSPEED) &&
2734 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2735 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2736 reg |= DWC3_DCFG_LPM_CAP;
2737 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2739 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2740 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2742 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2745 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2746 * DCFG.LPMCap is set, core responses with an ACK and the
2747 * BESL value in the LPM token is less than or equal to LPM
2748 * NYET threshold.
2750 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2751 && dwc->has_lpm_erratum,
2752 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2754 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2755 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2757 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2758 } else {
2759 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2760 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2761 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2764 dep = dwc->eps[0];
2765 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2766 if (ret) {
2767 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2768 return;
2771 dep = dwc->eps[1];
2772 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2773 if (ret) {
2774 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2775 return;
2779 * Configure PHY via GUSB3PIPECTLn if required.
2781 * Update GTXFIFOSIZn
2783 * In both cases reset values should be sufficient.
2787 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2790 * TODO take core out of low power mode when that's
2791 * implemented.
2794 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2795 spin_unlock(&dwc->lock);
2796 dwc->gadget_driver->resume(&dwc->gadget);
2797 spin_lock(&dwc->lock);
2801 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2802 unsigned int evtinfo)
2804 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2805 unsigned int pwropt;
2808 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2809 * Hibernation mode enabled which would show up when device detects
2810 * host-initiated U3 exit.
2812 * In that case, device will generate a Link State Change Interrupt
2813 * from U3 to RESUME which is only necessary if Hibernation is
2814 * configured in.
2816 * There are no functional changes due to such spurious event and we
2817 * just need to ignore it.
2819 * Refers to:
2821 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2822 * operational mode
2824 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2825 if ((dwc->revision < DWC3_REVISION_250A) &&
2826 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2827 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2828 (next == DWC3_LINK_STATE_RESUME)) {
2829 return;
2834 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2835 * on the link partner, the USB session might do multiple entry/exit
2836 * of low power states before a transfer takes place.
2838 * Due to this problem, we might experience lower throughput. The
2839 * suggested workaround is to disable DCTL[12:9] bits if we're
2840 * transitioning from U1/U2 to U0 and enable those bits again
2841 * after a transfer completes and there are no pending transfers
2842 * on any of the enabled endpoints.
2844 * This is the first half of that workaround.
2846 * Refers to:
2848 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2849 * core send LGO_Ux entering U0
2851 if (dwc->revision < DWC3_REVISION_183A) {
2852 if (next == DWC3_LINK_STATE_U0) {
2853 u32 u1u2;
2854 u32 reg;
2856 switch (dwc->link_state) {
2857 case DWC3_LINK_STATE_U1:
2858 case DWC3_LINK_STATE_U2:
2859 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2860 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2861 | DWC3_DCTL_ACCEPTU2ENA
2862 | DWC3_DCTL_INITU1ENA
2863 | DWC3_DCTL_ACCEPTU1ENA);
2865 if (!dwc->u1u2)
2866 dwc->u1u2 = reg & u1u2;
2868 reg &= ~u1u2;
2870 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2871 break;
2872 default:
2873 /* do nothing */
2874 break;
2879 switch (next) {
2880 case DWC3_LINK_STATE_U1:
2881 if (dwc->speed == USB_SPEED_SUPER)
2882 dwc3_suspend_gadget(dwc);
2883 break;
2884 case DWC3_LINK_STATE_U2:
2885 case DWC3_LINK_STATE_U3:
2886 dwc3_suspend_gadget(dwc);
2887 break;
2888 case DWC3_LINK_STATE_RESUME:
2889 dwc3_resume_gadget(dwc);
2890 break;
2891 default:
2892 /* do nothing */
2893 break;
2896 dwc->link_state = next;
2899 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2900 unsigned int evtinfo)
2902 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2904 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2905 dwc3_suspend_gadget(dwc);
2907 dwc->link_state = next;
2910 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2911 unsigned int evtinfo)
2913 unsigned int is_ss = evtinfo & BIT(4);
2916 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2917 * have a known issue which can cause USB CV TD.9.23 to fail
2918 * randomly.
2920 * Because of this issue, core could generate bogus hibernation
2921 * events which SW needs to ignore.
2923 * Refers to:
2925 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2926 * Device Fallback from SuperSpeed
2928 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2929 return;
2931 /* enter hibernation here */
2934 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2935 const struct dwc3_event_devt *event)
2937 switch (event->type) {
2938 case DWC3_DEVICE_EVENT_DISCONNECT:
2939 dwc3_gadget_disconnect_interrupt(dwc);
2940 break;
2941 case DWC3_DEVICE_EVENT_RESET:
2942 dwc3_gadget_reset_interrupt(dwc);
2943 break;
2944 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2945 dwc3_gadget_conndone_interrupt(dwc);
2946 break;
2947 case DWC3_DEVICE_EVENT_WAKEUP:
2948 dwc3_gadget_wakeup_interrupt(dwc);
2949 break;
2950 case DWC3_DEVICE_EVENT_HIBER_REQ:
2951 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2952 "unexpected hibernation event\n"))
2953 break;
2955 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2956 break;
2957 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2958 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2959 break;
2960 case DWC3_DEVICE_EVENT_EOPF:
2961 /* It changed to be suspend event for version 2.30a and above */
2962 if (dwc->revision >= DWC3_REVISION_230A) {
2964 * Ignore suspend event until the gadget enters into
2965 * USB_STATE_CONFIGURED state.
2967 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2968 dwc3_gadget_suspend_interrupt(dwc,
2969 event->event_info);
2971 break;
2972 case DWC3_DEVICE_EVENT_SOF:
2973 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2974 case DWC3_DEVICE_EVENT_CMD_CMPL:
2975 case DWC3_DEVICE_EVENT_OVERFLOW:
2976 break;
2977 default:
2978 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2982 static void dwc3_process_event_entry(struct dwc3 *dwc,
2983 const union dwc3_event *event)
2985 trace_dwc3_event(event->raw, dwc);
2987 if (!event->type.is_devspec)
2988 dwc3_endpoint_interrupt(dwc, &event->depevt);
2989 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
2990 dwc3_gadget_interrupt(dwc, &event->devt);
2991 else
2992 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2995 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
2997 struct dwc3 *dwc = evt->dwc;
2998 irqreturn_t ret = IRQ_NONE;
2999 int left;
3000 u32 reg;
3002 left = evt->count;
3004 if (!(evt->flags & DWC3_EVENT_PENDING))
3005 return IRQ_NONE;
3007 while (left > 0) {
3008 union dwc3_event event;
3010 event.raw = *(u32 *) (evt->cache + evt->lpos);
3012 dwc3_process_event_entry(dwc, &event);
3015 * FIXME we wrap around correctly to the next entry as
3016 * almost all entries are 4 bytes in size. There is one
3017 * entry which has 12 bytes which is a regular entry
3018 * followed by 8 bytes data. ATM I don't know how
3019 * things are organized if we get next to the a
3020 * boundary so I worry about that once we try to handle
3021 * that.
3023 evt->lpos = (evt->lpos + 4) % evt->length;
3024 left -= 4;
3027 evt->count = 0;
3028 evt->flags &= ~DWC3_EVENT_PENDING;
3029 ret = IRQ_HANDLED;
3031 /* Unmask interrupt */
3032 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3033 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3034 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3036 if (dwc->imod_interval) {
3037 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3038 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3041 return ret;
3044 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3046 struct dwc3_event_buffer *evt = _evt;
3047 struct dwc3 *dwc = evt->dwc;
3048 unsigned long flags;
3049 irqreturn_t ret = IRQ_NONE;
3051 spin_lock_irqsave(&dwc->lock, flags);
3052 ret = dwc3_process_event_buf(evt);
3053 spin_unlock_irqrestore(&dwc->lock, flags);
3055 return ret;
3058 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3060 struct dwc3 *dwc = evt->dwc;
3061 u32 amount;
3062 u32 count;
3063 u32 reg;
3065 if (pm_runtime_suspended(dwc->dev)) {
3066 pm_runtime_get(dwc->dev);
3067 disable_irq_nosync(dwc->irq_gadget);
3068 dwc->pending_events = true;
3069 return IRQ_HANDLED;
3073 * With PCIe legacy interrupt, test shows that top-half irq handler can
3074 * be called again after HW interrupt deassertion. Check if bottom-half
3075 * irq event handler completes before caching new event to prevent
3076 * losing events.
3078 if (evt->flags & DWC3_EVENT_PENDING)
3079 return IRQ_HANDLED;
3081 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3082 count &= DWC3_GEVNTCOUNT_MASK;
3083 if (!count)
3084 return IRQ_NONE;
3086 evt->count = count;
3087 evt->flags |= DWC3_EVENT_PENDING;
3089 /* Mask interrupt */
3090 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3091 reg |= DWC3_GEVNTSIZ_INTMASK;
3092 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3094 amount = min(count, evt->length - evt->lpos);
3095 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3097 if (amount < count)
3098 memcpy(evt->cache, evt->buf, count - amount);
3100 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3102 return IRQ_WAKE_THREAD;
3105 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3107 struct dwc3_event_buffer *evt = _evt;
3109 return dwc3_check_event_buf(evt);
3112 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3114 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3115 int irq;
3117 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3118 if (irq > 0)
3119 goto out;
3121 if (irq == -EPROBE_DEFER)
3122 goto out;
3124 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3125 if (irq > 0)
3126 goto out;
3128 if (irq == -EPROBE_DEFER)
3129 goto out;
3131 irq = platform_get_irq(dwc3_pdev, 0);
3132 if (irq > 0)
3133 goto out;
3135 if (irq != -EPROBE_DEFER)
3136 dev_err(dwc->dev, "missing peripheral IRQ\n");
3138 if (!irq)
3139 irq = -EINVAL;
3141 out:
3142 return irq;
3146 * dwc3_gadget_init - initializes gadget related registers
3147 * @dwc: pointer to our controller context structure
3149 * Returns 0 on success otherwise negative errno.
3151 int dwc3_gadget_init(struct dwc3 *dwc)
3153 int ret;
3154 int irq;
3156 irq = dwc3_gadget_get_irq(dwc);
3157 if (irq < 0) {
3158 ret = irq;
3159 goto err0;
3162 dwc->irq_gadget = irq;
3164 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3165 sizeof(*dwc->ep0_trb) * 2,
3166 &dwc->ep0_trb_addr, GFP_KERNEL);
3167 if (!dwc->ep0_trb) {
3168 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3169 ret = -ENOMEM;
3170 goto err0;
3173 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3174 if (!dwc->setup_buf) {
3175 ret = -ENOMEM;
3176 goto err1;
3179 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3180 &dwc->bounce_addr, GFP_KERNEL);
3181 if (!dwc->bounce) {
3182 ret = -ENOMEM;
3183 goto err2;
3186 init_completion(&dwc->ep0_in_setup);
3188 dwc->gadget.ops = &dwc3_gadget_ops;
3189 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3190 dwc->gadget.sg_supported = true;
3191 dwc->gadget.name = "dwc3-gadget";
3194 * FIXME We might be setting max_speed to <SUPER, however versions
3195 * <2.20a of dwc3 have an issue with metastability (documented
3196 * elsewhere in this driver) which tells us we can't set max speed to
3197 * anything lower than SUPER.
3199 * Because gadget.max_speed is only used by composite.c and function
3200 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3201 * to happen so we avoid sending SuperSpeed Capability descriptor
3202 * together with our BOS descriptor as that could confuse host into
3203 * thinking we can handle super speed.
3205 * Note that, in fact, we won't even support GetBOS requests when speed
3206 * is less than super speed because we don't have means, yet, to tell
3207 * composite.c that we are USB 2.0 + LPM ECN.
3209 if (dwc->revision < DWC3_REVISION_220A &&
3210 !dwc->dis_metastability_quirk)
3211 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3212 dwc->revision);
3214 dwc->gadget.max_speed = dwc->maximum_speed;
3217 * REVISIT: Here we should clear all pending IRQs to be
3218 * sure we're starting from a well known location.
3221 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3222 if (ret)
3223 goto err3;
3225 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3226 if (ret) {
3227 dev_err(dwc->dev, "failed to register udc\n");
3228 goto err4;
3231 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3233 return 0;
3235 err4:
3236 dwc3_gadget_free_endpoints(dwc);
3238 err3:
3239 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3240 dwc->bounce_addr);
3242 err2:
3243 kfree(dwc->setup_buf);
3245 err1:
3246 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3247 dwc->ep0_trb, dwc->ep0_trb_addr);
3249 err0:
3250 return ret;
3253 /* -------------------------------------------------------------------------- */
3255 void dwc3_gadget_exit(struct dwc3 *dwc)
3257 usb_del_gadget_udc(&dwc->gadget);
3258 dwc3_gadget_free_endpoints(dwc);
3259 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3260 dwc->bounce_addr);
3261 kfree(dwc->setup_buf);
3262 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3263 dwc->ep0_trb, dwc->ep0_trb_addr);
3266 int dwc3_gadget_suspend(struct dwc3 *dwc)
3268 if (!dwc->gadget_driver)
3269 return 0;
3271 dwc3_gadget_run_stop(dwc, false, false);
3272 dwc3_disconnect_gadget(dwc);
3273 __dwc3_gadget_stop(dwc);
3275 return 0;
3278 int dwc3_gadget_resume(struct dwc3 *dwc)
3280 int ret;
3282 if (!dwc->gadget_driver)
3283 return 0;
3285 ret = __dwc3_gadget_start(dwc);
3286 if (ret < 0)
3287 goto err0;
3289 ret = dwc3_gadget_run_stop(dwc, true, false);
3290 if (ret < 0)
3291 goto err1;
3293 return 0;
3295 err1:
3296 __dwc3_gadget_stop(dwc);
3298 err0:
3299 return ret;
3302 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3304 if (dwc->pending_events) {
3305 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3306 dwc->pending_events = false;
3307 enable_irq(dwc->irq_gadget);