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