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