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