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