]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/usb/dwc3/gadget.c
usb: dwc3: let non-DT platforms pass tx-fifo-resize flag;
[people/arne_f/kernel.git] / drivers / usb / dwc3 / gadget.c
CommitLineData
72246da4
FB
1/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
72246da4
FB
5 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
5945f789
FB
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
72246da4 12 *
5945f789
FB
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
72246da4
FB
17 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
33#include "core.h"
34#include "gadget.h"
35#include "io.h"
36
04a9bfcd
FB
37/**
38 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
39 * @dwc: pointer to our context structure
40 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
41 *
42 * Caller should take care of locking. This function will
43 * return 0 on success or -EINVAL if wrong Test Selector
44 * is passed
45 */
46int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
47{
48 u32 reg;
49
50 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
51 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
52
53 switch (mode) {
54 case TEST_J:
55 case TEST_K:
56 case TEST_SE0_NAK:
57 case TEST_PACKET:
58 case TEST_FORCE_EN:
59 reg |= mode << 1;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66
67 return 0;
68}
69
8598bde7
FB
70/**
71 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
72 * @dwc: pointer to our context structure
73 * @state: the state to put link into
74 *
75 * Caller should take care of locking. This function will
aee63e3c 76 * return 0 on success or -ETIMEDOUT.
8598bde7
FB
77 */
78int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
79{
aee63e3c 80 int retries = 10000;
8598bde7
FB
81 u32 reg;
82
802fde98
PZ
83 /*
84 * Wait until device controller is ready. Only applies to 1.94a and
85 * later RTL.
86 */
87 if (dwc->revision >= DWC3_REVISION_194A) {
88 while (--retries) {
89 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
90 if (reg & DWC3_DSTS_DCNRD)
91 udelay(5);
92 else
93 break;
94 }
95
96 if (retries <= 0)
97 return -ETIMEDOUT;
98 }
99
8598bde7
FB
100 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
101 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
102
103 /* set requested state */
104 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
105 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
106
802fde98
PZ
107 /*
108 * The following code is racy when called from dwc3_gadget_wakeup,
109 * and is not needed, at least on newer versions
110 */
111 if (dwc->revision >= DWC3_REVISION_194A)
112 return 0;
113
8598bde7 114 /* wait for a change in DSTS */
aed430e5 115 retries = 10000;
8598bde7
FB
116 while (--retries) {
117 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
118
8598bde7
FB
119 if (DWC3_DSTS_USBLNKST(reg) == state)
120 return 0;
121
aee63e3c 122 udelay(5);
8598bde7
FB
123 }
124
125 dev_vdbg(dwc->dev, "link state change request timed out\n");
126
127 return -ETIMEDOUT;
128}
129
457e84b6
FB
130/**
131 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
132 * @dwc: pointer to our context structure
133 *
134 * This function will a best effort FIFO allocation in order
135 * to improve FIFO usage and throughput, while still allowing
136 * us to enable as many endpoints as possible.
137 *
138 * Keep in mind that this operation will be highly dependent
139 * on the configured size for RAM1 - which contains TxFifo -,
140 * the amount of endpoints enabled on coreConsultant tool, and
141 * the width of the Master Bus.
142 *
143 * In the ideal world, we would always be able to satisfy the
144 * following equation:
145 *
146 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
147 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
148 *
149 * Unfortunately, due to many variables that's not always the case.
150 */
151int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
152{
153 int last_fifo_depth = 0;
154 int ram1_depth;
155 int fifo_size;
156 int mdwidth;
157 int num;
158
159 if (!dwc->needs_fifo_resize)
160 return 0;
161
162 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
163 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
164
165 /* MDWIDTH is represented in bits, we need it in bytes */
166 mdwidth >>= 3;
167
168 /*
169 * FIXME For now we will only allocate 1 wMaxPacketSize space
170 * for each enabled endpoint, later patches will come to
171 * improve this algorithm so that we better use the internal
172 * FIFO space
173 */
174 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
175 struct dwc3_ep *dep = dwc->eps[num];
176 int fifo_number = dep->number >> 1;
2e81c36a 177 int mult = 1;
457e84b6
FB
178 int tmp;
179
180 if (!(dep->number & 1))
181 continue;
182
183 if (!(dep->flags & DWC3_EP_ENABLED))
184 continue;
185
16e78db7
IS
186 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
187 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
2e81c36a
FB
188 mult = 3;
189
190 /*
191 * REVISIT: the following assumes we will always have enough
192 * space available on the FIFO RAM for all possible use cases.
193 * Make sure that's true somehow and change FIFO allocation
194 * accordingly.
195 *
196 * If we have Bulk or Isochronous endpoints, we want
197 * them to be able to be very, very fast. So we're giving
198 * those endpoints a fifo_size which is enough for 3 full
199 * packets
200 */
201 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
457e84b6
FB
202 tmp += mdwidth;
203
204 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
2e81c36a 205
457e84b6
FB
206 fifo_size |= (last_fifo_depth << 16);
207
208 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
209 dep->name, last_fifo_depth, fifo_size & 0xffff);
210
211 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
212 fifo_size);
213
214 last_fifo_depth += (fifo_size & 0xffff);
215 }
216
217 return 0;
218}
219
72246da4
FB
220void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
221 int status)
222{
223 struct dwc3 *dwc = dep->dwc;
e5ba5ec8 224 int i;
72246da4
FB
225
226 if (req->queued) {
e5ba5ec8
PA
227 i = 0;
228 do {
eeb720fb 229 dep->busy_slot++;
e5ba5ec8
PA
230 /*
231 * Skip LINK TRB. We can't use req->trb and check for
232 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
233 * just completed (not the LINK TRB).
234 */
235 if (((dep->busy_slot & DWC3_TRB_MASK) ==
236 DWC3_TRB_NUM- 1) &&
16e78db7 237 usb_endpoint_xfer_isoc(dep->endpoint.desc))
e5ba5ec8
PA
238 dep->busy_slot++;
239 } while(++i < req->request.num_mapped_sgs);
c9fda7d6 240 req->queued = false;
72246da4
FB
241 }
242 list_del(&req->list);
eeb720fb 243 req->trb = NULL;
72246da4
FB
244
245 if (req->request.status == -EINPROGRESS)
246 req->request.status = status;
247
0416e494
PA
248 if (dwc->ep0_bounced && dep->number == 0)
249 dwc->ep0_bounced = false;
250 else
251 usb_gadget_unmap_request(&dwc->gadget, &req->request,
252 req->direction);
72246da4
FB
253
254 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
255 req, dep->name, req->request.actual,
256 req->request.length, status);
257
258 spin_unlock(&dwc->lock);
0fc9a1be 259 req->request.complete(&dep->endpoint, &req->request);
72246da4
FB
260 spin_lock(&dwc->lock);
261}
262
263static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
264{
265 switch (cmd) {
266 case DWC3_DEPCMD_DEPSTARTCFG:
267 return "Start New Configuration";
268 case DWC3_DEPCMD_ENDTRANSFER:
269 return "End Transfer";
270 case DWC3_DEPCMD_UPDATETRANSFER:
271 return "Update Transfer";
272 case DWC3_DEPCMD_STARTTRANSFER:
273 return "Start Transfer";
274 case DWC3_DEPCMD_CLEARSTALL:
275 return "Clear Stall";
276 case DWC3_DEPCMD_SETSTALL:
277 return "Set Stall";
802fde98
PZ
278 case DWC3_DEPCMD_GETEPSTATE:
279 return "Get Endpoint State";
72246da4
FB
280 case DWC3_DEPCMD_SETTRANSFRESOURCE:
281 return "Set Endpoint Transfer Resource";
282 case DWC3_DEPCMD_SETEPCONFIG:
283 return "Set Endpoint Configuration";
284 default:
285 return "UNKNOWN command";
286 }
287}
288
b09bb642
FB
289int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
290{
291 u32 timeout = 500;
292 u32 reg;
293
294 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
295 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
296
297 do {
298 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
299 if (!(reg & DWC3_DGCMD_CMDACT)) {
300 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
301 DWC3_DGCMD_STATUS(reg));
302 return 0;
303 }
304
305 /*
306 * We can't sleep here, because it's also called from
307 * interrupt context.
308 */
309 timeout--;
310 if (!timeout)
311 return -ETIMEDOUT;
312 udelay(1);
313 } while (1);
314}
315
72246da4
FB
316int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
317 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
318{
319 struct dwc3_ep *dep = dwc->eps[ep];
61d58242 320 u32 timeout = 500;
72246da4
FB
321 u32 reg;
322
323 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
324 dep->name,
dc1c70a7
FB
325 dwc3_gadget_ep_cmd_string(cmd), params->param0,
326 params->param1, params->param2);
72246da4 327
dc1c70a7
FB
328 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
329 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
330 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
72246da4
FB
331
332 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
333 do {
334 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
164f6e14
FB
336 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
337 DWC3_DEPCMD_STATUS(reg));
72246da4
FB
338 return 0;
339 }
340
341 /*
72246da4
FB
342 * We can't sleep here, because it is also called from
343 * interrupt context.
344 */
345 timeout--;
346 if (!timeout)
347 return -ETIMEDOUT;
348
61d58242 349 udelay(1);
72246da4
FB
350 } while (1);
351}
352
353static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
f6bafc6a 354 struct dwc3_trb *trb)
72246da4 355{
c439ef87 356 u32 offset = (char *) trb - (char *) dep->trb_pool;
72246da4
FB
357
358 return dep->trb_pool_dma + offset;
359}
360
361static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
362{
363 struct dwc3 *dwc = dep->dwc;
364
365 if (dep->trb_pool)
366 return 0;
367
368 if (dep->number == 0 || dep->number == 1)
369 return 0;
370
371 dep->trb_pool = dma_alloc_coherent(dwc->dev,
372 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
373 &dep->trb_pool_dma, GFP_KERNEL);
374 if (!dep->trb_pool) {
375 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
376 dep->name);
377 return -ENOMEM;
378 }
379
380 return 0;
381}
382
383static void dwc3_free_trb_pool(struct dwc3_ep *dep)
384{
385 struct dwc3 *dwc = dep->dwc;
386
387 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
388 dep->trb_pool, dep->trb_pool_dma);
389
390 dep->trb_pool = NULL;
391 dep->trb_pool_dma = 0;
392}
393
394static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
395{
396 struct dwc3_gadget_ep_cmd_params params;
397 u32 cmd;
398
399 memset(&params, 0x00, sizeof(params));
400
401 if (dep->number != 1) {
402 cmd = DWC3_DEPCMD_DEPSTARTCFG;
403 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
b23c8439
PZ
404 if (dep->number > 1) {
405 if (dwc->start_config_issued)
406 return 0;
407 dwc->start_config_issued = true;
72246da4 408 cmd |= DWC3_DEPCMD_PARAM(2);
b23c8439 409 }
72246da4
FB
410
411 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
412 }
413
414 return 0;
415}
416
417static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
c90bfaec 418 const struct usb_endpoint_descriptor *desc,
4b345c9a
FB
419 const struct usb_ss_ep_comp_descriptor *comp_desc,
420 bool ignore)
72246da4
FB
421{
422 struct dwc3_gadget_ep_cmd_params params;
423
424 memset(&params, 0x00, sizeof(params));
425
dc1c70a7 426 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
d2e9a13a
CP
427 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
428
429 /* Burst size is only needed in SuperSpeed mode */
430 if (dwc->gadget.speed == USB_SPEED_SUPER) {
431 u32 burst = dep->endpoint.maxburst - 1;
432
433 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
434 }
72246da4 435
4b345c9a
FB
436 if (ignore)
437 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
438
dc1c70a7
FB
439 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
440 | DWC3_DEPCFG_XFER_NOT_READY_EN;
72246da4 441
18b7ede5 442 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
dc1c70a7
FB
443 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
444 | DWC3_DEPCFG_STREAM_EVENT_EN;
879631aa
FB
445 dep->stream_capable = true;
446 }
447
72246da4 448 if (usb_endpoint_xfer_isoc(desc))
dc1c70a7 449 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
72246da4
FB
450
451 /*
452 * We are doing 1:1 mapping for endpoints, meaning
453 * Physical Endpoints 2 maps to Logical Endpoint 2 and
454 * so on. We consider the direction bit as part of the physical
455 * endpoint number. So USB endpoint 0x81 is 0x03.
456 */
dc1c70a7 457 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
72246da4
FB
458
459 /*
460 * We must use the lower 16 TX FIFOs even though
461 * HW might have more
462 */
463 if (dep->direction)
dc1c70a7 464 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
72246da4
FB
465
466 if (desc->bInterval) {
dc1c70a7 467 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
72246da4
FB
468 dep->interval = 1 << (desc->bInterval - 1);
469 }
470
471 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
472 DWC3_DEPCMD_SETEPCONFIG, &params);
473}
474
475static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
476{
477 struct dwc3_gadget_ep_cmd_params params;
478
479 memset(&params, 0x00, sizeof(params));
480
dc1c70a7 481 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
72246da4
FB
482
483 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
484 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
485}
486
487/**
488 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
489 * @dep: endpoint to be initialized
490 * @desc: USB Endpoint Descriptor
491 *
492 * Caller should take care of locking
493 */
494static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
c90bfaec 495 const struct usb_endpoint_descriptor *desc,
4b345c9a
FB
496 const struct usb_ss_ep_comp_descriptor *comp_desc,
497 bool ignore)
72246da4
FB
498{
499 struct dwc3 *dwc = dep->dwc;
500 u32 reg;
501 int ret = -ENOMEM;
502
503 if (!(dep->flags & DWC3_EP_ENABLED)) {
504 ret = dwc3_gadget_start_config(dwc, dep);
505 if (ret)
506 return ret;
507 }
508
4b345c9a 509 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
72246da4
FB
510 if (ret)
511 return ret;
512
513 if (!(dep->flags & DWC3_EP_ENABLED)) {
f6bafc6a
FB
514 struct dwc3_trb *trb_st_hw;
515 struct dwc3_trb *trb_link;
72246da4
FB
516
517 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
518 if (ret)
519 return ret;
520
16e78db7 521 dep->endpoint.desc = desc;
c90bfaec 522 dep->comp_desc = comp_desc;
72246da4
FB
523 dep->type = usb_endpoint_type(desc);
524 dep->flags |= DWC3_EP_ENABLED;
525
526 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
527 reg |= DWC3_DALEPENA_EP(dep->number);
528 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
529
530 if (!usb_endpoint_xfer_isoc(desc))
531 return 0;
532
533 memset(&trb_link, 0, sizeof(trb_link));
534
1d046793 535 /* Link TRB for ISOC. The HWO bit is never reset */
72246da4
FB
536 trb_st_hw = &dep->trb_pool[0];
537
f6bafc6a 538 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
72246da4 539
f6bafc6a
FB
540 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
541 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
542 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
543 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
72246da4
FB
544 }
545
546 return 0;
547}
548
624407f9
SAS
549static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
550static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
72246da4
FB
551{
552 struct dwc3_request *req;
553
ea53b882 554 if (!list_empty(&dep->req_queued)) {
624407f9
SAS
555 dwc3_stop_active_transfer(dwc, dep->number);
556
57911504 557 /* - giveback all requests to gadget driver */
1591633e
PA
558 while (!list_empty(&dep->req_queued)) {
559 req = next_request(&dep->req_queued);
560
561 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
562 }
ea53b882
FB
563 }
564
72246da4
FB
565 while (!list_empty(&dep->request_list)) {
566 req = next_request(&dep->request_list);
567
624407f9 568 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
72246da4 569 }
72246da4
FB
570}
571
572/**
573 * __dwc3_gadget_ep_disable - Disables a HW endpoint
574 * @dep: the endpoint to disable
575 *
624407f9
SAS
576 * This function also removes requests which are currently processed ny the
577 * hardware and those which are not yet scheduled.
578 * Caller should take care of locking.
72246da4 579 */
72246da4
FB
580static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
581{
582 struct dwc3 *dwc = dep->dwc;
583 u32 reg;
584
624407f9 585 dwc3_remove_requests(dwc, dep);
72246da4
FB
586
587 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
588 reg &= ~DWC3_DALEPENA_EP(dep->number);
589 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
590
879631aa 591 dep->stream_capable = false;
f9c56cdd 592 dep->endpoint.desc = NULL;
c90bfaec 593 dep->comp_desc = NULL;
72246da4 594 dep->type = 0;
879631aa 595 dep->flags = 0;
72246da4
FB
596
597 return 0;
598}
599
600/* -------------------------------------------------------------------------- */
601
602static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
603 const struct usb_endpoint_descriptor *desc)
604{
605 return -EINVAL;
606}
607
608static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
609{
610 return -EINVAL;
611}
612
613/* -------------------------------------------------------------------------- */
614
615static int dwc3_gadget_ep_enable(struct usb_ep *ep,
616 const struct usb_endpoint_descriptor *desc)
617{
618 struct dwc3_ep *dep;
619 struct dwc3 *dwc;
620 unsigned long flags;
621 int ret;
622
623 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
624 pr_debug("dwc3: invalid parameters\n");
625 return -EINVAL;
626 }
627
628 if (!desc->wMaxPacketSize) {
629 pr_debug("dwc3: missing wMaxPacketSize\n");
630 return -EINVAL;
631 }
632
633 dep = to_dwc3_ep(ep);
634 dwc = dep->dwc;
635
c6f83f38
FB
636 if (dep->flags & DWC3_EP_ENABLED) {
637 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
638 dep->name);
639 return 0;
640 }
641
72246da4
FB
642 switch (usb_endpoint_type(desc)) {
643 case USB_ENDPOINT_XFER_CONTROL:
27a78d6a 644 strlcat(dep->name, "-control", sizeof(dep->name));
72246da4
FB
645 break;
646 case USB_ENDPOINT_XFER_ISOC:
27a78d6a 647 strlcat(dep->name, "-isoc", sizeof(dep->name));
72246da4
FB
648 break;
649 case USB_ENDPOINT_XFER_BULK:
27a78d6a 650 strlcat(dep->name, "-bulk", sizeof(dep->name));
72246da4
FB
651 break;
652 case USB_ENDPOINT_XFER_INT:
27a78d6a 653 strlcat(dep->name, "-int", sizeof(dep->name));
72246da4
FB
654 break;
655 default:
656 dev_err(dwc->dev, "invalid endpoint transfer type\n");
657 }
658
72246da4
FB
659 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
660
661 spin_lock_irqsave(&dwc->lock, flags);
4b345c9a 662 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
72246da4
FB
663 spin_unlock_irqrestore(&dwc->lock, flags);
664
665 return ret;
666}
667
668static int dwc3_gadget_ep_disable(struct usb_ep *ep)
669{
670 struct dwc3_ep *dep;
671 struct dwc3 *dwc;
672 unsigned long flags;
673 int ret;
674
675 if (!ep) {
676 pr_debug("dwc3: invalid parameters\n");
677 return -EINVAL;
678 }
679
680 dep = to_dwc3_ep(ep);
681 dwc = dep->dwc;
682
683 if (!(dep->flags & DWC3_EP_ENABLED)) {
684 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
685 dep->name);
686 return 0;
687 }
688
689 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
690 dep->number >> 1,
691 (dep->number & 1) ? "in" : "out");
692
693 spin_lock_irqsave(&dwc->lock, flags);
694 ret = __dwc3_gadget_ep_disable(dep);
695 spin_unlock_irqrestore(&dwc->lock, flags);
696
697 return ret;
698}
699
700static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
701 gfp_t gfp_flags)
702{
703 struct dwc3_request *req;
704 struct dwc3_ep *dep = to_dwc3_ep(ep);
705 struct dwc3 *dwc = dep->dwc;
706
707 req = kzalloc(sizeof(*req), gfp_flags);
708 if (!req) {
709 dev_err(dwc->dev, "not enough memory\n");
710 return NULL;
711 }
712
713 req->epnum = dep->number;
714 req->dep = dep;
72246da4
FB
715
716 return &req->request;
717}
718
719static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
720 struct usb_request *request)
721{
722 struct dwc3_request *req = to_dwc3_request(request);
723
724 kfree(req);
725}
726
c71fc37c
FB
727/**
728 * dwc3_prepare_one_trb - setup one TRB from one request
729 * @dep: endpoint for which this request is prepared
730 * @req: dwc3_request pointer
731 */
68e823e2 732static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
eeb720fb 733 struct dwc3_request *req, dma_addr_t dma,
e5ba5ec8 734 unsigned length, unsigned last, unsigned chain, unsigned node)
c71fc37c 735{
eeb720fb 736 struct dwc3 *dwc = dep->dwc;
f6bafc6a 737 struct dwc3_trb *trb;
c71fc37c 738
eeb720fb
FB
739 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
740 dep->name, req, (unsigned long long) dma,
741 length, last ? " last" : "",
742 chain ? " chain" : "");
743
c71fc37c 744 /* Skip the LINK-TRB on ISOC */
915e202a 745 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
16e78db7 746 usb_endpoint_xfer_isoc(dep->endpoint.desc))
915e202a
PA
747 dep->free_slot++;
748
749 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
c71fc37c 750
eeb720fb
FB
751 if (!req->trb) {
752 dwc3_gadget_move_request_queued(req);
f6bafc6a
FB
753 req->trb = trb;
754 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
e5ba5ec8 755 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
eeb720fb 756 }
c71fc37c 757
e5ba5ec8
PA
758 dep->free_slot++;
759
f6bafc6a
FB
760 trb->size = DWC3_TRB_SIZE_LENGTH(length);
761 trb->bpl = lower_32_bits(dma);
762 trb->bph = upper_32_bits(dma);
c71fc37c 763
16e78db7 764 switch (usb_endpoint_type(dep->endpoint.desc)) {
c71fc37c 765 case USB_ENDPOINT_XFER_CONTROL:
f6bafc6a 766 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
c71fc37c
FB
767 break;
768
769 case USB_ENDPOINT_XFER_ISOC:
e5ba5ec8
PA
770 if (!node)
771 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
772 else
773 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
c71fc37c 774
e5ba5ec8 775 if (!req->request.no_interrupt && !chain)
f6bafc6a 776 trb->ctrl |= DWC3_TRB_CTRL_IOC;
c71fc37c
FB
777 break;
778
779 case USB_ENDPOINT_XFER_BULK:
780 case USB_ENDPOINT_XFER_INT:
f6bafc6a 781 trb->ctrl = DWC3_TRBCTL_NORMAL;
c71fc37c
FB
782 break;
783 default:
784 /*
785 * This is only possible with faulty memory because we
786 * checked it already :)
787 */
788 BUG();
789 }
790
16e78db7 791 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
f6bafc6a
FB
792 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
793 trb->ctrl |= DWC3_TRB_CTRL_CSP;
e5ba5ec8
PA
794 } else if (last) {
795 trb->ctrl |= DWC3_TRB_CTRL_LST;
f6bafc6a 796 }
c71fc37c 797
e5ba5ec8
PA
798 if (chain)
799 trb->ctrl |= DWC3_TRB_CTRL_CHN;
800
16e78db7 801 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
f6bafc6a 802 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
c71fc37c 803
f6bafc6a 804 trb->ctrl |= DWC3_TRB_CTRL_HWO;
c71fc37c
FB
805}
806
72246da4
FB
807/*
808 * dwc3_prepare_trbs - setup TRBs from requests
809 * @dep: endpoint for which requests are being prepared
810 * @starting: true if the endpoint is idle and no requests are queued.
811 *
1d046793
PZ
812 * The function goes through the requests list and sets up TRBs for the
813 * transfers. The function returns once there are no more TRBs available or
814 * it runs out of requests.
72246da4 815 */
68e823e2 816static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
72246da4 817{
68e823e2 818 struct dwc3_request *req, *n;
72246da4 819 u32 trbs_left;
8d62cd65 820 u32 max;
c71fc37c 821 unsigned int last_one = 0;
72246da4
FB
822
823 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
824
825 /* the first request must not be queued */
826 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
c71fc37c 827
8d62cd65 828 /* Can't wrap around on a non-isoc EP since there's no link TRB */
16e78db7 829 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
8d62cd65
PZ
830 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
831 if (trbs_left > max)
832 trbs_left = max;
833 }
834
72246da4 835 /*
1d046793
PZ
836 * If busy & slot are equal than it is either full or empty. If we are
837 * starting to process requests then we are empty. Otherwise we are
72246da4
FB
838 * full and don't do anything
839 */
840 if (!trbs_left) {
841 if (!starting)
68e823e2 842 return;
72246da4
FB
843 trbs_left = DWC3_TRB_NUM;
844 /*
845 * In case we start from scratch, we queue the ISOC requests
846 * starting from slot 1. This is done because we use ring
847 * buffer and have no LST bit to stop us. Instead, we place
1d046793 848 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
72246da4
FB
849 * after the first request so we start at slot 1 and have
850 * 7 requests proceed before we hit the first IOC.
851 * Other transfer types don't use the ring buffer and are
852 * processed from the first TRB until the last one. Since we
853 * don't wrap around we have to start at the beginning.
854 */
16e78db7 855 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
856 dep->busy_slot = 1;
857 dep->free_slot = 1;
858 } else {
859 dep->busy_slot = 0;
860 dep->free_slot = 0;
861 }
862 }
863
864 /* The last TRB is a link TRB, not used for xfer */
16e78db7 865 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
68e823e2 866 return;
72246da4
FB
867
868 list_for_each_entry_safe(req, n, &dep->request_list, list) {
eeb720fb
FB
869 unsigned length;
870 dma_addr_t dma;
e5ba5ec8 871 last_one = false;
72246da4 872
eeb720fb
FB
873 if (req->request.num_mapped_sgs > 0) {
874 struct usb_request *request = &req->request;
875 struct scatterlist *sg = request->sg;
876 struct scatterlist *s;
877 int i;
72246da4 878
eeb720fb
FB
879 for_each_sg(sg, s, request->num_mapped_sgs, i) {
880 unsigned chain = true;
72246da4 881
eeb720fb
FB
882 length = sg_dma_len(s);
883 dma = sg_dma_address(s);
72246da4 884
1d046793
PZ
885 if (i == (request->num_mapped_sgs - 1) ||
886 sg_is_last(s)) {
e5ba5ec8
PA
887 if (list_is_last(&req->list,
888 &dep->request_list))
889 last_one = true;
eeb720fb
FB
890 chain = false;
891 }
72246da4 892
eeb720fb
FB
893 trbs_left--;
894 if (!trbs_left)
895 last_one = true;
72246da4 896
eeb720fb
FB
897 if (last_one)
898 chain = false;
72246da4 899
eeb720fb 900 dwc3_prepare_one_trb(dep, req, dma, length,
e5ba5ec8 901 last_one, chain, i);
72246da4 902
eeb720fb
FB
903 if (last_one)
904 break;
905 }
72246da4 906 } else {
eeb720fb
FB
907 dma = req->request.dma;
908 length = req->request.length;
909 trbs_left--;
72246da4 910
eeb720fb
FB
911 if (!trbs_left)
912 last_one = 1;
879631aa 913
eeb720fb
FB
914 /* Is this the last request? */
915 if (list_is_last(&req->list, &dep->request_list))
916 last_one = 1;
72246da4 917
eeb720fb 918 dwc3_prepare_one_trb(dep, req, dma, length,
e5ba5ec8 919 last_one, false, 0);
72246da4 920
eeb720fb
FB
921 if (last_one)
922 break;
72246da4 923 }
72246da4 924 }
72246da4
FB
925}
926
927static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
928 int start_new)
929{
930 struct dwc3_gadget_ep_cmd_params params;
931 struct dwc3_request *req;
932 struct dwc3 *dwc = dep->dwc;
933 int ret;
934 u32 cmd;
935
936 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
937 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
938 return -EBUSY;
939 }
940 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
941
942 /*
943 * If we are getting here after a short-out-packet we don't enqueue any
944 * new requests as we try to set the IOC bit only on the last request.
945 */
946 if (start_new) {
947 if (list_empty(&dep->req_queued))
948 dwc3_prepare_trbs(dep, start_new);
949
950 /* req points to the first request which will be sent */
951 req = next_request(&dep->req_queued);
952 } else {
68e823e2
FB
953 dwc3_prepare_trbs(dep, start_new);
954
72246da4 955 /*
1d046793 956 * req points to the first request where HWO changed from 0 to 1
72246da4 957 */
68e823e2 958 req = next_request(&dep->req_queued);
72246da4
FB
959 }
960 if (!req) {
961 dep->flags |= DWC3_EP_PENDING_REQUEST;
962 return 0;
963 }
964
965 memset(&params, 0, sizeof(params));
72246da4 966
1877d6c9
PA
967 if (start_new) {
968 params.param0 = upper_32_bits(req->trb_dma);
969 params.param1 = lower_32_bits(req->trb_dma);
72246da4 970 cmd = DWC3_DEPCMD_STARTTRANSFER;
1877d6c9 971 } else {
72246da4 972 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1877d6c9 973 }
72246da4
FB
974
975 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
976 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
977 if (ret < 0) {
978 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
979
980 /*
981 * FIXME we need to iterate over the list of requests
982 * here and stop, unmap, free and del each of the linked
1d046793 983 * requests instead of what we do now.
72246da4 984 */
0fc9a1be
FB
985 usb_gadget_unmap_request(&dwc->gadget, &req->request,
986 req->direction);
72246da4
FB
987 list_del(&req->list);
988 return ret;
989 }
990
991 dep->flags |= DWC3_EP_BUSY;
25b8ff68 992
f898ae09 993 if (start_new) {
b4996a86 994 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
f898ae09 995 dep->number);
b4996a86 996 WARN_ON_ONCE(!dep->resource_index);
f898ae09 997 }
25b8ff68 998
72246da4
FB
999 return 0;
1000}
1001
d6d6ec7b
PA
1002static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1003 struct dwc3_ep *dep, u32 cur_uf)
1004{
1005 u32 uf;
1006
1007 if (list_empty(&dep->request_list)) {
1008 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1009 dep->name);
f4a53c55 1010 dep->flags |= DWC3_EP_PENDING_REQUEST;
d6d6ec7b
PA
1011 return;
1012 }
1013
1014 /* 4 micro frames in the future */
1015 uf = cur_uf + dep->interval * 4;
1016
1017 __dwc3_gadget_kick_transfer(dep, uf, 1);
1018}
1019
1020static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1021 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1022{
1023 u32 cur_uf, mask;
1024
1025 mask = ~(dep->interval - 1);
1026 cur_uf = event->parameters & mask;
1027
1028 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1029}
1030
72246da4
FB
1031static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1032{
0fc9a1be
FB
1033 struct dwc3 *dwc = dep->dwc;
1034 int ret;
1035
72246da4
FB
1036 req->request.actual = 0;
1037 req->request.status = -EINPROGRESS;
1038 req->direction = dep->direction;
1039 req->epnum = dep->number;
1040
1041 /*
1042 * We only add to our list of requests now and
1043 * start consuming the list once we get XferNotReady
1044 * IRQ.
1045 *
1046 * That way, we avoid doing anything that we don't need
1047 * to do now and defer it until the point we receive a
1048 * particular token from the Host side.
1049 *
1050 * This will also avoid Host cancelling URBs due to too
1d046793 1051 * many NAKs.
72246da4 1052 */
0fc9a1be
FB
1053 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1054 dep->direction);
1055 if (ret)
1056 return ret;
1057
72246da4
FB
1058 list_add_tail(&req->list, &dep->request_list);
1059
1060 /*
b511e5e7 1061 * There are a few special cases:
72246da4 1062 *
f898ae09
PZ
1063 * 1. XferNotReady with empty list of requests. We need to kick the
1064 * transfer here in that situation, otherwise we will be NAKing
1065 * forever. If we get XferNotReady before gadget driver has a
1066 * chance to queue a request, we will ACK the IRQ but won't be
1067 * able to receive the data until the next request is queued.
1068 * The following code is handling exactly that.
72246da4 1069 *
72246da4
FB
1070 */
1071 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
f4a53c55
PA
1072 /*
1073 * If xfernotready is already elapsed and it is a case
1074 * of isoc transfer, then issue END TRANSFER, so that
1075 * you can receive xfernotready again and can have
1076 * notion of current microframe.
1077 */
1078 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
cdc359dd
PA
1079 if (list_empty(&dep->req_queued)) {
1080 dwc3_stop_active_transfer(dwc, dep->number);
1081 dep->flags = DWC3_EP_ENABLED;
1082 }
f4a53c55
PA
1083 return 0;
1084 }
1085
b511e5e7 1086 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
348e026f 1087 if (ret && ret != -EBUSY)
b511e5e7
FB
1088 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1089 dep->name);
15f86bde 1090 return ret;
b511e5e7 1091 }
72246da4 1092
b511e5e7
FB
1093 /*
1094 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1095 * kick the transfer here after queuing a request, otherwise the
1096 * core may not see the modified TRB(s).
1097 */
1098 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
79c9046e
PA
1099 (dep->flags & DWC3_EP_BUSY) &&
1100 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
b4996a86
FB
1101 WARN_ON_ONCE(!dep->resource_index);
1102 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
b511e5e7 1103 false);
348e026f 1104 if (ret && ret != -EBUSY)
72246da4
FB
1105 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1106 dep->name);
15f86bde 1107 return ret;
a0925324 1108 }
72246da4
FB
1109
1110 return 0;
1111}
1112
1113static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1114 gfp_t gfp_flags)
1115{
1116 struct dwc3_request *req = to_dwc3_request(request);
1117 struct dwc3_ep *dep = to_dwc3_ep(ep);
1118 struct dwc3 *dwc = dep->dwc;
1119
1120 unsigned long flags;
1121
1122 int ret;
1123
16e78db7 1124 if (!dep->endpoint.desc) {
72246da4
FB
1125 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1126 request, ep->name);
1127 return -ESHUTDOWN;
1128 }
1129
1130 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1131 request, ep->name, request->length);
1132
1133 spin_lock_irqsave(&dwc->lock, flags);
1134 ret = __dwc3_gadget_ep_queue(dep, req);
1135 spin_unlock_irqrestore(&dwc->lock, flags);
1136
1137 return ret;
1138}
1139
1140static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1141 struct usb_request *request)
1142{
1143 struct dwc3_request *req = to_dwc3_request(request);
1144 struct dwc3_request *r = NULL;
1145
1146 struct dwc3_ep *dep = to_dwc3_ep(ep);
1147 struct dwc3 *dwc = dep->dwc;
1148
1149 unsigned long flags;
1150 int ret = 0;
1151
1152 spin_lock_irqsave(&dwc->lock, flags);
1153
1154 list_for_each_entry(r, &dep->request_list, list) {
1155 if (r == req)
1156 break;
1157 }
1158
1159 if (r != req) {
1160 list_for_each_entry(r, &dep->req_queued, list) {
1161 if (r == req)
1162 break;
1163 }
1164 if (r == req) {
1165 /* wait until it is processed */
1166 dwc3_stop_active_transfer(dwc, dep->number);
e8d4e8be 1167 goto out1;
72246da4
FB
1168 }
1169 dev_err(dwc->dev, "request %p was not queued to %s\n",
1170 request, ep->name);
1171 ret = -EINVAL;
1172 goto out0;
1173 }
1174
e8d4e8be 1175out1:
72246da4
FB
1176 /* giveback the request */
1177 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1178
1179out0:
1180 spin_unlock_irqrestore(&dwc->lock, flags);
1181
1182 return ret;
1183}
1184
1185int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1186{
1187 struct dwc3_gadget_ep_cmd_params params;
1188 struct dwc3 *dwc = dep->dwc;
1189 int ret;
1190
1191 memset(&params, 0x00, sizeof(params));
1192
1193 if (value) {
72246da4
FB
1194 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1195 DWC3_DEPCMD_SETSTALL, &params);
1196 if (ret)
1197 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1198 value ? "set" : "clear",
1199 dep->name);
1200 else
1201 dep->flags |= DWC3_EP_STALL;
1202 } else {
5275455a
PZ
1203 if (dep->flags & DWC3_EP_WEDGE)
1204 return 0;
1205
72246da4
FB
1206 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1207 DWC3_DEPCMD_CLEARSTALL, &params);
1208 if (ret)
1209 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1210 value ? "set" : "clear",
1211 dep->name);
1212 else
1213 dep->flags &= ~DWC3_EP_STALL;
1214 }
5275455a 1215
72246da4
FB
1216 return ret;
1217}
1218
1219static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1220{
1221 struct dwc3_ep *dep = to_dwc3_ep(ep);
1222 struct dwc3 *dwc = dep->dwc;
1223
1224 unsigned long flags;
1225
1226 int ret;
1227
1228 spin_lock_irqsave(&dwc->lock, flags);
1229
16e78db7 1230 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
1231 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1232 ret = -EINVAL;
1233 goto out;
1234 }
1235
1236 ret = __dwc3_gadget_ep_set_halt(dep, value);
1237out:
1238 spin_unlock_irqrestore(&dwc->lock, flags);
1239
1240 return ret;
1241}
1242
1243static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1244{
1245 struct dwc3_ep *dep = to_dwc3_ep(ep);
249a4569
PZ
1246 struct dwc3 *dwc = dep->dwc;
1247 unsigned long flags;
72246da4 1248
249a4569 1249 spin_lock_irqsave(&dwc->lock, flags);
72246da4 1250 dep->flags |= DWC3_EP_WEDGE;
249a4569 1251 spin_unlock_irqrestore(&dwc->lock, flags);
72246da4 1252
08f0d966
PA
1253 if (dep->number == 0 || dep->number == 1)
1254 return dwc3_gadget_ep0_set_halt(ep, 1);
1255 else
1256 return dwc3_gadget_ep_set_halt(ep, 1);
72246da4
FB
1257}
1258
1259/* -------------------------------------------------------------------------- */
1260
1261static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1262 .bLength = USB_DT_ENDPOINT_SIZE,
1263 .bDescriptorType = USB_DT_ENDPOINT,
1264 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1265};
1266
1267static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1268 .enable = dwc3_gadget_ep0_enable,
1269 .disable = dwc3_gadget_ep0_disable,
1270 .alloc_request = dwc3_gadget_ep_alloc_request,
1271 .free_request = dwc3_gadget_ep_free_request,
1272 .queue = dwc3_gadget_ep0_queue,
1273 .dequeue = dwc3_gadget_ep_dequeue,
08f0d966 1274 .set_halt = dwc3_gadget_ep0_set_halt,
72246da4
FB
1275 .set_wedge = dwc3_gadget_ep_set_wedge,
1276};
1277
1278static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1279 .enable = dwc3_gadget_ep_enable,
1280 .disable = dwc3_gadget_ep_disable,
1281 .alloc_request = dwc3_gadget_ep_alloc_request,
1282 .free_request = dwc3_gadget_ep_free_request,
1283 .queue = dwc3_gadget_ep_queue,
1284 .dequeue = dwc3_gadget_ep_dequeue,
1285 .set_halt = dwc3_gadget_ep_set_halt,
1286 .set_wedge = dwc3_gadget_ep_set_wedge,
1287};
1288
1289/* -------------------------------------------------------------------------- */
1290
1291static int dwc3_gadget_get_frame(struct usb_gadget *g)
1292{
1293 struct dwc3 *dwc = gadget_to_dwc(g);
1294 u32 reg;
1295
1296 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1297 return DWC3_DSTS_SOFFN(reg);
1298}
1299
1300static int dwc3_gadget_wakeup(struct usb_gadget *g)
1301{
1302 struct dwc3 *dwc = gadget_to_dwc(g);
1303
1304 unsigned long timeout;
1305 unsigned long flags;
1306
1307 u32 reg;
1308
1309 int ret = 0;
1310
1311 u8 link_state;
1312 u8 speed;
1313
1314 spin_lock_irqsave(&dwc->lock, flags);
1315
1316 /*
1317 * According to the Databook Remote wakeup request should
1318 * be issued only when the device is in early suspend state.
1319 *
1320 * We can check that via USB Link State bits in DSTS register.
1321 */
1322 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1323
1324 speed = reg & DWC3_DSTS_CONNECTSPD;
1325 if (speed == DWC3_DSTS_SUPERSPEED) {
1326 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1327 ret = -EINVAL;
1328 goto out;
1329 }
1330
1331 link_state = DWC3_DSTS_USBLNKST(reg);
1332
1333 switch (link_state) {
1334 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1335 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1336 break;
1337 default:
1338 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1339 link_state);
1340 ret = -EINVAL;
1341 goto out;
1342 }
1343
8598bde7
FB
1344 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1345 if (ret < 0) {
1346 dev_err(dwc->dev, "failed to put link in Recovery\n");
1347 goto out;
1348 }
72246da4 1349
802fde98
PZ
1350 /* Recent versions do this automatically */
1351 if (dwc->revision < DWC3_REVISION_194A) {
1352 /* write zeroes to Link Change Request */
fcc023c7 1353 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
802fde98
PZ
1354 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1355 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1356 }
72246da4 1357
1d046793 1358 /* poll until Link State changes to ON */
72246da4
FB
1359 timeout = jiffies + msecs_to_jiffies(100);
1360
1d046793 1361 while (!time_after(jiffies, timeout)) {
72246da4
FB
1362 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1363
1364 /* in HS, means ON */
1365 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1366 break;
1367 }
1368
1369 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1370 dev_err(dwc->dev, "failed to send remote wakeup\n");
1371 ret = -EINVAL;
1372 }
1373
1374out:
1375 spin_unlock_irqrestore(&dwc->lock, flags);
1376
1377 return ret;
1378}
1379
1380static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1381 int is_selfpowered)
1382{
1383 struct dwc3 *dwc = gadget_to_dwc(g);
249a4569 1384 unsigned long flags;
72246da4 1385
249a4569 1386 spin_lock_irqsave(&dwc->lock, flags);
72246da4 1387 dwc->is_selfpowered = !!is_selfpowered;
249a4569 1388 spin_unlock_irqrestore(&dwc->lock, flags);
72246da4
FB
1389
1390 return 0;
1391}
1392
6f17f74b 1393static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
72246da4
FB
1394{
1395 u32 reg;
61d58242 1396 u32 timeout = 500;
72246da4
FB
1397
1398 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
8db7ed15 1399 if (is_on) {
802fde98
PZ
1400 if (dwc->revision <= DWC3_REVISION_187A) {
1401 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1402 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1403 }
1404
1405 if (dwc->revision >= DWC3_REVISION_194A)
1406 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1407 reg |= DWC3_DCTL_RUN_STOP;
9fcb3bd8 1408 dwc->pullups_connected = true;
8db7ed15 1409 } else {
72246da4 1410 reg &= ~DWC3_DCTL_RUN_STOP;
9fcb3bd8 1411 dwc->pullups_connected = false;
8db7ed15 1412 }
72246da4
FB
1413
1414 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1415
1416 do {
1417 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1418 if (is_on) {
1419 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1420 break;
1421 } else {
1422 if (reg & DWC3_DSTS_DEVCTRLHLT)
1423 break;
1424 }
72246da4
FB
1425 timeout--;
1426 if (!timeout)
6f17f74b 1427 return -ETIMEDOUT;
61d58242 1428 udelay(1);
72246da4
FB
1429 } while (1);
1430
1431 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1432 dwc->gadget_driver
1433 ? dwc->gadget_driver->function : "no-function",
1434 is_on ? "connect" : "disconnect");
6f17f74b
PA
1435
1436 return 0;
72246da4
FB
1437}
1438
1439static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1440{
1441 struct dwc3 *dwc = gadget_to_dwc(g);
1442 unsigned long flags;
6f17f74b 1443 int ret;
72246da4
FB
1444
1445 is_on = !!is_on;
1446
1447 spin_lock_irqsave(&dwc->lock, flags);
6f17f74b 1448 ret = dwc3_gadget_run_stop(dwc, is_on);
72246da4
FB
1449 spin_unlock_irqrestore(&dwc->lock, flags);
1450
6f17f74b 1451 return ret;
72246da4
FB
1452}
1453
8698e2ac
FB
1454static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1455{
1456 u32 reg;
1457
1458 /* Enable all but Start and End of Frame IRQs */
1459 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1460 DWC3_DEVTEN_EVNTOVERFLOWEN |
1461 DWC3_DEVTEN_CMDCMPLTEN |
1462 DWC3_DEVTEN_ERRTICERREN |
1463 DWC3_DEVTEN_WKUPEVTEN |
1464 DWC3_DEVTEN_ULSTCNGEN |
1465 DWC3_DEVTEN_CONNECTDONEEN |
1466 DWC3_DEVTEN_USBRSTEN |
1467 DWC3_DEVTEN_DISCONNEVTEN);
1468
1469 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1470}
1471
1472static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1473{
1474 /* mask all interrupts */
1475 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1476}
1477
1478static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
b15a762f 1479static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
8698e2ac 1480
72246da4
FB
1481static int dwc3_gadget_start(struct usb_gadget *g,
1482 struct usb_gadget_driver *driver)
1483{
1484 struct dwc3 *dwc = gadget_to_dwc(g);
1485 struct dwc3_ep *dep;
1486 unsigned long flags;
1487 int ret = 0;
8698e2ac 1488 int irq;
72246da4
FB
1489 u32 reg;
1490
b0d7ffd4
FB
1491 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1492 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1493 IRQF_SHARED | IRQF_ONESHOT, "dwc3", dwc);
1494 if (ret) {
1495 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1496 irq, ret);
1497 goto err0;
1498 }
1499
72246da4
FB
1500 spin_lock_irqsave(&dwc->lock, flags);
1501
1502 if (dwc->gadget_driver) {
1503 dev_err(dwc->dev, "%s is already bound to %s\n",
1504 dwc->gadget.name,
1505 dwc->gadget_driver->driver.name);
1506 ret = -EBUSY;
b0d7ffd4 1507 goto err1;
72246da4
FB
1508 }
1509
1510 dwc->gadget_driver = driver;
72246da4 1511
72246da4
FB
1512 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1513 reg &= ~(DWC3_DCFG_SPEED_MASK);
07e7f47b
FB
1514
1515 /**
1516 * WORKAROUND: DWC3 revision < 2.20a have an issue
1517 * which would cause metastability state on Run/Stop
1518 * bit if we try to force the IP to USB2-only mode.
1519 *
1520 * Because of that, we cannot configure the IP to any
1521 * speed other than the SuperSpeed
1522 *
1523 * Refers to:
1524 *
1525 * STAR#9000525659: Clock Domain Crossing on DCTL in
1526 * USB 2.0 Mode
1527 */
1528 if (dwc->revision < DWC3_REVISION_220A)
1529 reg |= DWC3_DCFG_SUPERSPEED;
1530 else
1531 reg |= dwc->maximum_speed;
72246da4
FB
1532 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1533
b23c8439
PZ
1534 dwc->start_config_issued = false;
1535
72246da4
FB
1536 /* Start with SuperSpeed Default */
1537 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1538
1539 dep = dwc->eps[0];
4b345c9a 1540 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
72246da4
FB
1541 if (ret) {
1542 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
b0d7ffd4 1543 goto err2;
72246da4
FB
1544 }
1545
1546 dep = dwc->eps[1];
4b345c9a 1547 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
72246da4
FB
1548 if (ret) {
1549 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
b0d7ffd4 1550 goto err3;
72246da4
FB
1551 }
1552
1553 /* begin to receive SETUP packets */
c7fcdeb2 1554 dwc->ep0state = EP0_SETUP_PHASE;
72246da4
FB
1555 dwc3_ep0_out_start(dwc);
1556
8698e2ac
FB
1557 dwc3_gadget_enable_irq(dwc);
1558
72246da4
FB
1559 spin_unlock_irqrestore(&dwc->lock, flags);
1560
1561 return 0;
1562
b0d7ffd4 1563err3:
72246da4
FB
1564 __dwc3_gadget_ep_disable(dwc->eps[0]);
1565
b0d7ffd4 1566err2:
cdcedd69 1567 dwc->gadget_driver = NULL;
b0d7ffd4
FB
1568
1569err1:
72246da4
FB
1570 spin_unlock_irqrestore(&dwc->lock, flags);
1571
b0d7ffd4
FB
1572 free_irq(irq, dwc);
1573
1574err0:
72246da4
FB
1575 return ret;
1576}
1577
1578static int dwc3_gadget_stop(struct usb_gadget *g,
1579 struct usb_gadget_driver *driver)
1580{
1581 struct dwc3 *dwc = gadget_to_dwc(g);
1582 unsigned long flags;
8698e2ac 1583 int irq;
72246da4
FB
1584
1585 spin_lock_irqsave(&dwc->lock, flags);
1586
8698e2ac 1587 dwc3_gadget_disable_irq(dwc);
72246da4
FB
1588 __dwc3_gadget_ep_disable(dwc->eps[0]);
1589 __dwc3_gadget_ep_disable(dwc->eps[1]);
1590
1591 dwc->gadget_driver = NULL;
72246da4
FB
1592
1593 spin_unlock_irqrestore(&dwc->lock, flags);
1594
b0d7ffd4
FB
1595 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1596 free_irq(irq, dwc);
1597
72246da4
FB
1598 return 0;
1599}
802fde98 1600
72246da4
FB
1601static const struct usb_gadget_ops dwc3_gadget_ops = {
1602 .get_frame = dwc3_gadget_get_frame,
1603 .wakeup = dwc3_gadget_wakeup,
1604 .set_selfpowered = dwc3_gadget_set_selfpowered,
1605 .pullup = dwc3_gadget_pullup,
1606 .udc_start = dwc3_gadget_start,
1607 .udc_stop = dwc3_gadget_stop,
1608};
1609
1610/* -------------------------------------------------------------------------- */
1611
6a1e3ef4
FB
1612static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1613 u8 num, u32 direction)
72246da4
FB
1614{
1615 struct dwc3_ep *dep;
6a1e3ef4 1616 u8 i;
72246da4 1617
6a1e3ef4
FB
1618 for (i = 0; i < num; i++) {
1619 u8 epnum = (i << 1) | (!!direction);
72246da4 1620
72246da4
FB
1621 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1622 if (!dep) {
1623 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1624 epnum);
1625 return -ENOMEM;
1626 }
1627
1628 dep->dwc = dwc;
1629 dep->number = epnum;
1630 dwc->eps[epnum] = dep;
1631
1632 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1633 (epnum & 1) ? "in" : "out");
6a1e3ef4 1634
72246da4
FB
1635 dep->endpoint.name = dep->name;
1636 dep->direction = (epnum & 1);
1637
1638 if (epnum == 0 || epnum == 1) {
1639 dep->endpoint.maxpacket = 512;
6048e4c6 1640 dep->endpoint.maxburst = 1;
72246da4
FB
1641 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1642 if (!epnum)
1643 dwc->gadget.ep0 = &dep->endpoint;
1644 } else {
1645 int ret;
1646
1647 dep->endpoint.maxpacket = 1024;
12d36c16 1648 dep->endpoint.max_streams = 15;
72246da4
FB
1649 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1650 list_add_tail(&dep->endpoint.ep_list,
1651 &dwc->gadget.ep_list);
1652
1653 ret = dwc3_alloc_trb_pool(dep);
25b8ff68 1654 if (ret)
72246da4 1655 return ret;
72246da4 1656 }
25b8ff68 1657
72246da4
FB
1658 INIT_LIST_HEAD(&dep->request_list);
1659 INIT_LIST_HEAD(&dep->req_queued);
1660 }
1661
1662 return 0;
1663}
1664
6a1e3ef4
FB
1665static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1666{
1667 int ret;
1668
1669 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1670
1671 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1672 if (ret < 0) {
1673 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1674 return ret;
1675 }
1676
1677 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1678 if (ret < 0) {
1679 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1680 return ret;
1681 }
1682
1683 return 0;
1684}
1685
72246da4
FB
1686static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1687{
1688 struct dwc3_ep *dep;
1689 u8 epnum;
1690
1691 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1692 dep = dwc->eps[epnum];
6a1e3ef4
FB
1693 if (!dep)
1694 continue;
5bf8fae3
GC
1695 /*
1696 * Physical endpoints 0 and 1 are special; they form the
1697 * bi-directional USB endpoint 0.
1698 *
1699 * For those two physical endpoints, we don't allocate a TRB
1700 * pool nor do we add them the endpoints list. Due to that, we
1701 * shouldn't do these two operations otherwise we would end up
1702 * with all sorts of bugs when removing dwc3.ko.
1703 */
1704 if (epnum != 0 && epnum != 1) {
1705 dwc3_free_trb_pool(dep);
72246da4 1706 list_del(&dep->endpoint.ep_list);
5bf8fae3 1707 }
72246da4
FB
1708
1709 kfree(dep);
1710 }
1711}
1712
72246da4 1713/* -------------------------------------------------------------------------- */
e5caff68 1714
e5ba5ec8
PA
1715static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1716 struct dwc3_request *req, struct dwc3_trb *trb,
72246da4
FB
1717 const struct dwc3_event_depevt *event, int status)
1718{
72246da4
FB
1719 unsigned int count;
1720 unsigned int s_pkt = 0;
d6d6ec7b 1721 unsigned int trb_status;
72246da4 1722
e5ba5ec8
PA
1723 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1724 /*
1725 * We continue despite the error. There is not much we
1726 * can do. If we don't clean it up we loop forever. If
1727 * we skip the TRB then it gets overwritten after a
1728 * while since we use them in a ring buffer. A BUG()
1729 * would help. Lets hope that if this occurs, someone
1730 * fixes the root cause instead of looking away :)
1731 */
1732 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1733 dep->name, trb);
1734 count = trb->size & DWC3_TRB_SIZE_MASK;
1735
1736 if (dep->direction) {
1737 if (count) {
1738 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1739 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1740 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1741 dep->name);
1742 /*
1743 * If missed isoc occurred and there is
1744 * no request queued then issue END
1745 * TRANSFER, so that core generates
1746 * next xfernotready and we will issue
1747 * a fresh START TRANSFER.
1748 * If there are still queued request
1749 * then wait, do not issue either END
1750 * or UPDATE TRANSFER, just attach next
1751 * request in request_list during
1752 * giveback.If any future queued request
1753 * is successfully transferred then we
1754 * will issue UPDATE TRANSFER for all
1755 * request in the request_list.
1756 */
1757 dep->flags |= DWC3_EP_MISSED_ISOC;
1758 } else {
1759 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1760 dep->name);
1761 status = -ECONNRESET;
1762 }
1763 } else {
1764 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1765 }
1766 } else {
1767 if (count && (event->status & DEPEVT_STATUS_SHORT))
1768 s_pkt = 1;
1769 }
1770
1771 /*
1772 * We assume here we will always receive the entire data block
1773 * which we should receive. Meaning, if we program RX to
1774 * receive 4K but we receive only 2K, we assume that's all we
1775 * should receive and we simply bounce the request back to the
1776 * gadget driver for further processing.
1777 */
1778 req->request.actual += req->request.length - count;
1779 if (s_pkt)
1780 return 1;
1781 if ((event->status & DEPEVT_STATUS_LST) &&
1782 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1783 DWC3_TRB_CTRL_HWO)))
1784 return 1;
1785 if ((event->status & DEPEVT_STATUS_IOC) &&
1786 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1787 return 1;
1788 return 0;
1789}
1790
1791static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1792 const struct dwc3_event_depevt *event, int status)
1793{
1794 struct dwc3_request *req;
1795 struct dwc3_trb *trb;
1796 unsigned int slot;
1797 unsigned int i;
1798 int ret;
1799
72246da4
FB
1800 do {
1801 req = next_request(&dep->req_queued);
d39ee7be
SAS
1802 if (!req) {
1803 WARN_ON_ONCE(1);
1804 return 1;
1805 }
e5ba5ec8
PA
1806 i = 0;
1807 do {
1808 slot = req->start_slot + i;
1809 if ((slot == DWC3_TRB_NUM - 1) &&
1810 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1811 slot++;
1812 slot %= DWC3_TRB_NUM;
1813 trb = &dep->trb_pool[slot];
72246da4 1814
e5ba5ec8
PA
1815 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1816 event, status);
1817 if (ret)
1818 break;
1819 }while (++i < req->request.num_mapped_sgs);
72246da4 1820
72246da4 1821 dwc3_gadget_giveback(dep, req, status);
e5ba5ec8
PA
1822
1823 if (ret)
72246da4
FB
1824 break;
1825 } while (1);
1826
cdc359dd
PA
1827 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1828 list_empty(&dep->req_queued)) {
1829 if (list_empty(&dep->request_list)) {
1830 /*
1831 * If there is no entry in request list then do
1832 * not issue END TRANSFER now. Just set PENDING
1833 * flag, so that END TRANSFER is issued when an
1834 * entry is added into request list.
1835 */
1836 dep->flags = DWC3_EP_PENDING_REQUEST;
1837 } else {
1838 dwc3_stop_active_transfer(dwc, dep->number);
1839 dep->flags = DWC3_EP_ENABLED;
1840 }
7efea86c
PA
1841 return 1;
1842 }
1843
f6bafc6a
FB
1844 if ((event->status & DEPEVT_STATUS_IOC) &&
1845 (trb->ctrl & DWC3_TRB_CTRL_IOC))
72246da4
FB
1846 return 0;
1847 return 1;
1848}
1849
1850static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1851 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1852 int start_new)
1853{
1854 unsigned status = 0;
1855 int clean_busy;
1856
1857 if (event->status & DEPEVT_STATUS_BUSERR)
1858 status = -ECONNRESET;
1859
1d046793 1860 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
c2df85ca 1861 if (clean_busy)
72246da4 1862 dep->flags &= ~DWC3_EP_BUSY;
fae2b904
FB
1863
1864 /*
1865 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1866 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1867 */
1868 if (dwc->revision < DWC3_REVISION_183A) {
1869 u32 reg;
1870 int i;
1871
1872 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
348e026f 1873 dep = dwc->eps[i];
fae2b904
FB
1874
1875 if (!(dep->flags & DWC3_EP_ENABLED))
1876 continue;
1877
1878 if (!list_empty(&dep->req_queued))
1879 return;
1880 }
1881
1882 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1883 reg |= dwc->u1u2;
1884 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1885
1886 dwc->u1u2 = 0;
1887 }
72246da4
FB
1888}
1889
72246da4
FB
1890static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1891 const struct dwc3_event_depevt *event)
1892{
1893 struct dwc3_ep *dep;
1894 u8 epnum = event->endpoint_number;
1895
1896 dep = dwc->eps[epnum];
1897
3336abb5
FB
1898 if (!(dep->flags & DWC3_EP_ENABLED))
1899 return;
1900
72246da4
FB
1901 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1902 dwc3_ep_event_string(event->endpoint_event));
1903
1904 if (epnum == 0 || epnum == 1) {
1905 dwc3_ep0_interrupt(dwc, event);
1906 return;
1907 }
1908
1909 switch (event->endpoint_event) {
1910 case DWC3_DEPEVT_XFERCOMPLETE:
b4996a86 1911 dep->resource_index = 0;
c2df85ca 1912
16e78db7 1913 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
1914 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1915 dep->name);
1916 return;
1917 }
1918
1919 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1920 break;
1921 case DWC3_DEPEVT_XFERINPROGRESS:
16e78db7 1922 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
1923 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1924 dep->name);
1925 return;
1926 }
1927
1928 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1929 break;
1930 case DWC3_DEPEVT_XFERNOTREADY:
16e78db7 1931 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
72246da4
FB
1932 dwc3_gadget_start_isoc(dwc, dep, event);
1933 } else {
1934 int ret;
1935
1936 dev_vdbg(dwc->dev, "%s: reason %s\n",
40aa41fb
FB
1937 dep->name, event->status &
1938 DEPEVT_STATUS_TRANSFER_ACTIVE
72246da4
FB
1939 ? "Transfer Active"
1940 : "Transfer Not Active");
1941
1942 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1943 if (!ret || ret == -EBUSY)
1944 return;
1945
1946 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1947 dep->name);
1948 }
1949
879631aa
FB
1950 break;
1951 case DWC3_DEPEVT_STREAMEVT:
16e78db7 1952 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
879631aa
FB
1953 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1954 dep->name);
1955 return;
1956 }
1957
1958 switch (event->status) {
1959 case DEPEVT_STREAMEVT_FOUND:
1960 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1961 event->parameters);
1962
1963 break;
1964 case DEPEVT_STREAMEVT_NOTFOUND:
1965 /* FALLTHROUGH */
1966 default:
1967 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1968 }
72246da4
FB
1969 break;
1970 case DWC3_DEPEVT_RXTXFIFOEVT:
1971 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1972 break;
72246da4 1973 case DWC3_DEPEVT_EPCMDCMPLT:
ea53b882 1974 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
72246da4
FB
1975 break;
1976 }
1977}
1978
1979static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1980{
1981 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1982 spin_unlock(&dwc->lock);
1983 dwc->gadget_driver->disconnect(&dwc->gadget);
1984 spin_lock(&dwc->lock);
1985 }
1986}
1987
1988static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1989{
1990 struct dwc3_ep *dep;
1991 struct dwc3_gadget_ep_cmd_params params;
1992 u32 cmd;
1993 int ret;
1994
1995 dep = dwc->eps[epnum];
1996
b4996a86 1997 if (!dep->resource_index)
3daf74d7
PA
1998 return;
1999
57911504
PA
2000 /*
2001 * NOTICE: We are violating what the Databook says about the
2002 * EndTransfer command. Ideally we would _always_ wait for the
2003 * EndTransfer Command Completion IRQ, but that's causing too
2004 * much trouble synchronizing between us and gadget driver.
2005 *
2006 * We have discussed this with the IP Provider and it was
2007 * suggested to giveback all requests here, but give HW some
2008 * extra time to synchronize with the interconnect. We're using
2009 * an arbitraty 100us delay for that.
2010 *
2011 * Note also that a similar handling was tested by Synopsys
2012 * (thanks a lot Paul) and nothing bad has come out of it.
2013 * In short, what we're doing is:
2014 *
2015 * - Issue EndTransfer WITH CMDIOC bit set
2016 * - Wait 100us
2017 */
2018
3daf74d7
PA
2019 cmd = DWC3_DEPCMD_ENDTRANSFER;
2020 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
b4996a86 2021 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3daf74d7
PA
2022 memset(&params, 0, sizeof(params));
2023 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2024 WARN_ON_ONCE(ret);
b4996a86 2025 dep->resource_index = 0;
041d81f4 2026 dep->flags &= ~DWC3_EP_BUSY;
57911504 2027 udelay(100);
72246da4
FB
2028}
2029
2030static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2031{
2032 u32 epnum;
2033
2034 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2035 struct dwc3_ep *dep;
2036
2037 dep = dwc->eps[epnum];
6a1e3ef4
FB
2038 if (!dep)
2039 continue;
2040
72246da4
FB
2041 if (!(dep->flags & DWC3_EP_ENABLED))
2042 continue;
2043
624407f9 2044 dwc3_remove_requests(dwc, dep);
72246da4
FB
2045 }
2046}
2047
2048static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2049{
2050 u32 epnum;
2051
2052 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2053 struct dwc3_ep *dep;
2054 struct dwc3_gadget_ep_cmd_params params;
2055 int ret;
2056
2057 dep = dwc->eps[epnum];
6a1e3ef4
FB
2058 if (!dep)
2059 continue;
72246da4
FB
2060
2061 if (!(dep->flags & DWC3_EP_STALL))
2062 continue;
2063
2064 dep->flags &= ~DWC3_EP_STALL;
2065
2066 memset(&params, 0, sizeof(params));
2067 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2068 DWC3_DEPCMD_CLEARSTALL, &params);
2069 WARN_ON_ONCE(ret);
2070 }
2071}
2072
2073static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2074{
c4430a26
FB
2075 int reg;
2076
72246da4 2077 dev_vdbg(dwc->dev, "%s\n", __func__);
72246da4
FB
2078
2079 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2080 reg &= ~DWC3_DCTL_INITU1ENA;
2081 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2082
2083 reg &= ~DWC3_DCTL_INITU2ENA;
2084 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72246da4 2085
72246da4 2086 dwc3_disconnect_gadget(dwc);
b23c8439 2087 dwc->start_config_issued = false;
72246da4
FB
2088
2089 dwc->gadget.speed = USB_SPEED_UNKNOWN;
df62df56 2090 dwc->setup_packet_pending = false;
72246da4
FB
2091}
2092
d7a46a8d 2093static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
72246da4
FB
2094{
2095 u32 reg;
2096
2097 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2098
d7a46a8d 2099 if (suspend)
72246da4 2100 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
d7a46a8d
PZ
2101 else
2102 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
72246da4
FB
2103
2104 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2105}
2106
d7a46a8d 2107static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
72246da4
FB
2108{
2109 u32 reg;
2110
2111 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2112
d7a46a8d 2113 if (suspend)
72246da4 2114 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
d7a46a8d
PZ
2115 else
2116 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
72246da4
FB
2117
2118 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2119}
2120
2121static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2122{
2123 u32 reg;
2124
2125 dev_vdbg(dwc->dev, "%s\n", __func__);
2126
df62df56
FB
2127 /*
2128 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2129 * would cause a missing Disconnect Event if there's a
2130 * pending Setup Packet in the FIFO.
2131 *
2132 * There's no suggested workaround on the official Bug
2133 * report, which states that "unless the driver/application
2134 * is doing any special handling of a disconnect event,
2135 * there is no functional issue".
2136 *
2137 * Unfortunately, it turns out that we _do_ some special
2138 * handling of a disconnect event, namely complete all
2139 * pending transfers, notify gadget driver of the
2140 * disconnection, and so on.
2141 *
2142 * Our suggested workaround is to follow the Disconnect
2143 * Event steps here, instead, based on a setup_packet_pending
2144 * flag. Such flag gets set whenever we have a XferNotReady
2145 * event on EP0 and gets cleared on XferComplete for the
2146 * same endpoint.
2147 *
2148 * Refers to:
2149 *
2150 * STAR#9000466709: RTL: Device : Disconnect event not
2151 * generated if setup packet pending in FIFO
2152 */
2153 if (dwc->revision < DWC3_REVISION_188A) {
2154 if (dwc->setup_packet_pending)
2155 dwc3_gadget_disconnect_interrupt(dwc);
2156 }
2157
961906ed 2158 /* after reset -> Default State */
14cd592f 2159 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
961906ed 2160
802fde98
PZ
2161 /* Recent versions support automatic phy suspend and don't need this */
2162 if (dwc->revision < DWC3_REVISION_194A) {
2163 /* Resume PHYs */
2164 dwc3_gadget_usb2_phy_suspend(dwc, false);
2165 dwc3_gadget_usb3_phy_suspend(dwc, false);
2166 }
72246da4
FB
2167
2168 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2169 dwc3_disconnect_gadget(dwc);
2170
2171 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2172 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2173 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3b637367 2174 dwc->test_mode = false;
72246da4
FB
2175
2176 dwc3_stop_active_transfers(dwc);
2177 dwc3_clear_stall_all_ep(dwc);
b23c8439 2178 dwc->start_config_issued = false;
72246da4
FB
2179
2180 /* Reset device address to zero */
2181 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2182 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2183 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
72246da4
FB
2184}
2185
2186static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2187{
2188 u32 reg;
2189 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2190
2191 /*
2192 * We change the clock only at SS but I dunno why I would want to do
2193 * this. Maybe it becomes part of the power saving plan.
2194 */
2195
2196 if (speed != DWC3_DSTS_SUPERSPEED)
2197 return;
2198
2199 /*
2200 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2201 * each time on Connect Done.
2202 */
2203 if (!usb30_clock)
2204 return;
2205
2206 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2207 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2208 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2209}
2210
d7a46a8d 2211static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
72246da4
FB
2212{
2213 switch (speed) {
2214 case USB_SPEED_SUPER:
d7a46a8d 2215 dwc3_gadget_usb2_phy_suspend(dwc, true);
72246da4
FB
2216 break;
2217 case USB_SPEED_HIGH:
2218 case USB_SPEED_FULL:
2219 case USB_SPEED_LOW:
d7a46a8d 2220 dwc3_gadget_usb3_phy_suspend(dwc, true);
72246da4
FB
2221 break;
2222 }
2223}
2224
2225static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2226{
72246da4
FB
2227 struct dwc3_ep *dep;
2228 int ret;
2229 u32 reg;
2230 u8 speed;
2231
2232 dev_vdbg(dwc->dev, "%s\n", __func__);
2233
72246da4
FB
2234 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2235 speed = reg & DWC3_DSTS_CONNECTSPD;
2236 dwc->speed = speed;
2237
2238 dwc3_update_ram_clk_sel(dwc, speed);
2239
2240 switch (speed) {
2241 case DWC3_DCFG_SUPERSPEED:
05870c5b
FB
2242 /*
2243 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2244 * would cause a missing USB3 Reset event.
2245 *
2246 * In such situations, we should force a USB3 Reset
2247 * event by calling our dwc3_gadget_reset_interrupt()
2248 * routine.
2249 *
2250 * Refers to:
2251 *
2252 * STAR#9000483510: RTL: SS : USB3 reset event may
2253 * not be generated always when the link enters poll
2254 */
2255 if (dwc->revision < DWC3_REVISION_190A)
2256 dwc3_gadget_reset_interrupt(dwc);
2257
72246da4
FB
2258 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2259 dwc->gadget.ep0->maxpacket = 512;
2260 dwc->gadget.speed = USB_SPEED_SUPER;
2261 break;
2262 case DWC3_DCFG_HIGHSPEED:
2263 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2264 dwc->gadget.ep0->maxpacket = 64;
2265 dwc->gadget.speed = USB_SPEED_HIGH;
2266 break;
2267 case DWC3_DCFG_FULLSPEED2:
2268 case DWC3_DCFG_FULLSPEED1:
2269 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2270 dwc->gadget.ep0->maxpacket = 64;
2271 dwc->gadget.speed = USB_SPEED_FULL;
2272 break;
2273 case DWC3_DCFG_LOWSPEED:
2274 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2275 dwc->gadget.ep0->maxpacket = 8;
2276 dwc->gadget.speed = USB_SPEED_LOW;
2277 break;
2278 }
2279
2b758350
PA
2280 /* Enable USB2 LPM Capability */
2281
2282 if ((dwc->revision > DWC3_REVISION_194A)
2283 && (speed != DWC3_DCFG_SUPERSPEED)) {
2284 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2285 reg |= DWC3_DCFG_LPM_CAP;
2286 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2287
2288 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2289 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2290
1a947746
FB
2291 /*
2292 * TODO: This should be configurable. For now using
2293 * maximum allowed HIRD threshold value of 0b1100
2294 */
2295 reg |= DWC3_DCTL_HIRD_THRES(12);
2b758350
PA
2296
2297 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2298 }
2299
802fde98
PZ
2300 /* Recent versions support automatic phy suspend and don't need this */
2301 if (dwc->revision < DWC3_REVISION_194A) {
2302 /* Suspend unneeded PHY */
2303 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2304 }
72246da4
FB
2305
2306 dep = dwc->eps[0];
4b345c9a 2307 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
72246da4
FB
2308 if (ret) {
2309 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2310 return;
2311 }
2312
2313 dep = dwc->eps[1];
4b345c9a 2314 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
72246da4
FB
2315 if (ret) {
2316 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2317 return;
2318 }
2319
2320 /*
2321 * Configure PHY via GUSB3PIPECTLn if required.
2322 *
2323 * Update GTXFIFOSIZn
2324 *
2325 * In both cases reset values should be sufficient.
2326 */
2327}
2328
2329static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2330{
2331 dev_vdbg(dwc->dev, "%s\n", __func__);
2332
2333 /*
2334 * TODO take core out of low power mode when that's
2335 * implemented.
2336 */
2337
2338 dwc->gadget_driver->resume(&dwc->gadget);
2339}
2340
2341static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2342 unsigned int evtinfo)
2343{
fae2b904 2344 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
0b0cc1cd
FB
2345 unsigned int pwropt;
2346
2347 /*
2348 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2349 * Hibernation mode enabled which would show up when device detects
2350 * host-initiated U3 exit.
2351 *
2352 * In that case, device will generate a Link State Change Interrupt
2353 * from U3 to RESUME which is only necessary if Hibernation is
2354 * configured in.
2355 *
2356 * There are no functional changes due to such spurious event and we
2357 * just need to ignore it.
2358 *
2359 * Refers to:
2360 *
2361 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2362 * operational mode
2363 */
2364 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2365 if ((dwc->revision < DWC3_REVISION_250A) &&
2366 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2367 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2368 (next == DWC3_LINK_STATE_RESUME)) {
2369 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2370 return;
2371 }
2372 }
fae2b904
FB
2373
2374 /*
2375 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2376 * on the link partner, the USB session might do multiple entry/exit
2377 * of low power states before a transfer takes place.
2378 *
2379 * Due to this problem, we might experience lower throughput. The
2380 * suggested workaround is to disable DCTL[12:9] bits if we're
2381 * transitioning from U1/U2 to U0 and enable those bits again
2382 * after a transfer completes and there are no pending transfers
2383 * on any of the enabled endpoints.
2384 *
2385 * This is the first half of that workaround.
2386 *
2387 * Refers to:
2388 *
2389 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2390 * core send LGO_Ux entering U0
2391 */
2392 if (dwc->revision < DWC3_REVISION_183A) {
2393 if (next == DWC3_LINK_STATE_U0) {
2394 u32 u1u2;
2395 u32 reg;
2396
2397 switch (dwc->link_state) {
2398 case DWC3_LINK_STATE_U1:
2399 case DWC3_LINK_STATE_U2:
2400 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2401 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2402 | DWC3_DCTL_ACCEPTU2ENA
2403 | DWC3_DCTL_INITU1ENA
2404 | DWC3_DCTL_ACCEPTU1ENA);
2405
2406 if (!dwc->u1u2)
2407 dwc->u1u2 = reg & u1u2;
2408
2409 reg &= ~u1u2;
2410
2411 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2412 break;
2413 default:
2414 /* do nothing */
2415 break;
2416 }
2417 }
2418 }
2419
2420 dwc->link_state = next;
019ac832
FB
2421
2422 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
72246da4
FB
2423}
2424
2425static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2426 const struct dwc3_event_devt *event)
2427{
2428 switch (event->type) {
2429 case DWC3_DEVICE_EVENT_DISCONNECT:
2430 dwc3_gadget_disconnect_interrupt(dwc);
2431 break;
2432 case DWC3_DEVICE_EVENT_RESET:
2433 dwc3_gadget_reset_interrupt(dwc);
2434 break;
2435 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2436 dwc3_gadget_conndone_interrupt(dwc);
2437 break;
2438 case DWC3_DEVICE_EVENT_WAKEUP:
2439 dwc3_gadget_wakeup_interrupt(dwc);
2440 break;
2441 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2442 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2443 break;
2444 case DWC3_DEVICE_EVENT_EOPF:
2445 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2446 break;
2447 case DWC3_DEVICE_EVENT_SOF:
2448 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2449 break;
2450 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2451 dev_vdbg(dwc->dev, "Erratic Error\n");
2452 break;
2453 case DWC3_DEVICE_EVENT_CMD_CMPL:
2454 dev_vdbg(dwc->dev, "Command Complete\n");
2455 break;
2456 case DWC3_DEVICE_EVENT_OVERFLOW:
2457 dev_vdbg(dwc->dev, "Overflow\n");
2458 break;
2459 default:
2460 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2461 }
2462}
2463
2464static void dwc3_process_event_entry(struct dwc3 *dwc,
2465 const union dwc3_event *event)
2466{
2467 /* Endpoint IRQ, handle it and return early */
2468 if (event->type.is_devspec == 0) {
2469 /* depevt */
2470 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2471 }
2472
2473 switch (event->type.type) {
2474 case DWC3_EVENT_TYPE_DEV:
2475 dwc3_gadget_interrupt(dwc, &event->devt);
2476 break;
2477 /* REVISIT what to do with Carkit and I2C events ? */
2478 default:
2479 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2480 }
2481}
2482
b15a762f
FB
2483static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2484{
2485 struct dwc3 *dwc = _dwc;
2486 unsigned long flags;
2487 irqreturn_t ret = IRQ_NONE;
2488 int i;
2489
2490 spin_lock_irqsave(&dwc->lock, flags);
2491
2492 for (i = 0; i < dwc->num_event_buffers; i++) {
2493 struct dwc3_event_buffer *evt;
2494 int left;
2495
2496 evt = dwc->ev_buffs[i];
2497 left = evt->count;
2498
2499 if (!(evt->flags & DWC3_EVENT_PENDING))
2500 continue;
2501
2502 while (left > 0) {
2503 union dwc3_event event;
2504
2505 event.raw = *(u32 *) (evt->buf + evt->lpos);
2506
2507 dwc3_process_event_entry(dwc, &event);
2508
2509 /*
2510 * FIXME we wrap around correctly to the next entry as
2511 * almost all entries are 4 bytes in size. There is one
2512 * entry which has 12 bytes which is a regular entry
2513 * followed by 8 bytes data. ATM I don't know how
2514 * things are organized if we get next to the a
2515 * boundary so I worry about that once we try to handle
2516 * that.
2517 */
2518 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2519 left -= 4;
2520
2521 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(i), 4);
2522 }
2523
2524 evt->count = 0;
2525 evt->flags &= ~DWC3_EVENT_PENDING;
2526 ret = IRQ_HANDLED;
2527 }
2528
2529 spin_unlock_irqrestore(&dwc->lock, flags);
2530
2531 return ret;
2532}
2533
72246da4
FB
2534static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2535{
2536 struct dwc3_event_buffer *evt;
72246da4
FB
2537 u32 count;
2538
b15a762f
FB
2539 evt = dwc->ev_buffs[buf];
2540
72246da4
FB
2541 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2542 count &= DWC3_GEVNTCOUNT_MASK;
2543 if (!count)
2544 return IRQ_NONE;
2545
b15a762f
FB
2546 evt->count = count;
2547 evt->flags |= DWC3_EVENT_PENDING;
72246da4 2548
b15a762f 2549 return IRQ_WAKE_THREAD;
72246da4
FB
2550}
2551
2552static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2553{
2554 struct dwc3 *dwc = _dwc;
2555 int i;
2556 irqreturn_t ret = IRQ_NONE;
2557
2558 spin_lock(&dwc->lock);
2559
9f622b2a 2560 for (i = 0; i < dwc->num_event_buffers; i++) {
72246da4
FB
2561 irqreturn_t status;
2562
2563 status = dwc3_process_event_buf(dwc, i);
b15a762f 2564 if (status == IRQ_WAKE_THREAD)
72246da4
FB
2565 ret = status;
2566 }
2567
2568 spin_unlock(&dwc->lock);
2569
2570 return ret;
2571}
2572
2573/**
2574 * dwc3_gadget_init - Initializes gadget related registers
1d046793 2575 * @dwc: pointer to our controller context structure
72246da4
FB
2576 *
2577 * Returns 0 on success otherwise negative errno.
2578 */
41ac7b3a 2579int dwc3_gadget_init(struct dwc3 *dwc)
72246da4
FB
2580{
2581 u32 reg;
2582 int ret;
72246da4
FB
2583
2584 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2585 &dwc->ctrl_req_addr, GFP_KERNEL);
2586 if (!dwc->ctrl_req) {
2587 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2588 ret = -ENOMEM;
2589 goto err0;
2590 }
2591
2592 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2593 &dwc->ep0_trb_addr, GFP_KERNEL);
2594 if (!dwc->ep0_trb) {
2595 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2596 ret = -ENOMEM;
2597 goto err1;
2598 }
2599
3ef35faf 2600 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
72246da4
FB
2601 if (!dwc->setup_buf) {
2602 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2603 ret = -ENOMEM;
2604 goto err2;
2605 }
2606
5812b1c2 2607 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
3ef35faf
FB
2608 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2609 GFP_KERNEL);
5812b1c2
FB
2610 if (!dwc->ep0_bounce) {
2611 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2612 ret = -ENOMEM;
2613 goto err3;
2614 }
2615
72246da4 2616 dwc->gadget.ops = &dwc3_gadget_ops;
d327ab5b 2617 dwc->gadget.max_speed = USB_SPEED_SUPER;
72246da4 2618 dwc->gadget.speed = USB_SPEED_UNKNOWN;
eeb720fb 2619 dwc->gadget.sg_supported = true;
72246da4
FB
2620 dwc->gadget.name = "dwc3-gadget";
2621
2622 /*
2623 * REVISIT: Here we should clear all pending IRQs to be
2624 * sure we're starting from a well known location.
2625 */
2626
2627 ret = dwc3_gadget_init_endpoints(dwc);
2628 if (ret)
5812b1c2 2629 goto err4;
72246da4 2630
e6a3b5e2
SAS
2631 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2632 reg |= DWC3_DCFG_LPM_CAP;
2633 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2634
8698e2ac 2635 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
802fde98 2636 if (dwc->revision >= DWC3_REVISION_194A) {
dcae3573
PA
2637 dwc3_gadget_usb2_phy_suspend(dwc, false);
2638 dwc3_gadget_usb3_phy_suspend(dwc, false);
802fde98
PZ
2639 }
2640
72246da4
FB
2641 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2642 if (ret) {
2643 dev_err(dwc->dev, "failed to register udc\n");
8698e2ac 2644 goto err5;
72246da4
FB
2645 }
2646
2647 return 0;
2648
5812b1c2 2649err5:
72246da4
FB
2650 dwc3_gadget_free_endpoints(dwc);
2651
5812b1c2 2652err4:
3ef35faf
FB
2653 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2654 dwc->ep0_bounce, dwc->ep0_bounce_addr);
5812b1c2 2655
72246da4 2656err3:
0fc9a1be 2657 kfree(dwc->setup_buf);
72246da4
FB
2658
2659err2:
2660 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2661 dwc->ep0_trb, dwc->ep0_trb_addr);
2662
2663err1:
2664 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2665 dwc->ctrl_req, dwc->ctrl_req_addr);
2666
2667err0:
2668 return ret;
2669}
2670
7415f17c
FB
2671/* -------------------------------------------------------------------------- */
2672
72246da4
FB
2673void dwc3_gadget_exit(struct dwc3 *dwc)
2674{
72246da4 2675 usb_del_gadget_udc(&dwc->gadget);
72246da4 2676
72246da4
FB
2677 dwc3_gadget_free_endpoints(dwc);
2678
3ef35faf
FB
2679 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2680 dwc->ep0_bounce, dwc->ep0_bounce_addr);
5812b1c2 2681
0fc9a1be 2682 kfree(dwc->setup_buf);
72246da4
FB
2683
2684 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2685 dwc->ep0_trb, dwc->ep0_trb_addr);
2686
2687 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2688 dwc->ctrl_req, dwc->ctrl_req_addr);
72246da4 2689}
7415f17c
FB
2690
2691int dwc3_gadget_prepare(struct dwc3 *dwc)
2692{
2693 if (dwc->pullups_connected)
2694 dwc3_gadget_disable_irq(dwc);
2695
2696 return 0;
2697}
2698
2699void dwc3_gadget_complete(struct dwc3 *dwc)
2700{
2701 if (dwc->pullups_connected) {
2702 dwc3_gadget_enable_irq(dwc);
2703 dwc3_gadget_run_stop(dwc, true);
2704 }
2705}
2706
2707int dwc3_gadget_suspend(struct dwc3 *dwc)
2708{
2709 __dwc3_gadget_ep_disable(dwc->eps[0]);
2710 __dwc3_gadget_ep_disable(dwc->eps[1]);
2711
2712 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2713
2714 return 0;
2715}
2716
2717int dwc3_gadget_resume(struct dwc3 *dwc)
2718{
2719 struct dwc3_ep *dep;
2720 int ret;
2721
2722 /* Start with SuperSpeed Default */
2723 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2724
2725 dep = dwc->eps[0];
2726 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
2727 if (ret)
2728 goto err0;
2729
2730 dep = dwc->eps[1];
2731 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
2732 if (ret)
2733 goto err1;
2734
2735 /* begin to receive SETUP packets */
2736 dwc->ep0state = EP0_SETUP_PHASE;
2737 dwc3_ep0_out_start(dwc);
2738
2739 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2740
2741 return 0;
2742
2743err1:
2744 __dwc3_gadget_ep_disable(dwc->eps[0]);
2745
2746err0:
2747 return ret;
2748}