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