blob: 750364eb11e12c2eabbe89a99b1af5a0a24434bd [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi8598bde2012-01-02 18:55:57 +0200142 return -ETIMEDOUT;
143}
144
John Youndca01192016-05-19 17:26:05 -0700145/**
146 * dwc3_ep_inc_trb() - Increment a TRB index.
147 * @index - Pointer to the TRB index to increment.
148 *
149 * The index should never point to the link TRB. After incrementing,
150 * if it is point to the link TRB, wrap around to the beginning. The
151 * link TRB is always at the last TRB entry.
152 */
153static void dwc3_ep_inc_trb(u8 *index)
154{
155 (*index)++;
156 if (*index == (DWC3_TRB_NUM - 1))
157 *index = 0;
158}
159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200161{
John Youndca01192016-05-19 17:26:05 -0700162 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300163}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200164
Felipe Balbief966b92016-04-05 13:09:51 +0300165static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
166{
John Youndca01192016-05-19 17:26:05 -0700167 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200168}
169
Felipe Balbi72246da2011-08-19 18:10:58 +0300170void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
171 int status)
172{
173 struct dwc3 *dwc = dep->dwc;
174
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300175 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300176 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200177 req->trb = NULL;
Felipe Balbie62c5bc52016-10-25 13:47:21 +0300178 req->remaining = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300179
180 if (req->request.status == -EINPROGRESS)
181 req->request.status = status;
182
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300183 usb_gadget_unmap_request_by_dev(dwc->sysdev,
184 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300185
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500186 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200189 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300190 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300191
192 if (dep->number > 1)
193 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300194}
195
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500196int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300197{
198 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300199 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300200 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300201 u32 reg;
202
203 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
204 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
205
206 do {
207 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
208 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300209 status = DWC3_DGCMD_STATUS(reg);
210 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300211 ret = -EINVAL;
212 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300213 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100214 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300215
216 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300217 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300218 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300219 }
220
Felipe Balbi71f7e702016-05-23 14:16:19 +0300221 trace_dwc3_gadget_generic_cmd(cmd, param, status);
222
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300223 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300224}
225
Felipe Balbic36d8e92016-04-04 12:46:33 +0300226static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
227
Felipe Balbi2cd47182016-04-12 16:42:43 +0300228int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
229 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300230{
Felipe Balbi8897a762016-09-22 10:56:08 +0300231 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300232 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200233 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300234 u32 reg;
235
Felipe Balbi0933df12016-05-23 14:02:33 +0300236 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300237 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300238 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300239
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300240 /*
241 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
242 * we're issuing an endpoint command, we must check if
243 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
244 *
245 * We will also set SUSPHY bit to what it was before returning as stated
246 * by the same section on Synopsys databook.
247 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300248 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
249 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
250 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
251 susphy = true;
252 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
253 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
254 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300255 }
256
Felipe Balbi59999142016-09-22 12:25:28 +0300257 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300258 int needs_wakeup;
259
260 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
261 dwc->link_state == DWC3_LINK_STATE_U2 ||
262 dwc->link_state == DWC3_LINK_STATE_U3);
263
264 if (unlikely(needs_wakeup)) {
265 ret = __dwc3_gadget_wakeup(dwc);
266 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
267 ret);
268 }
269 }
270
Felipe Balbi2eb88012016-04-12 16:53:39 +0300271 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
272 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
273 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300274
Felipe Balbi8897a762016-09-22 10:56:08 +0300275 /*
276 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
277 * not relying on XferNotReady, we can make use of a special "No
278 * Response Update Transfer" command where we should clear both CmdAct
279 * and CmdIOC bits.
280 *
281 * With this, we don't need to wait for command completion and can
282 * straight away issue further commands to the endpoint.
283 *
284 * NOTICE: We're making an assumption that control endpoints will never
285 * make use of Update Transfer command. This is a safe assumption
286 * because we can never have more than one request at a time with
287 * Control Endpoints. If anybody changes that assumption, this chunk
288 * needs to be updated accordingly.
289 */
290 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
291 !usb_endpoint_xfer_isoc(desc))
292 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
293 else
294 cmd |= DWC3_DEPCMD_CMDACT;
295
296 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300297 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300298 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300299 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300300 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000301
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000302 switch (cmd_status) {
303 case 0:
304 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300305 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000306 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000307 ret = -EINVAL;
308 break;
309 case DEPEVT_TRANSFER_BUS_EXPIRY:
310 /*
311 * SW issues START TRANSFER command to
312 * isochronous ep with future frame interval. If
313 * future interval time has already passed when
314 * core receives the command, it will respond
315 * with an error status of 'Bus Expiry'.
316 *
317 * Instead of always returning -EINVAL, let's
318 * give a hint to the gadget driver that this is
319 * the case by returning -EAGAIN.
320 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000321 ret = -EAGAIN;
322 break;
323 default:
324 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
325 }
326
Felipe Balbic0ca3242016-04-04 09:11:51 +0300327 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300328 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300329 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300330
Felipe Balbif6bb2252016-05-23 13:53:34 +0300331 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300332 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300333 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300334 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300335
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
337
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300338 if (ret == 0) {
339 switch (DWC3_DEPCMD_CMD(cmd)) {
340 case DWC3_DEPCMD_STARTTRANSFER:
341 dep->flags |= DWC3_EP_TRANSFER_STARTED;
342 break;
343 case DWC3_DEPCMD_ENDTRANSFER:
344 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
345 break;
346 default:
347 /* nothing */
348 break;
349 }
350 }
351
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300352 if (unlikely(susphy)) {
353 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
354 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
355 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
356 }
357
Felipe Balbic0ca3242016-04-04 09:11:51 +0300358 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300359}
360
John Youn50c763f2016-05-31 17:49:56 -0700361static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
362{
363 struct dwc3 *dwc = dep->dwc;
364 struct dwc3_gadget_ep_cmd_params params;
365 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
366
367 /*
368 * As of core revision 2.60a the recommended programming model
369 * is to set the ClearPendIN bit when issuing a Clear Stall EP
370 * command for IN endpoints. This is to prevent an issue where
371 * some (non-compliant) hosts may not send ACK TPs for pending
372 * IN transfers due to a mishandled error condition. Synopsys
373 * STAR 9000614252.
374 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800375 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
376 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700377 cmd |= DWC3_DEPCMD_CLEARPENDIN;
378
379 memset(&params, 0, sizeof(params));
380
Felipe Balbi2cd47182016-04-12 16:42:43 +0300381 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700382}
383
Felipe Balbi72246da2011-08-19 18:10:58 +0300384static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200385 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300386{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300387 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300388
389 return dep->trb_pool_dma + offset;
390}
391
392static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
393{
394 struct dwc3 *dwc = dep->dwc;
395
396 if (dep->trb_pool)
397 return 0;
398
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530399 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300400 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
401 &dep->trb_pool_dma, GFP_KERNEL);
402 if (!dep->trb_pool) {
403 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
404 dep->name);
405 return -ENOMEM;
406 }
407
408 return 0;
409}
410
411static void dwc3_free_trb_pool(struct dwc3_ep *dep)
412{
413 struct dwc3 *dwc = dep->dwc;
414
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530415 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 dep->trb_pool, dep->trb_pool_dma);
417
418 dep->trb_pool = NULL;
419 dep->trb_pool_dma = 0;
420}
421
John Younc4509602016-02-16 20:10:53 -0800422static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
423
424/**
425 * dwc3_gadget_start_config - Configure EP resources
426 * @dwc: pointer to our controller context structure
427 * @dep: endpoint that is being enabled
428 *
429 * The assignment of transfer resources cannot perfectly follow the
430 * data book due to the fact that the controller driver does not have
431 * all knowledge of the configuration in advance. It is given this
432 * information piecemeal by the composite gadget framework after every
433 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
434 * programming model in this scenario can cause errors. For two
435 * reasons:
436 *
437 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
438 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
439 * multiple interfaces.
440 *
441 * 2) The databook does not mention doing more DEPXFERCFG for new
442 * endpoint on alt setting (8.1.6).
443 *
444 * The following simplified method is used instead:
445 *
446 * All hardware endpoints can be assigned a transfer resource and this
447 * setting will stay persistent until either a core reset or
448 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
449 * do DEPXFERCFG for every hardware endpoint as well. We are
450 * guaranteed that there are as many transfer resources as endpoints.
451 *
452 * This function is called for each endpoint when it is being enabled
453 * but is triggered only when called for EP0-out, which always happens
454 * first, and which should only happen in one of the above conditions.
455 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300456static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
457{
458 struct dwc3_gadget_ep_cmd_params params;
459 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800460 int i;
461 int ret;
462
463 if (dep->number)
464 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
466 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800467 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
Felipe Balbi2cd47182016-04-12 16:42:43 +0300469 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800470 if (ret)
471 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300472
John Younc4509602016-02-16 20:10:53 -0800473 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
474 struct dwc3_ep *dep = dwc->eps[i];
475
476 if (!dep)
477 continue;
478
479 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
480 if (ret)
481 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300482 }
483
484 return 0;
485}
486
487static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300488 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300489{
John Youn39ebb052016-11-09 16:36:28 -0800490 const struct usb_ss_ep_comp_descriptor *comp_desc;
491 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300492 struct dwc3_gadget_ep_cmd_params params;
493
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300494 if (dev_WARN_ONCE(dwc->dev, modify && restore,
495 "Can't modify and restore\n"))
496 return -EINVAL;
497
John Youn39ebb052016-11-09 16:36:28 -0800498 comp_desc = dep->endpoint.comp_desc;
499 desc = dep->endpoint.desc;
500
Felipe Balbi72246da2011-08-19 18:10:58 +0300501 memset(&params, 0x00, sizeof(params));
502
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300503 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900504 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
505
506 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800507 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300508 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300509 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900510 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300511
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300512 if (modify) {
513 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
514 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600515 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
516 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300517 } else {
518 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600519 }
520
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300521 if (usb_endpoint_xfer_control(desc))
522 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300523
524 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
525 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300526
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200527 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300528 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
529 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300530 dep->stream_capable = true;
531 }
532
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500533 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300534 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535
536 /*
537 * We are doing 1:1 mapping for endpoints, meaning
538 * Physical Endpoints 2 maps to Logical Endpoint 2 and
539 * so on. We consider the direction bit as part of the physical
540 * endpoint number. So USB endpoint 0x81 is 0x03.
541 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300542 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
544 /*
545 * We must use the lower 16 TX FIFOs even though
546 * HW might have more
547 */
548 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300549 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300550
551 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300552 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 dep->interval = 1 << (desc->bInterval - 1);
554 }
555
Felipe Balbi2cd47182016-04-12 16:42:43 +0300556 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300557}
558
559static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
560{
561 struct dwc3_gadget_ep_cmd_params params;
562
563 memset(&params, 0x00, sizeof(params));
564
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300565 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
Felipe Balbi2cd47182016-04-12 16:42:43 +0300567 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
568 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300569}
570
571/**
572 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
573 * @dep: endpoint to be initialized
574 * @desc: USB Endpoint Descriptor
575 *
576 * Caller should take care of locking
577 */
578static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300579 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300580{
John Youn39ebb052016-11-09 16:36:28 -0800581 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800583
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300585 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300586
587 if (!(dep->flags & DWC3_EP_ENABLED)) {
588 ret = dwc3_gadget_start_config(dwc, dep);
589 if (ret)
590 return ret;
591 }
592
John Youn39ebb052016-11-09 16:36:28 -0800593 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 if (ret)
595 return ret;
596
597 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200598 struct dwc3_trb *trb_st_hw;
599 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300600
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 dep->type = usb_endpoint_type(desc);
602 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800603 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300604
605 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
606 reg |= DWC3_DALEPENA_EP(dep->number);
607 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
608
Baolin Wang76a638f2016-10-31 19:38:36 +0800609 init_waitqueue_head(&dep->wait_end_transfer);
610
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300611 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200612 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300613
John Youn0d257442016-05-19 17:26:08 -0700614 /* Initialize the TRB ring */
615 dep->trb_dequeue = 0;
616 dep->trb_enqueue = 0;
617 memset(dep->trb_pool, 0,
618 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
619
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300620 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 trb_st_hw = &dep->trb_pool[0];
622
Felipe Balbif6bafc62012-02-06 11:04:53 +0200623 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200624 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
625 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
626 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
627 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300628 }
629
Felipe Balbia97ea992016-09-29 16:28:56 +0300630 /*
631 * Issue StartTransfer here with no-op TRB so we can always rely on No
632 * Response Update Transfer command.
633 */
634 if (usb_endpoint_xfer_bulk(desc)) {
635 struct dwc3_gadget_ep_cmd_params params;
636 struct dwc3_trb *trb;
637 dma_addr_t trb_dma;
638 u32 cmd;
639
640 memset(&params, 0, sizeof(params));
641 trb = &dep->trb_pool[0];
642 trb_dma = dwc3_trb_dma_offset(dep, trb);
643
644 params.param0 = upper_32_bits(trb_dma);
645 params.param1 = lower_32_bits(trb_dma);
646
647 cmd = DWC3_DEPCMD_STARTTRANSFER;
648
649 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
650 if (ret < 0)
651 return ret;
652
653 dep->flags |= DWC3_EP_BUSY;
654
655 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
656 WARN_ON_ONCE(!dep->resource_index);
657 }
658
Felipe Balbi2870e502016-11-03 13:53:29 +0200659
660out:
661 trace_dwc3_gadget_ep_enable(dep);
662
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 return 0;
664}
665
Paul Zimmermanb992e682012-04-27 14:17:35 +0300666static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200667static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300668{
669 struct dwc3_request *req;
670
Felipe Balbi0e146022016-06-21 10:32:02 +0300671 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300672
Felipe Balbi0e146022016-06-21 10:32:02 +0300673 /* - giveback all requests to gadget driver */
674 while (!list_empty(&dep->started_list)) {
675 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200676
Felipe Balbi0e146022016-06-21 10:32:02 +0300677 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200678 }
679
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200680 while (!list_empty(&dep->pending_list)) {
681 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300682
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200683 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300685}
686
687/**
688 * __dwc3_gadget_ep_disable - Disables a HW endpoint
689 * @dep: the endpoint to disable
690 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200691 * This function also removes requests which are currently processed ny the
692 * hardware and those which are not yet scheduled.
693 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300694 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300695static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
696{
697 struct dwc3 *dwc = dep->dwc;
698 u32 reg;
699
Felipe Balbi2870e502016-11-03 13:53:29 +0200700 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500701
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200702 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300703
Felipe Balbi687ef982014-04-16 10:30:33 -0500704 /* make sure HW endpoint isn't stalled */
705 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500706 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500707
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
709 reg &= ~DWC3_DALEPENA_EP(dep->number);
710 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
711
Felipe Balbi879631a2011-09-30 10:58:47 +0300712 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300713 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800714 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
John Youn39ebb052016-11-09 16:36:28 -0800716 /* Clear out the ep descriptors for non-ep0 */
717 if (dep->number > 1) {
718 dep->endpoint.comp_desc = NULL;
719 dep->endpoint.desc = NULL;
720 }
721
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 return 0;
723}
724
725/* -------------------------------------------------------------------------- */
726
727static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
728 const struct usb_endpoint_descriptor *desc)
729{
730 return -EINVAL;
731}
732
733static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
734{
735 return -EINVAL;
736}
737
738/* -------------------------------------------------------------------------- */
739
740static int dwc3_gadget_ep_enable(struct usb_ep *ep,
741 const struct usb_endpoint_descriptor *desc)
742{
743 struct dwc3_ep *dep;
744 struct dwc3 *dwc;
745 unsigned long flags;
746 int ret;
747
748 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
749 pr_debug("dwc3: invalid parameters\n");
750 return -EINVAL;
751 }
752
753 if (!desc->wMaxPacketSize) {
754 pr_debug("dwc3: missing wMaxPacketSize\n");
755 return -EINVAL;
756 }
757
758 dep = to_dwc3_ep(ep);
759 dwc = dep->dwc;
760
Felipe Balbi95ca9612015-12-10 13:08:20 -0600761 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
762 "%s is already enabled\n",
763 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300764 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300765
Felipe Balbi72246da2011-08-19 18:10:58 +0300766 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800767 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300768 spin_unlock_irqrestore(&dwc->lock, flags);
769
770 return ret;
771}
772
773static int dwc3_gadget_ep_disable(struct usb_ep *ep)
774{
775 struct dwc3_ep *dep;
776 struct dwc3 *dwc;
777 unsigned long flags;
778 int ret;
779
780 if (!ep) {
781 pr_debug("dwc3: invalid parameters\n");
782 return -EINVAL;
783 }
784
785 dep = to_dwc3_ep(ep);
786 dwc = dep->dwc;
787
Felipe Balbi95ca9612015-12-10 13:08:20 -0600788 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
789 "%s is already disabled\n",
790 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300792
Felipe Balbi72246da2011-08-19 18:10:58 +0300793 spin_lock_irqsave(&dwc->lock, flags);
794 ret = __dwc3_gadget_ep_disable(dep);
795 spin_unlock_irqrestore(&dwc->lock, flags);
796
797 return ret;
798}
799
800static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
801 gfp_t gfp_flags)
802{
803 struct dwc3_request *req;
804 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300805
806 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900807 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300809
810 req->epnum = dep->number;
811 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300812
Felipe Balbi68d34c82016-05-30 13:34:58 +0300813 dep->allocated_requests++;
814
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500815 trace_dwc3_alloc_request(req);
816
Felipe Balbi72246da2011-08-19 18:10:58 +0300817 return &req->request;
818}
819
820static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
821 struct usb_request *request)
822{
823 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300824 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300825
Felipe Balbi68d34c82016-05-30 13:34:58 +0300826 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500827 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300828 kfree(req);
829}
830
Felipe Balbi2c78c022016-08-12 13:13:10 +0300831static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
832
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200833static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
834 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
835 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200836{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300837 struct dwc3 *dwc = dep->dwc;
838 struct usb_gadget *gadget = &dwc->gadget;
839 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200840
Felipe Balbief966b92016-04-05 13:09:51 +0300841 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530842
Felipe Balbif6bafc62012-02-06 11:04:53 +0200843 trb->size = DWC3_TRB_SIZE_LENGTH(length);
844 trb->bpl = lower_32_bits(dma);
845 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200846
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200847 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200848 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200849 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200850 break;
851
852 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300853 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530854 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300855
856 if (speed == USB_SPEED_HIGH) {
857 struct usb_ep *ep = &dep->endpoint;
858 trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
859 }
860 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530861 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300862 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200863
864 /* always enable Interrupt on Missed ISOC */
865 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200866 break;
867
868 case USB_ENDPOINT_XFER_BULK:
869 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200870 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200871 break;
872 default:
873 /*
874 * This is only possible with faulty memory because we
875 * checked it already :)
876 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300877 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
878 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200879 }
880
Felipe Balbica4d44e2016-03-10 13:53:27 +0200881 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300882 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300883 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600884
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200885 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300886 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
887 }
888
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200889 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300890 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300891 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200892
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530893 if (chain)
894 trb->ctrl |= DWC3_TRB_CTRL_CHN;
895
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200896 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200897 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200898
899 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500900
901 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200902}
903
John Youn361572b2016-05-19 17:26:17 -0700904/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200905 * dwc3_prepare_one_trb - setup one TRB from one request
906 * @dep: endpoint for which this request is prepared
907 * @req: dwc3_request pointer
908 * @chain: should this TRB be chained to the next?
909 * @node: only for isochronous endpoints. First TRB needs different type.
910 */
911static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
912 struct dwc3_request *req, unsigned chain, unsigned node)
913{
914 struct dwc3_trb *trb;
915 unsigned length = req->request.length;
916 unsigned stream_id = req->request.stream_id;
917 unsigned short_not_ok = req->request.short_not_ok;
918 unsigned no_interrupt = req->request.no_interrupt;
919 dma_addr_t dma = req->request.dma;
920
921 trb = &dep->trb_pool[dep->trb_enqueue];
922
923 if (!req->trb) {
924 dwc3_gadget_move_started_request(req);
925 req->trb = trb;
926 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
927 dep->queued_requests++;
928 }
929
930 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
931 stream_id, short_not_ok, no_interrupt);
932}
933
934/**
John Youn361572b2016-05-19 17:26:17 -0700935 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
936 * @dep: The endpoint with the TRB ring
937 * @index: The index of the current TRB in the ring
938 *
939 * Returns the TRB prior to the one pointed to by the index. If the
940 * index is 0, we will wrap backwards, skip the link TRB, and return
941 * the one just before that.
942 */
943static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
944{
Felipe Balbi45438a02016-08-11 12:26:59 +0300945 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -0700946
Felipe Balbi45438a02016-08-11 12:26:59 +0300947 if (!tmp)
948 tmp = DWC3_TRB_NUM - 1;
949
950 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -0700951}
952
Felipe Balbic4233572016-05-12 14:08:34 +0300953static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
954{
955 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700956 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300957
958 /*
959 * If enqueue & dequeue are equal than it is either full or empty.
960 *
961 * One way to know for sure is if the TRB right before us has HWO bit
962 * set or not. If it has, then we're definitely full and can't fit any
963 * more transfers in our ring.
964 */
965 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -0700966 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +0300967 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -0700968 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +0300969
970 return DWC3_TRB_NUM - 1;
971 }
972
John Youn9d7aba72016-08-26 18:43:01 -0700973 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -0700974 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -0700975
John Youn9d7aba72016-08-26 18:43:01 -0700976 if (dep->trb_dequeue < dep->trb_enqueue)
977 trbs_left--;
978
John Youn32db3d92016-05-19 17:26:12 -0700979 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300980}
981
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300982static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300983 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300984{
Felipe Balbi1f512112016-08-12 13:17:27 +0300985 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300986 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300987 int i;
988
Felipe Balbi1f512112016-08-12 13:17:27 +0300989 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +0200990 unsigned int length = req->request.length;
991 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
992 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300993 unsigned chain = true;
994
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300995 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300996 chain = false;
997
Felipe Balbic6267a52017-01-05 14:58:46 +0200998 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
999 struct dwc3 *dwc = dep->dwc;
1000 struct dwc3_trb *trb;
1001
1002 req->unaligned = true;
1003
1004 /* prepare normal TRB */
1005 dwc3_prepare_one_trb(dep, req, true, i);
1006
1007 /* Now prepare one extra TRB to align transfer size */
1008 trb = &dep->trb_pool[dep->trb_enqueue];
1009 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1010 maxp - rem, false, 0,
1011 req->request.stream_id,
1012 req->request.short_not_ok,
1013 req->request.no_interrupt);
1014 } else {
1015 dwc3_prepare_one_trb(dep, req, chain, i);
1016 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001017
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001018 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001019 break;
1020 }
1021}
1022
1023static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001024 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001025{
Felipe Balbic6267a52017-01-05 14:58:46 +02001026 unsigned int length = req->request.length;
1027 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1028 unsigned int rem = length % maxp;
1029
1030 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1031 struct dwc3 *dwc = dep->dwc;
1032 struct dwc3_trb *trb;
1033
1034 req->unaligned = true;
1035
1036 /* prepare normal TRB */
1037 dwc3_prepare_one_trb(dep, req, true, 0);
1038
1039 /* Now prepare one extra TRB to align transfer size */
1040 trb = &dep->trb_pool[dep->trb_enqueue];
1041 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1042 false, 0, req->request.stream_id,
1043 req->request.short_not_ok,
1044 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001045 } else if (req->request.zero && req->request.length &&
1046 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1047 struct dwc3 *dwc = dep->dwc;
1048 struct dwc3_trb *trb;
1049
1050 req->zero = true;
1051
1052 /* prepare normal TRB */
1053 dwc3_prepare_one_trb(dep, req, true, 0);
1054
1055 /* Now prepare one extra TRB to handle ZLP */
1056 trb = &dep->trb_pool[dep->trb_enqueue];
1057 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1058 false, 0, req->request.stream_id,
1059 req->request.short_not_ok,
1060 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001061 } else {
1062 dwc3_prepare_one_trb(dep, req, false, 0);
1063 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001064}
1065
Felipe Balbi72246da2011-08-19 18:10:58 +03001066/*
1067 * dwc3_prepare_trbs - setup TRBs from requests
1068 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001069 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001070 * The function goes through the requests list and sets up TRBs for the
1071 * transfers. The function returns once there are no more TRBs available or
1072 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001073 */
Felipe Balbic4233572016-05-12 14:08:34 +03001074static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001075{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001076 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001077
1078 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1079
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001080 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001081 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001082
Felipe Balbid86c5a62016-10-25 13:48:52 +03001083 /*
1084 * We can get in a situation where there's a request in the started list
1085 * but there weren't enough TRBs to fully kick it in the first time
1086 * around, so it has been waiting for more TRBs to be freed up.
1087 *
1088 * In that case, we should check if we have a request with pending_sgs
1089 * in the started list and prepare TRBs for that request first,
1090 * otherwise we will prepare TRBs completely out of order and that will
1091 * break things.
1092 */
1093 list_for_each_entry(req, &dep->started_list, list) {
1094 if (req->num_pending_sgs > 0)
1095 dwc3_prepare_one_trb_sg(dep, req);
1096
1097 if (!dwc3_calc_trbs_left(dep))
1098 return;
1099 }
1100
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001101 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001102 struct dwc3 *dwc = dep->dwc;
1103 int ret;
1104
1105 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1106 dep->direction);
1107 if (ret)
1108 return;
1109
1110 req->sg = req->request.sg;
1111 req->num_pending_sgs = req->request.num_mapped_sgs;
1112
Felipe Balbi1f512112016-08-12 13:17:27 +03001113 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001114 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001115 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001116 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001117
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001118 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001119 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001120 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001121}
1122
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001123static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001124{
1125 struct dwc3_gadget_ep_cmd_params params;
1126 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001127 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001128 int ret;
1129 u32 cmd;
1130
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001131 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001132
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001133 dwc3_prepare_trbs(dep);
1134 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001135 if (!req) {
1136 dep->flags |= DWC3_EP_PENDING_REQUEST;
1137 return 0;
1138 }
1139
1140 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001141
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001142 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301143 params.param0 = upper_32_bits(req->trb_dma);
1144 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001145 cmd = DWC3_DEPCMD_STARTTRANSFER |
1146 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301147 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001148 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1149 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301150 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001151
Felipe Balbi2cd47182016-04-12 16:42:43 +03001152 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001153 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001154 /*
1155 * FIXME we need to iterate over the list of requests
1156 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001157 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001158 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001159 if (req->trb)
1160 memset(req->trb, 0, sizeof(struct dwc3_trb));
Janusz Dziedzic8ab89da2016-11-09 11:01:31 +01001161 dep->queued_requests--;
Felipe Balbi15b8d932016-09-22 10:59:12 +03001162 dwc3_gadget_giveback(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001163 return ret;
1164 }
1165
1166 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001167
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001168 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001169 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001170 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001171 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001172
Felipe Balbi72246da2011-08-19 18:10:58 +03001173 return 0;
1174}
1175
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001176static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1177{
1178 u32 reg;
1179
1180 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1181 return DWC3_DSTS_SOFFN(reg);
1182}
1183
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301184static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1185 struct dwc3_ep *dep, u32 cur_uf)
1186{
1187 u32 uf;
1188
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001189 if (list_empty(&dep->pending_list)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001190 dev_info(dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001191 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301192 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301193 return;
1194 }
1195
John Younaf771d72017-01-26 11:58:40 -08001196 /*
1197 * Schedule the first trb for one interval in the future or at
1198 * least 4 microframes.
1199 */
1200 uf = cur_uf + max_t(u32, 4, dep->interval);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301201
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001202 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301203}
1204
1205static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1206 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1207{
1208 u32 cur_uf, mask;
1209
1210 mask = ~(dep->interval - 1);
1211 cur_uf = event->parameters & mask;
1212
1213 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1214}
1215
Felipe Balbi72246da2011-08-19 18:10:58 +03001216static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1217{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001218 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001219 int ret = 0;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001220
Felipe Balbibb423982015-11-16 15:31:21 -06001221 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001222 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1223 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001224 return -ESHUTDOWN;
1225 }
1226
1227 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1228 &req->request, req->dep->name)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001229 dev_err(dwc->dev, "%s: request %p belongs to '%s'\n",
1230 dep->name, &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001231 return -EINVAL;
1232 }
1233
Felipe Balbifc8bb912016-05-16 13:14:48 +03001234 pm_runtime_get(dwc->dev);
1235
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 req->request.actual = 0;
1237 req->request.status = -EINPROGRESS;
1238 req->direction = dep->direction;
1239 req->epnum = dep->number;
1240
Felipe Balbife84f522015-09-01 09:01:38 -05001241 trace_dwc3_ep_queue(req);
1242
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001243 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001244
Felipe Balbid889c232016-09-29 15:44:29 +03001245 /*
1246 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1247 * wait for a XferNotReady event so we will know what's the current
1248 * (micro-)frame number.
1249 *
1250 * Without this trick, we are very, very likely gonna get Bus Expiry
1251 * errors which will force us issue EndTransfer command.
1252 */
1253 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001254 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1255 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1256 dwc3_stop_active_transfer(dwc, dep->number, true);
1257 dep->flags = DWC3_EP_ENABLED;
1258 } else {
1259 u32 cur_uf;
1260
1261 cur_uf = __dwc3_gadget_get_frame(dwc);
1262 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzic87aba102016-11-09 11:01:34 +01001263 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001264 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001265 }
1266 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001267 }
1268
Felipe Balbi594e1212016-08-24 14:38:10 +03001269 if (!dwc3_calc_trbs_left(dep))
1270 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001271
Felipe Balbi08a36b52016-08-11 14:27:52 +03001272 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001273 if (ret == -EBUSY)
1274 ret = 0;
1275
1276 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001277}
1278
1279static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1280 gfp_t gfp_flags)
1281{
1282 struct dwc3_request *req = to_dwc3_request(request);
1283 struct dwc3_ep *dep = to_dwc3_ep(ep);
1284 struct dwc3 *dwc = dep->dwc;
1285
1286 unsigned long flags;
1287
1288 int ret;
1289
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001290 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001291 ret = __dwc3_gadget_ep_queue(dep, req);
1292 spin_unlock_irqrestore(&dwc->lock, flags);
1293
1294 return ret;
1295}
1296
1297static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1298 struct usb_request *request)
1299{
1300 struct dwc3_request *req = to_dwc3_request(request);
1301 struct dwc3_request *r = NULL;
1302
1303 struct dwc3_ep *dep = to_dwc3_ep(ep);
1304 struct dwc3 *dwc = dep->dwc;
1305
1306 unsigned long flags;
1307 int ret = 0;
1308
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001309 trace_dwc3_ep_dequeue(req);
1310
Felipe Balbi72246da2011-08-19 18:10:58 +03001311 spin_lock_irqsave(&dwc->lock, flags);
1312
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001313 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 if (r == req)
1315 break;
1316 }
1317
1318 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001319 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001320 if (r == req)
1321 break;
1322 }
1323 if (r == req) {
1324 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001325 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001326
1327 /*
1328 * If request was already started, this means we had to
1329 * stop the transfer. With that we also need to ignore
1330 * all TRBs used by the request, however TRBs can only
1331 * be modified after completion of END_TRANSFER
1332 * command. So what we do here is that we wait for
1333 * END_TRANSFER completion and only after that, we jump
1334 * over TRBs by clearing HWO and incrementing dequeue
1335 * pointer.
1336 *
1337 * Note that we have 2 possible types of transfers here:
1338 *
1339 * i) Linear buffer request
1340 * ii) SG-list based request
1341 *
1342 * SG-list based requests will have r->num_pending_sgs
1343 * set to a valid number (> 0). Linear requests,
1344 * normally use a single TRB.
1345 *
1346 * For each of these two cases, if r->unaligned flag is
1347 * set, one extra TRB has been used to align transfer
1348 * size to wMaxPacketSize.
1349 *
1350 * All of these cases need to be taken into
1351 * consideration so we don't mess up our TRB ring
1352 * pointers.
1353 */
1354 wait_event_lock_irq(dep->wait_end_transfer,
1355 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1356 dwc->lock);
1357
1358 if (!r->trb)
1359 goto out1;
1360
1361 if (r->num_pending_sgs) {
1362 struct dwc3_trb *trb;
1363 int i = 0;
1364
1365 for (i = 0; i < r->num_pending_sgs; i++) {
1366 trb = r->trb + i;
1367 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1368 dwc3_ep_inc_deq(dep);
1369 }
1370
Felipe Balbid6e5a542017-04-07 16:34:38 +03001371 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001372 trb = r->trb + r->num_pending_sgs + 1;
1373 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1374 dwc3_ep_inc_deq(dep);
1375 }
1376 } else {
1377 struct dwc3_trb *trb = r->trb;
1378
1379 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1380 dwc3_ep_inc_deq(dep);
1381
Felipe Balbid6e5a542017-04-07 16:34:38 +03001382 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001383 trb = r->trb + 1;
1384 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1385 dwc3_ep_inc_deq(dep);
1386 }
1387 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301388 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001389 }
1390 dev_err(dwc->dev, "request %p was not queued to %s\n",
1391 request, ep->name);
1392 ret = -EINVAL;
1393 goto out0;
1394 }
1395
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301396out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001397 /* giveback the request */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001398 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001399 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1400
1401out0:
1402 spin_unlock_irqrestore(&dwc->lock, flags);
1403
1404 return ret;
1405}
1406
Felipe Balbi7a608552014-09-24 14:19:52 -05001407int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001408{
1409 struct dwc3_gadget_ep_cmd_params params;
1410 struct dwc3 *dwc = dep->dwc;
1411 int ret;
1412
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001413 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1414 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1415 return -EINVAL;
1416 }
1417
Felipe Balbi72246da2011-08-19 18:10:58 +03001418 memset(&params, 0x00, sizeof(params));
1419
1420 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001421 struct dwc3_trb *trb;
1422
1423 unsigned transfer_in_flight;
1424 unsigned started;
1425
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001426 if (dep->flags & DWC3_EP_STALL)
1427 return 0;
1428
Felipe Balbi69450c42016-05-30 13:37:02 +03001429 if (dep->number > 1)
1430 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1431 else
1432 trb = &dwc->ep0_trb[dep->trb_enqueue];
1433
1434 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1435 started = !list_empty(&dep->started_list);
1436
1437 if (!protocol && ((dep->direction && transfer_in_flight) ||
1438 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001439 return -EAGAIN;
1440 }
1441
Felipe Balbi2cd47182016-04-12 16:42:43 +03001442 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1443 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001444 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001445 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 dep->name);
1447 else
1448 dep->flags |= DWC3_EP_STALL;
1449 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001450 if (!(dep->flags & DWC3_EP_STALL))
1451 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001452
John Youn50c763f2016-05-31 17:49:56 -07001453 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001454 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001455 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 dep->name);
1457 else
Alan Sterna535d812013-11-01 12:05:12 -04001458 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001459 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001460
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 return ret;
1462}
1463
1464static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1465{
1466 struct dwc3_ep *dep = to_dwc3_ep(ep);
1467 struct dwc3 *dwc = dep->dwc;
1468
1469 unsigned long flags;
1470
1471 int ret;
1472
1473 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001474 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 spin_unlock_irqrestore(&dwc->lock, flags);
1476
1477 return ret;
1478}
1479
1480static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1481{
1482 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001483 struct dwc3 *dwc = dep->dwc;
1484 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001485 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001486
Paul Zimmerman249a4562012-02-24 17:32:16 -08001487 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001488 dep->flags |= DWC3_EP_WEDGE;
1489
Pratyush Anand08f0d962012-06-25 22:40:43 +05301490 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001491 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301492 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001493 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001494 spin_unlock_irqrestore(&dwc->lock, flags);
1495
1496 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001497}
1498
1499/* -------------------------------------------------------------------------- */
1500
1501static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1502 .bLength = USB_DT_ENDPOINT_SIZE,
1503 .bDescriptorType = USB_DT_ENDPOINT,
1504 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1505};
1506
1507static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1508 .enable = dwc3_gadget_ep0_enable,
1509 .disable = dwc3_gadget_ep0_disable,
1510 .alloc_request = dwc3_gadget_ep_alloc_request,
1511 .free_request = dwc3_gadget_ep_free_request,
1512 .queue = dwc3_gadget_ep0_queue,
1513 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301514 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 .set_wedge = dwc3_gadget_ep_set_wedge,
1516};
1517
1518static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1519 .enable = dwc3_gadget_ep_enable,
1520 .disable = dwc3_gadget_ep_disable,
1521 .alloc_request = dwc3_gadget_ep_alloc_request,
1522 .free_request = dwc3_gadget_ep_free_request,
1523 .queue = dwc3_gadget_ep_queue,
1524 .dequeue = dwc3_gadget_ep_dequeue,
1525 .set_halt = dwc3_gadget_ep_set_halt,
1526 .set_wedge = dwc3_gadget_ep_set_wedge,
1527};
1528
1529/* -------------------------------------------------------------------------- */
1530
1531static int dwc3_gadget_get_frame(struct usb_gadget *g)
1532{
1533 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001534
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001535 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001536}
1537
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001538static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001539{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001540 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001541
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001542 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001543 u32 reg;
1544
Felipe Balbi72246da2011-08-19 18:10:58 +03001545 u8 link_state;
1546 u8 speed;
1547
Felipe Balbi72246da2011-08-19 18:10:58 +03001548 /*
1549 * According to the Databook Remote wakeup request should
1550 * be issued only when the device is in early suspend state.
1551 *
1552 * We can check that via USB Link State bits in DSTS register.
1553 */
1554 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1555
1556 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001557 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001558 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001559 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001560
1561 link_state = DWC3_DSTS_USBLNKST(reg);
1562
1563 switch (link_state) {
1564 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1565 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1566 break;
1567 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001568 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001569 }
1570
Felipe Balbi8598bde2012-01-02 18:55:57 +02001571 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1572 if (ret < 0) {
1573 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001574 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001575 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001576
Paul Zimmerman802fde92012-04-27 13:10:52 +03001577 /* Recent versions do this automatically */
1578 if (dwc->revision < DWC3_REVISION_194A) {
1579 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001580 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001581 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1582 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1583 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001584
Paul Zimmerman1d046792012-02-15 18:56:56 -08001585 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001586 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001587
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001588 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001589 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1590
1591 /* in HS, means ON */
1592 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1593 break;
1594 }
1595
1596 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1597 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001598 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001599 }
1600
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001601 return 0;
1602}
1603
1604static int dwc3_gadget_wakeup(struct usb_gadget *g)
1605{
1606 struct dwc3 *dwc = gadget_to_dwc(g);
1607 unsigned long flags;
1608 int ret;
1609
1610 spin_lock_irqsave(&dwc->lock, flags);
1611 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001612 spin_unlock_irqrestore(&dwc->lock, flags);
1613
1614 return ret;
1615}
1616
1617static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1618 int is_selfpowered)
1619{
1620 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001621 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001622
Paul Zimmerman249a4562012-02-24 17:32:16 -08001623 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001624 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001625 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001626
1627 return 0;
1628}
1629
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001630static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001631{
1632 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001633 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001634
Felipe Balbifc8bb912016-05-16 13:14:48 +03001635 if (pm_runtime_suspended(dwc->dev))
1636 return 0;
1637
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001639 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001640 if (dwc->revision <= DWC3_REVISION_187A) {
1641 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1642 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1643 }
1644
1645 if (dwc->revision >= DWC3_REVISION_194A)
1646 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1647 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001648
1649 if (dwc->has_hibernation)
1650 reg |= DWC3_DCTL_KEEP_CONNECT;
1651
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001652 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001653 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001654 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001655
1656 if (dwc->has_hibernation && !suspend)
1657 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1658
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001659 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001660 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001661
1662 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1663
1664 do {
1665 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001666 reg &= DWC3_DSTS_DEVCTRLHLT;
1667 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001668
1669 if (!timeout)
1670 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001671
Pratyush Anand6f17f742012-07-02 10:21:55 +05301672 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001673}
1674
1675static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1676{
1677 struct dwc3 *dwc = gadget_to_dwc(g);
1678 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301679 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001680
1681 is_on = !!is_on;
1682
Baolin Wangbb014732016-10-14 17:11:33 +08001683 /*
1684 * Per databook, when we want to stop the gadget, if a control transfer
1685 * is still in process, complete it and get the core into setup phase.
1686 */
1687 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1688 reinit_completion(&dwc->ep0_in_setup);
1689
1690 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1691 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1692 if (ret == 0) {
1693 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1694 return -ETIMEDOUT;
1695 }
1696 }
1697
Felipe Balbi72246da2011-08-19 18:10:58 +03001698 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001699 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001700 spin_unlock_irqrestore(&dwc->lock, flags);
1701
Pratyush Anand6f17f742012-07-02 10:21:55 +05301702 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001703}
1704
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001705static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1706{
1707 u32 reg;
1708
1709 /* Enable all but Start and End of Frame IRQs */
1710 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1711 DWC3_DEVTEN_EVNTOVERFLOWEN |
1712 DWC3_DEVTEN_CMDCMPLTEN |
1713 DWC3_DEVTEN_ERRTICERREN |
1714 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001715 DWC3_DEVTEN_CONNECTDONEEN |
1716 DWC3_DEVTEN_USBRSTEN |
1717 DWC3_DEVTEN_DISCONNEVTEN);
1718
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001719 if (dwc->revision < DWC3_REVISION_250A)
1720 reg |= DWC3_DEVTEN_ULSTCNGEN;
1721
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001722 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1723}
1724
1725static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1726{
1727 /* mask all interrupts */
1728 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1729}
1730
1731static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001732static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001733
Felipe Balbi4e994722016-05-13 14:09:59 +03001734/**
1735 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1736 * dwc: pointer to our context structure
1737 *
1738 * The following looks like complex but it's actually very simple. In order to
1739 * calculate the number of packets we can burst at once on OUT transfers, we're
1740 * gonna use RxFIFO size.
1741 *
1742 * To calculate RxFIFO size we need two numbers:
1743 * MDWIDTH = size, in bits, of the internal memory bus
1744 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1745 *
1746 * Given these two numbers, the formula is simple:
1747 *
1748 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1749 *
1750 * 24 bytes is for 3x SETUP packets
1751 * 16 bytes is a clock domain crossing tolerance
1752 *
1753 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1754 */
1755static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1756{
1757 u32 ram2_depth;
1758 u32 mdwidth;
1759 u32 nump;
1760 u32 reg;
1761
1762 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1763 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1764
1765 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1766 nump = min_t(u32, nump, 16);
1767
1768 /* update NumP */
1769 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1770 reg &= ~DWC3_DCFG_NUMP_MASK;
1771 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1772 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1773}
1774
Felipe Balbid7be2952016-05-04 15:49:37 +03001775static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001776{
Felipe Balbi72246da2011-08-19 18:10:58 +03001777 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001778 int ret = 0;
1779 u32 reg;
1780
John Youncf40b862016-11-14 12:32:43 -08001781 /*
1782 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1783 * the core supports IMOD, disable it.
1784 */
1785 if (dwc->imod_interval) {
1786 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1787 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1788 } else if (dwc3_has_imod(dwc)) {
1789 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1790 }
1791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1793 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001794
1795 /**
1796 * WORKAROUND: DWC3 revision < 2.20a have an issue
1797 * which would cause metastability state on Run/Stop
1798 * bit if we try to force the IP to USB2-only mode.
1799 *
1800 * Because of that, we cannot configure the IP to any
1801 * speed other than the SuperSpeed
1802 *
1803 * Refers to:
1804 *
1805 * STAR#9000525659: Clock Domain Crossing on DCTL in
1806 * USB 2.0 Mode
1807 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001808 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001809 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001810 } else {
1811 switch (dwc->maximum_speed) {
1812 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07001813 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001814 break;
1815 case USB_SPEED_FULL:
Roger Quadros9418ee12017-01-03 14:32:09 +02001816 reg |= DWC3_DCFG_FULLSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001817 break;
1818 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07001819 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001820 break;
John Youn75808622016-02-05 17:09:13 -08001821 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07001822 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08001823 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001824 default:
John Youn77966eb2016-02-19 17:31:01 -08001825 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1826 dwc->maximum_speed);
1827 /* fall through */
1828 case USB_SPEED_SUPER:
1829 reg |= DWC3_DCFG_SUPERSPEED;
1830 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001831 }
1832 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001833 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1834
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001835 /*
1836 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1837 * field instead of letting dwc3 itself calculate that automatically.
1838 *
1839 * This way, we maximize the chances that we'll be able to get several
1840 * bursts of data without going through any sort of endpoint throttling.
1841 */
1842 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1843 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1844 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1845
Felipe Balbi4e994722016-05-13 14:09:59 +03001846 dwc3_gadget_setup_nump(dwc);
1847
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 /* Start with SuperSpeed Default */
1849 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1850
1851 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001852 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 if (ret) {
1854 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001855 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001856 }
1857
1858 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001859 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001860 if (ret) {
1861 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001862 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 }
1864
1865 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001866 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001867 dwc3_ep0_out_start(dwc);
1868
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001869 dwc3_gadget_enable_irq(dwc);
1870
Felipe Balbid7be2952016-05-04 15:49:37 +03001871 return 0;
1872
1873err1:
1874 __dwc3_gadget_ep_disable(dwc->eps[0]);
1875
1876err0:
1877 return ret;
1878}
1879
1880static int dwc3_gadget_start(struct usb_gadget *g,
1881 struct usb_gadget_driver *driver)
1882{
1883 struct dwc3 *dwc = gadget_to_dwc(g);
1884 unsigned long flags;
1885 int ret = 0;
1886 int irq;
1887
Roger Quadros9522def2016-06-10 14:48:38 +03001888 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001889 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1890 IRQF_SHARED, "dwc3", dwc->ev_buf);
1891 if (ret) {
1892 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1893 irq, ret);
1894 goto err0;
1895 }
1896
1897 spin_lock_irqsave(&dwc->lock, flags);
1898 if (dwc->gadget_driver) {
1899 dev_err(dwc->dev, "%s is already bound to %s\n",
1900 dwc->gadget.name,
1901 dwc->gadget_driver->driver.name);
1902 ret = -EBUSY;
1903 goto err1;
1904 }
1905
1906 dwc->gadget_driver = driver;
1907
Felipe Balbifc8bb912016-05-16 13:14:48 +03001908 if (pm_runtime_active(dwc->dev))
1909 __dwc3_gadget_start(dwc);
1910
Felipe Balbi72246da2011-08-19 18:10:58 +03001911 spin_unlock_irqrestore(&dwc->lock, flags);
1912
1913 return 0;
1914
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001915err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001916 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001917 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001918
1919err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 return ret;
1921}
1922
Felipe Balbid7be2952016-05-04 15:49:37 +03001923static void __dwc3_gadget_stop(struct dwc3 *dwc)
1924{
1925 dwc3_gadget_disable_irq(dwc);
1926 __dwc3_gadget_ep_disable(dwc->eps[0]);
1927 __dwc3_gadget_ep_disable(dwc->eps[1]);
1928}
1929
Felipe Balbi22835b82014-10-17 12:05:12 -05001930static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001931{
1932 struct dwc3 *dwc = gadget_to_dwc(g);
1933 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001934 int epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03001935
1936 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001937
1938 if (pm_runtime_suspended(dwc->dev))
1939 goto out;
1940
Felipe Balbid7be2952016-05-04 15:49:37 +03001941 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001942
1943 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1944 struct dwc3_ep *dep = dwc->eps[epnum];
1945
1946 if (!dep)
1947 continue;
1948
1949 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1950 continue;
1951
1952 wait_event_lock_irq(dep->wait_end_transfer,
1953 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1954 dwc->lock);
1955 }
1956
1957out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001958 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001959 spin_unlock_irqrestore(&dwc->lock, flags);
1960
Felipe Balbi3f308d12016-05-16 14:17:06 +03001961 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001962
Felipe Balbi72246da2011-08-19 18:10:58 +03001963 return 0;
1964}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001965
Felipe Balbi72246da2011-08-19 18:10:58 +03001966static const struct usb_gadget_ops dwc3_gadget_ops = {
1967 .get_frame = dwc3_gadget_get_frame,
1968 .wakeup = dwc3_gadget_wakeup,
1969 .set_selfpowered = dwc3_gadget_set_selfpowered,
1970 .pullup = dwc3_gadget_pullup,
1971 .udc_start = dwc3_gadget_start,
1972 .udc_stop = dwc3_gadget_stop,
1973};
1974
1975/* -------------------------------------------------------------------------- */
1976
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00001977static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 num)
Felipe Balbi72246da2011-08-19 18:10:58 +03001978{
1979 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00001980 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03001981
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00001982 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1983
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00001984 for (epnum = 0; epnum < num; epnum++) {
1985 bool direction = epnum & 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001988 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001990
1991 dep->dwc = dwc;
1992 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00001993 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001994 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 dwc->eps[epnum] = dep;
1996
1997 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00001998 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001999
Felipe Balbi72246da2011-08-19 18:10:58 +03002000 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002001
2002 if (!(dep->number > 1)) {
2003 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2004 dep->endpoint.comp_desc = NULL;
2005 }
2006
Felipe Balbi74674cb2016-04-13 16:44:39 +03002007 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002008
2009 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002010 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302011 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002012 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2013 if (!epnum)
2014 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002015 } else if (direction) {
2016 int mdwidth;
2017 int size;
2018 int ret;
2019 int num;
2020
2021 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2022 /* MDWIDTH is represented in bits, we need it in bytes */
2023 mdwidth /= 8;
2024
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002025 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(epnum >> 1));
Felipe Balbi28781782017-01-23 18:01:59 +02002026 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2027
2028 /* FIFO Depth is in MDWDITH bytes. Multiply */
2029 size *= mdwidth;
2030
2031 num = size / 1024;
2032 if (num == 0)
2033 num = 1;
2034
2035 /*
2036 * FIFO sizes account an extra MDWIDTH * (num + 1) bytes for
2037 * internal overhead. We don't really know how these are used,
2038 * but documentation say it exists.
2039 */
2040 size -= mdwidth * (num + 1);
2041 size /= num;
2042
2043 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2044
2045 dep->endpoint.max_streams = 15;
2046 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2047 list_add_tail(&dep->endpoint.ep_list,
2048 &dwc->gadget.ep_list);
2049
2050 ret = dwc3_alloc_trb_pool(dep);
2051 if (ret)
2052 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002053 } else {
2054 int ret;
2055
Robert Baldygae117e742013-12-13 12:23:38 +01002056 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002057 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002058 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2059 list_add_tail(&dep->endpoint.ep_list,
2060 &dwc->gadget.ep_list);
2061
2062 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002063 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002064 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002065 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002066
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002067 if (epnum == 0 || epnum == 1) {
2068 dep->endpoint.caps.type_control = true;
2069 } else {
2070 dep->endpoint.caps.type_iso = true;
2071 dep->endpoint.caps.type_bulk = true;
2072 dep->endpoint.caps.type_int = true;
2073 }
2074
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002075 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002076 dep->endpoint.caps.dir_out = !direction;
2077
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002078 INIT_LIST_HEAD(&dep->pending_list);
2079 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002080 }
2081
2082 return 0;
2083}
2084
2085static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2086{
2087 struct dwc3_ep *dep;
2088 u8 epnum;
2089
2090 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2091 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002092 if (!dep)
2093 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302094 /*
2095 * Physical endpoints 0 and 1 are special; they form the
2096 * bi-directional USB endpoint 0.
2097 *
2098 * For those two physical endpoints, we don't allocate a TRB
2099 * pool nor do we add them the endpoints list. Due to that, we
2100 * shouldn't do these two operations otherwise we would end up
2101 * with all sorts of bugs when removing dwc3.ko.
2102 */
2103 if (epnum != 0 && epnum != 1) {
2104 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002105 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302106 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002107
2108 kfree(dep);
2109 }
2110}
2111
Felipe Balbi72246da2011-08-19 18:10:58 +03002112/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002113
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302114static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2115 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002116 const struct dwc3_event_depevt *event, int status,
2117 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302118{
2119 unsigned int count;
2120 unsigned int s_pkt = 0;
2121 unsigned int trb_status;
2122
Felipe Balbidc55c672016-08-12 13:20:32 +03002123 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002124
2125 if (req->trb == trb)
2126 dep->queued_requests--;
2127
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002128 trace_dwc3_complete_trb(dep, trb);
2129
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002130 /*
2131 * If we're in the middle of series of chained TRBs and we
2132 * receive a short transfer along the way, DWC3 will skip
2133 * through all TRBs including the last TRB in the chain (the
2134 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2135 * bit and SW has to do it manually.
2136 *
2137 * We're going to do that here to avoid problems of HW trying
2138 * to use bogus TRBs for transfers.
2139 */
2140 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2141 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2142
Felipe Balbic6267a52017-01-05 14:58:46 +02002143 /*
2144 * If we're dealing with unaligned size OUT transfer, we will be left
2145 * with one TRB pending in the ring. We need to manually clear HWO bit
2146 * from that TRB.
2147 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002148 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002149 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2150 return 1;
2151 }
2152
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302153 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002154 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302155
Felipe Balbi35b27192017-03-08 13:56:37 +02002156 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2157 return 1;
2158
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302159 if (dep->direction) {
2160 if (count) {
2161 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2162 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302163 /*
2164 * If missed isoc occurred and there is
2165 * no request queued then issue END
2166 * TRANSFER, so that core generates
2167 * next xfernotready and we will issue
2168 * a fresh START TRANSFER.
2169 * If there are still queued request
2170 * then wait, do not issue either END
2171 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002172 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302173 * giveback.If any future queued request
2174 * is successfully transferred then we
2175 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002176 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302177 */
2178 dep->flags |= DWC3_EP_MISSED_ISOC;
2179 } else {
2180 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2181 dep->name);
2182 status = -ECONNRESET;
2183 }
2184 } else {
2185 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2186 }
2187 } else {
2188 if (count && (event->status & DEPEVT_STATUS_SHORT))
2189 s_pkt = 1;
2190 }
2191
Felipe Balbi7c705df2016-08-10 12:35:30 +03002192 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302193 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002194
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302195 if ((event->status & DEPEVT_STATUS_IOC) &&
2196 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2197 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002198
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302199 return 0;
2200}
2201
Felipe Balbi72246da2011-08-19 18:10:58 +03002202static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2203 const struct dwc3_event_depevt *event, int status)
2204{
Felipe Balbi31162af2016-08-11 14:38:37 +03002205 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002206 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002207 bool ioc = false;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002208 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002209
Felipe Balbi31162af2016-08-11 14:38:37 +03002210 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002211 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002212 int chain;
2213
Felipe Balbi1f512112016-08-12 13:17:27 +03002214 length = req->request.length;
2215 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002216 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002217 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002218 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002219 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002220 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002221
Felipe Balbi1f512112016-08-12 13:17:27 +03002222 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002223 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002224
Felipe Balbi7282c4e2016-10-25 13:50:46 +03002225 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2226 break;
2227
Felipe Balbi1f512112016-08-12 13:17:27 +03002228 req->sg = sg_next(s);
2229 req->num_pending_sgs--;
2230
Felipe Balbi31162af2016-08-11 14:38:37 +03002231 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2232 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002233 if (ret)
2234 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002235 }
2236 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002237 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002238 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002239 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002240 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002241
Felipe Balbid6e5a542017-04-07 16:34:38 +03002242 if (req->unaligned || req->zero) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002243 trb = &dep->trb_pool[dep->trb_dequeue];
2244 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2245 event, status, false);
2246 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002247 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002248 }
2249
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002250 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002251
Felipe Balbiff377ae2016-10-25 13:54:00 +03002252 if ((req->request.actual < length) && req->num_pending_sgs)
Felipe Balbi1f512112016-08-12 13:17:27 +03002253 return __dwc3_gadget_kick_transfer(dep, 0);
2254
Ville Syrjäläd115d702015-08-31 19:48:28 +03002255 dwc3_gadget_giveback(dep, req, status);
2256
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002257 if (ret) {
2258 if ((event->status & DEPEVT_STATUS_IOC) &&
2259 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2260 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002261 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002262 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002263 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002264
Felipe Balbi4cb42212016-05-18 12:37:21 +03002265 /*
2266 * Our endpoint might get disabled by another thread during
2267 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2268 * early on so DWC3_EP_BUSY flag gets cleared
2269 */
2270 if (!dep->endpoint.desc)
2271 return 1;
2272
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302273 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002274 list_empty(&dep->started_list)) {
2275 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302276 /*
2277 * If there is no entry in request list then do
2278 * not issue END TRANSFER now. Just set PENDING
2279 * flag, so that END TRANSFER is issued when an
2280 * entry is added into request list.
2281 */
2282 dep->flags = DWC3_EP_PENDING_REQUEST;
2283 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002284 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302285 dep->flags = DWC3_EP_ENABLED;
2286 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302287 return 1;
2288 }
2289
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002290 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2291 return 0;
2292
Felipe Balbi72246da2011-08-19 18:10:58 +03002293 return 1;
2294}
2295
2296static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002297 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002298{
2299 unsigned status = 0;
2300 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002301 u32 is_xfer_complete;
2302
2303 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002304
2305 if (event->status & DEPEVT_STATUS_BUSERR)
2306 status = -ECONNRESET;
2307
Paul Zimmerman1d046792012-02-15 18:56:56 -08002308 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002309 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002310 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002311 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002312
2313 /*
2314 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2315 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2316 */
2317 if (dwc->revision < DWC3_REVISION_183A) {
2318 u32 reg;
2319 int i;
2320
2321 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002322 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002323
2324 if (!(dep->flags & DWC3_EP_ENABLED))
2325 continue;
2326
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002327 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002328 return;
2329 }
2330
2331 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2332 reg |= dwc->u1u2;
2333 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2334
2335 dwc->u1u2 = 0;
2336 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002337
Felipe Balbi4cb42212016-05-18 12:37:21 +03002338 /*
2339 * Our endpoint might get disabled by another thread during
2340 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2341 * early on so DWC3_EP_BUSY flag gets cleared
2342 */
2343 if (!dep->endpoint.desc)
2344 return;
2345
Felipe Balbie6e709b2015-09-28 15:16:56 -05002346 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002347 int ret;
2348
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002349 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002350 if (!ret || ret == -EBUSY)
2351 return;
2352 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002353}
2354
Felipe Balbi72246da2011-08-19 18:10:58 +03002355static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2356 const struct dwc3_event_depevt *event)
2357{
2358 struct dwc3_ep *dep;
2359 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002360 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002361
2362 dep = dwc->eps[epnum];
2363
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002364 if (!(dep->flags & DWC3_EP_ENABLED)) {
2365 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2366 return;
2367
2368 /* Handle only EPCMDCMPLT when EP disabled */
2369 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2370 return;
2371 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002372
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 if (epnum == 0 || epnum == 1) {
2374 dwc3_ep0_interrupt(dwc, event);
2375 return;
2376 }
2377
2378 switch (event->endpoint_event) {
2379 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002380 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002381
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002382 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002383 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002384 return;
2385 }
2386
Jingoo Han029d97f2014-07-04 15:00:51 +09002387 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002388 break;
2389 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002390 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 break;
2392 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002393 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 dwc3_gadget_start_isoc(dwc, dep, event);
2395 } else {
2396 int ret;
2397
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002398 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002399 if (!ret || ret == -EBUSY)
2400 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 }
2402
2403 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002404 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002405 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002406 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2407 dep->name);
2408 return;
2409 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002410 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002412 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2413
2414 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2415 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2416 wake_up(&dep->wait_end_transfer);
2417 }
2418 break;
2419 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002420 break;
2421 }
2422}
2423
2424static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2425{
2426 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2427 spin_unlock(&dwc->lock);
2428 dwc->gadget_driver->disconnect(&dwc->gadget);
2429 spin_lock(&dwc->lock);
2430 }
2431}
2432
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002433static void dwc3_suspend_gadget(struct dwc3 *dwc)
2434{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002435 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002436 spin_unlock(&dwc->lock);
2437 dwc->gadget_driver->suspend(&dwc->gadget);
2438 spin_lock(&dwc->lock);
2439 }
2440}
2441
2442static void dwc3_resume_gadget(struct dwc3 *dwc)
2443{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002444 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002445 spin_unlock(&dwc->lock);
2446 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002447 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002448 }
2449}
2450
2451static void dwc3_reset_gadget(struct dwc3 *dwc)
2452{
2453 if (!dwc->gadget_driver)
2454 return;
2455
2456 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2457 spin_unlock(&dwc->lock);
2458 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002459 spin_lock(&dwc->lock);
2460 }
2461}
2462
Paul Zimmermanb992e682012-04-27 14:17:35 +03002463static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002464{
2465 struct dwc3_ep *dep;
2466 struct dwc3_gadget_ep_cmd_params params;
2467 u32 cmd;
2468 int ret;
2469
2470 dep = dwc->eps[epnum];
2471
Baolin Wang76a638f2016-10-31 19:38:36 +08002472 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2473 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302474 return;
2475
Pratyush Anand57911502012-07-06 15:19:10 +05302476 /*
2477 * NOTICE: We are violating what the Databook says about the
2478 * EndTransfer command. Ideally we would _always_ wait for the
2479 * EndTransfer Command Completion IRQ, but that's causing too
2480 * much trouble synchronizing between us and gadget driver.
2481 *
2482 * We have discussed this with the IP Provider and it was
2483 * suggested to giveback all requests here, but give HW some
2484 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002485 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302486 *
2487 * Note also that a similar handling was tested by Synopsys
2488 * (thanks a lot Paul) and nothing bad has come out of it.
2489 * In short, what we're doing is:
2490 *
2491 * - Issue EndTransfer WITH CMDIOC bit set
2492 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002493 *
2494 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2495 * supports a mode to work around the above limitation. The
2496 * software can poll the CMDACT bit in the DEPCMD register
2497 * after issuing a EndTransfer command. This mode is enabled
2498 * by writing GUCTL2[14]. This polling is already done in the
2499 * dwc3_send_gadget_ep_cmd() function so if the mode is
2500 * enabled, the EndTransfer command will have completed upon
2501 * returning from this function and we don't need to delay for
2502 * 100us.
2503 *
2504 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302505 */
2506
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302507 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002508 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2509 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002510 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302511 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002512 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302513 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002514 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002515 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002516
Baolin Wang76a638f2016-10-31 19:38:36 +08002517 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2518 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002519 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002520 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002521}
2522
Felipe Balbi72246da2011-08-19 18:10:58 +03002523static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2524{
2525 u32 epnum;
2526
2527 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2528 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002529 int ret;
2530
2531 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002532 if (!dep)
2533 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002534
2535 if (!(dep->flags & DWC3_EP_STALL))
2536 continue;
2537
2538 dep->flags &= ~DWC3_EP_STALL;
2539
John Youn50c763f2016-05-31 17:49:56 -07002540 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002541 WARN_ON_ONCE(ret);
2542 }
2543}
2544
2545static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2546{
Felipe Balbic4430a22012-05-24 10:30:01 +03002547 int reg;
2548
Felipe Balbi72246da2011-08-19 18:10:58 +03002549 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2550 reg &= ~DWC3_DCTL_INITU1ENA;
2551 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2552
2553 reg &= ~DWC3_DCTL_INITU2ENA;
2554 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002555
Felipe Balbi72246da2011-08-19 18:10:58 +03002556 dwc3_disconnect_gadget(dwc);
2557
2558 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002559 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002560 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002561
2562 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002563}
2564
Felipe Balbi72246da2011-08-19 18:10:58 +03002565static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2566{
2567 u32 reg;
2568
Felipe Balbifc8bb912016-05-16 13:14:48 +03002569 dwc->connected = true;
2570
Felipe Balbidf62df52011-10-14 15:11:49 +03002571 /*
2572 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2573 * would cause a missing Disconnect Event if there's a
2574 * pending Setup Packet in the FIFO.
2575 *
2576 * There's no suggested workaround on the official Bug
2577 * report, which states that "unless the driver/application
2578 * is doing any special handling of a disconnect event,
2579 * there is no functional issue".
2580 *
2581 * Unfortunately, it turns out that we _do_ some special
2582 * handling of a disconnect event, namely complete all
2583 * pending transfers, notify gadget driver of the
2584 * disconnection, and so on.
2585 *
2586 * Our suggested workaround is to follow the Disconnect
2587 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002588 * flag. Such flag gets set whenever we have a SETUP_PENDING
2589 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002590 * same endpoint.
2591 *
2592 * Refers to:
2593 *
2594 * STAR#9000466709: RTL: Device : Disconnect event not
2595 * generated if setup packet pending in FIFO
2596 */
2597 if (dwc->revision < DWC3_REVISION_188A) {
2598 if (dwc->setup_packet_pending)
2599 dwc3_gadget_disconnect_interrupt(dwc);
2600 }
2601
Felipe Balbi8e744752014-11-06 14:27:53 +08002602 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002603
2604 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2605 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2606 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002607 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002608 dwc3_clear_stall_all_ep(dwc);
2609
2610 /* Reset device address to zero */
2611 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2612 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2613 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002614}
2615
Felipe Balbi72246da2011-08-19 18:10:58 +03002616static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2617{
Felipe Balbi72246da2011-08-19 18:10:58 +03002618 struct dwc3_ep *dep;
2619 int ret;
2620 u32 reg;
2621 u8 speed;
2622
Felipe Balbi72246da2011-08-19 18:10:58 +03002623 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2624 speed = reg & DWC3_DSTS_CONNECTSPD;
2625 dwc->speed = speed;
2626
John Youn5fb6fda2016-11-10 17:23:25 -08002627 /*
2628 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2629 * each time on Connect Done.
2630 *
2631 * Currently we always use the reset value. If any platform
2632 * wants to set this to a different value, we need to add a
2633 * setting and update GCTL.RAMCLKSEL here.
2634 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002635
2636 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002637 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002638 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2639 dwc->gadget.ep0->maxpacket = 512;
2640 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2641 break;
John Youn2da9ad72016-05-20 16:34:26 -07002642 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002643 /*
2644 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2645 * would cause a missing USB3 Reset event.
2646 *
2647 * In such situations, we should force a USB3 Reset
2648 * event by calling our dwc3_gadget_reset_interrupt()
2649 * routine.
2650 *
2651 * Refers to:
2652 *
2653 * STAR#9000483510: RTL: SS : USB3 reset event may
2654 * not be generated always when the link enters poll
2655 */
2656 if (dwc->revision < DWC3_REVISION_190A)
2657 dwc3_gadget_reset_interrupt(dwc);
2658
Felipe Balbi72246da2011-08-19 18:10:58 +03002659 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2660 dwc->gadget.ep0->maxpacket = 512;
2661 dwc->gadget.speed = USB_SPEED_SUPER;
2662 break;
John Youn2da9ad72016-05-20 16:34:26 -07002663 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002664 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2665 dwc->gadget.ep0->maxpacket = 64;
2666 dwc->gadget.speed = USB_SPEED_HIGH;
2667 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002668 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002669 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2670 dwc->gadget.ep0->maxpacket = 64;
2671 dwc->gadget.speed = USB_SPEED_FULL;
2672 break;
John Youn2da9ad72016-05-20 16:34:26 -07002673 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002674 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2675 dwc->gadget.ep0->maxpacket = 8;
2676 dwc->gadget.speed = USB_SPEED_LOW;
2677 break;
2678 }
2679
Pratyush Anand2b758352013-01-14 15:59:31 +05302680 /* Enable USB2 LPM Capability */
2681
John Younee5cd412016-02-05 17:08:45 -08002682 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002683 (speed != DWC3_DSTS_SUPERSPEED) &&
2684 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302685 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2686 reg |= DWC3_DCFG_LPM_CAP;
2687 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2688
2689 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2690 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2691
Huang Rui460d0982014-10-31 11:11:18 +08002692 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302693
Huang Rui80caf7d2014-10-28 19:54:26 +08002694 /*
2695 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2696 * DCFG.LPMCap is set, core responses with an ACK and the
2697 * BESL value in the LPM token is less than or equal to LPM
2698 * NYET threshold.
2699 */
2700 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2701 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002702 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002703
2704 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2705 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2706
Pratyush Anand2b758352013-01-14 15:59:31 +05302707 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002708 } else {
2709 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2710 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2711 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302712 }
2713
Felipe Balbi72246da2011-08-19 18:10:58 +03002714 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002715 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002716 if (ret) {
2717 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2718 return;
2719 }
2720
2721 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002722 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 if (ret) {
2724 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2725 return;
2726 }
2727
2728 /*
2729 * Configure PHY via GUSB3PIPECTLn if required.
2730 *
2731 * Update GTXFIFOSIZn
2732 *
2733 * In both cases reset values should be sufficient.
2734 */
2735}
2736
2737static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2738{
Felipe Balbi72246da2011-08-19 18:10:58 +03002739 /*
2740 * TODO take core out of low power mode when that's
2741 * implemented.
2742 */
2743
Jiebing Liad14d4e2014-12-11 13:26:29 +08002744 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2745 spin_unlock(&dwc->lock);
2746 dwc->gadget_driver->resume(&dwc->gadget);
2747 spin_lock(&dwc->lock);
2748 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002749}
2750
2751static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2752 unsigned int evtinfo)
2753{
Felipe Balbifae2b902011-10-14 13:00:30 +03002754 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002755 unsigned int pwropt;
2756
2757 /*
2758 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2759 * Hibernation mode enabled which would show up when device detects
2760 * host-initiated U3 exit.
2761 *
2762 * In that case, device will generate a Link State Change Interrupt
2763 * from U3 to RESUME which is only necessary if Hibernation is
2764 * configured in.
2765 *
2766 * There are no functional changes due to such spurious event and we
2767 * just need to ignore it.
2768 *
2769 * Refers to:
2770 *
2771 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2772 * operational mode
2773 */
2774 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2775 if ((dwc->revision < DWC3_REVISION_250A) &&
2776 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2777 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2778 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002779 return;
2780 }
2781 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002782
2783 /*
2784 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2785 * on the link partner, the USB session might do multiple entry/exit
2786 * of low power states before a transfer takes place.
2787 *
2788 * Due to this problem, we might experience lower throughput. The
2789 * suggested workaround is to disable DCTL[12:9] bits if we're
2790 * transitioning from U1/U2 to U0 and enable those bits again
2791 * after a transfer completes and there are no pending transfers
2792 * on any of the enabled endpoints.
2793 *
2794 * This is the first half of that workaround.
2795 *
2796 * Refers to:
2797 *
2798 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2799 * core send LGO_Ux entering U0
2800 */
2801 if (dwc->revision < DWC3_REVISION_183A) {
2802 if (next == DWC3_LINK_STATE_U0) {
2803 u32 u1u2;
2804 u32 reg;
2805
2806 switch (dwc->link_state) {
2807 case DWC3_LINK_STATE_U1:
2808 case DWC3_LINK_STATE_U2:
2809 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2810 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2811 | DWC3_DCTL_ACCEPTU2ENA
2812 | DWC3_DCTL_INITU1ENA
2813 | DWC3_DCTL_ACCEPTU1ENA);
2814
2815 if (!dwc->u1u2)
2816 dwc->u1u2 = reg & u1u2;
2817
2818 reg &= ~u1u2;
2819
2820 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2821 break;
2822 default:
2823 /* do nothing */
2824 break;
2825 }
2826 }
2827 }
2828
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002829 switch (next) {
2830 case DWC3_LINK_STATE_U1:
2831 if (dwc->speed == USB_SPEED_SUPER)
2832 dwc3_suspend_gadget(dwc);
2833 break;
2834 case DWC3_LINK_STATE_U2:
2835 case DWC3_LINK_STATE_U3:
2836 dwc3_suspend_gadget(dwc);
2837 break;
2838 case DWC3_LINK_STATE_RESUME:
2839 dwc3_resume_gadget(dwc);
2840 break;
2841 default:
2842 /* do nothing */
2843 break;
2844 }
2845
Felipe Balbie57ebc12014-04-22 13:20:12 -05002846 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002847}
2848
Baolin Wang72704f82016-05-16 16:43:53 +08002849static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2850 unsigned int evtinfo)
2851{
2852 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2853
2854 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2855 dwc3_suspend_gadget(dwc);
2856
2857 dwc->link_state = next;
2858}
2859
Felipe Balbie1dadd32014-02-25 14:47:54 -06002860static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2861 unsigned int evtinfo)
2862{
2863 unsigned int is_ss = evtinfo & BIT(4);
2864
2865 /**
2866 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2867 * have a known issue which can cause USB CV TD.9.23 to fail
2868 * randomly.
2869 *
2870 * Because of this issue, core could generate bogus hibernation
2871 * events which SW needs to ignore.
2872 *
2873 * Refers to:
2874 *
2875 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2876 * Device Fallback from SuperSpeed
2877 */
2878 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2879 return;
2880
2881 /* enter hibernation here */
2882}
2883
Felipe Balbi72246da2011-08-19 18:10:58 +03002884static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2885 const struct dwc3_event_devt *event)
2886{
2887 switch (event->type) {
2888 case DWC3_DEVICE_EVENT_DISCONNECT:
2889 dwc3_gadget_disconnect_interrupt(dwc);
2890 break;
2891 case DWC3_DEVICE_EVENT_RESET:
2892 dwc3_gadget_reset_interrupt(dwc);
2893 break;
2894 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2895 dwc3_gadget_conndone_interrupt(dwc);
2896 break;
2897 case DWC3_DEVICE_EVENT_WAKEUP:
2898 dwc3_gadget_wakeup_interrupt(dwc);
2899 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002900 case DWC3_DEVICE_EVENT_HIBER_REQ:
2901 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2902 "unexpected hibernation event\n"))
2903 break;
2904
2905 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2906 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002907 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2908 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2909 break;
2910 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002911 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002912 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002913 /*
2914 * Ignore suspend event until the gadget enters into
2915 * USB_STATE_CONFIGURED state.
2916 */
2917 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2918 dwc3_gadget_suspend_interrupt(dwc,
2919 event->event_info);
2920 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002921 break;
2922 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002924 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002925 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002926 break;
2927 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002928 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002929 }
2930}
2931
2932static void dwc3_process_event_entry(struct dwc3 *dwc,
2933 const union dwc3_event *event)
2934{
Felipe Balbi43c96be2016-09-26 13:23:34 +03002935 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002936
Felipe Balbi72246da2011-08-19 18:10:58 +03002937 /* Endpoint IRQ, handle it and return early */
2938 if (event->type.is_devspec == 0) {
2939 /* depevt */
2940 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2941 }
2942
2943 switch (event->type.type) {
2944 case DWC3_EVENT_TYPE_DEV:
2945 dwc3_gadget_interrupt(dwc, &event->devt);
2946 break;
2947 /* REVISIT what to do with Carkit and I2C events ? */
2948 default:
2949 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2950 }
2951}
2952
Felipe Balbidea520a2016-03-30 09:39:34 +03002953static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002954{
Felipe Balbidea520a2016-03-30 09:39:34 +03002955 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002956 irqreturn_t ret = IRQ_NONE;
2957 int left;
2958 u32 reg;
2959
Felipe Balbif42f2442013-06-12 21:25:08 +03002960 left = evt->count;
2961
2962 if (!(evt->flags & DWC3_EVENT_PENDING))
2963 return IRQ_NONE;
2964
2965 while (left > 0) {
2966 union dwc3_event event;
2967
John Younebbb2d52016-11-15 13:07:02 +02002968 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03002969
2970 dwc3_process_event_entry(dwc, &event);
2971
2972 /*
2973 * FIXME we wrap around correctly to the next entry as
2974 * almost all entries are 4 bytes in size. There is one
2975 * entry which has 12 bytes which is a regular entry
2976 * followed by 8 bytes data. ATM I don't know how
2977 * things are organized if we get next to the a
2978 * boundary so I worry about that once we try to handle
2979 * that.
2980 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02002981 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03002982 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03002983 }
2984
2985 evt->count = 0;
2986 evt->flags &= ~DWC3_EVENT_PENDING;
2987 ret = IRQ_HANDLED;
2988
2989 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002990 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002991 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002992 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002993
John Youncf40b862016-11-14 12:32:43 -08002994 if (dwc->imod_interval) {
2995 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2996 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2997 }
2998
Felipe Balbif42f2442013-06-12 21:25:08 +03002999 return ret;
3000}
3001
Felipe Balbidea520a2016-03-30 09:39:34 +03003002static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003003{
Felipe Balbidea520a2016-03-30 09:39:34 +03003004 struct dwc3_event_buffer *evt = _evt;
3005 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003006 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003007 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003008
Felipe Balbie5f68b42015-10-12 13:25:44 -05003009 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003010 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05003011 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003012
3013 return ret;
3014}
3015
Felipe Balbidea520a2016-03-30 09:39:34 +03003016static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003017{
Felipe Balbidea520a2016-03-30 09:39:34 +03003018 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003019 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003020 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003021 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003022
Felipe Balbifc8bb912016-05-16 13:14:48 +03003023 if (pm_runtime_suspended(dwc->dev)) {
3024 pm_runtime_get(dwc->dev);
3025 disable_irq_nosync(dwc->irq_gadget);
3026 dwc->pending_events = true;
3027 return IRQ_HANDLED;
3028 }
3029
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003030 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003031 count &= DWC3_GEVNTCOUNT_MASK;
3032 if (!count)
3033 return IRQ_NONE;
3034
Felipe Balbib15a7622011-06-30 16:57:15 +03003035 evt->count = count;
3036 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003037
Felipe Balbie8adfc32013-06-12 21:11:14 +03003038 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003039 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003040 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003041 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003042
John Younebbb2d52016-11-15 13:07:02 +02003043 amount = min(count, evt->length - evt->lpos);
3044 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3045
3046 if (amount < count)
3047 memcpy(evt->cache, evt->buf, count - amount);
3048
John Youn65aca322016-11-15 13:08:59 +02003049 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3050
Felipe Balbib15a7622011-06-30 16:57:15 +03003051 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003052}
3053
Felipe Balbidea520a2016-03-30 09:39:34 +03003054static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003055{
Felipe Balbidea520a2016-03-30 09:39:34 +03003056 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003057
Felipe Balbidea520a2016-03-30 09:39:34 +03003058 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003059}
3060
Felipe Balbi6db38122016-10-03 11:27:01 +03003061static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3062{
3063 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3064 int irq;
3065
3066 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3067 if (irq > 0)
3068 goto out;
3069
3070 if (irq == -EPROBE_DEFER)
3071 goto out;
3072
3073 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3074 if (irq > 0)
3075 goto out;
3076
3077 if (irq == -EPROBE_DEFER)
3078 goto out;
3079
3080 irq = platform_get_irq(dwc3_pdev, 0);
3081 if (irq > 0)
3082 goto out;
3083
3084 if (irq != -EPROBE_DEFER)
3085 dev_err(dwc->dev, "missing peripheral IRQ\n");
3086
3087 if (!irq)
3088 irq = -EINVAL;
3089
3090out:
3091 return irq;
3092}
3093
Felipe Balbi72246da2011-08-19 18:10:58 +03003094/**
3095 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003096 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003097 *
3098 * Returns 0 on success otherwise negative errno.
3099 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003100int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003101{
Felipe Balbi6db38122016-10-03 11:27:01 +03003102 int ret;
3103 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003104
Felipe Balbi6db38122016-10-03 11:27:01 +03003105 irq = dwc3_gadget_get_irq(dwc);
3106 if (irq < 0) {
3107 ret = irq;
3108 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003109 }
3110
3111 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003112
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303113 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3114 sizeof(*dwc->ep0_trb) * 2,
3115 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003116 if (!dwc->ep0_trb) {
3117 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3118 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003119 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003120 }
3121
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003122 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003123 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003124 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003125 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003126 }
3127
Felipe Balbi905dc042017-01-05 14:46:52 +02003128 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3129 &dwc->bounce_addr, GFP_KERNEL);
3130 if (!dwc->bounce) {
3131 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003132 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003133 }
3134
Baolin Wangbb014732016-10-14 17:11:33 +08003135 init_completion(&dwc->ep0_in_setup);
3136
Felipe Balbi72246da2011-08-19 18:10:58 +03003137 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003138 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003139 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003140 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003141 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003142
3143 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003144 * FIXME We might be setting max_speed to <SUPER, however versions
3145 * <2.20a of dwc3 have an issue with metastability (documented
3146 * elsewhere in this driver) which tells us we can't set max speed to
3147 * anything lower than SUPER.
3148 *
3149 * Because gadget.max_speed is only used by composite.c and function
3150 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3151 * to happen so we avoid sending SuperSpeed Capability descriptor
3152 * together with our BOS descriptor as that could confuse host into
3153 * thinking we can handle super speed.
3154 *
3155 * Note that, in fact, we won't even support GetBOS requests when speed
3156 * is less than super speed because we don't have means, yet, to tell
3157 * composite.c that we are USB 2.0 + LPM ECN.
3158 */
3159 if (dwc->revision < DWC3_REVISION_220A)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003160 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003161 dwc->revision);
3162
3163 dwc->gadget.max_speed = dwc->maximum_speed;
3164
3165 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003166 * REVISIT: Here we should clear all pending IRQs to be
3167 * sure we're starting from a well known location.
3168 */
3169
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003170 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003171 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003172 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003173
Felipe Balbi72246da2011-08-19 18:10:58 +03003174 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3175 if (ret) {
3176 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003177 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003178 }
3179
3180 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003181
3182err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003183 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003184
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003185err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003186 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3187 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003188
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003189err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003190 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003191
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003192err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303193 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003194 dwc->ep0_trb, dwc->ep0_trb_addr);
3195
Felipe Balbi72246da2011-08-19 18:10:58 +03003196err0:
3197 return ret;
3198}
3199
Felipe Balbi7415f172012-04-30 14:56:33 +03003200/* -------------------------------------------------------------------------- */
3201
Felipe Balbi72246da2011-08-19 18:10:58 +03003202void dwc3_gadget_exit(struct dwc3 *dwc)
3203{
Felipe Balbi72246da2011-08-19 18:10:58 +03003204 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003205 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003206 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003207 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003208 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303209 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003210 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003211}
Felipe Balbi7415f172012-04-30 14:56:33 +03003212
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003213int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003214{
Roger Quadros9772b472016-04-12 11:33:29 +03003215 if (!dwc->gadget_driver)
3216 return 0;
3217
Roger Quadros1551e352017-02-15 14:16:26 +02003218 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003219 dwc3_disconnect_gadget(dwc);
3220 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003221
3222 return 0;
3223}
3224
3225int dwc3_gadget_resume(struct dwc3 *dwc)
3226{
Felipe Balbi7415f172012-04-30 14:56:33 +03003227 int ret;
3228
Roger Quadros9772b472016-04-12 11:33:29 +03003229 if (!dwc->gadget_driver)
3230 return 0;
3231
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003232 ret = __dwc3_gadget_start(dwc);
3233 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003234 goto err0;
3235
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003236 ret = dwc3_gadget_run_stop(dwc, true, false);
3237 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003238 goto err1;
3239
Felipe Balbi7415f172012-04-30 14:56:33 +03003240 return 0;
3241
3242err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003243 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003244
3245err0:
3246 return ret;
3247}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003248
3249void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3250{
3251 if (dwc->pending_events) {
3252 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3253 dwc->pending_events = false;
3254 enable_irq(dwc->irq_gadget);
3255 }
3256}