blob: fe1a288bf06d3356ae809500cc9f6738e031e41c [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
Hemant Kumar7b7e0652019-04-09 18:00:55 -0700871static void dwc3_stop_active_transfers_to_halt(struct dwc3 *dwc)
872{
873 u32 epnum;
874 struct dwc3_request *req;
875 struct dwc3_ep *dep;
876
877 dbg_log_string("START");
878 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
879 dep = dwc->eps[epnum];
880 if (!dep)
881 continue;
882
883 if (!(dep->flags & DWC3_EP_ENABLED))
884 continue;
885
886 dwc3_stop_active_transfer_noioc(dwc, dep->number, true);
887
888 /* - giveback all requests to gadget driver */
889 while (!list_empty(&dep->started_list)) {
890 req = next_request(&dep->started_list);
891 if (req)
892 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
893 }
894
895 while (!list_empty(&dep->pending_list)) {
896 req = next_request(&dep->pending_list);
897 if (req)
898 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
899 }
900 }
901
902 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_CLEAR, 0);
903
904 dbg_log_string("DONE");
905}
906
Felipe Balbi72246da2011-08-19 18:10:58 +0300907/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300908 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300909 * @dep: the endpoint to disable
910 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300911 * This function undoes what __dwc3_gadget_ep_enable did and also removes
912 * requests which are currently being processed by the hardware and those which
913 * are not yet scheduled.
914 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200915 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300916 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300917static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
918{
919 struct dwc3 *dwc = dep->dwc;
920 u32 reg;
921
Felipe Balbi2870e502016-11-03 13:53:29 +0200922 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500923
Jack Pham62523502018-01-15 16:37:05 -0800924 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
925 dwc3_remove_requests(dwc, dep);
926 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
927 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300928
Felipe Balbi687ef982014-04-16 10:30:33 -0500929 /* make sure HW endpoint isn't stalled */
930 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500931 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500932
Felipe Balbi72246da2011-08-19 18:10:58 +0300933 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
934 reg &= ~DWC3_DALEPENA_EP(dep->number);
935 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
936
Felipe Balbi879631a2011-09-30 10:58:47 +0300937 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300938 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800939 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300940
John Youn39ebb052016-11-09 16:36:28 -0800941 /* Clear out the ep descriptors for non-ep0 */
942 if (dep->number > 1) {
943 dep->endpoint.comp_desc = NULL;
944 dep->endpoint.desc = NULL;
945 }
946
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 return 0;
948}
949
950/* -------------------------------------------------------------------------- */
951
952static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
953 const struct usb_endpoint_descriptor *desc)
954{
955 return -EINVAL;
956}
957
958static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
959{
960 return -EINVAL;
961}
962
963/* -------------------------------------------------------------------------- */
964
965static int dwc3_gadget_ep_enable(struct usb_ep *ep,
966 const struct usb_endpoint_descriptor *desc)
967{
968 struct dwc3_ep *dep;
969 struct dwc3 *dwc;
970 unsigned long flags;
971 int ret;
972
973 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
974 pr_debug("dwc3: invalid parameters\n");
975 return -EINVAL;
976 }
977
978 if (!desc->wMaxPacketSize) {
979 pr_debug("dwc3: missing wMaxPacketSize\n");
980 return -EINVAL;
981 }
982
983 dep = to_dwc3_ep(ep);
984 dwc = dep->dwc;
985
Felipe Balbi95ca9612015-12-10 13:08:20 -0600986 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
987 "%s is already enabled\n",
988 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300989 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300990
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300992 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Jack Pham62523502018-01-15 16:37:05 -0800993 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 spin_unlock_irqrestore(&dwc->lock, flags);
995
996 return ret;
997}
998
999static int dwc3_gadget_ep_disable(struct usb_ep *ep)
1000{
1001 struct dwc3_ep *dep;
1002 struct dwc3 *dwc;
1003 unsigned long flags;
1004 int ret;
1005
1006 if (!ep) {
1007 pr_debug("dwc3: invalid parameters\n");
1008 return -EINVAL;
1009 }
1010
1011 dep = to_dwc3_ep(ep);
1012 dwc = dep->dwc;
1013
Felipe Balbi95ca9612015-12-10 13:08:20 -06001014 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
1015 "%s is already disabled\n",
1016 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +03001017 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001018
Felipe Balbi72246da2011-08-19 18:10:58 +03001019 spin_lock_irqsave(&dwc->lock, flags);
1020 ret = __dwc3_gadget_ep_disable(dep);
Jack Pham62523502018-01-15 16:37:05 -08001021 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001022 spin_unlock_irqrestore(&dwc->lock, flags);
1023
1024 return ret;
1025}
1026
1027static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001028 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +03001029{
1030 struct dwc3_request *req;
1031 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001032
1033 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +09001034 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001036
Felipe Balbi31a2f5a2018-05-07 15:19:31 +03001037 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001038 req->epnum = dep->number;
1039 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001040
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001041 trace_dwc3_alloc_request(req);
1042
Felipe Balbi72246da2011-08-19 18:10:58 +03001043 return &req->request;
1044}
1045
1046static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1047 struct usb_request *request)
1048{
1049 struct dwc3_request *req = to_dwc3_request(request);
1050
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001051 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001052 kfree(req);
1053}
1054
Felipe Balbi42626912018-04-09 13:01:43 +03001055/**
1056 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1057 * @dep: The endpoint with the TRB ring
1058 * @index: The index of the current TRB in the ring
1059 *
1060 * Returns the TRB prior to the one pointed to by the index. If the
1061 * index is 0, we will wrap backwards, skip the link TRB, and return
1062 * the one just before that.
1063 */
1064static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1065{
1066 u8 tmp = index;
1067
Pratham Pratapd048da82017-12-05 14:38:29 +05301068 if (!dep->trb_pool)
1069 return NULL;
1070
Felipe Balbi42626912018-04-09 13:01:43 +03001071 if (!tmp)
1072 tmp = DWC3_TRB_NUM - 1;
1073
1074 return &dep->trb_pool[tmp - 1];
1075}
1076
1077static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1078{
1079 struct dwc3_trb *tmp;
1080 u8 trbs_left;
1081
1082 /*
1083 * If enqueue & dequeue are equal than it is either full or empty.
1084 *
1085 * One way to know for sure is if the TRB right before us has HWO bit
1086 * set or not. If it has, then we're definitely full and can't fit any
1087 * more transfers in our ring.
1088 */
1089 if (dep->trb_enqueue == dep->trb_dequeue) {
1090 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Pratham Pratapcca3d0b2018-03-15 10:31:20 +05301091 if (!tmp || tmp->ctrl & DWC3_TRB_CTRL_HWO)
Felipe Balbi42626912018-04-09 13:01:43 +03001092 return 0;
1093
1094 return DWC3_TRB_NUM - 1;
1095 }
1096
1097 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1098 trbs_left &= (DWC3_TRB_NUM - 1);
1099
1100 if (dep->trb_dequeue < dep->trb_enqueue)
1101 trbs_left--;
1102
1103 return trbs_left;
1104}
Felipe Balbi2c78c022016-08-12 13:13:10 +03001105
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001106static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
1107 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
1108 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +02001109{
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001110 struct dwc3 *dwc = dep->dwc;
1111 struct usb_gadget *gadget = &dwc->gadget;
1112 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +02001113
Felipe Balbif6bafc62012-02-06 11:04:53 +02001114 trb->size = DWC3_TRB_SIZE_LENGTH(length);
1115 trb->bpl = lower_32_bits(dma);
1116 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +02001117
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001118 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +02001119 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001120 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +02001121 break;
1122
1123 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001124 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301125 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001126
Manu Gautam40d829f2017-07-19 17:07:10 +05301127 /*
1128 * USB Specification 2.0 Section 5.9.2 states that: "If
1129 * there is only a single transaction in the microframe,
1130 * only a DATA0 data packet PID is used. If there are
1131 * two transactions per microframe, DATA1 is used for
1132 * the first transaction data packet and DATA0 is used
1133 * for the second transaction data packet. If there are
1134 * three transactions per microframe, DATA2 is used for
1135 * the first transaction data packet, DATA1 is used for
1136 * the second, and DATA0 is used for the third."
1137 *
1138 * IOW, we should satisfy the following cases:
1139 *
1140 * 1) length <= maxpacket
1141 * - DATA0
1142 *
1143 * 2) maxpacket < length <= (2 * maxpacket)
1144 * - DATA1, DATA0
1145 *
1146 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1147 * - DATA2, DATA1, DATA0
1148 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001149 if (speed == USB_SPEED_HIGH) {
1150 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301151 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301152 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1153
1154 if (length <= (2 * maxp))
1155 mult--;
1156
1157 if (length <= maxp)
1158 mult--;
1159
1160 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001161 }
1162 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301163 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001164 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001165
1166 /* always enable Interrupt on Missed ISOC */
1167 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001168 break;
1169
1170 case USB_ENDPOINT_XFER_BULK:
1171 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001172 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001173 break;
1174 default:
1175 /*
1176 * This is only possible with faulty memory because we
1177 * checked it already :)
1178 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001179 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1180 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001181 }
1182
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301183 /*
1184 * Enable Continue on Short Packet
1185 * when endpoint is not a stream capable
1186 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001187 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301188 if (!dep->stream_capable)
1189 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001190
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001191 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001192 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1193 }
1194
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001195 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301196 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001197 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001198
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301199 if (chain)
1200 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1201
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001202 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001203 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001204
1205 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001206
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301207 dwc3_ep_inc_enq(dep);
1208
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001209 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001210}
1211
John Youn361572b2016-05-19 17:26:17 -07001212/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001213 * dwc3_prepare_one_trb - setup one TRB from one request
1214 * @dep: endpoint for which this request is prepared
1215 * @req: dwc3_request pointer
1216 * @chain: should this TRB be chained to the next?
1217 * @node: only for isochronous endpoints. First TRB needs different type.
1218 */
1219static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1220 struct dwc3_request *req, unsigned chain, unsigned node)
1221{
1222 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301223 unsigned int length;
1224 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001225 unsigned stream_id = req->request.stream_id;
1226 unsigned short_not_ok = req->request.short_not_ok;
1227 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301228
1229 if (req->request.num_sgs > 0) {
1230 length = sg_dma_len(req->start_sg);
1231 dma = sg_dma_address(req->start_sg);
1232 } else {
1233 length = req->request.length;
1234 dma = req->request.dma;
1235 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001236
1237 trb = &dep->trb_pool[dep->trb_enqueue];
1238
1239 if (!req->trb) {
1240 dwc3_gadget_move_started_request(req);
1241 req->trb = trb;
1242 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001243 }
1244
1245 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1246 stream_id, short_not_ok, no_interrupt);
1247}
1248
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001249static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001250 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001251{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301252 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001253 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001254 int i;
1255
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301256 unsigned int remaining = req->request.num_mapped_sgs
1257 - req->num_queued_sgs;
1258
1259 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001260 unsigned int length = req->request.length;
1261 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1262 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001263 unsigned chain = true;
1264
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001265 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001266 chain = false;
1267
Felipe Balbic6267a52017-01-05 14:58:46 +02001268 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1269 struct dwc3 *dwc = dep->dwc;
1270 struct dwc3_trb *trb;
1271
1272 req->unaligned = true;
1273
1274 /* prepare normal TRB */
1275 dwc3_prepare_one_trb(dep, req, true, i);
1276
1277 /* Now prepare one extra TRB to align transfer size */
1278 trb = &dep->trb_pool[dep->trb_enqueue];
1279 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001280 maxp - rem, false, 1,
Felipe Balbic6267a52017-01-05 14:58:46 +02001281 req->request.stream_id,
1282 req->request.short_not_ok,
1283 req->request.no_interrupt);
1284 } else {
1285 dwc3_prepare_one_trb(dep, req, chain, i);
1286 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001287
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301288 /*
1289 * There can be a situation where all sgs in sglist are not
1290 * queued because of insufficient trb number. To handle this
1291 * case, update start_sg to next sg to be queued, so that
1292 * we have free trbs we can continue queuing from where we
1293 * previously stopped
1294 */
1295 if (chain)
1296 req->start_sg = sg_next(s);
1297
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301298 req->num_queued_sgs++;
1299
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001300 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001301 break;
1302 }
1303}
1304
1305static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001306 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001307{
Felipe Balbic6267a52017-01-05 14:58:46 +02001308 unsigned int length = req->request.length;
1309 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1310 unsigned int rem = length % maxp;
1311
Tejas Joglekar19bc5352019-01-22 13:26:51 +05301312 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001313 struct dwc3 *dwc = dep->dwc;
1314 struct dwc3_trb *trb;
1315
1316 req->unaligned = true;
1317
1318 /* prepare normal TRB */
1319 dwc3_prepare_one_trb(dep, req, true, 0);
1320
1321 /* Now prepare one extra TRB to align transfer size */
1322 trb = &dep->trb_pool[dep->trb_enqueue];
1323 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001324 false, 1, req->request.stream_id,
Felipe Balbic6267a52017-01-05 14:58:46 +02001325 req->request.short_not_ok,
1326 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001327 } else if (req->request.zero && req->request.length &&
Thinh Nguyen4ea438d2018-07-27 18:52:41 -07001328 (IS_ALIGNED(req->request.length, maxp))) {
Felipe Balbid6e5a542017-04-07 16:34:38 +03001329 struct dwc3 *dwc = dep->dwc;
1330 struct dwc3_trb *trb;
1331
1332 req->zero = true;
1333
1334 /* prepare normal TRB */
1335 dwc3_prepare_one_trb(dep, req, true, 0);
1336
1337 /* Now prepare one extra TRB to handle ZLP */
1338 trb = &dep->trb_pool[dep->trb_enqueue];
1339 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001340 false, 1, req->request.stream_id,
Felipe Balbid6e5a542017-04-07 16:34:38 +03001341 req->request.short_not_ok,
1342 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001343 } else {
1344 dwc3_prepare_one_trb(dep, req, false, 0);
1345 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001346}
1347
Felipe Balbi72246da2011-08-19 18:10:58 +03001348/*
1349 * dwc3_prepare_trbs - setup TRBs from requests
1350 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001351 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001352 * The function goes through the requests list and sets up TRBs for the
1353 * transfers. The function returns once there are no more TRBs available or
1354 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001355 */
Felipe Balbic4233572016-05-12 14:08:34 +03001356static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001357{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001358 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001359
1360 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1361
Felipe Balbid86c5a62016-10-25 13:48:52 +03001362 /*
1363 * We can get in a situation where there's a request in the started list
1364 * but there weren't enough TRBs to fully kick it in the first time
1365 * around, so it has been waiting for more TRBs to be freed up.
1366 *
1367 * In that case, we should check if we have a request with pending_sgs
1368 * in the started list and prepare TRBs for that request first,
1369 * otherwise we will prepare TRBs completely out of order and that will
1370 * break things.
1371 */
1372 list_for_each_entry(req, &dep->started_list, list) {
1373 if (req->num_pending_sgs > 0)
1374 dwc3_prepare_one_trb_sg(dep, req);
1375
1376 if (!dwc3_calc_trbs_left(dep))
1377 return;
1378 }
1379
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001380 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001381 struct dwc3 *dwc = dep->dwc;
1382 int ret;
1383
1384 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1385 dep->direction);
1386 if (ret)
1387 return;
1388
1389 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301390 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301391 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001392 req->num_pending_sgs = req->request.num_mapped_sgs;
1393
Felipe Balbi1f512112016-08-12 13:17:27 +03001394 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001395 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001396 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001397 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001398
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001399 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001400 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001401 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001402}
1403
Felipe Balbi7fdca762017-09-05 14:41:34 +03001404static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001405{
1406 struct dwc3_gadget_ep_cmd_params params;
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001407 struct dwc3_request *req, *req1, *n;
Jack Pham62523502018-01-15 16:37:05 -08001408 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001409 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001410 int ret;
1411 u32 cmd;
1412
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001413 if (!dwc3_calc_trbs_left(dep))
1414 return 0;
1415
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001416 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001417
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001418 dwc3_prepare_trbs(dep);
1419 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001420 if (!req) {
1421 dep->flags |= DWC3_EP_PENDING_REQUEST;
Jack Pham62523502018-01-15 16:37:05 -08001422 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001423 return 0;
1424 }
1425
1426 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001427
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001428 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301429 params.param0 = upper_32_bits(req->trb_dma);
1430 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001431 cmd = DWC3_DEPCMD_STARTTRANSFER;
1432
1433 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1434 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301435 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001436 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1437 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301438 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001439
Felipe Balbi2cd47182016-04-12 16:42:43 +03001440 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001441 if (ret < 0) {
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001442 if ((ret == -EAGAIN) && starting &&
1443 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1444 dbg_event(dep->number, "CMD_STS", ret);
1445 /* If bit13 in Command complete event is set, software
1446 * must issue ENDTRANDFER command and wait for
1447 * Xfernotready event to queue the requests again.
1448 */
1449 if (!dep->resource_index) {
1450 dwc3_gadget_ep_get_transfer_index(dep);
1451 WARN_ON_ONCE(!dep->resource_index);
1452 }
1453 dwc3_stop_active_transfer(dwc, dep->number, true);
1454
1455 list_for_each_entry_safe_reverse(req1, n,
1456 &dep->started_list, list) {
1457 req1->trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1458 req1->trb = NULL;
1459 dwc3_gadget_move_pending_list_front(req1);
1460 dwc3_ep_inc_deq(dep);
1461 }
1462
1463 return ret;
1464 }
1465
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 /*
1467 * FIXME we need to iterate over the list of requests
1468 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001469 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001470 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001471 if (req->trb)
1472 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001473 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 return ret;
1475 }
1476
Felipe Balbi72246da2011-08-19 18:10:58 +03001477 return 0;
1478}
1479
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001480static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1481{
1482 u32 reg;
1483
1484 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1485 return DWC3_DSTS_SOFFN(reg);
1486}
1487
Felipe Balbi5828cad2018-03-27 11:14:31 +03001488static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301489{
Hemant Kumardd36b892018-08-02 17:51:05 -07001490 u16 wraparound_bits;
1491
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001492 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001493 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001494 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301495 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301496 return;
1497 }
1498
Hemant Kumardd36b892018-08-02 17:51:05 -07001499 wraparound_bits = dep->frame_number & DWC3_FRAME_WRAP_AROUND_MASK;
1500 dep->frame_number = dep->frame_number & ~DWC3_FRAME_WRAP_AROUND_MASK;
1501
1502 /* if frame wrapped-around update wrap-around bits to reflect that */
1503 if (__dwc3_gadget_get_frame(dep->dwc) < dep->frame_number)
1504 wraparound_bits += BIT(14);
1505
1506 dep->frame_number = __dwc3_gadget_get_frame(dep->dwc) +
1507 2 * dep->interval;
1508
1509 /* align uf to ep interval */
1510 dep->frame_number = (wraparound_bits | dep->frame_number) &
1511 ~(dep->interval - 1);
1512
Felipe Balbi7fdca762017-09-05 14:41:34 +03001513 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301514}
1515
Felipe Balbi72246da2011-08-19 18:10:58 +03001516static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1517{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001518 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001519
Mayank Ranad32d7842018-06-29 10:20:13 -07001520 if (!dep->endpoint.desc || !dwc->pullups_connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001521 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1522 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001523 return -ESHUTDOWN;
1524 }
1525
Felipe Balbi04fb3652017-05-17 15:57:45 +03001526 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1527 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001528 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001529
Manu Gautam8bb9cbc2013-02-11 15:53:34 +05301530 if (req->request.status == -EINPROGRESS) {
1531 dev_err(dwc->dev, "%s: %pK request already in queue\n",
1532 dep->name, req);
1533 return -EBUSY;
1534 }
1535
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 req->request.actual = 0;
1537 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001538
Felipe Balbife84f522015-09-01 09:01:38 -05001539 trace_dwc3_ep_queue(req);
1540
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001541 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001542
Felipe Balbid889c232016-09-29 15:44:29 +03001543 /*
1544 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1545 * wait for a XferNotReady event so we will know what's the current
1546 * (micro-)frame number.
1547 *
1548 * Without this trick, we are very, very likely gonna get Bus Expiry
1549 * errors which will force us issue EndTransfer command.
1550 */
1551 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001552 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1553 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001554 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001555
1556 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1557 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1558 __dwc3_gadget_start_isoc(dep);
1559 return 0;
1560 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001561 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001562 }
1563
Felipe Balbi7fdca762017-09-05 14:41:34 +03001564 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001565}
1566
Jack Pham62523502018-01-15 16:37:05 -08001567static int dwc3_gadget_wakeup(struct usb_gadget *g)
1568{
1569 struct dwc3 *dwc = gadget_to_dwc(g);
1570
1571 schedule_work(&dwc->wakeup_work);
1572 return 0;
1573}
1574
1575static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1576{
1577 if (atomic_read(&dwc->in_lpm) ||
1578 dwc->link_state == DWC3_LINK_STATE_U3)
1579 return true;
1580 return false;
1581}
1582
Felipe Balbi72246da2011-08-19 18:10:58 +03001583static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1584 gfp_t gfp_flags)
1585{
1586 struct dwc3_request *req = to_dwc3_request(request);
1587 struct dwc3_ep *dep = to_dwc3_ep(ep);
1588 struct dwc3 *dwc = dep->dwc;
1589
1590 unsigned long flags;
1591
1592 int ret;
1593
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001594 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 ret = __dwc3_gadget_ep_queue(dep, req);
1596 spin_unlock_irqrestore(&dwc->lock, flags);
1597
1598 return ret;
1599}
1600
1601static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1602 struct usb_request *request)
1603{
1604 struct dwc3_request *req = to_dwc3_request(request);
1605 struct dwc3_request *r = NULL;
1606
1607 struct dwc3_ep *dep = to_dwc3_ep(ep);
1608 struct dwc3 *dwc = dep->dwc;
1609
1610 unsigned long flags;
1611 int ret = 0;
1612
Jack Pham62523502018-01-15 16:37:05 -08001613 if (atomic_read(&dwc->in_lpm)) {
1614 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1615 return -EAGAIN;
1616 }
1617
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001618 trace_dwc3_ep_dequeue(req);
1619
Felipe Balbi72246da2011-08-19 18:10:58 +03001620 spin_lock_irqsave(&dwc->lock, flags);
1621
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001622 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 if (r == req)
1624 break;
1625 }
1626
1627 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001628 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001629 if (r == req)
1630 break;
1631 }
1632 if (r == req) {
1633 /* wait until it is processed */
Jack Pham62523502018-01-15 16:37:05 -08001634 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001635
1636 /*
1637 * If request was already started, this means we had to
1638 * stop the transfer. With that we also need to ignore
1639 * all TRBs used by the request, however TRBs can only
1640 * be modified after completion of END_TRANSFER
1641 * command. So what we do here is that we wait for
1642 * END_TRANSFER completion and only after that, we jump
1643 * over TRBs by clearing HWO and incrementing dequeue
1644 * pointer.
1645 *
1646 * Note that we have 2 possible types of transfers here:
1647 *
1648 * i) Linear buffer request
1649 * ii) SG-list based request
1650 *
1651 * SG-list based requests will have r->num_pending_sgs
1652 * set to a valid number (> 0). Linear requests,
1653 * normally use a single TRB.
1654 *
1655 * For each of these two cases, if r->unaligned flag is
1656 * set, one extra TRB has been used to align transfer
1657 * size to wMaxPacketSize.
1658 *
1659 * All of these cases need to be taken into
1660 * consideration so we don't mess up our TRB ring
1661 * pointers.
1662 */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001663 if (!r->trb)
Mayank Rana05645362018-03-23 10:05:33 -07001664 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001665
1666 if (r->num_pending_sgs) {
1667 struct dwc3_trb *trb;
1668 int i = 0;
1669
1670 for (i = 0; i < r->num_pending_sgs; i++) {
1671 trb = r->trb + i;
1672 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1673 dwc3_ep_inc_deq(dep);
1674 }
1675
Felipe Balbid6e5a542017-04-07 16:34:38 +03001676 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001677 trb = r->trb + r->num_pending_sgs + 1;
1678 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1679 dwc3_ep_inc_deq(dep);
1680 }
1681 } else {
1682 struct dwc3_trb *trb = r->trb;
1683
1684 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1685 dwc3_ep_inc_deq(dep);
1686
Felipe Balbid6e5a542017-04-07 16:34:38 +03001687 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001688 trb = r->trb + 1;
1689 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1690 dwc3_ep_inc_deq(dep);
1691 }
1692 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301693 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001695 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001696 request, ep->name);
1697 ret = -EINVAL;
1698 goto out0;
1699 }
1700
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301701out1:
Jack Pham62523502018-01-15 16:37:05 -08001702 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001703 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001704
Felipe Balbi72246da2011-08-19 18:10:58 +03001705 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1706
1707out0:
1708 spin_unlock_irqrestore(&dwc->lock, flags);
1709
1710 return ret;
1711}
1712
Felipe Balbi7a608552014-09-24 14:19:52 -05001713int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001714{
1715 struct dwc3_gadget_ep_cmd_params params;
1716 struct dwc3 *dwc = dep->dwc;
1717 int ret;
1718
Jack Pham62523502018-01-15 16:37:05 -08001719 if (!dep->endpoint.desc) {
1720 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1721 return -EINVAL;
1722 }
1723
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001724 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1725 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1726 return -EINVAL;
1727 }
1728
Felipe Balbi72246da2011-08-19 18:10:58 +03001729 memset(&params, 0x00, sizeof(params));
Jack Pham62523502018-01-15 16:37:05 -08001730 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001731 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001732 struct dwc3_trb *trb;
1733
1734 unsigned transfer_in_flight;
1735 unsigned started;
1736
1737 if (dep->number > 1)
1738 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1739 else
1740 trb = &dwc->ep0_trb[dep->trb_enqueue];
1741
Pratham Pratapd048da82017-12-05 14:38:29 +05301742 if (trb)
1743 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1744 else
1745 transfer_in_flight = false;
1746
Felipe Balbi69450c42016-05-30 13:37:02 +03001747 started = !list_empty(&dep->started_list);
1748
1749 if (!protocol && ((dep->direction && transfer_in_flight) ||
1750 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001751 return -EAGAIN;
1752 }
1753
Felipe Balbi2cd47182016-04-12 16:42:43 +03001754 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1755 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001756 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001757 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 dep->name);
1759 else
1760 dep->flags |= DWC3_EP_STALL;
1761 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001762
John Youn50c763f2016-05-31 17:49:56 -07001763 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001765 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001766 dep->name);
1767 else
Alan Sterna535d812013-11-01 12:05:12 -04001768 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001769 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001770
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 return ret;
1772}
1773
1774static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1775{
1776 struct dwc3_ep *dep = to_dwc3_ep(ep);
1777 struct dwc3 *dwc = dep->dwc;
1778
1779 unsigned long flags;
1780
1781 int ret;
1782
Jack Pham62523502018-01-15 16:37:05 -08001783 if (!ep->desc) {
1784 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1785 return -EINVAL;
1786 }
1787
Felipe Balbi72246da2011-08-19 18:10:58 +03001788 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001789 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001790 spin_unlock_irqrestore(&dwc->lock, flags);
1791
1792 return ret;
1793}
1794
1795static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1796{
1797 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001798 struct dwc3 *dwc = dep->dwc;
1799 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001800 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001801
Paul Zimmerman249a4562012-02-24 17:32:16 -08001802 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08001803 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001804 dep->flags |= DWC3_EP_WEDGE;
1805
Pratyush Anand08f0d962012-06-25 22:40:43 +05301806 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001807 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301808 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001809 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001810 spin_unlock_irqrestore(&dwc->lock, flags);
1811
1812 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001813}
1814
1815/* -------------------------------------------------------------------------- */
1816
1817static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1818 .bLength = USB_DT_ENDPOINT_SIZE,
1819 .bDescriptorType = USB_DT_ENDPOINT,
1820 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1821};
1822
1823static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1824 .enable = dwc3_gadget_ep0_enable,
1825 .disable = dwc3_gadget_ep0_disable,
1826 .alloc_request = dwc3_gadget_ep_alloc_request,
1827 .free_request = dwc3_gadget_ep_free_request,
1828 .queue = dwc3_gadget_ep0_queue,
1829 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301830 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001831 .set_wedge = dwc3_gadget_ep_set_wedge,
1832};
1833
1834static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1835 .enable = dwc3_gadget_ep_enable,
1836 .disable = dwc3_gadget_ep_disable,
1837 .alloc_request = dwc3_gadget_ep_alloc_request,
1838 .free_request = dwc3_gadget_ep_free_request,
1839 .queue = dwc3_gadget_ep_queue,
1840 .dequeue = dwc3_gadget_ep_dequeue,
1841 .set_halt = dwc3_gadget_ep_set_halt,
1842 .set_wedge = dwc3_gadget_ep_set_wedge,
1843};
1844
1845/* -------------------------------------------------------------------------- */
1846
1847static int dwc3_gadget_get_frame(struct usb_gadget *g)
1848{
1849 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001850
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001851 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001852}
1853
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001854static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001855{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001856 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001857
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001858 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 u32 reg;
1860
Felipe Balbi72246da2011-08-19 18:10:58 +03001861 u8 link_state;
1862 u8 speed;
1863
Felipe Balbi72246da2011-08-19 18:10:58 +03001864 /*
1865 * According to the Databook Remote wakeup request should
1866 * be issued only when the device is in early suspend state.
1867 *
1868 * We can check that via USB Link State bits in DSTS register.
1869 */
1870 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1871
1872 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001873 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001874 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001875 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001876
1877 link_state = DWC3_DSTS_USBLNKST(reg);
1878
1879 switch (link_state) {
1880 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1881 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1882 break;
1883 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001884 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 }
1886
Felipe Balbi8598bde2012-01-02 18:55:57 +02001887 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1888 if (ret < 0) {
1889 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001890 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001891 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001892
Paul Zimmerman802fde92012-04-27 13:10:52 +03001893 /* Recent versions do this automatically */
1894 if (dwc->revision < DWC3_REVISION_194A) {
1895 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001896 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001897 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1898 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1899 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001900
Paul Zimmerman1d046792012-02-15 18:56:56 -08001901 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001902 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001903
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001904 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001905 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1906
1907 /* in HS, means ON */
1908 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1909 break;
1910 }
1911
1912 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1913 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001914 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001915 }
1916
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001917 return 0;
1918}
1919
Jack Pham62523502018-01-15 16:37:05 -08001920#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1921#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1922
1923static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001924{
Jack Pham62523502018-01-15 16:37:05 -08001925 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001926 int ret;
Jack Pham62523502018-01-15 16:37:05 -08001927 static int retry_count;
1928
1929 dwc = container_of(w, struct dwc3, wakeup_work);
1930
1931 ret = pm_runtime_get_sync(dwc->dev);
1932 if (ret) {
1933 /* pm_runtime_get_sync returns -EACCES error between
1934 * late_suspend and early_resume, wait for system resume to
1935 * finish and queue work again
1936 */
1937 dev_dbg(dwc->dev, "PM runtime get sync failed, ret %d\n", ret);
1938 if (ret == -EACCES) {
1939 pm_runtime_put_noidle(dwc->dev);
1940 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1941 retry_count = 0;
1942 dev_err(dwc->dev, "pm_runtime_get_sync timed out\n");
1943 return;
1944 }
1945 msleep(DWC3_PM_RESUME_DELAY);
1946 retry_count++;
1947 schedule_work(&dwc->wakeup_work);
1948 return;
1949 }
1950 }
1951 retry_count = 0;
1952 dbg_event(0xFF, "Gdgwake gsyn",
1953 atomic_read(&dwc->dev->power.usage_count));
1954
1955 ret = dwc3_gadget_wakeup_int(dwc);
1956 if (ret)
1957 dev_err(dwc->dev, "Remote wakeup failed. ret = %d\n", ret);
1958
1959 pm_runtime_put_noidle(dwc->dev);
1960 dbg_event(0xFF, "Gdgwake put",
1961 atomic_read(&dwc->dev->power.usage_count));
1962}
1963
1964static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1965{
1966 bool link_recover_only = false;
1967
1968 u32 reg;
1969 int ret = 0;
1970 u8 link_state;
1971 unsigned long flags;
1972
1973 dev_dbg(dwc->dev, "%s(): Entry\n", __func__);
1974 disable_irq(dwc->irq);
1975 spin_lock_irqsave(&dwc->lock, flags);
1976 /*
1977 * According to the Databook Remote wakeup request should
1978 * be issued only when the device is in early suspend state.
1979 *
1980 * We can check that via USB Link State bits in DSTS register.
1981 */
1982 link_state = dwc3_get_link_state(dwc);
1983
1984 switch (link_state) {
1985 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1986 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1987 break;
1988 case DWC3_LINK_STATE_U1:
1989 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1990 link_recover_only = true;
1991 break;
1992 }
1993 /* Intentional fallthrough */
1994 default:
1995 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1996 link_state);
1997 ret = -EINVAL;
1998 goto out;
1999 }
2000
2001 /* Enable LINK STATUS change event */
2002 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2003 reg |= DWC3_DEVTEN_ULSTCNGEN;
2004 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2005 /*
2006 * memory barrier is required to make sure that required events
2007 * with core is enabled before performing RECOVERY mechnism.
2008 */
2009 mb();
2010
2011 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2012 if (ret < 0) {
2013 dev_err(dwc->dev, "failed to put link in Recovery\n");
2014 /* Disable LINK STATUS change */
2015 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2016 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2017 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2018 /* Required to complete this operation before returning */
2019 mb();
2020 goto out;
2021 }
2022
2023 /* Recent versions do this automatically */
2024 if (dwc->revision < DWC3_REVISION_194A) {
2025 /* write zeroes to Link Change Request */
2026 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2027 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2028 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2029 }
2030
2031 spin_unlock_irqrestore(&dwc->lock, flags);
2032 enable_irq(dwc->irq);
2033
2034 /*
2035 * Have bigger value (16 sec) for timeout since some host PCs driving
2036 * resume for very long time (e.g. 8 sec)
2037 */
2038 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
2039 (dwc->link_state < DWC3_LINK_STATE_U3) ||
2040 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
2041 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002042
2043 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002044 /* Disable link status change event */
2045 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2046 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2047 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2048 /*
2049 * Complete this write before we go ahead and perform resume
2050 * as we don't need link status change notificaiton anymore.
2051 */
2052 mb();
2053
2054 if (!ret) {
2055 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
2056 dwc->link_state);
2057 ret = -EINVAL;
2058 spin_unlock_irqrestore(&dwc->lock, flags);
2059 goto out1;
2060 } else {
2061 ret = 0;
2062 /*
2063 * If USB is disconnected OR received RESET from host,
2064 * don't perform resume
2065 */
2066 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
2067 dwc->gadget.state == USB_STATE_DEFAULT)
2068 link_recover_only = true;
2069 }
2070
2071 /*
2072 * According to DWC3 databook, the controller does not
2073 * trigger a wakeup event when remote-wakeup is used.
2074 * Hence, after remote-wakeup sequence is complete, and
2075 * the device is back at U0 state, it is required that
2076 * the resume sequence is initiated by SW.
2077 */
2078 if (!link_recover_only)
2079 dwc3_gadget_wakeup_interrupt(dwc, true);
2080
Felipe Balbi72246da2011-08-19 18:10:58 +03002081 spin_unlock_irqrestore(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002082 dev_dbg(dwc->dev, "%s: Exit\n", __func__);
2083 return ret;
2084
2085out:
2086 spin_unlock_irqrestore(&dwc->lock, flags);
2087 enable_irq(dwc->irq);
2088
2089out1:
2090 return ret;
2091}
2092
2093static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
2094{
2095 int ret = 0;
2096 struct dwc3 *dwc = gadget_to_dwc(g);
2097
2098 if (!g || (g->speed != USB_SPEED_SUPER))
2099 return -ENOTSUPP;
2100
2101 if (dwc3_gadget_is_suspended(dwc)) {
2102 dev_dbg(dwc->dev, "USB bus is suspended, scheduling wakeup\n");
2103 dwc3_gadget_wakeup(&dwc->gadget);
2104 return -EAGAIN;
2105 }
2106
2107 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_XMIT_DEV,
2108 0x1 | (interface_id << 4));
2109 if (ret)
2110 dev_err(dwc->dev, "Function wakeup HW command failed, ret %d\n",
2111 ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002112
2113 return ret;
2114}
2115
2116static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2117 int is_selfpowered)
2118{
2119 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002120 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002121
Paul Zimmerman249a4562012-02-24 17:32:16 -08002122 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002123 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002124 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002125
2126 return 0;
2127}
2128
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002129static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002130{
Mayank Rana0f67c832018-03-07 14:11:41 -08002131 u32 reg, reg1;
Jack Pham62523502018-01-15 16:37:05 -08002132 u32 timeout = 1500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002133
Jack Pham62523502018-01-15 16:37:05 -08002134 dbg_event(0xFF, "run_stop", is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03002135 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002136 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002137 if (dwc->revision <= DWC3_REVISION_187A) {
2138 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2139 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2140 }
2141
2142 if (dwc->revision >= DWC3_REVISION_194A)
2143 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Jack Pham62523502018-01-15 16:37:05 -08002144
2145 dwc3_event_buffers_setup(dwc);
2146 __dwc3_gadget_start(dwc);
2147
Mayank Rana0f67c832018-03-07 14:11:41 -08002148 reg1 = dwc3_readl(dwc->regs, DWC3_DCFG);
2149 reg1 &= ~(DWC3_DCFG_SPEED_MASK);
2150
2151 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
2152 reg1 |= DWC3_DCFG_SUPERSPEED_PLUS;
2153 else if (dwc->maximum_speed == USB_SPEED_HIGH)
2154 reg1 |= DWC3_DCFG_HIGHSPEED;
2155 else
2156 reg1 |= DWC3_DCFG_SUPERSPEED;
2157 dwc3_writel(dwc->regs, DWC3_DCFG, reg1);
2158
Paul Zimmerman802fde92012-04-27 13:10:52 +03002159 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002160
2161 if (dwc->has_hibernation)
2162 reg |= DWC3_DCTL_KEEP_CONNECT;
2163
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002164 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002165 } else {
Jack Pham62523502018-01-15 16:37:05 -08002166 dwc3_gadget_disable_irq(dwc);
Mayank Ranad32d7842018-06-29 10:20:13 -07002167 dwc->pullups_connected = false;
Jack Pham62523502018-01-15 16:37:05 -08002168 __dwc3_gadget_ep_disable(dwc->eps[0]);
2169 __dwc3_gadget_ep_disable(dwc->eps[1]);
2170
2171 /*
2172 * According to dwc3 databook, it is must to remove any active
2173 * transfers before trying to stop USB device controller. Hence
2174 * call dwc3_stop_active_transfers() API before stopping USB
2175 * device controller.
2176 */
Hemant Kumar7b7e0652019-04-09 18:00:55 -07002177 dwc3_stop_active_transfers_to_halt(dwc);
Jack Pham62523502018-01-15 16:37:05 -08002178
Felipe Balbi72246da2011-08-19 18:10:58 +03002179 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002180
2181 if (dwc->has_hibernation && !suspend)
2182 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002183 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002184
2185 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2186
2187 do {
2188 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002189 reg &= DWC3_DSTS_DEVCTRLHLT;
2190 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002191
Hemant Kumar7b7e0652019-04-09 18:00:55 -07002192 if (!is_on)
2193 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_CLEAR_DB, 0);
2194
Jack Pham62523502018-01-15 16:37:05 -08002195 if (!timeout) {
2196 dev_err(dwc->dev, "failed to %s controller\n",
2197 is_on ? "start" : "stop");
2198 if (is_on)
2199 dbg_event(0xFF, "STARTTOUT", reg);
2200 else
2201 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03002202 return -ETIMEDOUT;
Jack Pham62523502018-01-15 16:37:05 -08002203 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002204
Pratyush Anand6f17f742012-07-02 10:21:55 +05302205 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002206}
2207
Jack Pham62523502018-01-15 16:37:05 -08002208static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2209{
2210 struct dwc3 *dwc = gadget_to_dwc(g);
2211
2212 dwc->vbus_draw = mA;
2213 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
2214 dbg_event(0xFF, "currentDraw", mA);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002215 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08002216 return 0;
2217}
2218
Felipe Balbi72246da2011-08-19 18:10:58 +03002219static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2220{
2221 struct dwc3 *dwc = gadget_to_dwc(g);
2222 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302223 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002224
2225 is_on = !!is_on;
Jack Pham62523502018-01-15 16:37:05 -08002226 dwc->softconnect = is_on;
2227
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002228 if ((dwc3_is_otg_or_drd(dwc) && !dwc->vbus_active)
Jack Pham62523502018-01-15 16:37:05 -08002229 || !dwc->gadget_driver) {
2230 /*
2231 * Need to wait for vbus_session(on) from otg driver or to
2232 * the udc_start.
2233 */
2234 dbg_event(0xFF, "WaitPullup", 0);
2235 return 0;
2236 }
2237
2238 pm_runtime_get_sync(dwc->dev);
2239 dbg_event(0xFF, "Pullup gsync",
2240 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03002241
Baolin Wangbb014732016-10-14 17:11:33 +08002242 /*
2243 * Per databook, when we want to stop the gadget, if a control transfer
2244 * is still in process, complete it and get the core into setup phase.
2245 */
Jack Phambe653d62018-06-19 18:42:40 -07002246 if (!is_on && (dwc->ep0state != EP0_SETUP_PHASE ||
2247 dwc->ep0_next_event != DWC3_EP0_COMPLETE)) {
Baolin Wangbb014732016-10-14 17:11:33 +08002248 reinit_completion(&dwc->ep0_in_setup);
2249
2250 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2251 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
Manu Gautam1f805462019-02-27 08:50:12 +05302252 if (ret == 0)
Baolin Wangbb014732016-10-14 17:11:33 +08002253 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
Baolin Wangbb014732016-10-14 17:11:33 +08002254 }
2255
Felipe Balbi72246da2011-08-19 18:10:58 +03002256 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002257 /*
2258 * If we are here after bus suspend notify otg state machine to
2259 * increment pm usage count of dwc to prevent pm_runtime_suspend
2260 * during enumeration.
2261 */
2262 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002263 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
2264
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002265 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002266 spin_unlock_irqrestore(&dwc->lock, flags);
2267
Jack Pham62523502018-01-15 16:37:05 -08002268 pm_runtime_mark_last_busy(dwc->dev);
2269 pm_runtime_put_autosuspend(dwc->dev);
2270 dbg_event(0xFF, "Pullup put",
2271 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302272 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002273}
2274
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002275static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2276{
2277 u32 reg;
2278
Jack Pham62523502018-01-15 16:37:05 -08002279 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002280 /* Enable all but Start and End of Frame IRQs */
2281 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2282 DWC3_DEVTEN_EVNTOVERFLOWEN |
2283 DWC3_DEVTEN_CMDCMPLTEN |
2284 DWC3_DEVTEN_ERRTICERREN |
2285 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002286 DWC3_DEVTEN_CONNECTDONEEN |
2287 DWC3_DEVTEN_USBRSTEN |
2288 DWC3_DEVTEN_DISCONNEVTEN);
2289
Jack Pham62523502018-01-15 16:37:05 -08002290 if (dwc->revision < DWC3_REVISION_230A)
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002291 reg |= DWC3_DEVTEN_ULSTCNGEN;
2292
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002293 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2294}
2295
Jack Pham62523502018-01-15 16:37:05 -08002296void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002297{
Jack Pham62523502018-01-15 16:37:05 -08002298 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002299 /* mask all interrupts */
2300 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2301}
2302
Felipe Balbib15a7622011-06-30 16:57:15 +03002303static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002304
Felipe Balbi4e994722016-05-13 14:09:59 +03002305/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002306 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2307 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002308 *
2309 * The following looks like complex but it's actually very simple. In order to
2310 * calculate the number of packets we can burst at once on OUT transfers, we're
2311 * gonna use RxFIFO size.
2312 *
2313 * To calculate RxFIFO size we need two numbers:
2314 * MDWIDTH = size, in bits, of the internal memory bus
2315 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2316 *
2317 * Given these two numbers, the formula is simple:
2318 *
2319 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2320 *
2321 * 24 bytes is for 3x SETUP packets
2322 * 16 bytes is a clock domain crossing tolerance
2323 *
2324 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2325 */
2326static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2327{
2328 u32 ram2_depth;
2329 u32 mdwidth;
2330 u32 nump;
2331 u32 reg;
2332
2333 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2334 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2335
2336 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2337 nump = min_t(u32, nump, 16);
2338
2339 /* update NumP */
2340 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2341 reg &= ~DWC3_DCFG_NUMP_MASK;
2342 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2343 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2344}
2345
Jack Pham62523502018-01-15 16:37:05 -08002346static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2347{
2348 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2349 unsigned long flags;
2350
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002351 if (dwc->dr_mode != USB_DR_MODE_OTG && dwc->dr_mode != USB_DR_MODE_DRD)
Jack Pham62523502018-01-15 16:37:05 -08002352 return -EPERM;
2353
2354 is_active = !!is_active;
2355
2356 dbg_event(0xFF, "VbusSess", is_active);
2357 spin_lock_irqsave(&dwc->lock, flags);
2358
2359 /* Mark that the vbus was powered */
2360 dwc->vbus_active = is_active;
2361
2362 /*
2363 * Check if upper level usb_gadget_driver was already registered with
2364 * this udc controller driver (if dwc3_gadget_start was called)
2365 */
2366 if (dwc->gadget_driver && dwc->softconnect) {
2367 if (dwc->vbus_active) {
2368 /*
2369 * Both vbus was activated by otg and pullup was
2370 * signaled by the gadget driver.
2371 */
2372 dwc3_gadget_run_stop(dwc, 1, false);
2373 } else {
2374 dwc3_gadget_run_stop(dwc, 0, false);
2375 }
2376 }
2377
2378 /*
2379 * Clearing run/stop bit might occur before disconnect event is seen.
2380 * Make sure to let gadget driver know in that case.
2381 */
2382 if (!dwc->vbus_active) {
2383 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2384 dwc3_gadget_disconnect_interrupt(dwc);
2385 }
2386
2387 spin_unlock_irqrestore(&dwc->lock, flags);
2388 return 0;
2389}
2390
Felipe Balbid7be2952016-05-04 15:49:37 +03002391static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002392{
Felipe Balbi72246da2011-08-19 18:10:58 +03002393 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 int ret = 0;
2395 u32 reg;
2396
Jack Pham62523502018-01-15 16:37:05 -08002397 dbg_event(0xFF, "__Gadgetstart", 0);
2398
John Youncf40b862016-11-14 12:32:43 -08002399 /*
2400 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2401 * the core supports IMOD, disable it.
2402 */
2403 if (dwc->imod_interval) {
2404 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2405 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2406 } else if (dwc3_has_imod(dwc)) {
2407 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2408 }
2409
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002410 /*
2411 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2412 * field instead of letting dwc3 itself calculate that automatically.
2413 *
2414 * This way, we maximize the chances that we'll be able to get several
2415 * bursts of data without going through any sort of endpoint throttling.
2416 */
2417 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002418 if (dwc3_is_usb31(dwc))
2419 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2420 else
2421 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2422
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002423 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2424
Jack Pham62523502018-01-15 16:37:05 -08002425 /*
2426 * Programs the number of outstanding pipelined transfer requests
2427 * the AXI master pushes to the AXI slave.
2428 */
2429 if (dwc->revision >= DWC3_REVISION_270A) {
2430 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2431 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2432 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2433 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2434 }
2435
Felipe Balbi4e994722016-05-13 14:09:59 +03002436 dwc3_gadget_setup_nump(dwc);
2437
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 /* Start with SuperSpeed Default */
2439 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2440
2441 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002442 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002443 if (ret) {
2444 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002445 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002446 }
2447
2448 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002449 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002450 if (ret) {
2451 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002452 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002453 }
2454
2455 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002456 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao08c937f2018-12-26 19:22:00 +08002457 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002458 dwc3_ep0_out_start(dwc);
2459
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002460 dwc3_gadget_enable_irq(dwc);
2461
Felipe Balbid7be2952016-05-04 15:49:37 +03002462 return 0;
2463
2464err1:
2465 __dwc3_gadget_ep_disable(dwc->eps[0]);
2466
2467err0:
2468 return ret;
2469}
2470
2471static int dwc3_gadget_start(struct usb_gadget *g,
2472 struct usb_gadget_driver *driver)
2473{
2474 struct dwc3 *dwc = gadget_to_dwc(g);
2475 unsigned long flags;
2476 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002477
Jack Pham62523502018-01-15 16:37:05 -08002478 dbg_event(0xFF, "Gadgetstart", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002479 spin_lock_irqsave(&dwc->lock, flags);
2480 if (dwc->gadget_driver) {
2481 dev_err(dwc->dev, "%s is already bound to %s\n",
2482 dwc->gadget.name,
2483 dwc->gadget_driver->driver.name);
2484 ret = -EBUSY;
Jack Pham62523502018-01-15 16:37:05 -08002485 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002486 }
2487
2488 dwc->gadget_driver = driver;
2489
Jack Pham62523502018-01-15 16:37:05 -08002490 /*
2491 * For DRD, this might get called by gadget driver during bootup
2492 * even though host mode might be active. Don't actually perform
2493 * device-specific initialization until device mode is activated.
2494 * In that case dwc3_gadget_restart() will handle it.
2495 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002496 spin_unlock_irqrestore(&dwc->lock, flags);
2497
2498 return 0;
2499
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002500err0:
Jack Pham62523502018-01-15 16:37:05 -08002501 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002502 return ret;
2503}
2504
Felipe Balbid7be2952016-05-04 15:49:37 +03002505static void __dwc3_gadget_stop(struct dwc3 *dwc)
2506{
Jack Pham62523502018-01-15 16:37:05 -08002507 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002508 dwc3_gadget_disable_irq(dwc);
2509 __dwc3_gadget_ep_disable(dwc->eps[0]);
2510 __dwc3_gadget_ep_disable(dwc->eps[1]);
2511}
2512
Felipe Balbi22835b82014-10-17 12:05:12 -05002513static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002514{
2515 struct dwc3 *dwc = gadget_to_dwc(g);
2516 unsigned long flags;
2517
2518 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002519 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002520 spin_unlock_irqrestore(&dwc->lock, flags);
2521
Jack Pham62523502018-01-15 16:37:05 -08002522 dbg_event(0xFF, "fwq_started", 0);
2523 flush_workqueue(dwc->dwc_wq);
2524 dbg_event(0xFF, "fwq_completed", 0);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002525
Felipe Balbi72246da2011-08-19 18:10:58 +03002526 return 0;
2527}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002528
Mayank Rana0f67c832018-03-07 14:11:41 -08002529static void __maybe_unused dwc3_gadget_set_speed(struct usb_gadget *g,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002530 enum usb_device_speed speed)
2531{
2532 struct dwc3 *dwc = gadget_to_dwc(g);
2533 unsigned long flags;
2534 u32 reg;
2535
2536 spin_lock_irqsave(&dwc->lock, flags);
2537 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2538 reg &= ~(DWC3_DCFG_SPEED_MASK);
2539
2540 /*
2541 * WORKAROUND: DWC3 revision < 2.20a have an issue
2542 * which would cause metastability state on Run/Stop
2543 * bit if we try to force the IP to USB2-only mode.
2544 *
2545 * Because of that, we cannot configure the IP to any
2546 * speed other than the SuperSpeed
2547 *
2548 * Refers to:
2549 *
2550 * STAR#9000525659: Clock Domain Crossing on DCTL in
2551 * USB 2.0 Mode
2552 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002553 if (dwc->revision < DWC3_REVISION_220A &&
2554 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002555 reg |= DWC3_DCFG_SUPERSPEED;
2556 } else {
2557 switch (speed) {
2558 case USB_SPEED_LOW:
2559 reg |= DWC3_DCFG_LOWSPEED;
2560 break;
2561 case USB_SPEED_FULL:
2562 reg |= DWC3_DCFG_FULLSPEED;
2563 break;
2564 case USB_SPEED_HIGH:
2565 reg |= DWC3_DCFG_HIGHSPEED;
2566 break;
2567 case USB_SPEED_SUPER:
2568 reg |= DWC3_DCFG_SUPERSPEED;
2569 break;
2570 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002571 if (dwc3_is_usb31(dwc))
2572 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2573 else
2574 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002575 break;
2576 default:
2577 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2578
2579 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2580 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2581 else
2582 reg |= DWC3_DCFG_SUPERSPEED;
2583 }
2584 }
2585 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2586
2587 spin_unlock_irqrestore(&dwc->lock, flags);
2588}
2589
Jack Pham62523502018-01-15 16:37:05 -08002590static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2591{
2592 struct dwc3 *dwc = gadget_to_dwc(g);
2593
2594 dbg_event(0xFF, "RestartUSBSession", 0);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002595 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Jack Pham62523502018-01-15 16:37:05 -08002596}
2597
Felipe Balbi72246da2011-08-19 18:10:58 +03002598static const struct usb_gadget_ops dwc3_gadget_ops = {
2599 .get_frame = dwc3_gadget_get_frame,
2600 .wakeup = dwc3_gadget_wakeup,
Jack Pham62523502018-01-15 16:37:05 -08002601 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002602 .set_selfpowered = dwc3_gadget_set_selfpowered,
Jack Pham62523502018-01-15 16:37:05 -08002603 .vbus_session = dwc3_gadget_vbus_session,
2604 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002605 .pullup = dwc3_gadget_pullup,
2606 .udc_start = dwc3_gadget_start,
2607 .udc_stop = dwc3_gadget_stop,
Jack Pham62523502018-01-15 16:37:05 -08002608 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002609};
2610
2611/* -------------------------------------------------------------------------- */
2612
Jack Pham62523502018-01-15 16:37:05 -08002613#define NUM_GSI_OUT_EPS 1
2614#define NUM_GSI_IN_EPS 2
2615
2616
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002617static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2618{
2619 struct dwc3 *dwc = dep->dwc;
2620
2621 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2622 dep->endpoint.maxburst = 1;
2623 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2624 if (!dep->direction)
2625 dwc->gadget.ep0 = &dep->endpoint;
2626
2627 dep->endpoint.caps.type_control = true;
2628
2629 return 0;
2630}
2631
Mayank Rana0f67c832018-03-07 14:11:41 -08002632static int dwc3_gadget_init_in_out_endpoint(struct dwc3_ep *dep)
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002633{
2634 struct dwc3 *dwc = dep->dwc;
2635
2636 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2637 dep->endpoint.max_streams = 15;
2638 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2639 list_add_tail(&dep->endpoint.ep_list,
2640 &dwc->gadget.ep_list);
2641 dep->endpoint.caps.type_iso = true;
2642 dep->endpoint.caps.type_bulk = true;
2643 dep->endpoint.caps.type_int = true;
2644
2645 return dwc3_alloc_trb_pool(dep);
2646}
2647
2648static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002649{
2650 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002651 bool direction = epnum & 1;
2652 int ret;
2653 u8 num = epnum >> 1;
2654
2655 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2656 if (!dep)
2657 return -ENOMEM;
2658
2659 dep->dwc = dwc;
2660 dep->number = epnum;
2661 dep->direction = direction;
2662 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2663 dwc->eps[epnum] = dep;
2664
2665 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2666 direction ? "in" : "out");
2667
Hemant Kumar72f784c2018-08-07 16:56:11 -07002668 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002669 dep->endpoint.name = dep->name;
2670
2671 if (!(dep->number > 1)) {
2672 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2673 dep->endpoint.comp_desc = NULL;
2674 }
2675
2676 spin_lock_init(&dep->lock);
2677
2678 if (num == 0)
2679 ret = dwc3_gadget_init_control_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002680 else
Mayank Rana0f67c832018-03-07 14:11:41 -08002681 ret = dwc3_gadget_init_in_out_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002682
2683 if (ret)
2684 return ret;
2685
2686 dep->endpoint.caps.dir_in = direction;
2687 dep->endpoint.caps.dir_out = !direction;
2688
2689 INIT_LIST_HEAD(&dep->pending_list);
2690 INIT_LIST_HEAD(&dep->started_list);
2691
2692 return 0;
2693}
2694
2695static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2696{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002697 u8 epnum;
Jack Pham62523502018-01-15 16:37:05 -08002698 u8 out_count;
2699 u8 in_count;
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002700 u8 idx;
Jack Pham62523502018-01-15 16:37:05 -08002701 struct dwc3_ep *dep;
2702
2703 in_count = out_count = total / 2;
2704 out_count += total & 1; /* in case odd, there is one more OUT */
Felipe Balbi72246da2011-08-19 18:10:58 +03002705
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002706 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2707
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002708 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002709 int ret;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002710 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002711
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002712 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2713 if (ret)
2714 return ret;
Jack Pham62523502018-01-15 16:37:05 -08002715
2716 dep = dwc->eps[epnum];
2717 /* Reserve EPs at the end for GSI */
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002718 if (!dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002719 out_count - NUM_GSI_OUT_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002720 idx = num - (out_count - NUM_GSI_OUT_EPS - 1);
2721 snprintf(dep->name, sizeof(dep->name), "gsi-epout%d",
2722 idx);
Jack Pham62523502018-01-15 16:37:05 -08002723 dep->endpoint.ep_type = EP_TYPE_GSI;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002724 } else if (dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002725 in_count - NUM_GSI_IN_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002726 idx = num - (in_count - NUM_GSI_IN_EPS - 1);
2727 snprintf(dep->name, sizeof(dep->name), "gsi-epin%d",
2728 idx);
Jack Pham62523502018-01-15 16:37:05 -08002729 dep->endpoint.ep_type = EP_TYPE_GSI;
2730 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002731 }
2732
2733 return 0;
2734}
2735
2736static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2737{
2738 struct dwc3_ep *dep;
2739 u8 epnum;
2740
2741 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2742 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002743 if (!dep)
2744 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302745 /*
2746 * Physical endpoints 0 and 1 are special; they form the
2747 * bi-directional USB endpoint 0.
2748 *
2749 * For those two physical endpoints, we don't allocate a TRB
2750 * pool nor do we add them the endpoints list. Due to that, we
2751 * shouldn't do these two operations otherwise we would end up
2752 * with all sorts of bugs when removing dwc3.ko.
2753 */
2754 if (epnum != 0 && epnum != 1) {
2755 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002756 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302757 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002758
2759 kfree(dep);
2760 }
2761}
2762
Felipe Balbi72246da2011-08-19 18:10:58 +03002763/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002764
Felipe Balbi8f608e82018-03-27 10:53:29 +03002765static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2766 struct dwc3_request *req, struct dwc3_trb *trb,
2767 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302768{
2769 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302770
Felipe Balbidc55c672016-08-12 13:20:32 +03002771 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002772
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002773 trace_dwc3_complete_trb(dep, trb);
2774
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002775 /*
2776 * If we're in the middle of series of chained TRBs and we
2777 * receive a short transfer along the way, DWC3 will skip
2778 * through all TRBs including the last TRB in the chain (the
2779 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2780 * bit and SW has to do it manually.
2781 *
2782 * We're going to do that here to avoid problems of HW trying
2783 * to use bogus TRBs for transfers.
2784 */
2785 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2786 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2787
Felipe Balbic6267a52017-01-05 14:58:46 +02002788 /*
2789 * If we're dealing with unaligned size OUT transfer, we will be left
2790 * with one TRB pending in the ring. We need to manually clear HWO bit
2791 * from that TRB.
2792 */
Thinh Nguyen4b977512018-08-02 20:17:16 -07002793 if ((req->zero || req->unaligned) && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002794 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2795 return 1;
2796 }
2797
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302798 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002799 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302800
Felipe Balbi35b27192017-03-08 13:56:37 +02002801 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2802 return 1;
2803
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002804 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302805 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002806
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002807 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302808 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002809
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302810 return 0;
2811}
2812
Felipe Balbid3692952018-03-29 13:32:10 +03002813static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2814 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2815 int status)
2816{
2817 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2818 struct scatterlist *sg = req->sg;
2819 struct scatterlist *s;
2820 unsigned int pending = req->num_pending_sgs;
2821 unsigned int i;
2822 int ret = 0;
2823
2824 for_each_sg(sg, s, pending, i) {
2825 trb = &dep->trb_pool[dep->trb_dequeue];
2826
2827 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2828 break;
2829
2830 req->sg = sg_next(s);
2831 req->num_pending_sgs--;
2832
2833 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2834 trb, event, status, true);
2835 if (ret)
2836 break;
2837 }
2838
2839 return ret;
2840}
2841
2842static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2843 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2844 int status)
2845{
2846 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2847
2848 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2849 event, status, false);
2850}
2851
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002852static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2853{
2854 return req->request.actual == req->request.length;
2855}
2856
Felipe Balbif38e35d2018-04-06 15:56:35 +03002857static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2858 const struct dwc3_event_depevt *event,
2859 struct dwc3_request *req, int status)
2860{
2861 int ret;
2862
2863 if (req->num_pending_sgs)
2864 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2865 status);
2866 else
2867 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2868 status);
2869
2870 if (req->unaligned || req->zero) {
2871 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2872 status);
2873 req->unaligned = false;
2874 req->zero = false;
2875 }
2876
2877 req->request.actual = req->request.length - req->remaining;
2878
2879 if (!dwc3_gadget_ep_request_completed(req) &&
2880 req->num_pending_sgs) {
2881 __dwc3_gadget_kick_transfer(dep);
2882 goto out;
2883 }
2884
2885 dwc3_gadget_giveback(dep, req, status);
2886
2887out:
2888 return ret;
2889}
2890
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002891static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002892 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002893{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002894 struct dwc3_request *req;
Felipe Balbi72246da2011-08-19 18:10:58 +03002895
Hemant Kumar7d2436362018-11-16 17:24:39 -08002896 while (!list_empty(&dep->started_list)) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002897 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002898
Hemant Kumar7d2436362018-11-16 17:24:39 -08002899 req = next_request(&dep->started_list);
Felipe Balbif38e35d2018-04-06 15:56:35 +03002900 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2901 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002902 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002903 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002904 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002905}
2906
Felipe Balbiee3638b2018-03-27 11:26:53 +03002907static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2908 const struct dwc3_event_depevt *event)
2909{
Felipe Balbif62afb42018-04-11 10:34:34 +03002910 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002911}
2912
Felipe Balbi8f608e82018-03-27 10:53:29 +03002913static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2914 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002915{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002916 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002917 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002918 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002919
Felipe Balbiee3638b2018-03-27 11:26:53 +03002920 dwc3_gadget_endpoint_frame_from_event(dep, event);
2921
Felipe Balbi72246da2011-08-19 18:10:58 +03002922 if (event->status & DEPEVT_STATUS_BUSERR)
2923 status = -ECONNRESET;
2924
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002925 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2926 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002927
2928 if (list_empty(&dep->started_list))
2929 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002930 }
2931
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002932 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002933
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002934 if (stop) {
Jack Pham62523502018-01-15 16:37:05 -08002935 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002936 dep->flags = DWC3_EP_ENABLED;
2937 }
2938
Felipe Balbifae2b902011-10-14 13:00:30 +03002939 /*
2940 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2941 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2942 */
2943 if (dwc->revision < DWC3_REVISION_183A) {
2944 u32 reg;
2945 int i;
2946
2947 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002948 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002949
2950 if (!(dep->flags & DWC3_EP_ENABLED))
2951 continue;
2952
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002953 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002954 return;
2955 }
2956
2957 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2958 reg |= dwc->u1u2;
2959 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2960
2961 dwc->u1u2 = 0;
2962 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002963}
2964
Felipe Balbi8f608e82018-03-27 10:53:29 +03002965static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2966 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002967{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002968 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002969 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002970}
2971
Felipe Balbi72246da2011-08-19 18:10:58 +03002972static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2973 const struct dwc3_event_depevt *event)
2974{
2975 struct dwc3_ep *dep;
2976 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002977 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002978
2979 dep = dwc->eps[epnum];
2980
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002981 if (!(dep->flags & DWC3_EP_ENABLED)) {
2982 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2983 return;
2984
2985 /* Handle only EPCMDCMPLT when EP disabled */
2986 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2987 return;
2988 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002989
Felipe Balbi72246da2011-08-19 18:10:58 +03002990 if (epnum == 0 || epnum == 1) {
2991 dwc3_ep0_interrupt(dwc, event);
2992 return;
2993 }
2994
Jack Phama4c7b782018-01-18 14:19:38 -08002995 dep->dbg_ep_events.total++;
2996
Felipe Balbi72246da2011-08-19 18:10:58 +03002997 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002998 case DWC3_DEPEVT_XFERINPROGRESS:
Jack Phama4c7b782018-01-18 14:19:38 -08002999 dep->dbg_ep_events.xferinprogress++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03003000 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003001 break;
3002 case DWC3_DEPEVT_XFERNOTREADY:
Jack Phama4c7b782018-01-18 14:19:38 -08003003 dep->dbg_ep_events.xfernotready++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03003004 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003005 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 case DWC3_DEPEVT_EPCMDCMPLT:
Jack Phama4c7b782018-01-18 14:19:38 -08003007 dep->dbg_ep_events.epcmdcomplete++;
Baolin Wang76a638f2016-10-31 19:38:36 +08003008 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3009
3010 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
3011 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3012 wake_up(&dep->wait_end_transfer);
3013 }
3014 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03003015 case DWC3_DEPEVT_STREAMEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08003016 dep->dbg_ep_events.streamevent++;
3017 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003018 case DWC3_DEPEVT_XFERCOMPLETE:
Jack Phama4c7b782018-01-18 14:19:38 -08003019 dep->dbg_ep_events.xfercomplete++;
3020 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003021 case DWC3_DEPEVT_RXTXFIFOEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08003022 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003023 break;
3024 }
3025}
3026
3027static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3028{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003029 struct usb_gadget_driver *gadget_driver;
3030
Felipe Balbi72246da2011-08-19 18:10:58 +03003031 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003032 gadget_driver = dwc->gadget_driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03003033 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003034 dbg_event(0xFF, "DISCONNECT", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003035 gadget_driver->disconnect(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003036 spin_lock(&dwc->lock);
3037 }
3038}
3039
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003040static void dwc3_suspend_gadget(struct dwc3 *dwc)
3041{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003042 struct usb_gadget_driver *gadget_driver;
3043
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003044 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003045 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003046 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003047 dbg_event(0xFF, "SUSPEND", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003048 gadget_driver->suspend(&dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003049 spin_lock(&dwc->lock);
3050 }
3051}
3052
3053static void dwc3_resume_gadget(struct dwc3 *dwc)
3054{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003055 struct usb_gadget_driver *gadget_driver;
3056
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003057 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003058 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003059 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003060 dbg_event(0xFF, "RESUME", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003061 gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003062 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003063 }
3064}
3065
3066static void dwc3_reset_gadget(struct dwc3 *dwc)
3067{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003068 struct usb_gadget_driver *gadget_driver;
3069
Felipe Balbi8e744752014-11-06 14:27:53 +08003070 if (!dwc->gadget_driver)
3071 return;
3072
3073 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003074 gadget_driver = dwc->gadget_driver;
Felipe Balbi8e744752014-11-06 14:27:53 +08003075 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003076 dbg_event(0xFF, "UDC RESET", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003077 usb_gadget_udc_reset(&dwc->gadget, gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003078 spin_lock(&dwc->lock);
3079 }
3080}
3081
Jack Pham62523502018-01-15 16:37:05 -08003082void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03003083{
Jack Pham62523502018-01-15 16:37:05 -08003084 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003085 struct dwc3_gadget_ep_cmd_params params;
3086 u32 cmd;
3087 int ret;
3088
Jack Pham62523502018-01-15 16:37:05 -08003089 dep = dwc->eps[epnum];
3090
Baolin Wang76a638f2016-10-31 19:38:36 +08003091 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
3092 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303093 return;
3094
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003095 if (dep->endpoint.endless)
3096 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
3097 dep->number);
3098
Pratyush Anand57911502012-07-06 15:19:10 +05303099 /*
3100 * NOTICE: We are violating what the Databook says about the
3101 * EndTransfer command. Ideally we would _always_ wait for the
3102 * EndTransfer Command Completion IRQ, but that's causing too
3103 * much trouble synchronizing between us and gadget driver.
3104 *
3105 * We have discussed this with the IP Provider and it was
3106 * suggested to giveback all requests here, but give HW some
3107 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01003108 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05303109 *
3110 * Note also that a similar handling was tested by Synopsys
3111 * (thanks a lot Paul) and nothing bad has come out of it.
3112 * In short, what we're doing is:
3113 *
3114 * - Issue EndTransfer WITH CMDIOC bit set
3115 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07003116 *
3117 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3118 * supports a mode to work around the above limitation. The
3119 * software can poll the CMDACT bit in the DEPCMD register
3120 * after issuing a EndTransfer command. This mode is enabled
3121 * by writing GUCTL2[14]. This polling is already done in the
3122 * dwc3_send_gadget_ep_cmd() function so if the mode is
3123 * enabled, the EndTransfer command will have completed upon
3124 * returning from this function and we don't need to delay for
3125 * 100us.
3126 *
3127 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303128 */
3129
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303130 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003131 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3132 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03003133 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303134 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003135 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303136 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003137 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003138
Baolin Wang76a638f2016-10-31 19:38:36 +08003139 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
Mayank Rana8bd2a062018-06-28 11:08:52 -07003140 if (dep->endpoint.ep_type != EP_TYPE_GSI)
3141 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07003142 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08003143 }
Mayank Rana21f76b32018-09-21 16:49:55 -07003144 dbg_log_string("%s(%d): endxfer ret:%d)",
3145 dep->name, dep->number, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003146}
3147
Hemant Kumar7b7e0652019-04-09 18:00:55 -07003148void dwc3_stop_active_transfer_noioc(struct dwc3 *dwc, u32 epnum, bool force)
3149{
3150 struct dwc3_ep *dep;
3151 struct dwc3_gadget_ep_cmd_params params;
3152 u32 cmd;
3153 int ret;
3154
3155 dep = dwc->eps[epnum];
3156
3157 if (!dep->resource_index)
3158 return;
3159
3160 if (dep->endpoint.endless)
3161 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
3162 dep->number);
3163
3164 cmd = DWC3_DEPCMD_ENDTRANSFER;
3165 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3166 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3167 memset(&params, 0, sizeof(params));
3168 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
3169 WARN_ON_ONCE(ret);
3170 dep->resource_index = 0;
3171
3172 dbg_log_string("%s(%d): endxfer ret:%d)",
3173 dep->name, dep->number, ret);
3174}
3175
Felipe Balbi72246da2011-08-19 18:10:58 +03003176static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3177{
3178 u32 epnum;
3179
3180 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3181 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003182 int ret;
3183
3184 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003185 if (!dep)
3186 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003187
3188 if (!(dep->flags & DWC3_EP_STALL))
3189 continue;
3190
3191 dep->flags &= ~DWC3_EP_STALL;
3192
John Youn50c763f2016-05-31 17:49:56 -07003193 ret = dwc3_send_clear_stall_ep_cmd(dep);
Jack Pham62523502018-01-15 16:37:05 -08003194 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003195 WARN_ON_ONCE(ret);
3196 }
3197}
3198
3199static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3200{
Felipe Balbic4430a22012-05-24 10:30:01 +03003201 int reg;
3202
Jack Pham62523502018-01-15 16:37:05 -08003203 dbg_event(0xFF, "DISCONNECT INT", 0);
3204 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3205 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003206 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003207
Felipe Balbi72246da2011-08-19 18:10:58 +03003208 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3209 reg &= ~DWC3_DCTL_INITU1ENA;
3210 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3211
3212 reg &= ~DWC3_DCTL_INITU2ENA;
3213 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003214
Felipe Balbi72246da2011-08-19 18:10:58 +03003215 dwc3_disconnect_gadget(dwc);
3216
3217 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003218 dwc->setup_packet_pending = false;
Jack Pham62523502018-01-15 16:37:05 -08003219 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05003220 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003221
3222 dwc->connected = false;
Jack Pham62523502018-01-15 16:37:05 -08003223 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003224}
3225
Felipe Balbi72246da2011-08-19 18:10:58 +03003226static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3227{
3228 u32 reg;
3229
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003230 usb_phy_start_link_training(dwc->usb3_phy);
3231
Felipe Balbifc8bb912016-05-16 13:14:48 +03003232 dwc->connected = true;
3233
Felipe Balbidf62df52011-10-14 15:11:49 +03003234 /*
3235 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3236 * would cause a missing Disconnect Event if there's a
3237 * pending Setup Packet in the FIFO.
3238 *
3239 * There's no suggested workaround on the official Bug
3240 * report, which states that "unless the driver/application
3241 * is doing any special handling of a disconnect event,
3242 * there is no functional issue".
3243 *
3244 * Unfortunately, it turns out that we _do_ some special
3245 * handling of a disconnect event, namely complete all
3246 * pending transfers, notify gadget driver of the
3247 * disconnection, and so on.
3248 *
3249 * Our suggested workaround is to follow the Disconnect
3250 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003251 * flag. Such flag gets set whenever we have a SETUP_PENDING
3252 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003253 * same endpoint.
3254 *
3255 * Refers to:
3256 *
3257 * STAR#9000466709: RTL: Device : Disconnect event not
3258 * generated if setup packet pending in FIFO
3259 */
3260 if (dwc->revision < DWC3_REVISION_188A) {
3261 if (dwc->setup_packet_pending)
3262 dwc3_gadget_disconnect_interrupt(dwc);
3263 }
3264
Jack Pham62523502018-01-15 16:37:05 -08003265 dbg_event(0xFF, "BUS RESET", 0);
3266 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3267 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003268 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003269
3270 usb_gadget_vbus_draw(&dwc->gadget, 100);
3271
Felipe Balbi8e744752014-11-06 14:27:53 +08003272 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003273
3274 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3275 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3276 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003277 dwc->test_mode = false;
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303278 /*
3279 * From SNPS databook section 8.1.2
3280 * the EP0 should be in setup phase. So ensure
3281 * that EP0 is in setup phase by issuing a stall
3282 * and restart if EP0 is not in setup phase.
3283 */
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303284 if (dwc->ep0state != EP0_SETUP_PHASE) {
3285 unsigned int dir;
3286
3287 dbg_event(0xFF, "CONTRPEND", dwc->ep0state);
3288 dir = !!dwc->ep0_expect_in;
3289 if (dwc->ep0state == EP0_DATA_PHASE)
3290 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
3291 else
3292 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303293 dwc3_ep0_stall_and_restart(dwc);
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303294 }
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303295
3296 dwc3_stop_active_transfers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003297 dwc3_clear_stall_all_ep(dwc);
3298
3299 /* Reset device address to zero */
3300 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3301 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3302 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Jack Pham62523502018-01-15 16:37:05 -08003303
3304 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3305 dwc->link_state = DWC3_LINK_STATE_U0;
3306 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003307}
3308
Felipe Balbi72246da2011-08-19 18:10:58 +03003309static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3310{
Felipe Balbi72246da2011-08-19 18:10:58 +03003311 struct dwc3_ep *dep;
3312 int ret;
3313 u32 reg;
3314 u8 speed;
3315
Jack Pham62523502018-01-15 16:37:05 -08003316 dbg_event(0xFF, "CONNECT DONE", 0);
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003317 usb_phy_stop_link_training(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +03003318 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3319 speed = reg & DWC3_DSTS_CONNECTSPD;
3320 dwc->speed = speed;
3321
Jack Pham74cc2572019-04-02 18:39:41 -07003322 /* Enable SUSPENDEVENT(BIT:6) for version 230A and above */
3323 if (dwc->revision >= DWC3_REVISION_230A) {
3324 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
3325 reg |= DWC3_DEVTEN_EOPFEN;
3326 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
3327 }
3328
John Youn5fb6fda2016-11-10 17:23:25 -08003329 /*
3330 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3331 * each time on Connect Done.
3332 *
3333 * Currently we always use the reset value. If any platform
3334 * wants to set this to a different value, we need to add a
3335 * setting and update GCTL.RAMCLKSEL here.
3336 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003337
3338 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003339 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003340 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3341 dwc->gadget.ep0->maxpacket = 512;
3342 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3343 break;
John Youn2da9ad72016-05-20 16:34:26 -07003344 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003345 /*
3346 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3347 * would cause a missing USB3 Reset event.
3348 *
3349 * In such situations, we should force a USB3 Reset
3350 * event by calling our dwc3_gadget_reset_interrupt()
3351 * routine.
3352 *
3353 * Refers to:
3354 *
3355 * STAR#9000483510: RTL: SS : USB3 reset event may
3356 * not be generated always when the link enters poll
3357 */
3358 if (dwc->revision < DWC3_REVISION_190A)
3359 dwc3_gadget_reset_interrupt(dwc);
3360
Felipe Balbi72246da2011-08-19 18:10:58 +03003361 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3362 dwc->gadget.ep0->maxpacket = 512;
3363 dwc->gadget.speed = USB_SPEED_SUPER;
3364 break;
John Youn2da9ad72016-05-20 16:34:26 -07003365 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003366 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3367 dwc->gadget.ep0->maxpacket = 64;
3368 dwc->gadget.speed = USB_SPEED_HIGH;
3369 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003370 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003371 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3372 dwc->gadget.ep0->maxpacket = 64;
3373 dwc->gadget.speed = USB_SPEED_FULL;
3374 break;
John Youn2da9ad72016-05-20 16:34:26 -07003375 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003376 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3377 dwc->gadget.ep0->maxpacket = 8;
3378 dwc->gadget.speed = USB_SPEED_LOW;
3379 break;
3380 }
3381
Thinh Nguyen61800262018-01-12 18:18:05 -08003382 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3383
Pratyush Anand2b758352013-01-14 15:59:31 +05303384 /* Enable USB2 LPM Capability */
3385
John Younee5cd412016-02-05 17:08:45 -08003386 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003387 (speed != DWC3_DSTS_SUPERSPEED) &&
3388 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303389 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3390 reg |= DWC3_DCFG_LPM_CAP;
3391 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3392
3393 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3394 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3395
Huang Rui460d0982014-10-31 11:11:18 +08003396 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303397
Huang Rui80caf7d2014-10-28 19:54:26 +08003398 /*
3399 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3400 * DCFG.LPMCap is set, core responses with an ACK and the
3401 * BESL value in the LPM token is less than or equal to LPM
3402 * NYET threshold.
3403 */
3404 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3405 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003406 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003407
3408 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3409 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3410
Pratyush Anand2b758352013-01-14 15:59:31 +05303411 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003412 } else {
3413 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3414 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3415 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303416 }
3417
Felipe Balbi72246da2011-08-19 18:10:58 +03003418 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003419 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003420 if (ret) {
3421 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3422 return;
3423 }
3424
3425 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003426 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003427 if (ret) {
3428 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3429 return;
3430 }
3431
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003432 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003433
Felipe Balbi72246da2011-08-19 18:10:58 +03003434 /*
3435 * Configure PHY via GUSB3PIPECTLn if required.
3436 *
3437 * Update GTXFIFOSIZn
3438 *
3439 * In both cases reset values should be sufficient.
3440 */
3441}
3442
Jack Pham62523502018-01-15 16:37:05 -08003443static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003444{
Jack Pham62523502018-01-15 16:37:05 -08003445 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003446
Jack Pham62523502018-01-15 16:37:05 -08003447 dev_dbg(dwc->dev, "%s\n", __func__);
3448
3449 dbg_event(0xFF, "WAKEUP", remote_wakeup);
3450 /*
3451 * Identify if it is called from wakeup_interrupt() context for bus
3452 * resume or as part of remote wakeup. And based on that check for
3453 * U3 state. as we need to handle case of L1 resume i.e. where we
3454 * don't want to perform resume.
3455 */
3456 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3457 perform_resume = false;
3458
3459 /* Only perform resume from L2 or Early Suspend states */
3460 if (perform_resume) {
3461
3462 /*
3463 * In case of remote wake up dwc3_gadget_wakeup_work()
3464 * is doing pm_runtime_get_sync().
3465 */
3466 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3467 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003468 dwc3_notify_event(dwc,
3469 DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003470
3471 /*
3472 * set state to U0 as function level resume is trying to queue
3473 * notification over USB interrupt endpoint which would fail
3474 * due to state is not being updated.
3475 */
3476 dwc->link_state = DWC3_LINK_STATE_U0;
3477 dwc3_resume_gadget(dwc);
3478 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003479 }
Jack Pham62523502018-01-15 16:37:05 -08003480
3481 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003482}
3483
3484static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3485 unsigned int evtinfo)
3486{
Felipe Balbifae2b902011-10-14 13:00:30 +03003487 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003488 unsigned int pwropt;
3489
3490 /*
3491 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3492 * Hibernation mode enabled which would show up when device detects
3493 * host-initiated U3 exit.
3494 *
3495 * In that case, device will generate a Link State Change Interrupt
3496 * from U3 to RESUME which is only necessary if Hibernation is
3497 * configured in.
3498 *
3499 * There are no functional changes due to such spurious event and we
3500 * just need to ignore it.
3501 *
3502 * Refers to:
3503 *
3504 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3505 * operational mode
3506 */
3507 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3508 if ((dwc->revision < DWC3_REVISION_250A) &&
3509 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3510 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3511 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003512 return;
3513 }
3514 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003515
3516 /*
3517 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3518 * on the link partner, the USB session might do multiple entry/exit
3519 * of low power states before a transfer takes place.
3520 *
3521 * Due to this problem, we might experience lower throughput. The
3522 * suggested workaround is to disable DCTL[12:9] bits if we're
3523 * transitioning from U1/U2 to U0 and enable those bits again
3524 * after a transfer completes and there are no pending transfers
3525 * on any of the enabled endpoints.
3526 *
3527 * This is the first half of that workaround.
3528 *
3529 * Refers to:
3530 *
3531 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3532 * core send LGO_Ux entering U0
3533 */
3534 if (dwc->revision < DWC3_REVISION_183A) {
3535 if (next == DWC3_LINK_STATE_U0) {
3536 u32 u1u2;
3537 u32 reg;
3538
3539 switch (dwc->link_state) {
3540 case DWC3_LINK_STATE_U1:
3541 case DWC3_LINK_STATE_U2:
3542 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3543 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3544 | DWC3_DCTL_ACCEPTU2ENA
3545 | DWC3_DCTL_INITU1ENA
3546 | DWC3_DCTL_ACCEPTU1ENA);
3547
3548 if (!dwc->u1u2)
3549 dwc->u1u2 = reg & u1u2;
3550
3551 reg &= ~u1u2;
3552
3553 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3554 break;
3555 default:
3556 /* do nothing */
3557 break;
3558 }
3559 }
3560 }
3561
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003562 switch (next) {
3563 case DWC3_LINK_STATE_U1:
3564 if (dwc->speed == USB_SPEED_SUPER)
3565 dwc3_suspend_gadget(dwc);
3566 break;
3567 case DWC3_LINK_STATE_U2:
3568 case DWC3_LINK_STATE_U3:
3569 dwc3_suspend_gadget(dwc);
3570 break;
3571 case DWC3_LINK_STATE_RESUME:
3572 dwc3_resume_gadget(dwc);
3573 break;
3574 default:
3575 /* do nothing */
3576 break;
3577 }
3578
Jack Pham62523502018-01-15 16:37:05 -08003579 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003580 dwc->link_state = next;
Jack Pham62523502018-01-15 16:37:05 -08003581 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003582}
3583
Baolin Wang72704f82016-05-16 16:43:53 +08003584static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3585 unsigned int evtinfo)
3586{
3587 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3588
Jack Pham62523502018-01-15 16:37:05 -08003589 dbg_event(0xFF, "SUSPEND INT", 0);
3590 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3591
3592 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3593 /*
3594 * When first connecting the cable, even before the initial
3595 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3596 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3597 * event. In such a case, ignore.
3598 * Ignore suspend event until device side usb is not into
3599 * CONFIGURED state.
3600 */
3601 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3602 dev_err(dwc->dev, "%s(): state:%d. Ignore SUSPEND.\n",
3603 __func__, dwc->gadget.state);
3604 return;
3605 }
3606
Baolin Wang72704f82016-05-16 16:43:53 +08003607 dwc3_suspend_gadget(dwc);
3608
Jack Pham62523502018-01-15 16:37:05 -08003609 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3610 dwc->b_suspend = true;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003611 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003612 }
3613
Baolin Wang72704f82016-05-16 16:43:53 +08003614 dwc->link_state = next;
3615}
3616
Felipe Balbie1dadd32014-02-25 14:47:54 -06003617static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3618 unsigned int evtinfo)
3619{
3620 unsigned int is_ss = evtinfo & BIT(4);
3621
Felipe Balbibfad65e2017-04-19 14:59:27 +03003622 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003623 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3624 * have a known issue which can cause USB CV TD.9.23 to fail
3625 * randomly.
3626 *
3627 * Because of this issue, core could generate bogus hibernation
3628 * events which SW needs to ignore.
3629 *
3630 * Refers to:
3631 *
3632 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3633 * Device Fallback from SuperSpeed
3634 */
3635 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3636 return;
3637
3638 /* enter hibernation here */
3639}
3640
Felipe Balbi72246da2011-08-19 18:10:58 +03003641static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3642 const struct dwc3_event_devt *event)
3643{
3644 switch (event->type) {
3645 case DWC3_DEVICE_EVENT_DISCONNECT:
3646 dwc3_gadget_disconnect_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003647 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003648 break;
3649 case DWC3_DEVICE_EVENT_RESET:
3650 dwc3_gadget_reset_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003651 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003652 break;
3653 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3654 dwc3_gadget_conndone_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003655 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003656 break;
3657 case DWC3_DEVICE_EVENT_WAKEUP:
Jack Pham62523502018-01-15 16:37:05 -08003658 dwc3_gadget_wakeup_interrupt(dwc, false);
Jack Phama4c7b782018-01-18 14:19:38 -08003659 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003660 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003661 case DWC3_DEVICE_EVENT_HIBER_REQ:
3662 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3663 "unexpected hibernation event\n"))
3664 break;
3665
3666 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3667 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003668 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3669 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Jack Phama4c7b782018-01-18 14:19:38 -08003670 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003671 break;
3672 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003673 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003674 if (dwc->revision >= DWC3_REVISION_230A) {
Jack Pham62523502018-01-15 16:37:05 -08003675 dbg_event(0xFF, "GAD SUS", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003676 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003677 /*
3678 * Ignore suspend event until the gadget enters into
3679 * USB_STATE_CONFIGURED state.
3680 */
3681 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3682 dwc3_gadget_suspend_interrupt(dwc,
3683 event->event_info);
Jack Pham51c5d1f2018-11-02 17:46:17 -07003684 else
3685 usb_gadget_vbus_draw(&dwc->gadget, 2);
Baolin Wang72704f82016-05-16 16:43:53 +08003686 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003687 break;
3688 case DWC3_DEVICE_EVENT_SOF:
Jack Phama4c7b782018-01-18 14:19:38 -08003689 dwc->dbg_gadget_events.sof++;
3690 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003691 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Jack Pham62523502018-01-15 16:37:05 -08003692 dbg_event(0xFF, "ERROR", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003693 dwc->dbg_gadget_events.erratic_error++;
Jack Pham62523502018-01-15 16:37:05 -08003694 dwc->err_evt_seen = true;
Jack Phama4c7b782018-01-18 14:19:38 -08003695 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003696 case DWC3_DEVICE_EVENT_CMD_CMPL:
Jack Phama4c7b782018-01-18 14:19:38 -08003697 dwc->dbg_gadget_events.cmdcmplt++;
3698 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003699 case DWC3_DEVICE_EVENT_OVERFLOW:
Jack Phama4c7b782018-01-18 14:19:38 -08003700 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003701 break;
3702 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003703 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Jack Phama4c7b782018-01-18 14:19:38 -08003704 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003705 }
3706}
3707
3708static void dwc3_process_event_entry(struct dwc3 *dwc,
3709 const union dwc3_event *event)
3710{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003711 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003712
Felipe Balbidfc5e802017-04-26 13:44:51 +03003713 if (!event->type.is_devspec)
3714 dwc3_endpoint_interrupt(dwc, &event->depevt);
3715 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003716 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003717 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003718 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003719}
3720
Felipe Balbidea520a2016-03-30 09:39:34 +03003721static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003722{
Felipe Balbidea520a2016-03-30 09:39:34 +03003723 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003724 irqreturn_t ret = IRQ_NONE;
3725 int left;
3726 u32 reg;
3727
Felipe Balbif42f2442013-06-12 21:25:08 +03003728 left = evt->count;
3729
3730 if (!(evt->flags & DWC3_EVENT_PENDING))
3731 return IRQ_NONE;
3732
3733 while (left > 0) {
3734 union dwc3_event event;
3735
John Younebbb2d52016-11-15 13:07:02 +02003736 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003737
3738 dwc3_process_event_entry(dwc, &event);
3739
Jack Pham62523502018-01-15 16:37:05 -08003740 if (dwc->err_evt_seen) {
3741 /*
3742 * if erratic error, skip remaining events
3743 * while controller undergoes reset
3744 */
3745 evt->lpos = (evt->lpos + left) %
3746 DWC3_EVENT_BUFFERS_SIZE;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003747 if (dwc3_notify_event(dwc,
3748 DWC3_CONTROLLER_ERROR_EVENT, 0))
Jack Pham62523502018-01-15 16:37:05 -08003749 dwc->err_evt_seen = 0;
3750 break;
3751 }
3752
Felipe Balbif42f2442013-06-12 21:25:08 +03003753 /*
3754 * FIXME we wrap around correctly to the next entry as
3755 * almost all entries are 4 bytes in size. There is one
3756 * entry which has 12 bytes which is a regular entry
3757 * followed by 8 bytes data. ATM I don't know how
3758 * things are organized if we get next to the a
3759 * boundary so I worry about that once we try to handle
3760 * that.
3761 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003762 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003763 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003764 }
3765
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003766 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003767 evt->count = 0;
3768 evt->flags &= ~DWC3_EVENT_PENDING;
3769 ret = IRQ_HANDLED;
3770
3771 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003772 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003773 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003774 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003775
John Youncf40b862016-11-14 12:32:43 -08003776 if (dwc->imod_interval) {
3777 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3778 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3779 }
3780
Felipe Balbif42f2442013-06-12 21:25:08 +03003781 return ret;
3782}
3783
Jack Pham62523502018-01-15 16:37:05 -08003784void dwc3_bh_work(struct work_struct *w)
3785{
3786 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3787
3788 pm_runtime_get_sync(dwc->dev);
3789 dwc3_thread_interrupt(dwc->irq, dwc->ev_buf);
3790 pm_runtime_put(dwc->dev);
3791}
3792
Felipe Balbidea520a2016-03-30 09:39:34 +03003793static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003794{
Felipe Balbidea520a2016-03-30 09:39:34 +03003795 struct dwc3_event_buffer *evt = _evt;
3796 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003797 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003798 irqreturn_t ret = IRQ_NONE;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003799 ktime_t start_time;
3800
3801 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003802
Felipe Balbie5f68b42015-10-12 13:25:44 -05003803 spin_lock_irqsave(&dwc->lock, flags);
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003804 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] = 0;
Felipe Balbidea520a2016-03-30 09:39:34 +03003805 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05003806 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003807
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003808 dwc->bh_completion_time[dwc->irq_dbg_index] =
3809 ktime_to_us(ktime_sub(ktime_get(), start_time));
3810 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3811
Felipe Balbib15a7622011-06-30 16:57:15 +03003812 return ret;
3813}
3814
Felipe Balbidea520a2016-03-30 09:39:34 +03003815static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003816{
Felipe Balbidea520a2016-03-30 09:39:34 +03003817 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003818 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003819 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003820 u32 reg;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003821 ktime_t start_time;
3822
3823 start_time = ktime_get();
3824 dwc->irq_cnt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003825
Jack Pham62523502018-01-15 16:37:05 -08003826 /* controller reset is still pending */
3827 if (dwc->err_evt_seen)
Felipe Balbifc8bb912016-05-16 13:14:48 +03003828 return IRQ_HANDLED;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003829
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003830 /*
3831 * With PCIe legacy interrupt, test shows that top-half irq handler can
3832 * be called again after HW interrupt deassertion. Check if bottom-half
3833 * irq event handler completes before caching new event to prevent
3834 * losing events.
3835 */
3836 if (evt->flags & DWC3_EVENT_PENDING)
3837 return IRQ_HANDLED;
3838
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003839 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003840 count &= DWC3_GEVNTCOUNT_MASK;
3841 if (!count)
3842 return IRQ_NONE;
3843
Felipe Balbib15a7622011-06-30 16:57:15 +03003844 evt->count = count;
3845 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003846
Felipe Balbie8adfc32013-06-12 21:11:14 +03003847 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003848 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003849 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003850 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003851
John Younebbb2d52016-11-15 13:07:02 +02003852 amount = min(count, evt->length - evt->lpos);
3853 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3854
3855 if (amount < count)
3856 memcpy(evt->cache, evt->buf, count - amount);
3857
John Youn65aca322016-11-15 13:08:59 +02003858 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3859
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003860 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3861 dwc->irq_completion_time[dwc->irq_dbg_index] =
3862 ktime_us_delta(ktime_get(), start_time);
3863 dwc->irq_event_count[dwc->irq_dbg_index] = count / 4;
3864 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3865
Felipe Balbib15a7622011-06-30 16:57:15 +03003866 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003867}
3868
Jack Pham62523502018-01-15 16:37:05 -08003869irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003870{
Jack Pham62523502018-01-15 16:37:05 -08003871 struct dwc3 *dwc = _dwc;
3872 irqreturn_t ret = IRQ_NONE;
3873 irqreturn_t status;
Felipe Balbi72246da2011-08-19 18:10:58 +03003874
Jack Pham62523502018-01-15 16:37:05 -08003875 status = dwc3_check_event_buf(dwc->ev_buf);
3876 if (status == IRQ_WAKE_THREAD)
3877 ret = status;
3878
3879 if (ret == IRQ_WAKE_THREAD)
3880 queue_work(dwc->dwc_wq, &dwc->bh_work);
3881
3882 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003883}
3884
Felipe Balbi6db38122016-10-03 11:27:01 +03003885static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3886{
3887 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3888 int irq;
3889
3890 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3891 if (irq > 0)
3892 goto out;
3893
3894 if (irq == -EPROBE_DEFER)
3895 goto out;
3896
3897 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3898 if (irq > 0)
3899 goto out;
3900
3901 if (irq == -EPROBE_DEFER)
3902 goto out;
3903
3904 irq = platform_get_irq(dwc3_pdev, 0);
3905 if (irq > 0)
3906 goto out;
3907
3908 if (irq != -EPROBE_DEFER)
3909 dev_err(dwc->dev, "missing peripheral IRQ\n");
3910
3911 if (!irq)
3912 irq = -EINVAL;
3913
3914out:
3915 return irq;
3916}
3917
Felipe Balbi72246da2011-08-19 18:10:58 +03003918/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003919 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003920 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003921 *
3922 * Returns 0 on success otherwise negative errno.
3923 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003924int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003925{
Felipe Balbi6db38122016-10-03 11:27:01 +03003926 int ret;
3927 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003928
Felipe Balbi6db38122016-10-03 11:27:01 +03003929 irq = dwc3_gadget_get_irq(dwc);
3930 if (irq < 0) {
3931 ret = irq;
3932 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003933 }
3934
3935 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003936
Jack Pham62523502018-01-15 16:37:05 -08003937 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3938
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303939 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3940 sizeof(*dwc->ep0_trb) * 2,
3941 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003942 if (!dwc->ep0_trb) {
3943 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3944 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003945 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003946 }
3947
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003948 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003949 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003950 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003951 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003952 }
3953
Felipe Balbi905dc042017-01-05 14:46:52 +02003954 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3955 &dwc->bounce_addr, GFP_KERNEL);
3956 if (!dwc->bounce) {
3957 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003958 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003959 }
3960
Baolin Wangbb014732016-10-14 17:11:33 +08003961 init_completion(&dwc->ep0_in_setup);
3962
Mayank Rana0f67c832018-03-07 14:11:41 -08003963 dwc->gadget.ops = &dwc3_gadget_ops;
3964 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3965 dwc->gadget.sg_supported = true;
3966 dwc->gadget.name = "dwc3-gadget";
3967 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003968
3969 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003970 * FIXME We might be setting max_speed to <SUPER, however versions
3971 * <2.20a of dwc3 have an issue with metastability (documented
3972 * elsewhere in this driver) which tells us we can't set max speed to
3973 * anything lower than SUPER.
3974 *
3975 * Because gadget.max_speed is only used by composite.c and function
3976 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3977 * to happen so we avoid sending SuperSpeed Capability descriptor
3978 * together with our BOS descriptor as that could confuse host into
3979 * thinking we can handle super speed.
3980 *
3981 * Note that, in fact, we won't even support GetBOS requests when speed
3982 * is less than super speed because we don't have means, yet, to tell
3983 * composite.c that we are USB 2.0 + LPM ECN.
3984 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003985 if (dwc->revision < DWC3_REVISION_220A &&
3986 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003987 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003988 dwc->revision);
3989
3990 dwc->gadget.max_speed = dwc->maximum_speed;
3991
3992 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003993 * REVISIT: Here we should clear all pending IRQs to be
3994 * sure we're starting from a well known location.
3995 */
3996
Mayank Rana0f67c832018-03-07 14:11:41 -08003997 dwc->num_eps = DWC3_ENDPOINTS_NUM;
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003998 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003999 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03004000 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03004001
Felipe Balbi72246da2011-08-19 18:10:58 +03004002 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
4003 if (ret) {
4004 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03004005 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03004006 }
4007
4008 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004009
4010err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004011 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03004012
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004013err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004014 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4015 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004016
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004017err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004018 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004019
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004020err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304021 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004022 dwc->ep0_trb, dwc->ep0_trb_addr);
4023
Felipe Balbi72246da2011-08-19 18:10:58 +03004024err0:
4025 return ret;
4026}
4027
Felipe Balbi7415f172012-04-30 14:56:33 +03004028/* -------------------------------------------------------------------------- */
4029
Felipe Balbi72246da2011-08-19 18:10:58 +03004030void dwc3_gadget_exit(struct dwc3 *dwc)
4031{
Felipe Balbi72246da2011-08-19 18:10:58 +03004032 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004033 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02004034 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004035 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004036 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304037 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004038 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004039}
Felipe Balbi7415f172012-04-30 14:56:33 +03004040
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004041int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004042{
Roger Quadros9772b472016-04-12 11:33:29 +03004043 if (!dwc->gadget_driver)
4044 return 0;
4045
Roger Quadros1551e352017-02-15 14:16:26 +02004046 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004047 dwc3_disconnect_gadget(dwc);
4048 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004049
Bo He03a5d4d2019-01-14 09:48:32 +02004050 synchronize_irq(dwc->irq_gadget);
4051
Felipe Balbi7415f172012-04-30 14:56:33 +03004052 return 0;
4053}
4054
4055int dwc3_gadget_resume(struct dwc3 *dwc)
4056{
Felipe Balbi7415f172012-04-30 14:56:33 +03004057 int ret;
4058
Roger Quadros9772b472016-04-12 11:33:29 +03004059 if (!dwc->gadget_driver)
4060 return 0;
4061
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004062 ret = __dwc3_gadget_start(dwc);
4063 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004064 goto err0;
4065
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004066 ret = dwc3_gadget_run_stop(dwc, true, false);
4067 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004068 goto err1;
4069
Felipe Balbi7415f172012-04-30 14:56:33 +03004070 return 0;
4071
4072err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004073 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004074
4075err0:
4076 return ret;
4077}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004078
4079void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4080{
4081 if (dwc->pending_events) {
4082 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4083 dwc->pending_events = false;
4084 enable_irq(dwc->irq_gadget);
4085 }
4086}