blob: 850fda01376894fad3d615112628f46ddafb946c [file] [log] [blame]
Felipe Balbibfad65e2017-04-19 14:59:27 +03001/*
Felipe Balbi72246da2011-08-19 18:10:58 +03002 * 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/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030039 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030043 * Caller should take care of locking. This function will return 0 on
44 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020045 */
46int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
47{
48 u32 reg;
49
50 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
51 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
52
53 switch (mode) {
54 case TEST_J:
55 case TEST_K:
56 case TEST_SE0_NAK:
57 case TEST_PACKET:
58 case TEST_FORCE_EN:
59 reg |= mode << 1;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66
67 return 0;
68}
69
Felipe Balbi8598bde2012-01-02 18:55:57 +020070/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030071 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * @dwc: pointer to our context structure
73 *
74 * Caller should take care of locking. This function will
75 * return the link state on success (>= 0) or -ETIMEDOUT.
76 */
77int dwc3_gadget_get_link_state(struct dwc3 *dwc)
78{
79 u32 reg;
80
81 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
82
83 return DWC3_DSTS_USBLNKST(reg);
84}
85
86/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030087 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * @dwc: pointer to our context structure
89 * @state: the state to put link into
90 *
91 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080092 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020093 */
94int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
95{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 u32 reg;
98
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 /*
100 * Wait until device controller is ready. Only applies to 1.94a and
101 * later RTL.
102 */
103 if (dwc->revision >= DWC3_REVISION_194A) {
104 while (--retries) {
105 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
106 if (reg & DWC3_DSTS_DCNRD)
107 udelay(5);
108 else
109 break;
110 }
111
112 if (retries <= 0)
113 return -ETIMEDOUT;
114 }
115
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
117 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
118
119 /* set requested state */
120 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
121 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
122
Paul Zimmerman802fde92012-04-27 13:10:52 +0300123 /*
124 * The following code is racy when called from dwc3_gadget_wakeup,
125 * and is not needed, at least on newer versions
126 */
127 if (dwc->revision >= DWC3_REVISION_194A)
128 return 0;
129
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300131 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200132 while (--retries) {
133 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 if (DWC3_DSTS_USBLNKST(reg) == state)
136 return 0;
137
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800138 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 }
140
Felipe Balbi8598bde2012-01-02 18:55:57 +0200141 return -ETIMEDOUT;
142}
143
John Youndca01192016-05-19 17:26:05 -0700144/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300145 * dwc3_ep_inc_trb - increment a trb index.
146 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700147 *
148 * The index should never point to the link TRB. After incrementing,
149 * if it is point to the link TRB, wrap around to the beginning. The
150 * link TRB is always at the last TRB entry.
151 */
152static void dwc3_ep_inc_trb(u8 *index)
153{
154 (*index)++;
155 if (*index == (DWC3_TRB_NUM - 1))
156 *index = 0;
157}
158
Felipe Balbibfad65e2017-04-19 14:59:27 +0300159/**
160 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
161 * @dep: The endpoint whose enqueue pointer we're incrementing
162 */
Felipe Balbief966b92016-04-05 13:09:51 +0300163static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200164{
John Youndca01192016-05-19 17:26:05 -0700165 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300166}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167
Felipe Balbibfad65e2017-04-19 14:59:27 +0300168/**
169 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
170 * @dep: The endpoint whose enqueue pointer we're incrementing
171 */
Felipe Balbief966b92016-04-05 13:09:51 +0300172static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
Felipe Balbibfad65e2017-04-19 14:59:27 +0300177/**
178 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
179 * @dep: The endpoint to whom the request belongs to
180 * @req: The request we're giving back
181 * @status: completion code for the request
182 *
183 * Must be called with controller's lock held and interrupts disabled. This
184 * function will unmap @req and call its ->complete() callback to notify upper
185 * layers that it has completed.
186 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300187void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
188 int status)
189{
190 struct dwc3 *dwc = dep->dwc;
191
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300192 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300193 list_del(&req->list);
Felipe Balbie62c5bc52016-10-25 13:47:21 +0300194 req->remaining = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300195
196 if (req->request.status == -EINPROGRESS)
197 req->request.status = status;
198
Jack Pham4a71fcb2017-06-29 00:53:31 -0700199 if (req->trb)
200 usb_gadget_unmap_request_by_dev(dwc->sysdev,
201 &req->request, req->direction);
202
203 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300204
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500205 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300206
207 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200208 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300209 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300210
211 if (dep->number > 1)
212 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300213}
214
Felipe Balbibfad65e2017-04-19 14:59:27 +0300215/**
216 * dwc3_send_gadget_generic_command - issue a generic command for the controller
217 * @dwc: pointer to the controller context
218 * @cmd: the command to be issued
219 * @param: command parameter
220 *
221 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
222 * and wait for its completion.
223 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500224int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300225{
226 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300227 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300228 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300229 u32 reg;
230
231 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
232 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
233
234 do {
235 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
236 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300237 status = DWC3_DGCMD_STATUS(reg);
238 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300239 ret = -EINVAL;
240 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300241 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100242 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300243
244 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300245 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300246 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300247 }
248
Felipe Balbi71f7e702016-05-23 14:16:19 +0300249 trace_dwc3_gadget_generic_cmd(cmd, param, status);
250
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300251 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300252}
253
Felipe Balbic36d8e92016-04-04 12:46:33 +0300254static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
255
Felipe Balbibfad65e2017-04-19 14:59:27 +0300256/**
257 * dwc3_send_gadget_ep_cmd - issue an endpoint command
258 * @dep: the endpoint to which the command is going to be issued
259 * @cmd: the command to be issued
260 * @params: parameters to the command
261 *
262 * Caller should handle locking. This function will issue @cmd with given
263 * @params to @dep and wait for its completion.
264 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300265int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
266 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300267{
Felipe Balbi8897a762016-09-22 10:56:08 +0300268 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300269 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200270 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300271 u32 reg;
272
Felipe Balbi0933df12016-05-23 14:02:33 +0300273 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300274 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300275 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300276
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300277 /*
278 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
279 * we're issuing an endpoint command, we must check if
280 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
281 *
282 * We will also set SUSPHY bit to what it was before returning as stated
283 * by the same section on Synopsys databook.
284 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300285 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
286 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
287 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
288 susphy = true;
289 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
290 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
291 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300292 }
293
Felipe Balbi59999142016-09-22 12:25:28 +0300294 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300295 int needs_wakeup;
296
297 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
298 dwc->link_state == DWC3_LINK_STATE_U2 ||
299 dwc->link_state == DWC3_LINK_STATE_U3);
300
301 if (unlikely(needs_wakeup)) {
302 ret = __dwc3_gadget_wakeup(dwc);
303 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
304 ret);
305 }
306 }
307
Felipe Balbi2eb88012016-04-12 16:53:39 +0300308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
310 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300311
Felipe Balbi8897a762016-09-22 10:56:08 +0300312 /*
313 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
314 * not relying on XferNotReady, we can make use of a special "No
315 * Response Update Transfer" command where we should clear both CmdAct
316 * and CmdIOC bits.
317 *
318 * With this, we don't need to wait for command completion and can
319 * straight away issue further commands to the endpoint.
320 *
321 * NOTICE: We're making an assumption that control endpoints will never
322 * make use of Update Transfer command. This is a safe assumption
323 * because we can never have more than one request at a time with
324 * Control Endpoints. If anybody changes that assumption, this chunk
325 * needs to be updated accordingly.
326 */
327 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
328 !usb_endpoint_xfer_isoc(desc))
329 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
330 else
331 cmd |= DWC3_DEPCMD_CMDACT;
332
333 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300334 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300335 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300337 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000338
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000339 switch (cmd_status) {
340 case 0:
341 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300342 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000343 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000344 ret = -EINVAL;
345 break;
346 case DEPEVT_TRANSFER_BUS_EXPIRY:
347 /*
348 * SW issues START TRANSFER command to
349 * isochronous ep with future frame interval. If
350 * future interval time has already passed when
351 * core receives the command, it will respond
352 * with an error status of 'Bus Expiry'.
353 *
354 * Instead of always returning -EINVAL, let's
355 * give a hint to the gadget driver that this is
356 * the case by returning -EAGAIN.
357 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000358 ret = -EAGAIN;
359 break;
360 default:
361 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
362 }
363
Felipe Balbic0ca3242016-04-04 09:11:51 +0300364 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300366 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300367
Felipe Balbif6bb2252016-05-23 13:53:34 +0300368 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300369 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300370 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300371 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300372
Felipe Balbi0933df12016-05-23 14:02:33 +0300373 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
374
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300375 if (ret == 0) {
376 switch (DWC3_DEPCMD_CMD(cmd)) {
377 case DWC3_DEPCMD_STARTTRANSFER:
378 dep->flags |= DWC3_EP_TRANSFER_STARTED;
379 break;
380 case DWC3_DEPCMD_ENDTRANSFER:
381 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
382 break;
383 default:
384 /* nothing */
385 break;
386 }
387 }
388
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300389 if (unlikely(susphy)) {
390 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
391 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
392 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
393 }
394
Felipe Balbic0ca3242016-04-04 09:11:51 +0300395 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300396}
397
John Youn50c763f2016-05-31 17:49:56 -0700398static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
399{
400 struct dwc3 *dwc = dep->dwc;
401 struct dwc3_gadget_ep_cmd_params params;
402 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
403
404 /*
405 * As of core revision 2.60a the recommended programming model
406 * is to set the ClearPendIN bit when issuing a Clear Stall EP
407 * command for IN endpoints. This is to prevent an issue where
408 * some (non-compliant) hosts may not send ACK TPs for pending
409 * IN transfers due to a mishandled error condition. Synopsys
410 * STAR 9000614252.
411 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800412 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
413 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700414 cmd |= DWC3_DEPCMD_CLEARPENDIN;
415
416 memset(&params, 0, sizeof(params));
417
Felipe Balbi2cd47182016-04-12 16:42:43 +0300418 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700419}
420
Felipe Balbi72246da2011-08-19 18:10:58 +0300421static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200422 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300423{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300424 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300425
426 return dep->trb_pool_dma + offset;
427}
428
429static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
430{
431 struct dwc3 *dwc = dep->dwc;
432
433 if (dep->trb_pool)
434 return 0;
435
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530436 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300437 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
438 &dep->trb_pool_dma, GFP_KERNEL);
439 if (!dep->trb_pool) {
440 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
441 dep->name);
442 return -ENOMEM;
443 }
444
445 return 0;
446}
447
448static void dwc3_free_trb_pool(struct dwc3_ep *dep)
449{
450 struct dwc3 *dwc = dep->dwc;
451
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530452 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300453 dep->trb_pool, dep->trb_pool_dma);
454
455 dep->trb_pool = NULL;
456 dep->trb_pool_dma = 0;
457}
458
John Younc4509602016-02-16 20:10:53 -0800459static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
460
461/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300462 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800463 * @dwc: pointer to our controller context structure
464 * @dep: endpoint that is being enabled
465 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300466 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
467 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800468 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300469 * The assignment of transfer resources cannot perfectly follow the data book
470 * due to the fact that the controller driver does not have all knowledge of the
471 * configuration in advance. It is given this information piecemeal by the
472 * composite gadget framework after every SET_CONFIGURATION and
473 * SET_INTERFACE. Trying to follow the databook programming model in this
474 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800475 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300476 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
477 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
478 * incorrect in the scenario of multiple interfaces.
479 *
480 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800481 * endpoint on alt setting (8.1.6).
482 *
483 * The following simplified method is used instead:
484 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300485 * All hardware endpoints can be assigned a transfer resource and this setting
486 * will stay persistent until either a core reset or hibernation. So whenever we
487 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
488 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800489 * guaranteed that there are as many transfer resources as endpoints.
490 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300491 * This function is called for each endpoint when it is being enabled but is
492 * triggered only when called for EP0-out, which always happens first, and which
493 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800494 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300495static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
496{
497 struct dwc3_gadget_ep_cmd_params params;
498 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800499 int i;
500 int ret;
501
502 if (dep->number)
503 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300504
505 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800506 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300507
Felipe Balbi2cd47182016-04-12 16:42:43 +0300508 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800509 if (ret)
510 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300511
John Younc4509602016-02-16 20:10:53 -0800512 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
513 struct dwc3_ep *dep = dwc->eps[i];
514
515 if (!dep)
516 continue;
517
518 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
519 if (ret)
520 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 }
522
523 return 0;
524}
525
526static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300527 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300528{
John Youn39ebb052016-11-09 16:36:28 -0800529 const struct usb_ss_ep_comp_descriptor *comp_desc;
530 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 struct dwc3_gadget_ep_cmd_params params;
532
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300533 if (dev_WARN_ONCE(dwc->dev, modify && restore,
534 "Can't modify and restore\n"))
535 return -EINVAL;
536
John Youn39ebb052016-11-09 16:36:28 -0800537 comp_desc = dep->endpoint.comp_desc;
538 desc = dep->endpoint.desc;
539
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 memset(&params, 0x00, sizeof(params));
541
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300542 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900543 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
544
545 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800546 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300547 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300548 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900549 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300550
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300551 if (modify) {
552 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
553 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600554 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
555 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300556 } else {
557 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600558 }
559
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300560 if (usb_endpoint_xfer_control(desc))
561 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300562
563 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
564 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300565
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200566 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300567 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
568 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300569 dep->stream_capable = true;
570 }
571
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500572 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300573 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300574
575 /*
576 * We are doing 1:1 mapping for endpoints, meaning
577 * Physical Endpoints 2 maps to Logical Endpoint 2 and
578 * so on. We consider the direction bit as part of the physical
579 * endpoint number. So USB endpoint 0x81 is 0x03.
580 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300581 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300582
583 /*
584 * We must use the lower 16 TX FIFOs even though
585 * HW might have more
586 */
587 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300588 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300589
590 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300591 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 dep->interval = 1 << (desc->bInterval - 1);
593 }
594
Felipe Balbi2cd47182016-04-12 16:42:43 +0300595 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300596}
597
598static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
599{
600 struct dwc3_gadget_ep_cmd_params params;
601
602 memset(&params, 0x00, sizeof(params));
603
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300604 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300605
Felipe Balbi2cd47182016-04-12 16:42:43 +0300606 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
607 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300608}
609
610/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300611 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 * @dep: endpoint to be initialized
Felipe Balbibfad65e2017-04-19 14:59:27 +0300613 * @modify: if true, modify existing endpoint configuration
614 * @restore: if true, restore endpoint configuration from scratch buffer
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300616 * Caller should take care of locking. Execute all necessary commands to
617 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 */
619static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300620 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300621{
John Youn39ebb052016-11-09 16:36:28 -0800622 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800624
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300626 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300627
628 if (!(dep->flags & DWC3_EP_ENABLED)) {
629 ret = dwc3_gadget_start_config(dwc, dep);
630 if (ret)
631 return ret;
632 }
633
John Youn39ebb052016-11-09 16:36:28 -0800634 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 if (ret)
636 return ret;
637
638 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200639 struct dwc3_trb *trb_st_hw;
640 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300641
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 dep->type = usb_endpoint_type(desc);
643 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800644 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
646 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
647 reg |= DWC3_DALEPENA_EP(dep->number);
648 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
649
Baolin Wang76a638f2016-10-31 19:38:36 +0800650 init_waitqueue_head(&dep->wait_end_transfer);
651
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300652 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200653 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300654
John Youn0d257442016-05-19 17:26:08 -0700655 /* Initialize the TRB ring */
656 dep->trb_dequeue = 0;
657 dep->trb_enqueue = 0;
658 memset(dep->trb_pool, 0,
659 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
660
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300661 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300662 trb_st_hw = &dep->trb_pool[0];
663
Felipe Balbif6bafc62012-02-06 11:04:53 +0200664 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200665 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
666 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
667 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
668 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 }
670
Felipe Balbia97ea992016-09-29 16:28:56 +0300671 /*
672 * Issue StartTransfer here with no-op TRB so we can always rely on No
673 * Response Update Transfer command.
674 */
675 if (usb_endpoint_xfer_bulk(desc)) {
676 struct dwc3_gadget_ep_cmd_params params;
677 struct dwc3_trb *trb;
678 dma_addr_t trb_dma;
679 u32 cmd;
680
681 memset(&params, 0, sizeof(params));
682 trb = &dep->trb_pool[0];
683 trb_dma = dwc3_trb_dma_offset(dep, trb);
684
685 params.param0 = upper_32_bits(trb_dma);
686 params.param1 = lower_32_bits(trb_dma);
687
688 cmd = DWC3_DEPCMD_STARTTRANSFER;
689
690 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
691 if (ret < 0)
692 return ret;
693
694 dep->flags |= DWC3_EP_BUSY;
695
696 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
697 WARN_ON_ONCE(!dep->resource_index);
698 }
699
Felipe Balbi2870e502016-11-03 13:53:29 +0200700
701out:
702 trace_dwc3_gadget_ep_enable(dep);
703
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 return 0;
705}
706
Paul Zimmermanb992e682012-04-27 14:17:35 +0300707static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200708static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300709{
710 struct dwc3_request *req;
711
Felipe Balbi0e146022016-06-21 10:32:02 +0300712 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300713
Felipe Balbi0e146022016-06-21 10:32:02 +0300714 /* - giveback all requests to gadget driver */
715 while (!list_empty(&dep->started_list)) {
716 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200717
Felipe Balbi0e146022016-06-21 10:32:02 +0300718 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200719 }
720
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200721 while (!list_empty(&dep->pending_list)) {
722 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300723
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200724 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300726}
727
728/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300729 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 * @dep: the endpoint to disable
731 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300732 * This function undoes what __dwc3_gadget_ep_enable did and also removes
733 * requests which are currently being processed by the hardware and those which
734 * are not yet scheduled.
735 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200736 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300738static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
739{
740 struct dwc3 *dwc = dep->dwc;
741 u32 reg;
742
Felipe Balbi2870e502016-11-03 13:53:29 +0200743 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500744
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200745 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300746
Felipe Balbi687ef982014-04-16 10:30:33 -0500747 /* make sure HW endpoint isn't stalled */
748 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500749 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
752 reg &= ~DWC3_DALEPENA_EP(dep->number);
753 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
754
Felipe Balbi879631a2011-09-30 10:58:47 +0300755 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800757 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
John Youn39ebb052016-11-09 16:36:28 -0800759 /* Clear out the ep descriptors for non-ep0 */
760 if (dep->number > 1) {
761 dep->endpoint.comp_desc = NULL;
762 dep->endpoint.desc = NULL;
763 }
764
Felipe Balbi72246da2011-08-19 18:10:58 +0300765 return 0;
766}
767
768/* -------------------------------------------------------------------------- */
769
770static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
771 const struct usb_endpoint_descriptor *desc)
772{
773 return -EINVAL;
774}
775
776static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
777{
778 return -EINVAL;
779}
780
781/* -------------------------------------------------------------------------- */
782
783static int dwc3_gadget_ep_enable(struct usb_ep *ep,
784 const struct usb_endpoint_descriptor *desc)
785{
786 struct dwc3_ep *dep;
787 struct dwc3 *dwc;
788 unsigned long flags;
789 int ret;
790
791 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
792 pr_debug("dwc3: invalid parameters\n");
793 return -EINVAL;
794 }
795
796 if (!desc->wMaxPacketSize) {
797 pr_debug("dwc3: missing wMaxPacketSize\n");
798 return -EINVAL;
799 }
800
801 dep = to_dwc3_ep(ep);
802 dwc = dep->dwc;
803
Felipe Balbi95ca9612015-12-10 13:08:20 -0600804 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
805 "%s is already enabled\n",
806 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300807 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300808
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800810 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300811 spin_unlock_irqrestore(&dwc->lock, flags);
812
813 return ret;
814}
815
816static int dwc3_gadget_ep_disable(struct usb_ep *ep)
817{
818 struct dwc3_ep *dep;
819 struct dwc3 *dwc;
820 unsigned long flags;
821 int ret;
822
823 if (!ep) {
824 pr_debug("dwc3: invalid parameters\n");
825 return -EINVAL;
826 }
827
828 dep = to_dwc3_ep(ep);
829 dwc = dep->dwc;
830
Felipe Balbi95ca9612015-12-10 13:08:20 -0600831 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
832 "%s is already disabled\n",
833 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300834 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300835
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 spin_lock_irqsave(&dwc->lock, flags);
837 ret = __dwc3_gadget_ep_disable(dep);
838 spin_unlock_irqrestore(&dwc->lock, flags);
839
840 return ret;
841}
842
843static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
844 gfp_t gfp_flags)
845{
846 struct dwc3_request *req;
847 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300848
849 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900850 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300852
853 req->epnum = dep->number;
854 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300855
Felipe Balbi68d34c82016-05-30 13:34:58 +0300856 dep->allocated_requests++;
857
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500858 trace_dwc3_alloc_request(req);
859
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 return &req->request;
861}
862
863static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
864 struct usb_request *request)
865{
866 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300867 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300868
Felipe Balbi68d34c82016-05-30 13:34:58 +0300869 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500870 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 kfree(req);
872}
873
Felipe Balbi2c78c022016-08-12 13:13:10 +0300874static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
875
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200876static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
877 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
878 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200879{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300880 struct dwc3 *dwc = dep->dwc;
881 struct usb_gadget *gadget = &dwc->gadget;
882 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200883
Felipe Balbief966b92016-04-05 13:09:51 +0300884 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530885
Felipe Balbif6bafc62012-02-06 11:04:53 +0200886 trb->size = DWC3_TRB_SIZE_LENGTH(length);
887 trb->bpl = lower_32_bits(dma);
888 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200889
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200890 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200891 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200892 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200893 break;
894
895 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300896 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530897 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300898
Manu Gautam40d829f2017-07-19 17:07:10 +0530899 /*
900 * USB Specification 2.0 Section 5.9.2 states that: "If
901 * there is only a single transaction in the microframe,
902 * only a DATA0 data packet PID is used. If there are
903 * two transactions per microframe, DATA1 is used for
904 * the first transaction data packet and DATA0 is used
905 * for the second transaction data packet. If there are
906 * three transactions per microframe, DATA2 is used for
907 * the first transaction data packet, DATA1 is used for
908 * the second, and DATA0 is used for the third."
909 *
910 * IOW, we should satisfy the following cases:
911 *
912 * 1) length <= maxpacket
913 * - DATA0
914 *
915 * 2) maxpacket < length <= (2 * maxpacket)
916 * - DATA1, DATA0
917 *
918 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
919 * - DATA2, DATA1, DATA0
920 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300921 if (speed == USB_SPEED_HIGH) {
922 struct usb_ep *ep = &dep->endpoint;
Manu Gautam40d829f2017-07-19 17:07:10 +0530923 unsigned int mult = ep->mult - 1;
924 unsigned int maxp = usb_endpoint_maxp(ep->desc);
925
926 if (length <= (2 * maxp))
927 mult--;
928
929 if (length <= maxp)
930 mult--;
931
932 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300933 }
934 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530935 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300936 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200937
938 /* always enable Interrupt on Missed ISOC */
939 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200940 break;
941
942 case USB_ENDPOINT_XFER_BULK:
943 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200944 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200945 break;
946 default:
947 /*
948 * This is only possible with faulty memory because we
949 * checked it already :)
950 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300951 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
952 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200953 }
954
Felipe Balbica4d44e2016-03-10 13:53:27 +0200955 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300956 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300957 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600958
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200959 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300960 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
961 }
962
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200963 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300964 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300965 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200966
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530967 if (chain)
968 trb->ctrl |= DWC3_TRB_CTRL_CHN;
969
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200970 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200971 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200972
973 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500974
975 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200976}
977
John Youn361572b2016-05-19 17:26:17 -0700978/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200979 * dwc3_prepare_one_trb - setup one TRB from one request
980 * @dep: endpoint for which this request is prepared
981 * @req: dwc3_request pointer
982 * @chain: should this TRB be chained to the next?
983 * @node: only for isochronous endpoints. First TRB needs different type.
984 */
985static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
986 struct dwc3_request *req, unsigned chain, unsigned node)
987{
988 struct dwc3_trb *trb;
989 unsigned length = req->request.length;
990 unsigned stream_id = req->request.stream_id;
991 unsigned short_not_ok = req->request.short_not_ok;
992 unsigned no_interrupt = req->request.no_interrupt;
993 dma_addr_t dma = req->request.dma;
994
995 trb = &dep->trb_pool[dep->trb_enqueue];
996
997 if (!req->trb) {
998 dwc3_gadget_move_started_request(req);
999 req->trb = trb;
1000 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1001 dep->queued_requests++;
1002 }
1003
1004 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1005 stream_id, short_not_ok, no_interrupt);
1006}
1007
1008/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001009 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -07001010 * @dep: The endpoint with the TRB ring
1011 * @index: The index of the current TRB in the ring
1012 *
1013 * Returns the TRB prior to the one pointed to by the index. If the
1014 * index is 0, we will wrap backwards, skip the link TRB, and return
1015 * the one just before that.
1016 */
1017static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1018{
Felipe Balbi45438a02016-08-11 12:26:59 +03001019 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001020
Felipe Balbi45438a02016-08-11 12:26:59 +03001021 if (!tmp)
1022 tmp = DWC3_TRB_NUM - 1;
1023
1024 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001025}
1026
Felipe Balbic4233572016-05-12 14:08:34 +03001027static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1028{
1029 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001030 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001031
1032 /*
1033 * If enqueue & dequeue are equal than it is either full or empty.
1034 *
1035 * One way to know for sure is if the TRB right before us has HWO bit
1036 * set or not. If it has, then we're definitely full and can't fit any
1037 * more transfers in our ring.
1038 */
1039 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001040 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001041 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001042 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001043
1044 return DWC3_TRB_NUM - 1;
1045 }
1046
John Youn9d7aba72016-08-26 18:43:01 -07001047 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001048 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001049
John Youn9d7aba72016-08-26 18:43:01 -07001050 if (dep->trb_dequeue < dep->trb_enqueue)
1051 trbs_left--;
1052
John Youn32db3d92016-05-19 17:26:12 -07001053 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001054}
1055
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001056static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001057 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001058{
Felipe Balbi1f512112016-08-12 13:17:27 +03001059 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001061 int i;
1062
Felipe Balbi1f512112016-08-12 13:17:27 +03001063 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001064 unsigned int length = req->request.length;
1065 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1066 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001067 unsigned chain = true;
1068
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001069 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001070 chain = false;
1071
Felipe Balbic6267a52017-01-05 14:58:46 +02001072 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1073 struct dwc3 *dwc = dep->dwc;
1074 struct dwc3_trb *trb;
1075
1076 req->unaligned = true;
1077
1078 /* prepare normal TRB */
1079 dwc3_prepare_one_trb(dep, req, true, i);
1080
1081 /* Now prepare one extra TRB to align transfer size */
1082 trb = &dep->trb_pool[dep->trb_enqueue];
1083 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1084 maxp - rem, false, 0,
1085 req->request.stream_id,
1086 req->request.short_not_ok,
1087 req->request.no_interrupt);
1088 } else {
1089 dwc3_prepare_one_trb(dep, req, chain, i);
1090 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001091
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001092 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001093 break;
1094 }
1095}
1096
1097static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001098 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001099{
Felipe Balbic6267a52017-01-05 14:58:46 +02001100 unsigned int length = req->request.length;
1101 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1102 unsigned int rem = length % maxp;
1103
1104 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1105 struct dwc3 *dwc = dep->dwc;
1106 struct dwc3_trb *trb;
1107
1108 req->unaligned = true;
1109
1110 /* prepare normal TRB */
1111 dwc3_prepare_one_trb(dep, req, true, 0);
1112
1113 /* Now prepare one extra TRB to align transfer size */
1114 trb = &dep->trb_pool[dep->trb_enqueue];
1115 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1116 false, 0, req->request.stream_id,
1117 req->request.short_not_ok,
1118 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001119 } else if (req->request.zero && req->request.length &&
1120 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1121 struct dwc3 *dwc = dep->dwc;
1122 struct dwc3_trb *trb;
1123
1124 req->zero = true;
1125
1126 /* prepare normal TRB */
1127 dwc3_prepare_one_trb(dep, req, true, 0);
1128
1129 /* Now prepare one extra TRB to handle ZLP */
1130 trb = &dep->trb_pool[dep->trb_enqueue];
1131 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1132 false, 0, req->request.stream_id,
1133 req->request.short_not_ok,
1134 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001135 } else {
1136 dwc3_prepare_one_trb(dep, req, false, 0);
1137 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001138}
1139
Felipe Balbi72246da2011-08-19 18:10:58 +03001140/*
1141 * dwc3_prepare_trbs - setup TRBs from requests
1142 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001143 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001144 * The function goes through the requests list and sets up TRBs for the
1145 * transfers. The function returns once there are no more TRBs available or
1146 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 */
Felipe Balbic4233572016-05-12 14:08:34 +03001148static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001149{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001150 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001151
1152 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1153
Felipe Balbid86c5a62016-10-25 13:48:52 +03001154 /*
1155 * We can get in a situation where there's a request in the started list
1156 * but there weren't enough TRBs to fully kick it in the first time
1157 * around, so it has been waiting for more TRBs to be freed up.
1158 *
1159 * In that case, we should check if we have a request with pending_sgs
1160 * in the started list and prepare TRBs for that request first,
1161 * otherwise we will prepare TRBs completely out of order and that will
1162 * break things.
1163 */
1164 list_for_each_entry(req, &dep->started_list, list) {
1165 if (req->num_pending_sgs > 0)
1166 dwc3_prepare_one_trb_sg(dep, req);
1167
1168 if (!dwc3_calc_trbs_left(dep))
1169 return;
1170 }
1171
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001172 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001173 struct dwc3 *dwc = dep->dwc;
1174 int ret;
1175
1176 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1177 dep->direction);
1178 if (ret)
1179 return;
1180
1181 req->sg = req->request.sg;
1182 req->num_pending_sgs = req->request.num_mapped_sgs;
1183
Felipe Balbi1f512112016-08-12 13:17:27 +03001184 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001185 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001186 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001187 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001188
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001189 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001190 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001191 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001192}
1193
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001194static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001195{
1196 struct dwc3_gadget_ep_cmd_params params;
1197 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001198 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 int ret;
1200 u32 cmd;
1201
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001202 if (!dwc3_calc_trbs_left(dep))
1203 return 0;
1204
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001205 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001206
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001207 dwc3_prepare_trbs(dep);
1208 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001209 if (!req) {
1210 dep->flags |= DWC3_EP_PENDING_REQUEST;
1211 return 0;
1212 }
1213
1214 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001215
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001216 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301217 params.param0 = upper_32_bits(req->trb_dma);
1218 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001219 cmd = DWC3_DEPCMD_STARTTRANSFER |
1220 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301221 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001222 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1223 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301224 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001225
Felipe Balbi2cd47182016-04-12 16:42:43 +03001226 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001227 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 /*
1229 * FIXME we need to iterate over the list of requests
1230 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001231 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001233 if (req->trb)
1234 memset(req->trb, 0, sizeof(struct dwc3_trb));
Janusz Dziedzic8ab89da2016-11-09 11:01:31 +01001235 dep->queued_requests--;
Felipe Balbi15b8d932016-09-22 10:59:12 +03001236 dwc3_gadget_giveback(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001237 return ret;
1238 }
1239
1240 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001241
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001242 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001243 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001244 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001245 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001246
Felipe Balbi72246da2011-08-19 18:10:58 +03001247 return 0;
1248}
1249
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001250static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1251{
1252 u32 reg;
1253
1254 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1255 return DWC3_DSTS_SOFFN(reg);
1256}
1257
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301258static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1259 struct dwc3_ep *dep, u32 cur_uf)
1260{
1261 u32 uf;
1262
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001263 if (list_empty(&dep->pending_list)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001264 dev_info(dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001265 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301266 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301267 return;
1268 }
1269
John Younaf771d72017-01-26 11:58:40 -08001270 /*
1271 * Schedule the first trb for one interval in the future or at
1272 * least 4 microframes.
1273 */
1274 uf = cur_uf + max_t(u32, 4, dep->interval);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301275
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001276 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301277}
1278
1279static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1280 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1281{
1282 u32 cur_uf, mask;
1283
1284 mask = ~(dep->interval - 1);
1285 cur_uf = event->parameters & mask;
1286
1287 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1288}
1289
Felipe Balbi72246da2011-08-19 18:10:58 +03001290static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1291{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001292 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001293
Felipe Balbibb423982015-11-16 15:31:21 -06001294 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001295 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1296 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001297 return -ESHUTDOWN;
1298 }
1299
Felipe Balbi04fb3652017-05-17 15:57:45 +03001300 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1301 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001302 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001303
Felipe Balbifc8bb912016-05-16 13:14:48 +03001304 pm_runtime_get(dwc->dev);
1305
Felipe Balbi72246da2011-08-19 18:10:58 +03001306 req->request.actual = 0;
1307 req->request.status = -EINPROGRESS;
1308 req->direction = dep->direction;
1309 req->epnum = dep->number;
1310
Felipe Balbife84f522015-09-01 09:01:38 -05001311 trace_dwc3_ep_queue(req);
1312
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001313 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001314
Felipe Balbid889c232016-09-29 15:44:29 +03001315 /*
1316 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1317 * wait for a XferNotReady event so we will know what's the current
1318 * (micro-)frame number.
1319 *
1320 * Without this trick, we are very, very likely gonna get Bus Expiry
1321 * errors which will force us issue EndTransfer command.
1322 */
1323 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001324 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1325 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1326 dwc3_stop_active_transfer(dwc, dep->number, true);
1327 dep->flags = DWC3_EP_ENABLED;
1328 } else {
1329 u32 cur_uf;
1330
1331 cur_uf = __dwc3_gadget_get_frame(dwc);
1332 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzic87aba102016-11-09 11:01:34 +01001333 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001334 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001335 return 0;
Felipe Balbi08a36b52016-08-11 14:27:52 +03001336 }
Roger Quadrosf1d68262017-04-21 15:58:08 +03001337
1338 if ((dep->flags & DWC3_EP_BUSY) &&
Felipe Balbi64e01082017-09-05 14:32:55 +03001339 !(dep->flags & DWC3_EP_MISSED_ISOC))
1340 goto out;
Roger Quadrosf1d68262017-04-21 15:58:08 +03001341
Felipe Balbi64e01082017-09-05 14:32:55 +03001342 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001343 }
1344
Roger Quadrosf1d68262017-04-21 15:58:08 +03001345out:
Felipe Balbi64e01082017-09-05 14:32:55 +03001346 return __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001347}
1348
1349static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1350 gfp_t gfp_flags)
1351{
1352 struct dwc3_request *req = to_dwc3_request(request);
1353 struct dwc3_ep *dep = to_dwc3_ep(ep);
1354 struct dwc3 *dwc = dep->dwc;
1355
1356 unsigned long flags;
1357
1358 int ret;
1359
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001360 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001361 ret = __dwc3_gadget_ep_queue(dep, req);
1362 spin_unlock_irqrestore(&dwc->lock, flags);
1363
1364 return ret;
1365}
1366
1367static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1368 struct usb_request *request)
1369{
1370 struct dwc3_request *req = to_dwc3_request(request);
1371 struct dwc3_request *r = NULL;
1372
1373 struct dwc3_ep *dep = to_dwc3_ep(ep);
1374 struct dwc3 *dwc = dep->dwc;
1375
1376 unsigned long flags;
1377 int ret = 0;
1378
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001379 trace_dwc3_ep_dequeue(req);
1380
Felipe Balbi72246da2011-08-19 18:10:58 +03001381 spin_lock_irqsave(&dwc->lock, flags);
1382
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001383 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001384 if (r == req)
1385 break;
1386 }
1387
1388 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001389 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001390 if (r == req)
1391 break;
1392 }
1393 if (r == req) {
1394 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001395 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001396
1397 /*
1398 * If request was already started, this means we had to
1399 * stop the transfer. With that we also need to ignore
1400 * all TRBs used by the request, however TRBs can only
1401 * be modified after completion of END_TRANSFER
1402 * command. So what we do here is that we wait for
1403 * END_TRANSFER completion and only after that, we jump
1404 * over TRBs by clearing HWO and incrementing dequeue
1405 * pointer.
1406 *
1407 * Note that we have 2 possible types of transfers here:
1408 *
1409 * i) Linear buffer request
1410 * ii) SG-list based request
1411 *
1412 * SG-list based requests will have r->num_pending_sgs
1413 * set to a valid number (> 0). Linear requests,
1414 * normally use a single TRB.
1415 *
1416 * For each of these two cases, if r->unaligned flag is
1417 * set, one extra TRB has been used to align transfer
1418 * size to wMaxPacketSize.
1419 *
1420 * All of these cases need to be taken into
1421 * consideration so we don't mess up our TRB ring
1422 * pointers.
1423 */
1424 wait_event_lock_irq(dep->wait_end_transfer,
1425 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1426 dwc->lock);
1427
1428 if (!r->trb)
1429 goto out1;
1430
1431 if (r->num_pending_sgs) {
1432 struct dwc3_trb *trb;
1433 int i = 0;
1434
1435 for (i = 0; i < r->num_pending_sgs; i++) {
1436 trb = r->trb + i;
1437 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1438 dwc3_ep_inc_deq(dep);
1439 }
1440
Felipe Balbid6e5a542017-04-07 16:34:38 +03001441 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001442 trb = r->trb + r->num_pending_sgs + 1;
1443 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1444 dwc3_ep_inc_deq(dep);
1445 }
1446 } else {
1447 struct dwc3_trb *trb = r->trb;
1448
1449 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1450 dwc3_ep_inc_deq(dep);
1451
Felipe Balbid6e5a542017-04-07 16:34:38 +03001452 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001453 trb = r->trb + 1;
1454 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1455 dwc3_ep_inc_deq(dep);
1456 }
1457 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301458 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001459 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001460 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 request, ep->name);
1462 ret = -EINVAL;
1463 goto out0;
1464 }
1465
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301466out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 /* giveback the request */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001468 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001469 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1470
1471out0:
1472 spin_unlock_irqrestore(&dwc->lock, flags);
1473
1474 return ret;
1475}
1476
Felipe Balbi7a608552014-09-24 14:19:52 -05001477int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001478{
1479 struct dwc3_gadget_ep_cmd_params params;
1480 struct dwc3 *dwc = dep->dwc;
1481 int ret;
1482
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001483 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1484 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1485 return -EINVAL;
1486 }
1487
Felipe Balbi72246da2011-08-19 18:10:58 +03001488 memset(&params, 0x00, sizeof(params));
1489
1490 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001491 struct dwc3_trb *trb;
1492
1493 unsigned transfer_in_flight;
1494 unsigned started;
1495
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001496 if (dep->flags & DWC3_EP_STALL)
1497 return 0;
1498
Felipe Balbi69450c42016-05-30 13:37:02 +03001499 if (dep->number > 1)
1500 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1501 else
1502 trb = &dwc->ep0_trb[dep->trb_enqueue];
1503
1504 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1505 started = !list_empty(&dep->started_list);
1506
1507 if (!protocol && ((dep->direction && transfer_in_flight) ||
1508 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001509 return -EAGAIN;
1510 }
1511
Felipe Balbi2cd47182016-04-12 16:42:43 +03001512 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1513 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001515 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 dep->name);
1517 else
1518 dep->flags |= DWC3_EP_STALL;
1519 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001520 if (!(dep->flags & DWC3_EP_STALL))
1521 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001522
John Youn50c763f2016-05-31 17:49:56 -07001523 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001525 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 dep->name);
1527 else
Alan Sterna535d812013-11-01 12:05:12 -04001528 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001530
Felipe Balbi72246da2011-08-19 18:10:58 +03001531 return ret;
1532}
1533
1534static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1535{
1536 struct dwc3_ep *dep = to_dwc3_ep(ep);
1537 struct dwc3 *dwc = dep->dwc;
1538
1539 unsigned long flags;
1540
1541 int ret;
1542
1543 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001544 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001545 spin_unlock_irqrestore(&dwc->lock, flags);
1546
1547 return ret;
1548}
1549
1550static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1551{
1552 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001553 struct dwc3 *dwc = dep->dwc;
1554 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001555 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001556
Paul Zimmerman249a4562012-02-24 17:32:16 -08001557 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 dep->flags |= DWC3_EP_WEDGE;
1559
Pratyush Anand08f0d962012-06-25 22:40:43 +05301560 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001561 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301562 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001563 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001564 spin_unlock_irqrestore(&dwc->lock, flags);
1565
1566 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001567}
1568
1569/* -------------------------------------------------------------------------- */
1570
1571static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1572 .bLength = USB_DT_ENDPOINT_SIZE,
1573 .bDescriptorType = USB_DT_ENDPOINT,
1574 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1575};
1576
1577static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1578 .enable = dwc3_gadget_ep0_enable,
1579 .disable = dwc3_gadget_ep0_disable,
1580 .alloc_request = dwc3_gadget_ep_alloc_request,
1581 .free_request = dwc3_gadget_ep_free_request,
1582 .queue = dwc3_gadget_ep0_queue,
1583 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301584 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001585 .set_wedge = dwc3_gadget_ep_set_wedge,
1586};
1587
1588static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1589 .enable = dwc3_gadget_ep_enable,
1590 .disable = dwc3_gadget_ep_disable,
1591 .alloc_request = dwc3_gadget_ep_alloc_request,
1592 .free_request = dwc3_gadget_ep_free_request,
1593 .queue = dwc3_gadget_ep_queue,
1594 .dequeue = dwc3_gadget_ep_dequeue,
1595 .set_halt = dwc3_gadget_ep_set_halt,
1596 .set_wedge = dwc3_gadget_ep_set_wedge,
1597};
1598
1599/* -------------------------------------------------------------------------- */
1600
1601static int dwc3_gadget_get_frame(struct usb_gadget *g)
1602{
1603 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001604
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001605 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001606}
1607
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001608static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001609{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001610 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001611
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001612 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001613 u32 reg;
1614
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 u8 link_state;
1616 u8 speed;
1617
Felipe Balbi72246da2011-08-19 18:10:58 +03001618 /*
1619 * According to the Databook Remote wakeup request should
1620 * be issued only when the device is in early suspend state.
1621 *
1622 * We can check that via USB Link State bits in DSTS register.
1623 */
1624 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1625
1626 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001627 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001628 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001629 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001630
1631 link_state = DWC3_DSTS_USBLNKST(reg);
1632
1633 switch (link_state) {
1634 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1635 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1636 break;
1637 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001638 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001639 }
1640
Felipe Balbi8598bde2012-01-02 18:55:57 +02001641 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1642 if (ret < 0) {
1643 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001644 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001645 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001646
Paul Zimmerman802fde92012-04-27 13:10:52 +03001647 /* Recent versions do this automatically */
1648 if (dwc->revision < DWC3_REVISION_194A) {
1649 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001650 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001651 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1652 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1653 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001654
Paul Zimmerman1d046792012-02-15 18:56:56 -08001655 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001656 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001657
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001658 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001659 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1660
1661 /* in HS, means ON */
1662 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1663 break;
1664 }
1665
1666 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1667 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001668 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001669 }
1670
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001671 return 0;
1672}
1673
1674static int dwc3_gadget_wakeup(struct usb_gadget *g)
1675{
1676 struct dwc3 *dwc = gadget_to_dwc(g);
1677 unsigned long flags;
1678 int ret;
1679
1680 spin_lock_irqsave(&dwc->lock, flags);
1681 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001682 spin_unlock_irqrestore(&dwc->lock, flags);
1683
1684 return ret;
1685}
1686
1687static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1688 int is_selfpowered)
1689{
1690 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001691 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001692
Paul Zimmerman249a4562012-02-24 17:32:16 -08001693 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001694 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001695 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001696
1697 return 0;
1698}
1699
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001700static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001701{
1702 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001703 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001704
Felipe Balbifc8bb912016-05-16 13:14:48 +03001705 if (pm_runtime_suspended(dwc->dev))
1706 return 0;
1707
Felipe Balbi72246da2011-08-19 18:10:58 +03001708 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001709 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001710 if (dwc->revision <= DWC3_REVISION_187A) {
1711 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1712 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1713 }
1714
1715 if (dwc->revision >= DWC3_REVISION_194A)
1716 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1717 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001718
1719 if (dwc->has_hibernation)
1720 reg |= DWC3_DCTL_KEEP_CONNECT;
1721
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001722 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001723 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001724 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001725
1726 if (dwc->has_hibernation && !suspend)
1727 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1728
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001729 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001730 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001731
1732 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1733
1734 do {
1735 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001736 reg &= DWC3_DSTS_DEVCTRLHLT;
1737 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001738
1739 if (!timeout)
1740 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741
Pratyush Anand6f17f742012-07-02 10:21:55 +05301742 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743}
1744
1745static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1746{
1747 struct dwc3 *dwc = gadget_to_dwc(g);
1748 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301749 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001750
1751 is_on = !!is_on;
1752
Baolin Wangbb014732016-10-14 17:11:33 +08001753 /*
1754 * Per databook, when we want to stop the gadget, if a control transfer
1755 * is still in process, complete it and get the core into setup phase.
1756 */
1757 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1758 reinit_completion(&dwc->ep0_in_setup);
1759
1760 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1761 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1762 if (ret == 0) {
1763 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1764 return -ETIMEDOUT;
1765 }
1766 }
1767
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001769 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001770 spin_unlock_irqrestore(&dwc->lock, flags);
1771
Pratyush Anand6f17f742012-07-02 10:21:55 +05301772 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001773}
1774
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001775static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1776{
1777 u32 reg;
1778
1779 /* Enable all but Start and End of Frame IRQs */
1780 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1781 DWC3_DEVTEN_EVNTOVERFLOWEN |
1782 DWC3_DEVTEN_CMDCMPLTEN |
1783 DWC3_DEVTEN_ERRTICERREN |
1784 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001785 DWC3_DEVTEN_CONNECTDONEEN |
1786 DWC3_DEVTEN_USBRSTEN |
1787 DWC3_DEVTEN_DISCONNEVTEN);
1788
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001789 if (dwc->revision < DWC3_REVISION_250A)
1790 reg |= DWC3_DEVTEN_ULSTCNGEN;
1791
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001792 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1793}
1794
1795static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1796{
1797 /* mask all interrupts */
1798 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1799}
1800
1801static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001802static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001803
Felipe Balbi4e994722016-05-13 14:09:59 +03001804/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001805 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1806 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001807 *
1808 * The following looks like complex but it's actually very simple. In order to
1809 * calculate the number of packets we can burst at once on OUT transfers, we're
1810 * gonna use RxFIFO size.
1811 *
1812 * To calculate RxFIFO size we need two numbers:
1813 * MDWIDTH = size, in bits, of the internal memory bus
1814 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1815 *
1816 * Given these two numbers, the formula is simple:
1817 *
1818 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1819 *
1820 * 24 bytes is for 3x SETUP packets
1821 * 16 bytes is a clock domain crossing tolerance
1822 *
1823 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1824 */
1825static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1826{
1827 u32 ram2_depth;
1828 u32 mdwidth;
1829 u32 nump;
1830 u32 reg;
1831
1832 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1833 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1834
1835 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1836 nump = min_t(u32, nump, 16);
1837
1838 /* update NumP */
1839 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1840 reg &= ~DWC3_DCFG_NUMP_MASK;
1841 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1842 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1843}
1844
Felipe Balbid7be2952016-05-04 15:49:37 +03001845static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001846{
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 int ret = 0;
1849 u32 reg;
1850
John Youncf40b862016-11-14 12:32:43 -08001851 /*
1852 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1853 * the core supports IMOD, disable it.
1854 */
1855 if (dwc->imod_interval) {
1856 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1857 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1858 } else if (dwc3_has_imod(dwc)) {
1859 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1860 }
1861
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001862 /*
1863 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1864 * field instead of letting dwc3 itself calculate that automatically.
1865 *
1866 * This way, we maximize the chances that we'll be able to get several
1867 * bursts of data without going through any sort of endpoint throttling.
1868 */
1869 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1870 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1871 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1872
Felipe Balbi4e994722016-05-13 14:09:59 +03001873 dwc3_gadget_setup_nump(dwc);
1874
Felipe Balbi72246da2011-08-19 18:10:58 +03001875 /* Start with SuperSpeed Default */
1876 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1877
1878 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001879 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001880 if (ret) {
1881 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001882 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001883 }
1884
1885 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001886 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001887 if (ret) {
1888 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001889 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 }
1891
1892 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001893 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001894 dwc3_ep0_out_start(dwc);
1895
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001896 dwc3_gadget_enable_irq(dwc);
1897
Felipe Balbid7be2952016-05-04 15:49:37 +03001898 return 0;
1899
1900err1:
1901 __dwc3_gadget_ep_disable(dwc->eps[0]);
1902
1903err0:
1904 return ret;
1905}
1906
1907static int dwc3_gadget_start(struct usb_gadget *g,
1908 struct usb_gadget_driver *driver)
1909{
1910 struct dwc3 *dwc = gadget_to_dwc(g);
1911 unsigned long flags;
1912 int ret = 0;
1913 int irq;
1914
Roger Quadros9522def2016-06-10 14:48:38 +03001915 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001916 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1917 IRQF_SHARED, "dwc3", dwc->ev_buf);
1918 if (ret) {
1919 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1920 irq, ret);
1921 goto err0;
1922 }
1923
1924 spin_lock_irqsave(&dwc->lock, flags);
1925 if (dwc->gadget_driver) {
1926 dev_err(dwc->dev, "%s is already bound to %s\n",
1927 dwc->gadget.name,
1928 dwc->gadget_driver->driver.name);
1929 ret = -EBUSY;
1930 goto err1;
1931 }
1932
1933 dwc->gadget_driver = driver;
1934
Felipe Balbifc8bb912016-05-16 13:14:48 +03001935 if (pm_runtime_active(dwc->dev))
1936 __dwc3_gadget_start(dwc);
1937
Felipe Balbi72246da2011-08-19 18:10:58 +03001938 spin_unlock_irqrestore(&dwc->lock, flags);
1939
1940 return 0;
1941
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001942err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001943 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001944 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001945
1946err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001947 return ret;
1948}
1949
Felipe Balbid7be2952016-05-04 15:49:37 +03001950static void __dwc3_gadget_stop(struct dwc3 *dwc)
1951{
1952 dwc3_gadget_disable_irq(dwc);
1953 __dwc3_gadget_ep_disable(dwc->eps[0]);
1954 __dwc3_gadget_ep_disable(dwc->eps[1]);
1955}
1956
Felipe Balbi22835b82014-10-17 12:05:12 -05001957static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001958{
1959 struct dwc3 *dwc = gadget_to_dwc(g);
1960 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001961 int epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03001962
1963 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001964
1965 if (pm_runtime_suspended(dwc->dev))
1966 goto out;
1967
Felipe Balbid7be2952016-05-04 15:49:37 +03001968 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001969
1970 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1971 struct dwc3_ep *dep = dwc->eps[epnum];
1972
1973 if (!dep)
1974 continue;
1975
1976 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1977 continue;
1978
1979 wait_event_lock_irq(dep->wait_end_transfer,
1980 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1981 dwc->lock);
1982 }
1983
1984out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 spin_unlock_irqrestore(&dwc->lock, flags);
1987
Felipe Balbi3f308d12016-05-16 14:17:06 +03001988 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001989
Felipe Balbi72246da2011-08-19 18:10:58 +03001990 return 0;
1991}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001992
Felipe Balbi7d8d0632017-06-06 16:05:23 +03001993static void dwc3_gadget_set_speed(struct usb_gadget *g,
1994 enum usb_device_speed speed)
1995{
1996 struct dwc3 *dwc = gadget_to_dwc(g);
1997 unsigned long flags;
1998 u32 reg;
1999
2000 spin_lock_irqsave(&dwc->lock, flags);
2001 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2002 reg &= ~(DWC3_DCFG_SPEED_MASK);
2003
2004 /*
2005 * WORKAROUND: DWC3 revision < 2.20a have an issue
2006 * which would cause metastability state on Run/Stop
2007 * bit if we try to force the IP to USB2-only mode.
2008 *
2009 * Because of that, we cannot configure the IP to any
2010 * speed other than the SuperSpeed
2011 *
2012 * Refers to:
2013 *
2014 * STAR#9000525659: Clock Domain Crossing on DCTL in
2015 * USB 2.0 Mode
2016 */
2017 if (dwc->revision < DWC3_REVISION_220A) {
2018 reg |= DWC3_DCFG_SUPERSPEED;
2019 } else {
2020 switch (speed) {
2021 case USB_SPEED_LOW:
2022 reg |= DWC3_DCFG_LOWSPEED;
2023 break;
2024 case USB_SPEED_FULL:
2025 reg |= DWC3_DCFG_FULLSPEED;
2026 break;
2027 case USB_SPEED_HIGH:
2028 reg |= DWC3_DCFG_HIGHSPEED;
2029 break;
2030 case USB_SPEED_SUPER:
2031 reg |= DWC3_DCFG_SUPERSPEED;
2032 break;
2033 case USB_SPEED_SUPER_PLUS:
2034 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2035 break;
2036 default:
2037 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2038
2039 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2040 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2041 else
2042 reg |= DWC3_DCFG_SUPERSPEED;
2043 }
2044 }
2045 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2046
2047 spin_unlock_irqrestore(&dwc->lock, flags);
2048}
2049
Felipe Balbi72246da2011-08-19 18:10:58 +03002050static const struct usb_gadget_ops dwc3_gadget_ops = {
2051 .get_frame = dwc3_gadget_get_frame,
2052 .wakeup = dwc3_gadget_wakeup,
2053 .set_selfpowered = dwc3_gadget_set_selfpowered,
2054 .pullup = dwc3_gadget_pullup,
2055 .udc_start = dwc3_gadget_start,
2056 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002057 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002058};
2059
2060/* -------------------------------------------------------------------------- */
2061
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002062static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
Felipe Balbi72246da2011-08-19 18:10:58 +03002063{
2064 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002065 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002066
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002067 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2068
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002069 for (epnum = 0; epnum < total; epnum++) {
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002070 bool direction = epnum & 1;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002071 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002072
Felipe Balbi72246da2011-08-19 18:10:58 +03002073 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002074 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002075 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002076
2077 dep->dwc = dwc;
2078 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002079 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002080 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002081 dwc->eps[epnum] = dep;
2082
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002083 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002084 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002085
Felipe Balbi72246da2011-08-19 18:10:58 +03002086 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002087
2088 if (!(dep->number > 1)) {
2089 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2090 dep->endpoint.comp_desc = NULL;
2091 }
2092
Felipe Balbi74674cb2016-04-13 16:44:39 +03002093 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002094
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002095 if (num == 0) {
Robert Baldygae117e742013-12-13 12:23:38 +01002096 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302097 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002098 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002099 if (!direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002100 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002101 } else if (direction) {
2102 int mdwidth;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002103 int kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002104 int size;
2105 int ret;
Felipe Balbi28781782017-01-23 18:01:59 +02002106
2107 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2108 /* MDWIDTH is represented in bits, we need it in bytes */
2109 mdwidth /= 8;
2110
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002111 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
Felipe Balbi28781782017-01-23 18:01:59 +02002112 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2113
2114 /* FIFO Depth is in MDWDITH bytes. Multiply */
2115 size *= mdwidth;
2116
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002117 kbytes = size / 1024;
2118 if (kbytes == 0)
2119 kbytes = 1;
Felipe Balbi28781782017-01-23 18:01:59 +02002120
2121 /*
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002122 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
Felipe Balbi28781782017-01-23 18:01:59 +02002123 * internal overhead. We don't really know how these are used,
2124 * but documentation say it exists.
2125 */
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002126 size -= mdwidth * (kbytes + 1);
2127 size /= kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002128
2129 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2130
2131 dep->endpoint.max_streams = 15;
2132 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2133 list_add_tail(&dep->endpoint.ep_list,
2134 &dwc->gadget.ep_list);
2135
2136 ret = dwc3_alloc_trb_pool(dep);
2137 if (ret)
2138 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002139 } else {
2140 int ret;
2141
Robert Baldygae117e742013-12-13 12:23:38 +01002142 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002143 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002144 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2145 list_add_tail(&dep->endpoint.ep_list,
2146 &dwc->gadget.ep_list);
2147
2148 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002149 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002152
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002153 if (num == 0) {
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002154 dep->endpoint.caps.type_control = true;
2155 } else {
2156 dep->endpoint.caps.type_iso = true;
2157 dep->endpoint.caps.type_bulk = true;
2158 dep->endpoint.caps.type_int = true;
2159 }
2160
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002161 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002162 dep->endpoint.caps.dir_out = !direction;
2163
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002164 INIT_LIST_HEAD(&dep->pending_list);
2165 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002166 }
2167
2168 return 0;
2169}
2170
2171static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2172{
2173 struct dwc3_ep *dep;
2174 u8 epnum;
2175
2176 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2177 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002178 if (!dep)
2179 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302180 /*
2181 * Physical endpoints 0 and 1 are special; they form the
2182 * bi-directional USB endpoint 0.
2183 *
2184 * For those two physical endpoints, we don't allocate a TRB
2185 * pool nor do we add them the endpoints list. Due to that, we
2186 * shouldn't do these two operations otherwise we would end up
2187 * with all sorts of bugs when removing dwc3.ko.
2188 */
2189 if (epnum != 0 && epnum != 1) {
2190 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302192 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002193
2194 kfree(dep);
2195 }
2196}
2197
Felipe Balbi72246da2011-08-19 18:10:58 +03002198/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002199
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302200static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2201 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002202 const struct dwc3_event_depevt *event, int status,
2203 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302204{
2205 unsigned int count;
2206 unsigned int s_pkt = 0;
2207 unsigned int trb_status;
2208
Felipe Balbidc55c672016-08-12 13:20:32 +03002209 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002210
2211 if (req->trb == trb)
2212 dep->queued_requests--;
2213
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002214 trace_dwc3_complete_trb(dep, trb);
2215
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002216 /*
2217 * If we're in the middle of series of chained TRBs and we
2218 * receive a short transfer along the way, DWC3 will skip
2219 * through all TRBs including the last TRB in the chain (the
2220 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2221 * bit and SW has to do it manually.
2222 *
2223 * We're going to do that here to avoid problems of HW trying
2224 * to use bogus TRBs for transfers.
2225 */
2226 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2227 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2228
Felipe Balbic6267a52017-01-05 14:58:46 +02002229 /*
2230 * If we're dealing with unaligned size OUT transfer, we will be left
2231 * with one TRB pending in the ring. We need to manually clear HWO bit
2232 * from that TRB.
2233 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002234 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002235 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2236 return 1;
2237 }
2238
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302239 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002240 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302241
Felipe Balbi35b27192017-03-08 13:56:37 +02002242 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2243 return 1;
2244
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302245 if (dep->direction) {
2246 if (count) {
2247 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2248 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302249 /*
2250 * If missed isoc occurred and there is
2251 * no request queued then issue END
2252 * TRANSFER, so that core generates
2253 * next xfernotready and we will issue
2254 * a fresh START TRANSFER.
2255 * If there are still queued request
2256 * then wait, do not issue either END
2257 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002258 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302259 * giveback.If any future queued request
2260 * is successfully transferred then we
2261 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002262 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302263 */
2264 dep->flags |= DWC3_EP_MISSED_ISOC;
2265 } else {
2266 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2267 dep->name);
2268 status = -ECONNRESET;
2269 }
2270 } else {
2271 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2272 }
2273 } else {
2274 if (count && (event->status & DEPEVT_STATUS_SHORT))
2275 s_pkt = 1;
2276 }
2277
Felipe Balbi7c705df2016-08-10 12:35:30 +03002278 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302279 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002280
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302281 if ((event->status & DEPEVT_STATUS_IOC) &&
2282 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2283 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002284
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302285 return 0;
2286}
2287
Felipe Balbi72246da2011-08-19 18:10:58 +03002288static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2289 const struct dwc3_event_depevt *event, int status)
2290{
Felipe Balbi31162af2016-08-11 14:38:37 +03002291 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002292 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002293 bool ioc = false;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002294 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002295
Felipe Balbi31162af2016-08-11 14:38:37 +03002296 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002297 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002298 int chain;
2299
Felipe Balbi1f512112016-08-12 13:17:27 +03002300 length = req->request.length;
2301 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002302 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002303 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002304 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002305 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002306 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002307
Felipe Balbi1f512112016-08-12 13:17:27 +03002308 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002309 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002310
Felipe Balbi7282c4e2016-10-25 13:50:46 +03002311 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2312 break;
2313
Felipe Balbi1f512112016-08-12 13:17:27 +03002314 req->sg = sg_next(s);
2315 req->num_pending_sgs--;
2316
Felipe Balbi31162af2016-08-11 14:38:37 +03002317 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2318 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002319 if (ret)
2320 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002321 }
2322 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002323 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002324 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002325 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002326 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002327
Felipe Balbid6e5a542017-04-07 16:34:38 +03002328 if (req->unaligned || req->zero) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002329 trb = &dep->trb_pool[dep->trb_dequeue];
2330 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2331 event, status, false);
2332 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002333 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002334 }
2335
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002336 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002337
Felipe Balbiff377ae2016-10-25 13:54:00 +03002338 if ((req->request.actual < length) && req->num_pending_sgs)
Felipe Balbi1f512112016-08-12 13:17:27 +03002339 return __dwc3_gadget_kick_transfer(dep, 0);
2340
Ville Syrjäläd115d702015-08-31 19:48:28 +03002341 dwc3_gadget_giveback(dep, req, status);
2342
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002343 if (ret) {
2344 if ((event->status & DEPEVT_STATUS_IOC) &&
2345 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2346 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002348 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002349 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002350
Felipe Balbi4cb42212016-05-18 12:37:21 +03002351 /*
2352 * Our endpoint might get disabled by another thread during
2353 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2354 * early on so DWC3_EP_BUSY flag gets cleared
2355 */
2356 if (!dep->endpoint.desc)
2357 return 1;
2358
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302359 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002360 list_empty(&dep->started_list)) {
2361 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302362 /*
2363 * If there is no entry in request list then do
2364 * not issue END TRANSFER now. Just set PENDING
2365 * flag, so that END TRANSFER is issued when an
2366 * entry is added into request list.
2367 */
2368 dep->flags = DWC3_EP_PENDING_REQUEST;
2369 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002370 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302371 dep->flags = DWC3_EP_ENABLED;
2372 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302373 return 1;
2374 }
2375
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002376 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2377 return 0;
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 return 1;
2380}
2381
2382static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002383 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002384{
2385 unsigned status = 0;
2386 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002387 u32 is_xfer_complete;
2388
2389 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002390
2391 if (event->status & DEPEVT_STATUS_BUSERR)
2392 status = -ECONNRESET;
2393
Paul Zimmerman1d046792012-02-15 18:56:56 -08002394 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002395 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002396 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002397 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002398
2399 /*
2400 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2401 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2402 */
2403 if (dwc->revision < DWC3_REVISION_183A) {
2404 u32 reg;
2405 int i;
2406
2407 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002408 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002409
2410 if (!(dep->flags & DWC3_EP_ENABLED))
2411 continue;
2412
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002413 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002414 return;
2415 }
2416
2417 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2418 reg |= dwc->u1u2;
2419 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2420
2421 dwc->u1u2 = 0;
2422 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002423
Felipe Balbi4cb42212016-05-18 12:37:21 +03002424 /*
2425 * Our endpoint might get disabled by another thread during
2426 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2427 * early on so DWC3_EP_BUSY flag gets cleared
2428 */
2429 if (!dep->endpoint.desc)
2430 return;
2431
Felipe Balbie6e709b2015-09-28 15:16:56 -05002432 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002433 int ret;
2434
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002435 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002436 if (!ret || ret == -EBUSY)
2437 return;
2438 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002439}
2440
Felipe Balbi72246da2011-08-19 18:10:58 +03002441static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2442 const struct dwc3_event_depevt *event)
2443{
2444 struct dwc3_ep *dep;
2445 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002446 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002447
2448 dep = dwc->eps[epnum];
2449
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002450 if (!(dep->flags & DWC3_EP_ENABLED)) {
2451 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2452 return;
2453
2454 /* Handle only EPCMDCMPLT when EP disabled */
2455 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2456 return;
2457 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002458
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 if (epnum == 0 || epnum == 1) {
2460 dwc3_ep0_interrupt(dwc, event);
2461 return;
2462 }
2463
2464 switch (event->endpoint_event) {
2465 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002466 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002467
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002468 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002469 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002470 return;
2471 }
2472
Jingoo Han029d97f2014-07-04 15:00:51 +09002473 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002474 break;
2475 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002476 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002477 break;
2478 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002479 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 dwc3_gadget_start_isoc(dwc, dep, event);
2481 } else {
2482 int ret;
2483
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002484 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002485 if (!ret || ret == -EBUSY)
2486 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 }
2488
2489 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002490 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002491 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002492 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2493 dep->name);
2494 return;
2495 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002496 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002498 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2499
2500 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2501 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2502 wake_up(&dep->wait_end_transfer);
2503 }
2504 break;
2505 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002506 break;
2507 }
2508}
2509
2510static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2511{
2512 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2513 spin_unlock(&dwc->lock);
2514 dwc->gadget_driver->disconnect(&dwc->gadget);
2515 spin_lock(&dwc->lock);
2516 }
2517}
2518
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002519static void dwc3_suspend_gadget(struct dwc3 *dwc)
2520{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002521 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002522 spin_unlock(&dwc->lock);
2523 dwc->gadget_driver->suspend(&dwc->gadget);
2524 spin_lock(&dwc->lock);
2525 }
2526}
2527
2528static void dwc3_resume_gadget(struct dwc3 *dwc)
2529{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002530 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002531 spin_unlock(&dwc->lock);
2532 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002533 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002534 }
2535}
2536
2537static void dwc3_reset_gadget(struct dwc3 *dwc)
2538{
2539 if (!dwc->gadget_driver)
2540 return;
2541
2542 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2543 spin_unlock(&dwc->lock);
2544 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002545 spin_lock(&dwc->lock);
2546 }
2547}
2548
Paul Zimmermanb992e682012-04-27 14:17:35 +03002549static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002550{
2551 struct dwc3_ep *dep;
2552 struct dwc3_gadget_ep_cmd_params params;
2553 u32 cmd;
2554 int ret;
2555
2556 dep = dwc->eps[epnum];
2557
Baolin Wang76a638f2016-10-31 19:38:36 +08002558 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2559 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302560 return;
2561
Pratyush Anand57911502012-07-06 15:19:10 +05302562 /*
2563 * NOTICE: We are violating what the Databook says about the
2564 * EndTransfer command. Ideally we would _always_ wait for the
2565 * EndTransfer Command Completion IRQ, but that's causing too
2566 * much trouble synchronizing between us and gadget driver.
2567 *
2568 * We have discussed this with the IP Provider and it was
2569 * suggested to giveback all requests here, but give HW some
2570 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002571 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302572 *
2573 * Note also that a similar handling was tested by Synopsys
2574 * (thanks a lot Paul) and nothing bad has come out of it.
2575 * In short, what we're doing is:
2576 *
2577 * - Issue EndTransfer WITH CMDIOC bit set
2578 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002579 *
2580 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2581 * supports a mode to work around the above limitation. The
2582 * software can poll the CMDACT bit in the DEPCMD register
2583 * after issuing a EndTransfer command. This mode is enabled
2584 * by writing GUCTL2[14]. This polling is already done in the
2585 * dwc3_send_gadget_ep_cmd() function so if the mode is
2586 * enabled, the EndTransfer command will have completed upon
2587 * returning from this function and we don't need to delay for
2588 * 100us.
2589 *
2590 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302591 */
2592
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302593 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002594 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2595 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002596 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302597 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002598 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302599 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002600 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002601 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002602
Baolin Wang76a638f2016-10-31 19:38:36 +08002603 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2604 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002605 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002606 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002607}
2608
Felipe Balbi72246da2011-08-19 18:10:58 +03002609static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2610{
2611 u32 epnum;
2612
2613 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2614 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002615 int ret;
2616
2617 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002618 if (!dep)
2619 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002620
2621 if (!(dep->flags & DWC3_EP_STALL))
2622 continue;
2623
2624 dep->flags &= ~DWC3_EP_STALL;
2625
John Youn50c763f2016-05-31 17:49:56 -07002626 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002627 WARN_ON_ONCE(ret);
2628 }
2629}
2630
2631static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2632{
Felipe Balbic4430a22012-05-24 10:30:01 +03002633 int reg;
2634
Felipe Balbi72246da2011-08-19 18:10:58 +03002635 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2636 reg &= ~DWC3_DCTL_INITU1ENA;
2637 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2638
2639 reg &= ~DWC3_DCTL_INITU2ENA;
2640 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002641
Felipe Balbi72246da2011-08-19 18:10:58 +03002642 dwc3_disconnect_gadget(dwc);
2643
2644 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002645 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002646 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002647
2648 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002649}
2650
Felipe Balbi72246da2011-08-19 18:10:58 +03002651static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2652{
2653 u32 reg;
2654
Felipe Balbifc8bb912016-05-16 13:14:48 +03002655 dwc->connected = true;
2656
Felipe Balbidf62df52011-10-14 15:11:49 +03002657 /*
2658 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2659 * would cause a missing Disconnect Event if there's a
2660 * pending Setup Packet in the FIFO.
2661 *
2662 * There's no suggested workaround on the official Bug
2663 * report, which states that "unless the driver/application
2664 * is doing any special handling of a disconnect event,
2665 * there is no functional issue".
2666 *
2667 * Unfortunately, it turns out that we _do_ some special
2668 * handling of a disconnect event, namely complete all
2669 * pending transfers, notify gadget driver of the
2670 * disconnection, and so on.
2671 *
2672 * Our suggested workaround is to follow the Disconnect
2673 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002674 * flag. Such flag gets set whenever we have a SETUP_PENDING
2675 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002676 * same endpoint.
2677 *
2678 * Refers to:
2679 *
2680 * STAR#9000466709: RTL: Device : Disconnect event not
2681 * generated if setup packet pending in FIFO
2682 */
2683 if (dwc->revision < DWC3_REVISION_188A) {
2684 if (dwc->setup_packet_pending)
2685 dwc3_gadget_disconnect_interrupt(dwc);
2686 }
2687
Felipe Balbi8e744752014-11-06 14:27:53 +08002688 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002689
2690 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2691 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2692 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002693 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002694 dwc3_clear_stall_all_ep(dwc);
2695
2696 /* Reset device address to zero */
2697 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2698 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2699 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002700}
2701
Felipe Balbi72246da2011-08-19 18:10:58 +03002702static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2703{
Felipe Balbi72246da2011-08-19 18:10:58 +03002704 struct dwc3_ep *dep;
2705 int ret;
2706 u32 reg;
2707 u8 speed;
2708
Felipe Balbi72246da2011-08-19 18:10:58 +03002709 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2710 speed = reg & DWC3_DSTS_CONNECTSPD;
2711 dwc->speed = speed;
2712
John Youn5fb6fda2016-11-10 17:23:25 -08002713 /*
2714 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2715 * each time on Connect Done.
2716 *
2717 * Currently we always use the reset value. If any platform
2718 * wants to set this to a different value, we need to add a
2719 * setting and update GCTL.RAMCLKSEL here.
2720 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002721
2722 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002723 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002724 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2725 dwc->gadget.ep0->maxpacket = 512;
2726 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2727 break;
John Youn2da9ad72016-05-20 16:34:26 -07002728 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002729 /*
2730 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2731 * would cause a missing USB3 Reset event.
2732 *
2733 * In such situations, we should force a USB3 Reset
2734 * event by calling our dwc3_gadget_reset_interrupt()
2735 * routine.
2736 *
2737 * Refers to:
2738 *
2739 * STAR#9000483510: RTL: SS : USB3 reset event may
2740 * not be generated always when the link enters poll
2741 */
2742 if (dwc->revision < DWC3_REVISION_190A)
2743 dwc3_gadget_reset_interrupt(dwc);
2744
Felipe Balbi72246da2011-08-19 18:10:58 +03002745 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2746 dwc->gadget.ep0->maxpacket = 512;
2747 dwc->gadget.speed = USB_SPEED_SUPER;
2748 break;
John Youn2da9ad72016-05-20 16:34:26 -07002749 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002750 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2751 dwc->gadget.ep0->maxpacket = 64;
2752 dwc->gadget.speed = USB_SPEED_HIGH;
2753 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002754 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002755 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2756 dwc->gadget.ep0->maxpacket = 64;
2757 dwc->gadget.speed = USB_SPEED_FULL;
2758 break;
John Youn2da9ad72016-05-20 16:34:26 -07002759 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2761 dwc->gadget.ep0->maxpacket = 8;
2762 dwc->gadget.speed = USB_SPEED_LOW;
2763 break;
2764 }
2765
Pratyush Anand2b758352013-01-14 15:59:31 +05302766 /* Enable USB2 LPM Capability */
2767
John Younee5cd412016-02-05 17:08:45 -08002768 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002769 (speed != DWC3_DSTS_SUPERSPEED) &&
2770 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302771 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2772 reg |= DWC3_DCFG_LPM_CAP;
2773 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2774
2775 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2776 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2777
Huang Rui460d0982014-10-31 11:11:18 +08002778 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302779
Huang Rui80caf7d2014-10-28 19:54:26 +08002780 /*
2781 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2782 * DCFG.LPMCap is set, core responses with an ACK and the
2783 * BESL value in the LPM token is less than or equal to LPM
2784 * NYET threshold.
2785 */
2786 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2787 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002788 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002789
2790 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2791 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2792
Pratyush Anand2b758352013-01-14 15:59:31 +05302793 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002794 } else {
2795 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2796 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2797 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302798 }
2799
Felipe Balbi72246da2011-08-19 18:10:58 +03002800 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002801 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002802 if (ret) {
2803 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2804 return;
2805 }
2806
2807 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002808 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002809 if (ret) {
2810 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2811 return;
2812 }
2813
2814 /*
2815 * Configure PHY via GUSB3PIPECTLn if required.
2816 *
2817 * Update GTXFIFOSIZn
2818 *
2819 * In both cases reset values should be sufficient.
2820 */
2821}
2822
2823static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2824{
Felipe Balbi72246da2011-08-19 18:10:58 +03002825 /*
2826 * TODO take core out of low power mode when that's
2827 * implemented.
2828 */
2829
Jiebing Liad14d4e2014-12-11 13:26:29 +08002830 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2831 spin_unlock(&dwc->lock);
2832 dwc->gadget_driver->resume(&dwc->gadget);
2833 spin_lock(&dwc->lock);
2834 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002835}
2836
2837static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2838 unsigned int evtinfo)
2839{
Felipe Balbifae2b902011-10-14 13:00:30 +03002840 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002841 unsigned int pwropt;
2842
2843 /*
2844 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2845 * Hibernation mode enabled which would show up when device detects
2846 * host-initiated U3 exit.
2847 *
2848 * In that case, device will generate a Link State Change Interrupt
2849 * from U3 to RESUME which is only necessary if Hibernation is
2850 * configured in.
2851 *
2852 * There are no functional changes due to such spurious event and we
2853 * just need to ignore it.
2854 *
2855 * Refers to:
2856 *
2857 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2858 * operational mode
2859 */
2860 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2861 if ((dwc->revision < DWC3_REVISION_250A) &&
2862 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2863 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2864 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002865 return;
2866 }
2867 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002868
2869 /*
2870 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2871 * on the link partner, the USB session might do multiple entry/exit
2872 * of low power states before a transfer takes place.
2873 *
2874 * Due to this problem, we might experience lower throughput. The
2875 * suggested workaround is to disable DCTL[12:9] bits if we're
2876 * transitioning from U1/U2 to U0 and enable those bits again
2877 * after a transfer completes and there are no pending transfers
2878 * on any of the enabled endpoints.
2879 *
2880 * This is the first half of that workaround.
2881 *
2882 * Refers to:
2883 *
2884 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2885 * core send LGO_Ux entering U0
2886 */
2887 if (dwc->revision < DWC3_REVISION_183A) {
2888 if (next == DWC3_LINK_STATE_U0) {
2889 u32 u1u2;
2890 u32 reg;
2891
2892 switch (dwc->link_state) {
2893 case DWC3_LINK_STATE_U1:
2894 case DWC3_LINK_STATE_U2:
2895 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2896 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2897 | DWC3_DCTL_ACCEPTU2ENA
2898 | DWC3_DCTL_INITU1ENA
2899 | DWC3_DCTL_ACCEPTU1ENA);
2900
2901 if (!dwc->u1u2)
2902 dwc->u1u2 = reg & u1u2;
2903
2904 reg &= ~u1u2;
2905
2906 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2907 break;
2908 default:
2909 /* do nothing */
2910 break;
2911 }
2912 }
2913 }
2914
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002915 switch (next) {
2916 case DWC3_LINK_STATE_U1:
2917 if (dwc->speed == USB_SPEED_SUPER)
2918 dwc3_suspend_gadget(dwc);
2919 break;
2920 case DWC3_LINK_STATE_U2:
2921 case DWC3_LINK_STATE_U3:
2922 dwc3_suspend_gadget(dwc);
2923 break;
2924 case DWC3_LINK_STATE_RESUME:
2925 dwc3_resume_gadget(dwc);
2926 break;
2927 default:
2928 /* do nothing */
2929 break;
2930 }
2931
Felipe Balbie57ebc12014-04-22 13:20:12 -05002932 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002933}
2934
Baolin Wang72704f82016-05-16 16:43:53 +08002935static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2936 unsigned int evtinfo)
2937{
2938 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2939
2940 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2941 dwc3_suspend_gadget(dwc);
2942
2943 dwc->link_state = next;
2944}
2945
Felipe Balbie1dadd32014-02-25 14:47:54 -06002946static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2947 unsigned int evtinfo)
2948{
2949 unsigned int is_ss = evtinfo & BIT(4);
2950
Felipe Balbibfad65e2017-04-19 14:59:27 +03002951 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002952 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2953 * have a known issue which can cause USB CV TD.9.23 to fail
2954 * randomly.
2955 *
2956 * Because of this issue, core could generate bogus hibernation
2957 * events which SW needs to ignore.
2958 *
2959 * Refers to:
2960 *
2961 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2962 * Device Fallback from SuperSpeed
2963 */
2964 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2965 return;
2966
2967 /* enter hibernation here */
2968}
2969
Felipe Balbi72246da2011-08-19 18:10:58 +03002970static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2971 const struct dwc3_event_devt *event)
2972{
2973 switch (event->type) {
2974 case DWC3_DEVICE_EVENT_DISCONNECT:
2975 dwc3_gadget_disconnect_interrupt(dwc);
2976 break;
2977 case DWC3_DEVICE_EVENT_RESET:
2978 dwc3_gadget_reset_interrupt(dwc);
2979 break;
2980 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2981 dwc3_gadget_conndone_interrupt(dwc);
2982 break;
2983 case DWC3_DEVICE_EVENT_WAKEUP:
2984 dwc3_gadget_wakeup_interrupt(dwc);
2985 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002986 case DWC3_DEVICE_EVENT_HIBER_REQ:
2987 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2988 "unexpected hibernation event\n"))
2989 break;
2990
2991 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2992 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002993 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2994 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2995 break;
2996 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002997 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002998 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002999 /*
3000 * Ignore suspend event until the gadget enters into
3001 * USB_STATE_CONFIGURED state.
3002 */
3003 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3004 dwc3_gadget_suspend_interrupt(dwc,
3005 event->event_info);
3006 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003007 break;
3008 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003009 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003010 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003011 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003012 break;
3013 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003014 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003015 }
3016}
3017
3018static void dwc3_process_event_entry(struct dwc3 *dwc,
3019 const union dwc3_event *event)
3020{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003021 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003022
Felipe Balbidfc5e802017-04-26 13:44:51 +03003023 if (!event->type.is_devspec)
3024 dwc3_endpoint_interrupt(dwc, &event->depevt);
3025 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003026 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003027 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003028 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003029}
3030
Felipe Balbidea520a2016-03-30 09:39:34 +03003031static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003032{
Felipe Balbidea520a2016-03-30 09:39:34 +03003033 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003034 irqreturn_t ret = IRQ_NONE;
3035 int left;
3036 u32 reg;
3037
Felipe Balbif42f2442013-06-12 21:25:08 +03003038 left = evt->count;
3039
3040 if (!(evt->flags & DWC3_EVENT_PENDING))
3041 return IRQ_NONE;
3042
3043 while (left > 0) {
3044 union dwc3_event event;
3045
John Younebbb2d52016-11-15 13:07:02 +02003046 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003047
3048 dwc3_process_event_entry(dwc, &event);
3049
3050 /*
3051 * FIXME we wrap around correctly to the next entry as
3052 * almost all entries are 4 bytes in size. There is one
3053 * entry which has 12 bytes which is a regular entry
3054 * followed by 8 bytes data. ATM I don't know how
3055 * things are organized if we get next to the a
3056 * boundary so I worry about that once we try to handle
3057 * that.
3058 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003059 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003060 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003061 }
3062
3063 evt->count = 0;
3064 evt->flags &= ~DWC3_EVENT_PENDING;
3065 ret = IRQ_HANDLED;
3066
3067 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003068 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003069 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003070 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003071
John Youncf40b862016-11-14 12:32:43 -08003072 if (dwc->imod_interval) {
3073 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3074 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3075 }
3076
Felipe Balbif42f2442013-06-12 21:25:08 +03003077 return ret;
3078}
3079
Felipe Balbidea520a2016-03-30 09:39:34 +03003080static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003081{
Felipe Balbidea520a2016-03-30 09:39:34 +03003082 struct dwc3_event_buffer *evt = _evt;
3083 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003084 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003085 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003086
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003087 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003088 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003089 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003090
3091 return ret;
3092}
3093
Felipe Balbidea520a2016-03-30 09:39:34 +03003094static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003095{
Felipe Balbidea520a2016-03-30 09:39:34 +03003096 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003097 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003098 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003099 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003100
Felipe Balbifc8bb912016-05-16 13:14:48 +03003101 if (pm_runtime_suspended(dwc->dev)) {
3102 pm_runtime_get(dwc->dev);
3103 disable_irq_nosync(dwc->irq_gadget);
3104 dwc->pending_events = true;
3105 return IRQ_HANDLED;
3106 }
3107
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003108 /*
3109 * With PCIe legacy interrupt, test shows that top-half irq handler can
3110 * be called again after HW interrupt deassertion. Check if bottom-half
3111 * irq event handler completes before caching new event to prevent
3112 * losing events.
3113 */
3114 if (evt->flags & DWC3_EVENT_PENDING)
3115 return IRQ_HANDLED;
3116
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003117 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003118 count &= DWC3_GEVNTCOUNT_MASK;
3119 if (!count)
3120 return IRQ_NONE;
3121
Felipe Balbib15a7622011-06-30 16:57:15 +03003122 evt->count = count;
3123 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003124
Felipe Balbie8adfc32013-06-12 21:11:14 +03003125 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003126 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003127 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003128 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003129
John Younebbb2d52016-11-15 13:07:02 +02003130 amount = min(count, evt->length - evt->lpos);
3131 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3132
3133 if (amount < count)
3134 memcpy(evt->cache, evt->buf, count - amount);
3135
John Youn65aca322016-11-15 13:08:59 +02003136 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3137
Felipe Balbib15a7622011-06-30 16:57:15 +03003138 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003139}
3140
Felipe Balbidea520a2016-03-30 09:39:34 +03003141static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003142{
Felipe Balbidea520a2016-03-30 09:39:34 +03003143 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003144
Felipe Balbidea520a2016-03-30 09:39:34 +03003145 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003146}
3147
Felipe Balbi6db38122016-10-03 11:27:01 +03003148static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3149{
3150 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3151 int irq;
3152
3153 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3154 if (irq > 0)
3155 goto out;
3156
3157 if (irq == -EPROBE_DEFER)
3158 goto out;
3159
3160 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3161 if (irq > 0)
3162 goto out;
3163
3164 if (irq == -EPROBE_DEFER)
3165 goto out;
3166
3167 irq = platform_get_irq(dwc3_pdev, 0);
3168 if (irq > 0)
3169 goto out;
3170
3171 if (irq != -EPROBE_DEFER)
3172 dev_err(dwc->dev, "missing peripheral IRQ\n");
3173
3174 if (!irq)
3175 irq = -EINVAL;
3176
3177out:
3178 return irq;
3179}
3180
Felipe Balbi72246da2011-08-19 18:10:58 +03003181/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003182 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003183 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003184 *
3185 * Returns 0 on success otherwise negative errno.
3186 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003187int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003188{
Felipe Balbi6db38122016-10-03 11:27:01 +03003189 int ret;
3190 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003191
Felipe Balbi6db38122016-10-03 11:27:01 +03003192 irq = dwc3_gadget_get_irq(dwc);
3193 if (irq < 0) {
3194 ret = irq;
3195 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003196 }
3197
3198 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003199
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303200 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3201 sizeof(*dwc->ep0_trb) * 2,
3202 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003203 if (!dwc->ep0_trb) {
3204 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3205 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003206 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 }
3208
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003209 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003210 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003211 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003212 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003213 }
3214
Felipe Balbi905dc042017-01-05 14:46:52 +02003215 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3216 &dwc->bounce_addr, GFP_KERNEL);
3217 if (!dwc->bounce) {
3218 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003219 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003220 }
3221
Baolin Wangbb014732016-10-14 17:11:33 +08003222 init_completion(&dwc->ep0_in_setup);
3223
Felipe Balbi72246da2011-08-19 18:10:58 +03003224 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003225 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003226 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003227 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003228 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003229
3230 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003231 * FIXME We might be setting max_speed to <SUPER, however versions
3232 * <2.20a of dwc3 have an issue with metastability (documented
3233 * elsewhere in this driver) which tells us we can't set max speed to
3234 * anything lower than SUPER.
3235 *
3236 * Because gadget.max_speed is only used by composite.c and function
3237 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3238 * to happen so we avoid sending SuperSpeed Capability descriptor
3239 * together with our BOS descriptor as that could confuse host into
3240 * thinking we can handle super speed.
3241 *
3242 * Note that, in fact, we won't even support GetBOS requests when speed
3243 * is less than super speed because we don't have means, yet, to tell
3244 * composite.c that we are USB 2.0 + LPM ECN.
3245 */
3246 if (dwc->revision < DWC3_REVISION_220A)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003247 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003248 dwc->revision);
3249
3250 dwc->gadget.max_speed = dwc->maximum_speed;
3251
3252 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003253 * REVISIT: Here we should clear all pending IRQs to be
3254 * sure we're starting from a well known location.
3255 */
3256
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003257 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003258 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003259 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003260
Felipe Balbi72246da2011-08-19 18:10:58 +03003261 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3262 if (ret) {
3263 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003264 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003265 }
3266
3267 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003268
3269err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003270 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003271
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003272err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003273 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3274 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003275
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003276err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003277 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003278
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003279err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303280 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003281 dwc->ep0_trb, dwc->ep0_trb_addr);
3282
Felipe Balbi72246da2011-08-19 18:10:58 +03003283err0:
3284 return ret;
3285}
3286
Felipe Balbi7415f172012-04-30 14:56:33 +03003287/* -------------------------------------------------------------------------- */
3288
Felipe Balbi72246da2011-08-19 18:10:58 +03003289void dwc3_gadget_exit(struct dwc3 *dwc)
3290{
Felipe Balbi72246da2011-08-19 18:10:58 +03003291 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003292 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003293 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003294 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003295 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303296 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003297 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003298}
Felipe Balbi7415f172012-04-30 14:56:33 +03003299
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003300int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003301{
Roger Quadros9772b472016-04-12 11:33:29 +03003302 if (!dwc->gadget_driver)
3303 return 0;
3304
Roger Quadros1551e352017-02-15 14:16:26 +02003305 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003306 dwc3_disconnect_gadget(dwc);
3307 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003308
3309 return 0;
3310}
3311
3312int dwc3_gadget_resume(struct dwc3 *dwc)
3313{
Felipe Balbi7415f172012-04-30 14:56:33 +03003314 int ret;
3315
Roger Quadros9772b472016-04-12 11:33:29 +03003316 if (!dwc->gadget_driver)
3317 return 0;
3318
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003319 ret = __dwc3_gadget_start(dwc);
3320 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003321 goto err0;
3322
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003323 ret = dwc3_gadget_run_stop(dwc, true, false);
3324 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003325 goto err1;
3326
Felipe Balbi7415f172012-04-30 14:56:33 +03003327 return 0;
3328
3329err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003330 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003331
3332err0:
3333 return ret;
3334}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003335
3336void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3337{
3338 if (dwc->pending_events) {
3339 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3340 dwc->pending_events = false;
3341 enable_irq(dwc->irq_gadget);
3342 }
3343}