blob: 4777bde8e457cf6f4ebd185198c070aae39922de [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) &&
Pratham Pratapfaea49f2018-08-16 19:05:26 +0530229 dep->endpoint.ep_type == EP_TYPE_GSI
230 && dwc3_is_usb31(dwc))
Mayank Ranab259e002018-06-08 16:11:51 -0700231 mult = 6;
232
Mayank Rana4c99f662017-04-25 13:48:46 -0700233 tmp = ((max_packet + mdwidth) * mult) + mdwidth;
234 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
235 dep->fifo_depth = fifo_size;
Mayank Rana1fbcd4d2018-03-12 14:34:21 -0700236
237 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
238 if (dwc3_is_usb31(dwc))
239 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
240 else
241 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
242
243 fifo_size |= (size + (dwc->last_fifo_depth << 16));
244 if (dwc3_is_usb31(dwc))
245 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEF(fifo_size);
246 else
247 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEF(fifo_size);
Mayank Rana4c99f662017-04-25 13:48:46 -0700248
249 dev_dbg(dwc->dev, "%s ep_num:%d last_fifo_depth:%04x fifo_depth:%d\n",
250 dep->endpoint.name, dep->endpoint.ep_num, dwc->last_fifo_depth,
251 dep->fifo_depth);
252
253 dbg_event(0xFF, "resize_fifo", dep->number);
254 dbg_event(0xFF, "fifo_depth", dep->fifo_depth);
255 /* Check fifo size allocation doesn't exceed available RAM size. */
256 if ((dwc->last_fifo_depth * mdwidth) >= dwc->tx_fifo_size) {
257 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
258 (dwc->last_fifo_depth * mdwidth), dwc->tx_fifo_size,
259 dep->endpoint.name, fifo_size);
Mayank Rana1fbcd4d2018-03-12 14:34:21 -0700260 if (dwc3_is_usb31(dwc))
261 fifo_size = DWC31_GTXFIFOSIZ_TXFDEF(fifo_size);
262 else
263 fifo_size = DWC3_GTXFIFOSIZ_TXFDEF(fifo_size);
264 dwc->last_fifo_depth -= fifo_size;
Mayank Rana4c99f662017-04-25 13:48:46 -0700265 dep->fifo_depth = 0;
266 WARN_ON(1);
267 return -ENOMEM;
268 }
269
Vamsi Krishna Samavedam4ee3d5a2018-05-08 23:39:51 -0700270 if ((dwc->revision == DWC3_USB31_REVISION_170A) &&
271 (dwc->versiontype == DWC3_USB31_VER_TYPE_EA06) &&
272 usb_endpoint_xfer_isoc(dep->endpoint.desc))
273 fifo_size |= DWC31_GTXFIFOSIZ_TXFRAMNUM;
274
Mayank Rana4c99f662017-04-25 13:48:46 -0700275 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->endpoint.ep_num),
276 fifo_size);
277 return 0;
278}
279
Wei Yongjun69102512018-03-29 02:20:10 +0000280static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300281 struct dwc3_request *req, int status)
282{
283 struct dwc3 *dwc = dep->dwc;
284
285 req->started = false;
286 list_del(&req->list);
287 req->remaining = 0;
Jack Phamf15271c2018-12-20 00:57:51 -0800288 req->unaligned = false;
289 req->zero = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300290
291 if (req->request.status == -EINPROGRESS)
292 req->request.status = status;
293
294 if (req->trb)
295 usb_gadget_unmap_request_by_dev(dwc->sysdev,
296 &req->request, req->direction);
297
298 req->trb = NULL;
299 trace_dwc3_gadget_giveback(req);
Felipe Balbic91815b2018-03-26 13:14:47 +0300300}
301
Felipe Balbibfad65e2017-04-19 14:59:27 +0300302/**
303 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
304 * @dep: The endpoint to whom the request belongs to
305 * @req: The request we're giving back
306 * @status: completion code for the request
307 *
308 * Must be called with controller's lock held and interrupts disabled. This
309 * function will unmap @req and call its ->complete() callback to notify upper
310 * layers that it has completed.
311 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300312void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
313 int status)
314{
315 struct dwc3 *dwc = dep->dwc;
316
Felipe Balbic91815b2018-03-26 13:14:47 +0300317 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300318
319 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200320 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300321 spin_lock(&dwc->lock);
322}
323
Felipe Balbibfad65e2017-04-19 14:59:27 +0300324/**
325 * dwc3_send_gadget_generic_command - issue a generic command for the controller
326 * @dwc: pointer to the controller context
327 * @cmd: the command to be issued
328 * @param: command parameter
329 *
330 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
331 * and wait for its completion.
332 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500333int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300334{
335 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300336 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300337 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300338 u32 reg;
339
340 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
341 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
342
343 do {
344 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
345 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300346 status = DWC3_DGCMD_STATUS(reg);
347 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300348 ret = -EINVAL;
349 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300350 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100351 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300352
353 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300354 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300355 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300356 }
357
Felipe Balbi71f7e702016-05-23 14:16:19 +0300358 trace_dwc3_gadget_generic_cmd(cmd, param, status);
359
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300360 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300361}
362
Felipe Balbic36d8e92016-04-04 12:46:33 +0300363static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
364
Felipe Balbibfad65e2017-04-19 14:59:27 +0300365/**
366 * dwc3_send_gadget_ep_cmd - issue an endpoint command
367 * @dep: the endpoint to which the command is going to be issued
368 * @cmd: the command to be issued
369 * @params: parameters to the command
370 *
371 * Caller should handle locking. This function will issue @cmd with given
372 * @params to @dep and wait for its completion.
373 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300374int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
375 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300376{
Felipe Balbi8897a762016-09-22 10:56:08 +0300377 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300378 struct dwc3 *dwc = dep->dwc;
Jack Pham62523502018-01-15 16:37:05 -0800379 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380 u32 reg;
381
Felipe Balbi0933df12016-05-23 14:02:33 +0300382 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300383 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300384 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300386 /*
387 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
388 * we're issuing an endpoint command, we must check if
389 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
390 *
391 * We will also set SUSPHY bit to what it was before returning as stated
392 * by the same section on Synopsys databook.
393 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300394 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
395 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
396 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
397 susphy = true;
398 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
399 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
400 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300401 }
402
Felipe Balbi59999142016-09-22 12:25:28 +0300403 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300404 int needs_wakeup;
405
406 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
407 dwc->link_state == DWC3_LINK_STATE_U2 ||
408 dwc->link_state == DWC3_LINK_STATE_U3);
409
410 if (unlikely(needs_wakeup)) {
411 ret = __dwc3_gadget_wakeup(dwc);
412 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
413 ret);
414 }
415 }
416
Felipe Balbi2eb88012016-04-12 16:53:39 +0300417 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
418 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
419 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300420
Felipe Balbi8897a762016-09-22 10:56:08 +0300421 /*
422 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
423 * not relying on XferNotReady, we can make use of a special "No
424 * Response Update Transfer" command where we should clear both CmdAct
425 * and CmdIOC bits.
426 *
427 * With this, we don't need to wait for command completion and can
428 * straight away issue further commands to the endpoint.
429 *
430 * NOTICE: We're making an assumption that control endpoints will never
431 * make use of Update Transfer command. This is a safe assumption
432 * because we can never have more than one request at a time with
433 * Control Endpoints. If anybody changes that assumption, this chunk
434 * needs to be updated accordingly.
435 */
436 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
437 !usb_endpoint_xfer_isoc(desc))
438 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
439 else
440 cmd |= DWC3_DEPCMD_CMDACT;
441
442 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300443 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300444 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300445 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300446 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000447
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000448 switch (cmd_status) {
449 case 0:
450 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300451 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000452 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000453 ret = -EINVAL;
454 break;
455 case DEPEVT_TRANSFER_BUS_EXPIRY:
456 /*
457 * SW issues START TRANSFER command to
458 * isochronous ep with future frame interval. If
459 * future interval time has already passed when
460 * core receives the command, it will respond
461 * with an error status of 'Bus Expiry'.
462 *
463 * Instead of always returning -EINVAL, let's
464 * give a hint to the gadget driver that this is
465 * the case by returning -EAGAIN.
466 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000467 ret = -EAGAIN;
468 break;
469 default:
470 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
471 }
472
Felipe Balbic0ca3242016-04-04 09:11:51 +0300473 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300475 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300476
Felipe Balbif6bb2252016-05-23 13:53:34 +0300477 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300478 ret = -ETIMEDOUT;
Jack Pham62523502018-01-15 16:37:05 -0800479 dev_err(dwc->dev, "%s command timeout for %s\n",
480 dwc3_gadget_ep_cmd_string(cmd), dep->name);
481 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
482 dwc->ep_cmd_timeout_cnt++;
483 dwc3_notify_event(dwc,
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -0700484 DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Jack Pham62523502018-01-15 16:37:05 -0800485 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300486 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300487 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300488
Felipe Balbi0933df12016-05-23 14:02:33 +0300489 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
490
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300491 if (ret == 0) {
492 switch (DWC3_DEPCMD_CMD(cmd)) {
493 case DWC3_DEPCMD_STARTTRANSFER:
494 dep->flags |= DWC3_EP_TRANSFER_STARTED;
Felipe Balbid7ca7e12018-04-11 12:58:46 +0300495 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300496 break;
497 case DWC3_DEPCMD_ENDTRANSFER:
498 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
499 break;
500 default:
501 /* nothing */
502 break;
503 }
504 }
505
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300506 if (unlikely(susphy)) {
507 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
508 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
509 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
510 }
511
Felipe Balbic0ca3242016-04-04 09:11:51 +0300512 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300513}
514
John Youn50c763f2016-05-31 17:49:56 -0700515static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
516{
517 struct dwc3 *dwc = dep->dwc;
518 struct dwc3_gadget_ep_cmd_params params;
519 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
520
521 /*
522 * As of core revision 2.60a the recommended programming model
523 * is to set the ClearPendIN bit when issuing a Clear Stall EP
524 * command for IN endpoints. This is to prevent an issue where
525 * some (non-compliant) hosts may not send ACK TPs for pending
526 * IN transfers due to a mishandled error condition. Synopsys
527 * STAR 9000614252.
528 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800529 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
530 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700531 cmd |= DWC3_DEPCMD_CLEARPENDIN;
532
533 memset(&params, 0, sizeof(params));
534
Felipe Balbi2cd47182016-04-12 16:42:43 +0300535 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700536}
537
Felipe Balbi72246da2011-08-19 18:10:58 +0300538static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
539{
540 struct dwc3 *dwc = dep->dwc;
Jack Pham62523502018-01-15 16:37:05 -0800541 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
543 if (dep->trb_pool)
544 return 0;
545
Jack Pham62523502018-01-15 16:37:05 -0800546 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
547 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300548 &dep->trb_pool_dma, GFP_KERNEL);
549 if (!dep->trb_pool) {
550 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
551 dep->name);
552 return -ENOMEM;
553 }
Jack Pham62523502018-01-15 16:37:05 -0800554 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
556 return 0;
557}
558
559static void dwc3_free_trb_pool(struct dwc3_ep *dep)
560{
561 struct dwc3 *dwc = dep->dwc;
562
Jack Pham62523502018-01-15 16:37:05 -0800563 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
564 if (dep->endpoint.ep_type == EP_TYPE_GSI)
565 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
Jack Pham62523502018-01-15 16:37:05 -0800567 /*
568 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
569 * with HWO bit set from previous composition when update transfer cmd
570 * is issued.
571 */
572 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
573 memset(&dep->trb_pool[0], 0,
574 sizeof(struct dwc3_trb) * dep->num_trbs);
575 dbg_event(dep->number, "Clr_TRB", 0);
576 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
577
578 dma_free_coherent(dwc->sysdev,
579 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
580 dep->trb_pool, dep->trb_pool_dma);
581 dep->trb_pool = NULL;
582 dep->trb_pool_dma = 0;
583 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300584}
585
Felipe Balbi20d1d432018-04-09 12:49:02 +0300586static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
587{
588 struct dwc3_gadget_ep_cmd_params params;
589
590 memset(&params, 0x00, sizeof(params));
591
592 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
593
594 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
595 &params);
596}
John Younc4509602016-02-16 20:10:53 -0800597
598/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300599 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800600 * @dep: endpoint that is being enabled
601 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300602 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
603 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800604 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300605 * The assignment of transfer resources cannot perfectly follow the data book
606 * due to the fact that the controller driver does not have all knowledge of the
607 * configuration in advance. It is given this information piecemeal by the
608 * composite gadget framework after every SET_CONFIGURATION and
609 * SET_INTERFACE. Trying to follow the databook programming model in this
610 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800611 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300612 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
613 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
614 * incorrect in the scenario of multiple interfaces.
615 *
616 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800617 * endpoint on alt setting (8.1.6).
618 *
619 * The following simplified method is used instead:
620 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300621 * All hardware endpoints can be assigned a transfer resource and this setting
622 * will stay persistent until either a core reset or hibernation. So whenever we
623 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
624 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800625 * guaranteed that there are as many transfer resources as endpoints.
626 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300627 * This function is called for each endpoint when it is being enabled but is
628 * triggered only when called for EP0-out, which always happens first, and which
629 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800630 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300631static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300632{
633 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300634 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800636 int i;
637 int ret;
638
639 if (dep->number)
640 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300641
642 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800643 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300644 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645
Felipe Balbi2cd47182016-04-12 16:42:43 +0300646 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800647 if (ret)
648 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300649
John Younc4509602016-02-16 20:10:53 -0800650 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
651 struct dwc3_ep *dep = dwc->eps[i];
652
653 if (!dep)
654 continue;
655
Felipe Balbib07c2db2018-04-09 12:46:47 +0300656 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800657 if (ret)
658 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300659 }
660
661 return 0;
662}
663
Felipe Balbib07c2db2018-04-09 12:46:47 +0300664static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300665{
John Youn39ebb052016-11-09 16:36:28 -0800666 const struct usb_ss_ep_comp_descriptor *comp_desc;
667 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300669 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300670
John Youn39ebb052016-11-09 16:36:28 -0800671 comp_desc = dep->endpoint.comp_desc;
672 desc = dep->endpoint.desc;
673
Felipe Balbi72246da2011-08-19 18:10:58 +0300674 memset(&params, 0x00, sizeof(params));
675
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300676 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900677 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
678
679 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800680 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300681 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300682 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900683 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300684
Felipe Balbia2d23f02018-04-09 12:40:48 +0300685 params.param0 |= action;
686 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600687 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600688
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300689 if (usb_endpoint_xfer_control(desc))
690 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300691
692 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
693 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300694
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200695 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300696 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
697 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300698 dep->stream_capable = true;
699 }
700
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500701 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300702 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300703
704 /*
705 * We are doing 1:1 mapping for endpoints, meaning
706 * Physical Endpoints 2 maps to Logical Endpoint 2 and
707 * so on. We consider the direction bit as part of the physical
708 * endpoint number. So USB endpoint 0x81 is 0x03.
709 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300710 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300711
712 /*
713 * We must use the lower 16 TX FIFOs even though
714 * HW might have more
715 */
716 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300717 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300718
719 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300720 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300721 dep->interval = 1 << (desc->bInterval - 1);
722 }
723
Felipe Balbi2cd47182016-04-12 16:42:43 +0300724 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300725}
726
Felipe Balbi72246da2011-08-19 18:10:58 +0300727/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300728 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300730 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300731 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300732 * Caller should take care of locking. Execute all necessary commands to
733 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300734 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300735static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300736{
John Youn39ebb052016-11-09 16:36:28 -0800737 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800739
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300741 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300742
743 if (!(dep->flags & DWC3_EP_ENABLED)) {
Mayank Rana4c99f662017-04-25 13:48:46 -0700744 ret = dwc3_gadget_resize_tx_fifos(dwc, dep);
745 if (ret)
746 return ret;
747
Felipe Balbib07c2db2018-04-09 12:46:47 +0300748 ret = dwc3_gadget_start_config(dep);
Jack Pham62523502018-01-15 16:37:05 -0800749 if (ret) {
750 dev_err(dwc->dev, "start_config() failed for %s\n",
751 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 return ret;
Jack Pham62523502018-01-15 16:37:05 -0800753 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 }
755
Felipe Balbib07c2db2018-04-09 12:46:47 +0300756 ret = dwc3_gadget_set_ep_config(dep, action);
Jack Pham62523502018-01-15 16:37:05 -0800757 if (ret) {
758 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300759 return ret;
Jack Pham62523502018-01-15 16:37:05 -0800760 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300761
762 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200763 struct dwc3_trb *trb_st_hw;
764 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300765
Felipe Balbi72246da2011-08-19 18:10:58 +0300766 dep->type = usb_endpoint_type(desc);
767 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800768 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300769
770 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
771 reg |= DWC3_DALEPENA_EP(dep->number);
772 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
773
Baolin Wang76a638f2016-10-31 19:38:36 +0800774 init_waitqueue_head(&dep->wait_end_transfer);
775
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300776 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200777 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300778
John Youn0d257442016-05-19 17:26:08 -0700779 /* Initialize the TRB ring */
780 dep->trb_dequeue = 0;
781 dep->trb_enqueue = 0;
782 memset(dep->trb_pool, 0,
783 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
784
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300785 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300786 trb_st_hw = &dep->trb_pool[0];
787
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200789 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
790 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
791 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
792 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300793 }
794
Felipe Balbia97ea992016-09-29 16:28:56 +0300795 /*
796 * Issue StartTransfer here with no-op TRB so we can always rely on No
797 * Response Update Transfer command.
798 */
Mayank Rana35b0dfa2018-03-13 12:01:58 -0700799 if ((usb_endpoint_xfer_bulk(desc) && !dep->endpoint.endless) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300800 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300801 struct dwc3_gadget_ep_cmd_params params;
802 struct dwc3_trb *trb;
803 dma_addr_t trb_dma;
804 u32 cmd;
805
806 memset(&params, 0, sizeof(params));
807 trb = &dep->trb_pool[0];
808 trb_dma = dwc3_trb_dma_offset(dep, trb);
809
810 params.param0 = upper_32_bits(trb_dma);
811 params.param1 = lower_32_bits(trb_dma);
812
813 cmd = DWC3_DEPCMD_STARTTRANSFER;
814
815 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
816 if (ret < 0)
817 return ret;
Felipe Balbia97ea992016-09-29 16:28:56 +0300818 }
819
Felipe Balbi2870e502016-11-03 13:53:29 +0200820out:
821 trace_dwc3_gadget_ep_enable(dep);
822
Felipe Balbi72246da2011-08-19 18:10:58 +0300823 return 0;
824}
825
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200826static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300827{
828 struct dwc3_request *req;
829
Mayank Rana21f76b32018-09-21 16:49:55 -0700830 dbg_log_string("START for %s(%d)", dep->name, dep->number);
Jack Pham62523502018-01-15 16:37:05 -0800831 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300832
Felipe Balbi0e146022016-06-21 10:32:02 +0300833 /* - giveback all requests to gadget driver */
834 while (!list_empty(&dep->started_list)) {
835 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200836
Felipe Balbi0e146022016-06-21 10:32:02 +0300837 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200838 }
839
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200840 while (!list_empty(&dep->pending_list)) {
841 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200843 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 }
Mayank Rana21f76b32018-09-21 16:49:55 -0700845 dbg_log_string("DONE for %s(%d)", dep->name, dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300846}
847
Jack Pham62523502018-01-15 16:37:05 -0800848static void dwc3_stop_active_transfers(struct dwc3 *dwc)
849{
850 u32 epnum;
851
Mayank Rana21f76b32018-09-21 16:49:55 -0700852 dbg_log_string("START");
Jack Pham62523502018-01-15 16:37:05 -0800853 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
854 struct dwc3_ep *dep;
855
856 dep = dwc->eps[epnum];
857 if (!dep)
858 continue;
859
860 if (!(dep->flags & DWC3_EP_ENABLED))
861 continue;
862
Vijayavardhan Vennapusaaae212f2018-07-17 17:33:16 +0530863 if (dep->endpoint.ep_type == EP_TYPE_GSI && dep->direction)
864 dwc3_notify_event(dwc,
865 DWC3_CONTROLLER_NOTIFY_CLEAR_DB, 0);
866
Jack Pham62523502018-01-15 16:37:05 -0800867 dwc3_remove_requests(dwc, dep);
868 }
Mayank Rana21f76b32018-09-21 16:49:55 -0700869 dbg_log_string("DONE");
Jack Pham62523502018-01-15 16:37:05 -0800870}
871
Hemant Kumar7b7e0652019-04-09 18:00:55 -0700872static void dwc3_stop_active_transfers_to_halt(struct dwc3 *dwc)
873{
874 u32 epnum;
875 struct dwc3_request *req;
876 struct dwc3_ep *dep;
877
878 dbg_log_string("START");
879 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
880 dep = dwc->eps[epnum];
881 if (!dep)
882 continue;
883
884 if (!(dep->flags & DWC3_EP_ENABLED))
885 continue;
886
887 dwc3_stop_active_transfer_noioc(dwc, dep->number, true);
888
889 /* - giveback all requests to gadget driver */
890 while (!list_empty(&dep->started_list)) {
891 req = next_request(&dep->started_list);
892 if (req)
893 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
894 }
895
896 while (!list_empty(&dep->pending_list)) {
897 req = next_request(&dep->pending_list);
898 if (req)
899 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
900 }
901 }
902
903 dwc3_notify_event(dwc, DWC3_GSI_EVT_BUF_CLEAR, 0);
904
905 dbg_log_string("DONE");
906}
907
Felipe Balbi72246da2011-08-19 18:10:58 +0300908/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300909 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 * @dep: the endpoint to disable
911 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300912 * This function undoes what __dwc3_gadget_ep_enable did and also removes
913 * requests which are currently being processed by the hardware and those which
914 * are not yet scheduled.
915 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200916 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300917 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300918static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
919{
920 struct dwc3 *dwc = dep->dwc;
921 u32 reg;
922
Felipe Balbi2870e502016-11-03 13:53:29 +0200923 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500924
Jack Pham62523502018-01-15 16:37:05 -0800925 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
926 dwc3_remove_requests(dwc, dep);
927 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
928 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300929
Felipe Balbi687ef982014-04-16 10:30:33 -0500930 /* make sure HW endpoint isn't stalled */
931 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500932 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500933
Felipe Balbi72246da2011-08-19 18:10:58 +0300934 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
935 reg &= ~DWC3_DALEPENA_EP(dep->number);
936 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
937
Felipe Balbi879631a2011-09-30 10:58:47 +0300938 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800940 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300941
John Youn39ebb052016-11-09 16:36:28 -0800942 /* Clear out the ep descriptors for non-ep0 */
943 if (dep->number > 1) {
944 dep->endpoint.comp_desc = NULL;
945 dep->endpoint.desc = NULL;
946 }
947
Felipe Balbi72246da2011-08-19 18:10:58 +0300948 return 0;
949}
950
951/* -------------------------------------------------------------------------- */
952
953static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
954 const struct usb_endpoint_descriptor *desc)
955{
956 return -EINVAL;
957}
958
959static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
960{
961 return -EINVAL;
962}
963
964/* -------------------------------------------------------------------------- */
965
966static int dwc3_gadget_ep_enable(struct usb_ep *ep,
967 const struct usb_endpoint_descriptor *desc)
968{
969 struct dwc3_ep *dep;
970 struct dwc3 *dwc;
971 unsigned long flags;
972 int ret;
973
974 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
975 pr_debug("dwc3: invalid parameters\n");
976 return -EINVAL;
977 }
978
979 if (!desc->wMaxPacketSize) {
980 pr_debug("dwc3: missing wMaxPacketSize\n");
981 return -EINVAL;
982 }
983
984 dep = to_dwc3_ep(ep);
985 dwc = dep->dwc;
986
Felipe Balbi95ca9612015-12-10 13:08:20 -0600987 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
988 "%s is already enabled\n",
989 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300990 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300991
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300993 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Jack Pham62523502018-01-15 16:37:05 -0800994 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 spin_unlock_irqrestore(&dwc->lock, flags);
996
997 return ret;
998}
999
1000static int dwc3_gadget_ep_disable(struct usb_ep *ep)
1001{
1002 struct dwc3_ep *dep;
1003 struct dwc3 *dwc;
1004 unsigned long flags;
1005 int ret;
1006
1007 if (!ep) {
1008 pr_debug("dwc3: invalid parameters\n");
1009 return -EINVAL;
1010 }
1011
1012 dep = to_dwc3_ep(ep);
1013 dwc = dep->dwc;
1014
Felipe Balbi95ca9612015-12-10 13:08:20 -06001015 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
1016 "%s is already disabled\n",
1017 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +03001018 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001019
Felipe Balbi72246da2011-08-19 18:10:58 +03001020 spin_lock_irqsave(&dwc->lock, flags);
1021 ret = __dwc3_gadget_ep_disable(dep);
Jack Pham62523502018-01-15 16:37:05 -08001022 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001023 spin_unlock_irqrestore(&dwc->lock, flags);
1024
1025 return ret;
1026}
1027
1028static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001029 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +03001030{
1031 struct dwc3_request *req;
1032 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001033
1034 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +09001035 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +03001036 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001037
Felipe Balbi31a2f5a2018-05-07 15:19:31 +03001038 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001039 req->epnum = dep->number;
1040 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001041
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001042 trace_dwc3_alloc_request(req);
1043
Felipe Balbi72246da2011-08-19 18:10:58 +03001044 return &req->request;
1045}
1046
1047static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1048 struct usb_request *request)
1049{
1050 struct dwc3_request *req = to_dwc3_request(request);
1051
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001052 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001053 kfree(req);
1054}
1055
Felipe Balbi42626912018-04-09 13:01:43 +03001056/**
1057 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1058 * @dep: The endpoint with the TRB ring
1059 * @index: The index of the current TRB in the ring
1060 *
1061 * Returns the TRB prior to the one pointed to by the index. If the
1062 * index is 0, we will wrap backwards, skip the link TRB, and return
1063 * the one just before that.
1064 */
1065static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1066{
1067 u8 tmp = index;
1068
Pratham Pratapd048da82017-12-05 14:38:29 +05301069 if (!dep->trb_pool)
1070 return NULL;
1071
Felipe Balbi42626912018-04-09 13:01:43 +03001072 if (!tmp)
1073 tmp = DWC3_TRB_NUM - 1;
1074
1075 return &dep->trb_pool[tmp - 1];
1076}
1077
1078static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1079{
1080 struct dwc3_trb *tmp;
1081 u8 trbs_left;
1082
1083 /*
1084 * If enqueue & dequeue are equal than it is either full or empty.
1085 *
1086 * One way to know for sure is if the TRB right before us has HWO bit
1087 * set or not. If it has, then we're definitely full and can't fit any
1088 * more transfers in our ring.
1089 */
1090 if (dep->trb_enqueue == dep->trb_dequeue) {
1091 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Pratham Pratapcca3d0b2018-03-15 10:31:20 +05301092 if (!tmp || tmp->ctrl & DWC3_TRB_CTRL_HWO)
Felipe Balbi42626912018-04-09 13:01:43 +03001093 return 0;
1094
1095 return DWC3_TRB_NUM - 1;
1096 }
1097
1098 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1099 trbs_left &= (DWC3_TRB_NUM - 1);
1100
1101 if (dep->trb_dequeue < dep->trb_enqueue)
1102 trbs_left--;
1103
1104 return trbs_left;
1105}
Felipe Balbi2c78c022016-08-12 13:13:10 +03001106
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001107static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
1108 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
1109 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +02001110{
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001111 struct dwc3 *dwc = dep->dwc;
1112 struct usb_gadget *gadget = &dwc->gadget;
1113 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +02001114
Felipe Balbif6bafc62012-02-06 11:04:53 +02001115 trb->size = DWC3_TRB_SIZE_LENGTH(length);
1116 trb->bpl = lower_32_bits(dma);
1117 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +02001118
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001119 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +02001120 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001121 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +02001122 break;
1123
1124 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001125 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301126 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001127
Manu Gautam40d829f2017-07-19 17:07:10 +05301128 /*
1129 * USB Specification 2.0 Section 5.9.2 states that: "If
1130 * there is only a single transaction in the microframe,
1131 * only a DATA0 data packet PID is used. If there are
1132 * two transactions per microframe, DATA1 is used for
1133 * the first transaction data packet and DATA0 is used
1134 * for the second transaction data packet. If there are
1135 * three transactions per microframe, DATA2 is used for
1136 * the first transaction data packet, DATA1 is used for
1137 * the second, and DATA0 is used for the third."
1138 *
1139 * IOW, we should satisfy the following cases:
1140 *
1141 * 1) length <= maxpacket
1142 * - DATA0
1143 *
1144 * 2) maxpacket < length <= (2 * maxpacket)
1145 * - DATA1, DATA0
1146 *
1147 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1148 * - DATA2, DATA1, DATA0
1149 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001150 if (speed == USB_SPEED_HIGH) {
1151 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301152 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301153 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1154
1155 if (length <= (2 * maxp))
1156 mult--;
1157
1158 if (length <= maxp)
1159 mult--;
1160
1161 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001162 }
1163 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301164 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001165 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001166
1167 /* always enable Interrupt on Missed ISOC */
1168 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001169 break;
1170
1171 case USB_ENDPOINT_XFER_BULK:
1172 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001173 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001174 break;
1175 default:
1176 /*
1177 * This is only possible with faulty memory because we
1178 * checked it already :)
1179 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001180 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1181 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001182 }
1183
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301184 /*
1185 * Enable Continue on Short Packet
1186 * when endpoint is not a stream capable
1187 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001188 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekarb59d70c2018-12-10 16:08:13 +05301189 if (!dep->stream_capable)
1190 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001191
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001192 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001193 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1194 }
1195
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001196 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301197 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001198 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001199
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301200 if (chain)
1201 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1202
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001203 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001204 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001205
1206 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001207
Anurag Kumar Vulisha93a6d342018-12-01 16:43:29 +05301208 dwc3_ep_inc_enq(dep);
1209
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001210 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001211}
1212
John Youn361572b2016-05-19 17:26:17 -07001213/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001214 * dwc3_prepare_one_trb - setup one TRB from one request
1215 * @dep: endpoint for which this request is prepared
1216 * @req: dwc3_request pointer
1217 * @chain: should this TRB be chained to the next?
1218 * @node: only for isochronous endpoints. First TRB needs different type.
1219 */
1220static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1221 struct dwc3_request *req, unsigned chain, unsigned node)
1222{
1223 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301224 unsigned int length;
1225 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001226 unsigned stream_id = req->request.stream_id;
1227 unsigned short_not_ok = req->request.short_not_ok;
1228 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301229
1230 if (req->request.num_sgs > 0) {
1231 length = sg_dma_len(req->start_sg);
1232 dma = sg_dma_address(req->start_sg);
1233 } else {
1234 length = req->request.length;
1235 dma = req->request.dma;
1236 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001237
1238 trb = &dep->trb_pool[dep->trb_enqueue];
1239
1240 if (!req->trb) {
1241 dwc3_gadget_move_started_request(req);
1242 req->trb = trb;
1243 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001244 }
1245
1246 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1247 stream_id, short_not_ok, no_interrupt);
1248}
1249
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001250static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001251 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001252{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301253 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001254 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001255 int i;
1256
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301257 unsigned int remaining = req->request.num_mapped_sgs
1258 - req->num_queued_sgs;
1259
1260 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001261 unsigned int length = req->request.length;
1262 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1263 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001264 unsigned chain = true;
1265
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001266 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001267 chain = false;
1268
Felipe Balbic6267a52017-01-05 14:58:46 +02001269 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1270 struct dwc3 *dwc = dep->dwc;
1271 struct dwc3_trb *trb;
1272
1273 req->unaligned = true;
1274
1275 /* prepare normal TRB */
1276 dwc3_prepare_one_trb(dep, req, true, i);
1277
1278 /* Now prepare one extra TRB to align transfer size */
1279 trb = &dep->trb_pool[dep->trb_enqueue];
1280 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001281 maxp - rem, false, 1,
Felipe Balbic6267a52017-01-05 14:58:46 +02001282 req->request.stream_id,
1283 req->request.short_not_ok,
1284 req->request.no_interrupt);
1285 } else {
1286 dwc3_prepare_one_trb(dep, req, chain, i);
1287 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001288
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301289 /*
1290 * There can be a situation where all sgs in sglist are not
1291 * queued because of insufficient trb number. To handle this
1292 * case, update start_sg to next sg to be queued, so that
1293 * we have free trbs we can continue queuing from where we
1294 * previously stopped
1295 */
1296 if (chain)
1297 req->start_sg = sg_next(s);
1298
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301299 req->num_queued_sgs++;
1300
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001301 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001302 break;
1303 }
1304}
1305
1306static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001307 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001308{
Felipe Balbic6267a52017-01-05 14:58:46 +02001309 unsigned int length = req->request.length;
1310 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1311 unsigned int rem = length % maxp;
1312
Tejas Joglekar19bc5352019-01-22 13:26:51 +05301313 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001314 struct dwc3 *dwc = dep->dwc;
1315 struct dwc3_trb *trb;
1316
1317 req->unaligned = true;
1318
1319 /* prepare normal TRB */
1320 dwc3_prepare_one_trb(dep, req, true, 0);
1321
1322 /* Now prepare one extra TRB to align transfer size */
1323 trb = &dep->trb_pool[dep->trb_enqueue];
1324 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001325 false, 1, req->request.stream_id,
Felipe Balbic6267a52017-01-05 14:58:46 +02001326 req->request.short_not_ok,
1327 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001328 } else if (req->request.zero && req->request.length &&
Thinh Nguyen4ea438d2018-07-27 18:52:41 -07001329 (IS_ALIGNED(req->request.length, maxp))) {
Felipe Balbid6e5a542017-04-07 16:34:38 +03001330 struct dwc3 *dwc = dep->dwc;
1331 struct dwc3_trb *trb;
1332
1333 req->zero = true;
1334
1335 /* prepare normal TRB */
1336 dwc3_prepare_one_trb(dep, req, true, 0);
1337
1338 /* Now prepare one extra TRB to handle ZLP */
1339 trb = &dep->trb_pool[dep->trb_enqueue];
1340 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
Felipe Balbi47cb2712018-08-01 09:37:34 +03001341 false, 1, req->request.stream_id,
Felipe Balbid6e5a542017-04-07 16:34:38 +03001342 req->request.short_not_ok,
1343 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001344 } else {
1345 dwc3_prepare_one_trb(dep, req, false, 0);
1346 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001347}
1348
Felipe Balbi72246da2011-08-19 18:10:58 +03001349/*
1350 * dwc3_prepare_trbs - setup TRBs from requests
1351 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001353 * The function goes through the requests list and sets up TRBs for the
1354 * transfers. The function returns once there are no more TRBs available or
1355 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001356 */
Felipe Balbic4233572016-05-12 14:08:34 +03001357static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001358{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001359 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001360
1361 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1362
Felipe Balbid86c5a62016-10-25 13:48:52 +03001363 /*
1364 * We can get in a situation where there's a request in the started list
1365 * but there weren't enough TRBs to fully kick it in the first time
1366 * around, so it has been waiting for more TRBs to be freed up.
1367 *
1368 * In that case, we should check if we have a request with pending_sgs
1369 * in the started list and prepare TRBs for that request first,
1370 * otherwise we will prepare TRBs completely out of order and that will
1371 * break things.
1372 */
1373 list_for_each_entry(req, &dep->started_list, list) {
1374 if (req->num_pending_sgs > 0)
1375 dwc3_prepare_one_trb_sg(dep, req);
1376
1377 if (!dwc3_calc_trbs_left(dep))
1378 return;
1379 }
1380
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001381 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001382 struct dwc3 *dwc = dep->dwc;
1383 int ret;
1384
1385 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1386 dep->direction);
1387 if (ret)
1388 return;
1389
1390 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301391 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301392 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001393 req->num_pending_sgs = req->request.num_mapped_sgs;
1394
Felipe Balbi1f512112016-08-12 13:17:27 +03001395 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001396 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001397 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001398 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001399
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001400 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001401 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001402 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001403}
1404
Felipe Balbi7fdca762017-09-05 14:41:34 +03001405static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001406{
1407 struct dwc3_gadget_ep_cmd_params params;
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001408 struct dwc3_request *req, *req1, *n;
Jack Pham62523502018-01-15 16:37:05 -08001409 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001410 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 int ret;
1412 u32 cmd;
1413
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001414 if (!dwc3_calc_trbs_left(dep))
1415 return 0;
1416
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001417 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001418
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001419 dwc3_prepare_trbs(dep);
1420 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001421 if (!req) {
1422 dep->flags |= DWC3_EP_PENDING_REQUEST;
Jack Pham62523502018-01-15 16:37:05 -08001423 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 return 0;
1425 }
1426
1427 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001428
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001429 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301430 params.param0 = upper_32_bits(req->trb_dma);
1431 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001432 cmd = DWC3_DEPCMD_STARTTRANSFER;
1433
1434 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1435 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301436 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001437 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1438 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001440
Felipe Balbi2cd47182016-04-12 16:42:43 +03001441 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001442 if (ret < 0) {
Hemant Kumar8c2f0b92018-11-27 15:17:52 -08001443 if ((ret == -EAGAIN) && starting &&
1444 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1445 dbg_event(dep->number, "CMD_STS", ret);
1446 /* If bit13 in Command complete event is set, software
1447 * must issue ENDTRANDFER command and wait for
1448 * Xfernotready event to queue the requests again.
1449 */
1450 if (!dep->resource_index) {
1451 dwc3_gadget_ep_get_transfer_index(dep);
1452 WARN_ON_ONCE(!dep->resource_index);
1453 }
1454 dwc3_stop_active_transfer(dwc, dep->number, true);
1455
1456 list_for_each_entry_safe_reverse(req1, n,
1457 &dep->started_list, list) {
1458 req1->trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1459 req1->trb = NULL;
1460 dwc3_gadget_move_pending_list_front(req1);
1461 dwc3_ep_inc_deq(dep);
1462 }
1463
1464 return ret;
1465 }
1466
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 /*
1468 * FIXME we need to iterate over the list of requests
1469 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001470 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001472 if (req->trb)
1473 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001474 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 return ret;
1476 }
1477
Felipe Balbi72246da2011-08-19 18:10:58 +03001478 return 0;
1479}
1480
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001481static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1482{
1483 u32 reg;
1484
1485 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1486 return DWC3_DSTS_SOFFN(reg);
1487}
1488
Felipe Balbi5828cad2018-03-27 11:14:31 +03001489static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301490{
Hemant Kumardd36b892018-08-02 17:51:05 -07001491 u16 wraparound_bits;
1492
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001493 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001494 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001495 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301496 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301497 return;
1498 }
1499
Hemant Kumardd36b892018-08-02 17:51:05 -07001500 wraparound_bits = dep->frame_number & DWC3_FRAME_WRAP_AROUND_MASK;
1501 dep->frame_number = dep->frame_number & ~DWC3_FRAME_WRAP_AROUND_MASK;
1502
1503 /* if frame wrapped-around update wrap-around bits to reflect that */
1504 if (__dwc3_gadget_get_frame(dep->dwc) < dep->frame_number)
1505 wraparound_bits += BIT(14);
1506
1507 dep->frame_number = __dwc3_gadget_get_frame(dep->dwc) +
1508 2 * dep->interval;
1509
1510 /* align uf to ep interval */
1511 dep->frame_number = (wraparound_bits | dep->frame_number) &
1512 ~(dep->interval - 1);
1513
Felipe Balbi7fdca762017-09-05 14:41:34 +03001514 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301515}
1516
Felipe Balbi72246da2011-08-19 18:10:58 +03001517static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1518{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001519 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001520
Mayank Ranad32d7842018-06-29 10:20:13 -07001521 if (!dep->endpoint.desc || !dwc->pullups_connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001522 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1523 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001524 return -ESHUTDOWN;
1525 }
1526
Felipe Balbi04fb3652017-05-17 15:57:45 +03001527 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1528 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001529 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001530
Manu Gautam8bb9cbc2013-02-11 15:53:34 +05301531 if (req->request.status == -EINPROGRESS) {
1532 dev_err(dwc->dev, "%s: %pK request already in queue\n",
1533 dep->name, req);
1534 return -EBUSY;
1535 }
1536
Felipe Balbi72246da2011-08-19 18:10:58 +03001537 req->request.actual = 0;
1538 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539
Felipe Balbife84f522015-09-01 09:01:38 -05001540 trace_dwc3_ep_queue(req);
1541
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001542 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001543
Felipe Balbid889c232016-09-29 15:44:29 +03001544 /*
1545 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1546 * wait for a XferNotReady event so we will know what's the current
1547 * (micro-)frame number.
1548 *
1549 * Without this trick, we are very, very likely gonna get Bus Expiry
1550 * errors which will force us issue EndTransfer command.
1551 */
1552 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001553 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1554 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001555 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001556
1557 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1558 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1559 __dwc3_gadget_start_isoc(dep);
1560 return 0;
1561 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001562 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001563 }
1564
Felipe Balbi7fdca762017-09-05 14:41:34 +03001565 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001566}
1567
Jack Pham62523502018-01-15 16:37:05 -08001568static int dwc3_gadget_wakeup(struct usb_gadget *g)
1569{
1570 struct dwc3 *dwc = gadget_to_dwc(g);
1571
1572 schedule_work(&dwc->wakeup_work);
1573 return 0;
1574}
1575
1576static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1577{
1578 if (atomic_read(&dwc->in_lpm) ||
1579 dwc->link_state == DWC3_LINK_STATE_U3)
1580 return true;
1581 return false;
1582}
1583
Felipe Balbi72246da2011-08-19 18:10:58 +03001584static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1585 gfp_t gfp_flags)
1586{
1587 struct dwc3_request *req = to_dwc3_request(request);
1588 struct dwc3_ep *dep = to_dwc3_ep(ep);
1589 struct dwc3 *dwc = dep->dwc;
1590
1591 unsigned long flags;
1592
1593 int ret;
1594
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001595 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001596 ret = __dwc3_gadget_ep_queue(dep, req);
1597 spin_unlock_irqrestore(&dwc->lock, flags);
1598
1599 return ret;
1600}
1601
1602static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1603 struct usb_request *request)
1604{
1605 struct dwc3_request *req = to_dwc3_request(request);
1606 struct dwc3_request *r = NULL;
1607
1608 struct dwc3_ep *dep = to_dwc3_ep(ep);
1609 struct dwc3 *dwc = dep->dwc;
1610
1611 unsigned long flags;
1612 int ret = 0;
1613
Jack Pham62523502018-01-15 16:37:05 -08001614 if (atomic_read(&dwc->in_lpm)) {
1615 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1616 return -EAGAIN;
1617 }
1618
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001619 trace_dwc3_ep_dequeue(req);
1620
Felipe Balbi72246da2011-08-19 18:10:58 +03001621 spin_lock_irqsave(&dwc->lock, flags);
1622
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001623 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001624 if (r == req)
1625 break;
1626 }
1627
1628 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001629 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 if (r == req)
1631 break;
1632 }
1633 if (r == req) {
1634 /* wait until it is processed */
Jack Pham62523502018-01-15 16:37:05 -08001635 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001636
1637 /*
1638 * If request was already started, this means we had to
1639 * stop the transfer. With that we also need to ignore
1640 * all TRBs used by the request, however TRBs can only
1641 * be modified after completion of END_TRANSFER
1642 * command. So what we do here is that we wait for
1643 * END_TRANSFER completion and only after that, we jump
1644 * over TRBs by clearing HWO and incrementing dequeue
1645 * pointer.
1646 *
1647 * Note that we have 2 possible types of transfers here:
1648 *
1649 * i) Linear buffer request
1650 * ii) SG-list based request
1651 *
1652 * SG-list based requests will have r->num_pending_sgs
1653 * set to a valid number (> 0). Linear requests,
1654 * normally use a single TRB.
1655 *
1656 * For each of these two cases, if r->unaligned flag is
1657 * set, one extra TRB has been used to align transfer
1658 * size to wMaxPacketSize.
1659 *
1660 * All of these cases need to be taken into
1661 * consideration so we don't mess up our TRB ring
1662 * pointers.
1663 */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001664 if (!r->trb)
Mayank Rana05645362018-03-23 10:05:33 -07001665 goto out0;
Felipe Balbicf3113d2017-02-17 11:12:44 +02001666
1667 if (r->num_pending_sgs) {
1668 struct dwc3_trb *trb;
1669 int i = 0;
1670
1671 for (i = 0; i < r->num_pending_sgs; i++) {
1672 trb = r->trb + i;
1673 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1674 dwc3_ep_inc_deq(dep);
1675 }
1676
Felipe Balbid6e5a542017-04-07 16:34:38 +03001677 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001678 trb = r->trb + r->num_pending_sgs + 1;
1679 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1680 dwc3_ep_inc_deq(dep);
1681 }
1682 } else {
1683 struct dwc3_trb *trb = r->trb;
1684
1685 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1686 dwc3_ep_inc_deq(dep);
1687
Felipe Balbid6e5a542017-04-07 16:34:38 +03001688 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001689 trb = r->trb + 1;
1690 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1691 dwc3_ep_inc_deq(dep);
1692 }
1693 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301694 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001695 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001696 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001697 request, ep->name);
1698 ret = -EINVAL;
1699 goto out0;
1700 }
1701
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301702out1:
Jack Pham62523502018-01-15 16:37:05 -08001703 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001704 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001705
Felipe Balbi72246da2011-08-19 18:10:58 +03001706 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1707
1708out0:
1709 spin_unlock_irqrestore(&dwc->lock, flags);
1710
1711 return ret;
1712}
1713
Felipe Balbi7a608552014-09-24 14:19:52 -05001714int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001715{
1716 struct dwc3_gadget_ep_cmd_params params;
1717 struct dwc3 *dwc = dep->dwc;
1718 int ret;
1719
Jack Pham62523502018-01-15 16:37:05 -08001720 if (!dep->endpoint.desc) {
1721 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1722 return -EINVAL;
1723 }
1724
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001725 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1726 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1727 return -EINVAL;
1728 }
1729
Felipe Balbi72246da2011-08-19 18:10:58 +03001730 memset(&params, 0x00, sizeof(params));
Jack Pham62523502018-01-15 16:37:05 -08001731 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001732 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001733 struct dwc3_trb *trb;
1734
1735 unsigned transfer_in_flight;
1736 unsigned started;
1737
1738 if (dep->number > 1)
1739 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1740 else
1741 trb = &dwc->ep0_trb[dep->trb_enqueue];
1742
Pratham Pratapd048da82017-12-05 14:38:29 +05301743 if (trb)
1744 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1745 else
1746 transfer_in_flight = false;
1747
Felipe Balbi69450c42016-05-30 13:37:02 +03001748 started = !list_empty(&dep->started_list);
1749
1750 if (!protocol && ((dep->direction && transfer_in_flight) ||
1751 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001752 return -EAGAIN;
1753 }
1754
Felipe Balbi2cd47182016-04-12 16:42:43 +03001755 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1756 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001757 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001758 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001759 dep->name);
1760 else
1761 dep->flags |= DWC3_EP_STALL;
1762 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001763
John Youn50c763f2016-05-31 17:49:56 -07001764 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001766 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001767 dep->name);
1768 else
Alan Sterna535d812013-11-01 12:05:12 -04001769 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001770 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001771
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 return ret;
1773}
1774
1775static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1776{
1777 struct dwc3_ep *dep = to_dwc3_ep(ep);
1778 struct dwc3 *dwc = dep->dwc;
1779
1780 unsigned long flags;
1781
1782 int ret;
1783
Jack Pham62523502018-01-15 16:37:05 -08001784 if (!ep->desc) {
1785 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1786 return -EINVAL;
1787 }
1788
Felipe Balbi72246da2011-08-19 18:10:58 +03001789 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001790 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001791 spin_unlock_irqrestore(&dwc->lock, flags);
1792
1793 return ret;
1794}
1795
1796static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1797{
1798 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001799 struct dwc3 *dwc = dep->dwc;
1800 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001801 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001802
Paul Zimmerman249a4562012-02-24 17:32:16 -08001803 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08001804 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001805 dep->flags |= DWC3_EP_WEDGE;
1806
Pratyush Anand08f0d962012-06-25 22:40:43 +05301807 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001808 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301809 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001810 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001811 spin_unlock_irqrestore(&dwc->lock, flags);
1812
1813 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001814}
1815
1816/* -------------------------------------------------------------------------- */
1817
1818static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1819 .bLength = USB_DT_ENDPOINT_SIZE,
1820 .bDescriptorType = USB_DT_ENDPOINT,
1821 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1822};
1823
1824static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1825 .enable = dwc3_gadget_ep0_enable,
1826 .disable = dwc3_gadget_ep0_disable,
1827 .alloc_request = dwc3_gadget_ep_alloc_request,
1828 .free_request = dwc3_gadget_ep_free_request,
1829 .queue = dwc3_gadget_ep0_queue,
1830 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301831 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 .set_wedge = dwc3_gadget_ep_set_wedge,
1833};
1834
1835static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1836 .enable = dwc3_gadget_ep_enable,
1837 .disable = dwc3_gadget_ep_disable,
1838 .alloc_request = dwc3_gadget_ep_alloc_request,
1839 .free_request = dwc3_gadget_ep_free_request,
1840 .queue = dwc3_gadget_ep_queue,
1841 .dequeue = dwc3_gadget_ep_dequeue,
1842 .set_halt = dwc3_gadget_ep_set_halt,
1843 .set_wedge = dwc3_gadget_ep_set_wedge,
1844};
1845
1846/* -------------------------------------------------------------------------- */
1847
1848static int dwc3_gadget_get_frame(struct usb_gadget *g)
1849{
1850 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001851
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001852 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001853}
1854
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001855static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001856{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001857 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001858
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001859 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001860 u32 reg;
1861
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 u8 link_state;
1863 u8 speed;
1864
Felipe Balbi72246da2011-08-19 18:10:58 +03001865 /*
1866 * According to the Databook Remote wakeup request should
1867 * be issued only when the device is in early suspend state.
1868 *
1869 * We can check that via USB Link State bits in DSTS register.
1870 */
1871 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1872
1873 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001874 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001875 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001876 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001877
1878 link_state = DWC3_DSTS_USBLNKST(reg);
1879
1880 switch (link_state) {
1881 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1882 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1883 break;
1884 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001885 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001886 }
1887
Felipe Balbi8598bde2012-01-02 18:55:57 +02001888 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1889 if (ret < 0) {
1890 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001891 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001892 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001893
Paul Zimmerman802fde92012-04-27 13:10:52 +03001894 /* Recent versions do this automatically */
1895 if (dwc->revision < DWC3_REVISION_194A) {
1896 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001897 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001898 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1899 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1900 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001901
Paul Zimmerman1d046792012-02-15 18:56:56 -08001902 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001903 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001904
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001905 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001906 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1907
1908 /* in HS, means ON */
1909 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1910 break;
1911 }
1912
1913 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1914 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001915 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001916 }
1917
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001918 return 0;
1919}
1920
Jack Pham62523502018-01-15 16:37:05 -08001921#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1922#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1923
1924static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001925{
Jack Pham62523502018-01-15 16:37:05 -08001926 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001927 int ret;
Jack Pham62523502018-01-15 16:37:05 -08001928 static int retry_count;
1929
1930 dwc = container_of(w, struct dwc3, wakeup_work);
1931
1932 ret = pm_runtime_get_sync(dwc->dev);
1933 if (ret) {
1934 /* pm_runtime_get_sync returns -EACCES error between
1935 * late_suspend and early_resume, wait for system resume to
1936 * finish and queue work again
1937 */
1938 dev_dbg(dwc->dev, "PM runtime get sync failed, ret %d\n", ret);
1939 if (ret == -EACCES) {
1940 pm_runtime_put_noidle(dwc->dev);
1941 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1942 retry_count = 0;
1943 dev_err(dwc->dev, "pm_runtime_get_sync timed out\n");
1944 return;
1945 }
1946 msleep(DWC3_PM_RESUME_DELAY);
1947 retry_count++;
1948 schedule_work(&dwc->wakeup_work);
1949 return;
1950 }
1951 }
1952 retry_count = 0;
1953 dbg_event(0xFF, "Gdgwake gsyn",
1954 atomic_read(&dwc->dev->power.usage_count));
1955
1956 ret = dwc3_gadget_wakeup_int(dwc);
1957 if (ret)
1958 dev_err(dwc->dev, "Remote wakeup failed. ret = %d\n", ret);
1959
1960 pm_runtime_put_noidle(dwc->dev);
1961 dbg_event(0xFF, "Gdgwake put",
1962 atomic_read(&dwc->dev->power.usage_count));
1963}
1964
1965static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1966{
1967 bool link_recover_only = false;
1968
1969 u32 reg;
1970 int ret = 0;
1971 u8 link_state;
1972 unsigned long flags;
1973
1974 dev_dbg(dwc->dev, "%s(): Entry\n", __func__);
1975 disable_irq(dwc->irq);
1976 spin_lock_irqsave(&dwc->lock, flags);
1977 /*
1978 * According to the Databook Remote wakeup request should
1979 * be issued only when the device is in early suspend state.
1980 *
1981 * We can check that via USB Link State bits in DSTS register.
1982 */
1983 link_state = dwc3_get_link_state(dwc);
1984
1985 switch (link_state) {
1986 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1987 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1988 break;
1989 case DWC3_LINK_STATE_U1:
1990 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1991 link_recover_only = true;
1992 break;
1993 }
1994 /* Intentional fallthrough */
1995 default:
1996 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1997 link_state);
1998 ret = -EINVAL;
1999 goto out;
2000 }
2001
2002 /* Enable LINK STATUS change event */
2003 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2004 reg |= DWC3_DEVTEN_ULSTCNGEN;
2005 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2006 /*
2007 * memory barrier is required to make sure that required events
2008 * with core is enabled before performing RECOVERY mechnism.
2009 */
2010 mb();
2011
2012 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2013 if (ret < 0) {
2014 dev_err(dwc->dev, "failed to put link in Recovery\n");
2015 /* Disable LINK STATUS change */
2016 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2017 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2018 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2019 /* Required to complete this operation before returning */
2020 mb();
2021 goto out;
2022 }
2023
2024 /* Recent versions do this automatically */
2025 if (dwc->revision < DWC3_REVISION_194A) {
2026 /* write zeroes to Link Change Request */
2027 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2028 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2029 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2030 }
2031
2032 spin_unlock_irqrestore(&dwc->lock, flags);
2033 enable_irq(dwc->irq);
2034
2035 /*
2036 * Have bigger value (16 sec) for timeout since some host PCs driving
2037 * resume for very long time (e.g. 8 sec)
2038 */
2039 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
2040 (dwc->link_state < DWC3_LINK_STATE_U3) ||
2041 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
2042 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002043
2044 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002045 /* Disable link status change event */
2046 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
2047 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
2048 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2049 /*
2050 * Complete this write before we go ahead and perform resume
2051 * as we don't need link status change notificaiton anymore.
2052 */
2053 mb();
2054
2055 if (!ret) {
2056 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
2057 dwc->link_state);
2058 ret = -EINVAL;
2059 spin_unlock_irqrestore(&dwc->lock, flags);
2060 goto out1;
2061 } else {
2062 ret = 0;
2063 /*
2064 * If USB is disconnected OR received RESET from host,
2065 * don't perform resume
2066 */
2067 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
2068 dwc->gadget.state == USB_STATE_DEFAULT)
2069 link_recover_only = true;
2070 }
2071
2072 /*
2073 * According to DWC3 databook, the controller does not
2074 * trigger a wakeup event when remote-wakeup is used.
2075 * Hence, after remote-wakeup sequence is complete, and
2076 * the device is back at U0 state, it is required that
2077 * the resume sequence is initiated by SW.
2078 */
2079 if (!link_recover_only)
2080 dwc3_gadget_wakeup_interrupt(dwc, true);
2081
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 spin_unlock_irqrestore(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002083 dev_dbg(dwc->dev, "%s: Exit\n", __func__);
2084 return ret;
2085
2086out:
2087 spin_unlock_irqrestore(&dwc->lock, flags);
2088 enable_irq(dwc->irq);
2089
2090out1:
2091 return ret;
2092}
2093
2094static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
2095{
2096 int ret = 0;
2097 struct dwc3 *dwc = gadget_to_dwc(g);
2098
2099 if (!g || (g->speed != USB_SPEED_SUPER))
2100 return -ENOTSUPP;
2101
2102 if (dwc3_gadget_is_suspended(dwc)) {
2103 dev_dbg(dwc->dev, "USB bus is suspended, scheduling wakeup\n");
2104 dwc3_gadget_wakeup(&dwc->gadget);
2105 return -EAGAIN;
2106 }
2107
2108 ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_XMIT_DEV,
2109 0x1 | (interface_id << 4));
2110 if (ret)
2111 dev_err(dwc->dev, "Function wakeup HW command failed, ret %d\n",
2112 ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002113
2114 return ret;
2115}
2116
2117static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2118 int is_selfpowered)
2119{
2120 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002121 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002122
Paul Zimmerman249a4562012-02-24 17:32:16 -08002123 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002124 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002125 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002126
2127 return 0;
2128}
2129
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002130static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002131{
Mayank Rana0f67c832018-03-07 14:11:41 -08002132 u32 reg, reg1;
Jack Pham62523502018-01-15 16:37:05 -08002133 u32 timeout = 1500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002134
Jack Pham62523502018-01-15 16:37:05 -08002135 dbg_event(0xFF, "run_stop", is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03002136 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002137 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002138 if (dwc->revision <= DWC3_REVISION_187A) {
2139 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2140 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2141 }
2142
2143 if (dwc->revision >= DWC3_REVISION_194A)
2144 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Jack Pham62523502018-01-15 16:37:05 -08002145
2146 dwc3_event_buffers_setup(dwc);
2147 __dwc3_gadget_start(dwc);
2148
Mayank Rana0f67c832018-03-07 14:11:41 -08002149 reg1 = dwc3_readl(dwc->regs, DWC3_DCFG);
2150 reg1 &= ~(DWC3_DCFG_SPEED_MASK);
2151
2152 if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
2153 reg1 |= DWC3_DCFG_SUPERSPEED_PLUS;
2154 else if (dwc->maximum_speed == USB_SPEED_HIGH)
2155 reg1 |= DWC3_DCFG_HIGHSPEED;
2156 else
2157 reg1 |= DWC3_DCFG_SUPERSPEED;
2158 dwc3_writel(dwc->regs, DWC3_DCFG, reg1);
2159
Paul Zimmerman802fde92012-04-27 13:10:52 +03002160 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002161
2162 if (dwc->has_hibernation)
2163 reg |= DWC3_DCTL_KEEP_CONNECT;
2164
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002165 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002166 } else {
Jack Pham62523502018-01-15 16:37:05 -08002167 dwc3_gadget_disable_irq(dwc);
Mayank Ranad32d7842018-06-29 10:20:13 -07002168 dwc->pullups_connected = false;
Jack Pham62523502018-01-15 16:37:05 -08002169 __dwc3_gadget_ep_disable(dwc->eps[0]);
2170 __dwc3_gadget_ep_disable(dwc->eps[1]);
2171
2172 /*
2173 * According to dwc3 databook, it is must to remove any active
2174 * transfers before trying to stop USB device controller. Hence
2175 * call dwc3_stop_active_transfers() API before stopping USB
2176 * device controller.
2177 */
Hemant Kumar7b7e0652019-04-09 18:00:55 -07002178 dwc3_stop_active_transfers_to_halt(dwc);
Jack Pham62523502018-01-15 16:37:05 -08002179
Felipe Balbi72246da2011-08-19 18:10:58 +03002180 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002181
2182 if (dwc->has_hibernation && !suspend)
2183 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002184 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002185
2186 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2187
2188 do {
2189 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002190 reg &= DWC3_DSTS_DEVCTRLHLT;
2191 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002192
Hemant Kumar7b7e0652019-04-09 18:00:55 -07002193 if (!is_on)
2194 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_CLEAR_DB, 0);
2195
Jack Pham62523502018-01-15 16:37:05 -08002196 if (!timeout) {
2197 dev_err(dwc->dev, "failed to %s controller\n",
2198 is_on ? "start" : "stop");
2199 if (is_on)
2200 dbg_event(0xFF, "STARTTOUT", reg);
2201 else
2202 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03002203 return -ETIMEDOUT;
Jack Pham62523502018-01-15 16:37:05 -08002204 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002205
Pratyush Anand6f17f742012-07-02 10:21:55 +05302206 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002207}
2208
Jack Pham62523502018-01-15 16:37:05 -08002209static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2210{
2211 struct dwc3 *dwc = gadget_to_dwc(g);
2212
2213 dwc->vbus_draw = mA;
2214 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
2215 dbg_event(0xFF, "currentDraw", mA);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002216 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08002217 return 0;
2218}
2219
Felipe Balbi72246da2011-08-19 18:10:58 +03002220static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2221{
2222 struct dwc3 *dwc = gadget_to_dwc(g);
2223 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302224 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002225
2226 is_on = !!is_on;
Jack Pham62523502018-01-15 16:37:05 -08002227 dwc->softconnect = is_on;
2228
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002229 if ((dwc3_is_otg_or_drd(dwc) && !dwc->vbus_active)
Jack Pham62523502018-01-15 16:37:05 -08002230 || !dwc->gadget_driver) {
2231 /*
2232 * Need to wait for vbus_session(on) from otg driver or to
2233 * the udc_start.
2234 */
2235 dbg_event(0xFF, "WaitPullup", 0);
2236 return 0;
2237 }
2238
2239 pm_runtime_get_sync(dwc->dev);
2240 dbg_event(0xFF, "Pullup gsync",
2241 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03002242
Baolin Wangbb014732016-10-14 17:11:33 +08002243 /*
2244 * Per databook, when we want to stop the gadget, if a control transfer
2245 * is still in process, complete it and get the core into setup phase.
2246 */
Jack Phambe653d62018-06-19 18:42:40 -07002247 if (!is_on && (dwc->ep0state != EP0_SETUP_PHASE ||
2248 dwc->ep0_next_event != DWC3_EP0_COMPLETE)) {
Baolin Wangbb014732016-10-14 17:11:33 +08002249 reinit_completion(&dwc->ep0_in_setup);
2250
2251 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2252 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
Manu Gautam1f805462019-02-27 08:50:12 +05302253 if (ret == 0)
Baolin Wangbb014732016-10-14 17:11:33 +08002254 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
Baolin Wangbb014732016-10-14 17:11:33 +08002255 }
2256
Felipe Balbi72246da2011-08-19 18:10:58 +03002257 spin_lock_irqsave(&dwc->lock, flags);
Jack Pham62523502018-01-15 16:37:05 -08002258 /*
2259 * If we are here after bus suspend notify otg state machine to
2260 * increment pm usage count of dwc to prevent pm_runtime_suspend
2261 * during enumeration.
2262 */
2263 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002264 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
2265
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002266 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002267 spin_unlock_irqrestore(&dwc->lock, flags);
2268
Jack Pham62523502018-01-15 16:37:05 -08002269 pm_runtime_mark_last_busy(dwc->dev);
2270 pm_runtime_put_autosuspend(dwc->dev);
2271 dbg_event(0xFF, "Pullup put",
2272 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302273 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002274}
2275
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002276static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2277{
2278 u32 reg;
2279
Jack Pham62523502018-01-15 16:37:05 -08002280 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002281 /* Enable all but Start and End of Frame IRQs */
2282 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2283 DWC3_DEVTEN_EVNTOVERFLOWEN |
2284 DWC3_DEVTEN_CMDCMPLTEN |
2285 DWC3_DEVTEN_ERRTICERREN |
2286 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002287 DWC3_DEVTEN_CONNECTDONEEN |
2288 DWC3_DEVTEN_USBRSTEN |
2289 DWC3_DEVTEN_DISCONNEVTEN);
2290
Jack Pham62523502018-01-15 16:37:05 -08002291 if (dwc->revision < DWC3_REVISION_230A)
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002292 reg |= DWC3_DEVTEN_ULSTCNGEN;
2293
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002294 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2295}
2296
Jack Pham62523502018-01-15 16:37:05 -08002297void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002298{
Jack Pham62523502018-01-15 16:37:05 -08002299 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002300 /* mask all interrupts */
2301 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2302}
2303
Felipe Balbib15a7622011-06-30 16:57:15 +03002304static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002305
Felipe Balbi4e994722016-05-13 14:09:59 +03002306/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002307 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2308 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002309 *
2310 * The following looks like complex but it's actually very simple. In order to
2311 * calculate the number of packets we can burst at once on OUT transfers, we're
2312 * gonna use RxFIFO size.
2313 *
2314 * To calculate RxFIFO size we need two numbers:
2315 * MDWIDTH = size, in bits, of the internal memory bus
2316 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2317 *
2318 * Given these two numbers, the formula is simple:
2319 *
2320 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2321 *
2322 * 24 bytes is for 3x SETUP packets
2323 * 16 bytes is a clock domain crossing tolerance
2324 *
2325 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2326 */
2327static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2328{
2329 u32 ram2_depth;
2330 u32 mdwidth;
2331 u32 nump;
2332 u32 reg;
2333
2334 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2335 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2336
2337 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2338 nump = min_t(u32, nump, 16);
2339
2340 /* update NumP */
2341 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2342 reg &= ~DWC3_DCFG_NUMP_MASK;
2343 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2344 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2345}
2346
Jack Pham62523502018-01-15 16:37:05 -08002347static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2348{
2349 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2350 unsigned long flags;
2351
Hemant Kumard4c4fb2d2018-09-21 17:38:18 -07002352 if (dwc->dr_mode != USB_DR_MODE_OTG && dwc->dr_mode != USB_DR_MODE_DRD)
Jack Pham62523502018-01-15 16:37:05 -08002353 return -EPERM;
2354
2355 is_active = !!is_active;
2356
2357 dbg_event(0xFF, "VbusSess", is_active);
2358 spin_lock_irqsave(&dwc->lock, flags);
2359
2360 /* Mark that the vbus was powered */
2361 dwc->vbus_active = is_active;
2362
2363 /*
2364 * Check if upper level usb_gadget_driver was already registered with
2365 * this udc controller driver (if dwc3_gadget_start was called)
2366 */
2367 if (dwc->gadget_driver && dwc->softconnect) {
2368 if (dwc->vbus_active) {
2369 /*
2370 * Both vbus was activated by otg and pullup was
2371 * signaled by the gadget driver.
2372 */
2373 dwc3_gadget_run_stop(dwc, 1, false);
2374 } else {
2375 dwc3_gadget_run_stop(dwc, 0, false);
2376 }
2377 }
2378
2379 /*
2380 * Clearing run/stop bit might occur before disconnect event is seen.
2381 * Make sure to let gadget driver know in that case.
2382 */
2383 if (!dwc->vbus_active) {
2384 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2385 dwc3_gadget_disconnect_interrupt(dwc);
2386 }
2387
2388 spin_unlock_irqrestore(&dwc->lock, flags);
2389 return 0;
2390}
2391
Felipe Balbid7be2952016-05-04 15:49:37 +03002392static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002393{
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002395 int ret = 0;
2396 u32 reg;
2397
Jack Pham62523502018-01-15 16:37:05 -08002398 dbg_event(0xFF, "__Gadgetstart", 0);
2399
John Youncf40b862016-11-14 12:32:43 -08002400 /*
2401 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2402 * the core supports IMOD, disable it.
2403 */
2404 if (dwc->imod_interval) {
2405 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2406 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2407 } else if (dwc3_has_imod(dwc)) {
2408 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2409 }
2410
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002411 /*
2412 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2413 * field instead of letting dwc3 itself calculate that automatically.
2414 *
2415 * This way, we maximize the chances that we'll be able to get several
2416 * bursts of data without going through any sort of endpoint throttling.
2417 */
2418 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002419 if (dwc3_is_usb31(dwc))
2420 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2421 else
2422 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2423
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002424 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2425
Jack Pham62523502018-01-15 16:37:05 -08002426 /*
2427 * Programs the number of outstanding pipelined transfer requests
2428 * the AXI master pushes to the AXI slave.
2429 */
2430 if (dwc->revision >= DWC3_REVISION_270A) {
2431 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2432 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2433 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2434 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2435 }
2436
Felipe Balbi4e994722016-05-13 14:09:59 +03002437 dwc3_gadget_setup_nump(dwc);
2438
Felipe Balbi72246da2011-08-19 18:10:58 +03002439 /* Start with SuperSpeed Default */
2440 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2441
2442 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002443 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002444 if (ret) {
2445 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002446 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002447 }
2448
2449 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002450 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002451 if (ret) {
2452 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002453 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002454 }
2455
2456 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002457 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao08c937f2018-12-26 19:22:00 +08002458 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 dwc3_ep0_out_start(dwc);
2460
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002461 dwc3_gadget_enable_irq(dwc);
2462
Felipe Balbid7be2952016-05-04 15:49:37 +03002463 return 0;
2464
2465err1:
2466 __dwc3_gadget_ep_disable(dwc->eps[0]);
2467
2468err0:
2469 return ret;
2470}
2471
2472static int dwc3_gadget_start(struct usb_gadget *g,
2473 struct usb_gadget_driver *driver)
2474{
2475 struct dwc3 *dwc = gadget_to_dwc(g);
2476 unsigned long flags;
2477 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002478
Jack Pham62523502018-01-15 16:37:05 -08002479 dbg_event(0xFF, "Gadgetstart", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002480 spin_lock_irqsave(&dwc->lock, flags);
2481 if (dwc->gadget_driver) {
2482 dev_err(dwc->dev, "%s is already bound to %s\n",
2483 dwc->gadget.name,
2484 dwc->gadget_driver->driver.name);
2485 ret = -EBUSY;
Jack Pham62523502018-01-15 16:37:05 -08002486 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002487 }
2488
2489 dwc->gadget_driver = driver;
2490
Jack Pham62523502018-01-15 16:37:05 -08002491 /*
2492 * For DRD, this might get called by gadget driver during bootup
2493 * even though host mode might be active. Don't actually perform
2494 * device-specific initialization until device mode is activated.
2495 * In that case dwc3_gadget_restart() will handle it.
2496 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 spin_unlock_irqrestore(&dwc->lock, flags);
2498
2499 return 0;
2500
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002501err0:
Jack Pham62523502018-01-15 16:37:05 -08002502 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002503 return ret;
2504}
2505
Felipe Balbid7be2952016-05-04 15:49:37 +03002506static void __dwc3_gadget_stop(struct dwc3 *dwc)
2507{
Jack Pham62523502018-01-15 16:37:05 -08002508 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002509 dwc3_gadget_disable_irq(dwc);
2510 __dwc3_gadget_ep_disable(dwc->eps[0]);
2511 __dwc3_gadget_ep_disable(dwc->eps[1]);
2512}
2513
Felipe Balbi22835b82014-10-17 12:05:12 -05002514static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002515{
2516 struct dwc3 *dwc = gadget_to_dwc(g);
2517 unsigned long flags;
2518
2519 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002520 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002521 spin_unlock_irqrestore(&dwc->lock, flags);
2522
Jack Pham62523502018-01-15 16:37:05 -08002523 dbg_event(0xFF, "fwq_started", 0);
2524 flush_workqueue(dwc->dwc_wq);
2525 dbg_event(0xFF, "fwq_completed", 0);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002526
Felipe Balbi72246da2011-08-19 18:10:58 +03002527 return 0;
2528}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002529
Mayank Rana0f67c832018-03-07 14:11:41 -08002530static void __maybe_unused dwc3_gadget_set_speed(struct usb_gadget *g,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002531 enum usb_device_speed speed)
2532{
2533 struct dwc3 *dwc = gadget_to_dwc(g);
2534 unsigned long flags;
2535 u32 reg;
2536
2537 spin_lock_irqsave(&dwc->lock, flags);
2538 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2539 reg &= ~(DWC3_DCFG_SPEED_MASK);
2540
2541 /*
2542 * WORKAROUND: DWC3 revision < 2.20a have an issue
2543 * which would cause metastability state on Run/Stop
2544 * bit if we try to force the IP to USB2-only mode.
2545 *
2546 * Because of that, we cannot configure the IP to any
2547 * speed other than the SuperSpeed
2548 *
2549 * Refers to:
2550 *
2551 * STAR#9000525659: Clock Domain Crossing on DCTL in
2552 * USB 2.0 Mode
2553 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002554 if (dwc->revision < DWC3_REVISION_220A &&
2555 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002556 reg |= DWC3_DCFG_SUPERSPEED;
2557 } else {
2558 switch (speed) {
2559 case USB_SPEED_LOW:
2560 reg |= DWC3_DCFG_LOWSPEED;
2561 break;
2562 case USB_SPEED_FULL:
2563 reg |= DWC3_DCFG_FULLSPEED;
2564 break;
2565 case USB_SPEED_HIGH:
2566 reg |= DWC3_DCFG_HIGHSPEED;
2567 break;
2568 case USB_SPEED_SUPER:
2569 reg |= DWC3_DCFG_SUPERSPEED;
2570 break;
2571 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002572 if (dwc3_is_usb31(dwc))
2573 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2574 else
2575 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002576 break;
2577 default:
2578 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2579
2580 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2581 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2582 else
2583 reg |= DWC3_DCFG_SUPERSPEED;
2584 }
2585 }
2586 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2587
2588 spin_unlock_irqrestore(&dwc->lock, flags);
2589}
2590
Jack Pham62523502018-01-15 16:37:05 -08002591static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2592{
2593 struct dwc3 *dwc = gadget_to_dwc(g);
2594
2595 dbg_event(0xFF, "RestartUSBSession", 0);
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07002596 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION, 0);
Jack Pham62523502018-01-15 16:37:05 -08002597}
2598
Felipe Balbi72246da2011-08-19 18:10:58 +03002599static const struct usb_gadget_ops dwc3_gadget_ops = {
2600 .get_frame = dwc3_gadget_get_frame,
2601 .wakeup = dwc3_gadget_wakeup,
Jack Pham62523502018-01-15 16:37:05 -08002602 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002603 .set_selfpowered = dwc3_gadget_set_selfpowered,
Jack Pham62523502018-01-15 16:37:05 -08002604 .vbus_session = dwc3_gadget_vbus_session,
2605 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002606 .pullup = dwc3_gadget_pullup,
2607 .udc_start = dwc3_gadget_start,
2608 .udc_stop = dwc3_gadget_stop,
Jack Pham62523502018-01-15 16:37:05 -08002609 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002610};
2611
2612/* -------------------------------------------------------------------------- */
2613
Jack Pham62523502018-01-15 16:37:05 -08002614#define NUM_GSI_OUT_EPS 1
2615#define NUM_GSI_IN_EPS 2
2616
2617
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002618static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2619{
2620 struct dwc3 *dwc = dep->dwc;
2621
2622 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2623 dep->endpoint.maxburst = 1;
2624 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2625 if (!dep->direction)
2626 dwc->gadget.ep0 = &dep->endpoint;
2627
2628 dep->endpoint.caps.type_control = true;
2629
2630 return 0;
2631}
2632
Mayank Rana0f67c832018-03-07 14:11:41 -08002633static int dwc3_gadget_init_in_out_endpoint(struct dwc3_ep *dep)
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002634{
2635 struct dwc3 *dwc = dep->dwc;
2636
2637 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2638 dep->endpoint.max_streams = 15;
2639 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2640 list_add_tail(&dep->endpoint.ep_list,
2641 &dwc->gadget.ep_list);
2642 dep->endpoint.caps.type_iso = true;
2643 dep->endpoint.caps.type_bulk = true;
2644 dep->endpoint.caps.type_int = true;
2645
2646 return dwc3_alloc_trb_pool(dep);
2647}
2648
2649static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002650{
2651 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002652 bool direction = epnum & 1;
2653 int ret;
2654 u8 num = epnum >> 1;
2655
2656 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2657 if (!dep)
2658 return -ENOMEM;
2659
2660 dep->dwc = dwc;
2661 dep->number = epnum;
2662 dep->direction = direction;
2663 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2664 dwc->eps[epnum] = dep;
2665
2666 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2667 direction ? "in" : "out");
2668
Hemant Kumar72f784c2018-08-07 16:56:11 -07002669 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002670 dep->endpoint.name = dep->name;
2671
2672 if (!(dep->number > 1)) {
2673 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2674 dep->endpoint.comp_desc = NULL;
2675 }
2676
2677 spin_lock_init(&dep->lock);
2678
2679 if (num == 0)
2680 ret = dwc3_gadget_init_control_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002681 else
Mayank Rana0f67c832018-03-07 14:11:41 -08002682 ret = dwc3_gadget_init_in_out_endpoint(dep);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002683
2684 if (ret)
2685 return ret;
2686
2687 dep->endpoint.caps.dir_in = direction;
2688 dep->endpoint.caps.dir_out = !direction;
2689
2690 INIT_LIST_HEAD(&dep->pending_list);
2691 INIT_LIST_HEAD(&dep->started_list);
2692
2693 return 0;
2694}
2695
2696static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2697{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002698 u8 epnum;
Jack Pham62523502018-01-15 16:37:05 -08002699 u8 out_count;
2700 u8 in_count;
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002701 u8 idx;
Jack Pham62523502018-01-15 16:37:05 -08002702 struct dwc3_ep *dep;
2703
2704 in_count = out_count = total / 2;
2705 out_count += total & 1; /* in case odd, there is one more OUT */
Felipe Balbi72246da2011-08-19 18:10:58 +03002706
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002707 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2708
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002709 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002710 int ret;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002711 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002712
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002713 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2714 if (ret)
2715 return ret;
Jack Pham62523502018-01-15 16:37:05 -08002716
2717 dep = dwc->eps[epnum];
2718 /* Reserve EPs at the end for GSI */
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002719 if (!dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002720 out_count - NUM_GSI_OUT_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002721 idx = num - (out_count - NUM_GSI_OUT_EPS - 1);
2722 snprintf(dep->name, sizeof(dep->name), "gsi-epout%d",
2723 idx);
Jack Pham62523502018-01-15 16:37:05 -08002724 dep->endpoint.ep_type = EP_TYPE_GSI;
Hemant Kumar28d1ca12019-02-14 17:15:06 -08002725 } else if (dep->direction && num >
Jack Pham62523502018-01-15 16:37:05 -08002726 in_count - NUM_GSI_IN_EPS - 1) {
Hemant Kumaree9b63c2019-04-15 18:54:50 -07002727 idx = num - (in_count - NUM_GSI_IN_EPS - 1);
2728 snprintf(dep->name, sizeof(dep->name), "gsi-epin%d",
2729 idx);
Jack Pham62523502018-01-15 16:37:05 -08002730 dep->endpoint.ep_type = EP_TYPE_GSI;
2731 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002732 }
2733
2734 return 0;
2735}
2736
2737static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2738{
2739 struct dwc3_ep *dep;
2740 u8 epnum;
2741
2742 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2743 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002744 if (!dep)
2745 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302746 /*
2747 * Physical endpoints 0 and 1 are special; they form the
2748 * bi-directional USB endpoint 0.
2749 *
2750 * For those two physical endpoints, we don't allocate a TRB
2751 * pool nor do we add them the endpoints list. Due to that, we
2752 * shouldn't do these two operations otherwise we would end up
2753 * with all sorts of bugs when removing dwc3.ko.
2754 */
2755 if (epnum != 0 && epnum != 1) {
2756 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302758 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002759
2760 kfree(dep);
2761 }
2762}
2763
Felipe Balbi72246da2011-08-19 18:10:58 +03002764/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002765
Felipe Balbi8f608e82018-03-27 10:53:29 +03002766static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2767 struct dwc3_request *req, struct dwc3_trb *trb,
2768 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302769{
2770 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302771
Felipe Balbidc55c672016-08-12 13:20:32 +03002772 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002773
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002774 trace_dwc3_complete_trb(dep, trb);
2775
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002776 /*
2777 * If we're in the middle of series of chained TRBs and we
2778 * receive a short transfer along the way, DWC3 will skip
2779 * through all TRBs including the last TRB in the chain (the
2780 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2781 * bit and SW has to do it manually.
2782 *
2783 * We're going to do that here to avoid problems of HW trying
2784 * to use bogus TRBs for transfers.
2785 */
2786 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2787 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2788
Felipe Balbic6267a52017-01-05 14:58:46 +02002789 /*
2790 * If we're dealing with unaligned size OUT transfer, we will be left
2791 * with one TRB pending in the ring. We need to manually clear HWO bit
2792 * from that TRB.
2793 */
Thinh Nguyen4b977512018-08-02 20:17:16 -07002794 if ((req->zero || req->unaligned) && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002795 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2796 return 1;
2797 }
2798
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302799 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002800 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302801
Felipe Balbi35b27192017-03-08 13:56:37 +02002802 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2803 return 1;
2804
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002805 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302806 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002807
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002808 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302809 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002810
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302811 return 0;
2812}
2813
Felipe Balbid3692952018-03-29 13:32:10 +03002814static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2815 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2816 int status)
2817{
2818 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2819 struct scatterlist *sg = req->sg;
2820 struct scatterlist *s;
2821 unsigned int pending = req->num_pending_sgs;
2822 unsigned int i;
2823 int ret = 0;
2824
2825 for_each_sg(sg, s, pending, i) {
2826 trb = &dep->trb_pool[dep->trb_dequeue];
2827
2828 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2829 break;
2830
2831 req->sg = sg_next(s);
2832 req->num_pending_sgs--;
2833
2834 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2835 trb, event, status, true);
2836 if (ret)
2837 break;
2838 }
2839
2840 return ret;
2841}
2842
2843static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2844 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2845 int status)
2846{
2847 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2848
2849 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2850 event, status, false);
2851}
2852
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002853static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2854{
2855 return req->request.actual == req->request.length;
2856}
2857
Felipe Balbif38e35d2018-04-06 15:56:35 +03002858static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2859 const struct dwc3_event_depevt *event,
2860 struct dwc3_request *req, int status)
2861{
2862 int ret;
2863
2864 if (req->num_pending_sgs)
2865 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2866 status);
2867 else
2868 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2869 status);
2870
2871 if (req->unaligned || req->zero) {
2872 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2873 status);
2874 req->unaligned = false;
2875 req->zero = false;
2876 }
2877
2878 req->request.actual = req->request.length - req->remaining;
2879
2880 if (!dwc3_gadget_ep_request_completed(req) &&
2881 req->num_pending_sgs) {
2882 __dwc3_gadget_kick_transfer(dep);
2883 goto out;
2884 }
2885
2886 dwc3_gadget_giveback(dep, req, status);
2887
2888out:
2889 return ret;
2890}
2891
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002892static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002893 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002894{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002895 struct dwc3_request *req;
Felipe Balbi72246da2011-08-19 18:10:58 +03002896
Hemant Kumar7d2436362018-11-16 17:24:39 -08002897 while (!list_empty(&dep->started_list)) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002898 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002899
Hemant Kumar7d2436362018-11-16 17:24:39 -08002900 req = next_request(&dep->started_list);
Felipe Balbif38e35d2018-04-06 15:56:35 +03002901 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2902 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002903 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002904 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002905 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002906}
2907
Felipe Balbiee3638b2018-03-27 11:26:53 +03002908static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2909 const struct dwc3_event_depevt *event)
2910{
Felipe Balbif62afb42018-04-11 10:34:34 +03002911 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002912}
2913
Felipe Balbi8f608e82018-03-27 10:53:29 +03002914static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2915 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002916{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002917 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002918 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002919 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002920
Felipe Balbiee3638b2018-03-27 11:26:53 +03002921 dwc3_gadget_endpoint_frame_from_event(dep, event);
2922
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 if (event->status & DEPEVT_STATUS_BUSERR)
2924 status = -ECONNRESET;
2925
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002926 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2927 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002928
2929 if (list_empty(&dep->started_list))
2930 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002931 }
2932
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002933 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002934
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002935 if (stop) {
Jack Pham62523502018-01-15 16:37:05 -08002936 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002937 dep->flags = DWC3_EP_ENABLED;
2938 }
2939
Felipe Balbifae2b902011-10-14 13:00:30 +03002940 /*
2941 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2942 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2943 */
2944 if (dwc->revision < DWC3_REVISION_183A) {
2945 u32 reg;
2946 int i;
2947
2948 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002949 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002950
2951 if (!(dep->flags & DWC3_EP_ENABLED))
2952 continue;
2953
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002954 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002955 return;
2956 }
2957
2958 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2959 reg |= dwc->u1u2;
2960 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2961
2962 dwc->u1u2 = 0;
2963 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002964}
2965
Felipe Balbi8f608e82018-03-27 10:53:29 +03002966static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2967 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002968{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002969 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002970 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002971}
2972
Felipe Balbi72246da2011-08-19 18:10:58 +03002973static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2974 const struct dwc3_event_depevt *event)
2975{
2976 struct dwc3_ep *dep;
2977 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002978 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002979
2980 dep = dwc->eps[epnum];
2981
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002982 if (!(dep->flags & DWC3_EP_ENABLED)) {
2983 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2984 return;
2985
2986 /* Handle only EPCMDCMPLT when EP disabled */
2987 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2988 return;
2989 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002990
Felipe Balbi72246da2011-08-19 18:10:58 +03002991 if (epnum == 0 || epnum == 1) {
2992 dwc3_ep0_interrupt(dwc, event);
2993 return;
2994 }
2995
Jack Phama4c7b782018-01-18 14:19:38 -08002996 dep->dbg_ep_events.total++;
2997
Felipe Balbi72246da2011-08-19 18:10:58 +03002998 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002999 case DWC3_DEPEVT_XFERINPROGRESS:
Jack Phama4c7b782018-01-18 14:19:38 -08003000 dep->dbg_ep_events.xferinprogress++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03003001 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003002 break;
3003 case DWC3_DEPEVT_XFERNOTREADY:
Jack Phama4c7b782018-01-18 14:19:38 -08003004 dep->dbg_ep_events.xfernotready++;
Felipe Balbi8f608e82018-03-27 10:53:29 +03003005 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003007 case DWC3_DEPEVT_EPCMDCMPLT:
Jack Phama4c7b782018-01-18 14:19:38 -08003008 dep->dbg_ep_events.epcmdcomplete++;
Baolin Wang76a638f2016-10-31 19:38:36 +08003009 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3010
3011 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
3012 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3013 wake_up(&dep->wait_end_transfer);
3014 }
3015 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03003016 case DWC3_DEPEVT_STREAMEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08003017 dep->dbg_ep_events.streamevent++;
3018 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003019 case DWC3_DEPEVT_XFERCOMPLETE:
Jack Phama4c7b782018-01-18 14:19:38 -08003020 dep->dbg_ep_events.xfercomplete++;
3021 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003022 case DWC3_DEPEVT_RXTXFIFOEVT:
Jack Phama4c7b782018-01-18 14:19:38 -08003023 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003024 break;
3025 }
3026}
3027
3028static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3029{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003030 struct usb_gadget_driver *gadget_driver;
3031
Felipe Balbi72246da2011-08-19 18:10:58 +03003032 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003033 gadget_driver = dwc->gadget_driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03003034 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003035 dbg_event(0xFF, "DISCONNECT", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003036 gadget_driver->disconnect(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003037 spin_lock(&dwc->lock);
3038 }
3039}
3040
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003041static void dwc3_suspend_gadget(struct dwc3 *dwc)
3042{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003043 struct usb_gadget_driver *gadget_driver;
3044
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003045 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003046 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003047 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003048 dbg_event(0xFF, "SUSPEND", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003049 gadget_driver->suspend(&dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003050 spin_lock(&dwc->lock);
3051 }
3052}
3053
3054static void dwc3_resume_gadget(struct dwc3 *dwc)
3055{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003056 struct usb_gadget_driver *gadget_driver;
3057
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003058 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003059 gadget_driver = dwc->gadget_driver;
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003060 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003061 dbg_event(0xFF, "RESUME", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003062 gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003063 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003064 }
3065}
3066
3067static void dwc3_reset_gadget(struct dwc3 *dwc)
3068{
Mayank Rana61e8bed2017-11-15 09:57:15 -08003069 struct usb_gadget_driver *gadget_driver;
3070
Felipe Balbi8e744752014-11-06 14:27:53 +08003071 if (!dwc->gadget_driver)
3072 return;
3073
3074 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
Mayank Rana61e8bed2017-11-15 09:57:15 -08003075 gadget_driver = dwc->gadget_driver;
Felipe Balbi8e744752014-11-06 14:27:53 +08003076 spin_unlock(&dwc->lock);
Jack Pham62523502018-01-15 16:37:05 -08003077 dbg_event(0xFF, "UDC RESET", 0);
Mayank Rana61e8bed2017-11-15 09:57:15 -08003078 usb_gadget_udc_reset(&dwc->gadget, gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003079 spin_lock(&dwc->lock);
3080 }
3081}
3082
Jack Pham62523502018-01-15 16:37:05 -08003083void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03003084{
Jack Pham62523502018-01-15 16:37:05 -08003085 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003086 struct dwc3_gadget_ep_cmd_params params;
3087 u32 cmd;
3088 int ret;
3089
Jack Pham62523502018-01-15 16:37:05 -08003090 dep = dwc->eps[epnum];
3091
Baolin Wang76a638f2016-10-31 19:38:36 +08003092 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
3093 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303094 return;
3095
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003096 if (dep->endpoint.endless)
3097 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
3098 dep->number);
3099
Pratyush Anand57911502012-07-06 15:19:10 +05303100 /*
3101 * NOTICE: We are violating what the Databook says about the
3102 * EndTransfer command. Ideally we would _always_ wait for the
3103 * EndTransfer Command Completion IRQ, but that's causing too
3104 * much trouble synchronizing between us and gadget driver.
3105 *
3106 * We have discussed this with the IP Provider and it was
3107 * suggested to giveback all requests here, but give HW some
3108 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01003109 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05303110 *
3111 * Note also that a similar handling was tested by Synopsys
3112 * (thanks a lot Paul) and nothing bad has come out of it.
3113 * In short, what we're doing is:
3114 *
3115 * - Issue EndTransfer WITH CMDIOC bit set
3116 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07003117 *
3118 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3119 * supports a mode to work around the above limitation. The
3120 * software can poll the CMDACT bit in the DEPCMD register
3121 * after issuing a EndTransfer command. This mode is enabled
3122 * by writing GUCTL2[14]. This polling is already done in the
3123 * dwc3_send_gadget_ep_cmd() function so if the mode is
3124 * enabled, the EndTransfer command will have completed upon
3125 * returning from this function and we don't need to delay for
3126 * 100us.
3127 *
3128 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303129 */
3130
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303131 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003132 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3133 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03003134 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303135 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003136 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303137 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003138 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003139
Baolin Wang76a638f2016-10-31 19:38:36 +08003140 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
Mayank Rana8bd2a062018-06-28 11:08:52 -07003141 if (dep->endpoint.ep_type != EP_TYPE_GSI)
3142 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07003143 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08003144 }
Mayank Rana21f76b32018-09-21 16:49:55 -07003145 dbg_log_string("%s(%d): endxfer ret:%d)",
3146 dep->name, dep->number, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003147}
3148
Hemant Kumar7b7e0652019-04-09 18:00:55 -07003149void dwc3_stop_active_transfer_noioc(struct dwc3 *dwc, u32 epnum, bool force)
3150{
3151 struct dwc3_ep *dep;
3152 struct dwc3_gadget_ep_cmd_params params;
3153 u32 cmd;
3154 int ret;
3155
3156 dep = dwc->eps[epnum];
3157
3158 if (!dep->resource_index)
3159 return;
3160
3161 if (dep->endpoint.endless)
3162 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_DISABLE_UPDXFER,
3163 dep->number);
3164
3165 cmd = DWC3_DEPCMD_ENDTRANSFER;
3166 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
3167 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
3168 memset(&params, 0, sizeof(params));
3169 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
3170 WARN_ON_ONCE(ret);
3171 dep->resource_index = 0;
3172
3173 dbg_log_string("%s(%d): endxfer ret:%d)",
3174 dep->name, dep->number, ret);
3175}
3176
Felipe Balbi72246da2011-08-19 18:10:58 +03003177static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3178{
3179 u32 epnum;
3180
3181 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3182 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003183 int ret;
3184
3185 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003186 if (!dep)
3187 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003188
3189 if (!(dep->flags & DWC3_EP_STALL))
3190 continue;
3191
3192 dep->flags &= ~DWC3_EP_STALL;
3193
John Youn50c763f2016-05-31 17:49:56 -07003194 ret = dwc3_send_clear_stall_ep_cmd(dep);
Jack Pham62523502018-01-15 16:37:05 -08003195 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03003196 WARN_ON_ONCE(ret);
3197 }
3198}
3199
3200static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3201{
Felipe Balbic4430a22012-05-24 10:30:01 +03003202 int reg;
3203
Jack Pham62523502018-01-15 16:37:05 -08003204 dbg_event(0xFF, "DISCONNECT INT", 0);
3205 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3206 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003207 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003208
Felipe Balbi72246da2011-08-19 18:10:58 +03003209 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3210 reg &= ~DWC3_DCTL_INITU1ENA;
3211 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3212
3213 reg &= ~DWC3_DCTL_INITU2ENA;
3214 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003215
Felipe Balbi72246da2011-08-19 18:10:58 +03003216 dwc3_disconnect_gadget(dwc);
3217
3218 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003219 dwc->setup_packet_pending = false;
Jack Pham62523502018-01-15 16:37:05 -08003220 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05003221 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003222
3223 dwc->connected = false;
Jack Pham62523502018-01-15 16:37:05 -08003224 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003225}
3226
Felipe Balbi72246da2011-08-19 18:10:58 +03003227static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3228{
3229 u32 reg;
3230
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003231 usb_phy_start_link_training(dwc->usb3_phy);
3232
Felipe Balbifc8bb912016-05-16 13:14:48 +03003233 dwc->connected = true;
3234
Felipe Balbidf62df52011-10-14 15:11:49 +03003235 /*
3236 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3237 * would cause a missing Disconnect Event if there's a
3238 * pending Setup Packet in the FIFO.
3239 *
3240 * There's no suggested workaround on the official Bug
3241 * report, which states that "unless the driver/application
3242 * is doing any special handling of a disconnect event,
3243 * there is no functional issue".
3244 *
3245 * Unfortunately, it turns out that we _do_ some special
3246 * handling of a disconnect event, namely complete all
3247 * pending transfers, notify gadget driver of the
3248 * disconnection, and so on.
3249 *
3250 * Our suggested workaround is to follow the Disconnect
3251 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003252 * flag. Such flag gets set whenever we have a SETUP_PENDING
3253 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003254 * same endpoint.
3255 *
3256 * Refers to:
3257 *
3258 * STAR#9000466709: RTL: Device : Disconnect event not
3259 * generated if setup packet pending in FIFO
3260 */
3261 if (dwc->revision < DWC3_REVISION_188A) {
3262 if (dwc->setup_packet_pending)
3263 dwc3_gadget_disconnect_interrupt(dwc);
3264 }
3265
Jack Pham62523502018-01-15 16:37:05 -08003266 dbg_event(0xFF, "BUS RESET", 0);
3267 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3268 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003269 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003270
3271 usb_gadget_vbus_draw(&dwc->gadget, 100);
3272
Felipe Balbi8e744752014-11-06 14:27:53 +08003273 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003274
3275 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3276 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3277 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003278 dwc->test_mode = false;
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303279 /*
3280 * From SNPS databook section 8.1.2
3281 * the EP0 should be in setup phase. So ensure
3282 * that EP0 is in setup phase by issuing a stall
3283 * and restart if EP0 is not in setup phase.
3284 */
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303285 if (dwc->ep0state != EP0_SETUP_PHASE) {
3286 unsigned int dir;
3287
3288 dbg_event(0xFF, "CONTRPEND", dwc->ep0state);
3289 dir = !!dwc->ep0_expect_in;
3290 if (dwc->ep0state == EP0_DATA_PHASE)
3291 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
3292 else
3293 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303294 dwc3_ep0_stall_and_restart(dwc);
Vijayavardhan Vennapusab54e6c32018-03-13 15:01:14 +05303295 }
Sriharsha Allenkic7279192017-11-16 17:00:09 +05303296
3297 dwc3_stop_active_transfers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003298 dwc3_clear_stall_all_ep(dwc);
3299
3300 /* Reset device address to zero */
3301 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3302 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3303 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Jack Pham62523502018-01-15 16:37:05 -08003304
3305 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3306 dwc->link_state = DWC3_LINK_STATE_U0;
3307 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003308}
3309
Felipe Balbi72246da2011-08-19 18:10:58 +03003310static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3311{
Felipe Balbi72246da2011-08-19 18:10:58 +03003312 struct dwc3_ep *dep;
3313 int ret;
3314 u32 reg;
3315 u8 speed;
3316
Jack Pham62523502018-01-15 16:37:05 -08003317 dbg_event(0xFF, "CONNECT DONE", 0);
Vamsi Krishna Samavedam77267712018-06-01 10:17:28 -07003318 usb_phy_stop_link_training(dwc->usb3_phy);
Felipe Balbi72246da2011-08-19 18:10:58 +03003319 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3320 speed = reg & DWC3_DSTS_CONNECTSPD;
3321 dwc->speed = speed;
3322
Jack Pham74cc2572019-04-02 18:39:41 -07003323 /* Enable SUSPENDEVENT(BIT:6) for version 230A and above */
3324 if (dwc->revision >= DWC3_REVISION_230A) {
3325 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
3326 reg |= DWC3_DEVTEN_EOPFEN;
3327 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
3328 }
3329
John Youn5fb6fda2016-11-10 17:23:25 -08003330 /*
3331 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3332 * each time on Connect Done.
3333 *
3334 * Currently we always use the reset value. If any platform
3335 * wants to set this to a different value, we need to add a
3336 * setting and update GCTL.RAMCLKSEL here.
3337 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003338
3339 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003340 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003341 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3342 dwc->gadget.ep0->maxpacket = 512;
3343 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3344 break;
John Youn2da9ad72016-05-20 16:34:26 -07003345 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003346 /*
3347 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3348 * would cause a missing USB3 Reset event.
3349 *
3350 * In such situations, we should force a USB3 Reset
3351 * event by calling our dwc3_gadget_reset_interrupt()
3352 * routine.
3353 *
3354 * Refers to:
3355 *
3356 * STAR#9000483510: RTL: SS : USB3 reset event may
3357 * not be generated always when the link enters poll
3358 */
3359 if (dwc->revision < DWC3_REVISION_190A)
3360 dwc3_gadget_reset_interrupt(dwc);
3361
Felipe Balbi72246da2011-08-19 18:10:58 +03003362 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3363 dwc->gadget.ep0->maxpacket = 512;
3364 dwc->gadget.speed = USB_SPEED_SUPER;
3365 break;
John Youn2da9ad72016-05-20 16:34:26 -07003366 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003367 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3368 dwc->gadget.ep0->maxpacket = 64;
3369 dwc->gadget.speed = USB_SPEED_HIGH;
3370 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003371 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003372 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3373 dwc->gadget.ep0->maxpacket = 64;
3374 dwc->gadget.speed = USB_SPEED_FULL;
3375 break;
John Youn2da9ad72016-05-20 16:34:26 -07003376 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003377 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3378 dwc->gadget.ep0->maxpacket = 8;
3379 dwc->gadget.speed = USB_SPEED_LOW;
3380 break;
3381 }
3382
Thinh Nguyen61800262018-01-12 18:18:05 -08003383 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3384
Pratyush Anand2b758352013-01-14 15:59:31 +05303385 /* Enable USB2 LPM Capability */
3386
John Younee5cd412016-02-05 17:08:45 -08003387 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003388 (speed != DWC3_DSTS_SUPERSPEED) &&
3389 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303390 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3391 reg |= DWC3_DCFG_LPM_CAP;
3392 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3393
3394 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3395 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3396
Huang Rui460d0982014-10-31 11:11:18 +08003397 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303398
Huang Rui80caf7d2014-10-28 19:54:26 +08003399 /*
3400 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3401 * DCFG.LPMCap is set, core responses with an ACK and the
3402 * BESL value in the LPM token is less than or equal to LPM
3403 * NYET threshold.
3404 */
3405 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3406 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003407 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003408
3409 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3410 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3411
Pratyush Anand2b758352013-01-14 15:59:31 +05303412 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003413 } else {
3414 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3415 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3416 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303417 }
3418
Felipe Balbi72246da2011-08-19 18:10:58 +03003419 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003420 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003421 if (ret) {
3422 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3423 return;
3424 }
3425
3426 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003427 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003428 if (ret) {
3429 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3430 return;
3431 }
3432
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003433 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003434
Felipe Balbi72246da2011-08-19 18:10:58 +03003435 /*
3436 * Configure PHY via GUSB3PIPECTLn if required.
3437 *
3438 * Update GTXFIFOSIZn
3439 *
3440 * In both cases reset values should be sufficient.
3441 */
3442}
3443
Jack Pham62523502018-01-15 16:37:05 -08003444static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003445{
Jack Pham62523502018-01-15 16:37:05 -08003446 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003447
Jack Pham62523502018-01-15 16:37:05 -08003448 dev_dbg(dwc->dev, "%s\n", __func__);
3449
3450 dbg_event(0xFF, "WAKEUP", remote_wakeup);
3451 /*
3452 * Identify if it is called from wakeup_interrupt() context for bus
3453 * resume or as part of remote wakeup. And based on that check for
3454 * U3 state. as we need to handle case of L1 resume i.e. where we
3455 * don't want to perform resume.
3456 */
3457 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3458 perform_resume = false;
3459
3460 /* Only perform resume from L2 or Early Suspend states */
3461 if (perform_resume) {
3462
3463 /*
3464 * In case of remote wake up dwc3_gadget_wakeup_work()
3465 * is doing pm_runtime_get_sync().
3466 */
3467 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3468 dwc->b_suspend = false;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003469 dwc3_notify_event(dwc,
3470 DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003471
3472 /*
3473 * set state to U0 as function level resume is trying to queue
3474 * notification over USB interrupt endpoint which would fail
3475 * due to state is not being updated.
3476 */
3477 dwc->link_state = DWC3_LINK_STATE_U0;
3478 dwc3_resume_gadget(dwc);
3479 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003480 }
Jack Pham62523502018-01-15 16:37:05 -08003481
3482 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003483}
3484
3485static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3486 unsigned int evtinfo)
3487{
Felipe Balbifae2b902011-10-14 13:00:30 +03003488 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003489 unsigned int pwropt;
3490
3491 /*
3492 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3493 * Hibernation mode enabled which would show up when device detects
3494 * host-initiated U3 exit.
3495 *
3496 * In that case, device will generate a Link State Change Interrupt
3497 * from U3 to RESUME which is only necessary if Hibernation is
3498 * configured in.
3499 *
3500 * There are no functional changes due to such spurious event and we
3501 * just need to ignore it.
3502 *
3503 * Refers to:
3504 *
3505 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3506 * operational mode
3507 */
3508 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3509 if ((dwc->revision < DWC3_REVISION_250A) &&
3510 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3511 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3512 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003513 return;
3514 }
3515 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003516
3517 /*
3518 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3519 * on the link partner, the USB session might do multiple entry/exit
3520 * of low power states before a transfer takes place.
3521 *
3522 * Due to this problem, we might experience lower throughput. The
3523 * suggested workaround is to disable DCTL[12:9] bits if we're
3524 * transitioning from U1/U2 to U0 and enable those bits again
3525 * after a transfer completes and there are no pending transfers
3526 * on any of the enabled endpoints.
3527 *
3528 * This is the first half of that workaround.
3529 *
3530 * Refers to:
3531 *
3532 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3533 * core send LGO_Ux entering U0
3534 */
3535 if (dwc->revision < DWC3_REVISION_183A) {
3536 if (next == DWC3_LINK_STATE_U0) {
3537 u32 u1u2;
3538 u32 reg;
3539
3540 switch (dwc->link_state) {
3541 case DWC3_LINK_STATE_U1:
3542 case DWC3_LINK_STATE_U2:
3543 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3544 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3545 | DWC3_DCTL_ACCEPTU2ENA
3546 | DWC3_DCTL_INITU1ENA
3547 | DWC3_DCTL_ACCEPTU1ENA);
3548
3549 if (!dwc->u1u2)
3550 dwc->u1u2 = reg & u1u2;
3551
3552 reg &= ~u1u2;
3553
3554 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3555 break;
3556 default:
3557 /* do nothing */
3558 break;
3559 }
3560 }
3561 }
3562
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003563 switch (next) {
3564 case DWC3_LINK_STATE_U1:
3565 if (dwc->speed == USB_SPEED_SUPER)
3566 dwc3_suspend_gadget(dwc);
3567 break;
3568 case DWC3_LINK_STATE_U2:
3569 case DWC3_LINK_STATE_U3:
3570 dwc3_suspend_gadget(dwc);
3571 break;
3572 case DWC3_LINK_STATE_RESUME:
3573 dwc3_resume_gadget(dwc);
3574 break;
3575 default:
3576 /* do nothing */
3577 break;
3578 }
3579
Jack Pham62523502018-01-15 16:37:05 -08003580 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003581 dwc->link_state = next;
Jack Pham62523502018-01-15 16:37:05 -08003582 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003583}
3584
Baolin Wang72704f82016-05-16 16:43:53 +08003585static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3586 unsigned int evtinfo)
3587{
3588 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3589
Jack Pham62523502018-01-15 16:37:05 -08003590 dbg_event(0xFF, "SUSPEND INT", 0);
3591 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3592
3593 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3594 /*
3595 * When first connecting the cable, even before the initial
3596 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3597 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3598 * event. In such a case, ignore.
3599 * Ignore suspend event until device side usb is not into
3600 * CONFIGURED state.
3601 */
3602 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3603 dev_err(dwc->dev, "%s(): state:%d. Ignore SUSPEND.\n",
3604 __func__, dwc->gadget.state);
3605 return;
3606 }
3607
Baolin Wang72704f82016-05-16 16:43:53 +08003608 dwc3_suspend_gadget(dwc);
3609
Jack Pham62523502018-01-15 16:37:05 -08003610 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3611 dwc->b_suspend = true;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003612 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT, 0);
Jack Pham62523502018-01-15 16:37:05 -08003613 }
3614
Baolin Wang72704f82016-05-16 16:43:53 +08003615 dwc->link_state = next;
3616}
3617
Felipe Balbie1dadd32014-02-25 14:47:54 -06003618static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3619 unsigned int evtinfo)
3620{
3621 unsigned int is_ss = evtinfo & BIT(4);
3622
Felipe Balbibfad65e2017-04-19 14:59:27 +03003623 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003624 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3625 * have a known issue which can cause USB CV TD.9.23 to fail
3626 * randomly.
3627 *
3628 * Because of this issue, core could generate bogus hibernation
3629 * events which SW needs to ignore.
3630 *
3631 * Refers to:
3632 *
3633 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3634 * Device Fallback from SuperSpeed
3635 */
3636 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3637 return;
3638
3639 /* enter hibernation here */
3640}
3641
Felipe Balbi72246da2011-08-19 18:10:58 +03003642static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3643 const struct dwc3_event_devt *event)
3644{
3645 switch (event->type) {
3646 case DWC3_DEVICE_EVENT_DISCONNECT:
3647 dwc3_gadget_disconnect_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003648 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003649 break;
3650 case DWC3_DEVICE_EVENT_RESET:
3651 dwc3_gadget_reset_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003652 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003653 break;
3654 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3655 dwc3_gadget_conndone_interrupt(dwc);
Jack Phama4c7b782018-01-18 14:19:38 -08003656 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003657 break;
3658 case DWC3_DEVICE_EVENT_WAKEUP:
Jack Pham62523502018-01-15 16:37:05 -08003659 dwc3_gadget_wakeup_interrupt(dwc, false);
Jack Phama4c7b782018-01-18 14:19:38 -08003660 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003661 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003662 case DWC3_DEVICE_EVENT_HIBER_REQ:
3663 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3664 "unexpected hibernation event\n"))
3665 break;
3666
3667 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3668 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003669 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3670 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Jack Phama4c7b782018-01-18 14:19:38 -08003671 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003672 break;
3673 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003674 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003675 if (dwc->revision >= DWC3_REVISION_230A) {
Jack Pham62523502018-01-15 16:37:05 -08003676 dbg_event(0xFF, "GAD SUS", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003677 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003678 /*
3679 * Ignore suspend event until the gadget enters into
3680 * USB_STATE_CONFIGURED state.
3681 */
3682 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3683 dwc3_gadget_suspend_interrupt(dwc,
3684 event->event_info);
Jack Pham51c5d1f2018-11-02 17:46:17 -07003685 else
3686 usb_gadget_vbus_draw(&dwc->gadget, 2);
Baolin Wang72704f82016-05-16 16:43:53 +08003687 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003688 break;
3689 case DWC3_DEVICE_EVENT_SOF:
Jack Phama4c7b782018-01-18 14:19:38 -08003690 dwc->dbg_gadget_events.sof++;
3691 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003692 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Jack Pham62523502018-01-15 16:37:05 -08003693 dbg_event(0xFF, "ERROR", 0);
Jack Phama4c7b782018-01-18 14:19:38 -08003694 dwc->dbg_gadget_events.erratic_error++;
Jack Pham62523502018-01-15 16:37:05 -08003695 dwc->err_evt_seen = true;
Jack Phama4c7b782018-01-18 14:19:38 -08003696 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003697 case DWC3_DEVICE_EVENT_CMD_CMPL:
Jack Phama4c7b782018-01-18 14:19:38 -08003698 dwc->dbg_gadget_events.cmdcmplt++;
3699 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003700 case DWC3_DEVICE_EVENT_OVERFLOW:
Jack Phama4c7b782018-01-18 14:19:38 -08003701 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003702 break;
3703 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003704 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Jack Phama4c7b782018-01-18 14:19:38 -08003705 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003706 }
3707}
3708
3709static void dwc3_process_event_entry(struct dwc3 *dwc,
3710 const union dwc3_event *event)
3711{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003712 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003713
Felipe Balbidfc5e802017-04-26 13:44:51 +03003714 if (!event->type.is_devspec)
3715 dwc3_endpoint_interrupt(dwc, &event->depevt);
3716 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003717 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003718 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003719 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003720}
3721
Felipe Balbidea520a2016-03-30 09:39:34 +03003722static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003723{
Felipe Balbidea520a2016-03-30 09:39:34 +03003724 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003725 irqreturn_t ret = IRQ_NONE;
3726 int left;
3727 u32 reg;
3728
Felipe Balbif42f2442013-06-12 21:25:08 +03003729 left = evt->count;
3730
3731 if (!(evt->flags & DWC3_EVENT_PENDING))
3732 return IRQ_NONE;
3733
3734 while (left > 0) {
3735 union dwc3_event event;
3736
John Younebbb2d52016-11-15 13:07:02 +02003737 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003738
3739 dwc3_process_event_entry(dwc, &event);
3740
Jack Pham62523502018-01-15 16:37:05 -08003741 if (dwc->err_evt_seen) {
3742 /*
3743 * if erratic error, skip remaining events
3744 * while controller undergoes reset
3745 */
3746 evt->lpos = (evt->lpos + left) %
3747 DWC3_EVENT_BUFFERS_SIZE;
Vijayavardhan Vennapusa6a0290f2018-07-20 10:51:27 -07003748 if (dwc3_notify_event(dwc,
3749 DWC3_CONTROLLER_ERROR_EVENT, 0))
Jack Pham62523502018-01-15 16:37:05 -08003750 dwc->err_evt_seen = 0;
3751 break;
3752 }
3753
Felipe Balbif42f2442013-06-12 21:25:08 +03003754 /*
3755 * FIXME we wrap around correctly to the next entry as
3756 * almost all entries are 4 bytes in size. There is one
3757 * entry which has 12 bytes which is a regular entry
3758 * followed by 8 bytes data. ATM I don't know how
3759 * things are organized if we get next to the a
3760 * boundary so I worry about that once we try to handle
3761 * that.
3762 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003763 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003764 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003765 }
3766
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003767 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003768 evt->count = 0;
3769 evt->flags &= ~DWC3_EVENT_PENDING;
3770 ret = IRQ_HANDLED;
3771
3772 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003773 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003774 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003775 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003776
John Youncf40b862016-11-14 12:32:43 -08003777 if (dwc->imod_interval) {
3778 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3779 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3780 }
3781
Felipe Balbif42f2442013-06-12 21:25:08 +03003782 return ret;
3783}
3784
Jack Pham62523502018-01-15 16:37:05 -08003785void dwc3_bh_work(struct work_struct *w)
3786{
3787 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3788
3789 pm_runtime_get_sync(dwc->dev);
3790 dwc3_thread_interrupt(dwc->irq, dwc->ev_buf);
3791 pm_runtime_put(dwc->dev);
3792}
3793
Felipe Balbidea520a2016-03-30 09:39:34 +03003794static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003795{
Felipe Balbidea520a2016-03-30 09:39:34 +03003796 struct dwc3_event_buffer *evt = _evt;
3797 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003798 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003799 irqreturn_t ret = IRQ_NONE;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003800 ktime_t start_time;
3801
3802 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003803
Felipe Balbie5f68b42015-10-12 13:25:44 -05003804 spin_lock_irqsave(&dwc->lock, flags);
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003805 dwc->bh_handled_evt_cnt[dwc->irq_dbg_index] = 0;
Felipe Balbidea520a2016-03-30 09:39:34 +03003806 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05003807 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003808
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003809 dwc->bh_completion_time[dwc->irq_dbg_index] =
3810 ktime_to_us(ktime_sub(ktime_get(), start_time));
3811 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3812
Felipe Balbib15a7622011-06-30 16:57:15 +03003813 return ret;
3814}
3815
Felipe Balbidea520a2016-03-30 09:39:34 +03003816static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003817{
Felipe Balbidea520a2016-03-30 09:39:34 +03003818 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003819 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003820 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003821 u32 reg;
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003822 ktime_t start_time;
3823
3824 start_time = ktime_get();
3825 dwc->irq_cnt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003826
Jack Pham62523502018-01-15 16:37:05 -08003827 /* controller reset is still pending */
3828 if (dwc->err_evt_seen)
Felipe Balbifc8bb912016-05-16 13:14:48 +03003829 return IRQ_HANDLED;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003830
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003831 /*
3832 * With PCIe legacy interrupt, test shows that top-half irq handler can
3833 * be called again after HW interrupt deassertion. Check if bottom-half
3834 * irq event handler completes before caching new event to prevent
3835 * losing events.
3836 */
3837 if (evt->flags & DWC3_EVENT_PENDING)
3838 return IRQ_HANDLED;
3839
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003840 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003841 count &= DWC3_GEVNTCOUNT_MASK;
3842 if (!count)
3843 return IRQ_NONE;
3844
Felipe Balbib15a7622011-06-30 16:57:15 +03003845 evt->count = count;
3846 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003847
Felipe Balbie8adfc32013-06-12 21:11:14 +03003848 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003849 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003850 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003851 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003852
John Younebbb2d52016-11-15 13:07:02 +02003853 amount = min(count, evt->length - evt->lpos);
3854 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3855
3856 if (amount < count)
3857 memcpy(evt->cache, evt->buf, count - amount);
3858
John Youn65aca322016-11-15 13:08:59 +02003859 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3860
Jack Phamfe9c8ba2014-10-15 18:23:37 -07003861 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3862 dwc->irq_completion_time[dwc->irq_dbg_index] =
3863 ktime_us_delta(ktime_get(), start_time);
3864 dwc->irq_event_count[dwc->irq_dbg_index] = count / 4;
3865 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3866
Felipe Balbib15a7622011-06-30 16:57:15 +03003867 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003868}
3869
Jack Pham62523502018-01-15 16:37:05 -08003870irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003871{
Jack Pham62523502018-01-15 16:37:05 -08003872 struct dwc3 *dwc = _dwc;
3873 irqreturn_t ret = IRQ_NONE;
3874 irqreturn_t status;
Felipe Balbi72246da2011-08-19 18:10:58 +03003875
Jack Pham62523502018-01-15 16:37:05 -08003876 status = dwc3_check_event_buf(dwc->ev_buf);
3877 if (status == IRQ_WAKE_THREAD)
3878 ret = status;
3879
3880 if (ret == IRQ_WAKE_THREAD)
3881 queue_work(dwc->dwc_wq, &dwc->bh_work);
3882
3883 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003884}
3885
Felipe Balbi6db38122016-10-03 11:27:01 +03003886static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3887{
3888 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3889 int irq;
3890
3891 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3892 if (irq > 0)
3893 goto out;
3894
3895 if (irq == -EPROBE_DEFER)
3896 goto out;
3897
3898 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3899 if (irq > 0)
3900 goto out;
3901
3902 if (irq == -EPROBE_DEFER)
3903 goto out;
3904
3905 irq = platform_get_irq(dwc3_pdev, 0);
3906 if (irq > 0)
3907 goto out;
3908
3909 if (irq != -EPROBE_DEFER)
3910 dev_err(dwc->dev, "missing peripheral IRQ\n");
3911
3912 if (!irq)
3913 irq = -EINVAL;
3914
3915out:
3916 return irq;
3917}
3918
Felipe Balbi72246da2011-08-19 18:10:58 +03003919/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003920 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003921 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003922 *
3923 * Returns 0 on success otherwise negative errno.
3924 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003925int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003926{
Felipe Balbi6db38122016-10-03 11:27:01 +03003927 int ret;
3928 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003929
Felipe Balbi6db38122016-10-03 11:27:01 +03003930 irq = dwc3_gadget_get_irq(dwc);
3931 if (irq < 0) {
3932 ret = irq;
3933 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003934 }
3935
3936 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003937
Jack Pham62523502018-01-15 16:37:05 -08003938 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3939
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303940 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3941 sizeof(*dwc->ep0_trb) * 2,
3942 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003943 if (!dwc->ep0_trb) {
3944 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3945 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003946 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003947 }
3948
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003949 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003950 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003951 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003952 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003953 }
3954
Felipe Balbi905dc042017-01-05 14:46:52 +02003955 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3956 &dwc->bounce_addr, GFP_KERNEL);
3957 if (!dwc->bounce) {
3958 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003959 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003960 }
3961
Baolin Wangbb014732016-10-14 17:11:33 +08003962 init_completion(&dwc->ep0_in_setup);
3963
Mayank Rana0f67c832018-03-07 14:11:41 -08003964 dwc->gadget.ops = &dwc3_gadget_ops;
3965 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3966 dwc->gadget.sg_supported = true;
3967 dwc->gadget.name = "dwc3-gadget";
3968 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003969
3970 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003971 * FIXME We might be setting max_speed to <SUPER, however versions
3972 * <2.20a of dwc3 have an issue with metastability (documented
3973 * elsewhere in this driver) which tells us we can't set max speed to
3974 * anything lower than SUPER.
3975 *
3976 * Because gadget.max_speed is only used by composite.c and function
3977 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3978 * to happen so we avoid sending SuperSpeed Capability descriptor
3979 * together with our BOS descriptor as that could confuse host into
3980 * thinking we can handle super speed.
3981 *
3982 * Note that, in fact, we won't even support GetBOS requests when speed
3983 * is less than super speed because we don't have means, yet, to tell
3984 * composite.c that we are USB 2.0 + LPM ECN.
3985 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003986 if (dwc->revision < DWC3_REVISION_220A &&
3987 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003988 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003989 dwc->revision);
3990
3991 dwc->gadget.max_speed = dwc->maximum_speed;
3992
3993 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003994 * REVISIT: Here we should clear all pending IRQs to be
3995 * sure we're starting from a well known location.
3996 */
3997
Mayank Rana0f67c832018-03-07 14:11:41 -08003998 dwc->num_eps = DWC3_ENDPOINTS_NUM;
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003999 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03004000 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03004001 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03004002
Felipe Balbi72246da2011-08-19 18:10:58 +03004003 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
4004 if (ret) {
4005 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03004006 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03004007 }
4008
4009 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004010
4011err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004012 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03004013
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004014err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004015 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4016 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004017
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004018err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004019 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004020
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004021err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304022 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004023 dwc->ep0_trb, dwc->ep0_trb_addr);
4024
Felipe Balbi72246da2011-08-19 18:10:58 +03004025err0:
4026 return ret;
4027}
4028
Felipe Balbi7415f172012-04-30 14:56:33 +03004029/* -------------------------------------------------------------------------- */
4030
Felipe Balbi72246da2011-08-19 18:10:58 +03004031void dwc3_gadget_exit(struct dwc3 *dwc)
4032{
Felipe Balbi72246da2011-08-19 18:10:58 +03004033 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004034 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02004035 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004036 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004037 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304038 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004039 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004040}
Felipe Balbi7415f172012-04-30 14:56:33 +03004041
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004042int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004043{
Roger Quadros9772b472016-04-12 11:33:29 +03004044 if (!dwc->gadget_driver)
4045 return 0;
4046
Roger Quadros1551e352017-02-15 14:16:26 +02004047 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004048 dwc3_disconnect_gadget(dwc);
4049 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004050
Bo He03a5d4d2019-01-14 09:48:32 +02004051 synchronize_irq(dwc->irq_gadget);
4052
Felipe Balbi7415f172012-04-30 14:56:33 +03004053 return 0;
4054}
4055
4056int dwc3_gadget_resume(struct dwc3 *dwc)
4057{
Felipe Balbi7415f172012-04-30 14:56:33 +03004058 int ret;
4059
Roger Quadros9772b472016-04-12 11:33:29 +03004060 if (!dwc->gadget_driver)
4061 return 0;
4062
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004063 ret = __dwc3_gadget_start(dwc);
4064 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004065 goto err0;
4066
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004067 ret = dwc3_gadget_run_stop(dwc, true, false);
4068 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004069 goto err1;
4070
Felipe Balbi7415f172012-04-30 14:56:33 +03004071 return 0;
4072
4073err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004074 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004075
4076err0:
4077 return ret;
4078}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004079
4080void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4081{
4082 if (dwc->pending_events) {
4083 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4084 dwc->pending_events = false;
4085 enable_irq(dwc->irq_gadget);
4086 }
4087}