blob: 8bccdd7b0ca2eaf6de62738f6243d46c14383a7d [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 *
Alexander A. Klimov10623b82020-07-11 15:58:04 +02005 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://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) {
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +020049 case USB_TEST_J:
50 case USB_TEST_K:
51 case USB_TEST_SE0_NAK:
52 case USB_TEST_PACKET:
53 case USB_TEST_FORCE_ENABLE:
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020054 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 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -070098 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 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 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +0300126 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 Balbie319bd62020-08-13 08:35:38 +0300230int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231 u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300232{
233 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300234 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236 u32 reg;
237
238 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
239 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240
241 do {
242 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
243 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300244 status = DWC3_DGCMD_STATUS(reg);
245 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 ret = -EINVAL;
247 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300248 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100249 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250
251 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300252 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300253 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300254 }
255
Felipe Balbi71f7e702016-05-23 14:16:19 +0300256 trace_dwc3_gadget_generic_cmd(cmd, param, status);
257
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300258 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300259}
260
Felipe Balbic36d8e92016-04-04 12:46:33 +0300261static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262
Felipe Balbibfad65e2017-04-19 14:59:27 +0300263/**
264 * dwc3_send_gadget_ep_cmd - issue an endpoint command
265 * @dep: the endpoint to which the command is going to be issued
266 * @cmd: the command to be issued
267 * @params: parameters to the command
268 *
269 * Caller should handle locking. This function will issue @cmd with given
270 * @params to @dep and wait for its completion.
271 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300272int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
Felipe Balbi2cd47182016-04-12 16:42:43 +0300273 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300274{
Felipe Balbi8897a762016-09-22 10:56:08 +0300275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300276 struct dwc3 *dwc = dep->dwc;
Yu Chen1c0e69a2020-05-21 16:46:43 +0800277 u32 timeout = 5000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700278 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 u32 reg;
280
Felipe Balbi0933df12016-05-23 14:02:33 +0300281 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300282 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300283
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700285 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
286 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
287 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300288 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700289 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
290 * settings. Restore them after the command is completed.
291 *
292 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300293 */
Peter Chene81a7012020-08-21 10:55:48 +0800294 if (dwc->gadget->speed <= USB_SPEED_HIGH) {
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300295 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
296 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700297 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300298 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300299 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700300
301 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
302 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
303 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
304 }
305
306 if (saved_config)
307 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300308 }
309
Felipe Balbi59999142016-09-22 12:25:28 +0300310 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Thinh Nguyenb624b322021-04-19 19:11:12 -0700311 int link_state;
Felipe Balbic36d8e92016-04-04 12:46:33 +0300312
Thinh Nguyenb624b322021-04-19 19:11:12 -0700313 link_state = dwc3_gadget_get_link_state(dwc);
314 if (link_state == DWC3_LINK_STATE_U1 ||
315 link_state == DWC3_LINK_STATE_U2 ||
316 link_state == DWC3_LINK_STATE_U3) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300317 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 Balbi6cb2e4e2016-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 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700423 if (dep->direction &&
424 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800425 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700426 cmd |= DWC3_DEPCMD_CLEARPENDIN;
427
428 memset(&params, 0, sizeof(params));
429
Felipe Balbi2cd47182016-04-12 16:42:43 +0300430 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700431}
432
Felipe Balbi72246da2011-08-19 18:10:58 +0300433static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200434 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300435{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300436 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300437
438 return dep->trb_pool_dma + offset;
439}
440
441static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
442{
443 struct dwc3 *dwc = dep->dwc;
444
445 if (dep->trb_pool)
446 return 0;
447
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530448 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300449 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
450 &dep->trb_pool_dma, GFP_KERNEL);
451 if (!dep->trb_pool) {
452 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
453 dep->name);
454 return -ENOMEM;
455 }
456
457 return 0;
458}
459
460static void dwc3_free_trb_pool(struct dwc3_ep *dep)
461{
462 struct dwc3 *dwc = dep->dwc;
463
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530464 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 dep->trb_pool, dep->trb_pool_dma);
466
467 dep->trb_pool = NULL;
468 dep->trb_pool_dma = 0;
469}
470
Felipe Balbi20d1d432018-04-09 12:49:02 +0300471static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
472{
473 struct dwc3_gadget_ep_cmd_params params;
474
475 memset(&params, 0x00, sizeof(params));
476
477 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
478
479 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
480 &params);
481}
John Younc4509602016-02-16 20:10:53 -0800482
483/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300484 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800485 * @dep: endpoint that is being enabled
486 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300487 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
488 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800489 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300490 * The assignment of transfer resources cannot perfectly follow the data book
491 * due to the fact that the controller driver does not have all knowledge of the
492 * configuration in advance. It is given this information piecemeal by the
493 * composite gadget framework after every SET_CONFIGURATION and
494 * SET_INTERFACE. Trying to follow the databook programming model in this
495 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800496 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300497 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
498 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
499 * incorrect in the scenario of multiple interfaces.
500 *
501 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800502 * endpoint on alt setting (8.1.6).
503 *
504 * The following simplified method is used instead:
505 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300506 * All hardware endpoints can be assigned a transfer resource and this setting
507 * will stay persistent until either a core reset or hibernation. So whenever we
508 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
509 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800510 * guaranteed that there are as many transfer resources as endpoints.
511 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300512 * This function is called for each endpoint when it is being enabled but is
513 * triggered only when called for EP0-out, which always happens first, and which
514 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800515 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300516static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300517{
518 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300519 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800521 int i;
522 int ret;
523
524 if (dep->number)
525 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300526
527 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800528 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300529 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
Felipe Balbi2cd47182016-04-12 16:42:43 +0300531 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800532 if (ret)
533 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
John Younc4509602016-02-16 20:10:53 -0800535 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
536 struct dwc3_ep *dep = dwc->eps[i];
537
538 if (!dep)
539 continue;
540
Felipe Balbib07c2db2018-04-09 12:46:47 +0300541 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800542 if (ret)
543 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 }
545
546 return 0;
547}
548
Felipe Balbib07c2db2018-04-09 12:46:47 +0300549static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300550{
John Youn39ebb052016-11-09 16:36:28 -0800551 const struct usb_ss_ep_comp_descriptor *comp_desc;
552 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300554 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
John Youn39ebb052016-11-09 16:36:28 -0800556 comp_desc = dep->endpoint.comp_desc;
557 desc = dep->endpoint.desc;
558
Felipe Balbi72246da2011-08-19 18:10:58 +0300559 memset(&params, 0x00, sizeof(params));
560
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300561 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900562 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
563
564 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800565 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300566 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300567
Felipe Balbi676e3492016-04-26 10:49:07 +0300568 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900569 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300570
Felipe Balbia2d23f02018-04-09 12:40:48 +0300571 params.param0 |= action;
572 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600573 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600574
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300575 if (usb_endpoint_xfer_control(desc))
576 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300577
578 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
579 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200581 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300582 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700583 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300584 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300585 dep->stream_capable = true;
586 }
587
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500588 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300589 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
591 /*
592 * We are doing 1:1 mapping for endpoints, meaning
593 * Physical Endpoints 2 maps to Logical Endpoint 2 and
594 * so on. We consider the direction bit as part of the physical
595 * endpoint number. So USB endpoint 0x81 is 0x03.
596 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300597 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
599 /*
600 * We must use the lower 16 TX FIFOs even though
601 * HW might have more
602 */
603 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300604 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300605
606 if (desc->bInterval) {
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800607 u8 bInterval_m1;
608
609 /*
Thinh Nguyenf9ddfaa2021-04-15 00:41:58 -0700610 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
611 *
612 * NOTE: The programming guide incorrectly stated bInterval_m1
613 * must be set to 0 when operating in fullspeed. Internally the
614 * controller does not have this limitation. See DWC_usb3x
615 * programming guide section 3.2.2.1.
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800616 */
617 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800618
Thinh Nguyen5b4cd962021-02-08 13:53:16 -0800619 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
620 dwc->gadget->speed == USB_SPEED_FULL)
621 dep->interval = desc->bInterval;
622 else
623 dep->interval = 1 << (desc->bInterval - 1);
624
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800625 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 }
627
Felipe Balbi2cd47182016-04-12 16:42:43 +0300628 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300629}
630
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700631static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
632 bool interrupt);
633
Felipe Balbi72246da2011-08-19 18:10:58 +0300634/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300635 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300637 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300638 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300639 * Caller should take care of locking. Execute all necessary commands to
640 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300642static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300643{
John Youn39ebb052016-11-09 16:36:28 -0800644 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300645 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800646
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300648 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300649
650 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300651 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 if (ret)
653 return ret;
654 }
655
Felipe Balbib07c2db2018-04-09 12:46:47 +0300656 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 if (ret)
658 return ret;
659
660 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200661 struct dwc3_trb *trb_st_hw;
662 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300663
Felipe Balbi72246da2011-08-19 18:10:58 +0300664 dep->type = usb_endpoint_type(desc);
665 dep->flags |= DWC3_EP_ENABLED;
666
667 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
668 reg |= DWC3_DALEPENA_EP(dep->number);
669 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
670
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300671 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200672 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300673
John Youn0d257442016-05-19 17:26:08 -0700674 /* Initialize the TRB ring */
675 dep->trb_dequeue = 0;
676 dep->trb_enqueue = 0;
677 memset(dep->trb_pool, 0,
678 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
679
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300680 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 trb_st_hw = &dep->trb_pool[0];
682
Felipe Balbif6bafc62012-02-06 11:04:53 +0200683 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200684 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
685 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
686 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
687 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 }
689
Felipe Balbia97ea992016-09-29 16:28:56 +0300690 /*
691 * Issue StartTransfer here with no-op TRB so we can always rely on No
692 * Response Update Transfer command.
693 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700694 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300695 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300696 struct dwc3_gadget_ep_cmd_params params;
697 struct dwc3_trb *trb;
698 dma_addr_t trb_dma;
699 u32 cmd;
700
701 memset(&params, 0, sizeof(params));
702 trb = &dep->trb_pool[0];
703 trb_dma = dwc3_trb_dma_offset(dep, trb);
704
705 params.param0 = upper_32_bits(trb_dma);
706 params.param1 = lower_32_bits(trb_dma);
707
708 cmd = DWC3_DEPCMD_STARTTRANSFER;
709
710 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
711 if (ret < 0)
712 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700713
714 if (dep->stream_capable) {
715 /*
716 * For streams, at start, there maybe a race where the
717 * host primes the endpoint before the function driver
718 * queues a request to initiate a stream. In that case,
719 * the controller will not see the prime to generate the
720 * ERDY and start stream. To workaround this, issue a
721 * no-op TRB as normal, but end it immediately. As a
722 * result, when the function driver queues the request,
723 * the next START_TRANSFER command will cause the
724 * controller to generate an ERDY to initiate the
725 * stream.
726 */
727 dwc3_stop_active_transfer(dep, true, true);
728
729 /*
730 * All stream eps will reinitiate stream on NoStream
731 * rejection until we can determine that the host can
732 * prime after the first transfer.
733 */
734 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
735 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300736 }
737
Felipe Balbi2870e502016-11-03 13:53:29 +0200738out:
739 trace_dwc3_gadget_ep_enable(dep);
740
Felipe Balbi72246da2011-08-19 18:10:58 +0300741 return 0;
742}
743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300745{
746 struct dwc3_request *req;
747
Felipe Balbic5353b22019-02-13 13:00:54 +0200748 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300749
Felipe Balbi0e146022016-06-21 10:32:02 +0300750 /* - giveback all requests to gadget driver */
751 while (!list_empty(&dep->started_list)) {
752 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200753
Felipe Balbi0e146022016-06-21 10:32:02 +0300754 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200755 }
756
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200757 while (!list_empty(&dep->pending_list)) {
758 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300759
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200760 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200762
763 while (!list_empty(&dep->cancelled_list)) {
764 req = next_request(&dep->cancelled_list);
765
766 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
767 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300768}
769
770/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300771 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 * @dep: the endpoint to disable
773 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300774 * This function undoes what __dwc3_gadget_ep_enable did and also removes
775 * requests which are currently being processed by the hardware and those which
776 * are not yet scheduled.
777 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200778 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300779 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300780static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
781{
782 struct dwc3 *dwc = dep->dwc;
783 u32 reg;
784
Felipe Balbi2870e502016-11-03 13:53:29 +0200785 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500786
Felipe Balbi687ef982014-04-16 10:30:33 -0500787 /* make sure HW endpoint isn't stalled */
788 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500789 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500790
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
792 reg &= ~DWC3_DALEPENA_EP(dep->number);
793 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
794
John Youn39ebb052016-11-09 16:36:28 -0800795 /* Clear out the ep descriptors for non-ep0 */
796 if (dep->number > 1) {
797 dep->endpoint.comp_desc = NULL;
798 dep->endpoint.desc = NULL;
799 }
800
Wesley Chengc7bb96a2021-03-11 15:59:02 -0800801 dwc3_remove_requests(dwc, dep);
802
Wesley Cheng996a5782021-03-24 11:31:04 -0700803 dep->stream_capable = false;
804 dep->type = 0;
805 dep->flags = 0;
806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 return 0;
808}
809
810/* -------------------------------------------------------------------------- */
811
812static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
813 const struct usb_endpoint_descriptor *desc)
814{
815 return -EINVAL;
816}
817
818static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
819{
820 return -EINVAL;
821}
822
823/* -------------------------------------------------------------------------- */
824
825static int dwc3_gadget_ep_enable(struct usb_ep *ep,
826 const struct usb_endpoint_descriptor *desc)
827{
828 struct dwc3_ep *dep;
829 struct dwc3 *dwc;
830 unsigned long flags;
831 int ret;
832
833 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
834 pr_debug("dwc3: invalid parameters\n");
835 return -EINVAL;
836 }
837
838 if (!desc->wMaxPacketSize) {
839 pr_debug("dwc3: missing wMaxPacketSize\n");
840 return -EINVAL;
841 }
842
843 dep = to_dwc3_ep(ep);
844 dwc = dep->dwc;
845
Felipe Balbi95ca9612015-12-10 13:08:20 -0600846 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
847 "%s is already enabled\n",
848 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300849 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300850
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300852 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 spin_unlock_irqrestore(&dwc->lock, flags);
854
855 return ret;
856}
857
858static int dwc3_gadget_ep_disable(struct usb_ep *ep)
859{
860 struct dwc3_ep *dep;
861 struct dwc3 *dwc;
862 unsigned long flags;
863 int ret;
864
865 if (!ep) {
866 pr_debug("dwc3: invalid parameters\n");
867 return -EINVAL;
868 }
869
870 dep = to_dwc3_ep(ep);
871 dwc = dep->dwc;
872
Felipe Balbi95ca9612015-12-10 13:08:20 -0600873 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
874 "%s is already disabled\n",
875 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300877
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 spin_lock_irqsave(&dwc->lock, flags);
879 ret = __dwc3_gadget_ep_disable(dep);
880 spin_unlock_irqrestore(&dwc->lock, flags);
881
882 return ret;
883}
884
885static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300886 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300887{
888 struct dwc3_request *req;
889 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300890
891 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900892 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300893 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300895 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300896 req->epnum = dep->number;
897 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200898 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300899
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500900 trace_dwc3_alloc_request(req);
901
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 return &req->request;
903}
904
905static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
906 struct usb_request *request)
907{
908 struct dwc3_request *req = to_dwc3_request(request);
909
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500910 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300911 kfree(req);
912}
913
Felipe Balbi42626912018-04-09 13:01:43 +0300914/**
915 * dwc3_ep_prev_trb - returns the previous TRB in the ring
916 * @dep: The endpoint with the TRB ring
917 * @index: The index of the current TRB in the ring
918 *
919 * Returns the TRB prior to the one pointed to by the index. If the
920 * index is 0, we will wrap backwards, skip the link TRB, and return
921 * the one just before that.
922 */
923static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
924{
925 u8 tmp = index;
926
927 if (!tmp)
928 tmp = DWC3_TRB_NUM - 1;
929
930 return &dep->trb_pool[tmp - 1];
931}
932
933static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
934{
935 struct dwc3_trb *tmp;
936 u8 trbs_left;
937
938 /*
939 * If enqueue & dequeue are equal than it is either full or empty.
940 *
941 * One way to know for sure is if the TRB right before us has HWO bit
942 * set or not. If it has, then we're definitely full and can't fit any
943 * more transfers in our ring.
944 */
945 if (dep->trb_enqueue == dep->trb_dequeue) {
946 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
947 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
948 return 0;
949
950 return DWC3_TRB_NUM - 1;
951 }
952
953 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
954 trbs_left &= (DWC3_TRB_NUM - 1);
955
956 if (dep->trb_dequeue < dep->trb_enqueue)
957 trbs_left--;
958
959 return trbs_left;
960}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300961
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200962static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300963 dma_addr_t dma, unsigned int length, unsigned int chain,
964 unsigned int node, unsigned int stream_id,
965 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700966 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200967{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300968 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800969 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300970 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200971
Felipe Balbif6bafc62012-02-06 11:04:53 +0200972 trb->size = DWC3_TRB_SIZE_LENGTH(length);
973 trb->bpl = lower_32_bits(dma);
974 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200975
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200976 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200977 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200978 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200979 break;
980
981 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300982 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530983 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300984
Manu Gautam40d829f2017-07-19 17:07:10 +0530985 /*
986 * USB Specification 2.0 Section 5.9.2 states that: "If
987 * there is only a single transaction in the microframe,
988 * only a DATA0 data packet PID is used. If there are
989 * two transactions per microframe, DATA1 is used for
990 * the first transaction data packet and DATA0 is used
991 * for the second transaction data packet. If there are
992 * three transactions per microframe, DATA2 is used for
993 * the first transaction data packet, DATA1 is used for
994 * the second, and DATA0 is used for the third."
995 *
996 * IOW, we should satisfy the following cases:
997 *
998 * 1) length <= maxpacket
999 * - DATA0
1000 *
1001 * 2) maxpacket < length <= (2 * maxpacket)
1002 * - DATA1, DATA0
1003 *
1004 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1005 * - DATA2, DATA1, DATA0
1006 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001007 if (speed == USB_SPEED_HIGH) {
1008 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301009 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301010 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1011
1012 if (length <= (2 * maxp))
1013 mult--;
1014
1015 if (length <= maxp)
1016 mult--;
1017
1018 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001019 }
1020 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301021 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001022 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001023
1024 /* always enable Interrupt on Missed ISOC */
1025 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001026 break;
1027
1028 case USB_ENDPOINT_XFER_BULK:
1029 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001030 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001031 break;
1032 default:
1033 /*
1034 * This is only possible with faulty memory because we
1035 * checked it already :)
1036 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001037 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1038 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001039 }
1040
Tejas Joglekar244add82018-12-10 16:08:13 +05301041 /*
1042 * Enable Continue on Short Packet
1043 * when endpoint is not a stream capable
1044 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001045 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301046 if (!dep->stream_capable)
1047 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001048
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001049 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001050 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1051 }
1052
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001053 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001054 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001055
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301056 if (chain)
1057 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001058 else if (dep->stream_capable && is_last)
1059 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301060
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001061 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001062 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001063
1064 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001065
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301066 dwc3_ep_inc_enq(dep);
1067
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001068 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001069}
1070
John Youn361572b2016-05-19 17:26:17 -07001071/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001072 * dwc3_prepare_one_trb - setup one TRB from one request
1073 * @dep: endpoint for which this request is prepared
1074 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001075 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001076 * @chain: should this TRB be chained to the next?
1077 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001078 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001079 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001080 */
1081static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001082 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001083 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1084 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001085{
1086 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301087 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001088 unsigned int stream_id = req->request.stream_id;
1089 unsigned int short_not_ok = req->request.short_not_ok;
1090 unsigned int no_interrupt = req->request.no_interrupt;
1091 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301092
Thinh Nguyen2b803572020-09-24 01:21:30 -07001093 if (use_bounce_buffer)
1094 dma = dep->dwc->bounce_addr;
1095 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301096 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001097 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301098 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001099
1100 trb = &dep->trb_pool[dep->trb_enqueue];
1101
1102 if (!req->trb) {
1103 dwc3_gadget_move_started_request(req);
1104 req->trb = trb;
1105 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001106 }
1107
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001108 req->num_trbs++;
1109
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001110 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001111 stream_id, short_not_ok, no_interrupt, is_last,
1112 must_interrupt);
1113}
1114
1115static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1116{
1117 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1118 unsigned int rem = req->request.length % maxp;
1119
1120 if ((req->request.length && req->request.zero && !rem &&
1121 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1122 (!req->direction && rem))
1123 return true;
1124
1125 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001126}
1127
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001128/**
1129 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1130 * @dep: The endpoint that the request belongs to
1131 * @req: The request to prepare
1132 * @entry_length: The last SG entry size
1133 * @node: Indicates whether this is not the first entry (for isoc only)
1134 *
1135 * Return the number of TRBs prepared.
1136 */
1137static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1138 struct dwc3_request *req, unsigned int entry_length,
1139 unsigned int node)
1140{
1141 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1142 unsigned int rem = req->request.length % maxp;
1143 unsigned int num_trbs = 1;
1144
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001145 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001146 num_trbs++;
1147
1148 if (dwc3_calc_trbs_left(dep) < num_trbs)
1149 return 0;
1150
1151 req->needs_extra_trb = num_trbs > 1;
1152
1153 /* Prepare a normal TRB */
1154 if (req->direction || req->request.length)
1155 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001156 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001157
1158 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1159 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1160 dwc3_prepare_one_trb(dep, req,
1161 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001162 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001163
1164 return num_trbs;
1165}
1166
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001167static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001168 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001169{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301170 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001171 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001172 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001173 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301174 unsigned int remaining = req->request.num_mapped_sgs
1175 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001176 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001177 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301178
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001179 /*
1180 * If we resume preparing the request, then get the remaining length of
1181 * the request and resume where we left off.
1182 */
1183 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1184 length -= sg_dma_len(s);
1185
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301186 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001187 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001188 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001189 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001190 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001191
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001192 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1193
1194 length -= trb_length;
1195
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001196 /*
1197 * IOMMU driver is coalescing the list of sgs which shares a
1198 * page boundary into one and giving it to USB driver. With
1199 * this the number of sgs mapped is not equal to the number of
1200 * sgs passed. So mark the chain bit to false if it isthe last
1201 * mapped sg.
1202 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001203 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001204 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001205
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001206 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001207 break;
1208
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001209 if (last_sg) {
1210 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001211 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001212 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001213 /*
1214 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001215 * next SG entry. If not, set interrupt on this TRB to
1216 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001217 * free.
1218 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001219 if (num_trbs_left == 1 || (needs_extra_trb &&
1220 num_trbs_left <= 2 &&
1221 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001222 must_interrupt = true;
1223
1224 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1225 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001226 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001227
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301228 /*
1229 * There can be a situation where all sgs in sglist are not
1230 * queued because of insufficient trb number. To handle this
1231 * case, update start_sg to next sg to be queued, so that
1232 * we have free trbs we can continue queuing from where we
1233 * previously stopped
1234 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001235 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301236 req->start_sg = sg_next(s);
1237
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301238 req->num_queued_sgs++;
Thinh Nguyenadccf172021-05-12 20:17:09 -07001239 req->num_pending_sgs--;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301240
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001241 /*
1242 * The number of pending SG entries may not correspond to the
1243 * number of mapped SG entries. If all the data are queued, then
1244 * don't include unused SG entries.
1245 */
1246 if (length == 0) {
Thinh Nguyenadccf172021-05-12 20:17:09 -07001247 req->num_pending_sgs = 0;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001248 break;
1249 }
1250
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001251 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001252 break;
1253 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001254
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001255 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001256}
1257
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001258static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001259 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001260{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001261 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001262}
1263
Felipe Balbi72246da2011-08-19 18:10:58 +03001264/*
1265 * dwc3_prepare_trbs - setup TRBs from requests
1266 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001267 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001268 * The function goes through the requests list and sets up TRBs for the
1269 * transfers. The function returns once there are no more TRBs available or
1270 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001271 *
1272 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001273 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001274static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001275{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001276 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001277 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001278
1279 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1280
Felipe Balbid86c5a62016-10-25 13:48:52 +03001281 /*
1282 * We can get in a situation where there's a request in the started list
1283 * but there weren't enough TRBs to fully kick it in the first time
1284 * around, so it has been waiting for more TRBs to be freed up.
1285 *
1286 * In that case, we should check if we have a request with pending_sgs
1287 * in the started list and prepare TRBs for that request first,
1288 * otherwise we will prepare TRBs completely out of order and that will
1289 * break things.
1290 */
1291 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001292 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001293 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001294 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001295 return ret;
1296 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001297
1298 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001299 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001300
1301 /*
1302 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1303 * burst capability may try to read and use TRBs beyond the
1304 * active transfer instead of stopping.
1305 */
1306 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001307 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001308 }
1309
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001310 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001311 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001312
1313 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1314 dep->direction);
1315 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001316 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001317
1318 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301319 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301320 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001321 req->num_pending_sgs = req->request.num_mapped_sgs;
1322
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001323 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001324 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001325 if (req->num_pending_sgs)
1326 return ret;
1327 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001328 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001329 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001330
Thinh Nguyen490410b2020-09-24 01:21:55 -07001331 if (!ret || !dwc3_calc_trbs_left(dep))
1332 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001333
1334 /*
1335 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1336 * burst capability may try to read and use TRBs beyond the
1337 * active transfer instead of stopping.
1338 */
1339 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001340 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001341 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001342
1343 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001344}
1345
Thinh Nguyen8d990872020-03-29 16:12:57 -07001346static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1347
Felipe Balbi7fdca762017-09-05 14:41:34 +03001348static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001349{
1350 struct dwc3_gadget_ep_cmd_params params;
1351 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001352 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001353 int ret;
1354 u32 cmd;
1355
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001356 /*
1357 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1358 * This happens when we need to stop and restart a transfer such as in
1359 * the case of reinitiating a stream or retrying an isoc transfer.
1360 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001361 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001362 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001363 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001364
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001365 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001366
Thinh Nguyen23384842020-09-30 17:44:38 -07001367 /*
1368 * If there's no new TRB prepared and we don't need to restart a
1369 * transfer, there's no need to update the transfer.
1370 */
1371 if (!ret && !starting)
1372 return ret;
1373
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001374 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 if (!req) {
1376 dep->flags |= DWC3_EP_PENDING_REQUEST;
1377 return 0;
1378 }
1379
1380 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001381
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001382 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301383 params.param0 = upper_32_bits(req->trb_dma);
1384 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001385 cmd = DWC3_DEPCMD_STARTTRANSFER;
1386
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301387 if (dep->stream_capable)
1388 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1389
Felipe Balbi7fdca762017-09-05 14:41:34 +03001390 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1391 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301392 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001393 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1394 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301395 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001396
Felipe Balbi2cd47182016-04-12 16:42:43 +03001397 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001398 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001399 struct dwc3_request *tmp;
1400
1401 if (ret == -EAGAIN)
1402 return ret;
1403
1404 dwc3_stop_active_transfer(dep, true, true);
1405
1406 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1407 dwc3_gadget_move_cancelled_request(req);
1408
1409 /* If ep isn't started, then there's no end transfer pending */
1410 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1411 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1412
Felipe Balbi72246da2011-08-19 18:10:58 +03001413 return ret;
1414 }
1415
Thinh Nguyene0d19562020-05-05 19:46:57 -07001416 if (dep->stream_capable && req->request.is_last)
1417 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1418
Felipe Balbi72246da2011-08-19 18:10:58 +03001419 return 0;
1420}
1421
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001422static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1423{
1424 u32 reg;
1425
1426 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1427 return DWC3_DSTS_SOFFN(reg);
1428}
1429
Thinh Nguyend92021f2018-11-14 22:56:54 -08001430/**
1431 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1432 * @dep: isoc endpoint
1433 *
1434 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1435 * microframe number reported by the XferNotReady event for the future frame
1436 * number to start the isoc transfer.
1437 *
1438 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1439 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1440 * XferNotReady event are invalid. The driver uses this number to schedule the
1441 * isochronous transfer and passes it to the START TRANSFER command. Because
1442 * this number is invalid, the command may fail. If BIT[15:14] matches the
1443 * internal 16-bit microframe, the START TRANSFER command will pass and the
1444 * transfer will start at the scheduled time, if it is off by 1, the command
1445 * will still pass, but the transfer will start 2 seconds in the future. For all
1446 * other conditions, the START TRANSFER command will fail with bus-expiry.
1447 *
1448 * In order to workaround this issue, we can test for the correct combination of
1449 * BIT[15:14] by sending START TRANSFER commands with different values of
1450 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1451 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1452 * As the result, within the 4 possible combinations for BIT[15:14], there will
1453 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1454 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1455 * value is the correct combination.
1456 *
1457 * Since there are only 4 outcomes and the results are ordered, we can simply
1458 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1459 * deduce the smaller successful combination.
1460 *
1461 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1462 * of BIT[15:14]. The correct combination is as follow:
1463 *
1464 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1465 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1466 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1467 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1468 *
1469 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1470 * endpoints.
1471 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001472static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301473{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001474 int cmd_status = 0;
1475 bool test0;
1476 bool test1;
1477
1478 while (dep->combo_num < 2) {
1479 struct dwc3_gadget_ep_cmd_params params;
1480 u32 test_frame_number;
1481 u32 cmd;
1482
1483 /*
1484 * Check if we can start isoc transfer on the next interval or
1485 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1486 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001487 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001488 test_frame_number |= dep->combo_num << 14;
1489 test_frame_number += max_t(u32, 4, dep->interval);
1490
1491 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1492 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1493
1494 cmd = DWC3_DEPCMD_STARTTRANSFER;
1495 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1496 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1497
1498 /* Redo if some other failure beside bus-expiry is received */
1499 if (cmd_status && cmd_status != -EAGAIN) {
1500 dep->start_cmd_status = 0;
1501 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001502 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001503 }
1504
1505 /* Store the first test status */
1506 if (dep->combo_num == 0)
1507 dep->start_cmd_status = cmd_status;
1508
1509 dep->combo_num++;
1510
1511 /*
1512 * End the transfer if the START_TRANSFER command is successful
1513 * to wait for the next XferNotReady to test the command again
1514 */
1515 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001516 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001517 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001518 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301519 }
1520
Thinh Nguyend92021f2018-11-14 22:56:54 -08001521 /* test0 and test1 are both completed at this point */
1522 test0 = (dep->start_cmd_status == 0);
1523 test1 = (cmd_status == 0);
1524
1525 if (!test0 && test1)
1526 dep->combo_num = 1;
1527 else if (!test0 && !test1)
1528 dep->combo_num = 2;
1529 else if (test0 && !test1)
1530 dep->combo_num = 3;
1531 else if (test0 && test1)
1532 dep->combo_num = 0;
1533
Michael Grzeschikca143782020-07-01 20:24:51 +02001534 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001535 dep->frame_number |= dep->combo_num << 14;
1536 dep->frame_number += max_t(u32, 4, dep->interval);
1537
1538 /* Reinitialize test variables */
1539 dep->start_cmd_status = 0;
1540 dep->combo_num = 0;
1541
Felipe Balbi25abad62018-08-14 10:41:19 +03001542 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001543}
1544
Felipe Balbi25abad62018-08-14 10:41:19 +03001545static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301546{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001547 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001548 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001549 int ret;
1550 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001551
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001552 if (list_empty(&dep->pending_list) &&
1553 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301554 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001555 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301556 }
1557
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001558 if (!dwc->dis_start_transfer_quirk &&
1559 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1560 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001561 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001562 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001563 }
1564
Michael Olbrichc5a70922020-07-01 20:24:52 +02001565 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001566 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001567 u32 frame = __dwc3_gadget_get_frame(dwc);
1568 bool rollover = frame <
1569 (dep->frame_number & DWC3_FRNUMBER_MASK);
1570
1571 /*
1572 * frame_number is set from XferNotReady and may be already
1573 * out of date. DSTS only provides the lower 14 bit of the
1574 * current frame number. So add the upper two bits of
1575 * frame_number and handle a possible rollover.
1576 * This will provide the correct frame_number unless more than
1577 * rollover has happened since XferNotReady.
1578 */
1579
1580 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1581 frame;
1582 if (rollover)
1583 dep->frame_number += BIT(14);
1584 }
1585
Felipe Balbid5370102018-08-14 10:42:43 +03001586 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1587 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1588
1589 ret = __dwc3_gadget_kick_transfer(dep);
1590 if (ret != -EAGAIN)
1591 break;
1592 }
1593
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001594 /*
1595 * After a number of unsuccessful start attempts due to bus-expiry
1596 * status, issue END_TRANSFER command and retry on the next XferNotReady
1597 * event.
1598 */
1599 if (ret == -EAGAIN) {
1600 struct dwc3_gadget_ep_cmd_params params;
1601 u32 cmd;
1602
1603 cmd = DWC3_DEPCMD_ENDTRANSFER |
1604 DWC3_DEPCMD_CMDIOC |
1605 DWC3_DEPCMD_PARAM(dep->resource_index);
1606
1607 dep->resource_index = 0;
1608 memset(&params, 0, sizeof(params));
1609
1610 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1611 if (!ret)
1612 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1613 }
1614
Felipe Balbid5370102018-08-14 10:42:43 +03001615 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301616}
1617
Felipe Balbi72246da2011-08-19 18:10:58 +03001618static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1619{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001620 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001621
Wesley Chengc7bb96a2021-03-11 15:59:02 -08001622 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001623 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1624 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001625 return -ESHUTDOWN;
1626 }
1627
Felipe Balbi04fb3652017-05-17 15:57:45 +03001628 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1629 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001630 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001631
Felipe Balbib2b6d602019-01-11 12:58:52 +02001632 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1633 "%s: request %pK already in flight\n",
1634 dep->name, &req->request))
1635 return -EINVAL;
1636
Felipe Balbifc8bb912016-05-16 13:14:48 +03001637 pm_runtime_get(dwc->dev);
1638
Felipe Balbi72246da2011-08-19 18:10:58 +03001639 req->request.actual = 0;
1640 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001641
Felipe Balbife84f522015-09-01 09:01:38 -05001642 trace_dwc3_ep_queue(req);
1643
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001644 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001645 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001646
Thinh Nguyene0d19562020-05-05 19:46:57 -07001647 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1648 return 0;
1649
Thinh Nguyenc5036722020-09-02 18:42:58 -07001650 /*
1651 * Start the transfer only after the END_TRANSFER is completed
1652 * and endpoint STALL is cleared.
1653 */
1654 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1655 (dep->flags & DWC3_EP_WEDGE) ||
1656 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001657 dep->flags |= DWC3_EP_DELAY_START;
1658 return 0;
1659 }
1660
Felipe Balbid889c232016-09-29 15:44:29 +03001661 /*
1662 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1663 * wait for a XferNotReady event so we will know what's the current
1664 * (micro-)frame number.
1665 *
1666 * Without this trick, we are very, very likely gonna get Bus Expiry
1667 * errors which will force us issue EndTransfer command.
1668 */
1669 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001670 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1671 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001672 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001673
1674 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001675 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001676 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001677 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001678 }
1679
Wesley Cheng9bd96a22021-05-07 10:55:19 -07001680 __dwc3_gadget_kick_transfer(dep);
1681
1682 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001683}
1684
1685static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1686 gfp_t gfp_flags)
1687{
1688 struct dwc3_request *req = to_dwc3_request(request);
1689 struct dwc3_ep *dep = to_dwc3_ep(ep);
1690 struct dwc3 *dwc = dep->dwc;
1691
1692 unsigned long flags;
1693
1694 int ret;
1695
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001696 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001697 ret = __dwc3_gadget_ep_queue(dep, req);
1698 spin_unlock_irqrestore(&dwc->lock, flags);
1699
1700 return ret;
1701}
1702
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001703static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1704{
1705 int i;
1706
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001707 /* If req->trb is not set, then the request has not started */
1708 if (!req->trb)
1709 return;
1710
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001711 /*
1712 * If request was already started, this means we had to
1713 * stop the transfer. With that we also need to ignore
1714 * all TRBs used by the request, however TRBs can only
1715 * be modified after completion of END_TRANSFER
1716 * command. So what we do here is that we wait for
1717 * END_TRANSFER completion and only after that, we jump
1718 * over TRBs by clearing HWO and incrementing dequeue
1719 * pointer.
1720 */
1721 for (i = 0; i < req->num_trbs; i++) {
1722 struct dwc3_trb *trb;
1723
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001724 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001725 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1726 dwc3_ep_inc_deq(dep);
1727 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001728
1729 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001730}
1731
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001732static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1733{
1734 struct dwc3_request *req;
1735 struct dwc3_request *tmp;
1736
1737 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1738 dwc3_gadget_ep_skip_trbs(dep, req);
1739 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1740 }
1741}
1742
Felipe Balbi72246da2011-08-19 18:10:58 +03001743static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1744 struct usb_request *request)
1745{
1746 struct dwc3_request *req = to_dwc3_request(request);
1747 struct dwc3_request *r = NULL;
1748
1749 struct dwc3_ep *dep = to_dwc3_ep(ep);
1750 struct dwc3 *dwc = dep->dwc;
1751
1752 unsigned long flags;
1753 int ret = 0;
1754
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001755 trace_dwc3_ep_dequeue(req);
1756
Felipe Balbi72246da2011-08-19 18:10:58 +03001757 spin_lock_irqsave(&dwc->lock, flags);
1758
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001759 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001761 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001762 }
1763
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001766 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1767 goto out;
1768 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001769 }
1770
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001771 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001773 struct dwc3_request *t;
1774
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001776 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001777
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001778 /*
1779 * Remove any started request if the transfer is
1780 * cancelled.
1781 */
1782 list_for_each_entry_safe(r, t, &dep->started_list, list)
1783 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001784
Thinh Nguyen8907a102021-01-04 22:42:39 -08001785 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1786
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001787 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001788 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001789 }
1790
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001791 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1792 request, ep->name);
1793 ret = -EINVAL;
1794out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 spin_unlock_irqrestore(&dwc->lock, flags);
1796
1797 return ret;
1798}
1799
Felipe Balbi7a608552014-09-24 14:19:52 -05001800int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001801{
1802 struct dwc3_gadget_ep_cmd_params params;
1803 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001804 struct dwc3_request *req;
1805 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 int ret;
1807
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001808 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1809 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1810 return -EINVAL;
1811 }
1812
Felipe Balbi72246da2011-08-19 18:10:58 +03001813 memset(&params, 0x00, sizeof(params));
1814
1815 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001816 struct dwc3_trb *trb;
1817
Felipe Balbie319bd62020-08-13 08:35:38 +03001818 unsigned int transfer_in_flight;
1819 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001820
1821 if (dep->number > 1)
1822 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1823 else
1824 trb = &dwc->ep0_trb[dep->trb_enqueue];
1825
1826 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1827 started = !list_empty(&dep->started_list);
1828
1829 if (!protocol && ((dep->direction && transfer_in_flight) ||
1830 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001831 return -EAGAIN;
1832 }
1833
Felipe Balbi2cd47182016-04-12 16:42:43 +03001834 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1835 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001837 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 dep->name);
1839 else
1840 dep->flags |= DWC3_EP_STALL;
1841 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001842 /*
1843 * Don't issue CLEAR_STALL command to control endpoints. The
1844 * controller automatically clears the STALL when it receives
1845 * the SETUP token.
1846 */
1847 if (dep->number <= 1) {
1848 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1849 return 0;
1850 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001851
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001852 dwc3_stop_active_transfer(dep, true, true);
1853
1854 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1855 dwc3_gadget_move_cancelled_request(req);
1856
1857 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1858 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1859 return 0;
1860 }
1861
1862 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1863
John Youn50c763f2016-05-31 17:49:56 -07001864 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001865 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001866 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001867 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001868 return ret;
1869 }
1870
1871 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1872
Thinh Nguyenc5036722020-09-02 18:42:58 -07001873 if ((dep->flags & DWC3_EP_DELAY_START) &&
1874 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1875 __dwc3_gadget_kick_transfer(dep);
1876
1877 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001879
Felipe Balbi72246da2011-08-19 18:10:58 +03001880 return ret;
1881}
1882
1883static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1884{
1885 struct dwc3_ep *dep = to_dwc3_ep(ep);
1886 struct dwc3 *dwc = dep->dwc;
1887
1888 unsigned long flags;
1889
1890 int ret;
1891
1892 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001893 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001894 spin_unlock_irqrestore(&dwc->lock, flags);
1895
1896 return ret;
1897}
1898
1899static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1900{
1901 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001902 struct dwc3 *dwc = dep->dwc;
1903 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001904 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001905
Paul Zimmerman249a4562012-02-24 17:32:16 -08001906 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001907 dep->flags |= DWC3_EP_WEDGE;
1908
Pratyush Anand08f0d962012-06-25 22:40:43 +05301909 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001910 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301911 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001912 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001913 spin_unlock_irqrestore(&dwc->lock, flags);
1914
1915 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001916}
1917
1918/* -------------------------------------------------------------------------- */
1919
1920static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1921 .bLength = USB_DT_ENDPOINT_SIZE,
1922 .bDescriptorType = USB_DT_ENDPOINT,
1923 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1924};
1925
1926static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1927 .enable = dwc3_gadget_ep0_enable,
1928 .disable = dwc3_gadget_ep0_disable,
1929 .alloc_request = dwc3_gadget_ep_alloc_request,
1930 .free_request = dwc3_gadget_ep_free_request,
1931 .queue = dwc3_gadget_ep0_queue,
1932 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301933 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001934 .set_wedge = dwc3_gadget_ep_set_wedge,
1935};
1936
1937static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1938 .enable = dwc3_gadget_ep_enable,
1939 .disable = dwc3_gadget_ep_disable,
1940 .alloc_request = dwc3_gadget_ep_alloc_request,
1941 .free_request = dwc3_gadget_ep_free_request,
1942 .queue = dwc3_gadget_ep_queue,
1943 .dequeue = dwc3_gadget_ep_dequeue,
1944 .set_halt = dwc3_gadget_ep_set_halt,
1945 .set_wedge = dwc3_gadget_ep_set_wedge,
1946};
1947
1948/* -------------------------------------------------------------------------- */
1949
1950static int dwc3_gadget_get_frame(struct usb_gadget *g)
1951{
1952 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001953
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001954 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001955}
1956
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001957static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001958{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001959 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001960
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001961 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 u32 reg;
1963
Felipe Balbi72246da2011-08-19 18:10:58 +03001964 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001965
Felipe Balbi72246da2011-08-19 18:10:58 +03001966 /*
1967 * According to the Databook Remote wakeup request should
1968 * be issued only when the device is in early suspend state.
1969 *
1970 * We can check that via USB Link State bits in DSTS register.
1971 */
1972 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1973
Felipe Balbi72246da2011-08-19 18:10:58 +03001974 link_state = DWC3_DSTS_USBLNKST(reg);
1975
1976 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001977 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001978 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1979 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyenb624b322021-04-19 19:11:12 -07001980 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
1981 case DWC3_LINK_STATE_U1:
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001982 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 break;
1984 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001985 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 }
1987
Felipe Balbi8598bde2012-01-02 18:55:57 +02001988 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1989 if (ret < 0) {
1990 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001991 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001992 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001993
Paul Zimmerman802fde92012-04-27 13:10:52 +03001994 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001995 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001996 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001997 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001998 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1999 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2000 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002001
Paul Zimmerman1d046792012-02-15 18:56:56 -08002002 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002003 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03002004
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002005 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2007
2008 /* in HS, means ON */
2009 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2010 break;
2011 }
2012
2013 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2014 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002015 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002016 }
2017
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002018 return 0;
2019}
2020
2021static int dwc3_gadget_wakeup(struct usb_gadget *g)
2022{
2023 struct dwc3 *dwc = gadget_to_dwc(g);
2024 unsigned long flags;
2025 int ret;
2026
2027 spin_lock_irqsave(&dwc->lock, flags);
2028 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002029 spin_unlock_irqrestore(&dwc->lock, flags);
2030
2031 return ret;
2032}
2033
2034static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2035 int is_selfpowered)
2036{
2037 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002038 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002039
Paul Zimmerman249a4562012-02-24 17:32:16 -08002040 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002041 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002042 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002043
2044 return 0;
2045}
2046
Wesley Chengae7e8612020-09-28 17:20:59 -07002047static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2048{
2049 u32 epnum;
2050
2051 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2052 struct dwc3_ep *dep;
2053
2054 dep = dwc->eps[epnum];
2055 if (!dep)
2056 continue;
2057
2058 dwc3_remove_requests(dwc, dep);
2059 }
2060}
2061
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002062static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002063{
2064 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002065 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002066
Felipe Balbifc8bb912016-05-16 13:14:48 +03002067 if (pm_runtime_suspended(dwc->dev))
2068 return 0;
2069
Felipe Balbi72246da2011-08-19 18:10:58 +03002070 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002071 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002072 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002073 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2074 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2075 }
2076
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002077 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002078 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2079 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002080
2081 if (dwc->has_hibernation)
2082 reg |= DWC3_DCTL_KEEP_CONNECT;
2083
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002084 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002085 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002086 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002087
2088 if (dwc->has_hibernation && !suspend)
2089 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2090
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002091 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002092 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002093
Thinh Nguyen5b738212019-10-23 19:15:43 -07002094 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002095
2096 do {
2097 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002098 reg &= DWC3_DSTS_DEVCTRLHLT;
2099 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002100
2101 if (!timeout)
2102 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002103
Pratyush Anand6f17f742012-07-02 10:21:55 +05302104 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002105}
2106
Wesley Chengae7e8612020-09-28 17:20:59 -07002107static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2108static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chengdd8363f2020-12-29 15:00:37 -08002109static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002110
Felipe Balbi72246da2011-08-19 18:10:58 +03002111static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2112{
2113 struct dwc3 *dwc = gadget_to_dwc(g);
2114 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302115 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002116
2117 is_on = !!is_on;
2118
Baolin Wangbb014732016-10-14 17:11:33 +08002119 /*
2120 * Per databook, when we want to stop the gadget, if a control transfer
2121 * is still in process, complete it and get the core into setup phase.
2122 */
2123 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2124 reinit_completion(&dwc->ep0_in_setup);
2125
2126 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2127 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2128 if (ret == 0) {
2129 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2130 return -ETIMEDOUT;
2131 }
2132 }
2133
Wesley Chengae7e8612020-09-28 17:20:59 -07002134 /*
Wesley Cheng395d2732020-12-29 15:05:35 -08002135 * Check the return value for successful resume, or error. For a
2136 * successful resume, the DWC3 runtime PM resume routine will handle
2137 * the run stop sequence, so avoid duplicate operations here.
2138 */
2139 ret = pm_runtime_get_sync(dwc->dev);
2140 if (!ret || ret < 0) {
2141 pm_runtime_put(dwc->dev);
2142 return 0;
2143 }
2144
2145 /*
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002146 * Synchronize and disable any further event handling while controller
2147 * is being enabled/disabled.
Wesley Chengae7e8612020-09-28 17:20:59 -07002148 */
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002149 disable_irq(dwc->irq_gadget);
Wesley Chengae7e8612020-09-28 17:20:59 -07002150
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002152
2153 if (!is_on) {
2154 u32 count;
2155
Wesley Chengc7bb96a2021-03-11 15:59:02 -08002156 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002157 /*
2158 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2159 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2160 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2161 * command for any active transfers" before clearing the RunStop
2162 * bit.
2163 */
2164 dwc3_stop_active_transfers(dwc);
2165 __dwc3_gadget_stop(dwc);
2166
2167 /*
2168 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2169 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2170 * "software needs to acknowledge the events that are generated
2171 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2172 * to be set to '1'."
2173 */
2174 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2175 count &= DWC3_GEVNTCOUNT_MASK;
2176 if (count > 0) {
2177 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2178 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2179 dwc->ev_buf->length;
2180 }
Wesley Chengdd8363f2020-12-29 15:00:37 -08002181 } else {
2182 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002183 }
2184
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002185 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002186 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002187 enable_irq(dwc->irq_gadget);
2188
Wesley Cheng395d2732020-12-29 15:05:35 -08002189 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002190
Pratyush Anand6f17f742012-07-02 10:21:55 +05302191 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002192}
2193
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002194static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2195{
2196 u32 reg;
2197
2198 /* Enable all but Start and End of Frame IRQs */
2199 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2200 DWC3_DEVTEN_EVNTOVERFLOWEN |
2201 DWC3_DEVTEN_CMDCMPLTEN |
2202 DWC3_DEVTEN_ERRTICERREN |
2203 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002204 DWC3_DEVTEN_CONNECTDONEEN |
2205 DWC3_DEVTEN_USBRSTEN |
2206 DWC3_DEVTEN_DISCONNEVTEN);
2207
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002208 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002209 reg |= DWC3_DEVTEN_ULSTCNGEN;
2210
Jack Pham45f37f52021-04-28 02:01:10 -07002211 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2212 if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2213 reg |= DWC3_DEVTEN_EOPFEN;
2214
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002215 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2216}
2217
2218static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2219{
2220 /* mask all interrupts */
2221 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2222}
2223
2224static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002225static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002226
Felipe Balbi4e994722016-05-13 14:09:59 +03002227/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002228 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2229 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002230 *
2231 * The following looks like complex but it's actually very simple. In order to
2232 * calculate the number of packets we can burst at once on OUT transfers, we're
2233 * gonna use RxFIFO size.
2234 *
2235 * To calculate RxFIFO size we need two numbers:
2236 * MDWIDTH = size, in bits, of the internal memory bus
2237 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2238 *
2239 * Given these two numbers, the formula is simple:
2240 *
2241 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2242 *
2243 * 24 bytes is for 3x SETUP packets
2244 * 16 bytes is a clock domain crossing tolerance
2245 *
2246 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2247 */
2248static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2249{
2250 u32 ram2_depth;
2251 u32 mdwidth;
2252 u32 nump;
2253 u32 reg;
2254
2255 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2256 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002257 if (DWC3_IP_IS(DWC32))
2258 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002259
2260 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2261 nump = min_t(u32, nump, 16);
2262
2263 /* update NumP */
2264 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2265 reg &= ~DWC3_DCFG_NUMP_MASK;
2266 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2267 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2268}
2269
Felipe Balbid7be2952016-05-04 15:49:37 +03002270static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002271{
Felipe Balbi72246da2011-08-19 18:10:58 +03002272 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002273 int ret = 0;
2274 u32 reg;
2275
John Youncf40b862016-11-14 12:32:43 -08002276 /*
2277 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2278 * the core supports IMOD, disable it.
2279 */
2280 if (dwc->imod_interval) {
2281 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2282 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2283 } else if (dwc3_has_imod(dwc)) {
2284 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2285 }
2286
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002287 /*
2288 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2289 * field instead of letting dwc3 itself calculate that automatically.
2290 *
2291 * This way, we maximize the chances that we'll be able to get several
2292 * bursts of data without going through any sort of endpoint throttling.
2293 */
2294 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002295 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002296 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002297 else
2298 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002299
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002300 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2301
Felipe Balbi4e994722016-05-13 14:09:59 +03002302 dwc3_gadget_setup_nump(dwc);
2303
Felipe Balbi72246da2011-08-19 18:10:58 +03002304 /* Start with SuperSpeed Default */
2305 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2306
2307 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002308 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002309 if (ret) {
2310 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002311 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002312 }
2313
2314 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002315 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002316 if (ret) {
2317 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002318 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002319 }
2320
2321 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002322 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002323 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 dwc3_ep0_out_start(dwc);
2325
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002326 dwc3_gadget_enable_irq(dwc);
2327
Felipe Balbid7be2952016-05-04 15:49:37 +03002328 return 0;
2329
2330err1:
2331 __dwc3_gadget_ep_disable(dwc->eps[0]);
2332
2333err0:
2334 return ret;
2335}
2336
2337static int dwc3_gadget_start(struct usb_gadget *g,
2338 struct usb_gadget_driver *driver)
2339{
2340 struct dwc3 *dwc = gadget_to_dwc(g);
2341 unsigned long flags;
2342 int ret = 0;
2343 int irq;
2344
Roger Quadros9522def2016-06-10 14:48:38 +03002345 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002346 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2347 IRQF_SHARED, "dwc3", dwc->ev_buf);
2348 if (ret) {
2349 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2350 irq, ret);
2351 goto err0;
2352 }
2353
2354 spin_lock_irqsave(&dwc->lock, flags);
2355 if (dwc->gadget_driver) {
2356 dev_err(dwc->dev, "%s is already bound to %s\n",
Peter Chene81a7012020-08-21 10:55:48 +08002357 dwc->gadget->name,
Felipe Balbid7be2952016-05-04 15:49:37 +03002358 dwc->gadget_driver->driver.name);
2359 ret = -EBUSY;
2360 goto err1;
2361 }
2362
2363 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002364 spin_unlock_irqrestore(&dwc->lock, flags);
2365
2366 return 0;
2367
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002368err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002369 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002370 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002371
2372err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 return ret;
2374}
2375
Felipe Balbid7be2952016-05-04 15:49:37 +03002376static void __dwc3_gadget_stop(struct dwc3 *dwc)
2377{
2378 dwc3_gadget_disable_irq(dwc);
2379 __dwc3_gadget_ep_disable(dwc->eps[0]);
2380 __dwc3_gadget_ep_disable(dwc->eps[1]);
2381}
2382
Felipe Balbi22835b82014-10-17 12:05:12 -05002383static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002384{
2385 struct dwc3 *dwc = gadget_to_dwc(g);
2386 unsigned long flags;
2387
2388 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002389 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002390 spin_unlock_irqrestore(&dwc->lock, flags);
2391
Felipe Balbi3f308d12016-05-16 14:17:06 +03002392 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002393
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 return 0;
2395}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002396
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302397static void dwc3_gadget_config_params(struct usb_gadget *g,
2398 struct usb_dcd_config_params *params)
2399{
2400 struct dwc3 *dwc = gadget_to_dwc(g);
2401
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002402 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2403 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2404
2405 /* Recommended BESL */
2406 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002407 /*
2408 * If the recommended BESL baseline is 0 or if the BESL deep is
2409 * less than 2, Microsoft's Windows 10 host usb stack will issue
2410 * a usb reset immediately after it receives the extended BOS
2411 * descriptor and the enumeration will fail. To maintain
2412 * compatibility with the Windows' usb stack, let's set the
2413 * recommended BESL baseline to 1 and clamp the BESL deep to be
2414 * within 2 to 15.
2415 */
2416 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002417 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002418 params->besl_deep =
2419 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002420 }
2421
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302422 /* U1 Device exit Latency */
2423 if (dwc->dis_u1_entry_quirk)
2424 params->bU1devExitLat = 0;
2425 else
2426 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2427
2428 /* U2 Device exit Latency */
2429 if (dwc->dis_u2_entry_quirk)
2430 params->bU2DevExitLat = 0;
2431 else
2432 params->bU2DevExitLat =
2433 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2434}
2435
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002436static void dwc3_gadget_set_speed(struct usb_gadget *g,
2437 enum usb_device_speed speed)
2438{
2439 struct dwc3 *dwc = gadget_to_dwc(g);
2440 unsigned long flags;
2441 u32 reg;
2442
2443 spin_lock_irqsave(&dwc->lock, flags);
2444 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2445 reg &= ~(DWC3_DCFG_SPEED_MASK);
2446
2447 /*
2448 * WORKAROUND: DWC3 revision < 2.20a have an issue
2449 * which would cause metastability state on Run/Stop
2450 * bit if we try to force the IP to USB2-only mode.
2451 *
2452 * Because of that, we cannot configure the IP to any
2453 * speed other than the SuperSpeed
2454 *
2455 * Refers to:
2456 *
2457 * STAR#9000525659: Clock Domain Crossing on DCTL in
2458 * USB 2.0 Mode
2459 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002460 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02002461 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002462 reg |= DWC3_DCFG_SUPERSPEED;
2463 } else {
2464 switch (speed) {
2465 case USB_SPEED_LOW:
2466 reg |= DWC3_DCFG_LOWSPEED;
2467 break;
2468 case USB_SPEED_FULL:
2469 reg |= DWC3_DCFG_FULLSPEED;
2470 break;
2471 case USB_SPEED_HIGH:
2472 reg |= DWC3_DCFG_HIGHSPEED;
2473 break;
2474 case USB_SPEED_SUPER:
2475 reg |= DWC3_DCFG_SUPERSPEED;
2476 break;
2477 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002478 if (DWC3_IP_IS(DWC3))
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002479 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002480 else
2481 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002482 break;
2483 default:
2484 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2485
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002486 if (DWC3_IP_IS(DWC3))
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002487 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002488 else
2489 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002490 }
2491 }
2492 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2493
2494 spin_unlock_irqrestore(&dwc->lock, flags);
2495}
2496
Felipe Balbi72246da2011-08-19 18:10:58 +03002497static const struct usb_gadget_ops dwc3_gadget_ops = {
2498 .get_frame = dwc3_gadget_get_frame,
2499 .wakeup = dwc3_gadget_wakeup,
2500 .set_selfpowered = dwc3_gadget_set_selfpowered,
2501 .pullup = dwc3_gadget_pullup,
2502 .udc_start = dwc3_gadget_start,
2503 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002504 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302505 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002506};
2507
2508/* -------------------------------------------------------------------------- */
2509
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002510static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2511{
2512 struct dwc3 *dwc = dep->dwc;
2513
2514 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2515 dep->endpoint.maxburst = 1;
2516 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2517 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002518 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002519
2520 dep->endpoint.caps.type_control = true;
2521
2522 return 0;
2523}
2524
2525static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2526{
2527 struct dwc3 *dwc = dep->dwc;
2528 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002529 int size;
2530
2531 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002532 if (DWC3_IP_IS(DWC32))
2533 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2534
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002535 /* MDWIDTH is represented in bits, we need it in bytes */
2536 mdwidth /= 8;
2537
2538 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002539 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002540 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002541 else
2542 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002543
2544 /* FIFO Depth is in MDWDITH bytes. Multiply */
2545 size *= mdwidth;
2546
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002547 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002548 * To meet performance requirement, a minimum TxFIFO size of 3x
2549 * MaxPacketSize is recommended for endpoints that support burst and a
2550 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2551 * support burst. Use those numbers and we can calculate the max packet
2552 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002553 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002554 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2555 size /= 3;
2556 else
2557 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002558
2559 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2560
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002561 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002562 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2563 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002564 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002565 dep->endpoint.caps.type_iso = true;
2566 dep->endpoint.caps.type_bulk = true;
2567 dep->endpoint.caps.type_int = true;
2568
2569 return dwc3_alloc_trb_pool(dep);
2570}
2571
2572static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2573{
2574 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002575 int mdwidth;
2576 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002577
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002578 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002579 if (DWC3_IP_IS(DWC32))
2580 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002581
2582 /* MDWIDTH is represented in bits, convert to bytes */
2583 mdwidth /= 8;
2584
2585 /* All OUT endpoints share a single RxFIFO space */
2586 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002587 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002588 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002589 else
2590 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002591
2592 /* FIFO depth is in MDWDITH bytes */
2593 size *= mdwidth;
2594
2595 /*
2596 * To meet performance requirement, a minimum recommended RxFIFO size
2597 * is defined as follow:
2598 * RxFIFO size >= (3 x MaxPacketSize) +
2599 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2600 *
2601 * Then calculate the max packet limit as below.
2602 */
2603 size -= (3 * 8) + 16;
2604 if (size < 0)
2605 size = 0;
2606 else
2607 size /= 3;
2608
2609 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002610 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002611 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2612 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002613 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002614 dep->endpoint.caps.type_iso = true;
2615 dep->endpoint.caps.type_bulk = true;
2616 dep->endpoint.caps.type_int = true;
2617
2618 return dwc3_alloc_trb_pool(dep);
2619}
2620
2621static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002622{
2623 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002624 bool direction = epnum & 1;
2625 int ret;
2626 u8 num = epnum >> 1;
2627
2628 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2629 if (!dep)
2630 return -ENOMEM;
2631
2632 dep->dwc = dwc;
2633 dep->number = epnum;
2634 dep->direction = direction;
2635 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2636 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002637 dep->combo_num = 0;
2638 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002639
2640 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2641 direction ? "in" : "out");
2642
2643 dep->endpoint.name = dep->name;
2644
2645 if (!(dep->number > 1)) {
2646 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2647 dep->endpoint.comp_desc = NULL;
2648 }
2649
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002650 if (num == 0)
2651 ret = dwc3_gadget_init_control_endpoint(dep);
2652 else if (direction)
2653 ret = dwc3_gadget_init_in_endpoint(dep);
2654 else
2655 ret = dwc3_gadget_init_out_endpoint(dep);
2656
2657 if (ret)
2658 return ret;
2659
2660 dep->endpoint.caps.dir_in = direction;
2661 dep->endpoint.caps.dir_out = !direction;
2662
2663 INIT_LIST_HEAD(&dep->pending_list);
2664 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002665 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002666
2667 return 0;
2668}
2669
2670static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2671{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002672 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002673
Peter Chene81a7012020-08-21 10:55:48 +08002674 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002675
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002676 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002677 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002678
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002679 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2680 if (ret)
2681 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 }
2683
2684 return 0;
2685}
2686
2687static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2688{
2689 struct dwc3_ep *dep;
2690 u8 epnum;
2691
2692 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2693 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002694 if (!dep)
2695 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302696 /*
2697 * Physical endpoints 0 and 1 are special; they form the
2698 * bi-directional USB endpoint 0.
2699 *
2700 * For those two physical endpoints, we don't allocate a TRB
2701 * pool nor do we add them the endpoints list. Due to that, we
2702 * shouldn't do these two operations otherwise we would end up
2703 * with all sorts of bugs when removing dwc3.ko.
2704 */
2705 if (epnum != 0 && epnum != 1) {
2706 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302708 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002709
2710 kfree(dep);
2711 }
2712}
2713
Felipe Balbi72246da2011-08-19 18:10:58 +03002714/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002715
Felipe Balbi8f608e82018-03-27 10:53:29 +03002716static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2717 struct dwc3_request *req, struct dwc3_trb *trb,
2718 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302719{
2720 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302721
Felipe Balbidc55c672016-08-12 13:20:32 +03002722 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002723
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002724 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002725 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002726
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002727 /*
2728 * If we're in the middle of series of chained TRBs and we
2729 * receive a short transfer along the way, DWC3 will skip
2730 * through all TRBs including the last TRB in the chain (the
2731 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2732 * bit and SW has to do it manually.
2733 *
2734 * We're going to do that here to avoid problems of HW trying
2735 * to use bogus TRBs for transfers.
2736 */
2737 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2738 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2739
Felipe Balbic6267a52017-01-05 14:58:46 +02002740 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002741 * For isochronous transfers, the first TRB in a service interval must
2742 * have the Isoc-First type. Track and report its interval frame number.
2743 */
2744 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2745 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2746 unsigned int frame_number;
2747
2748 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2749 frame_number &= ~(dep->interval - 1);
2750 req->request.frame_number = frame_number;
2751 }
2752
2753 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002754 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2755 * this TRB points to the bounce buffer address, it's a MPS alignment
2756 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002757 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002758 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2759 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002760 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2761 return 1;
2762 }
2763
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302764 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002765 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302766
Felipe Balbi35b27192017-03-08 13:56:37 +02002767 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2768 return 1;
2769
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002770 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302771 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002772
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002773 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2774 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302775 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002776
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302777 return 0;
2778}
2779
Felipe Balbid3692952018-03-29 13:32:10 +03002780static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2781 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2782 int status)
2783{
2784 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2785 struct scatterlist *sg = req->sg;
2786 struct scatterlist *s;
Thinh Nguyenadccf172021-05-12 20:17:09 -07002787 unsigned int num_queued = req->num_queued_sgs;
Felipe Balbid3692952018-03-29 13:32:10 +03002788 unsigned int i;
2789 int ret = 0;
2790
Thinh Nguyenadccf172021-05-12 20:17:09 -07002791 for_each_sg(sg, s, num_queued, i) {
Felipe Balbid3692952018-03-29 13:32:10 +03002792 trb = &dep->trb_pool[dep->trb_dequeue];
2793
Felipe Balbid3692952018-03-29 13:32:10 +03002794 req->sg = sg_next(s);
Thinh Nguyenadccf172021-05-12 20:17:09 -07002795 req->num_queued_sgs--;
Felipe Balbid3692952018-03-29 13:32:10 +03002796
2797 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2798 trb, event, status, true);
2799 if (ret)
2800 break;
2801 }
2802
2803 return ret;
2804}
2805
2806static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2807 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2808 int status)
2809{
2810 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2811
2812 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2813 event, status, false);
2814}
2815
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002816static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2817{
Thinh Nguyenadccf172021-05-12 20:17:09 -07002818 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002819}
2820
Felipe Balbif38e35d2018-04-06 15:56:35 +03002821static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2822 const struct dwc3_event_depevt *event,
2823 struct dwc3_request *req, int status)
2824{
2825 int ret;
2826
Thinh Nguyenadccf172021-05-12 20:17:09 -07002827 if (req->request.num_mapped_sgs)
Felipe Balbif38e35d2018-04-06 15:56:35 +03002828 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2829 status);
2830 else
2831 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2832 status);
2833
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002834 req->request.actual = req->request.length - req->remaining;
2835
2836 if (!dwc3_gadget_ep_request_completed(req))
2837 goto out;
2838
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002839 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002840 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2841 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002842 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002843 }
2844
Felipe Balbif38e35d2018-04-06 15:56:35 +03002845 dwc3_gadget_giveback(dep, req, status);
2846
2847out:
2848 return ret;
2849}
2850
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002851static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002852 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002853{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002854 struct dwc3_request *req;
2855 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002856
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002857 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002858 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002859
Felipe Balbif38e35d2018-04-06 15:56:35 +03002860 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2861 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002862 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002863 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002864 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002865}
2866
Thinh Nguyend9feef92020-03-31 01:40:42 -07002867static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2868{
2869 struct dwc3_request *req;
2870
2871 if (!list_empty(&dep->pending_list))
2872 return true;
2873
2874 /*
2875 * We only need to check the first entry of the started list. We can
2876 * assume the completed requests are removed from the started list.
2877 */
2878 req = next_request(&dep->started_list);
2879 if (!req)
2880 return false;
2881
2882 return !dwc3_gadget_ep_request_completed(req);
2883}
2884
Felipe Balbiee3638b2018-03-27 11:26:53 +03002885static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2886 const struct dwc3_event_depevt *event)
2887{
Felipe Balbif62afb42018-04-11 10:34:34 +03002888 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002889}
2890
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002891static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2892 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002893{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002894 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002895 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002896
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002897 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002898
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002899 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2900 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002901
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002902 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2903 list_empty(&dep->started_list) &&
2904 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002905 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002906 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002907 if (__dwc3_gadget_kick_transfer(dep) == 0)
2908 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002909
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002910out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002911 /*
2912 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2913 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2914 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002915 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002916 u32 reg;
2917 int i;
2918
2919 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002920 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002921
2922 if (!(dep->flags & DWC3_EP_ENABLED))
2923 continue;
2924
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002925 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002926 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002927 }
2928
2929 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2930 reg |= dwc->u1u2;
2931 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2932
2933 dwc->u1u2 = 0;
2934 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002935
2936 return no_started_trb;
2937}
2938
2939static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2940 const struct dwc3_event_depevt *event)
2941{
2942 int status = 0;
2943
2944 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2945 dwc3_gadget_endpoint_frame_from_event(dep, event);
2946
2947 if (event->status & DEPEVT_STATUS_BUSERR)
2948 status = -ECONNRESET;
2949
2950 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2951 status = -EXDEV;
2952
2953 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002954}
2955
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07002956static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
2957 const struct dwc3_event_depevt *event)
2958{
2959 int status = 0;
2960
2961 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2962
2963 if (event->status & DEPEVT_STATUS_BUSERR)
2964 status = -ECONNRESET;
2965
Thinh Nguyene0d19562020-05-05 19:46:57 -07002966 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
2967 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002968}
2969
Felipe Balbi8f608e82018-03-27 10:53:29 +03002970static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2971 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002972{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002973 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002974
2975 /*
2976 * The XferNotReady event is generated only once before the endpoint
2977 * starts. It will be generated again when END_TRANSFER command is
2978 * issued. For some controller versions, the XferNotReady event may be
2979 * generated while the END_TRANSFER command is still in process. Ignore
2980 * it and wait for the next XferNotReady event after the command is
2981 * completed.
2982 */
2983 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2984 return;
2985
Felipe Balbi25abad62018-08-14 10:41:19 +03002986 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002987}
2988
Thinh Nguyen8266b082020-07-30 16:29:03 -07002989static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
2990 const struct dwc3_event_depevt *event)
2991{
2992 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2993
2994 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
2995 return;
2996
2997 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2998 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2999 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3000
3001 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3002 struct dwc3 *dwc = dep->dwc;
3003
3004 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3005 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3006 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3007
3008 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3009 if (dwc->delayed_status)
3010 __dwc3_gadget_ep0_set_halt(ep0, 1);
3011 return;
3012 }
3013
3014 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3015 if (dwc->delayed_status)
3016 dwc3_ep0_send_delayed_status(dwc);
3017 }
3018
3019 if ((dep->flags & DWC3_EP_DELAY_START) &&
3020 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3021 __dwc3_gadget_kick_transfer(dep);
3022
3023 dep->flags &= ~DWC3_EP_DELAY_START;
3024}
3025
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003026static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3027 const struct dwc3_event_depevt *event)
3028{
3029 struct dwc3 *dwc = dep->dwc;
3030
3031 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3032 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3033 goto out;
3034 }
3035
3036 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3037 switch (event->parameters) {
3038 case DEPEVT_STREAM_PRIME:
3039 /*
3040 * If the host can properly transition the endpoint state from
3041 * idle to prime after a NoStream rejection, there's no need to
3042 * force restarting the endpoint to reinitiate the stream. To
3043 * simplify the check, assume the host follows the USB spec if
3044 * it primed the endpoint more than once.
3045 */
3046 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3047 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3048 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3049 else
3050 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3051 }
3052
3053 break;
3054 case DEPEVT_STREAM_NOSTREAM:
3055 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3056 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3057 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3058 break;
3059
3060 /*
3061 * If the host rejects a stream due to no active stream, by the
3062 * USB and xHCI spec, the endpoint will be put back to idle
3063 * state. When the host is ready (buffer added/updated), it will
3064 * prime the endpoint to inform the usb device controller. This
3065 * triggers the device controller to issue ERDY to restart the
3066 * stream. However, some hosts don't follow this and keep the
3067 * endpoint in the idle state. No prime will come despite host
3068 * streams are updated, and the device controller will not be
3069 * triggered to generate ERDY to move the next stream data. To
3070 * workaround this and maintain compatibility with various
3071 * hosts, force to reinitate the stream until the host is ready
3072 * instead of waiting for the host to prime the endpoint.
3073 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003074 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3075 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3076
3077 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3078 } else {
3079 dep->flags |= DWC3_EP_DELAY_START;
3080 dwc3_stop_active_transfer(dep, true, true);
3081 return;
3082 }
3083 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003084 }
3085
3086out:
3087 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3088}
3089
Felipe Balbi72246da2011-08-19 18:10:58 +03003090static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3091 const struct dwc3_event_depevt *event)
3092{
3093 struct dwc3_ep *dep;
3094 u8 epnum = event->endpoint_number;
3095
3096 dep = dwc->eps[epnum];
3097
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003098 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003099 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003100 return;
3101
3102 /* Handle only EPCMDCMPLT when EP disabled */
3103 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3104 return;
3105 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003106
Felipe Balbi72246da2011-08-19 18:10:58 +03003107 if (epnum == 0 || epnum == 1) {
3108 dwc3_ep0_interrupt(dwc, event);
3109 return;
3110 }
3111
3112 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003113 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003114 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003115 break;
3116 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003117 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003118 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003119 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003120 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003121 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003122 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003123 dwc3_gadget_endpoint_transfer_complete(dep, event);
3124 break;
3125 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003126 dwc3_gadget_endpoint_stream_event(dep, event);
3127 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003128 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003129 break;
3130 }
3131}
3132
3133static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3134{
3135 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3136 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003137 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003138 spin_lock(&dwc->lock);
3139 }
3140}
3141
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003142static void dwc3_suspend_gadget(struct dwc3 *dwc)
3143{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003144 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003145 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003146 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003147 spin_lock(&dwc->lock);
3148 }
3149}
3150
3151static void dwc3_resume_gadget(struct dwc3 *dwc)
3152{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003153 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003154 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003155 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003156 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003157 }
3158}
3159
3160static void dwc3_reset_gadget(struct dwc3 *dwc)
3161{
3162 if (!dwc->gadget_driver)
3163 return;
3164
Peter Chene81a7012020-08-21 10:55:48 +08003165 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003166 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003167 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003168 spin_lock(&dwc->lock);
3169 }
3170}
3171
Felipe Balbic5353b22019-02-13 13:00:54 +02003172static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3173 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003174{
Felipe Balbi72246da2011-08-19 18:10:58 +03003175 struct dwc3_gadget_ep_cmd_params params;
3176 u32 cmd;
3177 int ret;
3178
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003179 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3180 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303181 return;
3182
Pratyush Anand57911502012-07-06 15:19:10 +05303183 /*
3184 * NOTICE: We are violating what the Databook says about the
3185 * EndTransfer command. Ideally we would _always_ wait for the
3186 * EndTransfer Command Completion IRQ, but that's causing too
3187 * much trouble synchronizing between us and gadget driver.
3188 *
3189 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003190 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303191 *
3192 * Note also that a similar handling was tested by Synopsys
3193 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003194 * In short, what we're doing is issuing EndTransfer with
3195 * CMDIOC bit set and delay kicking transfer until the
3196 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003197 *
3198 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3199 * supports a mode to work around the above limitation. The
3200 * software can poll the CMDACT bit in the DEPCMD register
3201 * after issuing a EndTransfer command. This mode is enabled
3202 * by writing GUCTL2[14]. This polling is already done in the
3203 * dwc3_send_gadget_ep_cmd() function so if the mode is
3204 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003205 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003206 *
3207 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303208 */
3209
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303210 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003211 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003212 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003213 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303214 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003215 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303216 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003217 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003218
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003219 /*
3220 * The END_TRANSFER command will cause the controller to generate a
3221 * NoStream Event, and it's not due to the host DP NoStream rejection.
3222 * Ignore the next NoStream event.
3223 */
3224 if (dep->stream_capable)
3225 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3226
Thinh Nguyend3abda52019-11-27 13:10:47 -08003227 if (!interrupt)
3228 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003229 else
3230 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003231}
3232
Felipe Balbi72246da2011-08-19 18:10:58 +03003233static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3234{
3235 u32 epnum;
3236
3237 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3238 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003239 int ret;
3240
3241 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003242 if (!dep)
3243 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003244
3245 if (!(dep->flags & DWC3_EP_STALL))
3246 continue;
3247
3248 dep->flags &= ~DWC3_EP_STALL;
3249
John Youn50c763f2016-05-31 17:49:56 -07003250 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003251 WARN_ON_ONCE(ret);
3252 }
3253}
3254
3255static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3256{
Felipe Balbic4430a22012-05-24 10:30:01 +03003257 int reg;
3258
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003259 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3260
Felipe Balbi72246da2011-08-19 18:10:58 +03003261 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3262 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003263 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003264 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003265
Felipe Balbi72246da2011-08-19 18:10:58 +03003266 dwc3_disconnect_gadget(dwc);
3267
Peter Chene81a7012020-08-21 10:55:48 +08003268 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003269 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003270 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003271
3272 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003273}
3274
Felipe Balbi72246da2011-08-19 18:10:58 +03003275static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3276{
3277 u32 reg;
3278
Felipe Balbidf62df52011-10-14 15:11:49 +03003279 /*
Wesley Cheng45f879b2021-03-19 02:31:25 -07003280 * Ideally, dwc3_reset_gadget() would trigger the function
3281 * drivers to stop any active transfers through ep disable.
3282 * However, for functions which defer ep disable, such as mass
3283 * storage, we will need to rely on the call to stop active
3284 * transfers here, and avoid allowing of request queuing.
3285 */
3286 dwc->connected = false;
3287
3288 /*
Felipe Balbidf62df52011-10-14 15:11:49 +03003289 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3290 * would cause a missing Disconnect Event if there's a
3291 * pending Setup Packet in the FIFO.
3292 *
3293 * There's no suggested workaround on the official Bug
3294 * report, which states that "unless the driver/application
3295 * is doing any special handling of a disconnect event,
3296 * there is no functional issue".
3297 *
3298 * Unfortunately, it turns out that we _do_ some special
3299 * handling of a disconnect event, namely complete all
3300 * pending transfers, notify gadget driver of the
3301 * disconnection, and so on.
3302 *
3303 * Our suggested workaround is to follow the Disconnect
3304 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003305 * flag. Such flag gets set whenever we have a SETUP_PENDING
3306 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003307 * same endpoint.
3308 *
3309 * Refers to:
3310 *
3311 * STAR#9000466709: RTL: Device : Disconnect event not
3312 * generated if setup packet pending in FIFO
3313 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003314 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003315 if (dwc->setup_packet_pending)
3316 dwc3_gadget_disconnect_interrupt(dwc);
3317 }
3318
Felipe Balbi8e744752014-11-06 14:27:53 +08003319 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003320 /*
3321 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3322 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3323 * needs to ensure that it sends "a DEPENDXFER command for any active
3324 * transfers."
3325 */
3326 dwc3_stop_active_transfers(dwc);
Wesley Chengc7bb96a2021-03-11 15:59:02 -08003327 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003328
3329 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3330 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003331 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003332 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003333 dwc3_clear_stall_all_ep(dwc);
3334
3335 /* Reset device address to zero */
3336 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3337 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3338 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003339}
3340
Felipe Balbi72246da2011-08-19 18:10:58 +03003341static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3342{
Felipe Balbi72246da2011-08-19 18:10:58 +03003343 struct dwc3_ep *dep;
3344 int ret;
3345 u32 reg;
3346 u8 speed;
3347
Felipe Balbi72246da2011-08-19 18:10:58 +03003348 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3349 speed = reg & DWC3_DSTS_CONNECTSPD;
3350 dwc->speed = speed;
3351
John Youn5fb6fda2016-11-10 17:23:25 -08003352 /*
3353 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3354 * each time on Connect Done.
3355 *
3356 * Currently we always use the reset value. If any platform
3357 * wants to set this to a different value, we need to add a
3358 * setting and update GCTL.RAMCLKSEL here.
3359 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003360
3361 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003362 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003363 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003364 dwc->gadget->ep0->maxpacket = 512;
3365 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
John Youn75808622016-02-05 17:09:13 -08003366 break;
John Youn2da9ad72016-05-20 16:34:26 -07003367 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003368 /*
3369 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3370 * would cause a missing USB3 Reset event.
3371 *
3372 * In such situations, we should force a USB3 Reset
3373 * event by calling our dwc3_gadget_reset_interrupt()
3374 * routine.
3375 *
3376 * Refers to:
3377 *
3378 * STAR#9000483510: RTL: SS : USB3 reset event may
3379 * not be generated always when the link enters poll
3380 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003381 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003382 dwc3_gadget_reset_interrupt(dwc);
3383
Felipe Balbi72246da2011-08-19 18:10:58 +03003384 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003385 dwc->gadget->ep0->maxpacket = 512;
3386 dwc->gadget->speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03003387 break;
John Youn2da9ad72016-05-20 16:34:26 -07003388 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003389 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003390 dwc->gadget->ep0->maxpacket = 64;
3391 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003392 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003393 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003394 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003395 dwc->gadget->ep0->maxpacket = 64;
3396 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003397 break;
John Youn2da9ad72016-05-20 16:34:26 -07003398 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003399 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003400 dwc->gadget->ep0->maxpacket = 8;
3401 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003402 break;
3403 }
3404
Peter Chene81a7012020-08-21 10:55:48 +08003405 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003406
Pratyush Anand2b758352013-01-14 15:59:31 +05303407 /* Enable USB2 LPM Capability */
3408
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003409 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003410 !dwc->usb2_gadget_lpm_disable &&
John Youn2da9ad72016-05-20 16:34:26 -07003411 (speed != DWC3_DSTS_SUPERSPEED) &&
3412 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303413 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3414 reg |= DWC3_DCFG_LPM_CAP;
3415 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3416
3417 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3418 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3419
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003420 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3421 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303422
Huang Rui80caf7d2014-10-28 19:54:26 +08003423 /*
3424 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3425 * DCFG.LPMCap is set, core responses with an ACK and the
3426 * BESL value in the LPM token is less than or equal to LPM
3427 * NYET threshold.
3428 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003429 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003430 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003431
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003432 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003433 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003434
Thinh Nguyen5b738212019-10-23 19:15:43 -07003435 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003436 } else {
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003437 if (dwc->usb2_gadget_lpm_disable) {
3438 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3439 reg &= ~DWC3_DCFG_LPM_CAP;
3440 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3441 }
3442
Felipe Balbi356363b2013-12-19 16:37:05 -06003443 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3444 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003445 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303446 }
3447
Felipe Balbi72246da2011-08-19 18:10:58 +03003448 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003449 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003450 if (ret) {
3451 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3452 return;
3453 }
3454
3455 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003456 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003457 if (ret) {
3458 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3459 return;
3460 }
3461
3462 /*
3463 * Configure PHY via GUSB3PIPECTLn if required.
3464 *
3465 * Update GTXFIFOSIZn
3466 *
3467 * In both cases reset values should be sufficient.
3468 */
3469}
3470
3471static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3472{
Felipe Balbi72246da2011-08-19 18:10:58 +03003473 /*
3474 * TODO take core out of low power mode when that's
3475 * implemented.
3476 */
3477
Jiebing Liad14d4e2014-12-11 13:26:29 +08003478 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3479 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003480 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003481 spin_lock(&dwc->lock);
3482 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003483}
3484
3485static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3486 unsigned int evtinfo)
3487{
Felipe Balbifae2b902011-10-14 13:00:30 +03003488 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003489 unsigned int pwropt;
3490
3491 /*
3492 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3493 * Hibernation mode enabled which would show up when device detects
3494 * host-initiated U3 exit.
3495 *
3496 * In that case, device will generate a Link State Change Interrupt
3497 * from U3 to RESUME which is only necessary if Hibernation is
3498 * configured in.
3499 *
3500 * There are no functional changes due to such spurious event and we
3501 * just need to ignore it.
3502 *
3503 * Refers to:
3504 *
3505 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3506 * operational mode
3507 */
3508 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003509 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003510 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3511 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3512 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003513 return;
3514 }
3515 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003516
3517 /*
3518 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3519 * on the link partner, the USB session might do multiple entry/exit
3520 * of low power states before a transfer takes place.
3521 *
3522 * Due to this problem, we might experience lower throughput. The
3523 * suggested workaround is to disable DCTL[12:9] bits if we're
3524 * transitioning from U1/U2 to U0 and enable those bits again
3525 * after a transfer completes and there are no pending transfers
3526 * on any of the enabled endpoints.
3527 *
3528 * This is the first half of that workaround.
3529 *
3530 * Refers to:
3531 *
3532 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3533 * core send LGO_Ux entering U0
3534 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003535 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003536 if (next == DWC3_LINK_STATE_U0) {
3537 u32 u1u2;
3538 u32 reg;
3539
3540 switch (dwc->link_state) {
3541 case DWC3_LINK_STATE_U1:
3542 case DWC3_LINK_STATE_U2:
3543 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3544 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3545 | DWC3_DCTL_ACCEPTU2ENA
3546 | DWC3_DCTL_INITU1ENA
3547 | DWC3_DCTL_ACCEPTU1ENA);
3548
3549 if (!dwc->u1u2)
3550 dwc->u1u2 = reg & u1u2;
3551
3552 reg &= ~u1u2;
3553
Thinh Nguyen5b738212019-10-23 19:15:43 -07003554 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003555 break;
3556 default:
3557 /* do nothing */
3558 break;
3559 }
3560 }
3561 }
3562
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003563 switch (next) {
3564 case DWC3_LINK_STATE_U1:
3565 if (dwc->speed == USB_SPEED_SUPER)
3566 dwc3_suspend_gadget(dwc);
3567 break;
3568 case DWC3_LINK_STATE_U2:
3569 case DWC3_LINK_STATE_U3:
3570 dwc3_suspend_gadget(dwc);
3571 break;
3572 case DWC3_LINK_STATE_RESUME:
3573 dwc3_resume_gadget(dwc);
3574 break;
3575 default:
3576 /* do nothing */
3577 break;
3578 }
3579
Felipe Balbie57ebc12014-04-22 13:20:12 -05003580 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003581}
3582
Baolin Wang72704f82016-05-16 16:43:53 +08003583static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3584 unsigned int evtinfo)
3585{
3586 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3587
3588 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3589 dwc3_suspend_gadget(dwc);
3590
3591 dwc->link_state = next;
3592}
3593
Felipe Balbie1dadd32014-02-25 14:47:54 -06003594static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3595 unsigned int evtinfo)
3596{
3597 unsigned int is_ss = evtinfo & BIT(4);
3598
Felipe Balbibfad65e2017-04-19 14:59:27 +03003599 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003600 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3601 * have a known issue which can cause USB CV TD.9.23 to fail
3602 * randomly.
3603 *
3604 * Because of this issue, core could generate bogus hibernation
3605 * events which SW needs to ignore.
3606 *
3607 * Refers to:
3608 *
3609 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3610 * Device Fallback from SuperSpeed
3611 */
3612 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3613 return;
3614
3615 /* enter hibernation here */
3616}
3617
Felipe Balbi72246da2011-08-19 18:10:58 +03003618static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3619 const struct dwc3_event_devt *event)
3620{
3621 switch (event->type) {
3622 case DWC3_DEVICE_EVENT_DISCONNECT:
3623 dwc3_gadget_disconnect_interrupt(dwc);
3624 break;
3625 case DWC3_DEVICE_EVENT_RESET:
3626 dwc3_gadget_reset_interrupt(dwc);
3627 break;
3628 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3629 dwc3_gadget_conndone_interrupt(dwc);
3630 break;
3631 case DWC3_DEVICE_EVENT_WAKEUP:
3632 dwc3_gadget_wakeup_interrupt(dwc);
3633 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003634 case DWC3_DEVICE_EVENT_HIBER_REQ:
3635 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3636 "unexpected hibernation event\n"))
3637 break;
3638
3639 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3640 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003641 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3642 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3643 break;
3644 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003645 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003646 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003647 /*
3648 * Ignore suspend event until the gadget enters into
3649 * USB_STATE_CONFIGURED state.
3650 */
Peter Chene81a7012020-08-21 10:55:48 +08003651 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003652 dwc3_gadget_suspend_interrupt(dwc,
3653 event->event_info);
3654 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003655 break;
3656 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003657 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003658 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003659 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003660 break;
3661 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003662 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003663 }
3664}
3665
3666static void dwc3_process_event_entry(struct dwc3 *dwc,
3667 const union dwc3_event *event)
3668{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003669 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003670
Felipe Balbidfc5e802017-04-26 13:44:51 +03003671 if (!event->type.is_devspec)
3672 dwc3_endpoint_interrupt(dwc, &event->depevt);
3673 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003674 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003675 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003676 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003677}
3678
Felipe Balbidea520a2016-03-30 09:39:34 +03003679static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003680{
Felipe Balbidea520a2016-03-30 09:39:34 +03003681 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003682 irqreturn_t ret = IRQ_NONE;
3683 int left;
3684 u32 reg;
3685
Felipe Balbif42f2442013-06-12 21:25:08 +03003686 left = evt->count;
3687
3688 if (!(evt->flags & DWC3_EVENT_PENDING))
3689 return IRQ_NONE;
3690
3691 while (left > 0) {
3692 union dwc3_event event;
3693
John Younebbb2d52016-11-15 13:07:02 +02003694 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003695
3696 dwc3_process_event_entry(dwc, &event);
3697
3698 /*
3699 * FIXME we wrap around correctly to the next entry as
3700 * almost all entries are 4 bytes in size. There is one
3701 * entry which has 12 bytes which is a regular entry
3702 * followed by 8 bytes data. ATM I don't know how
3703 * things are organized if we get next to the a
3704 * boundary so I worry about that once we try to handle
3705 * that.
3706 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003707 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003708 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003709 }
3710
3711 evt->count = 0;
3712 evt->flags &= ~DWC3_EVENT_PENDING;
3713 ret = IRQ_HANDLED;
3714
3715 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003716 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003717 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003718 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003719
John Youncf40b862016-11-14 12:32:43 -08003720 if (dwc->imod_interval) {
3721 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3722 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3723 }
3724
Felipe Balbif42f2442013-06-12 21:25:08 +03003725 return ret;
3726}
3727
Felipe Balbidea520a2016-03-30 09:39:34 +03003728static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003729{
Felipe Balbidea520a2016-03-30 09:39:34 +03003730 struct dwc3_event_buffer *evt = _evt;
3731 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003732 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003733 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003734
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003735 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003736 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003737 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003738
3739 return ret;
3740}
3741
Felipe Balbidea520a2016-03-30 09:39:34 +03003742static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003743{
Felipe Balbidea520a2016-03-30 09:39:34 +03003744 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003745 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003746 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003747 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003748
Felipe Balbifc8bb912016-05-16 13:14:48 +03003749 if (pm_runtime_suspended(dwc->dev)) {
3750 pm_runtime_get(dwc->dev);
3751 disable_irq_nosync(dwc->irq_gadget);
3752 dwc->pending_events = true;
3753 return IRQ_HANDLED;
3754 }
3755
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003756 /*
3757 * With PCIe legacy interrupt, test shows that top-half irq handler can
3758 * be called again after HW interrupt deassertion. Check if bottom-half
3759 * irq event handler completes before caching new event to prevent
3760 * losing events.
3761 */
3762 if (evt->flags & DWC3_EVENT_PENDING)
3763 return IRQ_HANDLED;
3764
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003765 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003766 count &= DWC3_GEVNTCOUNT_MASK;
3767 if (!count)
3768 return IRQ_NONE;
3769
Felipe Balbib15a7622011-06-30 16:57:15 +03003770 evt->count = count;
3771 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003772
Felipe Balbie8adfc32013-06-12 21:11:14 +03003773 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003774 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003775 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003776 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003777
John Younebbb2d52016-11-15 13:07:02 +02003778 amount = min(count, evt->length - evt->lpos);
3779 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3780
3781 if (amount < count)
3782 memcpy(evt->cache, evt->buf, count - amount);
3783
John Youn65aca322016-11-15 13:08:59 +02003784 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3785
Felipe Balbib15a7622011-06-30 16:57:15 +03003786 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003787}
3788
Felipe Balbidea520a2016-03-30 09:39:34 +03003789static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003790{
Felipe Balbidea520a2016-03-30 09:39:34 +03003791 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003792
Felipe Balbidea520a2016-03-30 09:39:34 +03003793 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003794}
3795
Felipe Balbi6db38122016-10-03 11:27:01 +03003796static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3797{
3798 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3799 int irq;
3800
Hans de Goedef146b402019-10-05 23:04:48 +02003801 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003802 if (irq > 0)
3803 goto out;
3804
3805 if (irq == -EPROBE_DEFER)
3806 goto out;
3807
Hans de Goedef146b402019-10-05 23:04:48 +02003808 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003809 if (irq > 0)
3810 goto out;
3811
3812 if (irq == -EPROBE_DEFER)
3813 goto out;
3814
3815 irq = platform_get_irq(dwc3_pdev, 0);
3816 if (irq > 0)
3817 goto out;
3818
Felipe Balbi6db38122016-10-03 11:27:01 +03003819 if (!irq)
3820 irq = -EINVAL;
3821
3822out:
3823 return irq;
3824}
3825
Peter Chene81a7012020-08-21 10:55:48 +08003826static void dwc_gadget_release(struct device *dev)
3827{
3828 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3829
3830 kfree(gadget);
3831}
3832
Felipe Balbi72246da2011-08-19 18:10:58 +03003833/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003834 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003835 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003836 *
3837 * Returns 0 on success otherwise negative errno.
3838 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003839int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003840{
Felipe Balbi6db38122016-10-03 11:27:01 +03003841 int ret;
3842 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003843 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003844
Felipe Balbi6db38122016-10-03 11:27:01 +03003845 irq = dwc3_gadget_get_irq(dwc);
3846 if (irq < 0) {
3847 ret = irq;
3848 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003849 }
3850
3851 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003852
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303853 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3854 sizeof(*dwc->ep0_trb) * 2,
3855 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003856 if (!dwc->ep0_trb) {
3857 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3858 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003859 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003860 }
3861
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003862 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003863 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003864 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003865 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003866 }
3867
Felipe Balbi905dc042017-01-05 14:46:52 +02003868 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3869 &dwc->bounce_addr, GFP_KERNEL);
3870 if (!dwc->bounce) {
3871 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003872 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003873 }
3874
Baolin Wangbb014732016-10-14 17:11:33 +08003875 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003876 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3877 if (!dwc->gadget) {
3878 ret = -ENOMEM;
3879 goto err3;
3880 }
Baolin Wangbb014732016-10-14 17:11:33 +08003881
Peter Chene81a7012020-08-21 10:55:48 +08003882
3883 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3884 dev = &dwc->gadget->dev;
3885 dev->platform_data = dwc;
3886 dwc->gadget->ops = &dwc3_gadget_ops;
3887 dwc->gadget->speed = USB_SPEED_UNKNOWN;
3888 dwc->gadget->sg_supported = true;
3889 dwc->gadget->name = "dwc3-gadget";
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003890 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03003891
3892 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003893 * FIXME We might be setting max_speed to <SUPER, however versions
3894 * <2.20a of dwc3 have an issue with metastability (documented
3895 * elsewhere in this driver) which tells us we can't set max speed to
3896 * anything lower than SUPER.
3897 *
3898 * Because gadget.max_speed is only used by composite.c and function
3899 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3900 * to happen so we avoid sending SuperSpeed Capability descriptor
3901 * together with our BOS descriptor as that could confuse host into
3902 * thinking we can handle super speed.
3903 *
3904 * Note that, in fact, we won't even support GetBOS requests when speed
3905 * is less than super speed because we don't have means, yet, to tell
3906 * composite.c that we are USB 2.0 + LPM ECN.
3907 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003908 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003909 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003910 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003911 dwc->revision);
3912
Peter Chene81a7012020-08-21 10:55:48 +08003913 dwc->gadget->max_speed = dwc->maximum_speed;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003914
3915 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003916 * REVISIT: Here we should clear all pending IRQs to be
3917 * sure we're starting from a well known location.
3918 */
3919
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003920 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003921 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003922 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003923
3924 ret = usb_add_gadget(dwc->gadget);
3925 if (ret) {
3926 dev_err(dwc->dev, "failed to add gadget\n");
3927 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003928 }
3929
Peter Chene81a7012020-08-21 10:55:48 +08003930 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003931
Felipe Balbi72246da2011-08-19 18:10:58 +03003932 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003933
Peter Chene81a7012020-08-21 10:55:48 +08003934err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003935 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003936err4:
3937 usb_put_gadget(dwc->gadget);
Jack Pham851dee52021-05-28 09:04:05 -07003938 dwc->gadget = NULL;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003939err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003940 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3941 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003942
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003943err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003944 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003945
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003946err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303947 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003948 dwc->ep0_trb, dwc->ep0_trb_addr);
3949
Felipe Balbi72246da2011-08-19 18:10:58 +03003950err0:
3951 return ret;
3952}
3953
Felipe Balbi7415f172012-04-30 14:56:33 +03003954/* -------------------------------------------------------------------------- */
3955
Felipe Balbi72246da2011-08-19 18:10:58 +03003956void dwc3_gadget_exit(struct dwc3 *dwc)
3957{
Jack Pham851dee52021-05-28 09:04:05 -07003958 if (!dwc->gadget)
3959 return;
3960
Jack Pham1ea77502021-05-01 02:35:58 -07003961 usb_del_gadget(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003962 dwc3_gadget_free_endpoints(dwc);
Jack Pham1ea77502021-05-01 02:35:58 -07003963 usb_put_gadget(dwc->gadget);
Felipe Balbi905dc042017-01-05 14:46:52 +02003964 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003965 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003966 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303967 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003968 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003969}
Felipe Balbi7415f172012-04-30 14:56:33 +03003970
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003971int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003972{
Roger Quadros9772b472016-04-12 11:33:29 +03003973 if (!dwc->gadget_driver)
3974 return 0;
3975
Roger Quadros1551e352017-02-15 14:16:26 +02003976 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003977 dwc3_disconnect_gadget(dwc);
3978 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003979
3980 return 0;
3981}
3982
3983int dwc3_gadget_resume(struct dwc3 *dwc)
3984{
Felipe Balbi7415f172012-04-30 14:56:33 +03003985 int ret;
3986
Roger Quadros9772b472016-04-12 11:33:29 +03003987 if (!dwc->gadget_driver)
3988 return 0;
3989
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003990 ret = __dwc3_gadget_start(dwc);
3991 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003992 goto err0;
3993
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003994 ret = dwc3_gadget_run_stop(dwc, true, false);
3995 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003996 goto err1;
3997
Felipe Balbi7415f172012-04-30 14:56:33 +03003998 return 0;
3999
4000err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004001 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004002
4003err0:
4004 return ret;
4005}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004006
4007void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4008{
4009 if (dwc->pending_events) {
4010 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4011 dwc->pending_events = false;
4012 enable_irq(dwc->irq_gadget);
4013 }
4014}