blob: fab32575647df72e993d4e79732fdc926d341b72 [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;
Marek Szyprowskic6de6cf2019-10-02 14:19:14 +0200251 if (req->request.length)
252 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;
259 else
260 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
1609static const struct usb_gadget_ops dwc3_gadget_ops = {
1610 .get_frame = dwc3_gadget_get_frame,
1611 .wakeup = dwc3_gadget_wakeup,
1612 .set_selfpowered = dwc3_gadget_set_selfpowered,
1613 .pullup = dwc3_gadget_pullup,
1614 .udc_start = dwc3_gadget_start,
1615 .udc_stop = dwc3_gadget_stop,
1616};
1617
1618/* -------------------------------------------------------------------------- */
1619
1620static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1621 u8 num, u32 direction)
1622{
1623 struct dwc3_ep *dep;
1624 u8 i;
1625
1626 for (i = 0; i < num; i++) {
1627 u8 epnum = (i << 1) | (!!direction);
1628
1629 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1630 if (!dep)
1631 return -ENOMEM;
1632
1633 dep->dwc = dwc;
1634 dep->number = epnum;
1635 dep->direction = !!direction;
1636 dwc->eps[epnum] = dep;
1637
1638 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1639 (epnum & 1) ? "in" : "out");
1640
1641 dep->endpoint.name = dep->name;
1642
1643 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1644
1645 if (epnum == 0 || epnum == 1) {
1646 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1647 dep->endpoint.maxburst = 1;
1648 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1649 if (!epnum)
1650 dwc->gadget.ep0 = &dep->endpoint;
1651 } else {
1652 int ret;
1653
Lukasz Majewskib0abde92015-03-03 17:32:14 +01001654 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301655 dep->endpoint.max_streams = 15;
1656 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1657 list_add_tail(&dep->endpoint.ep_list,
1658 &dwc->gadget.ep_list);
1659
1660 ret = dwc3_alloc_trb_pool(dep);
1661 if (ret)
1662 return ret;
1663 }
1664
1665 INIT_LIST_HEAD(&dep->request_list);
1666 INIT_LIST_HEAD(&dep->req_queued);
1667 }
1668
1669 return 0;
1670}
1671
1672static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1673{
1674 int ret;
1675
1676 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1677
1678 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1679 if (ret < 0) {
1680 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1681 return ret;
1682 }
1683
1684 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1685 if (ret < 0) {
1686 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1687 return ret;
1688 }
1689
1690 return 0;
1691}
1692
1693static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1694{
1695 struct dwc3_ep *dep;
1696 u8 epnum;
1697
1698 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1699 dep = dwc->eps[epnum];
1700 if (!dep)
1701 continue;
1702 /*
1703 * Physical endpoints 0 and 1 are special; they form the
1704 * bi-directional USB endpoint 0.
1705 *
1706 * For those two physical endpoints, we don't allocate a TRB
1707 * pool nor do we add them the endpoints list. Due to that, we
1708 * shouldn't do these two operations otherwise we would end up
1709 * with all sorts of bugs when removing dwc3.ko.
1710 */
1711 if (epnum != 0 && epnum != 1) {
1712 dwc3_free_trb_pool(dep);
1713 list_del(&dep->endpoint.ep_list);
1714 }
1715
1716 kfree(dep);
1717 }
1718}
1719
1720/* -------------------------------------------------------------------------- */
1721
1722static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1723 struct dwc3_request *req, struct dwc3_trb *trb,
1724 const struct dwc3_event_depevt *event, int status)
1725{
1726 unsigned int count;
1727 unsigned int s_pkt = 0;
1728 unsigned int trb_status;
1729
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301730 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1731 /*
1732 * We continue despite the error. There is not much we
1733 * can do. If we don't clean it up we loop forever. If
1734 * we skip the TRB then it gets overwritten after a
1735 * while since we use them in a ring buffer. A BUG()
1736 * would help. Lets hope that if this occurs, someone
1737 * fixes the root cause instead of looking away :)
1738 */
1739 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1740 dep->name, trb);
1741 count = trb->size & DWC3_TRB_SIZE_MASK;
1742
1743 if (dep->direction) {
1744 if (count) {
1745 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1746 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1747 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1748 dep->name);
1749 /*
1750 * If missed isoc occurred and there is
1751 * no request queued then issue END
1752 * TRANSFER, so that core generates
1753 * next xfernotready and we will issue
1754 * a fresh START TRANSFER.
1755 * If there are still queued request
1756 * then wait, do not issue either END
1757 * or UPDATE TRANSFER, just attach next
1758 * request in request_list during
1759 * giveback.If any future queued request
1760 * is successfully transferred then we
1761 * will issue UPDATE TRANSFER for all
1762 * request in the request_list.
1763 */
1764 dep->flags |= DWC3_EP_MISSED_ISOC;
1765 } else {
1766 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1767 dep->name);
1768 status = -ECONNRESET;
1769 }
1770 } else {
1771 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1772 }
1773 } else {
1774 if (count && (event->status & DEPEVT_STATUS_SHORT))
1775 s_pkt = 1;
1776 }
1777
1778 /*
1779 * We assume here we will always receive the entire data block
1780 * which we should receive. Meaning, if we program RX to
1781 * receive 4K but we receive only 2K, we assume that's all we
1782 * should receive and we simply bounce the request back to the
1783 * gadget driver for further processing.
1784 */
1785 req->request.actual += req->request.length - count;
1786 if (s_pkt)
1787 return 1;
1788 if ((event->status & DEPEVT_STATUS_LST) &&
1789 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1790 DWC3_TRB_CTRL_HWO)))
1791 return 1;
1792 if ((event->status & DEPEVT_STATUS_IOC) &&
1793 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1794 return 1;
1795 return 0;
1796}
1797
1798static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1799 const struct dwc3_event_depevt *event, int status)
1800{
1801 struct dwc3_request *req;
1802 struct dwc3_trb *trb;
1803 unsigned int slot;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301804
Lukasz Majewski5eee56a2015-03-03 17:32:15 +01001805 req = next_request(&dep->req_queued);
1806 if (!req) {
1807 WARN_ON_ONCE(1);
1808 return 1;
1809 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301810
Lukasz Majewski5eee56a2015-03-03 17:32:15 +01001811 slot = req->start_slot;
1812 if ((slot == DWC3_TRB_NUM - 1) &&
1813 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1814 slot++;
1815 slot %= DWC3_TRB_NUM;
1816 trb = &dep->trb_pool[slot];
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301817
Philipp Tomsiched121672017-04-06 16:58:52 +02001818 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
Lukasz Majewski5eee56a2015-03-03 17:32:15 +01001819 __dwc3_cleanup_done_trbs(dwc, dep, req, trb, event, status);
1820 dwc3_gadget_giveback(dep, req, status);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05301821
1822 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1823 list_empty(&dep->req_queued)) {
1824 if (list_empty(&dep->request_list)) {
1825 /*
1826 * If there is no entry in request list then do
1827 * not issue END TRANSFER now. Just set PENDING
1828 * flag, so that END TRANSFER is issued when an
1829 * entry is added into request list.
1830 */
1831 dep->flags = DWC3_EP_PENDING_REQUEST;
1832 } else {
1833 dwc3_stop_active_transfer(dwc, dep->number, true);
1834 dep->flags = DWC3_EP_ENABLED;
1835 }
1836 return 1;
1837 }
1838
1839 return 1;
1840}
1841
1842static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1843 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1844{
1845 unsigned status = 0;
1846 int clean_busy;
1847
1848 if (event->status & DEPEVT_STATUS_BUSERR)
1849 status = -ECONNRESET;
1850
1851 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1852 if (clean_busy)
1853 dep->flags &= ~DWC3_EP_BUSY;
1854
1855 /*
1856 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1857 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1858 */
1859 if (dwc->revision < DWC3_REVISION_183A) {
1860 u32 reg;
1861 int i;
1862
1863 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1864 dep = dwc->eps[i];
1865
1866 if (!(dep->flags & DWC3_EP_ENABLED))
1867 continue;
1868
1869 if (!list_empty(&dep->req_queued))
1870 return;
1871 }
1872
1873 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1874 reg |= dwc->u1u2;
1875 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1876
1877 dwc->u1u2 = 0;
1878 }
1879}
1880
1881static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1882 const struct dwc3_event_depevt *event)
1883{
1884 struct dwc3_ep *dep;
1885 u8 epnum = event->endpoint_number;
1886
1887 dep = dwc->eps[epnum];
1888
1889 if (!(dep->flags & DWC3_EP_ENABLED))
1890 return;
1891
1892 if (epnum == 0 || epnum == 1) {
1893 dwc3_ep0_interrupt(dwc, event);
1894 return;
1895 }
1896
1897 switch (event->endpoint_event) {
1898 case DWC3_DEPEVT_XFERCOMPLETE:
1899 dep->resource_index = 0;
1900
1901 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1902 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1903 dep->name);
1904 return;
1905 }
1906
1907 dwc3_endpoint_transfer_complete(dwc, dep, event);
1908 break;
1909 case DWC3_DEPEVT_XFERINPROGRESS:
1910 dwc3_endpoint_transfer_complete(dwc, dep, event);
1911 break;
1912 case DWC3_DEPEVT_XFERNOTREADY:
1913 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1914 dwc3_gadget_start_isoc(dwc, dep, event);
1915 } else {
1916 int ret;
1917
1918 dev_vdbg(dwc->dev, "%s: reason %s\n",
1919 dep->name, event->status &
1920 DEPEVT_STATUS_TRANSFER_ACTIVE
1921 ? "Transfer Active"
1922 : "Transfer Not Active");
1923
1924 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1925 if (!ret || ret == -EBUSY)
1926 return;
1927
1928 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1929 dep->name);
1930 }
1931
1932 break;
1933 case DWC3_DEPEVT_STREAMEVT:
1934 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
1935 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1936 dep->name);
1937 return;
1938 }
1939
1940 switch (event->status) {
1941 case DEPEVT_STREAMEVT_FOUND:
1942 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1943 event->parameters);
1944
1945 break;
1946 case DEPEVT_STREAMEVT_NOTFOUND:
1947 /* FALLTHROUGH */
1948 default:
1949 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1950 }
1951 break;
1952 case DWC3_DEPEVT_RXTXFIFOEVT:
1953 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1954 break;
1955 case DWC3_DEPEVT_EPCMDCMPLT:
1956 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
1957 break;
1958 }
1959}
1960
1961static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1962{
1963 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1964 spin_unlock(&dwc->lock);
1965 dwc->gadget_driver->disconnect(&dwc->gadget);
1966 spin_lock(&dwc->lock);
1967 }
1968}
1969
1970static void dwc3_suspend_gadget(struct dwc3 *dwc)
1971{
1972 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
1973 spin_unlock(&dwc->lock);
1974 dwc->gadget_driver->suspend(&dwc->gadget);
1975 spin_lock(&dwc->lock);
1976 }
1977}
1978
1979static void dwc3_resume_gadget(struct dwc3 *dwc)
1980{
1981 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
1982 spin_unlock(&dwc->lock);
1983 dwc->gadget_driver->resume(&dwc->gadget);
1984 }
1985}
1986
1987static void dwc3_reset_gadget(struct dwc3 *dwc)
1988{
1989 if (!dwc->gadget_driver)
1990 return;
1991
1992 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
1993 spin_unlock(&dwc->lock);
1994 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
1995 spin_lock(&dwc->lock);
1996 }
1997}
1998
1999static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2000{
2001 struct dwc3_ep *dep;
2002 struct dwc3_gadget_ep_cmd_params params;
2003 u32 cmd;
2004 int ret;
2005
2006 dep = dwc->eps[epnum];
2007
2008 if (!dep->resource_index)
2009 return;
2010
2011 /*
2012 * NOTICE: We are violating what the Databook says about the
2013 * EndTransfer command. Ideally we would _always_ wait for the
2014 * EndTransfer Command Completion IRQ, but that's causing too
2015 * much trouble synchronizing between us and gadget driver.
2016 *
2017 * We have discussed this with the IP Provider and it was
2018 * suggested to giveback all requests here, but give HW some
2019 * extra time to synchronize with the interconnect. We're using
2020 * an arbitraty 100us delay for that.
2021 *
2022 * Note also that a similar handling was tested by Synopsys
2023 * (thanks a lot Paul) and nothing bad has come out of it.
2024 * In short, what we're doing is:
2025 *
2026 * - Issue EndTransfer WITH CMDIOC bit set
2027 * - Wait 100us
2028 */
2029
2030 cmd = DWC3_DEPCMD_ENDTRANSFER;
2031 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2032 cmd |= DWC3_DEPCMD_CMDIOC;
2033 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2034 memset(&params, 0, sizeof(params));
2035 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2036 WARN_ON_ONCE(ret);
2037 dep->resource_index = 0;
2038 dep->flags &= ~DWC3_EP_BUSY;
2039 udelay(100);
2040}
2041
2042static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2043{
2044 u32 epnum;
2045
2046 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2047 struct dwc3_ep *dep;
2048
2049 dep = dwc->eps[epnum];
2050 if (!dep)
2051 continue;
2052
2053 if (!(dep->flags & DWC3_EP_ENABLED))
2054 continue;
2055
2056 dwc3_remove_requests(dwc, dep);
2057 }
2058}
2059
2060static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2061{
2062 u32 epnum;
2063
2064 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2065 struct dwc3_ep *dep;
2066 struct dwc3_gadget_ep_cmd_params params;
2067 int ret;
2068
2069 dep = dwc->eps[epnum];
2070 if (!dep)
2071 continue;
2072
2073 if (!(dep->flags & DWC3_EP_STALL))
2074 continue;
2075
2076 dep->flags &= ~DWC3_EP_STALL;
2077
2078 memset(&params, 0, sizeof(params));
2079 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2080 DWC3_DEPCMD_CLEARSTALL, &params);
2081 WARN_ON_ONCE(ret);
2082 }
2083}
2084
2085static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2086{
2087 int reg;
2088
2089 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2090 reg &= ~DWC3_DCTL_INITU1ENA;
2091 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2092
2093 reg &= ~DWC3_DCTL_INITU2ENA;
2094 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2095
2096 dwc3_disconnect_gadget(dwc);
2097 dwc->start_config_issued = false;
2098
2099 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2100 dwc->setup_packet_pending = false;
2101 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2102}
2103
2104static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2105{
2106 u32 reg;
2107
2108 /*
2109 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2110 * would cause a missing Disconnect Event if there's a
2111 * pending Setup Packet in the FIFO.
2112 *
2113 * There's no suggested workaround on the official Bug
2114 * report, which states that "unless the driver/application
2115 * is doing any special handling of a disconnect event,
2116 * there is no functional issue".
2117 *
2118 * Unfortunately, it turns out that we _do_ some special
2119 * handling of a disconnect event, namely complete all
2120 * pending transfers, notify gadget driver of the
2121 * disconnection, and so on.
2122 *
2123 * Our suggested workaround is to follow the Disconnect
2124 * Event steps here, instead, based on a setup_packet_pending
2125 * flag. Such flag gets set whenever we have a XferNotReady
2126 * event on EP0 and gets cleared on XferComplete for the
2127 * same endpoint.
2128 *
2129 * Refers to:
2130 *
2131 * STAR#9000466709: RTL: Device : Disconnect event not
2132 * generated if setup packet pending in FIFO
2133 */
2134 if (dwc->revision < DWC3_REVISION_188A) {
2135 if (dwc->setup_packet_pending)
2136 dwc3_gadget_disconnect_interrupt(dwc);
2137 }
2138
2139 dwc3_reset_gadget(dwc);
2140
2141 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2142 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2143 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2144 dwc->test_mode = false;
2145
2146 dwc3_stop_active_transfers(dwc);
2147 dwc3_clear_stall_all_ep(dwc);
2148 dwc->start_config_issued = false;
2149
2150 /* Reset device address to zero */
2151 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2152 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2153 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2154}
2155
2156static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2157{
2158 u32 reg;
2159 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2160
2161 /*
2162 * We change the clock only at SS but I dunno why I would want to do
2163 * this. Maybe it becomes part of the power saving plan.
2164 */
2165
2166 if (speed != DWC3_DSTS_SUPERSPEED)
2167 return;
2168
2169 /*
2170 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2171 * each time on Connect Done.
2172 */
2173 if (!usb30_clock)
2174 return;
2175
2176 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2177 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2178 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2179}
2180
2181static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2182{
2183 struct dwc3_ep *dep;
2184 int ret;
2185 u32 reg;
2186 u8 speed;
2187
2188 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2189 speed = reg & DWC3_DSTS_CONNECTSPD;
2190 dwc->speed = speed;
2191
2192 dwc3_update_ram_clk_sel(dwc, speed);
2193
2194 switch (speed) {
2195 case DWC3_DCFG_SUPERSPEED:
2196 /*
2197 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2198 * would cause a missing USB3 Reset event.
2199 *
2200 * In such situations, we should force a USB3 Reset
2201 * event by calling our dwc3_gadget_reset_interrupt()
2202 * routine.
2203 *
2204 * Refers to:
2205 *
2206 * STAR#9000483510: RTL: SS : USB3 reset event may
2207 * not be generated always when the link enters poll
2208 */
2209 if (dwc->revision < DWC3_REVISION_190A)
2210 dwc3_gadget_reset_interrupt(dwc);
2211
2212 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2213 dwc->gadget.ep0->maxpacket = 512;
2214 dwc->gadget.speed = USB_SPEED_SUPER;
2215 break;
2216 case DWC3_DCFG_HIGHSPEED:
2217 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2218 dwc->gadget.ep0->maxpacket = 64;
2219 dwc->gadget.speed = USB_SPEED_HIGH;
2220 break;
2221 case DWC3_DCFG_FULLSPEED2:
2222 case DWC3_DCFG_FULLSPEED1:
2223 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2224 dwc->gadget.ep0->maxpacket = 64;
2225 dwc->gadget.speed = USB_SPEED_FULL;
2226 break;
2227 case DWC3_DCFG_LOWSPEED:
2228 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2229 dwc->gadget.ep0->maxpacket = 8;
2230 dwc->gadget.speed = USB_SPEED_LOW;
2231 break;
2232 }
2233
2234 /* Enable USB2 LPM Capability */
2235
2236 if ((dwc->revision > DWC3_REVISION_194A)
2237 && (speed != DWC3_DCFG_SUPERSPEED)) {
2238 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2239 reg |= DWC3_DCFG_LPM_CAP;
2240 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2241
2242 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2243 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2244
2245 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2246
2247 /*
2248 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2249 * DCFG.LPMCap is set, core responses with an ACK and the
2250 * BESL value in the LPM token is less than or equal to LPM
2251 * NYET threshold.
2252 */
Wolfgang Denk62fb2b42021-09-27 17:42:39 +02002253 if (dwc->revision < DWC3_REVISION_240A && dwc->has_lpm_erratum)
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302254 WARN(true, "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302255
2256 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2257 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2258
2259 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2260 } else {
2261 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2262 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2263 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2264 }
2265
2266 dep = dwc->eps[0];
2267 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2268 false);
2269 if (ret) {
2270 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2271 return;
2272 }
2273
2274 dep = dwc->eps[1];
2275 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2276 false);
2277 if (ret) {
2278 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2279 return;
2280 }
2281
2282 /*
2283 * Configure PHY via GUSB3PIPECTLn if required.
2284 *
2285 * Update GTXFIFOSIZn
2286 *
2287 * In both cases reset values should be sufficient.
2288 */
2289}
2290
2291static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2292{
2293 /*
2294 * TODO take core out of low power mode when that's
2295 * implemented.
2296 */
2297
2298 dwc->gadget_driver->resume(&dwc->gadget);
2299}
2300
2301static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2302 unsigned int evtinfo)
2303{
2304 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2305 unsigned int pwropt;
2306
2307 /*
2308 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2309 * Hibernation mode enabled which would show up when device detects
2310 * host-initiated U3 exit.
2311 *
2312 * In that case, device will generate a Link State Change Interrupt
2313 * from U3 to RESUME which is only necessary if Hibernation is
2314 * configured in.
2315 *
2316 * There are no functional changes due to such spurious event and we
2317 * just need to ignore it.
2318 *
2319 * Refers to:
2320 *
2321 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2322 * operational mode
2323 */
2324 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2325 if ((dwc->revision < DWC3_REVISION_250A) &&
2326 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2327 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2328 (next == DWC3_LINK_STATE_RESUME)) {
2329 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2330 return;
2331 }
2332 }
2333
2334 /*
2335 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2336 * on the link partner, the USB session might do multiple entry/exit
2337 * of low power states before a transfer takes place.
2338 *
2339 * Due to this problem, we might experience lower throughput. The
2340 * suggested workaround is to disable DCTL[12:9] bits if we're
2341 * transitioning from U1/U2 to U0 and enable those bits again
2342 * after a transfer completes and there are no pending transfers
2343 * on any of the enabled endpoints.
2344 *
2345 * This is the first half of that workaround.
2346 *
2347 * Refers to:
2348 *
2349 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2350 * core send LGO_Ux entering U0
2351 */
2352 if (dwc->revision < DWC3_REVISION_183A) {
2353 if (next == DWC3_LINK_STATE_U0) {
2354 u32 u1u2;
2355 u32 reg;
2356
2357 switch (dwc->link_state) {
2358 case DWC3_LINK_STATE_U1:
2359 case DWC3_LINK_STATE_U2:
2360 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2361 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2362 | DWC3_DCTL_ACCEPTU2ENA
2363 | DWC3_DCTL_INITU1ENA
2364 | DWC3_DCTL_ACCEPTU1ENA);
2365
2366 if (!dwc->u1u2)
2367 dwc->u1u2 = reg & u1u2;
2368
2369 reg &= ~u1u2;
2370
2371 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2372 break;
2373 default:
2374 /* do nothing */
2375 break;
2376 }
2377 }
2378 }
2379
2380 switch (next) {
2381 case DWC3_LINK_STATE_U1:
2382 if (dwc->speed == USB_SPEED_SUPER)
2383 dwc3_suspend_gadget(dwc);
2384 break;
2385 case DWC3_LINK_STATE_U2:
2386 case DWC3_LINK_STATE_U3:
2387 dwc3_suspend_gadget(dwc);
2388 break;
2389 case DWC3_LINK_STATE_RESUME:
2390 dwc3_resume_gadget(dwc);
2391 break;
2392 default:
2393 /* do nothing */
2394 break;
2395 }
2396
2397 dwc->link_state = next;
2398}
2399
2400static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2401 unsigned int evtinfo)
2402{
Lukasz Majewskidc6d2402015-03-03 17:32:08 +01002403 unsigned int is_ss = evtinfo & (1UL << 4);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302404
2405 /**
2406 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2407 * have a known issue which can cause USB CV TD.9.23 to fail
2408 * randomly.
2409 *
2410 * Because of this issue, core could generate bogus hibernation
2411 * events which SW needs to ignore.
2412 *
2413 * Refers to:
2414 *
2415 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2416 * Device Fallback from SuperSpeed
2417 */
2418 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2419 return;
2420
2421 /* enter hibernation here */
2422}
2423
2424static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2425 const struct dwc3_event_devt *event)
2426{
2427 switch (event->type) {
2428 case DWC3_DEVICE_EVENT_DISCONNECT:
2429 dwc3_gadget_disconnect_interrupt(dwc);
2430 break;
2431 case DWC3_DEVICE_EVENT_RESET:
2432 dwc3_gadget_reset_interrupt(dwc);
2433 break;
2434 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2435 dwc3_gadget_conndone_interrupt(dwc);
2436 break;
2437 case DWC3_DEVICE_EVENT_WAKEUP:
2438 dwc3_gadget_wakeup_interrupt(dwc);
2439 break;
2440 case DWC3_DEVICE_EVENT_HIBER_REQ:
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302441 if (!dwc->has_hibernation) {
2442 WARN(1 ,"unexpected hibernation event\n");
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302443 break;
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302444 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302445 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2446 break;
2447 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2448 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2449 break;
2450 case DWC3_DEVICE_EVENT_EOPF:
2451 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2452 break;
2453 case DWC3_DEVICE_EVENT_SOF:
2454 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2455 break;
2456 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2457 dev_vdbg(dwc->dev, "Erratic Error\n");
2458 break;
2459 case DWC3_DEVICE_EVENT_CMD_CMPL:
2460 dev_vdbg(dwc->dev, "Command Complete\n");
2461 break;
2462 case DWC3_DEVICE_EVENT_OVERFLOW:
2463 dev_vdbg(dwc->dev, "Overflow\n");
2464 break;
2465 default:
2466 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2467 }
2468}
2469
2470static void dwc3_process_event_entry(struct dwc3 *dwc,
2471 const union dwc3_event *event)
2472{
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302473 /* Endpoint IRQ, handle it and return early */
2474 if (event->type.is_devspec == 0) {
2475 /* depevt */
2476 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2477 }
2478
2479 switch (event->type.type) {
2480 case DWC3_EVENT_TYPE_DEV:
2481 dwc3_gadget_interrupt(dwc, &event->devt);
2482 break;
2483 /* REVISIT what to do with Carkit and I2C events ? */
2484 default:
2485 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2486 }
2487}
2488
2489static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2490{
2491 struct dwc3_event_buffer *evt;
2492 irqreturn_t ret = IRQ_NONE;
2493 int left;
2494 u32 reg;
2495
2496 evt = dwc->ev_buffs[buf];
2497 left = evt->count;
2498
2499 if (!(evt->flags & DWC3_EVENT_PENDING))
2500 return IRQ_NONE;
2501
2502 while (left > 0) {
2503 union dwc3_event event;
2504
2505 event.raw = *(u32 *) (evt->buf + evt->lpos);
2506
2507 dwc3_process_event_entry(dwc, &event);
2508
2509 /*
2510 * FIXME we wrap around correctly to the next entry as
2511 * almost all entries are 4 bytes in size. There is one
2512 * entry which has 12 bytes which is a regular entry
2513 * followed by 8 bytes data. ATM I don't know how
2514 * things are organized if we get next to the a
2515 * boundary so I worry about that once we try to handle
2516 * that.
2517 */
2518 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2519 left -= 4;
2520
2521 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2522 }
2523
2524 evt->count = 0;
2525 evt->flags &= ~DWC3_EVENT_PENDING;
2526 ret = IRQ_HANDLED;
2527
2528 /* Unmask interrupt */
2529 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2530 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2531 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2532
2533 return ret;
2534}
2535
2536static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2537{
2538 struct dwc3 *dwc = _dwc;
2539 unsigned long flags;
2540 irqreturn_t ret = IRQ_NONE;
2541 int i;
2542
2543 spin_lock_irqsave(&dwc->lock, flags);
2544
2545 for (i = 0; i < dwc->num_event_buffers; i++)
2546 ret |= dwc3_process_event_buf(dwc, i);
2547
2548 spin_unlock_irqrestore(&dwc->lock, flags);
2549
2550 return ret;
2551}
2552
2553static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
2554{
2555 struct dwc3_event_buffer *evt;
2556 u32 count;
2557 u32 reg;
2558
2559 evt = dwc->ev_buffs[buf];
2560
2561 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2562 count &= DWC3_GEVNTCOUNT_MASK;
2563 if (!count)
2564 return IRQ_NONE;
2565
2566 evt->count = count;
2567 evt->flags |= DWC3_EVENT_PENDING;
2568
2569 /* Mask interrupt */
2570 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2571 reg |= DWC3_GEVNTSIZ_INTMASK;
2572 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2573
2574 return IRQ_WAKE_THREAD;
2575}
2576
2577static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2578{
2579 struct dwc3 *dwc = _dwc;
2580 int i;
2581 irqreturn_t ret = IRQ_NONE;
2582
2583 spin_lock(&dwc->lock);
2584
2585 for (i = 0; i < dwc->num_event_buffers; i++) {
2586 irqreturn_t status;
2587
2588 status = dwc3_check_event_buf(dwc, i);
2589 if (status == IRQ_WAKE_THREAD)
2590 ret = status;
2591 }
2592
2593 spin_unlock(&dwc->lock);
2594
2595 return ret;
2596}
2597
2598/**
2599 * dwc3_gadget_init - Initializes gadget related registers
2600 * @dwc: pointer to our controller context structure
2601 *
2602 * Returns 0 on success otherwise negative errno.
2603 */
2604int dwc3_gadget_init(struct dwc3 *dwc)
2605{
2606 int ret;
2607
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302608 dwc->ctrl_req = dma_alloc_coherent(sizeof(*dwc->ctrl_req),
2609 (unsigned long *)&dwc->ctrl_req_addr);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302610 if (!dwc->ctrl_req) {
2611 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2612 ret = -ENOMEM;
2613 goto err0;
2614 }
2615
Kishon Vijay Abraham If18ed9a2015-02-23 18:40:15 +05302616 dwc->ep0_trb = dma_alloc_coherent(sizeof(*dwc->ep0_trb) * 2,
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302617 (unsigned long *)&dwc->ep0_trb_addr);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302618 if (!dwc->ep0_trb) {
2619 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2620 ret = -ENOMEM;
2621 goto err1;
2622 }
2623
Kishon Vijay Abraham Ic7bdfe32015-02-23 18:40:13 +05302624 dwc->setup_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
2625 DWC3_EP0_BOUNCE_SIZE);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302626 if (!dwc->setup_buf) {
2627 ret = -ENOMEM;
2628 goto err2;
2629 }
2630
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302631 dwc->ep0_bounce = dma_alloc_coherent(DWC3_EP0_BOUNCE_SIZE,
2632 (unsigned long *)&dwc->ep0_bounce_addr);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302633 if (!dwc->ep0_bounce) {
2634 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2635 ret = -ENOMEM;
2636 goto err3;
2637 }
2638
2639 dwc->gadget.ops = &dwc3_gadget_ops;
2640 dwc->gadget.max_speed = USB_SPEED_SUPER;
2641 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302642 dwc->gadget.name = "dwc3-gadget";
2643
2644 /*
2645 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2646 * on ep out.
2647 */
2648 dwc->gadget.quirk_ep_out_aligned_size = true;
2649
2650 /*
2651 * REVISIT: Here we should clear all pending IRQs to be
2652 * sure we're starting from a well known location.
2653 */
2654
2655 ret = dwc3_gadget_init_endpoints(dwc);
2656 if (ret)
2657 goto err4;
2658
Mugunthan V N5f7ff712018-05-18 13:15:04 +02002659 ret = usb_add_gadget_udc((struct device *)dwc->dev, &dwc->gadget);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302660 if (ret) {
2661 dev_err(dwc->dev, "failed to register udc\n");
2662 goto err4;
2663 }
2664
2665 return 0;
2666
2667err4:
2668 dwc3_gadget_free_endpoints(dwc);
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302669 dma_free_coherent(dwc->ep0_bounce);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302670
2671err3:
2672 kfree(dwc->setup_buf);
2673
2674err2:
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302675 dma_free_coherent(dwc->ep0_trb);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302676
2677err1:
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302678 dma_free_coherent(dwc->ctrl_req);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302679
2680err0:
2681 return ret;
2682}
2683
2684/* -------------------------------------------------------------------------- */
2685
2686void dwc3_gadget_exit(struct dwc3 *dwc)
2687{
2688 usb_del_gadget_udc(&dwc->gadget);
2689
2690 dwc3_gadget_free_endpoints(dwc);
2691
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302692 dma_free_coherent(dwc->ep0_bounce);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302693
2694 kfree(dwc->setup_buf);
2695
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302696 dma_free_coherent(dwc->ep0_trb);
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302697
Kishon Vijay Abraham I35ffd282015-02-23 18:39:58 +05302698 dma_free_coherent(dwc->ctrl_req);
2699}
2700
2701/**
2702 * dwc3_gadget_uboot_handle_interrupt - handle dwc3 gadget interrupt
2703 * @dwc: struct dwce *
2704 *
2705 * Handles ep0 and gadget interrupt
2706 *
2707 * Should be called from dwc3 core.
2708 */
2709void dwc3_gadget_uboot_handle_interrupt(struct dwc3 *dwc)
2710{
Marek Szyprowskib0bacbe2015-03-03 17:32:12 +01002711 int ret = dwc3_interrupt(0, dwc);
2712
2713 if (ret == IRQ_WAKE_THREAD) {
2714 int i;
2715 struct dwc3_event_buffer *evt;
2716
Philipp Tomsich8e17c162017-04-06 16:58:53 +02002717 dwc3_thread_interrupt(0, dwc);
2718
2719 /* Clean + Invalidate the buffers after touching them */
Marek Szyprowskib0bacbe2015-03-03 17:32:12 +01002720 for (i = 0; i < dwc->num_event_buffers; i++) {
2721 evt = dwc->ev_buffs[i];
Philipp Tomsiched121672017-04-06 16:58:52 +02002722 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
Marek Szyprowskib0bacbe2015-03-03 17:32:12 +01002723 }
Marek Szyprowskib0bacbe2015-03-03 17:32:12 +01002724 }
Kishon Vijay Abraham I1530fe32015-02-23 18:39:50 +05302725}