blob: 87b171dd6da45723dc0e877044bcbadad68389e4 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
Jack Pham62523502018-01-15 16:37:05 -080023#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030024#include <linux/usb/gadget.h>
25
Felipe Balbi80977dc2014-08-19 16:37:22 -050026#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030027#include "core.h"
28#include "gadget.h"
29#include "io.h"
30
Felipe Balbif62afb42018-04-11 10:34:34 +030031#define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \
32 & ~((d)->interval - 1))
33
Jack Pham62523502018-01-15 16:37:05 -080034static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup);
35static int dwc3_gadget_wakeup_int(struct dwc3 *dwc);
36static int __dwc3_gadget_start(struct dwc3 *dwc);
37static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
38
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020039/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030040 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020041 * @dwc: pointer to our context structure
42 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
43 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030044 * Caller should take care of locking. This function will return 0 on
45 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020046 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030072 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030073 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030088 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020089 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi8598bde2012-01-02 18:55:57 +0200142 return -ETIMEDOUT;
143}
144
John Youndca01192016-05-19 17:26:05 -0700145/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300146 * dwc3_ep_inc_trb - increment a trb index.
147 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700148 *
149 * The index should never point to the link TRB. After incrementing,
150 * if it is point to the link TRB, wrap around to the beginning. The
151 * link TRB is always at the last TRB entry.
152 */
153static void dwc3_ep_inc_trb(u8 *index)
154{
155 (*index)++;
156 if (*index == (DWC3_TRB_NUM - 1))
157 *index = 0;
158}
159
Felipe Balbibfad65e2017-04-19 14:59:27 +0300160/**
161 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
Jack Pham62523502018-01-15 16:37:05 -0800164void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200165{
John Youndca01192016-05-19 17:26:05 -0700166 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300167}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200168
Felipe Balbibfad65e2017-04-19 14:59:27 +0300169/**
170 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
171 * @dep: The endpoint whose enqueue pointer we're incrementing
172 */
Jack Pham62523502018-01-15 16:37:05 -0800173void dwc3_ep_inc_deq(struct dwc3_ep *dep)
Felipe Balbief966b92016-04-05 13:09:51 +0300174{
John Youndca01192016-05-19 17:26:05 -0700175 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200176}
177
Mayank Rana4c99f662017-04-25 13:48:46 -0700178/*
179 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
180 * @dwc: pointer to our context structure
181 *
182 * This function will a best effort FIFO allocation in order
183 * to improve FIFO usage and throughput, while still allowing
184 * us to enable as many endpoints as possible.
185 *
186 * Keep in mind that this operation will be highly dependent
187 * on the configured size for RAM1 - which contains TxFifo -,
188 * the amount of endpoints enabled on coreConsultant tool, and
189 * the width of the Master Bus.
190 *
191 * In the ideal world, we would always be able to satisfy the
192 * following equation:
193 *
194 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
195 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
196 *
197 * Unfortunately, due to many variables that's not always the case.
198 */
199int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc, struct dwc3_ep *dep)
200{
201 int fifo_size, mdwidth, max_packet = 1024;
Mayank Rana1fbcd4d2018-03-12 14:34:21 -0700202 int tmp, mult = 1, size;
Mayank Rana4c99f662017-04-25 13:48:46 -0700203
204 if (!dwc->needs_fifo_resize || !dwc->tx_fifo_size)
205 return 0;
206
207 /* resize IN endpoints excepts ep0 */
208 if (!usb_endpoint_dir_in(dep->endpoint.desc) ||
209 dep->endpoint.ep_num == 0)
210 return 0;
211
212 /* Don't resize already resized IN endpoint */
213 if (dep->fifo_depth) {
214 dev_dbg(dwc->dev, "%s fifo_depth:%d is already set\n",
215 dep->endpoint.name, dep->fifo_depth);
216 return 0;
217 }
218
219 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
220 /* MDWIDTH is represented in bits, we need it in bytes */
221 mdwidth >>= 3;
222
Mayank Rana4c99f662017-04-25 13:48:46 -0700223 if (((dep->endpoint.maxburst > 1) &&
224 usb_endpoint_xfer_bulk(dep->endpoint.desc))
225 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
226 mult = 3;
227
Mayank Ranab259e002018-06-08 16:11:51 -0700228 if ((dep->endpoint.maxburst > 2) &&
229 dep->endpoint.ep_type == EP_TYPE_GSI)
230 mult = 6;
231
Mayank Rana4c99f662017-04-25 13:48:46 -0700232 tmp = ((max_packet + mdwidth) * mult) + mdwidth;
233 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
234 dep->fifo_depth = fifo_size;
Mayank Rana1fbcd4d2018-03-12 14:34:21 -0700235
236 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
237 if (dwc3_is_usb31(dwc))
238 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
239 else
240 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
241
242 fifo_size |= (size + (dwc->last_fifo_depth << 16));
243 if (dwc3_is_usb31(dwc))
244 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEF(fifo_size);
245 else
246 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEF(fifo_size);
Mayank Rana4c99f662017-04-25 13:48:46 -0700247
248 dev_dbg(dwc->dev, "%s ep_num:%d last_fifo_depth:%04x fifo_depth:%d\n",
249 dep->endpoint.name, dep->endpoint.ep_num, dwc->last_fifo_depth,
250 dep->fifo_depth);
251
252 dbg_event(0xFF, "resize_fifo", dep->number);
253 dbg_event(0xFF, "fifo_depth", dep->fifo_depth);
254 /* Check fifo size allocation doesn't exceed available RAM size. */
255 if ((dwc->last_fifo_depth * mdwidth) >= dwc->tx_fifo_size) {
256 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
257 (dwc->last_fifo_depth * mdwidth), dwc->tx_fifo_size,
258 dep->endpoint.name, fifo_size);
Mayank Rana1fbcd4d2018-03-12 14:34:21 -0700259 if (dwc3_is_usb31(dwc))
260 fifo_size = DWC31_GTXFIFOSIZ_TXFDEF(fifo_size);
261 else
262 fifo_size = DWC3_GTXFIFOSIZ_TXFDEF(fifo_size);
263 dwc->last_fifo_depth -= fifo_size;
Mayank Rana4c99f662017-04-25 13:48:46 -0700264 dep->fifo_depth = 0;
265 WARN_ON(1);
266 return -ENOMEM;
267 }
268
Vamsi Krishna Samavedam4ee3d5a2018-05-08 23:39:51 -0700269 if ((dwc->revision == DWC3_USB31_REVISION_170A) &&
270 (dwc->versiontype == DWC3_USB31_VER_TYPE_EA06) &&
271 usb_endpoint_xfer_isoc(dep->endpoint.desc))
272 fifo_size |= DWC31_GTXFIFOSIZ_TXFRAMNUM;
273
Mayank Rana4c99f662017-04-25 13:48:46 -0700274 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->endpoint.ep_num),
275 fifo_size);
276 return 0;
277}
278
Wei Yongjun69102512018-03-29 02:20:10 +0000279static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300280 struct dwc3_request *req, int status)
281{
282 struct dwc3 *dwc = dep->dwc;
283
284 req->started = false;
285 list_del(&req->list);
286 req->remaining = 0;
Jack Phamf15271c2018-12-20 00:57:51 -0800287 req->unaligned = false;
288 req->zero = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300289
290 if (req->request.status == -EINPROGRESS)
291 req->request.status = status;
292
293 if (req->trb)
294 usb_gadget_unmap_request_by_dev(dwc->sysdev,
295 &req->request, req->direction);
296
297 req->trb = NULL;
298 trace_dwc3_gadget_giveback(req);
Felipe Balbic91815b2018-03-26 13:14:47 +0300299}
300
Felipe Balbibfad65e2017-04-19 14:59:27 +0300301/**
302 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
303 * @dep: The endpoint to whom the request belongs to
304 * @req: The request we're giving back
305 * @status: completion code for the request
306 *
307 * Must be called with controller's lock held and interrupts disabled. This
308 * function will unmap @req and call its ->complete() callback to notify upper
309 * layers that it has completed.
310 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300311void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
312 int status)
313{
314 struct dwc3 *dwc = dep->dwc;
315
Felipe Balbic91815b2018-03-26 13:14:47 +0300316 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300317
318 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200319 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300320 spin_lock(&dwc->lock);
321}
322
Felipe Balbibfad65e2017-04-19 14:59:27 +0300323/**
324 * dwc3_send_gadget_generic_command - issue a generic command for the controller
325 * @dwc: pointer to the controller context
326 * @cmd: the command to be issued
327 * @param: command parameter
328 *
329 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
330 * and wait for its completion.
331 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500332int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300333{
334 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300335 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300336 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300337 u32 reg;
338
339 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
340 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
341
342 do {
343 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
344 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300345 status = DWC3_DGCMD_STATUS(reg);
346 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300347 ret = -EINVAL;
348 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300349 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100350 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300351
352 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300353 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300354 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300355 }
356
Felipe Balbi71f7e702016-05-23 14:16:19 +0300357 trace_dwc3_gadget_generic_cmd(cmd, param, status);
358
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300359 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300360}
361
Felipe Balbic36d8e92016-04-04 12:46:33 +0300362static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
363
Felipe Balbibfad65e2017-04-19 14:59:27 +0300364/**
365 * dwc3_send_gadget_ep_cmd - issue an endpoint command
366 * @dep: the endpoint to which the command is going to be issued
367 * @cmd: the command to be issued
368 * @params: parameters to the command
369 *
370 * Caller should handle locking. This function will issue @cmd with given
371 * @params to @dep and wait for its completion.
372 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300373int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
374 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300375{
Felipe Balbi8897a762016-09-22 10:56:08 +0300376 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300377 struct dwc3 *dwc = dep->dwc;
Jack Pham62523502018-01-15 16:37:05 -0800378 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379 u32 reg;
380
Felipe Balbi0933df12016-05-23 14:02:33 +0300381 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300382 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300383 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300384
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300385 /*
386 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
387 * we're issuing an endpoint command, we must check if
388 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
389 *
390 * We will also set SUSPHY bit to what it was before returning as stated
391 * by the same section on Synopsys databook.
392 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300393 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
394 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
395 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
396 susphy = true;
397 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
398 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
399 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300400 }
401
Felipe Balbi59999142016-09-22 12:25:28 +0300402 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300403 int needs_wakeup;
404
405 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
406 dwc->link_state == DWC3_LINK_STATE_U2 ||
407 dwc->link_state == DWC3_LINK_STATE_U3);
408
409 if (unlikely(needs_wakeup)) {
410 ret = __dwc3_gadget_wakeup(dwc);
411 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
412 ret);
413 }
414 }
415
Felipe Balbi2eb88012016-04-12 16:53:39 +0300416 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
417 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
418 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419
Felipe Balbi8897a762016-09-22 10:56:08 +0300420 /*
421 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
422 * not relying on XferNotReady, we can make use of a special "No
423 * Response Update Transfer" command where we should clear both CmdAct
424 * and CmdIOC bits.
425 *
426 * With this, we don't need to wait for command completion and can
427 * straight away issue further commands to the endpoint.
428 *
429 * NOTICE: We're making an assumption that control endpoints will never
430 * make use of Update Transfer command. This is a safe assumption
431 * because we can never have more than one request at a time with
432 * Control Endpoints. If anybody changes that assumption, this chunk
433 * needs to be updated accordingly.
434 */
435 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
436 !usb_endpoint_xfer_isoc(desc))
437 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
438 else
439 cmd |= DWC3_DEPCMD_CMDACT;
440
441 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300443 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300444 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300445 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000446
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000447 switch (cmd_status) {
448 case 0:
449 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300450 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000451 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000452 ret = -EINVAL;
453 break;
454 case DEPEVT_TRANSFER_BUS_EXPIRY:
455 /*
456 * SW issues START TRANSFER command to
457 * isochronous ep with future frame interval. If
458 * future interval time has already passed when
459 * core receives the command, it will respond
460 * with an error status of 'Bus Expiry'.
461 *
462 * Instead of always returning -EINVAL, let's
463 * give a hint to the gadget driver that this is
464 * the case by returning -EAGAIN.
465 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000466 ret = -EAGAIN;
467 break;
468 default:
469 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
470 }
471
Felipe Balbic0ca3242016-04-04 09:11:51 +0300472 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300474 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
Felipe Balbif6bb2252016-05-23 13:53:34 +0300476 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300477 ret = -ETIMEDOUT;
Jack Pham62523502018-01-15 16:37:05 -0800478 dev_err(dwc->dev, "%s command timeout for %s\n",
479 dwc3_gadget_ep_cmd_string(cmd), dep->name);
480 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
481 dwc->ep_cmd_timeout_cnt++;
482 dwc3_notify_event(dwc,
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -0700483 DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Jack Pham62523502018-01-15 16:37:05 -0800484 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300485 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300486 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300487
Felipe Balbi0933df12016-05-23 14:02:33 +0300488 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
489
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300490 if (ret == 0) {
491 switch (DWC3_DEPCMD_CMD(cmd)) {
492 case DWC3_DEPCMD_STARTTRANSFER:
493 dep->flags |= DWC3_EP_TRANSFER_STARTED;
Felipe Balbid7ca7e12018-04-11 12:58:46 +0300494 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300495 break;
496 case DWC3_DEPCMD_ENDTRANSFER:
497 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
498 break;
499 default:
500 /* nothing */
501 break;
502 }
503 }
504
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300505 if (unlikely(susphy)) {
506 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
507 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
508 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
509 }
510
Felipe Balbic0ca3242016-04-04 09:11:51 +0300511 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300512}
513
John Youn50c763f2016-05-31 17:49:56 -0700514static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
515{
516 struct dwc3 *dwc = dep->dwc;
517 struct dwc3_gadget_ep_cmd_params params;
518 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
519
520 /*
521 * As of core revision 2.60a the recommended programming model
522 * is to set the ClearPendIN bit when issuing a Clear Stall EP
523 * command for IN endpoints. This is to prevent an issue where
524 * some (non-compliant) hosts may not send ACK TPs for pending
525 * IN transfers due to a mishandled error condition. Synopsys
526 * STAR 9000614252.
527 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800528 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
529 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700530 cmd |= DWC3_DEPCMD_CLEARPENDIN;
531
532 memset(&params, 0, sizeof(params));
533
Felipe Balbi2cd47182016-04-12 16:42:43 +0300534 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700535}
536
Felipe Balbi72246da2011-08-19 18:10:58 +0300537static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
538{
539 struct dwc3 *dwc = dep->dwc;
Jack Pham62523502018-01-15 16:37:05 -0800540 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
542 if (dep->trb_pool)
543 return 0;
544
Jack Pham62523502018-01-15 16:37:05 -0800545 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
546 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 &dep->trb_pool_dma, GFP_KERNEL);
548 if (!dep->trb_pool) {
549 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
550 dep->name);
551 return -ENOMEM;
552 }
Jack Pham62523502018-01-15 16:37:05 -0800553 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
555 return 0;
556}
557
558static void dwc3_free_trb_pool(struct dwc3_ep *dep)
559{
560 struct dwc3 *dwc = dep->dwc;
561
Jack Pham62523502018-01-15 16:37:05 -0800562 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
563 if (dep->endpoint.ep_type == EP_TYPE_GSI)
564 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300565
Jack Pham62523502018-01-15 16:37:05 -0800566 /*
567 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
568 * with HWO bit set from previous composition when update transfer cmd
569 * is issued.
570 */
571 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
572 memset(&dep->trb_pool[0], 0,
573 sizeof(struct dwc3_trb) * dep->num_trbs);
574 dbg_event(dep->number, "Clr_TRB", 0);
575 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
576
577 dma_free_coherent(dwc->sysdev,
578 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
579 dep->trb_pool, dep->trb_pool_dma);
580 dep->trb_pool = NULL;
581 dep->trb_pool_dma = 0;
582 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300583}
584
Felipe Balbi20d1d432018-04-09 12:49:02 +0300585static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
586{
587 struct dwc3_gadget_ep_cmd_params params;
588
589 memset(&params, 0x00, sizeof(params));
590
591 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
592
593 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
594 &params);
595}
John Younc4509602016-02-16 20:10:53 -0800596
597/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300598 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800599 * @dep: endpoint that is being enabled
600 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300601 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
602 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800603 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300604 * The assignment of transfer resources cannot perfectly follow the data book
605 * due to the fact that the controller driver does not have all knowledge of the
606 * configuration in advance. It is given this information piecemeal by the
607 * composite gadget framework after every SET_CONFIGURATION and
608 * SET_INTERFACE. Trying to follow the databook programming model in this
609 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800610 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300611 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
612 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
613 * incorrect in the scenario of multiple interfaces.
614 *
615 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800616 * endpoint on alt setting (8.1.6).
617 *
618 * The following simplified method is used instead:
619 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300620 * All hardware endpoints can be assigned a transfer resource and this setting
621 * will stay persistent until either a core reset or hibernation. So whenever we
622 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
623 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800624 * guaranteed that there are as many transfer resources as endpoints.
625 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300626 * This function is called for each endpoint when it is being enabled but is
627 * triggered only when called for EP0-out, which always happens first, and which
628 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800629 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300630static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300631{
632 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300633 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800635 int i;
636 int ret;
637
638 if (dep->number)
639 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300640
641 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800642 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300643 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
Felipe Balbi2cd47182016-04-12 16:42:43 +0300645 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800646 if (ret)
647 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300648
John Younc4509602016-02-16 20:10:53 -0800649 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
650 struct dwc3_ep *dep = dwc->eps[i];
651
652 if (!dep)
653 continue;
654
Felipe Balbib07c2db2018-04-09 12:46:47 +0300655 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800656 if (ret)
657 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 }
659
660 return 0;
661}
662
Felipe Balbib07c2db2018-04-09 12:46:47 +0300663static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300664{
John Youn39ebb052016-11-09 16:36:28 -0800665 const struct usb_ss_ep_comp_descriptor *comp_desc;
666 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300667 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300668 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300669
John Youn39ebb052016-11-09 16:36:28 -0800670 comp_desc = dep->endpoint.comp_desc;
671 desc = dep->endpoint.desc;
672
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 memset(&params, 0x00, sizeof(params));
674
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300675 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900676 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
677
678 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800679 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300680 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300681 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900682 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300683
Felipe Balbia2d23f02018-04-09 12:40:48 +0300684 params.param0 |= action;
685 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600686 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600687
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300688 if (usb_endpoint_xfer_control(desc))
689 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300690
691 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
692 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300693
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200694 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300695 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
696 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300697 dep->stream_capable = true;
698 }
699
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500700 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300701 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300702
703 /*
704 * We are doing 1:1 mapping for endpoints, meaning
705 * Physical Endpoints 2 maps to Logical Endpoint 2 and
706 * so on. We consider the direction bit as part of the physical
707 * endpoint number. So USB endpoint 0x81 is 0x03.
708 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300709 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300710
711 /*
712 * We must use the lower 16 TX FIFOs even though
713 * HW might have more
714 */
715 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300716 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300717
718 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300719 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300720 dep->interval = 1 << (desc->bInterval - 1);
721 }
722
Felipe Balbi2cd47182016-04-12 16:42:43 +0300723 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300724}
725
Felipe Balbi72246da2011-08-19 18:10:58 +0300726/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300727 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300729 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300731 * Caller should take care of locking. Execute all necessary commands to
732 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300733 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300734static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300735{
John Youn39ebb052016-11-09 16:36:28 -0800736 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800738
Felipe Balbi72246da2011-08-19 18:10:58 +0300739 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300740 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300741
742 if (!(dep->flags & DWC3_EP_ENABLED)) {
Mayank Rana4c99f662017-04-25 13:48:46 -0700743 ret = dwc3_gadget_resize_tx_fifos(dwc, dep);
744 if (ret)
745 return ret;
746
Felipe Balbib07c2db2018-04-09 12:46:47 +0300747 ret = dwc3_gadget_start_config(dep);
Jack Pham62523502018-01-15 16:37:05 -0800748 if (ret) {
749 dev_err(dwc->dev, "start_config() failed for %s\n",
750 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 return ret;
Jack Pham62523502018-01-15 16:37:05 -0800752 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 }
754
Felipe Balbib07c2db2018-04-09 12:46:47 +0300755 ret = dwc3_gadget_set_ep_config(dep, action);
Jack Pham62523502018-01-15 16:37:05 -0800756 if (ret) {
757 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 return ret;
Jack Pham62523502018-01-15 16:37:05 -0800759 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300760
761 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200762 struct dwc3_trb *trb_st_hw;
763 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300764
Felipe Balbi72246da2011-08-19 18:10:58 +0300765 dep->type = usb_endpoint_type(desc);
766 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800767 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300768
769 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
770 reg |= DWC3_DALEPENA_EP(dep->number);
771 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
772
Baolin Wang76a638f2016-10-31 19:38:36 +0800773 init_waitqueue_head(&dep->wait_end_transfer);
774
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300775 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200776 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300777
John Youn0d257442016-05-19 17:26:08 -0700778 /* Initialize the TRB ring */
779 dep->trb_dequeue = 0;
780 dep->trb_enqueue = 0;
781 memset(dep->trb_pool, 0,
782 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
783
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300784 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300785 trb_st_hw = &dep->trb_pool[0];
786
Felipe Balbif6bafc62012-02-06 11:04:53 +0200787 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
789 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
790 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
791 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300792 }
793
Felipe Balbia97ea992016-09-29 16:28:56 +0300794 /*
795 * Issue StartTransfer here with no-op TRB so we can always rely on No
796 * Response Update Transfer command.
797 */
Mayank Rana35b0dfa2018-03-13 12:01:58 -0700798 if ((usb_endpoint_xfer_bulk(desc) && !dep->endpoint.endless) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300799 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300800 struct dwc3_gadget_ep_cmd_params params;
801 struct dwc3_trb *trb;
802 dma_addr_t trb_dma;
803 u32 cmd;
804
805 memset(&params, 0, sizeof(params));
806 trb = &dep->trb_pool[0];
807 trb_dma = dwc3_trb_dma_offset(dep, trb);
808
809 params.param0 = upper_32_bits(trb_dma);
810 params.param1 = lower_32_bits(trb_dma);
811
812 cmd = DWC3_DEPCMD_STARTTRANSFER;
813
814 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
815 if (ret < 0)
816 return ret;
Felipe Balbia97ea992016-09-29 16:28:56 +0300817 }
818
Felipe Balbi2870e502016-11-03 13:53:29 +0200819out:
820 trace_dwc3_gadget_ep_enable(dep);
821
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 return 0;
823}
824
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200825static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300826{
827 struct dwc3_request *req;
828
Mayank Rana21f76b32018-09-21 16:49:55 -0700829 dbg_log_string("START for %s(%d)", dep->name, dep->number);
Jack Pham62523502018-01-15 16:37:05 -0800830 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300831
Felipe Balbi0e146022016-06-21 10:32:02 +0300832 /* - giveback all requests to gadget driver */
833 while (!list_empty(&dep->started_list)) {
834 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200835
Felipe Balbi0e146022016-06-21 10:32:02 +0300836 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200837 }
838
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200839 while (!list_empty(&dep->pending_list)) {
840 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300841
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200842 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 }
Mayank Rana21f76b32018-09-21 16:49:55 -0700844 dbg_log_string("DONE for %s(%d)", dep->name, dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300845}
846
Jack Pham62523502018-01-15 16:37:05 -0800847static void dwc3_stop_active_transfers(struct dwc3 *dwc)
848{
849 u32 epnum;
850
Mayank Rana21f76b32018-09-21 16:49:55 -0700851 dbg_log_string("START");
Jack Pham62523502018-01-15 16:37:05 -0800852 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
853 struct dwc3_ep *dep;
854
855 dep = dwc->eps[epnum];
856 if (!dep)
857 continue;
858
859 if (!(dep->flags & DWC3_EP_ENABLED))
860 continue;
861
Vijayavardhan Vennapusaaae212f2018-07-17 17:33:16 +0530862 if (dep->endpoint.ep_type == EP_TYPE_GSI && dep->direction)
863 dwc3_notify_event(dwc,
864 DWC3_CONTROLLER_NOTIFY_CLEAR_DB, 0);
865
Jack Pham62523502018-01-15 16:37:05 -0800866 dwc3_remove_requests(dwc, dep);
867 }
Mayank Rana21f76b32018-09-21 16:49:55 -0700868 dbg_log_string("DONE");
Jack Pham62523502018-01-15 16:37:05 -0800869}
870
Felipe Balbi72246da2011-08-19 18:10:58 +0300871/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300872 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 * @dep: the endpoint to disable
874 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300875 * This function undoes what __dwc3_gadget_ep_enable did and also removes
876 * requests which are currently being processed by the hardware and those which
877 * are not yet scheduled.
878 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200879 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300881static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
882{
883 struct dwc3 *dwc = dep->dwc;
884 u32 reg;
885
Felipe Balbi2870e502016-11-03 13:53:29 +0200886 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500887
Jack Pham62523502018-01-15 16:37:05 -0800888 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
889 dwc3_remove_requests(dwc, dep);
890 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
891 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300892
Felipe Balbi687ef982014-04-16 10:30:33 -0500893 /* make sure HW endpoint isn't stalled */
894 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500895 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500896
Felipe Balbi72246da2011-08-19 18:10:58 +0300897 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
898 reg &= ~DWC3_DALEPENA_EP(dep->number);
899 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
900
Felipe Balbi879631a2011-09-30 10:58:47 +0300901 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800903 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300904
John Youn39ebb052016-11-09 16:36:28 -0800905 /* Clear out the ep descriptors for non-ep0 */
906 if (dep->number > 1) {
907 dep->endpoint.comp_desc = NULL;
908 dep->endpoint.desc = NULL;
909 }
910
Felipe Balbi72246da2011-08-19 18:10:58 +0300911 return 0;
912}
913
914/* -------------------------------------------------------------------------- */
915
916static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
917 const struct usb_endpoint_descriptor *desc)
918{
919 return -EINVAL;
920}
921
922static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
923{
924 return -EINVAL;
925}
926
927/* -------------------------------------------------------------------------- */
928
929static int dwc3_gadget_ep_enable(struct usb_ep *ep,
930 const struct usb_endpoint_descriptor *desc)
931{
932 struct dwc3_ep *dep;
933 struct dwc3 *dwc;
934 unsigned long flags;
935 int ret;
936
937 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
938 pr_debug("dwc3: invalid parameters\n");
939 return -EINVAL;
940 }
941
942 if (!desc->wMaxPacketSize) {
943 pr_debug("dwc3: missing wMaxPacketSize\n");
944 return -EINVAL;
945 }
946
947 dep = to_dwc3_ep(ep);
948 dwc = dep->dwc;
949
Felipe Balbi95ca9612015-12-10 13:08:20 -0600950 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
951 "%s is already enabled\n",
952 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300953 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300954
Felipe Balbi72246da2011-08-19 18:10:58 +0300955 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300956 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Jack Pham62523502018-01-15 16:37:05 -0800957 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 spin_unlock_irqrestore(&dwc->lock, flags);
959
960 return ret;
961}
962
963static int dwc3_gadget_ep_disable(struct usb_ep *ep)
964{
965 struct dwc3_ep *dep;
966 struct dwc3 *dwc;
967 unsigned long flags;
968 int ret;
969
970 if (!ep) {
971 pr_debug("dwc3: invalid parameters\n");
972 return -EINVAL;
973 }
974
975 dep = to_dwc3_ep(ep);
976 dwc = dep->dwc;
977
Felipe Balbi95ca9612015-12-10 13:08:20 -0600978 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
979 "%s is already disabled\n",
980 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300981 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300982
Felipe Balbi72246da2011-08-19 18:10:58 +0300983 spin_lock_irqsave(&dwc->lock, flags);
984 ret = __dwc3_gadget_ep_disable(dep);
Jack Pham62523502018-01-15 16:37:05 -0800985 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300986 spin_unlock_irqrestore(&dwc->lock, flags);
987
988 return ret;
989}
990
991static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300992 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300993{
994 struct dwc3_request *req;
995 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300996
997 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900998 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001000
Felipe Balbi31a2f5a2018-05-07 15:19:31 +03001001 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001002 req->epnum = dep->number;
1003 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001004
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001005 trace_dwc3_alloc_request(req);
1006
Felipe Balbi72246da2011-08-19 18:10:58 +03001007 return &req->request;
1008}
1009
1010static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1011 struct usb_request *request)
1012{
1013 struct dwc3_request *req = to_dwc3_request(request);
1014
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001015 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 kfree(req);
1017}
1018
Felipe Balbi42626912018-04-09 13:01:43 +03001019/**
1020 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1021 * @dep: The endpoint with the TRB ring
1022 * @index: The index of the current TRB in the ring
1023 *
1024 * Returns the TRB prior to the one pointed to by the index. If the
1025 * index is 0, we will wrap backwards, skip the link TRB, and return
1026 * the one just before that.
1027 */
1028static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1029{
1030 u8 tmp = index;
1031
Pratham Pratapd048da82017-12-05 14:38:29 +05301032 if (!dep->trb_pool)
1033 return NULL;
1034
Felipe Balbi42626912018-04-09 13:01:43 +03001035 if (!tmp)
1036 tmp = DWC3_TRB_NUM - 1;
1037
1038 return &dep->trb_pool[tmp - 1];
1039}
1040
1041static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1042{
1043 struct dwc3_trb *tmp;
1044 u8 trbs_left;
1045
1046 /*
1047 * If enqueue & dequeue are equal than it is either full or empty.
1048 *
1049 * One way to know for sure is if the TRB right before us has HWO bit
1050 * set or not. If it has, then we're definitely full and can't fit any
1051 * more transfers in our ring.
1052 */
1053 if (dep->trb_enqueue == dep->trb_dequeue) {
1054 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1055 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
1056 return 0;
1057
1058 return DWC3_TRB_NUM - 1;
1059 }
1060
1061 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1062 trbs_left &= (DWC3_TRB_NUM - 1);
1063
1064 if (dep->trb_dequeue < dep->trb_enqueue)
1065 trbs_left--;
1066
1067 return trbs_left;
1068}
Felipe Balbi2c78c022016-08-12 13:13:10 +03001069
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001070static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
1071 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
1072 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +02001073{
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001074 struct dwc3 *dwc = dep->dwc;
1075 struct usb_gadget *gadget = &dwc->gadget;
1076 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +02001077
Felipe Balbif6bafc62012-02-06 11:04:53 +02001078 trb->size = DWC3_TRB_SIZE_LENGTH(length);
1079 trb->bpl = lower_32_bits(dma);
1080 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +02001081
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001082 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +02001083 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001084 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +02001085 break;
1086
1087 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001088 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301089 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001090
Manu Gautam40d829f2017-07-19 17:07:10 +05301091 /*
1092 * USB Specification 2.0 Section 5.9.2 states that: "If
1093 * there is only a single transaction in the microframe,
1094 * only a DATA0 data packet PID is used. If there are
1095 * two transactions per microframe, DATA1 is used for
1096 * the first transaction data packet and DATA0 is used
1097 * for the second transaction data packet. If there are
1098 * three transactions per microframe, DATA2 is used for
1099 * the first transaction data packet, DATA1 is used for
1100 * the second, and DATA0 is used for the third."
1101 *
1102 * IOW, we should satisfy the following cases:
1103 *
1104 * 1) length <= maxpacket
1105 * - DATA0
1106 *
1107 * 2) maxpacket < length <= (2 * maxpacket)
1108 * - DATA1, DATA0
1109 *
1110 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1111 * - DATA2, DATA1, DATA0
1112 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001113 if (speed == USB_SPEED_HIGH) {
1114 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301115 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301116 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1117
1118 if (length <= (2 * maxp))
1119 mult--;
1120
1121 if (length <= maxp)
1122 mult--;
1123
1124 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001125 }
1126 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301127 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001128 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001129
1130 /* always enable Interrupt on Missed ISOC */
1131 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001132 break;
1133
1134 case USB_ENDPOINT_XFER_BULK:
1135 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001136 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001137 break;
1138 default:
1139 /*
1140 * This is only possible with faulty memory because we
1141 * checked it already :)
1142 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001143 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1144 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001145 }
1146
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301147 /*
1148 * Enable Continue on Short Packet
1149 * when endpoint is not a stream capable
1150 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001151 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301152 if (!dep->stream_capable)
1153 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001154
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001155 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001156 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1157 }
1158
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001159 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301160 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001161 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001162
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301163 if (chain)
1164 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1165
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001166 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001167 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001168
1169 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001170
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301171 dwc3_ep_inc_enq(dep);
1172
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001173 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001174}
1175
John Youn361572b2016-05-19 17:26:17 -07001176/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001177 * dwc3_prepare_one_trb - setup one TRB from one request
1178 * @dep: endpoint for which this request is prepared
1179 * @req: dwc3_request pointer
1180 * @chain: should this TRB be chained to the next?
1181 * @node: only for isochronous endpoints. First TRB needs different type.
1182 */
1183static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1184 struct dwc3_request *req, unsigned chain, unsigned node)
1185{
1186 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301187 unsigned int length;
1188 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001189 unsigned stream_id = req->request.stream_id;
1190 unsigned short_not_ok = req->request.short_not_ok;
1191 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301192
1193 if (req->request.num_sgs > 0) {
1194 length = sg_dma_len(req->start_sg);
1195 dma = sg_dma_address(req->start_sg);
1196 } else {
1197 length = req->request.length;
1198 dma = req->request.dma;
1199 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001200
1201 trb = &dep->trb_pool[dep->trb_enqueue];
1202
1203 if (!req->trb) {
1204 dwc3_gadget_move_started_request(req);
1205 req->trb = trb;
1206 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001207 }
1208
1209 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1210 stream_id, short_not_ok, no_interrupt);
1211}
1212
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001213static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001214 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001215{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301216 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001217 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001218 int i;
1219
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301220 unsigned int remaining = req->request.num_mapped_sgs
1221 - req->num_queued_sgs;
1222
1223 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001224 unsigned int length = req->request.length;
1225 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1226 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001227 unsigned chain = true;
1228
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001229 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001230 chain = false;
1231
Felipe Balbic6267a52017-01-05 14:58:46 +02001232 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1233 struct dwc3 *dwc = dep->dwc;
1234 struct dwc3_trb *trb;
1235
1236 req->unaligned = true;
1237
1238 /* prepare normal TRB */
1239 dwc3_prepare_one_trb(dep, req, true, i);
1240
1241 /* Now prepare one extra TRB to align transfer size */
1242 trb = &dep->trb_pool[dep->trb_enqueue];
1243 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001244 maxp - rem, false, 1,
Felipe Balbic6267a52017-01-05 14:58:46 +02001245 req->request.stream_id,
1246 req->request.short_not_ok,
1247 req->request.no_interrupt);
1248 } else {
1249 dwc3_prepare_one_trb(dep, req, chain, i);
1250 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001251
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301252 /*
1253 * There can be a situation where all sgs in sglist are not
1254 * queued because of insufficient trb number. To handle this
1255 * case, update start_sg to next sg to be queued, so that
1256 * we have free trbs we can continue queuing from where we
1257 * previously stopped
1258 */
1259 if (chain)
1260 req->start_sg = sg_next(s);
1261
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301262 req->num_queued_sgs++;
1263
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001264 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001265 break;
1266 }
1267}
1268
1269static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001270 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001271{
Felipe Balbic6267a52017-01-05 14:58:46 +02001272 unsigned int length = req->request.length;
1273 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1274 unsigned int rem = length % maxp;
1275
Tejas Joglekar19bc5352019-01-22 13:26:51 +05301276 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001277 struct dwc3 *dwc = dep->dwc;
1278 struct dwc3_trb *trb;
1279
1280 req->unaligned = true;
1281
1282 /* prepare normal TRB */
1283 dwc3_prepare_one_trb(dep, req, true, 0);
1284
1285 /* Now prepare one extra TRB to align transfer size */
1286 trb = &dep->trb_pool[dep->trb_enqueue];
1287 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001288 false, 1, req->request.stream_id,
Felipe Balbic6267a52017-01-05 14:58:46 +02001289 req->request.short_not_ok,
1290 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001291 } else if (req->request.zero && req->request.length &&
Thinh Nguyen4ea438d2018-07-27 18:52:41 -07001292 (IS_ALIGNED(req->request.length, maxp))) {
Felipe Balbid6e5a542017-04-07 16:34:38 +03001293 struct dwc3 *dwc = dep->dwc;
1294 struct dwc3_trb *trb;
1295
1296 req->zero = true;
1297
1298 /* prepare normal TRB */
1299 dwc3_prepare_one_trb(dep, req, true, 0);
1300
1301 /* Now prepare one extra TRB to handle ZLP */
1302 trb = &dep->trb_pool[dep->trb_enqueue];
1303 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001304 false, 1, req->request.stream_id,
Felipe Balbid6e5a542017-04-07 16:34:38 +03001305 req->request.short_not_ok,
1306 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001307 } else {
1308 dwc3_prepare_one_trb(dep, req, false, 0);
1309 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001310}
1311
Felipe Balbi72246da2011-08-19 18:10:58 +03001312/*
1313 * dwc3_prepare_trbs - setup TRBs from requests
1314 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001315 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001316 * The function goes through the requests list and sets up TRBs for the
1317 * transfers. The function returns once there are no more TRBs available or
1318 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001319 */
Felipe Balbic4233572016-05-12 14:08:34 +03001320static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001321{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001322 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001323
1324 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1325
Felipe Balbid86c5a62016-10-25 13:48:52 +03001326 /*
1327 * We can get in a situation where there's a request in the started list
1328 * but there weren't enough TRBs to fully kick it in the first time
1329 * around, so it has been waiting for more TRBs to be freed up.
1330 *
1331 * In that case, we should check if we have a request with pending_sgs
1332 * in the started list and prepare TRBs for that request first,
1333 * otherwise we will prepare TRBs completely out of order and that will
1334 * break things.
1335 */
1336 list_for_each_entry(req, &dep->started_list, list) {
1337 if (req->num_pending_sgs > 0)
1338 dwc3_prepare_one_trb_sg(dep, req);
1339
1340 if (!dwc3_calc_trbs_left(dep))
1341 return;
1342 }
1343
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001344 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001345 struct dwc3 *dwc = dep->dwc;
1346 int ret;
1347
1348 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1349 dep->direction);
1350 if (ret)
1351 return;
1352
1353 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301354 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301355 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001356 req->num_pending_sgs = req->request.num_mapped_sgs;
1357
Felipe Balbi1f512112016-08-12 13:17:27 +03001358 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001359 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001360 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001361 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001362
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001363 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001364 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001365 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001366}
1367
Felipe Balbi7fdca762017-09-05 14:41:34 +03001368static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001369{
1370 struct dwc3_gadget_ep_cmd_params params;
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001371 struct dwc3_request *req, *req1, *n;
Jack Pham62523502018-01-15 16:37:05 -08001372 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001373 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001374 int ret;
1375 u32 cmd;
1376
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001377 if (!dwc3_calc_trbs_left(dep))
1378 return 0;
1379
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001380 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001381
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001382 dwc3_prepare_trbs(dep);
1383 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001384 if (!req) {
1385 dep->flags |= DWC3_EP_PENDING_REQUEST;
Jack Pham62523502018-01-15 16:37:05 -08001386 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001387 return 0;
1388 }
1389
1390 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001391
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001392 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301393 params.param0 = upper_32_bits(req->trb_dma);
1394 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001395 cmd = DWC3_DEPCMD_STARTTRANSFER;
1396
1397 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1398 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301399 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001400 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1401 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301402 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001403
Felipe Balbi2cd47182016-04-12 16:42:43 +03001404 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001405 if (ret < 0) {
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001406 if ((ret == -EAGAIN) && starting &&
1407 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1408 dbg_event(dep->number, "CMD_STS", ret);
1409 /* If bit13 in Command complete event is set, software
1410 * must issue ENDTRANDFER command and wait for
1411 * Xfernotready event to queue the requests again.
1412 */
1413 if (!dep->resource_index) {
1414 dwc3_gadget_ep_get_transfer_index(dep);
1415 WARN_ON_ONCE(!dep->resource_index);
1416 }
1417 dwc3_stop_active_transfer(dwc, dep->number, true);
1418
1419 list_for_each_entry_safe_reverse(req1, n,
1420 &dep->started_list, list) {
1421 req1->trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1422 req1->trb = NULL;
1423 dwc3_gadget_move_pending_list_front(req1);
1424 dwc3_ep_inc_deq(dep);
1425 }
1426
1427 return ret;
1428 }
1429
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 /*
1431 * FIXME we need to iterate over the list of requests
1432 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001433 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001434 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001435 if (req->trb)
1436 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001437 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001438 return ret;
1439 }
1440
Felipe Balbi72246da2011-08-19 18:10:58 +03001441 return 0;
1442}
1443
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001444static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1445{
1446 u32 reg;
1447
1448 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1449 return DWC3_DSTS_SOFFN(reg);
1450}
1451
Felipe Balbi5828cad2018-03-27 11:14:31 +03001452static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301453{
Hemant Kumardd36b892018-08-02 17:51:05 -07001454 u16 wraparound_bits;
1455
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001456 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001457 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001458 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301459 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301460 return;
1461 }
1462
Hemant Kumardd36b892018-08-02 17:51:05 -07001463 wraparound_bits = dep->frame_number & DWC3_FRAME_WRAP_AROUND_MASK;
1464 dep->frame_number = dep->frame_number & ~DWC3_FRAME_WRAP_AROUND_MASK;
1465
1466 /* if frame wrapped-around update wrap-around bits to reflect that */
1467 if (__dwc3_gadget_get_frame(dep->dwc) < dep->frame_number)
1468 wraparound_bits += BIT(14);
1469
1470 dep->frame_number = __dwc3_gadget_get_frame(dep->dwc) +
1471 2 * dep->interval;
1472
1473 /* align uf to ep interval */
1474 dep->frame_number = (wraparound_bits | dep->frame_number) &
1475 ~(dep->interval - 1);
1476
Felipe Balbi7fdca762017-09-05 14:41:34 +03001477 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301478}
1479
Felipe Balbi72246da2011-08-19 18:10:58 +03001480static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1481{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001482 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001483
Mayank Ranad32d7842018-06-29 10:20:13 -07001484 if (!dep->endpoint.desc || !dwc->pullups_connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001485 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1486 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001487 return -ESHUTDOWN;
1488 }
1489
Felipe Balbi04fb3652017-05-17 15:57:45 +03001490 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1491 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001492 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001493
Manu Gautam8bb9cbc2013-02-11 15:53:34 +05301494 if (req->request.status == -EINPROGRESS) {
1495 dev_err(dwc->dev, "%s: %pK request already in queue\n",
1496 dep->name, req);
1497 return -EBUSY;
1498 }
1499
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 req->request.actual = 0;
1501 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001502
Felipe Balbife84f522015-09-01 09:01:38 -05001503 trace_dwc3_ep_queue(req);
1504
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001505 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001506
Felipe Balbid889c232016-09-29 15:44:29 +03001507 /*
1508 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1509 * wait for a XferNotReady event so we will know what's the current
1510 * (micro-)frame number.
1511 *
1512 * Without this trick, we are very, very likely gonna get Bus Expiry
1513 * errors which will force us issue EndTransfer command.
1514 */
1515 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001516 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1517 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001518 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001519
1520 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1521 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1522 __dwc3_gadget_start_isoc(dep);
1523 return 0;
1524 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001525 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001526 }
1527
Felipe Balbi7fdca762017-09-05 14:41:34 +03001528 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001529}
1530
Jack Pham62523502018-01-15 16:37:05 -08001531static int dwc3_gadget_wakeup(struct usb_gadget *g)
1532{
1533 struct dwc3 *dwc = gadget_to_dwc(g);
1534
1535 schedule_work(&dwc->wakeup_work);
1536 return 0;
1537}
1538
1539static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1540{
1541 if (atomic_read(&dwc->in_lpm) ||
1542 dwc->link_state == DWC3_LINK_STATE_U3)
1543 return true;
1544 return false;
1545}
1546
Felipe Balbi72246da2011-08-19 18:10:58 +03001547static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1548 gfp_t gfp_flags)
1549{
1550 struct dwc3_request *req = to_dwc3_request(request);
1551 struct dwc3_ep *dep = to_dwc3_ep(ep);
1552 struct dwc3 *dwc = dep->dwc;
1553
1554 unsigned long flags;
1555
1556 int ret;
1557
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001558 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001559 ret = __dwc3_gadget_ep_queue(dep, req);
1560 spin_unlock_irqrestore(&dwc->lock, flags);
1561
1562 return ret;
1563}
1564
1565static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1566 struct usb_request *request)
1567{
1568 struct dwc3_request *req = to_dwc3_request(request);
1569 struct dwc3_request *r = NULL;
1570
1571 struct dwc3_ep *dep = to_dwc3_ep(ep);
1572 struct dwc3 *dwc = dep->dwc;
1573
1574 unsigned long flags;
1575 int ret = 0;
1576
Jack Pham62523502018-01-15 16:37:05 -08001577 if (atomic_read(&dwc->in_lpm)) {
1578 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1579 return -EAGAIN;
1580 }
1581
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001582 trace_dwc3_ep_dequeue(req);
1583
Felipe Balbi72246da2011-08-19 18:10:58 +03001584 spin_lock_irqsave(&dwc->lock, flags);
1585
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001586 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001587 if (r == req)
1588 break;
1589 }
1590
1591 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001592 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001593 if (r == req)
1594 break;
1595 }
1596 if (r == req) {
1597 /* wait until it is processed */
Jack Pham62523502018-01-15 16:37:05 -08001598 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001599
1600 /*
1601 * If request was already started, this means we had to
1602 * stop the transfer. With that we also need to ignore
1603 * all TRBs used by the request, however TRBs can only
1604 * be modified after completion of END_TRANSFER
1605 * command. So what we do here is that we wait for
1606 * END_TRANSFER completion and only after that, we jump
1607 * over TRBs by clearing HWO and incrementing dequeue
1608 * pointer.
1609 *
1610 * Note that we have 2 possible types of transfers here:
1611 *
1612 * i) Linear buffer request
1613 * ii) SG-list based request
1614 *
1615 * SG-list based requests will have r->num_pending_sgs
1616 * set to a valid number (> 0). Linear requests,
1617 * normally use a single TRB.
1618 *
1619 * For each of these two cases, if r->unaligned flag is
1620 * set, one extra TRB has been used to align transfer
1621 * size to wMaxPacketSize.
1622 *
1623 * All of these cases need to be taken into
1624 * consideration so we don't mess up our TRB ring
1625 * pointers.
1626 */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001627 if (!r->trb)
Mayank Rana05645362018-03-23 10:05:33 -07001628 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001629
1630 if (r->num_pending_sgs) {
1631 struct dwc3_trb *trb;
1632 int i = 0;
1633
1634 for (i = 0; i < r->num_pending_sgs; i++) {
1635 trb = r->trb + i;
1636 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1637 dwc3_ep_inc_deq(dep);
1638 }
1639
Felipe Balbid6e5a542017-04-07 16:34:38 +03001640 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001641 trb = r->trb + r->num_pending_sgs + 1;
1642 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1643 dwc3_ep_inc_deq(dep);
1644 }
1645 } else {
1646 struct dwc3_trb *trb = r->trb;
1647
1648 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1649 dwc3_ep_inc_deq(dep);
1650
Felipe Balbid6e5a542017-04-07 16:34:38 +03001651 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001652 trb = r->trb + 1;
1653 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1654 dwc3_ep_inc_deq(dep);
1655 }
1656 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301657 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001658 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001659 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 request, ep->name);
1661 ret = -EINVAL;
1662 goto out0;
1663 }
1664
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301665out1:
Jack Pham62523502018-01-15 16:37:05 -08001666 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001667 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001668
Felipe Balbi72246da2011-08-19 18:10:58 +03001669 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1670
1671out0:
1672 spin_unlock_irqrestore(&dwc->lock, flags);
1673
1674 return ret;
1675}
1676
Felipe Balbi7a608552014-09-24 14:19:52 -05001677int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001678{
1679 struct dwc3_gadget_ep_cmd_params params;
1680 struct dwc3 *dwc = dep->dwc;
1681 int ret;
1682
Jack Pham62523502018-01-15 16:37:05 -08001683 if (!dep->endpoint.desc) {
1684 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1685 return -EINVAL;
1686 }
1687
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001688 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1689 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1690 return -EINVAL;
1691 }
1692
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 memset(&params, 0x00, sizeof(params));
Jack Pham62523502018-01-15 16:37:05 -08001694 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001695 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001696 struct dwc3_trb *trb;
1697
1698 unsigned transfer_in_flight;
1699 unsigned started;
1700
1701 if (dep->number > 1)
1702 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1703 else
1704 trb = &dwc->ep0_trb[dep->trb_enqueue];
1705
Pratham Pratapd048da82017-12-05 14:38:29 +05301706 if (trb)
1707 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1708 else
1709 transfer_in_flight = false;
1710
Felipe Balbi69450c42016-05-30 13:37:02 +03001711 started = !list_empty(&dep->started_list);
1712
1713 if (!protocol && ((dep->direction && transfer_in_flight) ||
1714 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001715 return -EAGAIN;
1716 }
1717
Felipe Balbi2cd47182016-04-12 16:42:43 +03001718 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1719 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001720 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001721 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 dep->name);
1723 else
1724 dep->flags |= DWC3_EP_STALL;
1725 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001726
John Youn50c763f2016-05-31 17:49:56 -07001727 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001728 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001729 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001730 dep->name);
1731 else
Alan Sterna535d812013-11-01 12:05:12 -04001732 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001733 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001734
Felipe Balbi72246da2011-08-19 18:10:58 +03001735 return ret;
1736}
1737
1738static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1739{
1740 struct dwc3_ep *dep = to_dwc3_ep(ep);
1741 struct dwc3 *dwc = dep->dwc;
1742
1743 unsigned long flags;
1744
1745 int ret;
1746
Jack Pham62523502018-01-15 16:37:05 -08001747 if (!ep->desc) {
1748 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1749 return -EINVAL;
1750 }
1751
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001753 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001754 spin_unlock_irqrestore(&dwc->lock, flags);
1755
1756 return ret;
1757}
1758
1759static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1760{
1761 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001762 struct dwc3 *dwc = dep->dwc;
1763 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001764 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001765
Paul Zimmerman249a4562012-02-24 17:32:16 -08001766 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08001767 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 dep->flags |= DWC3_EP_WEDGE;
1769
Pratyush Anand08f0d962012-06-25 22:40:43 +05301770 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001771 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301772 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001773 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001774 spin_unlock_irqrestore(&dwc->lock, flags);
1775
1776 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001777}
1778
1779/* -------------------------------------------------------------------------- */
1780
1781static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1782 .bLength = USB_DT_ENDPOINT_SIZE,
1783 .bDescriptorType = USB_DT_ENDPOINT,
1784 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1785};
1786
1787static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1788 .enable = dwc3_gadget_ep0_enable,
1789 .disable = dwc3_gadget_ep0_disable,
1790 .alloc_request = dwc3_gadget_ep_alloc_request,
1791 .free_request = dwc3_gadget_ep_free_request,
1792 .queue = dwc3_gadget_ep0_queue,
1793 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301794 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 .set_wedge = dwc3_gadget_ep_set_wedge,
1796};
1797
1798static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1799 .enable = dwc3_gadget_ep_enable,
1800 .disable = dwc3_gadget_ep_disable,
1801 .alloc_request = dwc3_gadget_ep_alloc_request,
1802 .free_request = dwc3_gadget_ep_free_request,
1803 .queue = dwc3_gadget_ep_queue,
1804 .dequeue = dwc3_gadget_ep_dequeue,
1805 .set_halt = dwc3_gadget_ep_set_halt,
1806 .set_wedge = dwc3_gadget_ep_set_wedge,
1807};
1808
1809/* -------------------------------------------------------------------------- */
1810
1811static int dwc3_gadget_get_frame(struct usb_gadget *g)
1812{
1813 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001814
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001815 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001816}
1817
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001818static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001819{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001820 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001821
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001822 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 u32 reg;
1824
Felipe Balbi72246da2011-08-19 18:10:58 +03001825 u8 link_state;
1826 u8 speed;
1827
Felipe Balbi72246da2011-08-19 18:10:58 +03001828 /*
1829 * According to the Databook Remote wakeup request should
1830 * be issued only when the device is in early suspend state.
1831 *
1832 * We can check that via USB Link State bits in DSTS register.
1833 */
1834 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1835
1836 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001837 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001838 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001839 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001840
1841 link_state = DWC3_DSTS_USBLNKST(reg);
1842
1843 switch (link_state) {
1844 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1845 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1846 break;
1847 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001848 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001849 }
1850
Felipe Balbi8598bde2012-01-02 18:55:57 +02001851 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1852 if (ret < 0) {
1853 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001854 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001855 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001856
Paul Zimmerman802fde92012-04-27 13:10:52 +03001857 /* Recent versions do this automatically */
1858 if (dwc->revision < DWC3_REVISION_194A) {
1859 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001860 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001861 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1862 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1863 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001864
Paul Zimmerman1d046792012-02-15 18:56:56 -08001865 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001866 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001867
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001868 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001869 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1870
1871 /* in HS, means ON */
1872 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1873 break;
1874 }
1875
1876 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1877 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001878 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001879 }
1880
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001881 return 0;
1882}
1883
Jack Pham62523502018-01-15 16:37:05 -08001884#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1885#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1886
1887static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001888{
Jack Pham62523502018-01-15 16:37:05 -08001889 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001890 int ret;
Jack Pham62523502018-01-15 16:37:05 -08001891 static int retry_count;
1892
1893 dwc = container_of(w, struct dwc3, wakeup_work);
1894
1895 ret = pm_runtime_get_sync(dwc->dev);
1896 if (ret) {
1897 /* pm_runtime_get_sync returns -EACCES error between
1898 * late_suspend and early_resume, wait for system resume to
1899 * finish and queue work again
1900 */
1901 dev_dbg(dwc->dev, "PM runtime get sync failed, ret %d\n", ret);
1902 if (ret == -EACCES) {
1903 pm_runtime_put_noidle(dwc->dev);
1904 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1905 retry_count = 0;
1906 dev_err(dwc->dev, "pm_runtime_get_sync timed out\n");
1907 return;
1908 }
1909 msleep(DWC3_PM_RESUME_DELAY);
1910 retry_count++;
1911 schedule_work(&dwc->wakeup_work);
1912 return;
1913 }
1914 }
1915 retry_count = 0;
1916 dbg_event(0xFF, "Gdgwake gsyn",
1917 atomic_read(&dwc->dev->power.usage_count));
1918
1919 ret = dwc3_gadget_wakeup_int(dwc);
1920 if (ret)
1921 dev_err(dwc->dev, "Remote wakeup failed. ret = %d\n", ret);
1922
1923 pm_runtime_put_noidle(dwc->dev);
1924 dbg_event(0xFF, "Gdgwake put",
1925 atomic_read(&dwc->dev->power.usage_count));
1926}
1927
1928static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1929{
1930 bool link_recover_only = false;
1931
1932 u32 reg;
1933 int ret = 0;
1934 u8 link_state;
1935 unsigned long flags;
1936
1937 dev_dbg(dwc->dev, "%s(): Entry\n", __func__);
1938 disable_irq(dwc->irq);
1939 spin_lock_irqsave(&dwc->lock, flags);
1940 /*
1941 * According to the Databook Remote wakeup request should
1942 * be issued only when the device is in early suspend state.
1943 *
1944 * We can check that via USB Link State bits in DSTS register.
1945 */
1946 link_state = dwc3_get_link_state(dwc);
1947
1948 switch (link_state) {
1949 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1950 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1951 break;
1952 case DWC3_LINK_STATE_U1:
1953 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1954 link_recover_only = true;
1955 break;
1956 }
1957 /* Intentional fallthrough */
1958 default:
1959 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1960 link_state);
1961 ret = -EINVAL;
1962 goto out;
1963 }
1964
1965 /* Enable LINK STATUS change event */
1966 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1967 reg |= DWC3_DEVTEN_ULSTCNGEN;
1968 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1969 /*
1970 * memory barrier is required to make sure that required events
1971 * with core is enabled before performing RECOVERY mechnism.
1972 */
1973 mb();
1974
1975 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1976 if (ret < 0) {
1977 dev_err(dwc->dev, "failed to put link in Recovery\n");
1978 /* Disable LINK STATUS change */
1979 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1980 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1981 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1982 /* Required to complete this operation before returning */
1983 mb();
1984 goto out;
1985 }
1986
1987 /* Recent versions do this automatically */
1988 if (dwc->revision < DWC3_REVISION_194A) {
1989 /* write zeroes to Link Change Request */
1990 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1991 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1992 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1993 }
1994
1995 spin_unlock_irqrestore(&dwc->lock, flags);
1996 enable_irq(dwc->irq);
1997
1998 /*
1999 * Have bigger value (16 sec) for timeout since some host PCs driving
2000 * resume for very long time (e.g. 8 sec)
2001 */
2002 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
2003 (dwc->link_state < DWC3_LINK_STATE_U3) ||
2004 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
2005 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002006
2007 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002008 /* Disable link status change event */
2009 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2010 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2011 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2012 /*
2013 * Complete this write before we go ahead and perform resume
2014 * as we don't need link status change notificaiton anymore.
2015 */
2016 mb();
2017
2018 if (!ret) {
2019 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
2020 dwc->link_state);
2021 ret = -EINVAL;
2022 spin_unlock_irqrestore(&dwc->lock, flags);
2023 goto out1;
2024 } else {
2025 ret = 0;
2026 /*
2027 * If USB is disconnected OR received RESET from host,
2028 * don't perform resume
2029 */
2030 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
2031 dwc->gadget.state == USB_STATE_DEFAULT)
2032 link_recover_only = true;
2033 }
2034
2035 /*
2036 * According to DWC3 databook, the controller does not
2037 * trigger a wakeup event when remote-wakeup is used.
2038 * Hence, after remote-wakeup sequence is complete, and
2039 * the device is back at U0 state, it is required that
2040 * the resume sequence is initiated by SW.
2041 */
2042 if (!link_recover_only)
2043 dwc3_gadget_wakeup_interrupt(dwc, true);
2044
Felipe Balbi72246da2011-08-19 18:10:58 +03002045 spin_unlock_irqrestore(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002046 dev_dbg(dwc->dev, "%s: Exit\n", __func__);
2047 return ret;
2048
2049out:
2050 spin_unlock_irqrestore(&dwc->lock, flags);
2051 enable_irq(dwc->irq);
2052
2053out1:
2054 return ret;
2055}
2056
2057static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
2058{
2059 int ret = 0;
2060 struct dwc3 *dwc = gadget_to_dwc(g);
2061
2062 if (!g || (g->speed != USB_SPEED_SUPER))
2063 return -ENOTSUPP;
2064
2065 if (dwc3_gadget_is_suspended(dwc)) {
2066 dev_dbg(dwc->dev, "USB bus is suspended, scheduling wakeup\n");
2067 dwc3_gadget_wakeup(&dwc->gadget);
2068 return -EAGAIN;
2069 }
2070
2071 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_XMIT_DEV,
2072 0x1 | (interface_id << 4));
2073 if (ret)
2074 dev_err(dwc->dev, "Function wakeup HW command failed, ret %d\n",
2075 ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002076
2077 return ret;
2078}
2079
2080static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2081 int is_selfpowered)
2082{
2083 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002084 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002085
Paul Zimmerman249a4562012-02-24 17:32:16 -08002086 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002087 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002088 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
2090 return 0;
2091}
2092
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002093static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002094{
Mayank Rana0f67c832018-03-07 14:11:41 -08002095 u32 reg, reg1;
Jack Pham62523502018-01-15 16:37:05 -08002096 u32 timeout = 1500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002097
Jack Pham62523502018-01-15 16:37:05 -08002098 dbg_event(0xFF, "run_stop", is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03002099 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002100 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002101 if (dwc->revision <= DWC3_REVISION_187A) {
2102 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2103 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2104 }
2105
2106 if (dwc->revision >= DWC3_REVISION_194A)
2107 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Jack Pham62523502018-01-15 16:37:05 -08002108
2109 dwc3_event_buffers_setup(dwc);
2110 __dwc3_gadget_start(dwc);
2111
Mayank Rana0f67c832018-03-07 14:11:41 -08002112 reg1 = dwc3_readl(dwc->regs, DWC3_DCFG);
2113 reg1 &= ~(DWC3_DCFG_SPEED_MASK);
2114
2115 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
2116 reg1 |= DWC3_DCFG_SUPERSPEED_PLUS;
2117 else if (dwc->maximum_speed == USB_SPEED_HIGH)
2118 reg1 |= DWC3_DCFG_HIGHSPEED;
2119 else
2120 reg1 |= DWC3_DCFG_SUPERSPEED;
2121 dwc3_writel(dwc->regs, DWC3_DCFG, reg1);
2122
Paul Zimmerman802fde92012-04-27 13:10:52 +03002123 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002124
2125 if (dwc->has_hibernation)
2126 reg |= DWC3_DCTL_KEEP_CONNECT;
2127
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002128 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002129 } else {
Jack Pham62523502018-01-15 16:37:05 -08002130 dwc3_gadget_disable_irq(dwc);
Mayank Ranad32d7842018-06-29 10:20:13 -07002131 dwc->pullups_connected = false;
Jack Pham62523502018-01-15 16:37:05 -08002132 __dwc3_gadget_ep_disable(dwc->eps[0]);
2133 __dwc3_gadget_ep_disable(dwc->eps[1]);
2134
2135 /*
2136 * According to dwc3 databook, it is must to remove any active
2137 * transfers before trying to stop USB device controller. Hence
2138 * call dwc3_stop_active_transfers() API before stopping USB
2139 * device controller.
2140 */
2141 dwc3_stop_active_transfers(dwc);
2142
Felipe Balbi72246da2011-08-19 18:10:58 +03002143 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002144
2145 if (dwc->has_hibernation && !suspend)
2146 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002147 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002148
2149 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2150
2151 do {
2152 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002153 reg &= DWC3_DSTS_DEVCTRLHLT;
2154 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002155
Jack Pham62523502018-01-15 16:37:05 -08002156 if (!timeout) {
2157 dev_err(dwc->dev, "failed to %s controller\n",
2158 is_on ? "start" : "stop");
2159 if (is_on)
2160 dbg_event(0xFF, "STARTTOUT", reg);
2161 else
2162 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03002163 return -ETIMEDOUT;
Jack Pham62523502018-01-15 16:37:05 -08002164 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002165
Pratyush Anand6f17f742012-07-02 10:21:55 +05302166 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002167}
2168
Jack Pham62523502018-01-15 16:37:05 -08002169static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2170{
2171 struct dwc3 *dwc = gadget_to_dwc(g);
2172
2173 dwc->vbus_draw = mA;
2174 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
2175 dbg_event(0xFF, "currentDraw", mA);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002176 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08002177 return 0;
2178}
2179
Felipe Balbi72246da2011-08-19 18:10:58 +03002180static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2181{
2182 struct dwc3 *dwc = gadget_to_dwc(g);
2183 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302184 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002185
2186 is_on = !!is_on;
Jack Pham62523502018-01-15 16:37:05 -08002187 dwc->softconnect = is_on;
2188
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002189 if ((dwc3_is_otg_or_drd(dwc) && !dwc->vbus_active)
Jack Pham62523502018-01-15 16:37:05 -08002190 || !dwc->gadget_driver) {
2191 /*
2192 * Need to wait for vbus_session(on) from otg driver or to
2193 * the udc_start.
2194 */
2195 dbg_event(0xFF, "WaitPullup", 0);
2196 return 0;
2197 }
2198
2199 pm_runtime_get_sync(dwc->dev);
2200 dbg_event(0xFF, "Pullup gsync",
2201 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03002202
Baolin Wangbb014732016-10-14 17:11:33 +08002203 /*
2204 * Per databook, when we want to stop the gadget, if a control transfer
2205 * is still in process, complete it and get the core into setup phase.
2206 */
Jack Phambe653d62018-06-19 18:42:40 -07002207 if (!is_on && (dwc->ep0state != EP0_SETUP_PHASE ||
2208 dwc->ep0_next_event != DWC3_EP0_COMPLETE)) {
Baolin Wangbb014732016-10-14 17:11:33 +08002209 reinit_completion(&dwc->ep0_in_setup);
2210
2211 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2212 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
Manu Gautam1f805462019-02-27 08:50:12 +05302213 if (ret == 0)
Baolin Wangbb014732016-10-14 17:11:33 +08002214 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
Baolin Wangbb014732016-10-14 17:11:33 +08002215 }
2216
Felipe Balbi72246da2011-08-19 18:10:58 +03002217 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002218 /*
2219 * If we are here after bus suspend notify otg state machine to
2220 * increment pm usage count of dwc to prevent pm_runtime_suspend
2221 * during enumeration.
2222 */
2223 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002224 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
2225
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002226 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 spin_unlock_irqrestore(&dwc->lock, flags);
2228
Jack Pham62523502018-01-15 16:37:05 -08002229 pm_runtime_mark_last_busy(dwc->dev);
2230 pm_runtime_put_autosuspend(dwc->dev);
2231 dbg_event(0xFF, "Pullup put",
2232 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302233 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002234}
2235
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002236static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2237{
2238 u32 reg;
2239
Jack Pham62523502018-01-15 16:37:05 -08002240 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002241 /* Enable all but Start and End of Frame IRQs */
2242 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2243 DWC3_DEVTEN_EVNTOVERFLOWEN |
2244 DWC3_DEVTEN_CMDCMPLTEN |
2245 DWC3_DEVTEN_ERRTICERREN |
2246 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002247 DWC3_DEVTEN_CONNECTDONEEN |
2248 DWC3_DEVTEN_USBRSTEN |
2249 DWC3_DEVTEN_DISCONNEVTEN);
2250
Jack Pham62523502018-01-15 16:37:05 -08002251 /*
2252 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
2253 * else enable USB Link change event (BIT:3) for older version
2254 */
2255 if (dwc->revision < DWC3_REVISION_230A)
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002256 reg |= DWC3_DEVTEN_ULSTCNGEN;
Jack Pham62523502018-01-15 16:37:05 -08002257 else
2258 reg |= DWC3_DEVTEN_EOPFEN;
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002259
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002260 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2261}
2262
Jack Pham62523502018-01-15 16:37:05 -08002263void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002264{
Jack Pham62523502018-01-15 16:37:05 -08002265 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002266 /* mask all interrupts */
2267 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2268}
2269
Felipe Balbib15a7622011-06-30 16:57:15 +03002270static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002271
Felipe Balbi4e994722016-05-13 14:09:59 +03002272/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002273 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2274 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002275 *
2276 * The following looks like complex but it's actually very simple. In order to
2277 * calculate the number of packets we can burst at once on OUT transfers, we're
2278 * gonna use RxFIFO size.
2279 *
2280 * To calculate RxFIFO size we need two numbers:
2281 * MDWIDTH = size, in bits, of the internal memory bus
2282 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2283 *
2284 * Given these two numbers, the formula is simple:
2285 *
2286 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2287 *
2288 * 24 bytes is for 3x SETUP packets
2289 * 16 bytes is a clock domain crossing tolerance
2290 *
2291 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2292 */
2293static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2294{
2295 u32 ram2_depth;
2296 u32 mdwidth;
2297 u32 nump;
2298 u32 reg;
2299
2300 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2301 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2302
2303 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2304 nump = min_t(u32, nump, 16);
2305
2306 /* update NumP */
2307 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2308 reg &= ~DWC3_DCFG_NUMP_MASK;
2309 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2310 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2311}
2312
Jack Pham62523502018-01-15 16:37:05 -08002313static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2314{
2315 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2316 unsigned long flags;
2317
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002318 if (dwc->dr_mode != USB_DR_MODE_OTG && dwc->dr_mode != USB_DR_MODE_DRD)
Jack Pham62523502018-01-15 16:37:05 -08002319 return -EPERM;
2320
2321 is_active = !!is_active;
2322
2323 dbg_event(0xFF, "VbusSess", is_active);
2324 spin_lock_irqsave(&dwc->lock, flags);
2325
2326 /* Mark that the vbus was powered */
2327 dwc->vbus_active = is_active;
2328
2329 /*
2330 * Check if upper level usb_gadget_driver was already registered with
2331 * this udc controller driver (if dwc3_gadget_start was called)
2332 */
2333 if (dwc->gadget_driver && dwc->softconnect) {
2334 if (dwc->vbus_active) {
2335 /*
2336 * Both vbus was activated by otg and pullup was
2337 * signaled by the gadget driver.
2338 */
2339 dwc3_gadget_run_stop(dwc, 1, false);
2340 } else {
2341 dwc3_gadget_run_stop(dwc, 0, false);
2342 }
2343 }
2344
2345 /*
2346 * Clearing run/stop bit might occur before disconnect event is seen.
2347 * Make sure to let gadget driver know in that case.
2348 */
2349 if (!dwc->vbus_active) {
2350 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2351 dwc3_gadget_disconnect_interrupt(dwc);
2352 }
2353
2354 spin_unlock_irqrestore(&dwc->lock, flags);
2355 return 0;
2356}
2357
Felipe Balbid7be2952016-05-04 15:49:37 +03002358static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002359{
Felipe Balbi72246da2011-08-19 18:10:58 +03002360 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002361 int ret = 0;
2362 u32 reg;
2363
Jack Pham62523502018-01-15 16:37:05 -08002364 dbg_event(0xFF, "__Gadgetstart", 0);
2365
John Youncf40b862016-11-14 12:32:43 -08002366 /*
2367 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2368 * the core supports IMOD, disable it.
2369 */
2370 if (dwc->imod_interval) {
2371 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2372 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2373 } else if (dwc3_has_imod(dwc)) {
2374 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2375 }
2376
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002377 /*
2378 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2379 * field instead of letting dwc3 itself calculate that automatically.
2380 *
2381 * This way, we maximize the chances that we'll be able to get several
2382 * bursts of data without going through any sort of endpoint throttling.
2383 */
2384 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002385 if (dwc3_is_usb31(dwc))
2386 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2387 else
2388 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2389
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002390 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2391
Jack Pham62523502018-01-15 16:37:05 -08002392 /*
2393 * Programs the number of outstanding pipelined transfer requests
2394 * the AXI master pushes to the AXI slave.
2395 */
2396 if (dwc->revision >= DWC3_REVISION_270A) {
2397 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2398 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2399 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2400 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2401 }
2402
Felipe Balbi4e994722016-05-13 14:09:59 +03002403 dwc3_gadget_setup_nump(dwc);
2404
Felipe Balbi72246da2011-08-19 18:10:58 +03002405 /* Start with SuperSpeed Default */
2406 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2407
2408 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002409 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002410 if (ret) {
2411 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002412 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002413 }
2414
2415 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002416 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002417 if (ret) {
2418 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002419 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002420 }
2421
2422 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002423 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao08c937f2018-12-26 19:22:00 +08002424 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002425 dwc3_ep0_out_start(dwc);
2426
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002427 dwc3_gadget_enable_irq(dwc);
2428
Felipe Balbid7be2952016-05-04 15:49:37 +03002429 return 0;
2430
2431err1:
2432 __dwc3_gadget_ep_disable(dwc->eps[0]);
2433
2434err0:
2435 return ret;
2436}
2437
2438static int dwc3_gadget_start(struct usb_gadget *g,
2439 struct usb_gadget_driver *driver)
2440{
2441 struct dwc3 *dwc = gadget_to_dwc(g);
2442 unsigned long flags;
2443 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002444
Jack Pham62523502018-01-15 16:37:05 -08002445 dbg_event(0xFF, "Gadgetstart", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002446 spin_lock_irqsave(&dwc->lock, flags);
2447 if (dwc->gadget_driver) {
2448 dev_err(dwc->dev, "%s is already bound to %s\n",
2449 dwc->gadget.name,
2450 dwc->gadget_driver->driver.name);
2451 ret = -EBUSY;
Jack Pham62523502018-01-15 16:37:05 -08002452 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002453 }
2454
2455 dwc->gadget_driver = driver;
2456
Jack Pham62523502018-01-15 16:37:05 -08002457 /*
2458 * For DRD, this might get called by gadget driver during bootup
2459 * even though host mode might be active. Don't actually perform
2460 * device-specific initialization until device mode is activated.
2461 * In that case dwc3_gadget_restart() will handle it.
2462 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002463 spin_unlock_irqrestore(&dwc->lock, flags);
2464
2465 return 0;
2466
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002467err0:
Jack Pham62523502018-01-15 16:37:05 -08002468 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002469 return ret;
2470}
2471
Felipe Balbid7be2952016-05-04 15:49:37 +03002472static void __dwc3_gadget_stop(struct dwc3 *dwc)
2473{
Jack Pham62523502018-01-15 16:37:05 -08002474 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002475 dwc3_gadget_disable_irq(dwc);
2476 __dwc3_gadget_ep_disable(dwc->eps[0]);
2477 __dwc3_gadget_ep_disable(dwc->eps[1]);
2478}
2479
Felipe Balbi22835b82014-10-17 12:05:12 -05002480static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002481{
2482 struct dwc3 *dwc = gadget_to_dwc(g);
2483 unsigned long flags;
2484
2485 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002486 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 spin_unlock_irqrestore(&dwc->lock, flags);
2488
Jack Pham62523502018-01-15 16:37:05 -08002489 dbg_event(0xFF, "fwq_started", 0);
2490 flush_workqueue(dwc->dwc_wq);
2491 dbg_event(0xFF, "fwq_completed", 0);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002492
Felipe Balbi72246da2011-08-19 18:10:58 +03002493 return 0;
2494}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002495
Mayank Rana0f67c832018-03-07 14:11:41 -08002496static void __maybe_unused dwc3_gadget_set_speed(struct usb_gadget *g,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002497 enum usb_device_speed speed)
2498{
2499 struct dwc3 *dwc = gadget_to_dwc(g);
2500 unsigned long flags;
2501 u32 reg;
2502
2503 spin_lock_irqsave(&dwc->lock, flags);
2504 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2505 reg &= ~(DWC3_DCFG_SPEED_MASK);
2506
2507 /*
2508 * WORKAROUND: DWC3 revision < 2.20a have an issue
2509 * which would cause metastability state on Run/Stop
2510 * bit if we try to force the IP to USB2-only mode.
2511 *
2512 * Because of that, we cannot configure the IP to any
2513 * speed other than the SuperSpeed
2514 *
2515 * Refers to:
2516 *
2517 * STAR#9000525659: Clock Domain Crossing on DCTL in
2518 * USB 2.0 Mode
2519 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002520 if (dwc->revision < DWC3_REVISION_220A &&
2521 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002522 reg |= DWC3_DCFG_SUPERSPEED;
2523 } else {
2524 switch (speed) {
2525 case USB_SPEED_LOW:
2526 reg |= DWC3_DCFG_LOWSPEED;
2527 break;
2528 case USB_SPEED_FULL:
2529 reg |= DWC3_DCFG_FULLSPEED;
2530 break;
2531 case USB_SPEED_HIGH:
2532 reg |= DWC3_DCFG_HIGHSPEED;
2533 break;
2534 case USB_SPEED_SUPER:
2535 reg |= DWC3_DCFG_SUPERSPEED;
2536 break;
2537 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002538 if (dwc3_is_usb31(dwc))
2539 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2540 else
2541 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002542 break;
2543 default:
2544 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2545
2546 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2547 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2548 else
2549 reg |= DWC3_DCFG_SUPERSPEED;
2550 }
2551 }
2552 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2553
2554 spin_unlock_irqrestore(&dwc->lock, flags);
2555}
2556
Jack Pham62523502018-01-15 16:37:05 -08002557static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2558{
2559 struct dwc3 *dwc = gadget_to_dwc(g);
2560
2561 dbg_event(0xFF, "RestartUSBSession", 0);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002562 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Jack Pham62523502018-01-15 16:37:05 -08002563}
2564
Felipe Balbi72246da2011-08-19 18:10:58 +03002565static const struct usb_gadget_ops dwc3_gadget_ops = {
2566 .get_frame = dwc3_gadget_get_frame,
2567 .wakeup = dwc3_gadget_wakeup,
Jack Pham62523502018-01-15 16:37:05 -08002568 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002569 .set_selfpowered = dwc3_gadget_set_selfpowered,
Jack Pham62523502018-01-15 16:37:05 -08002570 .vbus_session = dwc3_gadget_vbus_session,
2571 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 .pullup = dwc3_gadget_pullup,
2573 .udc_start = dwc3_gadget_start,
2574 .udc_stop = dwc3_gadget_stop,
Jack Pham62523502018-01-15 16:37:05 -08002575 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002576};
2577
2578/* -------------------------------------------------------------------------- */
2579
Jack Pham62523502018-01-15 16:37:05 -08002580#define NUM_GSI_OUT_EPS 1
2581#define NUM_GSI_IN_EPS 2
2582
2583
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002584static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2585{
2586 struct dwc3 *dwc = dep->dwc;
2587
2588 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2589 dep->endpoint.maxburst = 1;
2590 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2591 if (!dep->direction)
2592 dwc->gadget.ep0 = &dep->endpoint;
2593
2594 dep->endpoint.caps.type_control = true;
2595
2596 return 0;
2597}
2598
Mayank Rana0f67c832018-03-07 14:11:41 -08002599static int dwc3_gadget_init_in_out_endpoint(struct dwc3_ep *dep)
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002600{
2601 struct dwc3 *dwc = dep->dwc;
2602
2603 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2604 dep->endpoint.max_streams = 15;
2605 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2606 list_add_tail(&dep->endpoint.ep_list,
2607 &dwc->gadget.ep_list);
2608 dep->endpoint.caps.type_iso = true;
2609 dep->endpoint.caps.type_bulk = true;
2610 dep->endpoint.caps.type_int = true;
2611
2612 return dwc3_alloc_trb_pool(dep);
2613}
2614
2615static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002616{
2617 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002618 bool direction = epnum & 1;
2619 int ret;
2620 u8 num = epnum >> 1;
2621
2622 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2623 if (!dep)
2624 return -ENOMEM;
2625
2626 dep->dwc = dwc;
2627 dep->number = epnum;
2628 dep->direction = direction;
2629 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2630 dwc->eps[epnum] = dep;
2631
2632 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2633 direction ? "in" : "out");
2634
Hemant Kumar72f784c2018-08-07 16:56:11 -07002635 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002636 dep->endpoint.name = dep->name;
2637
2638 if (!(dep->number > 1)) {
2639 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2640 dep->endpoint.comp_desc = NULL;
2641 }
2642
2643 spin_lock_init(&dep->lock);
2644
2645 if (num == 0)
2646 ret = dwc3_gadget_init_control_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002647 else
Mayank Rana0f67c832018-03-07 14:11:41 -08002648 ret = dwc3_gadget_init_in_out_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002649
2650 if (ret)
2651 return ret;
2652
2653 dep->endpoint.caps.dir_in = direction;
2654 dep->endpoint.caps.dir_out = !direction;
2655
2656 INIT_LIST_HEAD(&dep->pending_list);
2657 INIT_LIST_HEAD(&dep->started_list);
2658
2659 return 0;
2660}
2661
2662static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2663{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002664 u8 epnum;
Jack Pham62523502018-01-15 16:37:05 -08002665 u8 out_count;
2666 u8 in_count;
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002667 u8 idx;
Jack Pham62523502018-01-15 16:37:05 -08002668 struct dwc3_ep *dep;
2669
2670 in_count = out_count = total / 2;
2671 out_count += total & 1; /* in case odd, there is one more OUT */
Felipe Balbi72246da2011-08-19 18:10:58 +03002672
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002673 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2674
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002675 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002676 int ret;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002677 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002678
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002679 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2680 if (ret)
2681 return ret;
Jack Pham62523502018-01-15 16:37:05 -08002682
2683 dep = dwc->eps[epnum];
2684 /* Reserve EPs at the end for GSI */
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002685 if (!dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002686 out_count - NUM_GSI_OUT_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002687 idx = num - (out_count - NUM_GSI_OUT_EPS - 1);
2688 snprintf(dep->name, sizeof(dep->name), "gsi-epout%d",
2689 idx);
Jack Pham62523502018-01-15 16:37:05 -08002690 dep->endpoint.ep_type = EP_TYPE_GSI;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002691 } else if (dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002692 in_count - NUM_GSI_IN_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002693 idx = num - (in_count - NUM_GSI_IN_EPS - 1);
2694 snprintf(dep->name, sizeof(dep->name), "gsi-epin%d",
2695 idx);
Jack Pham62523502018-01-15 16:37:05 -08002696 dep->endpoint.ep_type = EP_TYPE_GSI;
2697 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002698 }
2699
2700 return 0;
2701}
2702
2703static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2704{
2705 struct dwc3_ep *dep;
2706 u8 epnum;
2707
2708 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2709 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002710 if (!dep)
2711 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302712 /*
2713 * Physical endpoints 0 and 1 are special; they form the
2714 * bi-directional USB endpoint 0.
2715 *
2716 * For those two physical endpoints, we don't allocate a TRB
2717 * pool nor do we add them the endpoints list. Due to that, we
2718 * shouldn't do these two operations otherwise we would end up
2719 * with all sorts of bugs when removing dwc3.ko.
2720 */
2721 if (epnum != 0 && epnum != 1) {
2722 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302724 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002725
2726 kfree(dep);
2727 }
2728}
2729
Felipe Balbi72246da2011-08-19 18:10:58 +03002730/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002731
Felipe Balbi8f608e82018-03-27 10:53:29 +03002732static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2733 struct dwc3_request *req, struct dwc3_trb *trb,
2734 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302735{
2736 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302737
Felipe Balbidc55c672016-08-12 13:20:32 +03002738 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002739
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002740 trace_dwc3_complete_trb(dep, trb);
2741
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002742 /*
2743 * If we're in the middle of series of chained TRBs and we
2744 * receive a short transfer along the way, DWC3 will skip
2745 * through all TRBs including the last TRB in the chain (the
2746 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2747 * bit and SW has to do it manually.
2748 *
2749 * We're going to do that here to avoid problems of HW trying
2750 * to use bogus TRBs for transfers.
2751 */
2752 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2753 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2754
Felipe Balbic6267a52017-01-05 14:58:46 +02002755 /*
2756 * If we're dealing with unaligned size OUT transfer, we will be left
2757 * with one TRB pending in the ring. We need to manually clear HWO bit
2758 * from that TRB.
2759 */
Thinh Nguyen4b977512018-08-02 20:17:16 -07002760 if ((req->zero || req->unaligned) && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002761 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2762 return 1;
2763 }
2764
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302765 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002766 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302767
Felipe Balbi35b27192017-03-08 13:56:37 +02002768 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2769 return 1;
2770
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002771 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302772 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002773
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002774 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302775 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002776
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302777 return 0;
2778}
2779
Felipe Balbid3692952018-03-29 13:32:10 +03002780static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2781 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2782 int status)
2783{
2784 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2785 struct scatterlist *sg = req->sg;
2786 struct scatterlist *s;
2787 unsigned int pending = req->num_pending_sgs;
2788 unsigned int i;
2789 int ret = 0;
2790
2791 for_each_sg(sg, s, pending, i) {
2792 trb = &dep->trb_pool[dep->trb_dequeue];
2793
2794 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2795 break;
2796
2797 req->sg = sg_next(s);
2798 req->num_pending_sgs--;
2799
2800 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2801 trb, event, status, true);
2802 if (ret)
2803 break;
2804 }
2805
2806 return ret;
2807}
2808
2809static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2810 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2811 int status)
2812{
2813 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2814
2815 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2816 event, status, false);
2817}
2818
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002819static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2820{
2821 return req->request.actual == req->request.length;
2822}
2823
Felipe Balbif38e35d2018-04-06 15:56:35 +03002824static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2825 const struct dwc3_event_depevt *event,
2826 struct dwc3_request *req, int status)
2827{
2828 int ret;
2829
2830 if (req->num_pending_sgs)
2831 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2832 status);
2833 else
2834 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2835 status);
2836
2837 if (req->unaligned || req->zero) {
2838 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2839 status);
2840 req->unaligned = false;
2841 req->zero = false;
2842 }
2843
2844 req->request.actual = req->request.length - req->remaining;
2845
2846 if (!dwc3_gadget_ep_request_completed(req) &&
2847 req->num_pending_sgs) {
2848 __dwc3_gadget_kick_transfer(dep);
2849 goto out;
2850 }
2851
2852 dwc3_gadget_giveback(dep, req, status);
2853
2854out:
2855 return ret;
2856}
2857
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002858static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002859 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002860{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002861 struct dwc3_request *req;
Felipe Balbi72246da2011-08-19 18:10:58 +03002862
Hemant Kumar7d2436362018-11-16 17:24:39 -08002863 while (!list_empty(&dep->started_list)) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002864 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002865
Hemant Kumar7d2436362018-11-16 17:24:39 -08002866 req = next_request(&dep->started_list);
Felipe Balbif38e35d2018-04-06 15:56:35 +03002867 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2868 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002869 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002871 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002872}
2873
Felipe Balbiee3638b2018-03-27 11:26:53 +03002874static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2875 const struct dwc3_event_depevt *event)
2876{
Felipe Balbif62afb42018-04-11 10:34:34 +03002877 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002878}
2879
Felipe Balbi8f608e82018-03-27 10:53:29 +03002880static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2881 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002882{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002883 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002884 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002885 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002886
Felipe Balbiee3638b2018-03-27 11:26:53 +03002887 dwc3_gadget_endpoint_frame_from_event(dep, event);
2888
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 if (event->status & DEPEVT_STATUS_BUSERR)
2890 status = -ECONNRESET;
2891
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002892 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2893 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002894
2895 if (list_empty(&dep->started_list))
2896 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002897 }
2898
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002899 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002900
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002901 if (stop) {
Jack Pham62523502018-01-15 16:37:05 -08002902 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002903 dep->flags = DWC3_EP_ENABLED;
2904 }
2905
Felipe Balbifae2b902011-10-14 13:00:30 +03002906 /*
2907 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2908 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2909 */
2910 if (dwc->revision < DWC3_REVISION_183A) {
2911 u32 reg;
2912 int i;
2913
2914 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002915 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002916
2917 if (!(dep->flags & DWC3_EP_ENABLED))
2918 continue;
2919
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002920 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002921 return;
2922 }
2923
2924 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2925 reg |= dwc->u1u2;
2926 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2927
2928 dwc->u1u2 = 0;
2929 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002930}
2931
Felipe Balbi8f608e82018-03-27 10:53:29 +03002932static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2933 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002934{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002935 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002936 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002937}
2938
Felipe Balbi72246da2011-08-19 18:10:58 +03002939static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2940 const struct dwc3_event_depevt *event)
2941{
2942 struct dwc3_ep *dep;
2943 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002944 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002945
2946 dep = dwc->eps[epnum];
2947
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002948 if (!(dep->flags & DWC3_EP_ENABLED)) {
2949 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2950 return;
2951
2952 /* Handle only EPCMDCMPLT when EP disabled */
2953 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2954 return;
2955 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002956
Felipe Balbi72246da2011-08-19 18:10:58 +03002957 if (epnum == 0 || epnum == 1) {
2958 dwc3_ep0_interrupt(dwc, event);
2959 return;
2960 }
2961
Jack Phama4c7b782018-01-18 14:19:38 -08002962 dep->dbg_ep_events.total++;
2963
Felipe Balbi72246da2011-08-19 18:10:58 +03002964 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002965 case DWC3_DEPEVT_XFERINPROGRESS:
Jack Phama4c7b782018-01-18 14:19:38 -08002966 dep->dbg_ep_events.xferinprogress++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03002967 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002968 break;
2969 case DWC3_DEPEVT_XFERNOTREADY:
Jack Phama4c7b782018-01-18 14:19:38 -08002970 dep->dbg_ep_events.xfernotready++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03002971 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002972 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002973 case DWC3_DEPEVT_EPCMDCMPLT:
Jack Phama4c7b782018-01-18 14:19:38 -08002974 dep->dbg_ep_events.epcmdcomplete++;
Baolin Wang76a638f2016-10-31 19:38:36 +08002975 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2976
2977 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2978 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2979 wake_up(&dep->wait_end_transfer);
2980 }
2981 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002982 case DWC3_DEPEVT_STREAMEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08002983 dep->dbg_ep_events.streamevent++;
2984 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002985 case DWC3_DEPEVT_XFERCOMPLETE:
Jack Phama4c7b782018-01-18 14:19:38 -08002986 dep->dbg_ep_events.xfercomplete++;
2987 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08002988 case DWC3_DEPEVT_RXTXFIFOEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08002989 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002990 break;
2991 }
2992}
2993
2994static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2995{
2996 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2997 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08002998 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002999 dwc->gadget_driver->disconnect(&dwc->gadget);
3000 spin_lock(&dwc->lock);
3001 }
3002}
3003
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003004static void dwc3_suspend_gadget(struct dwc3 *dwc)
3005{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003006 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003007 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003008 dbg_event(0xFF, "SUSPEND", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003009 dwc->gadget_driver->suspend(&dwc->gadget);
3010 spin_lock(&dwc->lock);
3011 }
3012}
3013
3014static void dwc3_resume_gadget(struct dwc3 *dwc)
3015{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003016 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003017 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003018 dbg_event(0xFF, "RESUME", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003019 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003020 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003021 }
3022}
3023
3024static void dwc3_reset_gadget(struct dwc3 *dwc)
3025{
3026 if (!dwc->gadget_driver)
3027 return;
3028
3029 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
3030 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003031 dbg_event(0xFF, "UDC RESET", 0);
Felipe Balbi8e744752014-11-06 14:27:53 +08003032 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003033 spin_lock(&dwc->lock);
3034 }
3035}
3036
Jack Pham62523502018-01-15 16:37:05 -08003037void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03003038{
Jack Pham62523502018-01-15 16:37:05 -08003039 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003040 struct dwc3_gadget_ep_cmd_params params;
3041 u32 cmd;
3042 int ret;
3043
Jack Pham62523502018-01-15 16:37:05 -08003044 dep = dwc->eps[epnum];
3045
Baolin Wang76a638f2016-10-31 19:38:36 +08003046 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
3047 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303048 return;
3049
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003050 if (dep->endpoint.endless)
3051 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
3052 dep->number);
3053
Pratyush Anand57911502012-07-06 15:19:10 +05303054 /*
3055 * NOTICE: We are violating what the Databook says about the
3056 * EndTransfer command. Ideally we would _always_ wait for the
3057 * EndTransfer Command Completion IRQ, but that's causing too
3058 * much trouble synchronizing between us and gadget driver.
3059 *
3060 * We have discussed this with the IP Provider and it was
3061 * suggested to giveback all requests here, but give HW some
3062 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01003063 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05303064 *
3065 * Note also that a similar handling was tested by Synopsys
3066 * (thanks a lot Paul) and nothing bad has come out of it.
3067 * In short, what we're doing is:
3068 *
3069 * - Issue EndTransfer WITH CMDIOC bit set
3070 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07003071 *
3072 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3073 * supports a mode to work around the above limitation. The
3074 * software can poll the CMDACT bit in the DEPCMD register
3075 * after issuing a EndTransfer command. This mode is enabled
3076 * by writing GUCTL2[14]. This polling is already done in the
3077 * dwc3_send_gadget_ep_cmd() function so if the mode is
3078 * enabled, the EndTransfer command will have completed upon
3079 * returning from this function and we don't need to delay for
3080 * 100us.
3081 *
3082 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303083 */
3084
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303085 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003086 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3087 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03003088 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303089 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003090 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303091 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003092 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003093
Baolin Wang76a638f2016-10-31 19:38:36 +08003094 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
Mayank Rana8bd2a062018-06-28 11:08:52 -07003095 if (dep->endpoint.ep_type != EP_TYPE_GSI)
3096 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07003097 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08003098 }
Mayank Rana21f76b32018-09-21 16:49:55 -07003099 dbg_log_string("%s(%d): endxfer ret:%d)",
3100 dep->name, dep->number, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003101}
3102
Felipe Balbi72246da2011-08-19 18:10:58 +03003103static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3104{
3105 u32 epnum;
3106
3107 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3108 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003109 int ret;
3110
3111 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003112 if (!dep)
3113 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003114
3115 if (!(dep->flags & DWC3_EP_STALL))
3116 continue;
3117
3118 dep->flags &= ~DWC3_EP_STALL;
3119
John Youn50c763f2016-05-31 17:49:56 -07003120 ret = dwc3_send_clear_stall_ep_cmd(dep);
Jack Pham62523502018-01-15 16:37:05 -08003121 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003122 WARN_ON_ONCE(ret);
3123 }
3124}
3125
3126static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3127{
Felipe Balbic4430a22012-05-24 10:30:01 +03003128 int reg;
3129
Jack Pham62523502018-01-15 16:37:05 -08003130 dbg_event(0xFF, "DISCONNECT INT", 0);
3131 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3132 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003133 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003134
Felipe Balbi72246da2011-08-19 18:10:58 +03003135 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3136 reg &= ~DWC3_DCTL_INITU1ENA;
3137 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3138
3139 reg &= ~DWC3_DCTL_INITU2ENA;
3140 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003141
Felipe Balbi72246da2011-08-19 18:10:58 +03003142 dwc3_disconnect_gadget(dwc);
3143
3144 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003145 dwc->setup_packet_pending = false;
Jack Pham62523502018-01-15 16:37:05 -08003146 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05003147 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003148
3149 dwc->connected = false;
Jack Pham62523502018-01-15 16:37:05 -08003150 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003151}
3152
Felipe Balbi72246da2011-08-19 18:10:58 +03003153static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3154{
3155 u32 reg;
3156
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003157 usb_phy_start_link_training(dwc->usb3_phy);
3158
Felipe Balbifc8bb912016-05-16 13:14:48 +03003159 dwc->connected = true;
3160
Felipe Balbidf62df52011-10-14 15:11:49 +03003161 /*
3162 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3163 * would cause a missing Disconnect Event if there's a
3164 * pending Setup Packet in the FIFO.
3165 *
3166 * There's no suggested workaround on the official Bug
3167 * report, which states that "unless the driver/application
3168 * is doing any special handling of a disconnect event,
3169 * there is no functional issue".
3170 *
3171 * Unfortunately, it turns out that we _do_ some special
3172 * handling of a disconnect event, namely complete all
3173 * pending transfers, notify gadget driver of the
3174 * disconnection, and so on.
3175 *
3176 * Our suggested workaround is to follow the Disconnect
3177 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003178 * flag. Such flag gets set whenever we have a SETUP_PENDING
3179 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003180 * same endpoint.
3181 *
3182 * Refers to:
3183 *
3184 * STAR#9000466709: RTL: Device : Disconnect event not
3185 * generated if setup packet pending in FIFO
3186 */
3187 if (dwc->revision < DWC3_REVISION_188A) {
3188 if (dwc->setup_packet_pending)
3189 dwc3_gadget_disconnect_interrupt(dwc);
3190 }
3191
Jack Pham62523502018-01-15 16:37:05 -08003192 dbg_event(0xFF, "BUS RESET", 0);
3193 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3194 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003195 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003196
3197 usb_gadget_vbus_draw(&dwc->gadget, 100);
3198
Felipe Balbi8e744752014-11-06 14:27:53 +08003199 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003200
3201 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3202 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3203 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003204 dwc->test_mode = false;
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303205 /*
3206 * From SNPS databook section 8.1.2
3207 * the EP0 should be in setup phase. So ensure
3208 * that EP0 is in setup phase by issuing a stall
3209 * and restart if EP0 is not in setup phase.
3210 */
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303211 if (dwc->ep0state != EP0_SETUP_PHASE) {
3212 unsigned int dir;
3213
3214 dbg_event(0xFF, "CONTRPEND", dwc->ep0state);
3215 dir = !!dwc->ep0_expect_in;
3216 if (dwc->ep0state == EP0_DATA_PHASE)
3217 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
3218 else
3219 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303220 dwc3_ep0_stall_and_restart(dwc);
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303221 }
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303222
3223 dwc3_stop_active_transfers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003224 dwc3_clear_stall_all_ep(dwc);
3225
3226 /* Reset device address to zero */
3227 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3228 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3229 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Jack Pham62523502018-01-15 16:37:05 -08003230
3231 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3232 dwc->link_state = DWC3_LINK_STATE_U0;
3233 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003234}
3235
Felipe Balbi72246da2011-08-19 18:10:58 +03003236static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3237{
Felipe Balbi72246da2011-08-19 18:10:58 +03003238 struct dwc3_ep *dep;
3239 int ret;
3240 u32 reg;
3241 u8 speed;
3242
Jack Pham62523502018-01-15 16:37:05 -08003243 dbg_event(0xFF, "CONNECT DONE", 0);
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003244 usb_phy_stop_link_training(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +03003245 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3246 speed = reg & DWC3_DSTS_CONNECTSPD;
3247 dwc->speed = speed;
3248
John Youn5fb6fda2016-11-10 17:23:25 -08003249 /*
3250 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3251 * each time on Connect Done.
3252 *
3253 * Currently we always use the reset value. If any platform
3254 * wants to set this to a different value, we need to add a
3255 * setting and update GCTL.RAMCLKSEL here.
3256 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003257
3258 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003259 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003260 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3261 dwc->gadget.ep0->maxpacket = 512;
3262 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3263 break;
John Youn2da9ad72016-05-20 16:34:26 -07003264 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003265 /*
3266 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3267 * would cause a missing USB3 Reset event.
3268 *
3269 * In such situations, we should force a USB3 Reset
3270 * event by calling our dwc3_gadget_reset_interrupt()
3271 * routine.
3272 *
3273 * Refers to:
3274 *
3275 * STAR#9000483510: RTL: SS : USB3 reset event may
3276 * not be generated always when the link enters poll
3277 */
3278 if (dwc->revision < DWC3_REVISION_190A)
3279 dwc3_gadget_reset_interrupt(dwc);
3280
Felipe Balbi72246da2011-08-19 18:10:58 +03003281 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3282 dwc->gadget.ep0->maxpacket = 512;
3283 dwc->gadget.speed = USB_SPEED_SUPER;
3284 break;
John Youn2da9ad72016-05-20 16:34:26 -07003285 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003286 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3287 dwc->gadget.ep0->maxpacket = 64;
3288 dwc->gadget.speed = USB_SPEED_HIGH;
3289 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003290 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003291 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3292 dwc->gadget.ep0->maxpacket = 64;
3293 dwc->gadget.speed = USB_SPEED_FULL;
3294 break;
John Youn2da9ad72016-05-20 16:34:26 -07003295 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003296 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3297 dwc->gadget.ep0->maxpacket = 8;
3298 dwc->gadget.speed = USB_SPEED_LOW;
3299 break;
3300 }
3301
Thinh Nguyen61800262018-01-12 18:18:05 -08003302 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3303
Pratyush Anand2b758352013-01-14 15:59:31 +05303304 /* Enable USB2 LPM Capability */
3305
John Younee5cd412016-02-05 17:08:45 -08003306 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003307 (speed != DWC3_DSTS_SUPERSPEED) &&
3308 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303309 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3310 reg |= DWC3_DCFG_LPM_CAP;
3311 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3312
3313 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3314 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3315
Huang Rui460d0982014-10-31 11:11:18 +08003316 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303317
Huang Rui80caf7d2014-10-28 19:54:26 +08003318 /*
3319 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3320 * DCFG.LPMCap is set, core responses with an ACK and the
3321 * BESL value in the LPM token is less than or equal to LPM
3322 * NYET threshold.
3323 */
3324 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3325 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003326 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003327
3328 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3329 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3330
Pratyush Anand2b758352013-01-14 15:59:31 +05303331 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003332 } else {
3333 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3334 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3335 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303336 }
3337
Felipe Balbi72246da2011-08-19 18:10:58 +03003338 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003339 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003340 if (ret) {
3341 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3342 return;
3343 }
3344
3345 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003346 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003347 if (ret) {
3348 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3349 return;
3350 }
3351
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003352 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003353
Felipe Balbi72246da2011-08-19 18:10:58 +03003354 /*
3355 * Configure PHY via GUSB3PIPECTLn if required.
3356 *
3357 * Update GTXFIFOSIZn
3358 *
3359 * In both cases reset values should be sufficient.
3360 */
3361}
3362
Jack Pham62523502018-01-15 16:37:05 -08003363static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003364{
Jack Pham62523502018-01-15 16:37:05 -08003365 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003366
Jack Pham62523502018-01-15 16:37:05 -08003367 dev_dbg(dwc->dev, "%s\n", __func__);
3368
3369 dbg_event(0xFF, "WAKEUP", remote_wakeup);
3370 /*
3371 * Identify if it is called from wakeup_interrupt() context for bus
3372 * resume or as part of remote wakeup. And based on that check for
3373 * U3 state. as we need to handle case of L1 resume i.e. where we
3374 * don't want to perform resume.
3375 */
3376 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3377 perform_resume = false;
3378
3379 /* Only perform resume from L2 or Early Suspend states */
3380 if (perform_resume) {
3381
3382 /*
3383 * In case of remote wake up dwc3_gadget_wakeup_work()
3384 * is doing pm_runtime_get_sync().
3385 */
3386 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3387 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003388 dwc3_notify_event(dwc,
3389 DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003390
3391 /*
3392 * set state to U0 as function level resume is trying to queue
3393 * notification over USB interrupt endpoint which would fail
3394 * due to state is not being updated.
3395 */
3396 dwc->link_state = DWC3_LINK_STATE_U0;
3397 dwc3_resume_gadget(dwc);
3398 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003399 }
Jack Pham62523502018-01-15 16:37:05 -08003400
3401 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003402}
3403
3404static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3405 unsigned int evtinfo)
3406{
Felipe Balbifae2b902011-10-14 13:00:30 +03003407 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003408 unsigned int pwropt;
3409
3410 /*
3411 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3412 * Hibernation mode enabled which would show up when device detects
3413 * host-initiated U3 exit.
3414 *
3415 * In that case, device will generate a Link State Change Interrupt
3416 * from U3 to RESUME which is only necessary if Hibernation is
3417 * configured in.
3418 *
3419 * There are no functional changes due to such spurious event and we
3420 * just need to ignore it.
3421 *
3422 * Refers to:
3423 *
3424 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3425 * operational mode
3426 */
3427 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3428 if ((dwc->revision < DWC3_REVISION_250A) &&
3429 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3430 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3431 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003432 return;
3433 }
3434 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003435
3436 /*
3437 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3438 * on the link partner, the USB session might do multiple entry/exit
3439 * of low power states before a transfer takes place.
3440 *
3441 * Due to this problem, we might experience lower throughput. The
3442 * suggested workaround is to disable DCTL[12:9] bits if we're
3443 * transitioning from U1/U2 to U0 and enable those bits again
3444 * after a transfer completes and there are no pending transfers
3445 * on any of the enabled endpoints.
3446 *
3447 * This is the first half of that workaround.
3448 *
3449 * Refers to:
3450 *
3451 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3452 * core send LGO_Ux entering U0
3453 */
3454 if (dwc->revision < DWC3_REVISION_183A) {
3455 if (next == DWC3_LINK_STATE_U0) {
3456 u32 u1u2;
3457 u32 reg;
3458
3459 switch (dwc->link_state) {
3460 case DWC3_LINK_STATE_U1:
3461 case DWC3_LINK_STATE_U2:
3462 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3463 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3464 | DWC3_DCTL_ACCEPTU2ENA
3465 | DWC3_DCTL_INITU1ENA
3466 | DWC3_DCTL_ACCEPTU1ENA);
3467
3468 if (!dwc->u1u2)
3469 dwc->u1u2 = reg & u1u2;
3470
3471 reg &= ~u1u2;
3472
3473 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3474 break;
3475 default:
3476 /* do nothing */
3477 break;
3478 }
3479 }
3480 }
3481
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003482 switch (next) {
3483 case DWC3_LINK_STATE_U1:
3484 if (dwc->speed == USB_SPEED_SUPER)
3485 dwc3_suspend_gadget(dwc);
3486 break;
3487 case DWC3_LINK_STATE_U2:
3488 case DWC3_LINK_STATE_U3:
3489 dwc3_suspend_gadget(dwc);
3490 break;
3491 case DWC3_LINK_STATE_RESUME:
3492 dwc3_resume_gadget(dwc);
3493 break;
3494 default:
3495 /* do nothing */
3496 break;
3497 }
3498
Jack Pham62523502018-01-15 16:37:05 -08003499 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003500 dwc->link_state = next;
Jack Pham62523502018-01-15 16:37:05 -08003501 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003502}
3503
Baolin Wang72704f82016-05-16 16:43:53 +08003504static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3505 unsigned int evtinfo)
3506{
3507 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3508
Jack Pham62523502018-01-15 16:37:05 -08003509 dbg_event(0xFF, "SUSPEND INT", 0);
3510 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3511
3512 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3513 /*
3514 * When first connecting the cable, even before the initial
3515 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3516 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3517 * event. In such a case, ignore.
3518 * Ignore suspend event until device side usb is not into
3519 * CONFIGURED state.
3520 */
3521 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3522 dev_err(dwc->dev, "%s(): state:%d. Ignore SUSPEND.\n",
3523 __func__, dwc->gadget.state);
3524 return;
3525 }
3526
Baolin Wang72704f82016-05-16 16:43:53 +08003527 dwc3_suspend_gadget(dwc);
3528
Jack Pham62523502018-01-15 16:37:05 -08003529 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3530 dwc->b_suspend = true;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003531 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003532 }
3533
Baolin Wang72704f82016-05-16 16:43:53 +08003534 dwc->link_state = next;
3535}
3536
Felipe Balbie1dadd32014-02-25 14:47:54 -06003537static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3538 unsigned int evtinfo)
3539{
3540 unsigned int is_ss = evtinfo & BIT(4);
3541
Felipe Balbibfad65e2017-04-19 14:59:27 +03003542 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003543 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3544 * have a known issue which can cause USB CV TD.9.23 to fail
3545 * randomly.
3546 *
3547 * Because of this issue, core could generate bogus hibernation
3548 * events which SW needs to ignore.
3549 *
3550 * Refers to:
3551 *
3552 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3553 * Device Fallback from SuperSpeed
3554 */
3555 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3556 return;
3557
3558 /* enter hibernation here */
3559}
3560
Felipe Balbi72246da2011-08-19 18:10:58 +03003561static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3562 const struct dwc3_event_devt *event)
3563{
3564 switch (event->type) {
3565 case DWC3_DEVICE_EVENT_DISCONNECT:
3566 dwc3_gadget_disconnect_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003567 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003568 break;
3569 case DWC3_DEVICE_EVENT_RESET:
3570 dwc3_gadget_reset_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003571 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003572 break;
3573 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3574 dwc3_gadget_conndone_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003575 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003576 break;
3577 case DWC3_DEVICE_EVENT_WAKEUP:
Jack Pham62523502018-01-15 16:37:05 -08003578 dwc3_gadget_wakeup_interrupt(dwc, false);
Jack Phama4c7b782018-01-18 14:19:38 -08003579 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003580 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003581 case DWC3_DEVICE_EVENT_HIBER_REQ:
3582 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3583 "unexpected hibernation event\n"))
3584 break;
3585
3586 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3587 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003588 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3589 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Jack Phama4c7b782018-01-18 14:19:38 -08003590 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003591 break;
3592 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003593 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003594 if (dwc->revision >= DWC3_REVISION_230A) {
Jack Pham62523502018-01-15 16:37:05 -08003595 dbg_event(0xFF, "GAD SUS", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003596 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003597 /*
3598 * Ignore suspend event until the gadget enters into
3599 * USB_STATE_CONFIGURED state.
3600 */
3601 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3602 dwc3_gadget_suspend_interrupt(dwc,
3603 event->event_info);
Jack Pham51c5d1f2018-11-02 17:46:17 -07003604 else
3605 usb_gadget_vbus_draw(&dwc->gadget, 2);
Baolin Wang72704f82016-05-16 16:43:53 +08003606 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003607 break;
3608 case DWC3_DEVICE_EVENT_SOF:
Jack Phama4c7b782018-01-18 14:19:38 -08003609 dwc->dbg_gadget_events.sof++;
3610 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003611 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Jack Pham62523502018-01-15 16:37:05 -08003612 dbg_event(0xFF, "ERROR", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003613 dwc->dbg_gadget_events.erratic_error++;
Jack Pham62523502018-01-15 16:37:05 -08003614 dwc->err_evt_seen = true;
Jack Phama4c7b782018-01-18 14:19:38 -08003615 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003616 case DWC3_DEVICE_EVENT_CMD_CMPL:
Jack Phama4c7b782018-01-18 14:19:38 -08003617 dwc->dbg_gadget_events.cmdcmplt++;
3618 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003619 case DWC3_DEVICE_EVENT_OVERFLOW:
Jack Phama4c7b782018-01-18 14:19:38 -08003620 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003621 break;
3622 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003623 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Jack Phama4c7b782018-01-18 14:19:38 -08003624 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003625 }
3626}
3627
3628static void dwc3_process_event_entry(struct dwc3 *dwc,
3629 const union dwc3_event *event)
3630{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003631 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003632
Felipe Balbidfc5e802017-04-26 13:44:51 +03003633 if (!event->type.is_devspec)
3634 dwc3_endpoint_interrupt(dwc, &event->depevt);
3635 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003636 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003637 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003638 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003639}
3640
Felipe Balbidea520a2016-03-30 09:39:34 +03003641static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003642{
Felipe Balbidea520a2016-03-30 09:39:34 +03003643 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003644 irqreturn_t ret = IRQ_NONE;
3645 int left;
3646 u32 reg;
3647
Felipe Balbif42f2442013-06-12 21:25:08 +03003648 left = evt->count;
3649
3650 if (!(evt->flags & DWC3_EVENT_PENDING))
3651 return IRQ_NONE;
3652
3653 while (left > 0) {
3654 union dwc3_event event;
3655
John Younebbb2d52016-11-15 13:07:02 +02003656 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003657
3658 dwc3_process_event_entry(dwc, &event);
3659
Jack Pham62523502018-01-15 16:37:05 -08003660 if (dwc->err_evt_seen) {
3661 /*
3662 * if erratic error, skip remaining events
3663 * while controller undergoes reset
3664 */
3665 evt->lpos = (evt->lpos + left) %
3666 DWC3_EVENT_BUFFERS_SIZE;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003667 if (dwc3_notify_event(dwc,
3668 DWC3_CONTROLLER_ERROR_EVENT, 0))
Jack Pham62523502018-01-15 16:37:05 -08003669 dwc->err_evt_seen = 0;
3670 break;
3671 }
3672
Felipe Balbif42f2442013-06-12 21:25:08 +03003673 /*
3674 * FIXME we wrap around correctly to the next entry as
3675 * almost all entries are 4 bytes in size. There is one
3676 * entry which has 12 bytes which is a regular entry
3677 * followed by 8 bytes data. ATM I don't know how
3678 * things are organized if we get next to the a
3679 * boundary so I worry about that once we try to handle
3680 * that.
3681 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003682 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003683 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003684 }
3685
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003686 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003687 evt->count = 0;
3688 evt->flags &= ~DWC3_EVENT_PENDING;
3689 ret = IRQ_HANDLED;
3690
3691 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003692 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003693 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003694 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003695
John Youncf40b862016-11-14 12:32:43 -08003696 if (dwc->imod_interval) {
3697 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3698 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3699 }
3700
Felipe Balbif42f2442013-06-12 21:25:08 +03003701 return ret;
3702}
3703
Jack Pham62523502018-01-15 16:37:05 -08003704void dwc3_bh_work(struct work_struct *w)
3705{
3706 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3707
3708 pm_runtime_get_sync(dwc->dev);
3709 dwc3_thread_interrupt(dwc->irq, dwc->ev_buf);
3710 pm_runtime_put(dwc->dev);
3711}
3712
Felipe Balbidea520a2016-03-30 09:39:34 +03003713static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003714{
Felipe Balbidea520a2016-03-30 09:39:34 +03003715 struct dwc3_event_buffer *evt = _evt;
3716 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003717 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003718 irqreturn_t ret = IRQ_NONE;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003719 ktime_t start_time;
3720
3721 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003722
Felipe Balbie5f68b42015-10-12 13:25:44 -05003723 spin_lock_irqsave(&dwc->lock, flags);
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003724 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] = 0;
Felipe Balbidea520a2016-03-30 09:39:34 +03003725 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05003726 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003727
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003728 dwc->bh_completion_time[dwc->irq_dbg_index] =
3729 ktime_to_us(ktime_sub(ktime_get(), start_time));
3730 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3731
Felipe Balbib15a7622011-06-30 16:57:15 +03003732 return ret;
3733}
3734
Felipe Balbidea520a2016-03-30 09:39:34 +03003735static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003736{
Felipe Balbidea520a2016-03-30 09:39:34 +03003737 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003738 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003739 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003740 u32 reg;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003741 ktime_t start_time;
3742
3743 start_time = ktime_get();
3744 dwc->irq_cnt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003745
Jack Pham62523502018-01-15 16:37:05 -08003746 /* controller reset is still pending */
3747 if (dwc->err_evt_seen)
Felipe Balbifc8bb912016-05-16 13:14:48 +03003748 return IRQ_HANDLED;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003749
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003750 /*
3751 * With PCIe legacy interrupt, test shows that top-half irq handler can
3752 * be called again after HW interrupt deassertion. Check if bottom-half
3753 * irq event handler completes before caching new event to prevent
3754 * losing events.
3755 */
3756 if (evt->flags & DWC3_EVENT_PENDING)
3757 return IRQ_HANDLED;
3758
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003759 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003760 count &= DWC3_GEVNTCOUNT_MASK;
3761 if (!count)
3762 return IRQ_NONE;
3763
Felipe Balbib15a7622011-06-30 16:57:15 +03003764 evt->count = count;
3765 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003766
Felipe Balbie8adfc32013-06-12 21:11:14 +03003767 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003768 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003769 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003770 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003771
John Younebbb2d52016-11-15 13:07:02 +02003772 amount = min(count, evt->length - evt->lpos);
3773 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3774
3775 if (amount < count)
3776 memcpy(evt->cache, evt->buf, count - amount);
3777
John Youn65aca322016-11-15 13:08:59 +02003778 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3779
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003780 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3781 dwc->irq_completion_time[dwc->irq_dbg_index] =
3782 ktime_us_delta(ktime_get(), start_time);
3783 dwc->irq_event_count[dwc->irq_dbg_index] = count / 4;
3784 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3785
Felipe Balbib15a7622011-06-30 16:57:15 +03003786 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003787}
3788
Jack Pham62523502018-01-15 16:37:05 -08003789irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003790{
Jack Pham62523502018-01-15 16:37:05 -08003791 struct dwc3 *dwc = _dwc;
3792 irqreturn_t ret = IRQ_NONE;
3793 irqreturn_t status;
Felipe Balbi72246da2011-08-19 18:10:58 +03003794
Jack Pham62523502018-01-15 16:37:05 -08003795 status = dwc3_check_event_buf(dwc->ev_buf);
3796 if (status == IRQ_WAKE_THREAD)
3797 ret = status;
3798
3799 if (ret == IRQ_WAKE_THREAD)
3800 queue_work(dwc->dwc_wq, &dwc->bh_work);
3801
3802 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003803}
3804
Felipe Balbi6db38122016-10-03 11:27:01 +03003805static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3806{
3807 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3808 int irq;
3809
3810 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3811 if (irq > 0)
3812 goto out;
3813
3814 if (irq == -EPROBE_DEFER)
3815 goto out;
3816
3817 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3818 if (irq > 0)
3819 goto out;
3820
3821 if (irq == -EPROBE_DEFER)
3822 goto out;
3823
3824 irq = platform_get_irq(dwc3_pdev, 0);
3825 if (irq > 0)
3826 goto out;
3827
3828 if (irq != -EPROBE_DEFER)
3829 dev_err(dwc->dev, "missing peripheral IRQ\n");
3830
3831 if (!irq)
3832 irq = -EINVAL;
3833
3834out:
3835 return irq;
3836}
3837
Felipe Balbi72246da2011-08-19 18:10:58 +03003838/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003839 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003840 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003841 *
3842 * Returns 0 on success otherwise negative errno.
3843 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003844int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003845{
Felipe Balbi6db38122016-10-03 11:27:01 +03003846 int ret;
3847 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003848
Felipe Balbi6db38122016-10-03 11:27:01 +03003849 irq = dwc3_gadget_get_irq(dwc);
3850 if (irq < 0) {
3851 ret = irq;
3852 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003853 }
3854
3855 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003856
Jack Pham62523502018-01-15 16:37:05 -08003857 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3858
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303859 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3860 sizeof(*dwc->ep0_trb) * 2,
3861 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003862 if (!dwc->ep0_trb) {
3863 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3864 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003865 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003866 }
3867
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003868 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003869 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003870 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003871 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003872 }
3873
Felipe Balbi905dc042017-01-05 14:46:52 +02003874 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3875 &dwc->bounce_addr, GFP_KERNEL);
3876 if (!dwc->bounce) {
3877 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003878 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003879 }
3880
Baolin Wangbb014732016-10-14 17:11:33 +08003881 init_completion(&dwc->ep0_in_setup);
3882
Mayank Rana0f67c832018-03-07 14:11:41 -08003883 dwc->gadget.ops = &dwc3_gadget_ops;
3884 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3885 dwc->gadget.sg_supported = true;
3886 dwc->gadget.name = "dwc3-gadget";
3887 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003888
3889 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003890 * FIXME We might be setting max_speed to <SUPER, however versions
3891 * <2.20a of dwc3 have an issue with metastability (documented
3892 * elsewhere in this driver) which tells us we can't set max speed to
3893 * anything lower than SUPER.
3894 *
3895 * Because gadget.max_speed is only used by composite.c and function
3896 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3897 * to happen so we avoid sending SuperSpeed Capability descriptor
3898 * together with our BOS descriptor as that could confuse host into
3899 * thinking we can handle super speed.
3900 *
3901 * Note that, in fact, we won't even support GetBOS requests when speed
3902 * is less than super speed because we don't have means, yet, to tell
3903 * composite.c that we are USB 2.0 + LPM ECN.
3904 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003905 if (dwc->revision < DWC3_REVISION_220A &&
3906 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003907 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003908 dwc->revision);
3909
3910 dwc->gadget.max_speed = dwc->maximum_speed;
3911
3912 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003913 * REVISIT: Here we should clear all pending IRQs to be
3914 * sure we're starting from a well known location.
3915 */
3916
Mayank Rana0f67c832018-03-07 14:11:41 -08003917 dwc->num_eps = DWC3_ENDPOINTS_NUM;
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003918 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003919 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003920 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003921
Felipe Balbi72246da2011-08-19 18:10:58 +03003922 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3923 if (ret) {
3924 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003925 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003926 }
3927
3928 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003929
3930err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003931 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003932
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003933err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003934 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3935 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003936
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003937err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003938 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003939
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003940err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303941 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003942 dwc->ep0_trb, dwc->ep0_trb_addr);
3943
Felipe Balbi72246da2011-08-19 18:10:58 +03003944err0:
3945 return ret;
3946}
3947
Felipe Balbi7415f172012-04-30 14:56:33 +03003948/* -------------------------------------------------------------------------- */
3949
Felipe Balbi72246da2011-08-19 18:10:58 +03003950void dwc3_gadget_exit(struct dwc3 *dwc)
3951{
Felipe Balbi72246da2011-08-19 18:10:58 +03003952 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003953 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003954 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003955 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003956 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303957 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003958 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003959}
Felipe Balbi7415f172012-04-30 14:56:33 +03003960
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003961int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003962{
Roger Quadros9772b472016-04-12 11:33:29 +03003963 if (!dwc->gadget_driver)
3964 return 0;
3965
Roger Quadros1551e352017-02-15 14:16:26 +02003966 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003967 dwc3_disconnect_gadget(dwc);
3968 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003969
Bo He03a5d4d2019-01-14 09:48:32 +02003970 synchronize_irq(dwc->irq_gadget);
3971
Felipe Balbi7415f172012-04-30 14:56:33 +03003972 return 0;
3973}
3974
3975int dwc3_gadget_resume(struct dwc3 *dwc)
3976{
Felipe Balbi7415f172012-04-30 14:56:33 +03003977 int ret;
3978
Roger Quadros9772b472016-04-12 11:33:29 +03003979 if (!dwc->gadget_driver)
3980 return 0;
3981
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003982 ret = __dwc3_gadget_start(dwc);
3983 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003984 goto err0;
3985
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003986 ret = dwc3_gadget_run_stop(dwc, true, false);
3987 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003988 goto err1;
3989
Felipe Balbi7415f172012-04-30 14:56:33 +03003990 return 0;
3991
3992err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003993 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003994
3995err0:
3996 return ret;
3997}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003998
3999void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4000{
4001 if (dwc->pending_events) {
4002 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4003 dwc->pending_events = false;
4004 enable_irq(dwc->irq_gadget);
4005 }
4006}