blob: a0555252dee0e96b7f80bac9169bdfc0ae28980a [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>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbid5370102018-08-14 10:42:43 +030030#define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
Felipe Balbif62afb42018-04-11 10:34:34 +030031 & ~((d)->interval - 1))
32
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020033/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030034 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020035 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030038 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 */
41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48 switch (mode) {
49 case TEST_J:
50 case TEST_K:
51 case TEST_SE0_NAK:
52 case TEST_PACKET:
53 case TEST_FORCE_EN:
54 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
Thinh Nguyen5b738212019-10-23 19:15:43 -070060 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020061
62 return 0;
63}
64
Felipe Balbi8598bde2012-01-02 18:55:57 +020065/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030066 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030067 * @dwc: pointer to our context structure
68 *
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
71 */
72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78 return DWC3_DSTS_USBLNKST(reg);
79}
80
81/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030082 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020083 * @dwc: pointer to our context structure
84 * @state: the state to put link into
85 *
86 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080087 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 */
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080091 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 u32 reg;
93
Paul Zimmerman802fde92012-04-27 13:10:52 +030094 /*
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
97 */
98 if (dwc->revision >= DWC3_REVISION_194A) {
99 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
Felipe Balbi8598bde2012-01-02 18:55:57 +0200111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
Thinh Nguyen2e708fa2019-10-23 19:15:55 -0700114 /* set no action before sending new link state change */
115 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 /* set requested state */
118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120
Paul Zimmerman802fde92012-04-27 13:10:52 +0300121 /*
122 * The following code is racy when called from dwc3_gadget_wakeup,
123 * and is not needed, at least on newer versions
124 */
125 if (dwc->revision >= DWC3_REVISION_194A)
126 return 0;
127
Felipe Balbi8598bde2012-01-02 18:55:57 +0200128 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300129 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 while (--retries) {
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 if (DWC3_DSTS_USBLNKST(reg) == state)
134 return 0;
135
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800136 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 }
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 return -ETIMEDOUT;
140}
141
John Youndca01192016-05-19 17:26:05 -0700142/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300143 * dwc3_ep_inc_trb - increment a trb index.
144 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700145 *
146 * The index should never point to the link TRB. After incrementing,
147 * if it is point to the link TRB, wrap around to the beginning. The
148 * link TRB is always at the last TRB entry.
149 */
150static void dwc3_ep_inc_trb(u8 *index)
151{
152 (*index)++;
153 if (*index == (DWC3_TRB_NUM - 1))
154 *index = 0;
155}
156
Felipe Balbibfad65e2017-04-19 14:59:27 +0300157/**
158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159 * @dep: The endpoint whose enqueue pointer we're incrementing
160 */
Felipe Balbief966b92016-04-05 13:09:51 +0300161static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162{
John Youndca01192016-05-19 17:26:05 -0700163 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300164}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200165
Felipe Balbibfad65e2017-04-19 14:59:27 +0300166/**
167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168 * @dep: The endpoint whose enqueue pointer we're incrementing
169 */
Felipe Balbief966b92016-04-05 13:09:51 +0300170static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171{
John Youndca01192016-05-19 17:26:05 -0700172 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200173}
174
Wei Yongjun69102512018-03-29 02:20:10 +0000175static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300176 struct dwc3_request *req, int status)
177{
178 struct dwc3 *dwc = dep->dwc;
179
Felipe Balbic91815b2018-03-26 13:14:47 +0300180 list_del(&req->list);
181 req->remaining = 0;
Jack Phambd6742242019-01-10 12:39:55 -0800182 req->needs_extra_trb = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300183
184 if (req->request.status == -EINPROGRESS)
185 req->request.status = status;
186
187 if (req->trb)
188 usb_gadget_unmap_request_by_dev(dwc->sysdev,
189 &req->request, req->direction);
190
191 req->trb = NULL;
192 trace_dwc3_gadget_giveback(req);
193
194 if (dep->number > 1)
195 pm_runtime_put(dwc->dev);
196}
197
Felipe Balbibfad65e2017-04-19 14:59:27 +0300198/**
199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
200 * @dep: The endpoint to whom the request belongs to
201 * @req: The request we're giving back
202 * @status: completion code for the request
203 *
204 * Must be called with controller's lock held and interrupts disabled. This
205 * function will unmap @req and call its ->complete() callback to notify upper
206 * layers that it has completed.
207 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300208void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
209 int status)
210{
211 struct dwc3 *dwc = dep->dwc;
212
Felipe Balbic91815b2018-03-26 13:14:47 +0300213 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbia3af5e32019-01-11 12:57:09 +0200214 req->status = DWC3_REQUEST_STATUS_COMPLETED;
Felipe Balbi72246da2011-08-19 18:10:58 +0300215
216 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200217 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 spin_lock(&dwc->lock);
219}
220
Felipe Balbibfad65e2017-04-19 14:59:27 +0300221/**
222 * dwc3_send_gadget_generic_command - issue a generic command for the controller
223 * @dwc: pointer to the controller context
224 * @cmd: the command to be issued
225 * @param: command parameter
226 *
227 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
228 * and wait for its completion.
229 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500230int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300231{
232 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300233 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300234 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300235 u32 reg;
236
237 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
238 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
239
240 do {
241 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
242 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300243 status = DWC3_DGCMD_STATUS(reg);
244 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300245 ret = -EINVAL;
246 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300247 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100248 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300249
250 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300251 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300252 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300253 }
254
Felipe Balbi71f7e702016-05-23 14:16:19 +0300255 trace_dwc3_gadget_generic_cmd(cmd, param, status);
256
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300257 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300258}
259
Felipe Balbic36d8e92016-04-04 12:46:33 +0300260static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
261
Felipe Balbibfad65e2017-04-19 14:59:27 +0300262/**
263 * dwc3_send_gadget_ep_cmd - issue an endpoint command
264 * @dep: the endpoint to which the command is going to be issued
265 * @cmd: the command to be issued
266 * @params: parameters to the command
267 *
268 * Caller should handle locking. This function will issue @cmd with given
269 * @params to @dep and wait for its completion.
270 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300271int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
272 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300273{
Felipe Balbi8897a762016-09-22 10:56:08 +0300274 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300275 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000276 u32 timeout = 1000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700277 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300278 u32 reg;
279
Felipe Balbi0933df12016-05-23 14:02:33 +0300280 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300281 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300282
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300283 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700284 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
285 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
286 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300287 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700288 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
289 * settings. Restore them after the command is completed.
290 *
291 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300292 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300293 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
294 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
295 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700296 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300297 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300298 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700299
300 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
301 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
302 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
303 }
304
305 if (saved_config)
306 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300307 }
308
Felipe Balbi59999142016-09-22 12:25:28 +0300309 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300310 int needs_wakeup;
311
312 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
313 dwc->link_state == DWC3_LINK_STATE_U2 ||
314 dwc->link_state == DWC3_LINK_STATE_U3);
315
316 if (unlikely(needs_wakeup)) {
317 ret = __dwc3_gadget_wakeup(dwc);
318 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
319 ret);
320 }
321 }
322
Felipe Balbi2eb88012016-04-12 16:53:39 +0300323 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
324 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
325 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326
Felipe Balbi8897a762016-09-22 10:56:08 +0300327 /*
328 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
329 * not relying on XferNotReady, we can make use of a special "No
330 * Response Update Transfer" command where we should clear both CmdAct
331 * and CmdIOC bits.
332 *
333 * With this, we don't need to wait for command completion and can
334 * straight away issue further commands to the endpoint.
335 *
336 * NOTICE: We're making an assumption that control endpoints will never
337 * make use of Update Transfer command. This is a safe assumption
338 * because we can never have more than one request at a time with
339 * Control Endpoints. If anybody changes that assumption, this chunk
340 * needs to be updated accordingly.
341 */
342 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
343 !usb_endpoint_xfer_isoc(desc))
344 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
345 else
346 cmd |= DWC3_DEPCMD_CMDACT;
347
348 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300349 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300350 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300351 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300352 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000353
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000354 switch (cmd_status) {
355 case 0:
356 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300357 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000358 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700359 dev_WARN(dwc->dev, "No resource for %s\n",
360 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000361 ret = -EINVAL;
362 break;
363 case DEPEVT_TRANSFER_BUS_EXPIRY:
364 /*
365 * SW issues START TRANSFER command to
366 * isochronous ep with future frame interval. If
367 * future interval time has already passed when
368 * core receives the command, it will respond
369 * with an error status of 'Bus Expiry'.
370 *
371 * Instead of always returning -EINVAL, let's
372 * give a hint to the gadget driver that this is
373 * the case by returning -EAGAIN.
374 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000375 ret = -EAGAIN;
376 break;
377 default:
378 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
379 }
380
Felipe Balbic0ca3242016-04-04 09:11:51 +0300381 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300383 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300384
Felipe Balbif6bb2252016-05-23 13:53:34 +0300385 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300386 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300387 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300388 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300389
Felipe Balbi0933df12016-05-23 14:02:33 +0300390 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
391
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700392 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
393 if (ret == 0)
394 dep->flags |= DWC3_EP_TRANSFER_STARTED;
395
396 if (ret != -ETIMEDOUT)
397 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300398 }
399
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700400 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300401 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700402 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300403 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
404 }
405
Felipe Balbic0ca3242016-04-04 09:11:51 +0300406 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407}
408
John Youn50c763f2016-05-31 17:49:56 -0700409static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
410{
411 struct dwc3 *dwc = dep->dwc;
412 struct dwc3_gadget_ep_cmd_params params;
413 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
414
415 /*
416 * As of core revision 2.60a the recommended programming model
417 * is to set the ClearPendIN bit when issuing a Clear Stall EP
418 * command for IN endpoints. This is to prevent an issue where
419 * some (non-compliant) hosts may not send ACK TPs for pending
420 * IN transfers due to a mishandled error condition. Synopsys
421 * STAR 9000614252.
422 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800423 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
424 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700425 cmd |= DWC3_DEPCMD_CLEARPENDIN;
426
427 memset(&params, 0, sizeof(params));
428
Felipe Balbi2cd47182016-04-12 16:42:43 +0300429 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700430}
431
Felipe Balbi72246da2011-08-19 18:10:58 +0300432static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200433 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300434{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300435 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300436
437 return dep->trb_pool_dma + offset;
438}
439
440static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
441{
442 struct dwc3 *dwc = dep->dwc;
443
444 if (dep->trb_pool)
445 return 0;
446
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530447 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300448 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
449 &dep->trb_pool_dma, GFP_KERNEL);
450 if (!dep->trb_pool) {
451 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
452 dep->name);
453 return -ENOMEM;
454 }
455
456 return 0;
457}
458
459static void dwc3_free_trb_pool(struct dwc3_ep *dep)
460{
461 struct dwc3 *dwc = dep->dwc;
462
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530463 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300464 dep->trb_pool, dep->trb_pool_dma);
465
466 dep->trb_pool = NULL;
467 dep->trb_pool_dma = 0;
468}
469
Felipe Balbi20d1d432018-04-09 12:49:02 +0300470static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
471{
472 struct dwc3_gadget_ep_cmd_params params;
473
474 memset(&params, 0x00, sizeof(params));
475
476 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
477
478 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
479 &params);
480}
John Younc4509602016-02-16 20:10:53 -0800481
482/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300483 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800484 * @dep: endpoint that is being enabled
485 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300486 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
487 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800488 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300489 * The assignment of transfer resources cannot perfectly follow the data book
490 * due to the fact that the controller driver does not have all knowledge of the
491 * configuration in advance. It is given this information piecemeal by the
492 * composite gadget framework after every SET_CONFIGURATION and
493 * SET_INTERFACE. Trying to follow the databook programming model in this
494 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800495 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300496 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
497 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
498 * incorrect in the scenario of multiple interfaces.
499 *
500 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800501 * endpoint on alt setting (8.1.6).
502 *
503 * The following simplified method is used instead:
504 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300505 * All hardware endpoints can be assigned a transfer resource and this setting
506 * will stay persistent until either a core reset or hibernation. So whenever we
507 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
508 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800509 * guaranteed that there are as many transfer resources as endpoints.
510 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300511 * This function is called for each endpoint when it is being enabled but is
512 * triggered only when called for EP0-out, which always happens first, and which
513 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800514 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300515static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300516{
517 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300518 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300519 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800520 int i;
521 int ret;
522
523 if (dep->number)
524 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525
526 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800527 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300528 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300529
Felipe Balbi2cd47182016-04-12 16:42:43 +0300530 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800531 if (ret)
532 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
John Younc4509602016-02-16 20:10:53 -0800534 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
535 struct dwc3_ep *dep = dwc->eps[i];
536
537 if (!dep)
538 continue;
539
Felipe Balbib07c2db2018-04-09 12:46:47 +0300540 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800541 if (ret)
542 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 }
544
545 return 0;
546}
547
Felipe Balbib07c2db2018-04-09 12:46:47 +0300548static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300549{
John Youn39ebb052016-11-09 16:36:28 -0800550 const struct usb_ss_ep_comp_descriptor *comp_desc;
551 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300552 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300553 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
John Youn39ebb052016-11-09 16:36:28 -0800555 comp_desc = dep->endpoint.comp_desc;
556 desc = dep->endpoint.desc;
557
Felipe Balbi72246da2011-08-19 18:10:58 +0300558 memset(&params, 0x00, sizeof(params));
559
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300560 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900561 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
562
563 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800564 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300565 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300566 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900567 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300568
Felipe Balbia2d23f02018-04-09 12:40:48 +0300569 params.param0 |= action;
570 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600571 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600572
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300573 if (usb_endpoint_xfer_control(desc))
574 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300575
576 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
577 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300578
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200579 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300580 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
581 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300582 dep->stream_capable = true;
583 }
584
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500585 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300586 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
588 /*
589 * We are doing 1:1 mapping for endpoints, meaning
590 * Physical Endpoints 2 maps to Logical Endpoint 2 and
591 * so on. We consider the direction bit as part of the physical
592 * endpoint number. So USB endpoint 0x81 is 0x03.
593 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300594 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300595
596 /*
597 * We must use the lower 16 TX FIFOs even though
598 * HW might have more
599 */
600 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300601 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300602
603 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300604 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300605 dep->interval = 1 << (desc->bInterval - 1);
606 }
607
Felipe Balbi2cd47182016-04-12 16:42:43 +0300608 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609}
610
Felipe Balbi72246da2011-08-19 18:10:58 +0300611/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300612 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300614 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300615 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300616 * Caller should take care of locking. Execute all necessary commands to
617 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300619static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300620{
John Youn39ebb052016-11-09 16:36:28 -0800621 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800623
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300625 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300628 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 if (ret)
630 return ret;
631 }
632
Felipe Balbib07c2db2018-04-09 12:46:47 +0300633 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 if (ret)
635 return ret;
636
637 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200638 struct dwc3_trb *trb_st_hw;
639 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300640
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 dep->type = usb_endpoint_type(desc);
642 dep->flags |= DWC3_EP_ENABLED;
643
644 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
645 reg |= DWC3_DALEPENA_EP(dep->number);
646 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
647
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300648 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200649 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300650
John Youn0d257442016-05-19 17:26:08 -0700651 /* Initialize the TRB ring */
652 dep->trb_dequeue = 0;
653 dep->trb_enqueue = 0;
654 memset(dep->trb_pool, 0,
655 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
656
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300657 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 trb_st_hw = &dep->trb_pool[0];
659
Felipe Balbif6bafc62012-02-06 11:04:53 +0200660 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200661 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
662 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
663 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
664 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 }
666
Felipe Balbia97ea992016-09-29 16:28:56 +0300667 /*
668 * Issue StartTransfer here with no-op TRB so we can always rely on No
669 * Response Update Transfer command.
670 */
Anurag Kumar Vulisha26d62b42018-12-01 16:43:27 +0530671 if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300672 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300673 struct dwc3_gadget_ep_cmd_params params;
674 struct dwc3_trb *trb;
675 dma_addr_t trb_dma;
676 u32 cmd;
677
678 memset(&params, 0, sizeof(params));
679 trb = &dep->trb_pool[0];
680 trb_dma = dwc3_trb_dma_offset(dep, trb);
681
682 params.param0 = upper_32_bits(trb_dma);
683 params.param1 = lower_32_bits(trb_dma);
684
685 cmd = DWC3_DEPCMD_STARTTRANSFER;
686
687 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
688 if (ret < 0)
689 return ret;
Felipe Balbia97ea992016-09-29 16:28:56 +0300690 }
691
Felipe Balbi2870e502016-11-03 13:53:29 +0200692out:
693 trace_dwc3_gadget_ep_enable(dep);
694
Felipe Balbi72246da2011-08-19 18:10:58 +0300695 return 0;
696}
697
Felipe Balbic5353b22019-02-13 13:00:54 +0200698static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
699 bool interrupt);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200700static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300701{
702 struct dwc3_request *req;
703
Felipe Balbic5353b22019-02-13 13:00:54 +0200704 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300705
Felipe Balbi0e146022016-06-21 10:32:02 +0300706 /* - giveback all requests to gadget driver */
707 while (!list_empty(&dep->started_list)) {
708 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200709
Felipe Balbi0e146022016-06-21 10:32:02 +0300710 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200711 }
712
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200713 while (!list_empty(&dep->pending_list)) {
714 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200716 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300717 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200718
719 while (!list_empty(&dep->cancelled_list)) {
720 req = next_request(&dep->cancelled_list);
721
722 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
723 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300724}
725
726/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300727 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 * @dep: the endpoint to disable
729 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300730 * This function undoes what __dwc3_gadget_ep_enable did and also removes
731 * requests which are currently being processed by the hardware and those which
732 * are not yet scheduled.
733 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200734 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300735 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300736static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
737{
738 struct dwc3 *dwc = dep->dwc;
739 u32 reg;
740
Felipe Balbi2870e502016-11-03 13:53:29 +0200741 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500742
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
Felipe Balbi687ef982014-04-16 10:30:33 -0500745 /* make sure HW endpoint isn't stalled */
746 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500747 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500748
Felipe Balbi72246da2011-08-19 18:10:58 +0300749 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
750 reg &= ~DWC3_DALEPENA_EP(dep->number);
751 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
752
Felipe Balbi879631a2011-09-30 10:58:47 +0300753 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200755 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300756
John Youn39ebb052016-11-09 16:36:28 -0800757 /* Clear out the ep descriptors for non-ep0 */
758 if (dep->number > 1) {
759 dep->endpoint.comp_desc = NULL;
760 dep->endpoint.desc = NULL;
761 }
762
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 return 0;
764}
765
766/* -------------------------------------------------------------------------- */
767
768static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
769 const struct usb_endpoint_descriptor *desc)
770{
771 return -EINVAL;
772}
773
774static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
775{
776 return -EINVAL;
777}
778
779/* -------------------------------------------------------------------------- */
780
781static int dwc3_gadget_ep_enable(struct usb_ep *ep,
782 const struct usb_endpoint_descriptor *desc)
783{
784 struct dwc3_ep *dep;
785 struct dwc3 *dwc;
786 unsigned long flags;
787 int ret;
788
789 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
790 pr_debug("dwc3: invalid parameters\n");
791 return -EINVAL;
792 }
793
794 if (!desc->wMaxPacketSize) {
795 pr_debug("dwc3: missing wMaxPacketSize\n");
796 return -EINVAL;
797 }
798
799 dep = to_dwc3_ep(ep);
800 dwc = dep->dwc;
801
Felipe Balbi95ca9612015-12-10 13:08:20 -0600802 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
803 "%s is already enabled\n",
804 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300805 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300808 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 spin_unlock_irqrestore(&dwc->lock, flags);
810
811 return ret;
812}
813
814static int dwc3_gadget_ep_disable(struct usb_ep *ep)
815{
816 struct dwc3_ep *dep;
817 struct dwc3 *dwc;
818 unsigned long flags;
819 int ret;
820
821 if (!ep) {
822 pr_debug("dwc3: invalid parameters\n");
823 return -EINVAL;
824 }
825
826 dep = to_dwc3_ep(ep);
827 dwc = dep->dwc;
828
Felipe Balbi95ca9612015-12-10 13:08:20 -0600829 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
830 "%s is already disabled\n",
831 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300832 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
Felipe Balbi72246da2011-08-19 18:10:58 +0300834 spin_lock_irqsave(&dwc->lock, flags);
835 ret = __dwc3_gadget_ep_disable(dep);
836 spin_unlock_irqrestore(&dwc->lock, flags);
837
838 return ret;
839}
840
841static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300842 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300843{
844 struct dwc3_request *req;
845 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300846
847 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900848 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300850
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300851 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 req->epnum = dep->number;
853 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200854 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300855
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500856 trace_dwc3_alloc_request(req);
857
Felipe Balbi72246da2011-08-19 18:10:58 +0300858 return &req->request;
859}
860
861static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
862 struct usb_request *request)
863{
864 struct dwc3_request *req = to_dwc3_request(request);
865
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500866 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 kfree(req);
868}
869
Felipe Balbi42626912018-04-09 13:01:43 +0300870/**
871 * dwc3_ep_prev_trb - returns the previous TRB in the ring
872 * @dep: The endpoint with the TRB ring
873 * @index: The index of the current TRB in the ring
874 *
875 * Returns the TRB prior to the one pointed to by the index. If the
876 * index is 0, we will wrap backwards, skip the link TRB, and return
877 * the one just before that.
878 */
879static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
880{
881 u8 tmp = index;
882
883 if (!tmp)
884 tmp = DWC3_TRB_NUM - 1;
885
886 return &dep->trb_pool[tmp - 1];
887}
888
889static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
890{
891 struct dwc3_trb *tmp;
892 u8 trbs_left;
893
894 /*
895 * If enqueue & dequeue are equal than it is either full or empty.
896 *
897 * One way to know for sure is if the TRB right before us has HWO bit
898 * set or not. If it has, then we're definitely full and can't fit any
899 * more transfers in our ring.
900 */
901 if (dep->trb_enqueue == dep->trb_dequeue) {
902 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
903 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
904 return 0;
905
906 return DWC3_TRB_NUM - 1;
907 }
908
909 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
910 trbs_left &= (DWC3_TRB_NUM - 1);
911
912 if (dep->trb_dequeue < dep->trb_enqueue)
913 trbs_left--;
914
915 return trbs_left;
916}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300917
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200918static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
919 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
920 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200921{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300922 struct dwc3 *dwc = dep->dwc;
923 struct usb_gadget *gadget = &dwc->gadget;
924 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200925
Felipe Balbif6bafc62012-02-06 11:04:53 +0200926 trb->size = DWC3_TRB_SIZE_LENGTH(length);
927 trb->bpl = lower_32_bits(dma);
928 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200929
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200930 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200931 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200932 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200933 break;
934
935 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300936 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530937 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300938
Manu Gautam40d829f2017-07-19 17:07:10 +0530939 /*
940 * USB Specification 2.0 Section 5.9.2 states that: "If
941 * there is only a single transaction in the microframe,
942 * only a DATA0 data packet PID is used. If there are
943 * two transactions per microframe, DATA1 is used for
944 * the first transaction data packet and DATA0 is used
945 * for the second transaction data packet. If there are
946 * three transactions per microframe, DATA2 is used for
947 * the first transaction data packet, DATA1 is used for
948 * the second, and DATA0 is used for the third."
949 *
950 * IOW, we should satisfy the following cases:
951 *
952 * 1) length <= maxpacket
953 * - DATA0
954 *
955 * 2) maxpacket < length <= (2 * maxpacket)
956 * - DATA1, DATA0
957 *
958 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
959 * - DATA2, DATA1, DATA0
960 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300961 if (speed == USB_SPEED_HIGH) {
962 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530963 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530964 unsigned int maxp = usb_endpoint_maxp(ep->desc);
965
966 if (length <= (2 * maxp))
967 mult--;
968
969 if (length <= maxp)
970 mult--;
971
972 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300973 }
974 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530975 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300976 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200977
978 /* always enable Interrupt on Missed ISOC */
979 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200980 break;
981
982 case USB_ENDPOINT_XFER_BULK:
983 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200984 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200985 break;
986 default:
987 /*
988 * This is only possible with faulty memory because we
989 * checked it already :)
990 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300991 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
992 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200993 }
994
Tejas Joglekar244add82018-12-10 16:08:13 +0530995 /*
996 * Enable Continue on Short Packet
997 * when endpoint is not a stream capable
998 */
Felipe Balbic9508c82016-10-05 14:26:23 +0300999 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301000 if (!dep->stream_capable)
1001 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001002
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001003 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001004 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1005 }
1006
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001007 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301008 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001009 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001010
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301011 if (chain)
1012 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1013
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001014 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001015 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001016
1017 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001018
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301019 dwc3_ep_inc_enq(dep);
1020
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001021 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001022}
1023
John Youn361572b2016-05-19 17:26:17 -07001024/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001025 * dwc3_prepare_one_trb - setup one TRB from one request
1026 * @dep: endpoint for which this request is prepared
1027 * @req: dwc3_request pointer
1028 * @chain: should this TRB be chained to the next?
1029 * @node: only for isochronous endpoints. First TRB needs different type.
1030 */
1031static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1032 struct dwc3_request *req, unsigned chain, unsigned node)
1033{
1034 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301035 unsigned int length;
1036 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001037 unsigned stream_id = req->request.stream_id;
1038 unsigned short_not_ok = req->request.short_not_ok;
1039 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301040
1041 if (req->request.num_sgs > 0) {
1042 length = sg_dma_len(req->start_sg);
1043 dma = sg_dma_address(req->start_sg);
1044 } else {
1045 length = req->request.length;
1046 dma = req->request.dma;
1047 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001048
1049 trb = &dep->trb_pool[dep->trb_enqueue];
1050
1051 if (!req->trb) {
1052 dwc3_gadget_move_started_request(req);
1053 req->trb = trb;
1054 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001055 }
1056
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001057 req->num_trbs++;
1058
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001059 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1060 stream_id, short_not_ok, no_interrupt);
1061}
1062
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001063static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001064 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001065{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301066 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001067 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001068 int i;
1069
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301070 unsigned int remaining = req->request.num_mapped_sgs
1071 - req->num_queued_sgs;
1072
1073 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001074 unsigned int length = req->request.length;
1075 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1076 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001077 unsigned chain = true;
1078
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001079 /*
1080 * IOMMU driver is coalescing the list of sgs which shares a
1081 * page boundary into one and giving it to USB driver. With
1082 * this the number of sgs mapped is not equal to the number of
1083 * sgs passed. So mark the chain bit to false if it isthe last
1084 * mapped sg.
1085 */
1086 if (i == remaining - 1)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001087 chain = false;
1088
Felipe Balbic6267a52017-01-05 14:58:46 +02001089 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1090 struct dwc3 *dwc = dep->dwc;
1091 struct dwc3_trb *trb;
1092
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001093 req->needs_extra_trb = true;
Felipe Balbic6267a52017-01-05 14:58:46 +02001094
1095 /* prepare normal TRB */
1096 dwc3_prepare_one_trb(dep, req, true, i);
1097
1098 /* Now prepare one extra TRB to align transfer size */
1099 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001100 req->num_trbs++;
Felipe Balbic6267a52017-01-05 14:58:46 +02001101 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001102 maxp - rem, false, 1,
Felipe Balbic6267a52017-01-05 14:58:46 +02001103 req->request.stream_id,
1104 req->request.short_not_ok,
1105 req->request.no_interrupt);
1106 } else {
1107 dwc3_prepare_one_trb(dep, req, chain, i);
1108 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001109
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301110 /*
1111 * There can be a situation where all sgs in sglist are not
1112 * queued because of insufficient trb number. To handle this
1113 * case, update start_sg to next sg to be queued, so that
1114 * we have free trbs we can continue queuing from where we
1115 * previously stopped
1116 */
1117 if (chain)
1118 req->start_sg = sg_next(s);
1119
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301120 req->num_queued_sgs++;
1121
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001122 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001123 break;
1124 }
1125}
1126
1127static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001128 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001129{
Felipe Balbic6267a52017-01-05 14:58:46 +02001130 unsigned int length = req->request.length;
1131 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1132 unsigned int rem = length % maxp;
1133
Tejas Joglekar1e19cdc2019-01-22 13:26:51 +05301134 if ((!length || rem) && usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001135 struct dwc3 *dwc = dep->dwc;
1136 struct dwc3_trb *trb;
1137
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001138 req->needs_extra_trb = true;
Felipe Balbic6267a52017-01-05 14:58:46 +02001139
1140 /* prepare normal TRB */
1141 dwc3_prepare_one_trb(dep, req, true, 0);
1142
1143 /* Now prepare one extra TRB to align transfer size */
1144 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001145 req->num_trbs++;
Felipe Balbic6267a52017-01-05 14:58:46 +02001146 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001147 false, 1, req->request.stream_id,
Felipe Balbic6267a52017-01-05 14:58:46 +02001148 req->request.short_not_ok,
1149 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001150 } else if (req->request.zero && req->request.length &&
Thinh Nguyen4ea438d2018-07-27 18:52:41 -07001151 (IS_ALIGNED(req->request.length, maxp))) {
Felipe Balbid6e5a542017-04-07 16:34:38 +03001152 struct dwc3 *dwc = dep->dwc;
1153 struct dwc3_trb *trb;
1154
Felipe Balbi1a22ec62018-08-01 13:15:05 +03001155 req->needs_extra_trb = true;
Felipe Balbid6e5a542017-04-07 16:34:38 +03001156
1157 /* prepare normal TRB */
1158 dwc3_prepare_one_trb(dep, req, true, 0);
1159
1160 /* Now prepare one extra TRB to handle ZLP */
1161 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001162 req->num_trbs++;
Felipe Balbid6e5a542017-04-07 16:34:38 +03001163 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
Felipe Balbi2fc6d4b2018-08-01 09:37:34 +03001164 false, 1, req->request.stream_id,
Felipe Balbid6e5a542017-04-07 16:34:38 +03001165 req->request.short_not_ok,
1166 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001167 } else {
1168 dwc3_prepare_one_trb(dep, req, false, 0);
1169 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001170}
1171
Felipe Balbi72246da2011-08-19 18:10:58 +03001172/*
1173 * dwc3_prepare_trbs - setup TRBs from requests
1174 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001175 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001176 * The function goes through the requests list and sets up TRBs for the
1177 * transfers. The function returns once there are no more TRBs available or
1178 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001179 */
Felipe Balbic4233572016-05-12 14:08:34 +03001180static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001181{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001182 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001183
1184 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1185
Felipe Balbid86c5a62016-10-25 13:48:52 +03001186 /*
1187 * We can get in a situation where there's a request in the started list
1188 * but there weren't enough TRBs to fully kick it in the first time
1189 * around, so it has been waiting for more TRBs to be freed up.
1190 *
1191 * In that case, we should check if we have a request with pending_sgs
1192 * in the started list and prepare TRBs for that request first,
1193 * otherwise we will prepare TRBs completely out of order and that will
1194 * break things.
1195 */
1196 list_for_each_entry(req, &dep->started_list, list) {
1197 if (req->num_pending_sgs > 0)
1198 dwc3_prepare_one_trb_sg(dep, req);
1199
1200 if (!dwc3_calc_trbs_left(dep))
1201 return;
1202 }
1203
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001204 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001205 struct dwc3 *dwc = dep->dwc;
1206 int ret;
1207
1208 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1209 dep->direction);
1210 if (ret)
1211 return;
1212
1213 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301214 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301215 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001216 req->num_pending_sgs = req->request.num_mapped_sgs;
1217
Felipe Balbi1f512112016-08-12 13:17:27 +03001218 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001219 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001220 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001221 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001222
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001223 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001224 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001225 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001226}
1227
Thinh Nguyen8d990872020-03-29 16:12:57 -07001228static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1229
Felipe Balbi7fdca762017-09-05 14:41:34 +03001230static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001231{
1232 struct dwc3_gadget_ep_cmd_params params;
1233 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001234 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001235 int ret;
1236 u32 cmd;
1237
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001238 if (!dwc3_calc_trbs_left(dep))
1239 return 0;
1240
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001241 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001242
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001243 dwc3_prepare_trbs(dep);
1244 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001245 if (!req) {
1246 dep->flags |= DWC3_EP_PENDING_REQUEST;
1247 return 0;
1248 }
1249
1250 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001251
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001252 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301253 params.param0 = upper_32_bits(req->trb_dma);
1254 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001255 cmd = DWC3_DEPCMD_STARTTRANSFER;
1256
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301257 if (dep->stream_capable)
1258 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1259
Felipe Balbi7fdca762017-09-05 14:41:34 +03001260 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1261 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301262 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001263 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1264 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301265 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001266
Felipe Balbi2cd47182016-04-12 16:42:43 +03001267 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001268 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001269 struct dwc3_request *tmp;
1270
1271 if (ret == -EAGAIN)
1272 return ret;
1273
1274 dwc3_stop_active_transfer(dep, true, true);
1275
1276 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1277 dwc3_gadget_move_cancelled_request(req);
1278
1279 /* If ep isn't started, then there's no end transfer pending */
1280 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1281 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1282
Felipe Balbi72246da2011-08-19 18:10:58 +03001283 return ret;
1284 }
1285
Felipe Balbi72246da2011-08-19 18:10:58 +03001286 return 0;
1287}
1288
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001289static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1290{
1291 u32 reg;
1292
1293 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1294 return DWC3_DSTS_SOFFN(reg);
1295}
1296
Thinh Nguyend92021f2018-11-14 22:56:54 -08001297/**
1298 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1299 * @dep: isoc endpoint
1300 *
1301 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1302 * microframe number reported by the XferNotReady event for the future frame
1303 * number to start the isoc transfer.
1304 *
1305 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1306 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1307 * XferNotReady event are invalid. The driver uses this number to schedule the
1308 * isochronous transfer and passes it to the START TRANSFER command. Because
1309 * this number is invalid, the command may fail. If BIT[15:14] matches the
1310 * internal 16-bit microframe, the START TRANSFER command will pass and the
1311 * transfer will start at the scheduled time, if it is off by 1, the command
1312 * will still pass, but the transfer will start 2 seconds in the future. For all
1313 * other conditions, the START TRANSFER command will fail with bus-expiry.
1314 *
1315 * In order to workaround this issue, we can test for the correct combination of
1316 * BIT[15:14] by sending START TRANSFER commands with different values of
1317 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1318 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1319 * As the result, within the 4 possible combinations for BIT[15:14], there will
1320 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1321 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1322 * value is the correct combination.
1323 *
1324 * Since there are only 4 outcomes and the results are ordered, we can simply
1325 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1326 * deduce the smaller successful combination.
1327 *
1328 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1329 * of BIT[15:14]. The correct combination is as follow:
1330 *
1331 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1332 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1333 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1334 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1335 *
1336 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1337 * endpoints.
1338 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001339static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301340{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001341 int cmd_status = 0;
1342 bool test0;
1343 bool test1;
1344
1345 while (dep->combo_num < 2) {
1346 struct dwc3_gadget_ep_cmd_params params;
1347 u32 test_frame_number;
1348 u32 cmd;
1349
1350 /*
1351 * Check if we can start isoc transfer on the next interval or
1352 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1353 */
1354 test_frame_number = dep->frame_number & 0x3fff;
1355 test_frame_number |= dep->combo_num << 14;
1356 test_frame_number += max_t(u32, 4, dep->interval);
1357
1358 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1359 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1360
1361 cmd = DWC3_DEPCMD_STARTTRANSFER;
1362 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1363 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1364
1365 /* Redo if some other failure beside bus-expiry is received */
1366 if (cmd_status && cmd_status != -EAGAIN) {
1367 dep->start_cmd_status = 0;
1368 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001369 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001370 }
1371
1372 /* Store the first test status */
1373 if (dep->combo_num == 0)
1374 dep->start_cmd_status = cmd_status;
1375
1376 dep->combo_num++;
1377
1378 /*
1379 * End the transfer if the START_TRANSFER command is successful
1380 * to wait for the next XferNotReady to test the command again
1381 */
1382 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001383 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001384 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001385 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301386 }
1387
Thinh Nguyend92021f2018-11-14 22:56:54 -08001388 /* test0 and test1 are both completed at this point */
1389 test0 = (dep->start_cmd_status == 0);
1390 test1 = (cmd_status == 0);
1391
1392 if (!test0 && test1)
1393 dep->combo_num = 1;
1394 else if (!test0 && !test1)
1395 dep->combo_num = 2;
1396 else if (test0 && !test1)
1397 dep->combo_num = 3;
1398 else if (test0 && test1)
1399 dep->combo_num = 0;
1400
1401 dep->frame_number &= 0x3fff;
1402 dep->frame_number |= dep->combo_num << 14;
1403 dep->frame_number += max_t(u32, 4, dep->interval);
1404
1405 /* Reinitialize test variables */
1406 dep->start_cmd_status = 0;
1407 dep->combo_num = 0;
1408
Felipe Balbi25abad62018-08-14 10:41:19 +03001409 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001410}
1411
Felipe Balbi25abad62018-08-14 10:41:19 +03001412static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301413{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001414 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001415 int ret;
1416 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001417
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001418 if (list_empty(&dep->pending_list) &&
1419 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301420 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001421 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301422 }
1423
Thinh Nguyend92021f2018-11-14 22:56:54 -08001424 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1425 (dwc->revision <= DWC3_USB31_REVISION_160A ||
1426 (dwc->revision == DWC3_USB31_REVISION_170A &&
1427 dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1428 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1429
Felipe Balbi25abad62018-08-14 10:41:19 +03001430 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1431 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001432 }
1433
Felipe Balbid5370102018-08-14 10:42:43 +03001434 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1435 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1436
1437 ret = __dwc3_gadget_kick_transfer(dep);
1438 if (ret != -EAGAIN)
1439 break;
1440 }
1441
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001442 /*
1443 * After a number of unsuccessful start attempts due to bus-expiry
1444 * status, issue END_TRANSFER command and retry on the next XferNotReady
1445 * event.
1446 */
1447 if (ret == -EAGAIN) {
1448 struct dwc3_gadget_ep_cmd_params params;
1449 u32 cmd;
1450
1451 cmd = DWC3_DEPCMD_ENDTRANSFER |
1452 DWC3_DEPCMD_CMDIOC |
1453 DWC3_DEPCMD_PARAM(dep->resource_index);
1454
1455 dep->resource_index = 0;
1456 memset(&params, 0, sizeof(params));
1457
1458 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1459 if (!ret)
1460 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1461 }
1462
Felipe Balbid5370102018-08-14 10:42:43 +03001463 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301464}
1465
Felipe Balbi72246da2011-08-19 18:10:58 +03001466static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1467{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001468 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001469
Felipe Balbibb423982015-11-16 15:31:21 -06001470 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001471 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1472 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001473 return -ESHUTDOWN;
1474 }
1475
Felipe Balbi04fb3652017-05-17 15:57:45 +03001476 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1477 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001478 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001479
Felipe Balbib2b6d602019-01-11 12:58:52 +02001480 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1481 "%s: request %pK already in flight\n",
1482 dep->name, &req->request))
1483 return -EINVAL;
1484
Felipe Balbifc8bb912016-05-16 13:14:48 +03001485 pm_runtime_get(dwc->dev);
1486
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 req->request.actual = 0;
1488 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001489
Felipe Balbife84f522015-09-01 09:01:38 -05001490 trace_dwc3_ep_queue(req);
1491
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001492 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001493 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001494
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001495 /* Start the transfer only after the END_TRANSFER is completed */
1496 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1497 dep->flags |= DWC3_EP_DELAY_START;
1498 return 0;
1499 }
1500
Felipe Balbid889c232016-09-29 15:44:29 +03001501 /*
1502 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1503 * wait for a XferNotReady event so we will know what's the current
1504 * (micro-)frame number.
1505 *
1506 * Without this trick, we are very, very likely gonna get Bus Expiry
1507 * errors which will force us issue EndTransfer command.
1508 */
1509 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001510 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1511 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001512 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001513
1514 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1515 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
Felipe Balbi25abad62018-08-14 10:41:19 +03001516 return __dwc3_gadget_start_isoc(dep);
Felipe Balbife990ce2018-03-29 13:23:53 +03001517 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001518 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001519 }
1520
Felipe Balbi7fdca762017-09-05 14:41:34 +03001521 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001522}
1523
1524static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1525 gfp_t gfp_flags)
1526{
1527 struct dwc3_request *req = to_dwc3_request(request);
1528 struct dwc3_ep *dep = to_dwc3_ep(ep);
1529 struct dwc3 *dwc = dep->dwc;
1530
1531 unsigned long flags;
1532
1533 int ret;
1534
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001535 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 ret = __dwc3_gadget_ep_queue(dep, req);
1537 spin_unlock_irqrestore(&dwc->lock, flags);
1538
1539 return ret;
1540}
1541
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001542static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1543{
1544 int i;
1545
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001546 /* If req->trb is not set, then the request has not started */
1547 if (!req->trb)
1548 return;
1549
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001550 /*
1551 * If request was already started, this means we had to
1552 * stop the transfer. With that we also need to ignore
1553 * all TRBs used by the request, however TRBs can only
1554 * be modified after completion of END_TRANSFER
1555 * command. So what we do here is that we wait for
1556 * END_TRANSFER completion and only after that, we jump
1557 * over TRBs by clearing HWO and incrementing dequeue
1558 * pointer.
1559 */
1560 for (i = 0; i < req->num_trbs; i++) {
1561 struct dwc3_trb *trb;
1562
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001563 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001564 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1565 dwc3_ep_inc_deq(dep);
1566 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001567
1568 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001569}
1570
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001571static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1572{
1573 struct dwc3_request *req;
1574 struct dwc3_request *tmp;
1575
1576 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1577 dwc3_gadget_ep_skip_trbs(dep, req);
1578 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1579 }
1580}
1581
Felipe Balbi72246da2011-08-19 18:10:58 +03001582static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1583 struct usb_request *request)
1584{
1585 struct dwc3_request *req = to_dwc3_request(request);
1586 struct dwc3_request *r = NULL;
1587
1588 struct dwc3_ep *dep = to_dwc3_ep(ep);
1589 struct dwc3 *dwc = dep->dwc;
1590
1591 unsigned long flags;
1592 int ret = 0;
1593
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001594 trace_dwc3_ep_dequeue(req);
1595
Felipe Balbi72246da2011-08-19 18:10:58 +03001596 spin_lock_irqsave(&dwc->lock, flags);
1597
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001598 list_for_each_entry(r, &dep->cancelled_list, list) {
1599 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001600 goto out;
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001601 }
1602
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001603 list_for_each_entry(r, &dep->pending_list, list) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001604 if (r == req) {
1605 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1606 goto out;
1607 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001608 }
1609
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001610 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001611 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001612 struct dwc3_request *t;
1613
Felipe Balbi72246da2011-08-19 18:10:58 +03001614 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001615 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001616
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001617 /*
1618 * Remove any started request if the transfer is
1619 * cancelled.
1620 */
1621 list_for_each_entry_safe(r, t, &dep->started_list, list)
1622 dwc3_gadget_move_cancelled_request(r);
1623
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001624 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001626 }
1627
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001628 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1629 request, ep->name);
1630 ret = -EINVAL;
1631out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001632 spin_unlock_irqrestore(&dwc->lock, flags);
1633
1634 return ret;
1635}
1636
Felipe Balbi7a608552014-09-24 14:19:52 -05001637int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001638{
1639 struct dwc3_gadget_ep_cmd_params params;
1640 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001641 struct dwc3_request *req;
1642 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001643 int ret;
1644
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001645 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1646 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1647 return -EINVAL;
1648 }
1649
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 memset(&params, 0x00, sizeof(params));
1651
1652 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001653 struct dwc3_trb *trb;
1654
1655 unsigned transfer_in_flight;
1656 unsigned started;
1657
1658 if (dep->number > 1)
1659 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1660 else
1661 trb = &dwc->ep0_trb[dep->trb_enqueue];
1662
1663 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1664 started = !list_empty(&dep->started_list);
1665
1666 if (!protocol && ((dep->direction && transfer_in_flight) ||
1667 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001668 return -EAGAIN;
1669 }
1670
Felipe Balbi2cd47182016-04-12 16:42:43 +03001671 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1672 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001674 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 dep->name);
1676 else
1677 dep->flags |= DWC3_EP_STALL;
1678 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001679 /*
1680 * Don't issue CLEAR_STALL command to control endpoints. The
1681 * controller automatically clears the STALL when it receives
1682 * the SETUP token.
1683 */
1684 if (dep->number <= 1) {
1685 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1686 return 0;
1687 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001688
John Youn50c763f2016-05-31 17:49:56 -07001689 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001690 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001691 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001692 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001693 return ret;
1694 }
1695
1696 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1697
1698 dwc3_stop_active_transfer(dep, true, true);
1699
1700 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1701 dwc3_gadget_move_cancelled_request(req);
1702
1703 list_for_each_entry_safe(req, tmp, &dep->pending_list, list)
1704 dwc3_gadget_move_cancelled_request(req);
1705
1706 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) {
1707 dep->flags &= ~DWC3_EP_DELAY_START;
1708 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1709 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001710 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001711
Felipe Balbi72246da2011-08-19 18:10:58 +03001712 return ret;
1713}
1714
1715static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1716{
1717 struct dwc3_ep *dep = to_dwc3_ep(ep);
1718 struct dwc3 *dwc = dep->dwc;
1719
1720 unsigned long flags;
1721
1722 int ret;
1723
1724 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001725 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001726 spin_unlock_irqrestore(&dwc->lock, flags);
1727
1728 return ret;
1729}
1730
1731static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1732{
1733 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001734 struct dwc3 *dwc = dep->dwc;
1735 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001736 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001737
Paul Zimmerman249a4562012-02-24 17:32:16 -08001738 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001739 dep->flags |= DWC3_EP_WEDGE;
1740
Pratyush Anand08f0d962012-06-25 22:40:43 +05301741 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001742 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301743 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001744 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001745 spin_unlock_irqrestore(&dwc->lock, flags);
1746
1747 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001748}
1749
1750/* -------------------------------------------------------------------------- */
1751
1752static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1753 .bLength = USB_DT_ENDPOINT_SIZE,
1754 .bDescriptorType = USB_DT_ENDPOINT,
1755 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1756};
1757
1758static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1759 .enable = dwc3_gadget_ep0_enable,
1760 .disable = dwc3_gadget_ep0_disable,
1761 .alloc_request = dwc3_gadget_ep_alloc_request,
1762 .free_request = dwc3_gadget_ep_free_request,
1763 .queue = dwc3_gadget_ep0_queue,
1764 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301765 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001766 .set_wedge = dwc3_gadget_ep_set_wedge,
1767};
1768
1769static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1770 .enable = dwc3_gadget_ep_enable,
1771 .disable = dwc3_gadget_ep_disable,
1772 .alloc_request = dwc3_gadget_ep_alloc_request,
1773 .free_request = dwc3_gadget_ep_free_request,
1774 .queue = dwc3_gadget_ep_queue,
1775 .dequeue = dwc3_gadget_ep_dequeue,
1776 .set_halt = dwc3_gadget_ep_set_halt,
1777 .set_wedge = dwc3_gadget_ep_set_wedge,
1778};
1779
1780/* -------------------------------------------------------------------------- */
1781
1782static int dwc3_gadget_get_frame(struct usb_gadget *g)
1783{
1784 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001785
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001786 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001787}
1788
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001789static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001790{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001791 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001792
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001793 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001794 u32 reg;
1795
Felipe Balbi72246da2011-08-19 18:10:58 +03001796 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001797
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 /*
1799 * According to the Databook Remote wakeup request should
1800 * be issued only when the device is in early suspend state.
1801 *
1802 * We can check that via USB Link State bits in DSTS register.
1803 */
1804 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1805
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 link_state = DWC3_DSTS_USBLNKST(reg);
1807
1808 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001809 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001810 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1811 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001812 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001813 break;
1814 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001815 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001816 }
1817
Felipe Balbi8598bde2012-01-02 18:55:57 +02001818 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1819 if (ret < 0) {
1820 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001821 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001822 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001823
Paul Zimmerman802fde92012-04-27 13:10:52 +03001824 /* Recent versions do this automatically */
1825 if (dwc->revision < DWC3_REVISION_194A) {
1826 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001827 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001828 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1829 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1830 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001831
Paul Zimmerman1d046792012-02-15 18:56:56 -08001832 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001833 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001834
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001835 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1837
1838 /* in HS, means ON */
1839 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1840 break;
1841 }
1842
1843 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1844 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001845 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 }
1847
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001848 return 0;
1849}
1850
1851static int dwc3_gadget_wakeup(struct usb_gadget *g)
1852{
1853 struct dwc3 *dwc = gadget_to_dwc(g);
1854 unsigned long flags;
1855 int ret;
1856
1857 spin_lock_irqsave(&dwc->lock, flags);
1858 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 spin_unlock_irqrestore(&dwc->lock, flags);
1860
1861 return ret;
1862}
1863
1864static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1865 int is_selfpowered)
1866{
1867 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001868 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001869
Paul Zimmerman249a4562012-02-24 17:32:16 -08001870 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001871 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001872 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001873
1874 return 0;
1875}
1876
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001877static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001878{
1879 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001880 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001881
Felipe Balbifc8bb912016-05-16 13:14:48 +03001882 if (pm_runtime_suspended(dwc->dev))
1883 return 0;
1884
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001886 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001887 if (dwc->revision <= DWC3_REVISION_187A) {
1888 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1889 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1890 }
1891
1892 if (dwc->revision >= DWC3_REVISION_194A)
1893 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1894 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001895
1896 if (dwc->has_hibernation)
1897 reg |= DWC3_DCTL_KEEP_CONNECT;
1898
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001899 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001900 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001901 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001902
1903 if (dwc->has_hibernation && !suspend)
1904 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1905
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001906 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001907 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001908
Thinh Nguyen5b738212019-10-23 19:15:43 -07001909 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001910
1911 do {
1912 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001913 reg &= DWC3_DSTS_DEVCTRLHLT;
1914 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001915
1916 if (!timeout)
1917 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001918
Pratyush Anand6f17f742012-07-02 10:21:55 +05301919 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001920}
1921
1922static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1923{
1924 struct dwc3 *dwc = gadget_to_dwc(g);
1925 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301926 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001927
1928 is_on = !!is_on;
1929
Baolin Wangbb014732016-10-14 17:11:33 +08001930 /*
1931 * Per databook, when we want to stop the gadget, if a control transfer
1932 * is still in process, complete it and get the core into setup phase.
1933 */
1934 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1935 reinit_completion(&dwc->ep0_in_setup);
1936
1937 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1938 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1939 if (ret == 0) {
1940 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1941 return -ETIMEDOUT;
1942 }
1943 }
1944
Felipe Balbi72246da2011-08-19 18:10:58 +03001945 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001946 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001947 spin_unlock_irqrestore(&dwc->lock, flags);
1948
Pratyush Anand6f17f742012-07-02 10:21:55 +05301949 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001950}
1951
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001952static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1953{
1954 u32 reg;
1955
1956 /* Enable all but Start and End of Frame IRQs */
1957 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1958 DWC3_DEVTEN_EVNTOVERFLOWEN |
1959 DWC3_DEVTEN_CMDCMPLTEN |
1960 DWC3_DEVTEN_ERRTICERREN |
1961 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001962 DWC3_DEVTEN_CONNECTDONEEN |
1963 DWC3_DEVTEN_USBRSTEN |
1964 DWC3_DEVTEN_DISCONNEVTEN);
1965
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001966 if (dwc->revision < DWC3_REVISION_250A)
1967 reg |= DWC3_DEVTEN_ULSTCNGEN;
1968
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001969 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1970}
1971
1972static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1973{
1974 /* mask all interrupts */
1975 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1976}
1977
1978static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001979static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001980
Felipe Balbi4e994722016-05-13 14:09:59 +03001981/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001982 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1983 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001984 *
1985 * The following looks like complex but it's actually very simple. In order to
1986 * calculate the number of packets we can burst at once on OUT transfers, we're
1987 * gonna use RxFIFO size.
1988 *
1989 * To calculate RxFIFO size we need two numbers:
1990 * MDWIDTH = size, in bits, of the internal memory bus
1991 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1992 *
1993 * Given these two numbers, the formula is simple:
1994 *
1995 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1996 *
1997 * 24 bytes is for 3x SETUP packets
1998 * 16 bytes is a clock domain crossing tolerance
1999 *
2000 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2001 */
2002static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2003{
2004 u32 ram2_depth;
2005 u32 mdwidth;
2006 u32 nump;
2007 u32 reg;
2008
2009 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2010 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2011
2012 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2013 nump = min_t(u32, nump, 16);
2014
2015 /* update NumP */
2016 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2017 reg &= ~DWC3_DCFG_NUMP_MASK;
2018 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2019 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2020}
2021
Felipe Balbid7be2952016-05-04 15:49:37 +03002022static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002023{
Felipe Balbi72246da2011-08-19 18:10:58 +03002024 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002025 int ret = 0;
2026 u32 reg;
2027
John Youncf40b862016-11-14 12:32:43 -08002028 /*
2029 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2030 * the core supports IMOD, disable it.
2031 */
2032 if (dwc->imod_interval) {
2033 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2034 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2035 } else if (dwc3_has_imod(dwc)) {
2036 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2037 }
2038
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002039 /*
2040 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2041 * field instead of letting dwc3 itself calculate that automatically.
2042 *
2043 * This way, we maximize the chances that we'll be able to get several
2044 * bursts of data without going through any sort of endpoint throttling.
2045 */
2046 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002047 if (dwc3_is_usb31(dwc))
2048 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2049 else
2050 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2051
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002052 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2053
Felipe Balbi4e994722016-05-13 14:09:59 +03002054 dwc3_gadget_setup_nump(dwc);
2055
Felipe Balbi72246da2011-08-19 18:10:58 +03002056 /* Start with SuperSpeed Default */
2057 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2058
2059 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002060 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002061 if (ret) {
2062 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002063 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002064 }
2065
2066 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002067 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002068 if (ret) {
2069 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002070 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002071 }
2072
2073 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002074 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002075 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002076 dwc3_ep0_out_start(dwc);
2077
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002078 dwc3_gadget_enable_irq(dwc);
2079
Felipe Balbid7be2952016-05-04 15:49:37 +03002080 return 0;
2081
2082err1:
2083 __dwc3_gadget_ep_disable(dwc->eps[0]);
2084
2085err0:
2086 return ret;
2087}
2088
2089static int dwc3_gadget_start(struct usb_gadget *g,
2090 struct usb_gadget_driver *driver)
2091{
2092 struct dwc3 *dwc = gadget_to_dwc(g);
2093 unsigned long flags;
2094 int ret = 0;
2095 int irq;
2096
Roger Quadros9522def2016-06-10 14:48:38 +03002097 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002098 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2099 IRQF_SHARED, "dwc3", dwc->ev_buf);
2100 if (ret) {
2101 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2102 irq, ret);
2103 goto err0;
2104 }
2105
2106 spin_lock_irqsave(&dwc->lock, flags);
2107 if (dwc->gadget_driver) {
2108 dev_err(dwc->dev, "%s is already bound to %s\n",
2109 dwc->gadget.name,
2110 dwc->gadget_driver->driver.name);
2111 ret = -EBUSY;
2112 goto err1;
2113 }
2114
2115 dwc->gadget_driver = driver;
2116
Felipe Balbifc8bb912016-05-16 13:14:48 +03002117 if (pm_runtime_active(dwc->dev))
2118 __dwc3_gadget_start(dwc);
2119
Felipe Balbi72246da2011-08-19 18:10:58 +03002120 spin_unlock_irqrestore(&dwc->lock, flags);
2121
2122 return 0;
2123
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002124err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002125 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002126 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002127
2128err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002129 return ret;
2130}
2131
Felipe Balbid7be2952016-05-04 15:49:37 +03002132static void __dwc3_gadget_stop(struct dwc3 *dwc)
2133{
2134 dwc3_gadget_disable_irq(dwc);
2135 __dwc3_gadget_ep_disable(dwc->eps[0]);
2136 __dwc3_gadget_ep_disable(dwc->eps[1]);
2137}
2138
Felipe Balbi22835b82014-10-17 12:05:12 -05002139static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002140{
2141 struct dwc3 *dwc = gadget_to_dwc(g);
2142 unsigned long flags;
2143
2144 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08002145
2146 if (pm_runtime_suspended(dwc->dev))
2147 goto out;
2148
Felipe Balbid7be2952016-05-04 15:49:37 +03002149 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08002150
Baolin Wang76a638f2016-10-31 19:38:36 +08002151out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002152 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 spin_unlock_irqrestore(&dwc->lock, flags);
2154
Felipe Balbi3f308d12016-05-16 14:17:06 +03002155 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002156
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 return 0;
2158}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002159
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302160static void dwc3_gadget_config_params(struct usb_gadget *g,
2161 struct usb_dcd_config_params *params)
2162{
2163 struct dwc3 *dwc = gadget_to_dwc(g);
2164
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002165 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2166 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2167
2168 /* Recommended BESL */
2169 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002170 /*
2171 * If the recommended BESL baseline is 0 or if the BESL deep is
2172 * less than 2, Microsoft's Windows 10 host usb stack will issue
2173 * a usb reset immediately after it receives the extended BOS
2174 * descriptor and the enumeration will fail. To maintain
2175 * compatibility with the Windows' usb stack, let's set the
2176 * recommended BESL baseline to 1 and clamp the BESL deep to be
2177 * within 2 to 15.
2178 */
2179 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002180 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002181 params->besl_deep =
2182 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002183 }
2184
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302185 /* U1 Device exit Latency */
2186 if (dwc->dis_u1_entry_quirk)
2187 params->bU1devExitLat = 0;
2188 else
2189 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2190
2191 /* U2 Device exit Latency */
2192 if (dwc->dis_u2_entry_quirk)
2193 params->bU2DevExitLat = 0;
2194 else
2195 params->bU2DevExitLat =
2196 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2197}
2198
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002199static void dwc3_gadget_set_speed(struct usb_gadget *g,
2200 enum usb_device_speed speed)
2201{
2202 struct dwc3 *dwc = gadget_to_dwc(g);
2203 unsigned long flags;
2204 u32 reg;
2205
2206 spin_lock_irqsave(&dwc->lock, flags);
2207 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2208 reg &= ~(DWC3_DCFG_SPEED_MASK);
2209
2210 /*
2211 * WORKAROUND: DWC3 revision < 2.20a have an issue
2212 * which would cause metastability state on Run/Stop
2213 * bit if we try to force the IP to USB2-only mode.
2214 *
2215 * Because of that, we cannot configure the IP to any
2216 * speed other than the SuperSpeed
2217 *
2218 * Refers to:
2219 *
2220 * STAR#9000525659: Clock Domain Crossing on DCTL in
2221 * USB 2.0 Mode
2222 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002223 if (dwc->revision < DWC3_REVISION_220A &&
2224 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002225 reg |= DWC3_DCFG_SUPERSPEED;
2226 } else {
2227 switch (speed) {
2228 case USB_SPEED_LOW:
2229 reg |= DWC3_DCFG_LOWSPEED;
2230 break;
2231 case USB_SPEED_FULL:
2232 reg |= DWC3_DCFG_FULLSPEED;
2233 break;
2234 case USB_SPEED_HIGH:
2235 reg |= DWC3_DCFG_HIGHSPEED;
2236 break;
2237 case USB_SPEED_SUPER:
2238 reg |= DWC3_DCFG_SUPERSPEED;
2239 break;
2240 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002241 if (dwc3_is_usb31(dwc))
2242 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2243 else
2244 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002245 break;
2246 default:
2247 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2248
2249 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2250 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2251 else
2252 reg |= DWC3_DCFG_SUPERSPEED;
2253 }
2254 }
2255 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2256
2257 spin_unlock_irqrestore(&dwc->lock, flags);
2258}
2259
Felipe Balbi72246da2011-08-19 18:10:58 +03002260static const struct usb_gadget_ops dwc3_gadget_ops = {
2261 .get_frame = dwc3_gadget_get_frame,
2262 .wakeup = dwc3_gadget_wakeup,
2263 .set_selfpowered = dwc3_gadget_set_selfpowered,
2264 .pullup = dwc3_gadget_pullup,
2265 .udc_start = dwc3_gadget_start,
2266 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002267 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302268 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002269};
2270
2271/* -------------------------------------------------------------------------- */
2272
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002273static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2274{
2275 struct dwc3 *dwc = dep->dwc;
2276
2277 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2278 dep->endpoint.maxburst = 1;
2279 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2280 if (!dep->direction)
2281 dwc->gadget.ep0 = &dep->endpoint;
2282
2283 dep->endpoint.caps.type_control = true;
2284
2285 return 0;
2286}
2287
2288static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2289{
2290 struct dwc3 *dwc = dep->dwc;
2291 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002292 int size;
2293
2294 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2295 /* MDWIDTH is represented in bits, we need it in bytes */
2296 mdwidth /= 8;
2297
2298 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2299 if (dwc3_is_usb31(dwc))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002300 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002301 else
Thinh Nguyen586f4332020-01-31 16:59:21 -08002302 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002303
2304 /* FIFO Depth is in MDWDITH bytes. Multiply */
2305 size *= mdwidth;
2306
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002307 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002308 * To meet performance requirement, a minimum TxFIFO size of 3x
2309 * MaxPacketSize is recommended for endpoints that support burst and a
2310 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2311 * support burst. Use those numbers and we can calculate the max packet
2312 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002313 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002314 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2315 size /= 3;
2316 else
2317 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002318
2319 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2320
2321 dep->endpoint.max_streams = 15;
2322 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2323 list_add_tail(&dep->endpoint.ep_list,
2324 &dwc->gadget.ep_list);
2325 dep->endpoint.caps.type_iso = true;
2326 dep->endpoint.caps.type_bulk = true;
2327 dep->endpoint.caps.type_int = true;
2328
2329 return dwc3_alloc_trb_pool(dep);
2330}
2331
2332static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2333{
2334 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002335 int mdwidth;
2336 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002337
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002338 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2339
2340 /* MDWIDTH is represented in bits, convert to bytes */
2341 mdwidth /= 8;
2342
2343 /* All OUT endpoints share a single RxFIFO space */
2344 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
2345 if (dwc3_is_usb31(dwc))
2346 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
2347 else
2348 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
2349
2350 /* FIFO depth is in MDWDITH bytes */
2351 size *= mdwidth;
2352
2353 /*
2354 * To meet performance requirement, a minimum recommended RxFIFO size
2355 * is defined as follow:
2356 * RxFIFO size >= (3 x MaxPacketSize) +
2357 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2358 *
2359 * Then calculate the max packet limit as below.
2360 */
2361 size -= (3 * 8) + 16;
2362 if (size < 0)
2363 size = 0;
2364 else
2365 size /= 3;
2366
2367 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002368 dep->endpoint.max_streams = 15;
2369 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2370 list_add_tail(&dep->endpoint.ep_list,
2371 &dwc->gadget.ep_list);
2372 dep->endpoint.caps.type_iso = true;
2373 dep->endpoint.caps.type_bulk = true;
2374 dep->endpoint.caps.type_int = true;
2375
2376 return dwc3_alloc_trb_pool(dep);
2377}
2378
2379static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002380{
2381 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002382 bool direction = epnum & 1;
2383 int ret;
2384 u8 num = epnum >> 1;
2385
2386 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2387 if (!dep)
2388 return -ENOMEM;
2389
2390 dep->dwc = dwc;
2391 dep->number = epnum;
2392 dep->direction = direction;
2393 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2394 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002395 dep->combo_num = 0;
2396 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002397
2398 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2399 direction ? "in" : "out");
2400
2401 dep->endpoint.name = dep->name;
2402
2403 if (!(dep->number > 1)) {
2404 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2405 dep->endpoint.comp_desc = NULL;
2406 }
2407
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002408 if (num == 0)
2409 ret = dwc3_gadget_init_control_endpoint(dep);
2410 else if (direction)
2411 ret = dwc3_gadget_init_in_endpoint(dep);
2412 else
2413 ret = dwc3_gadget_init_out_endpoint(dep);
2414
2415 if (ret)
2416 return ret;
2417
2418 dep->endpoint.caps.dir_in = direction;
2419 dep->endpoint.caps.dir_out = !direction;
2420
2421 INIT_LIST_HEAD(&dep->pending_list);
2422 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002423 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002424
2425 return 0;
2426}
2427
2428static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2429{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002430 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002431
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002432 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2433
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002434 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002435 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002436
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002437 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2438 if (ret)
2439 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002440 }
2441
2442 return 0;
2443}
2444
2445static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2446{
2447 struct dwc3_ep *dep;
2448 u8 epnum;
2449
2450 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2451 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002452 if (!dep)
2453 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302454 /*
2455 * Physical endpoints 0 and 1 are special; they form the
2456 * bi-directional USB endpoint 0.
2457 *
2458 * For those two physical endpoints, we don't allocate a TRB
2459 * pool nor do we add them the endpoints list. Due to that, we
2460 * shouldn't do these two operations otherwise we would end up
2461 * with all sorts of bugs when removing dwc3.ko.
2462 */
2463 if (epnum != 0 && epnum != 1) {
2464 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002465 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302466 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002467
2468 kfree(dep);
2469 }
2470}
2471
Felipe Balbi72246da2011-08-19 18:10:58 +03002472/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002473
Felipe Balbi8f608e82018-03-27 10:53:29 +03002474static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2475 struct dwc3_request *req, struct dwc3_trb *trb,
2476 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302477{
2478 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302479
Felipe Balbidc55c672016-08-12 13:20:32 +03002480 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002481
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002482 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002483 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002484
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002485 /*
2486 * If we're in the middle of series of chained TRBs and we
2487 * receive a short transfer along the way, DWC3 will skip
2488 * through all TRBs including the last TRB in the chain (the
2489 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2490 * bit and SW has to do it manually.
2491 *
2492 * We're going to do that here to avoid problems of HW trying
2493 * to use bogus TRBs for transfers.
2494 */
2495 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2496 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2497
Felipe Balbic6267a52017-01-05 14:58:46 +02002498 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002499 * For isochronous transfers, the first TRB in a service interval must
2500 * have the Isoc-First type. Track and report its interval frame number.
2501 */
2502 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2503 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2504 unsigned int frame_number;
2505
2506 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2507 frame_number &= ~(dep->interval - 1);
2508 req->request.frame_number = frame_number;
2509 }
2510
2511 /*
Felipe Balbic6267a52017-01-05 14:58:46 +02002512 * If we're dealing with unaligned size OUT transfer, we will be left
2513 * with one TRB pending in the ring. We need to manually clear HWO bit
2514 * from that TRB.
2515 */
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002516
2517 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002518 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2519 return 1;
2520 }
2521
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302522 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002523 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302524
Felipe Balbi35b27192017-03-08 13:56:37 +02002525 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2526 return 1;
2527
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002528 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302529 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002530
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002531 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2532 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302533 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002534
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302535 return 0;
2536}
2537
Felipe Balbid3692952018-03-29 13:32:10 +03002538static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2539 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2540 int status)
2541{
2542 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2543 struct scatterlist *sg = req->sg;
2544 struct scatterlist *s;
2545 unsigned int pending = req->num_pending_sgs;
2546 unsigned int i;
2547 int ret = 0;
2548
2549 for_each_sg(sg, s, pending, i) {
2550 trb = &dep->trb_pool[dep->trb_dequeue];
2551
2552 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2553 break;
2554
2555 req->sg = sg_next(s);
2556 req->num_pending_sgs--;
2557
2558 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2559 trb, event, status, true);
2560 if (ret)
2561 break;
2562 }
2563
2564 return ret;
2565}
2566
2567static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2568 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2569 int status)
2570{
2571 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2572
2573 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2574 event, status, false);
2575}
2576
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002577static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2578{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002579 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002580}
2581
Felipe Balbif38e35d2018-04-06 15:56:35 +03002582static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2583 const struct dwc3_event_depevt *event,
2584 struct dwc3_request *req, int status)
2585{
2586 int ret;
2587
2588 if (req->num_pending_sgs)
2589 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2590 status);
2591 else
2592 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2593 status);
2594
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002595 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002596 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2597 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002598 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002599 }
2600
2601 req->request.actual = req->request.length - req->remaining;
2602
Thinh Nguyen49e05902020-03-31 01:40:35 -07002603 if (!dwc3_gadget_ep_request_completed(req)) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002604 __dwc3_gadget_kick_transfer(dep);
2605 goto out;
2606 }
2607
2608 dwc3_gadget_giveback(dep, req, status);
2609
2610out:
2611 return ret;
2612}
2613
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002614static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002615 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002616{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002617 struct dwc3_request *req;
2618 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002619
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002620 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002621 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002622
Felipe Balbif38e35d2018-04-06 15:56:35 +03002623 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2624 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002625 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002626 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002627 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002628}
2629
Felipe Balbiee3638b2018-03-27 11:26:53 +03002630static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2631 const struct dwc3_event_depevt *event)
2632{
Felipe Balbif62afb42018-04-11 10:34:34 +03002633 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002634}
2635
Felipe Balbi8f608e82018-03-27 10:53:29 +03002636static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2637 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002638{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002639 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002640 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002641 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002642
Felipe Balbiee3638b2018-03-27 11:26:53 +03002643 dwc3_gadget_endpoint_frame_from_event(dep, event);
2644
Felipe Balbi72246da2011-08-19 18:10:58 +03002645 if (event->status & DEPEVT_STATUS_BUSERR)
2646 status = -ECONNRESET;
2647
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002648 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2649 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002650
2651 if (list_empty(&dep->started_list))
2652 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002653 }
2654
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002655 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002656
Thinh Nguyena114c4c2020-03-05 13:23:49 -08002657 if (stop)
Felipe Balbic5353b22019-02-13 13:00:54 +02002658 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002659
Felipe Balbifae2b902011-10-14 13:00:30 +03002660 /*
2661 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2662 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2663 */
2664 if (dwc->revision < DWC3_REVISION_183A) {
2665 u32 reg;
2666 int i;
2667
2668 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002669 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002670
2671 if (!(dep->flags & DWC3_EP_ENABLED))
2672 continue;
2673
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002674 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002675 return;
2676 }
2677
2678 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2679 reg |= dwc->u1u2;
2680 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2681
2682 dwc->u1u2 = 0;
2683 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002684}
2685
Felipe Balbi8f608e82018-03-27 10:53:29 +03002686static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2687 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002688{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002689 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002690
2691 /*
2692 * The XferNotReady event is generated only once before the endpoint
2693 * starts. It will be generated again when END_TRANSFER command is
2694 * issued. For some controller versions, the XferNotReady event may be
2695 * generated while the END_TRANSFER command is still in process. Ignore
2696 * it and wait for the next XferNotReady event after the command is
2697 * completed.
2698 */
2699 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2700 return;
2701
Felipe Balbi25abad62018-08-14 10:41:19 +03002702 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002703}
2704
Felipe Balbi72246da2011-08-19 18:10:58 +03002705static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2706 const struct dwc3_event_depevt *event)
2707{
2708 struct dwc3_ep *dep;
2709 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002710 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002711
2712 dep = dwc->eps[epnum];
2713
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002714 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02002715 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002716 return;
2717
2718 /* Handle only EPCMDCMPLT when EP disabled */
2719 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2720 return;
2721 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002722
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 if (epnum == 0 || epnum == 1) {
2724 dwc3_ep0_interrupt(dwc, event);
2725 return;
2726 }
2727
2728 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002729 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002730 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002731 break;
2732 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002733 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002734 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002735 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002736 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2737
2738 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08002739 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi3aec9912019-01-21 13:08:44 +02002740 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Felipe Balbifec90952018-08-01 13:56:50 +03002741 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08002742 if ((dep->flags & DWC3_EP_DELAY_START) &&
2743 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2744 __dwc3_gadget_kick_transfer(dep);
2745
2746 dep->flags &= ~DWC3_EP_DELAY_START;
Baolin Wang76a638f2016-10-31 19:38:36 +08002747 }
2748 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002749 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002750 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002751 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002752 break;
2753 }
2754}
2755
2756static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2757{
2758 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2759 spin_unlock(&dwc->lock);
2760 dwc->gadget_driver->disconnect(&dwc->gadget);
2761 spin_lock(&dwc->lock);
2762 }
2763}
2764
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002765static void dwc3_suspend_gadget(struct dwc3 *dwc)
2766{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002767 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002768 spin_unlock(&dwc->lock);
2769 dwc->gadget_driver->suspend(&dwc->gadget);
2770 spin_lock(&dwc->lock);
2771 }
2772}
2773
2774static void dwc3_resume_gadget(struct dwc3 *dwc)
2775{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002776 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002777 spin_unlock(&dwc->lock);
2778 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002779 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002780 }
2781}
2782
2783static void dwc3_reset_gadget(struct dwc3 *dwc)
2784{
2785 if (!dwc->gadget_driver)
2786 return;
2787
2788 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2789 spin_unlock(&dwc->lock);
2790 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002791 spin_lock(&dwc->lock);
2792 }
2793}
2794
Felipe Balbic5353b22019-02-13 13:00:54 +02002795static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
2796 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002797{
Felipe Balbi72246da2011-08-19 18:10:58 +03002798 struct dwc3_gadget_ep_cmd_params params;
2799 u32 cmd;
2800 int ret;
2801
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08002802 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
2803 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302804 return;
2805
Pratyush Anand57911502012-07-06 15:19:10 +05302806 /*
2807 * NOTICE: We are violating what the Databook says about the
2808 * EndTransfer command. Ideally we would _always_ wait for the
2809 * EndTransfer Command Completion IRQ, but that's causing too
2810 * much trouble synchronizing between us and gadget driver.
2811 *
2812 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08002813 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05302814 *
2815 * Note also that a similar handling was tested by Synopsys
2816 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08002817 * In short, what we're doing is issuing EndTransfer with
2818 * CMDIOC bit set and delay kicking transfer until the
2819 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07002820 *
2821 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2822 * supports a mode to work around the above limitation. The
2823 * software can poll the CMDACT bit in the DEPCMD register
2824 * after issuing a EndTransfer command. This mode is enabled
2825 * by writing GUCTL2[14]. This polling is already done in the
2826 * dwc3_send_gadget_ep_cmd() function so if the mode is
2827 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08002828 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07002829 *
2830 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302831 */
2832
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302833 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002834 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02002835 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03002836 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302837 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002838 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302839 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002840 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002841
Thinh Nguyend3abda52019-11-27 13:10:47 -08002842 if (!interrupt)
2843 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08002844 else
2845 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002846}
2847
Felipe Balbi72246da2011-08-19 18:10:58 +03002848static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2849{
2850 u32 epnum;
2851
2852 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2853 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 int ret;
2855
2856 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002857 if (!dep)
2858 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002859
2860 if (!(dep->flags & DWC3_EP_STALL))
2861 continue;
2862
2863 dep->flags &= ~DWC3_EP_STALL;
2864
John Youn50c763f2016-05-31 17:49:56 -07002865 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002866 WARN_ON_ONCE(ret);
2867 }
2868}
2869
2870static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2871{
Felipe Balbic4430a22012-05-24 10:30:01 +03002872 int reg;
2873
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07002874 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
2875
Felipe Balbi72246da2011-08-19 18:10:58 +03002876 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2877 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07002879 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002880
Felipe Balbi72246da2011-08-19 18:10:58 +03002881 dwc3_disconnect_gadget(dwc);
2882
2883 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002884 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002885 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002886
2887 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002888}
2889
Felipe Balbi72246da2011-08-19 18:10:58 +03002890static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2891{
2892 u32 reg;
2893
Felipe Balbifc8bb912016-05-16 13:14:48 +03002894 dwc->connected = true;
2895
Felipe Balbidf62df52011-10-14 15:11:49 +03002896 /*
2897 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2898 * would cause a missing Disconnect Event if there's a
2899 * pending Setup Packet in the FIFO.
2900 *
2901 * There's no suggested workaround on the official Bug
2902 * report, which states that "unless the driver/application
2903 * is doing any special handling of a disconnect event,
2904 * there is no functional issue".
2905 *
2906 * Unfortunately, it turns out that we _do_ some special
2907 * handling of a disconnect event, namely complete all
2908 * pending transfers, notify gadget driver of the
2909 * disconnection, and so on.
2910 *
2911 * Our suggested workaround is to follow the Disconnect
2912 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002913 * flag. Such flag gets set whenever we have a SETUP_PENDING
2914 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002915 * same endpoint.
2916 *
2917 * Refers to:
2918 *
2919 * STAR#9000466709: RTL: Device : Disconnect event not
2920 * generated if setup packet pending in FIFO
2921 */
2922 if (dwc->revision < DWC3_REVISION_188A) {
2923 if (dwc->setup_packet_pending)
2924 dwc3_gadget_disconnect_interrupt(dwc);
2925 }
2926
Felipe Balbi8e744752014-11-06 14:27:53 +08002927 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002928
2929 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2930 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07002931 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002932 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002933 dwc3_clear_stall_all_ep(dwc);
2934
2935 /* Reset device address to zero */
2936 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2937 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2938 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002939}
2940
Felipe Balbi72246da2011-08-19 18:10:58 +03002941static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2942{
Felipe Balbi72246da2011-08-19 18:10:58 +03002943 struct dwc3_ep *dep;
2944 int ret;
2945 u32 reg;
2946 u8 speed;
2947
Felipe Balbi72246da2011-08-19 18:10:58 +03002948 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2949 speed = reg & DWC3_DSTS_CONNECTSPD;
2950 dwc->speed = speed;
2951
John Youn5fb6fda2016-11-10 17:23:25 -08002952 /*
2953 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2954 * each time on Connect Done.
2955 *
2956 * Currently we always use the reset value. If any platform
2957 * wants to set this to a different value, we need to add a
2958 * setting and update GCTL.RAMCLKSEL here.
2959 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002960
2961 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002962 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002963 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2964 dwc->gadget.ep0->maxpacket = 512;
2965 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2966 break;
John Youn2da9ad72016-05-20 16:34:26 -07002967 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002968 /*
2969 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2970 * would cause a missing USB3 Reset event.
2971 *
2972 * In such situations, we should force a USB3 Reset
2973 * event by calling our dwc3_gadget_reset_interrupt()
2974 * routine.
2975 *
2976 * Refers to:
2977 *
2978 * STAR#9000483510: RTL: SS : USB3 reset event may
2979 * not be generated always when the link enters poll
2980 */
2981 if (dwc->revision < DWC3_REVISION_190A)
2982 dwc3_gadget_reset_interrupt(dwc);
2983
Felipe Balbi72246da2011-08-19 18:10:58 +03002984 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2985 dwc->gadget.ep0->maxpacket = 512;
2986 dwc->gadget.speed = USB_SPEED_SUPER;
2987 break;
John Youn2da9ad72016-05-20 16:34:26 -07002988 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002989 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2990 dwc->gadget.ep0->maxpacket = 64;
2991 dwc->gadget.speed = USB_SPEED_HIGH;
2992 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002993 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002994 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2995 dwc->gadget.ep0->maxpacket = 64;
2996 dwc->gadget.speed = USB_SPEED_FULL;
2997 break;
John Youn2da9ad72016-05-20 16:34:26 -07002998 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002999 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3000 dwc->gadget.ep0->maxpacket = 8;
3001 dwc->gadget.speed = USB_SPEED_LOW;
3002 break;
3003 }
3004
Thinh Nguyen61800262018-01-12 18:18:05 -08003005 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
3006
Pratyush Anand2b758352013-01-14 15:59:31 +05303007 /* Enable USB2 LPM Capability */
3008
John Younee5cd412016-02-05 17:08:45 -08003009 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003010 (speed != DWC3_DSTS_SUPERSPEED) &&
3011 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303012 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3013 reg |= DWC3_DCFG_LPM_CAP;
3014 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3015
3016 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3017 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3018
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003019 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3020 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303021
Huang Rui80caf7d2014-10-28 19:54:26 +08003022 /*
3023 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3024 * DCFG.LPMCap is set, core responses with an ACK and the
3025 * BESL value in the LPM token is less than or equal to LPM
3026 * NYET threshold.
3027 */
3028 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3029 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003030 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003031
3032 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003033 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003034
Thinh Nguyen5b738212019-10-23 19:15:43 -07003035 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003036 } else {
3037 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3038 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003039 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303040 }
3041
Felipe Balbi72246da2011-08-19 18:10:58 +03003042 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003043 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003044 if (ret) {
3045 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3046 return;
3047 }
3048
3049 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003050 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003051 if (ret) {
3052 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3053 return;
3054 }
3055
3056 /*
3057 * Configure PHY via GUSB3PIPECTLn if required.
3058 *
3059 * Update GTXFIFOSIZn
3060 *
3061 * In both cases reset values should be sufficient.
3062 */
3063}
3064
3065static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3066{
Felipe Balbi72246da2011-08-19 18:10:58 +03003067 /*
3068 * TODO take core out of low power mode when that's
3069 * implemented.
3070 */
3071
Jiebing Liad14d4e2014-12-11 13:26:29 +08003072 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3073 spin_unlock(&dwc->lock);
3074 dwc->gadget_driver->resume(&dwc->gadget);
3075 spin_lock(&dwc->lock);
3076 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003077}
3078
3079static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3080 unsigned int evtinfo)
3081{
Felipe Balbifae2b902011-10-14 13:00:30 +03003082 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003083 unsigned int pwropt;
3084
3085 /*
3086 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3087 * Hibernation mode enabled which would show up when device detects
3088 * host-initiated U3 exit.
3089 *
3090 * In that case, device will generate a Link State Change Interrupt
3091 * from U3 to RESUME which is only necessary if Hibernation is
3092 * configured in.
3093 *
3094 * There are no functional changes due to such spurious event and we
3095 * just need to ignore it.
3096 *
3097 * Refers to:
3098 *
3099 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3100 * operational mode
3101 */
3102 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3103 if ((dwc->revision < DWC3_REVISION_250A) &&
3104 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3105 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3106 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003107 return;
3108 }
3109 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003110
3111 /*
3112 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3113 * on the link partner, the USB session might do multiple entry/exit
3114 * of low power states before a transfer takes place.
3115 *
3116 * Due to this problem, we might experience lower throughput. The
3117 * suggested workaround is to disable DCTL[12:9] bits if we're
3118 * transitioning from U1/U2 to U0 and enable those bits again
3119 * after a transfer completes and there are no pending transfers
3120 * on any of the enabled endpoints.
3121 *
3122 * This is the first half of that workaround.
3123 *
3124 * Refers to:
3125 *
3126 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3127 * core send LGO_Ux entering U0
3128 */
3129 if (dwc->revision < DWC3_REVISION_183A) {
3130 if (next == DWC3_LINK_STATE_U0) {
3131 u32 u1u2;
3132 u32 reg;
3133
3134 switch (dwc->link_state) {
3135 case DWC3_LINK_STATE_U1:
3136 case DWC3_LINK_STATE_U2:
3137 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3138 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3139 | DWC3_DCTL_ACCEPTU2ENA
3140 | DWC3_DCTL_INITU1ENA
3141 | DWC3_DCTL_ACCEPTU1ENA);
3142
3143 if (!dwc->u1u2)
3144 dwc->u1u2 = reg & u1u2;
3145
3146 reg &= ~u1u2;
3147
Thinh Nguyen5b738212019-10-23 19:15:43 -07003148 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003149 break;
3150 default:
3151 /* do nothing */
3152 break;
3153 }
3154 }
3155 }
3156
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003157 switch (next) {
3158 case DWC3_LINK_STATE_U1:
3159 if (dwc->speed == USB_SPEED_SUPER)
3160 dwc3_suspend_gadget(dwc);
3161 break;
3162 case DWC3_LINK_STATE_U2:
3163 case DWC3_LINK_STATE_U3:
3164 dwc3_suspend_gadget(dwc);
3165 break;
3166 case DWC3_LINK_STATE_RESUME:
3167 dwc3_resume_gadget(dwc);
3168 break;
3169 default:
3170 /* do nothing */
3171 break;
3172 }
3173
Felipe Balbie57ebc12014-04-22 13:20:12 -05003174 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003175}
3176
Baolin Wang72704f82016-05-16 16:43:53 +08003177static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3178 unsigned int evtinfo)
3179{
3180 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3181
3182 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3183 dwc3_suspend_gadget(dwc);
3184
3185 dwc->link_state = next;
3186}
3187
Felipe Balbie1dadd32014-02-25 14:47:54 -06003188static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3189 unsigned int evtinfo)
3190{
3191 unsigned int is_ss = evtinfo & BIT(4);
3192
Felipe Balbibfad65e2017-04-19 14:59:27 +03003193 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003194 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3195 * have a known issue which can cause USB CV TD.9.23 to fail
3196 * randomly.
3197 *
3198 * Because of this issue, core could generate bogus hibernation
3199 * events which SW needs to ignore.
3200 *
3201 * Refers to:
3202 *
3203 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3204 * Device Fallback from SuperSpeed
3205 */
3206 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3207 return;
3208
3209 /* enter hibernation here */
3210}
3211
Felipe Balbi72246da2011-08-19 18:10:58 +03003212static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3213 const struct dwc3_event_devt *event)
3214{
3215 switch (event->type) {
3216 case DWC3_DEVICE_EVENT_DISCONNECT:
3217 dwc3_gadget_disconnect_interrupt(dwc);
3218 break;
3219 case DWC3_DEVICE_EVENT_RESET:
3220 dwc3_gadget_reset_interrupt(dwc);
3221 break;
3222 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3223 dwc3_gadget_conndone_interrupt(dwc);
3224 break;
3225 case DWC3_DEVICE_EVENT_WAKEUP:
3226 dwc3_gadget_wakeup_interrupt(dwc);
3227 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003228 case DWC3_DEVICE_EVENT_HIBER_REQ:
3229 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3230 "unexpected hibernation event\n"))
3231 break;
3232
3233 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3234 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003235 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3236 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3237 break;
3238 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003239 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003240 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08003241 /*
3242 * Ignore suspend event until the gadget enters into
3243 * USB_STATE_CONFIGURED state.
3244 */
3245 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3246 dwc3_gadget_suspend_interrupt(dwc,
3247 event->event_info);
3248 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003249 break;
3250 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003251 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003252 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003253 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003254 break;
3255 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003256 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003257 }
3258}
3259
3260static void dwc3_process_event_entry(struct dwc3 *dwc,
3261 const union dwc3_event *event)
3262{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003263 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003264
Felipe Balbidfc5e802017-04-26 13:44:51 +03003265 if (!event->type.is_devspec)
3266 dwc3_endpoint_interrupt(dwc, &event->depevt);
3267 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003268 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003269 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003270 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003271}
3272
Felipe Balbidea520a2016-03-30 09:39:34 +03003273static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003274{
Felipe Balbidea520a2016-03-30 09:39:34 +03003275 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003276 irqreturn_t ret = IRQ_NONE;
3277 int left;
3278 u32 reg;
3279
Felipe Balbif42f2442013-06-12 21:25:08 +03003280 left = evt->count;
3281
3282 if (!(evt->flags & DWC3_EVENT_PENDING))
3283 return IRQ_NONE;
3284
3285 while (left > 0) {
3286 union dwc3_event event;
3287
John Younebbb2d52016-11-15 13:07:02 +02003288 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003289
3290 dwc3_process_event_entry(dwc, &event);
3291
3292 /*
3293 * FIXME we wrap around correctly to the next entry as
3294 * almost all entries are 4 bytes in size. There is one
3295 * entry which has 12 bytes which is a regular entry
3296 * followed by 8 bytes data. ATM I don't know how
3297 * things are organized if we get next to the a
3298 * boundary so I worry about that once we try to handle
3299 * that.
3300 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003301 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003302 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003303 }
3304
3305 evt->count = 0;
3306 evt->flags &= ~DWC3_EVENT_PENDING;
3307 ret = IRQ_HANDLED;
3308
3309 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003310 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003311 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003312 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003313
John Youncf40b862016-11-14 12:32:43 -08003314 if (dwc->imod_interval) {
3315 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3316 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3317 }
3318
Felipe Balbif42f2442013-06-12 21:25:08 +03003319 return ret;
3320}
3321
Felipe Balbidea520a2016-03-30 09:39:34 +03003322static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003323{
Felipe Balbidea520a2016-03-30 09:39:34 +03003324 struct dwc3_event_buffer *evt = _evt;
3325 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003326 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003327 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003328
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003329 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003330 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003331 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003332
3333 return ret;
3334}
3335
Felipe Balbidea520a2016-03-30 09:39:34 +03003336static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003337{
Felipe Balbidea520a2016-03-30 09:39:34 +03003338 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003339 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003340 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003341 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003342
Felipe Balbifc8bb912016-05-16 13:14:48 +03003343 if (pm_runtime_suspended(dwc->dev)) {
3344 pm_runtime_get(dwc->dev);
3345 disable_irq_nosync(dwc->irq_gadget);
3346 dwc->pending_events = true;
3347 return IRQ_HANDLED;
3348 }
3349
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003350 /*
3351 * With PCIe legacy interrupt, test shows that top-half irq handler can
3352 * be called again after HW interrupt deassertion. Check if bottom-half
3353 * irq event handler completes before caching new event to prevent
3354 * losing events.
3355 */
3356 if (evt->flags & DWC3_EVENT_PENDING)
3357 return IRQ_HANDLED;
3358
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003359 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003360 count &= DWC3_GEVNTCOUNT_MASK;
3361 if (!count)
3362 return IRQ_NONE;
3363
Felipe Balbib15a7622011-06-30 16:57:15 +03003364 evt->count = count;
3365 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003366
Felipe Balbie8adfc32013-06-12 21:11:14 +03003367 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003368 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003369 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003370 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003371
John Younebbb2d52016-11-15 13:07:02 +02003372 amount = min(count, evt->length - evt->lpos);
3373 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3374
3375 if (amount < count)
3376 memcpy(evt->cache, evt->buf, count - amount);
3377
John Youn65aca322016-11-15 13:08:59 +02003378 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3379
Felipe Balbib15a7622011-06-30 16:57:15 +03003380 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003381}
3382
Felipe Balbidea520a2016-03-30 09:39:34 +03003383static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003384{
Felipe Balbidea520a2016-03-30 09:39:34 +03003385 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003386
Felipe Balbidea520a2016-03-30 09:39:34 +03003387 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003388}
3389
Felipe Balbi6db38122016-10-03 11:27:01 +03003390static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3391{
3392 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3393 int irq;
3394
Hans de Goedef146b40b2019-10-05 23:04:48 +02003395 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003396 if (irq > 0)
3397 goto out;
3398
3399 if (irq == -EPROBE_DEFER)
3400 goto out;
3401
Hans de Goedef146b40b2019-10-05 23:04:48 +02003402 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003403 if (irq > 0)
3404 goto out;
3405
3406 if (irq == -EPROBE_DEFER)
3407 goto out;
3408
3409 irq = platform_get_irq(dwc3_pdev, 0);
3410 if (irq > 0)
3411 goto out;
3412
Felipe Balbi6db38122016-10-03 11:27:01 +03003413 if (!irq)
3414 irq = -EINVAL;
3415
3416out:
3417 return irq;
3418}
3419
Felipe Balbi72246da2011-08-19 18:10:58 +03003420/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003421 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003422 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003423 *
3424 * Returns 0 on success otherwise negative errno.
3425 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003426int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003427{
Felipe Balbi6db38122016-10-03 11:27:01 +03003428 int ret;
3429 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003430
Felipe Balbi6db38122016-10-03 11:27:01 +03003431 irq = dwc3_gadget_get_irq(dwc);
3432 if (irq < 0) {
3433 ret = irq;
3434 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003435 }
3436
3437 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003438
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303439 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3440 sizeof(*dwc->ep0_trb) * 2,
3441 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003442 if (!dwc->ep0_trb) {
3443 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3444 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003445 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003446 }
3447
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003448 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003449 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003450 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003451 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003452 }
3453
Felipe Balbi905dc042017-01-05 14:46:52 +02003454 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3455 &dwc->bounce_addr, GFP_KERNEL);
3456 if (!dwc->bounce) {
3457 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003458 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003459 }
3460
Baolin Wangbb014732016-10-14 17:11:33 +08003461 init_completion(&dwc->ep0_in_setup);
3462
Felipe Balbi72246da2011-08-19 18:10:58 +03003463 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003464 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003465 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003466 dwc->gadget.name = "dwc3-gadget";
Thinh Nguyenc7299692019-04-25 14:28:24 -07003467 dwc->gadget.lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003468
3469 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003470 * FIXME We might be setting max_speed to <SUPER, however versions
3471 * <2.20a of dwc3 have an issue with metastability (documented
3472 * elsewhere in this driver) which tells us we can't set max speed to
3473 * anything lower than SUPER.
3474 *
3475 * Because gadget.max_speed is only used by composite.c and function
3476 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3477 * to happen so we avoid sending SuperSpeed Capability descriptor
3478 * together with our BOS descriptor as that could confuse host into
3479 * thinking we can handle super speed.
3480 *
3481 * Note that, in fact, we won't even support GetBOS requests when speed
3482 * is less than super speed because we don't have means, yet, to tell
3483 * composite.c that we are USB 2.0 + LPM ECN.
3484 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003485 if (dwc->revision < DWC3_REVISION_220A &&
3486 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003487 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003488 dwc->revision);
3489
3490 dwc->gadget.max_speed = dwc->maximum_speed;
3491
3492 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003493 * REVISIT: Here we should clear all pending IRQs to be
3494 * sure we're starting from a well known location.
3495 */
3496
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003497 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003498 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003499 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003500
Felipe Balbi72246da2011-08-19 18:10:58 +03003501 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3502 if (ret) {
3503 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003504 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003505 }
3506
Roger Quadros169e3b62019-01-10 17:04:28 +02003507 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3508
Felipe Balbi72246da2011-08-19 18:10:58 +03003509 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003510
3511err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003512 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003513
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003514err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003515 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3516 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003517
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003518err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003519 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003520
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003521err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303522 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003523 dwc->ep0_trb, dwc->ep0_trb_addr);
3524
Felipe Balbi72246da2011-08-19 18:10:58 +03003525err0:
3526 return ret;
3527}
3528
Felipe Balbi7415f172012-04-30 14:56:33 +03003529/* -------------------------------------------------------------------------- */
3530
Felipe Balbi72246da2011-08-19 18:10:58 +03003531void dwc3_gadget_exit(struct dwc3 *dwc)
3532{
Felipe Balbi72246da2011-08-19 18:10:58 +03003533 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003534 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003535 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003536 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003537 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303538 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003539 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003540}
Felipe Balbi7415f172012-04-30 14:56:33 +03003541
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003542int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003543{
Roger Quadros9772b472016-04-12 11:33:29 +03003544 if (!dwc->gadget_driver)
3545 return 0;
3546
Roger Quadros1551e352017-02-15 14:16:26 +02003547 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003548 dwc3_disconnect_gadget(dwc);
3549 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003550
3551 return 0;
3552}
3553
3554int dwc3_gadget_resume(struct dwc3 *dwc)
3555{
Felipe Balbi7415f172012-04-30 14:56:33 +03003556 int ret;
3557
Roger Quadros9772b472016-04-12 11:33:29 +03003558 if (!dwc->gadget_driver)
3559 return 0;
3560
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003561 ret = __dwc3_gadget_start(dwc);
3562 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003563 goto err0;
3564
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003565 ret = dwc3_gadget_run_stop(dwc, true, false);
3566 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003567 goto err1;
3568
Felipe Balbi7415f172012-04-30 14:56:33 +03003569 return 0;
3570
3571err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003572 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003573
3574err0:
3575 return ret;
3576}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003577
3578void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3579{
3580 if (dwc->pending_events) {
3581 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3582 dwc->pending_events = false;
3583 enable_irq(dwc->irq_gadget);
3584 }
3585}