blob: 2174519b7cc5245d7e6fb1d928359ca91eef4732 [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 Nguyen140e2df2021-10-25 16:35:06 -0700313 /*
314 * Initiate remote wakeup if the link state is in U3 when
315 * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
316 * link state is in U1/U2, no remote wakeup is needed. The Start
317 * Transfer command will initiate the link recovery.
318 */
Thinh Nguyenb624b322021-04-19 19:11:12 -0700319 link_state = dwc3_gadget_get_link_state(dwc);
Thinh Nguyen140e2df2021-10-25 16:35:06 -0700320 switch (link_state) {
321 case DWC3_LINK_STATE_U2:
322 if (dwc->gadget->speed >= USB_SPEED_SUPER)
323 break;
324
325 fallthrough;
326 case DWC3_LINK_STATE_U3:
Felipe Balbic36d8e92016-04-04 12:46:33 +0300327 ret = __dwc3_gadget_wakeup(dwc);
328 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
329 ret);
Thinh Nguyen140e2df2021-10-25 16:35:06 -0700330 break;
Felipe Balbic36d8e92016-04-04 12:46:33 +0300331 }
332 }
333
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
335 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
336 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300337
Felipe Balbi8897a762016-09-22 10:56:08 +0300338 /*
339 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
340 * not relying on XferNotReady, we can make use of a special "No
341 * Response Update Transfer" command where we should clear both CmdAct
342 * and CmdIOC bits.
343 *
344 * With this, we don't need to wait for command completion and can
345 * straight away issue further commands to the endpoint.
346 *
347 * NOTICE: We're making an assumption that control endpoints will never
348 * make use of Update Transfer command. This is a safe assumption
349 * because we can never have more than one request at a time with
350 * Control Endpoints. If anybody changes that assumption, this chunk
351 * needs to be updated accordingly.
352 */
353 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
354 !usb_endpoint_xfer_isoc(desc))
355 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
356 else
357 cmd |= DWC3_DEPCMD_CMDACT;
358
359 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300360 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300361 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300362 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300363 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000364
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000365 switch (cmd_status) {
366 case 0:
367 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300368 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000369 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700370 dev_WARN(dwc->dev, "No resource for %s\n",
371 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000372 ret = -EINVAL;
373 break;
374 case DEPEVT_TRANSFER_BUS_EXPIRY:
375 /*
376 * SW issues START TRANSFER command to
377 * isochronous ep with future frame interval. If
378 * future interval time has already passed when
379 * core receives the command, it will respond
380 * with an error status of 'Bus Expiry'.
381 *
382 * Instead of always returning -EINVAL, let's
383 * give a hint to the gadget driver that this is
384 * the case by returning -EAGAIN.
385 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000386 ret = -EAGAIN;
387 break;
388 default:
389 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
390 }
391
Felipe Balbic0ca3242016-04-04 09:11:51 +0300392 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300393 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300394 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
Felipe Balbif6bb2252016-05-23 13:53:34 +0300396 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300397 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300398 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300399 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300400
Felipe Balbi0933df12016-05-23 14:02:33 +0300401 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
402
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700403 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
404 if (ret == 0)
405 dep->flags |= DWC3_EP_TRANSFER_STARTED;
406
407 if (ret != -ETIMEDOUT)
408 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300409 }
410
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700411 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300412 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700413 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300414 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
415 }
416
Felipe Balbic0ca3242016-04-04 09:11:51 +0300417 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300418}
419
John Youn50c763f2016-05-31 17:49:56 -0700420static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
421{
422 struct dwc3 *dwc = dep->dwc;
423 struct dwc3_gadget_ep_cmd_params params;
424 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
425
426 /*
427 * As of core revision 2.60a the recommended programming model
428 * is to set the ClearPendIN bit when issuing a Clear Stall EP
429 * command for IN endpoints. This is to prevent an issue where
430 * some (non-compliant) hosts may not send ACK TPs for pending
431 * IN transfers due to a mishandled error condition. Synopsys
432 * STAR 9000614252.
433 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700434 if (dep->direction &&
435 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800436 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700437 cmd |= DWC3_DEPCMD_CLEARPENDIN;
438
439 memset(&params, 0, sizeof(params));
440
Felipe Balbi2cd47182016-04-12 16:42:43 +0300441 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700442}
443
Felipe Balbi72246da2011-08-19 18:10:58 +0300444static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200445 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300446{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300447 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300448
449 return dep->trb_pool_dma + offset;
450}
451
452static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
453{
454 struct dwc3 *dwc = dep->dwc;
455
456 if (dep->trb_pool)
457 return 0;
458
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530459 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300460 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
461 &dep->trb_pool_dma, GFP_KERNEL);
462 if (!dep->trb_pool) {
463 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
464 dep->name);
465 return -ENOMEM;
466 }
467
468 return 0;
469}
470
471static void dwc3_free_trb_pool(struct dwc3_ep *dep)
472{
473 struct dwc3 *dwc = dep->dwc;
474
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530475 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300476 dep->trb_pool, dep->trb_pool_dma);
477
478 dep->trb_pool = NULL;
479 dep->trb_pool_dma = 0;
480}
481
Felipe Balbi20d1d432018-04-09 12:49:02 +0300482static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
483{
484 struct dwc3_gadget_ep_cmd_params params;
485
486 memset(&params, 0x00, sizeof(params));
487
488 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
489
490 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
491 &params);
492}
John Younc4509602016-02-16 20:10:53 -0800493
494/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300495 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800496 * @dep: endpoint that is being enabled
497 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300498 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
499 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800500 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300501 * The assignment of transfer resources cannot perfectly follow the data book
502 * due to the fact that the controller driver does not have all knowledge of the
503 * configuration in advance. It is given this information piecemeal by the
504 * composite gadget framework after every SET_CONFIGURATION and
505 * SET_INTERFACE. Trying to follow the databook programming model in this
506 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800507 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300508 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
509 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
510 * incorrect in the scenario of multiple interfaces.
511 *
512 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800513 * endpoint on alt setting (8.1.6).
514 *
515 * The following simplified method is used instead:
516 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300517 * All hardware endpoints can be assigned a transfer resource and this setting
518 * will stay persistent until either a core reset or hibernation. So whenever we
519 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
520 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800521 * guaranteed that there are as many transfer resources as endpoints.
522 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300523 * This function is called for each endpoint when it is being enabled but is
524 * triggered only when called for EP0-out, which always happens first, and which
525 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800526 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300527static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300528{
529 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300530 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800532 int i;
533 int ret;
534
535 if (dep->number)
536 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300537
538 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800539 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300540 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
Felipe Balbi2cd47182016-04-12 16:42:43 +0300542 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800543 if (ret)
544 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545
John Younc4509602016-02-16 20:10:53 -0800546 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
547 struct dwc3_ep *dep = dwc->eps[i];
548
549 if (!dep)
550 continue;
551
Felipe Balbib07c2db2018-04-09 12:46:47 +0300552 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800553 if (ret)
554 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 }
556
557 return 0;
558}
559
Felipe Balbib07c2db2018-04-09 12:46:47 +0300560static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300561{
John Youn39ebb052016-11-09 16:36:28 -0800562 const struct usb_ss_ep_comp_descriptor *comp_desc;
563 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300565 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300566
John Youn39ebb052016-11-09 16:36:28 -0800567 comp_desc = dep->endpoint.comp_desc;
568 desc = dep->endpoint.desc;
569
Felipe Balbi72246da2011-08-19 18:10:58 +0300570 memset(&params, 0x00, sizeof(params));
571
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300572 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900573 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
574
575 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800576 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300577 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300578
Felipe Balbi676e3492016-04-26 10:49:07 +0300579 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900580 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
Felipe Balbia2d23f02018-04-09 12:40:48 +0300582 params.param0 |= action;
583 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600584 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600585
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300586 if (usb_endpoint_xfer_control(desc))
587 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300588
589 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
590 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200592 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300593 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700594 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300595 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300596 dep->stream_capable = true;
597 }
598
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500599 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300600 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601
602 /*
603 * We are doing 1:1 mapping for endpoints, meaning
604 * Physical Endpoints 2 maps to Logical Endpoint 2 and
605 * so on. We consider the direction bit as part of the physical
606 * endpoint number. So USB endpoint 0x81 is 0x03.
607 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300608 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609
610 /*
611 * We must use the lower 16 TX FIFOs even though
612 * HW might have more
613 */
614 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300615 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 if (desc->bInterval) {
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800618 u8 bInterval_m1;
619
620 /*
Thinh Nguyenf9ddfaa2021-04-15 00:41:58 -0700621 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
622 *
623 * NOTE: The programming guide incorrectly stated bInterval_m1
624 * must be set to 0 when operating in fullspeed. Internally the
625 * controller does not have this limitation. See DWC_usb3x
626 * programming guide section 3.2.2.1.
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800627 */
628 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800629
Thinh Nguyen5b4cd962021-02-08 13:53:16 -0800630 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
631 dwc->gadget->speed == USB_SPEED_FULL)
632 dep->interval = desc->bInterval;
633 else
634 dep->interval = 1 << (desc->bInterval - 1);
635
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800636 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 }
638
Felipe Balbi2cd47182016-04-12 16:42:43 +0300639 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300640}
641
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700642static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
643 bool interrupt);
644
Felipe Balbi72246da2011-08-19 18:10:58 +0300645/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300646 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300648 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300650 * Caller should take care of locking. Execute all necessary commands to
651 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300652 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300653static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300654{
John Youn39ebb052016-11-09 16:36:28 -0800655 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800657
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300659 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300660
661 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300662 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 if (ret)
664 return ret;
665 }
666
Felipe Balbib07c2db2018-04-09 12:46:47 +0300667 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 if (ret)
669 return ret;
670
671 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200672 struct dwc3_trb *trb_st_hw;
673 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300674
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 dep->type = usb_endpoint_type(desc);
676 dep->flags |= DWC3_EP_ENABLED;
677
678 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
679 reg |= DWC3_DALEPENA_EP(dep->number);
680 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
681
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300682 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200683 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300684
John Youn0d257442016-05-19 17:26:08 -0700685 /* Initialize the TRB ring */
686 dep->trb_dequeue = 0;
687 dep->trb_enqueue = 0;
688 memset(dep->trb_pool, 0,
689 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
690
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300691 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 trb_st_hw = &dep->trb_pool[0];
693
Felipe Balbif6bafc62012-02-06 11:04:53 +0200694 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200695 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
696 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
697 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
698 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300699 }
700
Felipe Balbia97ea992016-09-29 16:28:56 +0300701 /*
702 * Issue StartTransfer here with no-op TRB so we can always rely on No
703 * Response Update Transfer command.
704 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700705 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300706 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300707 struct dwc3_gadget_ep_cmd_params params;
708 struct dwc3_trb *trb;
709 dma_addr_t trb_dma;
710 u32 cmd;
711
712 memset(&params, 0, sizeof(params));
713 trb = &dep->trb_pool[0];
714 trb_dma = dwc3_trb_dma_offset(dep, trb);
715
716 params.param0 = upper_32_bits(trb_dma);
717 params.param1 = lower_32_bits(trb_dma);
718
719 cmd = DWC3_DEPCMD_STARTTRANSFER;
720
721 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
722 if (ret < 0)
723 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700724
725 if (dep->stream_capable) {
726 /*
727 * For streams, at start, there maybe a race where the
728 * host primes the endpoint before the function driver
729 * queues a request to initiate a stream. In that case,
730 * the controller will not see the prime to generate the
731 * ERDY and start stream. To workaround this, issue a
732 * no-op TRB as normal, but end it immediately. As a
733 * result, when the function driver queues the request,
734 * the next START_TRANSFER command will cause the
735 * controller to generate an ERDY to initiate the
736 * stream.
737 */
738 dwc3_stop_active_transfer(dep, true, true);
739
740 /*
741 * All stream eps will reinitiate stream on NoStream
742 * rejection until we can determine that the host can
743 * prime after the first transfer.
744 */
745 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
746 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300747 }
748
Felipe Balbi2870e502016-11-03 13:53:29 +0200749out:
750 trace_dwc3_gadget_ep_enable(dep);
751
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 return 0;
753}
754
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200755static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300756{
757 struct dwc3_request *req;
758
Felipe Balbic5353b22019-02-13 13:00:54 +0200759 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300760
Felipe Balbi0e146022016-06-21 10:32:02 +0300761 /* - giveback all requests to gadget driver */
762 while (!list_empty(&dep->started_list)) {
763 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200764
Felipe Balbi0e146022016-06-21 10:32:02 +0300765 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200766 }
767
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200768 while (!list_empty(&dep->pending_list)) {
769 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300770
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200771 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200773
774 while (!list_empty(&dep->cancelled_list)) {
775 req = next_request(&dep->cancelled_list);
776
777 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
778 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300779}
780
781/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300782 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300783 * @dep: the endpoint to disable
784 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300785 * This function undoes what __dwc3_gadget_ep_enable did and also removes
786 * requests which are currently being processed by the hardware and those which
787 * are not yet scheduled.
788 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200789 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300791static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
792{
793 struct dwc3 *dwc = dep->dwc;
794 u32 reg;
795
Felipe Balbi2870e502016-11-03 13:53:29 +0200796 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500797
Felipe Balbi687ef982014-04-16 10:30:33 -0500798 /* make sure HW endpoint isn't stalled */
799 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500800 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500801
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
803 reg &= ~DWC3_DALEPENA_EP(dep->number);
804 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
805
John Youn39ebb052016-11-09 16:36:28 -0800806 /* Clear out the ep descriptors for non-ep0 */
807 if (dep->number > 1) {
808 dep->endpoint.comp_desc = NULL;
809 dep->endpoint.desc = NULL;
810 }
811
Wesley Chengc7bb96a2021-03-11 15:59:02 -0800812 dwc3_remove_requests(dwc, dep);
813
Wesley Cheng996a5782021-03-24 11:31:04 -0700814 dep->stream_capable = false;
815 dep->type = 0;
816 dep->flags = 0;
817
Felipe Balbi72246da2011-08-19 18:10:58 +0300818 return 0;
819}
820
821/* -------------------------------------------------------------------------- */
822
823static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
824 const struct usb_endpoint_descriptor *desc)
825{
826 return -EINVAL;
827}
828
829static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
830{
831 return -EINVAL;
832}
833
834/* -------------------------------------------------------------------------- */
835
836static int dwc3_gadget_ep_enable(struct usb_ep *ep,
837 const struct usb_endpoint_descriptor *desc)
838{
839 struct dwc3_ep *dep;
840 struct dwc3 *dwc;
841 unsigned long flags;
842 int ret;
843
844 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
845 pr_debug("dwc3: invalid parameters\n");
846 return -EINVAL;
847 }
848
849 if (!desc->wMaxPacketSize) {
850 pr_debug("dwc3: missing wMaxPacketSize\n");
851 return -EINVAL;
852 }
853
854 dep = to_dwc3_ep(ep);
855 dwc = dep->dwc;
856
Felipe Balbi95ca9612015-12-10 13:08:20 -0600857 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
858 "%s is already enabled\n",
859 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300860 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300861
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300863 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300864 spin_unlock_irqrestore(&dwc->lock, flags);
865
866 return ret;
867}
868
869static int dwc3_gadget_ep_disable(struct usb_ep *ep)
870{
871 struct dwc3_ep *dep;
872 struct dwc3 *dwc;
873 unsigned long flags;
874 int ret;
875
876 if (!ep) {
877 pr_debug("dwc3: invalid parameters\n");
878 return -EINVAL;
879 }
880
881 dep = to_dwc3_ep(ep);
882 dwc = dep->dwc;
883
Felipe Balbi95ca9612015-12-10 13:08:20 -0600884 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
885 "%s is already disabled\n",
886 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300887 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300888
Felipe Balbi72246da2011-08-19 18:10:58 +0300889 spin_lock_irqsave(&dwc->lock, flags);
890 ret = __dwc3_gadget_ep_disable(dep);
891 spin_unlock_irqrestore(&dwc->lock, flags);
892
893 return ret;
894}
895
896static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300897 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300898{
899 struct dwc3_request *req;
900 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
902 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900903 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300904 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300905
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300906 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300907 req->epnum = dep->number;
908 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200909 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300910
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500911 trace_dwc3_alloc_request(req);
912
Felipe Balbi72246da2011-08-19 18:10:58 +0300913 return &req->request;
914}
915
916static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
917 struct usb_request *request)
918{
919 struct dwc3_request *req = to_dwc3_request(request);
920
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500921 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300922 kfree(req);
923}
924
Felipe Balbi42626912018-04-09 13:01:43 +0300925/**
926 * dwc3_ep_prev_trb - returns the previous TRB in the ring
927 * @dep: The endpoint with the TRB ring
928 * @index: The index of the current TRB in the ring
929 *
930 * Returns the TRB prior to the one pointed to by the index. If the
931 * index is 0, we will wrap backwards, skip the link TRB, and return
932 * the one just before that.
933 */
934static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
935{
936 u8 tmp = index;
937
938 if (!tmp)
939 tmp = DWC3_TRB_NUM - 1;
940
941 return &dep->trb_pool[tmp - 1];
942}
943
944static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
945{
Felipe Balbi42626912018-04-09 13:01:43 +0300946 u8 trbs_left;
947
948 /*
Thinh Nguyen87b20162021-08-19 03:17:03 +0200949 * If the enqueue & dequeue are equal then the TRB ring is either full
950 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
951 * pending to be processed by the driver.
Felipe Balbi42626912018-04-09 13:01:43 +0300952 */
953 if (dep->trb_enqueue == dep->trb_dequeue) {
Thinh Nguyen87b20162021-08-19 03:17:03 +0200954 /*
955 * If there is any request remained in the started_list at
956 * this point, that means there is no TRB available.
957 */
958 if (!list_empty(&dep->started_list))
Felipe Balbi42626912018-04-09 13:01:43 +0300959 return 0;
960
961 return DWC3_TRB_NUM - 1;
962 }
963
964 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
965 trbs_left &= (DWC3_TRB_NUM - 1);
966
967 if (dep->trb_dequeue < dep->trb_enqueue)
968 trbs_left--;
969
970 return trbs_left;
971}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300972
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200973static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300974 dma_addr_t dma, unsigned int length, unsigned int chain,
975 unsigned int node, unsigned int stream_id,
976 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700977 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200978{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300979 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800980 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300981 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200982
Felipe Balbif6bafc62012-02-06 11:04:53 +0200983 trb->size = DWC3_TRB_SIZE_LENGTH(length);
984 trb->bpl = lower_32_bits(dma);
985 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200986
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200987 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200988 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200989 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200990 break;
991
992 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300993 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530994 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300995
Manu Gautam40d829f2017-07-19 17:07:10 +0530996 /*
997 * USB Specification 2.0 Section 5.9.2 states that: "If
998 * there is only a single transaction in the microframe,
999 * only a DATA0 data packet PID is used. If there are
1000 * two transactions per microframe, DATA1 is used for
1001 * the first transaction data packet and DATA0 is used
1002 * for the second transaction data packet. If there are
1003 * three transactions per microframe, DATA2 is used for
1004 * the first transaction data packet, DATA1 is used for
1005 * the second, and DATA0 is used for the third."
1006 *
1007 * IOW, we should satisfy the following cases:
1008 *
1009 * 1) length <= maxpacket
1010 * - DATA0
1011 *
1012 * 2) maxpacket < length <= (2 * maxpacket)
1013 * - DATA1, DATA0
1014 *
1015 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1016 * - DATA2, DATA1, DATA0
1017 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001018 if (speed == USB_SPEED_HIGH) {
1019 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301020 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301021 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1022
1023 if (length <= (2 * maxp))
1024 mult--;
1025
1026 if (length <= maxp)
1027 mult--;
1028
1029 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001030 }
1031 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301032 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001033 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001034
1035 /* always enable Interrupt on Missed ISOC */
1036 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001037 break;
1038
1039 case USB_ENDPOINT_XFER_BULK:
1040 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001041 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001042 break;
1043 default:
1044 /*
1045 * This is only possible with faulty memory because we
1046 * checked it already :)
1047 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001048 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1049 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001050 }
1051
Tejas Joglekar244add82018-12-10 16:08:13 +05301052 /*
1053 * Enable Continue on Short Packet
1054 * when endpoint is not a stream capable
1055 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001056 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301057 if (!dep->stream_capable)
1058 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001059
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001060 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001061 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1062 }
1063
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001064 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001065 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001066
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301067 if (chain)
1068 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001069 else if (dep->stream_capable && is_last)
1070 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301071
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001072 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001073 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001074
1075 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001076
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301077 dwc3_ep_inc_enq(dep);
1078
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001079 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001080}
1081
John Youn361572b2016-05-19 17:26:17 -07001082/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001083 * dwc3_prepare_one_trb - setup one TRB from one request
1084 * @dep: endpoint for which this request is prepared
1085 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001086 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001087 * @chain: should this TRB be chained to the next?
1088 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001089 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001090 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001091 */
1092static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001093 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001094 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1095 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001096{
1097 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301098 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001099 unsigned int stream_id = req->request.stream_id;
1100 unsigned int short_not_ok = req->request.short_not_ok;
1101 unsigned int no_interrupt = req->request.no_interrupt;
1102 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301103
Thinh Nguyen2b803572020-09-24 01:21:30 -07001104 if (use_bounce_buffer)
1105 dma = dep->dwc->bounce_addr;
1106 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301107 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001108 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301109 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001110
1111 trb = &dep->trb_pool[dep->trb_enqueue];
1112
1113 if (!req->trb) {
1114 dwc3_gadget_move_started_request(req);
1115 req->trb = trb;
1116 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001117 }
1118
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001119 req->num_trbs++;
1120
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001121 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001122 stream_id, short_not_ok, no_interrupt, is_last,
1123 must_interrupt);
1124}
1125
1126static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1127{
1128 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1129 unsigned int rem = req->request.length % maxp;
1130
1131 if ((req->request.length && req->request.zero && !rem &&
1132 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1133 (!req->direction && rem))
1134 return true;
1135
1136 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001137}
1138
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001139/**
1140 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1141 * @dep: The endpoint that the request belongs to
1142 * @req: The request to prepare
1143 * @entry_length: The last SG entry size
1144 * @node: Indicates whether this is not the first entry (for isoc only)
1145 *
1146 * Return the number of TRBs prepared.
1147 */
1148static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1149 struct dwc3_request *req, unsigned int entry_length,
1150 unsigned int node)
1151{
1152 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1153 unsigned int rem = req->request.length % maxp;
1154 unsigned int num_trbs = 1;
1155
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001156 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001157 num_trbs++;
1158
1159 if (dwc3_calc_trbs_left(dep) < num_trbs)
1160 return 0;
1161
1162 req->needs_extra_trb = num_trbs > 1;
1163
1164 /* Prepare a normal TRB */
1165 if (req->direction || req->request.length)
1166 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001167 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001168
1169 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1170 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1171 dwc3_prepare_one_trb(dep, req,
1172 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001173 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001174
1175 return num_trbs;
1176}
1177
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001178static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001179 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001180{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301181 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001182 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001183 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001184 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301185 unsigned int remaining = req->request.num_mapped_sgs
1186 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001187 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001188 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301189
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001190 /*
1191 * If we resume preparing the request, then get the remaining length of
1192 * the request and resume where we left off.
1193 */
1194 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1195 length -= sg_dma_len(s);
1196
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301197 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001198 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001199 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001200 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001201 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001202
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001203 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1204
1205 length -= trb_length;
1206
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001207 /*
1208 * IOMMU driver is coalescing the list of sgs which shares a
1209 * page boundary into one and giving it to USB driver. With
1210 * this the number of sgs mapped is not equal to the number of
1211 * sgs passed. So mark the chain bit to false if it isthe last
1212 * mapped sg.
1213 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001214 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001215 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001216
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001217 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001218 break;
1219
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001220 if (last_sg) {
1221 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001222 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001223 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001224 /*
1225 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001226 * next SG entry. If not, set interrupt on this TRB to
1227 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001228 * free.
1229 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001230 if (num_trbs_left == 1 || (needs_extra_trb &&
1231 num_trbs_left <= 2 &&
1232 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001233 must_interrupt = true;
1234
1235 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1236 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001237 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001238
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301239 /*
1240 * There can be a situation where all sgs in sglist are not
1241 * queued because of insufficient trb number. To handle this
1242 * case, update start_sg to next sg to be queued, so that
1243 * we have free trbs we can continue queuing from where we
1244 * previously stopped
1245 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001246 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301247 req->start_sg = sg_next(s);
1248
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301249 req->num_queued_sgs++;
Thinh Nguyenadccf172021-05-12 20:17:09 -07001250 req->num_pending_sgs--;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301251
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001252 /*
1253 * The number of pending SG entries may not correspond to the
1254 * number of mapped SG entries. If all the data are queued, then
1255 * don't include unused SG entries.
1256 */
1257 if (length == 0) {
Thinh Nguyenadccf172021-05-12 20:17:09 -07001258 req->num_pending_sgs = 0;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001259 break;
1260 }
1261
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001262 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001263 break;
1264 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001265
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001266 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001267}
1268
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001269static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001270 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001271{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001272 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001273}
1274
Felipe Balbi72246da2011-08-19 18:10:58 +03001275/*
1276 * dwc3_prepare_trbs - setup TRBs from requests
1277 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001278 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001279 * The function goes through the requests list and sets up TRBs for the
1280 * transfers. The function returns once there are no more TRBs available or
1281 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001282 *
1283 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001285static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001286{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001287 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001288 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001289
1290 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1291
Felipe Balbid86c5a62016-10-25 13:48:52 +03001292 /*
1293 * We can get in a situation where there's a request in the started list
1294 * but there weren't enough TRBs to fully kick it in the first time
1295 * around, so it has been waiting for more TRBs to be freed up.
1296 *
1297 * In that case, we should check if we have a request with pending_sgs
1298 * in the started list and prepare TRBs for that request first,
1299 * otherwise we will prepare TRBs completely out of order and that will
1300 * break things.
1301 */
1302 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001303 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001304 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001305 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001306 return ret;
1307 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001308
1309 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001310 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001311
1312 /*
1313 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1314 * burst capability may try to read and use TRBs beyond the
1315 * active transfer instead of stopping.
1316 */
1317 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001318 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001319 }
1320
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001321 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001322 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001323
1324 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1325 dep->direction);
1326 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001327 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001328
1329 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301330 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301331 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001332 req->num_pending_sgs = req->request.num_mapped_sgs;
1333
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001334 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001335 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001336 if (req->num_pending_sgs)
1337 return ret;
1338 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001339 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001340 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001341
Thinh Nguyen490410b2020-09-24 01:21:55 -07001342 if (!ret || !dwc3_calc_trbs_left(dep))
1343 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001344
1345 /*
1346 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1347 * burst capability may try to read and use TRBs beyond the
1348 * active transfer instead of stopping.
1349 */
1350 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001351 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001353
1354 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001355}
1356
Thinh Nguyen8d990872020-03-29 16:12:57 -07001357static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1358
Felipe Balbi7fdca762017-09-05 14:41:34 +03001359static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001360{
1361 struct dwc3_gadget_ep_cmd_params params;
1362 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001363 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001364 int ret;
1365 u32 cmd;
1366
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001367 /*
1368 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1369 * This happens when we need to stop and restart a transfer such as in
1370 * the case of reinitiating a stream or retrying an isoc transfer.
1371 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001372 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001373 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001374 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001375
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001376 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001377
Thinh Nguyen23384842020-09-30 17:44:38 -07001378 /*
1379 * If there's no new TRB prepared and we don't need to restart a
1380 * transfer, there's no need to update the transfer.
1381 */
1382 if (!ret && !starting)
1383 return ret;
1384
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001385 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001386 if (!req) {
1387 dep->flags |= DWC3_EP_PENDING_REQUEST;
1388 return 0;
1389 }
1390
1391 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001392
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001393 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301394 params.param0 = upper_32_bits(req->trb_dma);
1395 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001396 cmd = DWC3_DEPCMD_STARTTRANSFER;
1397
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301398 if (dep->stream_capable)
1399 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1400
Felipe Balbi7fdca762017-09-05 14:41:34 +03001401 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1402 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301403 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001404 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1405 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301406 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001407
Felipe Balbi2cd47182016-04-12 16:42:43 +03001408 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001409 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001410 struct dwc3_request *tmp;
1411
1412 if (ret == -EAGAIN)
1413 return ret;
1414
1415 dwc3_stop_active_transfer(dep, true, true);
1416
1417 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1418 dwc3_gadget_move_cancelled_request(req);
1419
1420 /* If ep isn't started, then there's no end transfer pending */
1421 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1422 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1423
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 return ret;
1425 }
1426
Thinh Nguyene0d19562020-05-05 19:46:57 -07001427 if (dep->stream_capable && req->request.is_last)
1428 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1429
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 return 0;
1431}
1432
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001433static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1434{
1435 u32 reg;
1436
1437 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1438 return DWC3_DSTS_SOFFN(reg);
1439}
1440
Thinh Nguyend92021f2018-11-14 22:56:54 -08001441/**
1442 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1443 * @dep: isoc endpoint
1444 *
1445 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1446 * microframe number reported by the XferNotReady event for the future frame
1447 * number to start the isoc transfer.
1448 *
1449 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1450 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1451 * XferNotReady event are invalid. The driver uses this number to schedule the
1452 * isochronous transfer and passes it to the START TRANSFER command. Because
1453 * this number is invalid, the command may fail. If BIT[15:14] matches the
1454 * internal 16-bit microframe, the START TRANSFER command will pass and the
1455 * transfer will start at the scheduled time, if it is off by 1, the command
1456 * will still pass, but the transfer will start 2 seconds in the future. For all
1457 * other conditions, the START TRANSFER command will fail with bus-expiry.
1458 *
1459 * In order to workaround this issue, we can test for the correct combination of
1460 * BIT[15:14] by sending START TRANSFER commands with different values of
1461 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1462 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1463 * As the result, within the 4 possible combinations for BIT[15:14], there will
1464 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1465 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1466 * value is the correct combination.
1467 *
1468 * Since there are only 4 outcomes and the results are ordered, we can simply
1469 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1470 * deduce the smaller successful combination.
1471 *
1472 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1473 * of BIT[15:14]. The correct combination is as follow:
1474 *
1475 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1476 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1477 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1478 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1479 *
1480 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1481 * endpoints.
1482 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001483static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301484{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001485 int cmd_status = 0;
1486 bool test0;
1487 bool test1;
1488
1489 while (dep->combo_num < 2) {
1490 struct dwc3_gadget_ep_cmd_params params;
1491 u32 test_frame_number;
1492 u32 cmd;
1493
1494 /*
1495 * Check if we can start isoc transfer on the next interval or
1496 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1497 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001498 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001499 test_frame_number |= dep->combo_num << 14;
1500 test_frame_number += max_t(u32, 4, dep->interval);
1501
1502 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1503 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1504
1505 cmd = DWC3_DEPCMD_STARTTRANSFER;
1506 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1507 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1508
1509 /* Redo if some other failure beside bus-expiry is received */
1510 if (cmd_status && cmd_status != -EAGAIN) {
1511 dep->start_cmd_status = 0;
1512 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001513 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001514 }
1515
1516 /* Store the first test status */
1517 if (dep->combo_num == 0)
1518 dep->start_cmd_status = cmd_status;
1519
1520 dep->combo_num++;
1521
1522 /*
1523 * End the transfer if the START_TRANSFER command is successful
1524 * to wait for the next XferNotReady to test the command again
1525 */
1526 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001527 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001528 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001529 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301530 }
1531
Thinh Nguyend92021f2018-11-14 22:56:54 -08001532 /* test0 and test1 are both completed at this point */
1533 test0 = (dep->start_cmd_status == 0);
1534 test1 = (cmd_status == 0);
1535
1536 if (!test0 && test1)
1537 dep->combo_num = 1;
1538 else if (!test0 && !test1)
1539 dep->combo_num = 2;
1540 else if (test0 && !test1)
1541 dep->combo_num = 3;
1542 else if (test0 && test1)
1543 dep->combo_num = 0;
1544
Michael Grzeschikca143782020-07-01 20:24:51 +02001545 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001546 dep->frame_number |= dep->combo_num << 14;
1547 dep->frame_number += max_t(u32, 4, dep->interval);
1548
1549 /* Reinitialize test variables */
1550 dep->start_cmd_status = 0;
1551 dep->combo_num = 0;
1552
Felipe Balbi25abad62018-08-14 10:41:19 +03001553 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001554}
1555
Felipe Balbi25abad62018-08-14 10:41:19 +03001556static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301557{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001558 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001559 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001560 int ret;
1561 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001562
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001563 if (list_empty(&dep->pending_list) &&
1564 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301565 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001566 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301567 }
1568
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001569 if (!dwc->dis_start_transfer_quirk &&
1570 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1571 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001572 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001573 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001574 }
1575
Michael Olbrichc5a70922020-07-01 20:24:52 +02001576 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001577 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001578 u32 frame = __dwc3_gadget_get_frame(dwc);
1579 bool rollover = frame <
1580 (dep->frame_number & DWC3_FRNUMBER_MASK);
1581
1582 /*
1583 * frame_number is set from XferNotReady and may be already
1584 * out of date. DSTS only provides the lower 14 bit of the
1585 * current frame number. So add the upper two bits of
1586 * frame_number and handle a possible rollover.
1587 * This will provide the correct frame_number unless more than
1588 * rollover has happened since XferNotReady.
1589 */
1590
1591 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1592 frame;
1593 if (rollover)
1594 dep->frame_number += BIT(14);
1595 }
1596
Felipe Balbid5370102018-08-14 10:42:43 +03001597 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1598 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1599
1600 ret = __dwc3_gadget_kick_transfer(dep);
1601 if (ret != -EAGAIN)
1602 break;
1603 }
1604
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001605 /*
1606 * After a number of unsuccessful start attempts due to bus-expiry
1607 * status, issue END_TRANSFER command and retry on the next XferNotReady
1608 * event.
1609 */
1610 if (ret == -EAGAIN) {
1611 struct dwc3_gadget_ep_cmd_params params;
1612 u32 cmd;
1613
1614 cmd = DWC3_DEPCMD_ENDTRANSFER |
1615 DWC3_DEPCMD_CMDIOC |
1616 DWC3_DEPCMD_PARAM(dep->resource_index);
1617
1618 dep->resource_index = 0;
1619 memset(&params, 0, sizeof(params));
1620
1621 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1622 if (!ret)
1623 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1624 }
1625
Felipe Balbid5370102018-08-14 10:42:43 +03001626 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301627}
1628
Felipe Balbi72246da2011-08-19 18:10:58 +03001629static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1630{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001631 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001632
Wesley Chengc7bb96a2021-03-11 15:59:02 -08001633 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001634 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1635 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001636 return -ESHUTDOWN;
1637 }
1638
Felipe Balbi04fb3652017-05-17 15:57:45 +03001639 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1640 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001641 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001642
Felipe Balbib2b6d602019-01-11 12:58:52 +02001643 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1644 "%s: request %pK already in flight\n",
1645 dep->name, &req->request))
1646 return -EINVAL;
1647
Felipe Balbifc8bb912016-05-16 13:14:48 +03001648 pm_runtime_get(dwc->dev);
1649
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 req->request.actual = 0;
1651 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001652
Felipe Balbife84f522015-09-01 09:01:38 -05001653 trace_dwc3_ep_queue(req);
1654
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001655 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001656 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001657
Thinh Nguyene0d19562020-05-05 19:46:57 -07001658 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1659 return 0;
1660
Thinh Nguyenc5036722020-09-02 18:42:58 -07001661 /*
1662 * Start the transfer only after the END_TRANSFER is completed
1663 * and endpoint STALL is cleared.
1664 */
1665 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1666 (dep->flags & DWC3_EP_WEDGE) ||
1667 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001668 dep->flags |= DWC3_EP_DELAY_START;
1669 return 0;
1670 }
1671
Felipe Balbid889c232016-09-29 15:44:29 +03001672 /*
1673 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1674 * wait for a XferNotReady event so we will know what's the current
1675 * (micro-)frame number.
1676 *
1677 * Without this trick, we are very, very likely gonna get Bus Expiry
1678 * errors which will force us issue EndTransfer command.
1679 */
1680 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001681 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1682 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001683 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001684
1685 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001686 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001687 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001688 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001689 }
1690
Wesley Cheng9bd96a22021-05-07 10:55:19 -07001691 __dwc3_gadget_kick_transfer(dep);
1692
1693 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001694}
1695
1696static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1697 gfp_t gfp_flags)
1698{
1699 struct dwc3_request *req = to_dwc3_request(request);
1700 struct dwc3_ep *dep = to_dwc3_ep(ep);
1701 struct dwc3 *dwc = dep->dwc;
1702
1703 unsigned long flags;
1704
1705 int ret;
1706
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001707 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001708 ret = __dwc3_gadget_ep_queue(dep, req);
1709 spin_unlock_irqrestore(&dwc->lock, flags);
1710
1711 return ret;
1712}
1713
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001714static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1715{
1716 int i;
1717
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001718 /* If req->trb is not set, then the request has not started */
1719 if (!req->trb)
1720 return;
1721
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001722 /*
1723 * If request was already started, this means we had to
1724 * stop the transfer. With that we also need to ignore
1725 * all TRBs used by the request, however TRBs can only
1726 * be modified after completion of END_TRANSFER
1727 * command. So what we do here is that we wait for
1728 * END_TRANSFER completion and only after that, we jump
1729 * over TRBs by clearing HWO and incrementing dequeue
1730 * pointer.
1731 */
1732 for (i = 0; i < req->num_trbs; i++) {
1733 struct dwc3_trb *trb;
1734
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001735 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001736 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1737 dwc3_ep_inc_deq(dep);
1738 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001739
1740 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001741}
1742
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001743static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1744{
1745 struct dwc3_request *req;
1746 struct dwc3_request *tmp;
1747
1748 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1749 dwc3_gadget_ep_skip_trbs(dep, req);
1750 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1751 }
1752}
1753
Felipe Balbi72246da2011-08-19 18:10:58 +03001754static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1755 struct usb_request *request)
1756{
1757 struct dwc3_request *req = to_dwc3_request(request);
1758 struct dwc3_request *r = NULL;
1759
1760 struct dwc3_ep *dep = to_dwc3_ep(ep);
1761 struct dwc3 *dwc = dep->dwc;
1762
1763 unsigned long flags;
1764 int ret = 0;
1765
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001766 trace_dwc3_ep_dequeue(req);
1767
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 spin_lock_irqsave(&dwc->lock, flags);
1769
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001770 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001772 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001773 }
1774
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001776 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001777 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1778 goto out;
1779 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 }
1781
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001782 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001783 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001784 struct dwc3_request *t;
1785
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001787 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001788
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001789 /*
1790 * Remove any started request if the transfer is
1791 * cancelled.
1792 */
1793 list_for_each_entry_safe(r, t, &dep->started_list, list)
1794 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001795
Thinh Nguyen8907a102021-01-04 22:42:39 -08001796 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1797
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001798 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001799 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001800 }
1801
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001802 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1803 request, ep->name);
1804 ret = -EINVAL;
1805out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 spin_unlock_irqrestore(&dwc->lock, flags);
1807
1808 return ret;
1809}
1810
Felipe Balbi7a608552014-09-24 14:19:52 -05001811int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001812{
1813 struct dwc3_gadget_ep_cmd_params params;
1814 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001815 struct dwc3_request *req;
1816 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001817 int ret;
1818
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001819 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1820 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1821 return -EINVAL;
1822 }
1823
Felipe Balbi72246da2011-08-19 18:10:58 +03001824 memset(&params, 0x00, sizeof(params));
1825
1826 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001827 struct dwc3_trb *trb;
1828
Felipe Balbie319bd62020-08-13 08:35:38 +03001829 unsigned int transfer_in_flight;
1830 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001831
1832 if (dep->number > 1)
1833 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1834 else
1835 trb = &dwc->ep0_trb[dep->trb_enqueue];
1836
1837 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1838 started = !list_empty(&dep->started_list);
1839
1840 if (!protocol && ((dep->direction && transfer_in_flight) ||
1841 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001842 return -EAGAIN;
1843 }
1844
Felipe Balbi2cd47182016-04-12 16:42:43 +03001845 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1846 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001848 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001849 dep->name);
1850 else
1851 dep->flags |= DWC3_EP_STALL;
1852 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001853 /*
1854 * Don't issue CLEAR_STALL command to control endpoints. The
1855 * controller automatically clears the STALL when it receives
1856 * the SETUP token.
1857 */
1858 if (dep->number <= 1) {
1859 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1860 return 0;
1861 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001862
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001863 dwc3_stop_active_transfer(dep, true, true);
1864
1865 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1866 dwc3_gadget_move_cancelled_request(req);
1867
1868 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1869 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1870 return 0;
1871 }
1872
1873 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1874
John Youn50c763f2016-05-31 17:49:56 -07001875 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001876 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001877 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001879 return ret;
1880 }
1881
1882 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1883
Thinh Nguyenc5036722020-09-02 18:42:58 -07001884 if ((dep->flags & DWC3_EP_DELAY_START) &&
1885 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1886 __dwc3_gadget_kick_transfer(dep);
1887
1888 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001890
Felipe Balbi72246da2011-08-19 18:10:58 +03001891 return ret;
1892}
1893
1894static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1895{
1896 struct dwc3_ep *dep = to_dwc3_ep(ep);
1897 struct dwc3 *dwc = dep->dwc;
1898
1899 unsigned long flags;
1900
1901 int ret;
1902
1903 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001904 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001905 spin_unlock_irqrestore(&dwc->lock, flags);
1906
1907 return ret;
1908}
1909
1910static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1911{
1912 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001913 struct dwc3 *dwc = dep->dwc;
1914 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001915 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001916
Paul Zimmerman249a4562012-02-24 17:32:16 -08001917 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001918 dep->flags |= DWC3_EP_WEDGE;
1919
Pratyush Anand08f0d962012-06-25 22:40:43 +05301920 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001921 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301922 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001923 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001924 spin_unlock_irqrestore(&dwc->lock, flags);
1925
1926 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001927}
1928
1929/* -------------------------------------------------------------------------- */
1930
1931static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1932 .bLength = USB_DT_ENDPOINT_SIZE,
1933 .bDescriptorType = USB_DT_ENDPOINT,
1934 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1935};
1936
1937static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1938 .enable = dwc3_gadget_ep0_enable,
1939 .disable = dwc3_gadget_ep0_disable,
1940 .alloc_request = dwc3_gadget_ep_alloc_request,
1941 .free_request = dwc3_gadget_ep_free_request,
1942 .queue = dwc3_gadget_ep0_queue,
1943 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301944 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001945 .set_wedge = dwc3_gadget_ep_set_wedge,
1946};
1947
1948static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1949 .enable = dwc3_gadget_ep_enable,
1950 .disable = dwc3_gadget_ep_disable,
1951 .alloc_request = dwc3_gadget_ep_alloc_request,
1952 .free_request = dwc3_gadget_ep_free_request,
1953 .queue = dwc3_gadget_ep_queue,
1954 .dequeue = dwc3_gadget_ep_dequeue,
1955 .set_halt = dwc3_gadget_ep_set_halt,
1956 .set_wedge = dwc3_gadget_ep_set_wedge,
1957};
1958
1959/* -------------------------------------------------------------------------- */
1960
1961static int dwc3_gadget_get_frame(struct usb_gadget *g)
1962{
1963 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001964
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001965 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001966}
1967
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001968static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001969{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001970 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001971
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001972 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001973 u32 reg;
1974
Felipe Balbi72246da2011-08-19 18:10:58 +03001975 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001976
Felipe Balbi72246da2011-08-19 18:10:58 +03001977 /*
1978 * According to the Databook Remote wakeup request should
1979 * be issued only when the device is in early suspend state.
1980 *
1981 * We can check that via USB Link State bits in DSTS register.
1982 */
1983 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1984
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 link_state = DWC3_DSTS_USBLNKST(reg);
1986
1987 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001988 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1990 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyenb624b322021-04-19 19:11:12 -07001991 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
1992 case DWC3_LINK_STATE_U1:
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001993 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001994 break;
1995 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001996 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001997 }
1998
Felipe Balbi8598bde2012-01-02 18:55:57 +02001999 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2000 if (ret < 0) {
2001 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002002 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02002003 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002004
Paul Zimmerman802fde92012-04-27 13:10:52 +03002005 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002006 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002007 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03002008 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002009 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2010 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2011 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002012
Paul Zimmerman1d046792012-02-15 18:56:56 -08002013 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002014 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03002015
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002016 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002017 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2018
2019 /* in HS, means ON */
2020 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2021 break;
2022 }
2023
2024 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2025 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002026 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002027 }
2028
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002029 return 0;
2030}
2031
2032static int dwc3_gadget_wakeup(struct usb_gadget *g)
2033{
2034 struct dwc3 *dwc = gadget_to_dwc(g);
2035 unsigned long flags;
2036 int ret;
2037
2038 spin_lock_irqsave(&dwc->lock, flags);
2039 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 spin_unlock_irqrestore(&dwc->lock, flags);
2041
2042 return ret;
2043}
2044
2045static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2046 int is_selfpowered)
2047{
2048 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002049 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002050
Paul Zimmerman249a4562012-02-24 17:32:16 -08002051 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002052 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002053 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002054
2055 return 0;
2056}
2057
Wesley Chengae7e8612020-09-28 17:20:59 -07002058static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2059{
2060 u32 epnum;
2061
2062 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2063 struct dwc3_ep *dep;
2064
2065 dep = dwc->eps[epnum];
2066 if (!dep)
2067 continue;
2068
2069 dwc3_remove_requests(dwc, dep);
2070 }
2071}
2072
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002073static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002074{
2075 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002076 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002077
Felipe Balbifc8bb912016-05-16 13:14:48 +03002078 if (pm_runtime_suspended(dwc->dev))
2079 return 0;
2080
Felipe Balbi72246da2011-08-19 18:10:58 +03002081 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002082 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002083 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002084 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2085 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2086 }
2087
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002088 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002089 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2090 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002091
2092 if (dwc->has_hibernation)
2093 reg |= DWC3_DCTL_KEEP_CONNECT;
2094
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002095 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002096 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002097 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002098
2099 if (dwc->has_hibernation && !suspend)
2100 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2101
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002102 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002103 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002104
Thinh Nguyen5b738212019-10-23 19:15:43 -07002105 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002106
2107 do {
2108 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002109 reg &= DWC3_DSTS_DEVCTRLHLT;
2110 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002111
2112 if (!timeout)
2113 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114
Pratyush Anand6f17f742012-07-02 10:21:55 +05302115 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002116}
2117
Wesley Chengae7e8612020-09-28 17:20:59 -07002118static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2119static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chengdd8363f2020-12-29 15:00:37 -08002120static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002121
Felipe Balbi72246da2011-08-19 18:10:58 +03002122static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2123{
2124 struct dwc3 *dwc = gadget_to_dwc(g);
2125 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302126 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002127
2128 is_on = !!is_on;
2129
Baolin Wangbb014732016-10-14 17:11:33 +08002130 /*
2131 * Per databook, when we want to stop the gadget, if a control transfer
2132 * is still in process, complete it and get the core into setup phase.
2133 */
2134 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2135 reinit_completion(&dwc->ep0_in_setup);
2136
2137 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2138 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
Wesley Cheng01da7c12021-08-24 21:28:55 -07002139 if (ret == 0)
2140 dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
Baolin Wangbb014732016-10-14 17:11:33 +08002141 }
2142
Wesley Chengae7e8612020-09-28 17:20:59 -07002143 /*
Wesley Cheng98c83d72021-08-03 23:24:05 -07002144 * Avoid issuing a runtime resume if the device is already in the
2145 * suspended state during gadget disconnect. DWC3 gadget was already
2146 * halted/stopped during runtime suspend.
2147 */
2148 if (!is_on) {
2149 pm_runtime_barrier(dwc->dev);
2150 if (pm_runtime_suspended(dwc->dev))
2151 return 0;
2152 }
2153
2154 /*
Wesley Cheng395d2732020-12-29 15:05:35 -08002155 * Check the return value for successful resume, or error. For a
2156 * successful resume, the DWC3 runtime PM resume routine will handle
2157 * the run stop sequence, so avoid duplicate operations here.
2158 */
2159 ret = pm_runtime_get_sync(dwc->dev);
2160 if (!ret || ret < 0) {
2161 pm_runtime_put(dwc->dev);
2162 return 0;
2163 }
2164
2165 /*
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002166 * Synchronize and disable any further event handling while controller
2167 * is being enabled/disabled.
Wesley Chengae7e8612020-09-28 17:20:59 -07002168 */
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002169 disable_irq(dwc->irq_gadget);
Wesley Chengae7e8612020-09-28 17:20:59 -07002170
Felipe Balbi72246da2011-08-19 18:10:58 +03002171 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002172
2173 if (!is_on) {
2174 u32 count;
2175
Wesley Chengc7bb96a2021-03-11 15:59:02 -08002176 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002177 /*
2178 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2179 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2180 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2181 * command for any active transfers" before clearing the RunStop
2182 * bit.
2183 */
2184 dwc3_stop_active_transfers(dwc);
2185 __dwc3_gadget_stop(dwc);
2186
2187 /*
2188 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2189 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2190 * "software needs to acknowledge the events that are generated
2191 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2192 * to be set to '1'."
2193 */
2194 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2195 count &= DWC3_GEVNTCOUNT_MASK;
2196 if (count > 0) {
2197 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2198 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2199 dwc->ev_buf->length;
2200 }
Wesley Chengdd8363f2020-12-29 15:00:37 -08002201 } else {
2202 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002203 }
2204
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002205 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002206 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng9e0677c2021-05-20 21:23:57 -07002207 enable_irq(dwc->irq_gadget);
2208
Wesley Cheng395d2732020-12-29 15:05:35 -08002209 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002210
Pratyush Anand6f17f742012-07-02 10:21:55 +05302211 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002212}
2213
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002214static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2215{
2216 u32 reg;
2217
2218 /* Enable all but Start and End of Frame IRQs */
2219 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2220 DWC3_DEVTEN_EVNTOVERFLOWEN |
2221 DWC3_DEVTEN_CMDCMPLTEN |
2222 DWC3_DEVTEN_ERRTICERREN |
2223 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002224 DWC3_DEVTEN_CONNECTDONEEN |
2225 DWC3_DEVTEN_USBRSTEN |
2226 DWC3_DEVTEN_DISCONNEVTEN);
2227
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002228 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002229 reg |= DWC3_DEVTEN_ULSTCNGEN;
2230
Jack Pham45f37f52021-04-28 02:01:10 -07002231 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2232 if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2233 reg |= DWC3_DEVTEN_EOPFEN;
2234
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002235 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2236}
2237
2238static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2239{
2240 /* mask all interrupts */
2241 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2242}
2243
2244static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002245static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002246
Felipe Balbi4e994722016-05-13 14:09:59 +03002247/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002248 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2249 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002250 *
2251 * The following looks like complex but it's actually very simple. In order to
2252 * calculate the number of packets we can burst at once on OUT transfers, we're
2253 * gonna use RxFIFO size.
2254 *
2255 * To calculate RxFIFO size we need two numbers:
2256 * MDWIDTH = size, in bits, of the internal memory bus
2257 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2258 *
2259 * Given these two numbers, the formula is simple:
2260 *
2261 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2262 *
2263 * 24 bytes is for 3x SETUP packets
2264 * 16 bytes is a clock domain crossing tolerance
2265 *
2266 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2267 */
2268static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2269{
2270 u32 ram2_depth;
2271 u32 mdwidth;
2272 u32 nump;
2273 u32 reg;
2274
2275 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2276 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002277 if (DWC3_IP_IS(DWC32))
2278 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002279
2280 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2281 nump = min_t(u32, nump, 16);
2282
2283 /* update NumP */
2284 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2285 reg &= ~DWC3_DCFG_NUMP_MASK;
2286 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2287 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2288}
2289
Felipe Balbid7be2952016-05-04 15:49:37 +03002290static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002291{
Felipe Balbi72246da2011-08-19 18:10:58 +03002292 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002293 int ret = 0;
2294 u32 reg;
2295
John Youncf40b862016-11-14 12:32:43 -08002296 /*
2297 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2298 * the core supports IMOD, disable it.
2299 */
2300 if (dwc->imod_interval) {
2301 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2302 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2303 } else if (dwc3_has_imod(dwc)) {
2304 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2305 }
2306
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002307 /*
2308 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2309 * field instead of letting dwc3 itself calculate that automatically.
2310 *
2311 * This way, we maximize the chances that we'll be able to get several
2312 * bursts of data without going through any sort of endpoint throttling.
2313 */
2314 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002315 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002316 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002317 else
2318 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002319
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002320 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2321
Felipe Balbi4e994722016-05-13 14:09:59 +03002322 dwc3_gadget_setup_nump(dwc);
2323
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 /* Start with SuperSpeed Default */
2325 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2326
2327 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002328 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002329 if (ret) {
2330 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002331 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002332 }
2333
2334 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002335 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002336 if (ret) {
2337 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002338 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002339 }
2340
2341 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002342 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002343 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Wesley Cheng01da7c12021-08-24 21:28:55 -07002344 dwc->delayed_status = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002345 dwc3_ep0_out_start(dwc);
2346
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002347 dwc3_gadget_enable_irq(dwc);
2348
Felipe Balbid7be2952016-05-04 15:49:37 +03002349 return 0;
2350
2351err1:
2352 __dwc3_gadget_ep_disable(dwc->eps[0]);
2353
2354err0:
2355 return ret;
2356}
2357
2358static int dwc3_gadget_start(struct usb_gadget *g,
2359 struct usb_gadget_driver *driver)
2360{
2361 struct dwc3 *dwc = gadget_to_dwc(g);
2362 unsigned long flags;
2363 int ret = 0;
2364 int irq;
2365
Roger Quadros9522def2016-06-10 14:48:38 +03002366 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002367 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2368 IRQF_SHARED, "dwc3", dwc->ev_buf);
2369 if (ret) {
2370 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2371 irq, ret);
2372 goto err0;
2373 }
2374
2375 spin_lock_irqsave(&dwc->lock, flags);
2376 if (dwc->gadget_driver) {
2377 dev_err(dwc->dev, "%s is already bound to %s\n",
Peter Chene81a7012020-08-21 10:55:48 +08002378 dwc->gadget->name,
Felipe Balbid7be2952016-05-04 15:49:37 +03002379 dwc->gadget_driver->driver.name);
2380 ret = -EBUSY;
2381 goto err1;
2382 }
2383
2384 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 spin_unlock_irqrestore(&dwc->lock, flags);
2386
2387 return 0;
2388
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002389err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002390 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002391 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002392
2393err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 return ret;
2395}
2396
Felipe Balbid7be2952016-05-04 15:49:37 +03002397static void __dwc3_gadget_stop(struct dwc3 *dwc)
2398{
2399 dwc3_gadget_disable_irq(dwc);
2400 __dwc3_gadget_ep_disable(dwc->eps[0]);
2401 __dwc3_gadget_ep_disable(dwc->eps[1]);
2402}
2403
Felipe Balbi22835b82014-10-17 12:05:12 -05002404static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002405{
2406 struct dwc3 *dwc = gadget_to_dwc(g);
2407 unsigned long flags;
2408
2409 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002410 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 spin_unlock_irqrestore(&dwc->lock, flags);
2412
Felipe Balbi3f308d12016-05-16 14:17:06 +03002413 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002414
Felipe Balbi72246da2011-08-19 18:10:58 +03002415 return 0;
2416}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002417
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302418static void dwc3_gadget_config_params(struct usb_gadget *g,
2419 struct usb_dcd_config_params *params)
2420{
2421 struct dwc3 *dwc = gadget_to_dwc(g);
2422
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002423 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2424 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2425
2426 /* Recommended BESL */
2427 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002428 /*
2429 * If the recommended BESL baseline is 0 or if the BESL deep is
2430 * less than 2, Microsoft's Windows 10 host usb stack will issue
2431 * a usb reset immediately after it receives the extended BOS
2432 * descriptor and the enumeration will fail. To maintain
2433 * compatibility with the Windows' usb stack, let's set the
2434 * recommended BESL baseline to 1 and clamp the BESL deep to be
2435 * within 2 to 15.
2436 */
2437 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002438 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002439 params->besl_deep =
2440 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002441 }
2442
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302443 /* U1 Device exit Latency */
2444 if (dwc->dis_u1_entry_quirk)
2445 params->bU1devExitLat = 0;
2446 else
2447 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2448
2449 /* U2 Device exit Latency */
2450 if (dwc->dis_u2_entry_quirk)
2451 params->bU2DevExitLat = 0;
2452 else
2453 params->bU2DevExitLat =
2454 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2455}
2456
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002457static void dwc3_gadget_set_speed(struct usb_gadget *g,
2458 enum usb_device_speed speed)
2459{
2460 struct dwc3 *dwc = gadget_to_dwc(g);
2461 unsigned long flags;
2462 u32 reg;
2463
2464 spin_lock_irqsave(&dwc->lock, flags);
2465 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2466 reg &= ~(DWC3_DCFG_SPEED_MASK);
2467
2468 /*
2469 * WORKAROUND: DWC3 revision < 2.20a have an issue
2470 * which would cause metastability state on Run/Stop
2471 * bit if we try to force the IP to USB2-only mode.
2472 *
2473 * Because of that, we cannot configure the IP to any
2474 * speed other than the SuperSpeed
2475 *
2476 * Refers to:
2477 *
2478 * STAR#9000525659: Clock Domain Crossing on DCTL in
2479 * USB 2.0 Mode
2480 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002481 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02002482 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002483 reg |= DWC3_DCFG_SUPERSPEED;
2484 } else {
2485 switch (speed) {
2486 case USB_SPEED_LOW:
2487 reg |= DWC3_DCFG_LOWSPEED;
2488 break;
2489 case USB_SPEED_FULL:
2490 reg |= DWC3_DCFG_FULLSPEED;
2491 break;
2492 case USB_SPEED_HIGH:
2493 reg |= DWC3_DCFG_HIGHSPEED;
2494 break;
2495 case USB_SPEED_SUPER:
2496 reg |= DWC3_DCFG_SUPERSPEED;
2497 break;
2498 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002499 if (DWC3_IP_IS(DWC3))
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002500 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002501 else
2502 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002503 break;
2504 default:
2505 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2506
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002507 if (DWC3_IP_IS(DWC3))
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002508 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002509 else
2510 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002511 }
2512 }
2513 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2514
2515 spin_unlock_irqrestore(&dwc->lock, flags);
2516}
2517
Felipe Balbi72246da2011-08-19 18:10:58 +03002518static const struct usb_gadget_ops dwc3_gadget_ops = {
2519 .get_frame = dwc3_gadget_get_frame,
2520 .wakeup = dwc3_gadget_wakeup,
2521 .set_selfpowered = dwc3_gadget_set_selfpowered,
2522 .pullup = dwc3_gadget_pullup,
2523 .udc_start = dwc3_gadget_start,
2524 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002525 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302526 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002527};
2528
2529/* -------------------------------------------------------------------------- */
2530
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002531static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2532{
2533 struct dwc3 *dwc = dep->dwc;
2534
2535 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2536 dep->endpoint.maxburst = 1;
2537 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2538 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002539 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002540
2541 dep->endpoint.caps.type_control = true;
2542
2543 return 0;
2544}
2545
2546static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2547{
2548 struct dwc3 *dwc = dep->dwc;
2549 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002550 int size;
2551
2552 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002553 if (DWC3_IP_IS(DWC32))
2554 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2555
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002556 /* MDWIDTH is represented in bits, we need it in bytes */
2557 mdwidth /= 8;
2558
2559 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002560 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002561 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002562 else
2563 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002564
2565 /* FIFO Depth is in MDWDITH bytes. Multiply */
2566 size *= mdwidth;
2567
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002568 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002569 * To meet performance requirement, a minimum TxFIFO size of 3x
2570 * MaxPacketSize is recommended for endpoints that support burst and a
2571 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2572 * support burst. Use those numbers and we can calculate the max packet
2573 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002574 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002575 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2576 size /= 3;
2577 else
2578 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002579
2580 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2581
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002582 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002583 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2584 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002585 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002586 dep->endpoint.caps.type_iso = true;
2587 dep->endpoint.caps.type_bulk = true;
2588 dep->endpoint.caps.type_int = true;
2589
2590 return dwc3_alloc_trb_pool(dep);
2591}
2592
2593static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2594{
2595 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002596 int mdwidth;
2597 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002598
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002599 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002600 if (DWC3_IP_IS(DWC32))
2601 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002602
2603 /* MDWIDTH is represented in bits, convert to bytes */
2604 mdwidth /= 8;
2605
2606 /* All OUT endpoints share a single RxFIFO space */
2607 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002608 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002609 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002610 else
2611 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002612
2613 /* FIFO depth is in MDWDITH bytes */
2614 size *= mdwidth;
2615
2616 /*
2617 * To meet performance requirement, a minimum recommended RxFIFO size
2618 * is defined as follow:
2619 * RxFIFO size >= (3 x MaxPacketSize) +
2620 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2621 *
2622 * Then calculate the max packet limit as below.
2623 */
2624 size -= (3 * 8) + 16;
2625 if (size < 0)
2626 size = 0;
2627 else
2628 size /= 3;
2629
2630 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002631 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002632 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2633 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002634 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002635 dep->endpoint.caps.type_iso = true;
2636 dep->endpoint.caps.type_bulk = true;
2637 dep->endpoint.caps.type_int = true;
2638
2639 return dwc3_alloc_trb_pool(dep);
2640}
2641
2642static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002643{
2644 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002645 bool direction = epnum & 1;
2646 int ret;
2647 u8 num = epnum >> 1;
2648
2649 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2650 if (!dep)
2651 return -ENOMEM;
2652
2653 dep->dwc = dwc;
2654 dep->number = epnum;
2655 dep->direction = direction;
2656 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2657 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002658 dep->combo_num = 0;
2659 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002660
2661 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2662 direction ? "in" : "out");
2663
2664 dep->endpoint.name = dep->name;
2665
2666 if (!(dep->number > 1)) {
2667 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2668 dep->endpoint.comp_desc = NULL;
2669 }
2670
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002671 if (num == 0)
2672 ret = dwc3_gadget_init_control_endpoint(dep);
2673 else if (direction)
2674 ret = dwc3_gadget_init_in_endpoint(dep);
2675 else
2676 ret = dwc3_gadget_init_out_endpoint(dep);
2677
2678 if (ret)
2679 return ret;
2680
2681 dep->endpoint.caps.dir_in = direction;
2682 dep->endpoint.caps.dir_out = !direction;
2683
2684 INIT_LIST_HEAD(&dep->pending_list);
2685 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002686 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002687
Jack Phame52d43c2021-05-29 12:29:32 -07002688 dwc3_debugfs_create_endpoint_dir(dep);
2689
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002690 return 0;
2691}
2692
2693static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2694{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002695 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002696
Peter Chene81a7012020-08-21 10:55:48 +08002697 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002698
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002699 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002700 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002702 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2703 if (ret)
2704 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 }
2706
2707 return 0;
2708}
2709
2710static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2711{
2712 struct dwc3_ep *dep;
2713 u8 epnum;
2714
2715 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2716 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002717 if (!dep)
2718 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302719 /*
2720 * Physical endpoints 0 and 1 are special; they form the
2721 * bi-directional USB endpoint 0.
2722 *
2723 * For those two physical endpoints, we don't allocate a TRB
2724 * pool nor do we add them the endpoints list. Due to that, we
2725 * shouldn't do these two operations otherwise we would end up
2726 * with all sorts of bugs when removing dwc3.ko.
2727 */
2728 if (epnum != 0 && epnum != 1) {
2729 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302731 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002732
Jack Phame52d43c2021-05-29 12:29:32 -07002733 debugfs_remove_recursive(debugfs_lookup(dep->name, dwc->root));
Felipe Balbi72246da2011-08-19 18:10:58 +03002734 kfree(dep);
2735 }
2736}
2737
Felipe Balbi72246da2011-08-19 18:10:58 +03002738/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002739
Felipe Balbi8f608e82018-03-27 10:53:29 +03002740static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2741 struct dwc3_request *req, struct dwc3_trb *trb,
2742 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302743{
2744 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302745
Felipe Balbidc55c672016-08-12 13:20:32 +03002746 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002747
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002748 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002749 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002750
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002751 /*
2752 * If we're in the middle of series of chained TRBs and we
2753 * receive a short transfer along the way, DWC3 will skip
2754 * through all TRBs including the last TRB in the chain (the
2755 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2756 * bit and SW has to do it manually.
2757 *
2758 * We're going to do that here to avoid problems of HW trying
2759 * to use bogus TRBs for transfers.
2760 */
2761 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2762 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2763
Felipe Balbic6267a52017-01-05 14:58:46 +02002764 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002765 * For isochronous transfers, the first TRB in a service interval must
2766 * have the Isoc-First type. Track and report its interval frame number.
2767 */
2768 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2769 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2770 unsigned int frame_number;
2771
2772 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2773 frame_number &= ~(dep->interval - 1);
2774 req->request.frame_number = frame_number;
2775 }
2776
2777 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002778 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2779 * this TRB points to the bounce buffer address, it's a MPS alignment
2780 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002781 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002782 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2783 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002784 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2785 return 1;
2786 }
2787
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302788 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002789 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302790
Felipe Balbi35b27192017-03-08 13:56:37 +02002791 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2792 return 1;
2793
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002794 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302795 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002796
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002797 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2798 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302799 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002800
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302801 return 0;
2802}
2803
Felipe Balbid3692952018-03-29 13:32:10 +03002804static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2805 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2806 int status)
2807{
2808 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2809 struct scatterlist *sg = req->sg;
2810 struct scatterlist *s;
Thinh Nguyenadccf172021-05-12 20:17:09 -07002811 unsigned int num_queued = req->num_queued_sgs;
Felipe Balbid3692952018-03-29 13:32:10 +03002812 unsigned int i;
2813 int ret = 0;
2814
Thinh Nguyenadccf172021-05-12 20:17:09 -07002815 for_each_sg(sg, s, num_queued, i) {
Felipe Balbid3692952018-03-29 13:32:10 +03002816 trb = &dep->trb_pool[dep->trb_dequeue];
2817
Felipe Balbid3692952018-03-29 13:32:10 +03002818 req->sg = sg_next(s);
Thinh Nguyenadccf172021-05-12 20:17:09 -07002819 req->num_queued_sgs--;
Felipe Balbid3692952018-03-29 13:32:10 +03002820
2821 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2822 trb, event, status, true);
2823 if (ret)
2824 break;
2825 }
2826
2827 return ret;
2828}
2829
2830static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2831 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2832 int status)
2833{
2834 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2835
2836 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2837 event, status, false);
2838}
2839
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002840static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2841{
Thinh Nguyenadccf172021-05-12 20:17:09 -07002842 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002843}
2844
Felipe Balbif38e35d2018-04-06 15:56:35 +03002845static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2846 const struct dwc3_event_depevt *event,
2847 struct dwc3_request *req, int status)
2848{
2849 int ret;
2850
Thinh Nguyenadccf172021-05-12 20:17:09 -07002851 if (req->request.num_mapped_sgs)
Felipe Balbif38e35d2018-04-06 15:56:35 +03002852 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2853 status);
2854 else
2855 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2856 status);
2857
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002858 req->request.actual = req->request.length - req->remaining;
2859
2860 if (!dwc3_gadget_ep_request_completed(req))
2861 goto out;
2862
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002863 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002864 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2865 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002866 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002867 }
2868
Felipe Balbif38e35d2018-04-06 15:56:35 +03002869 dwc3_gadget_giveback(dep, req, status);
2870
2871out:
2872 return ret;
2873}
2874
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002875static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002876 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002877{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002878 struct dwc3_request *req;
2879 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002880
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002881 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002882 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002883
Felipe Balbif38e35d2018-04-06 15:56:35 +03002884 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2885 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002886 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002887 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002888 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002889}
2890
Thinh Nguyend9feef92020-03-31 01:40:42 -07002891static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2892{
2893 struct dwc3_request *req;
2894
2895 if (!list_empty(&dep->pending_list))
2896 return true;
2897
2898 /*
2899 * We only need to check the first entry of the started list. We can
2900 * assume the completed requests are removed from the started list.
2901 */
2902 req = next_request(&dep->started_list);
2903 if (!req)
2904 return false;
2905
2906 return !dwc3_gadget_ep_request_completed(req);
2907}
2908
Felipe Balbiee3638b2018-03-27 11:26:53 +03002909static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2910 const struct dwc3_event_depevt *event)
2911{
Felipe Balbif62afb42018-04-11 10:34:34 +03002912 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002913}
2914
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002915static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2916 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002917{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002918 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002919 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002920
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002921 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002922
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002923 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2924 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002925
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002926 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2927 list_empty(&dep->started_list) &&
2928 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002929 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002930 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002931 if (__dwc3_gadget_kick_transfer(dep) == 0)
2932 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002933
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002934out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002935 /*
2936 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2937 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2938 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002939 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002940 u32 reg;
2941 int i;
2942
2943 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002944 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002945
2946 if (!(dep->flags & DWC3_EP_ENABLED))
2947 continue;
2948
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002949 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002950 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002951 }
2952
2953 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2954 reg |= dwc->u1u2;
2955 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2956
2957 dwc->u1u2 = 0;
2958 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002959
2960 return no_started_trb;
2961}
2962
2963static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2964 const struct dwc3_event_depevt *event)
2965{
2966 int status = 0;
2967
2968 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2969 dwc3_gadget_endpoint_frame_from_event(dep, event);
2970
2971 if (event->status & DEPEVT_STATUS_BUSERR)
2972 status = -ECONNRESET;
2973
2974 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2975 status = -EXDEV;
2976
2977 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002978}
2979
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07002980static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
2981 const struct dwc3_event_depevt *event)
2982{
2983 int status = 0;
2984
2985 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2986
2987 if (event->status & DEPEVT_STATUS_BUSERR)
2988 status = -ECONNRESET;
2989
Thinh Nguyene0d19562020-05-05 19:46:57 -07002990 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
2991 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002992}
2993
Felipe Balbi8f608e82018-03-27 10:53:29 +03002994static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2995 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002996{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002997 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002998
2999 /*
3000 * The XferNotReady event is generated only once before the endpoint
3001 * starts. It will be generated again when END_TRANSFER command is
3002 * issued. For some controller versions, the XferNotReady event may be
3003 * generated while the END_TRANSFER command is still in process. Ignore
3004 * it and wait for the next XferNotReady event after the command is
3005 * completed.
3006 */
3007 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3008 return;
3009
Felipe Balbi25abad62018-08-14 10:41:19 +03003010 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03003011}
3012
Thinh Nguyen8266b082020-07-30 16:29:03 -07003013static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3014 const struct dwc3_event_depevt *event)
3015{
3016 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3017
3018 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3019 return;
3020
Thinh Nguyen3abf7462021-10-25 16:21:10 -07003021 /*
3022 * The END_TRANSFER command will cause the controller to generate a
3023 * NoStream Event, and it's not due to the host DP NoStream rejection.
3024 * Ignore the next NoStream event.
3025 */
3026 if (dep->stream_capable)
3027 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3028
Thinh Nguyen8266b082020-07-30 16:29:03 -07003029 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3030 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3031 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3032
3033 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3034 struct dwc3 *dwc = dep->dwc;
3035
3036 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3037 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3038 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3039
3040 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3041 if (dwc->delayed_status)
3042 __dwc3_gadget_ep0_set_halt(ep0, 1);
3043 return;
3044 }
3045
3046 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3047 if (dwc->delayed_status)
3048 dwc3_ep0_send_delayed_status(dwc);
3049 }
3050
3051 if ((dep->flags & DWC3_EP_DELAY_START) &&
3052 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3053 __dwc3_gadget_kick_transfer(dep);
3054
3055 dep->flags &= ~DWC3_EP_DELAY_START;
3056}
3057
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003058static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3059 const struct dwc3_event_depevt *event)
3060{
3061 struct dwc3 *dwc = dep->dwc;
3062
3063 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3064 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3065 goto out;
3066 }
3067
3068 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3069 switch (event->parameters) {
3070 case DEPEVT_STREAM_PRIME:
3071 /*
3072 * If the host can properly transition the endpoint state from
3073 * idle to prime after a NoStream rejection, there's no need to
3074 * force restarting the endpoint to reinitiate the stream. To
3075 * simplify the check, assume the host follows the USB spec if
3076 * it primed the endpoint more than once.
3077 */
3078 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3079 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3080 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3081 else
3082 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3083 }
3084
3085 break;
3086 case DEPEVT_STREAM_NOSTREAM:
3087 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3088 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3089 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3090 break;
3091
3092 /*
3093 * If the host rejects a stream due to no active stream, by the
3094 * USB and xHCI spec, the endpoint will be put back to idle
3095 * state. When the host is ready (buffer added/updated), it will
3096 * prime the endpoint to inform the usb device controller. This
3097 * triggers the device controller to issue ERDY to restart the
3098 * stream. However, some hosts don't follow this and keep the
3099 * endpoint in the idle state. No prime will come despite host
3100 * streams are updated, and the device controller will not be
3101 * triggered to generate ERDY to move the next stream data. To
3102 * workaround this and maintain compatibility with various
3103 * hosts, force to reinitate the stream until the host is ready
3104 * instead of waiting for the host to prime the endpoint.
3105 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003106 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3107 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3108
3109 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3110 } else {
3111 dep->flags |= DWC3_EP_DELAY_START;
3112 dwc3_stop_active_transfer(dep, true, true);
3113 return;
3114 }
3115 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003116 }
3117
3118out:
3119 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3120}
3121
Felipe Balbi72246da2011-08-19 18:10:58 +03003122static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3123 const struct dwc3_event_depevt *event)
3124{
3125 struct dwc3_ep *dep;
3126 u8 epnum = event->endpoint_number;
3127
3128 dep = dwc->eps[epnum];
3129
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003130 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003131 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003132 return;
3133
3134 /* Handle only EPCMDCMPLT when EP disabled */
3135 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3136 return;
3137 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003138
Felipe Balbi72246da2011-08-19 18:10:58 +03003139 if (epnum == 0 || epnum == 1) {
3140 dwc3_ep0_interrupt(dwc, event);
3141 return;
3142 }
3143
3144 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003145 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003146 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003147 break;
3148 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003149 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003150 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003151 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003152 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003153 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003154 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003155 dwc3_gadget_endpoint_transfer_complete(dep, event);
3156 break;
3157 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003158 dwc3_gadget_endpoint_stream_event(dep, event);
3159 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003160 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003161 break;
3162 }
3163}
3164
3165static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3166{
3167 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3168 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003169 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003170 spin_lock(&dwc->lock);
3171 }
3172}
3173
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003174static void dwc3_suspend_gadget(struct dwc3 *dwc)
3175{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003176 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003177 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003178 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003179 spin_lock(&dwc->lock);
3180 }
3181}
3182
3183static void dwc3_resume_gadget(struct dwc3 *dwc)
3184{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003185 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003186 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003187 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003188 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003189 }
3190}
3191
3192static void dwc3_reset_gadget(struct dwc3 *dwc)
3193{
3194 if (!dwc->gadget_driver)
3195 return;
3196
Peter Chene81a7012020-08-21 10:55:48 +08003197 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003198 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003199 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003200 spin_lock(&dwc->lock);
3201 }
3202}
3203
Felipe Balbic5353b22019-02-13 13:00:54 +02003204static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3205 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003206{
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 struct dwc3_gadget_ep_cmd_params params;
3208 u32 cmd;
3209 int ret;
3210
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003211 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3212 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303213 return;
3214
Pratyush Anand57911502012-07-06 15:19:10 +05303215 /*
3216 * NOTICE: We are violating what the Databook says about the
3217 * EndTransfer command. Ideally we would _always_ wait for the
3218 * EndTransfer Command Completion IRQ, but that's causing too
3219 * much trouble synchronizing between us and gadget driver.
3220 *
3221 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003222 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303223 *
3224 * Note also that a similar handling was tested by Synopsys
3225 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003226 * In short, what we're doing is issuing EndTransfer with
3227 * CMDIOC bit set and delay kicking transfer until the
3228 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003229 *
3230 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3231 * supports a mode to work around the above limitation. The
3232 * software can poll the CMDACT bit in the DEPCMD register
3233 * after issuing a EndTransfer command. This mode is enabled
3234 * by writing GUCTL2[14]. This polling is already done in the
3235 * dwc3_send_gadget_ep_cmd() function so if the mode is
3236 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003237 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003238 *
3239 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303240 */
3241
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303242 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003243 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003244 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003245 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303246 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003247 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303248 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003249 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003250
Thinh Nguyend3abda52019-11-27 13:10:47 -08003251 if (!interrupt)
3252 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003253 else
3254 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003255}
3256
Felipe Balbi72246da2011-08-19 18:10:58 +03003257static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3258{
3259 u32 epnum;
3260
3261 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3262 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003263 int ret;
3264
3265 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003266 if (!dep)
3267 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003268
3269 if (!(dep->flags & DWC3_EP_STALL))
3270 continue;
3271
3272 dep->flags &= ~DWC3_EP_STALL;
3273
John Youn50c763f2016-05-31 17:49:56 -07003274 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003275 WARN_ON_ONCE(ret);
3276 }
3277}
3278
3279static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3280{
Felipe Balbic4430a22012-05-24 10:30:01 +03003281 int reg;
3282
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003283 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3284
Felipe Balbi72246da2011-08-19 18:10:58 +03003285 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3286 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003287 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003288 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003289
Felipe Balbi72246da2011-08-19 18:10:58 +03003290 dwc3_disconnect_gadget(dwc);
3291
Peter Chene81a7012020-08-21 10:55:48 +08003292 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003293 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003294 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003295
3296 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003297}
3298
Felipe Balbi72246da2011-08-19 18:10:58 +03003299static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3300{
3301 u32 reg;
3302
Felipe Balbidf62df52011-10-14 15:11:49 +03003303 /*
Wesley Cheng45f879b2021-03-19 02:31:25 -07003304 * Ideally, dwc3_reset_gadget() would trigger the function
3305 * drivers to stop any active transfers through ep disable.
3306 * However, for functions which defer ep disable, such as mass
3307 * storage, we will need to rely on the call to stop active
3308 * transfers here, and avoid allowing of request queuing.
3309 */
3310 dwc->connected = false;
3311
3312 /*
Felipe Balbidf62df52011-10-14 15:11:49 +03003313 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3314 * would cause a missing Disconnect Event if there's a
3315 * pending Setup Packet in the FIFO.
3316 *
3317 * There's no suggested workaround on the official Bug
3318 * report, which states that "unless the driver/application
3319 * is doing any special handling of a disconnect event,
3320 * there is no functional issue".
3321 *
3322 * Unfortunately, it turns out that we _do_ some special
3323 * handling of a disconnect event, namely complete all
3324 * pending transfers, notify gadget driver of the
3325 * disconnection, and so on.
3326 *
3327 * Our suggested workaround is to follow the Disconnect
3328 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003329 * flag. Such flag gets set whenever we have a SETUP_PENDING
3330 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003331 * same endpoint.
3332 *
3333 * Refers to:
3334 *
3335 * STAR#9000466709: RTL: Device : Disconnect event not
3336 * generated if setup packet pending in FIFO
3337 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003338 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003339 if (dwc->setup_packet_pending)
3340 dwc3_gadget_disconnect_interrupt(dwc);
3341 }
3342
Felipe Balbi8e744752014-11-06 14:27:53 +08003343 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003344 /*
3345 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3346 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3347 * needs to ensure that it sends "a DEPENDXFER command for any active
3348 * transfers."
3349 */
3350 dwc3_stop_active_transfers(dwc);
Wesley Chengc7bb96a2021-03-11 15:59:02 -08003351 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003352
3353 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3354 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003355 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003356 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003357 dwc3_clear_stall_all_ep(dwc);
3358
3359 /* Reset device address to zero */
3360 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3361 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3362 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003363}
3364
Felipe Balbi72246da2011-08-19 18:10:58 +03003365static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3366{
Felipe Balbi72246da2011-08-19 18:10:58 +03003367 struct dwc3_ep *dep;
3368 int ret;
3369 u32 reg;
3370 u8 speed;
3371
Felipe Balbi72246da2011-08-19 18:10:58 +03003372 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3373 speed = reg & DWC3_DSTS_CONNECTSPD;
3374 dwc->speed = speed;
3375
John Youn5fb6fda2016-11-10 17:23:25 -08003376 /*
3377 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3378 * each time on Connect Done.
3379 *
3380 * Currently we always use the reset value. If any platform
3381 * wants to set this to a different value, we need to add a
3382 * setting and update GCTL.RAMCLKSEL here.
3383 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003384
3385 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003386 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003387 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003388 dwc->gadget->ep0->maxpacket = 512;
3389 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
John Youn75808622016-02-05 17:09:13 -08003390 break;
John Youn2da9ad72016-05-20 16:34:26 -07003391 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003392 /*
3393 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3394 * would cause a missing USB3 Reset event.
3395 *
3396 * In such situations, we should force a USB3 Reset
3397 * event by calling our dwc3_gadget_reset_interrupt()
3398 * routine.
3399 *
3400 * Refers to:
3401 *
3402 * STAR#9000483510: RTL: SS : USB3 reset event may
3403 * not be generated always when the link enters poll
3404 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003405 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003406 dwc3_gadget_reset_interrupt(dwc);
3407
Felipe Balbi72246da2011-08-19 18:10:58 +03003408 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003409 dwc->gadget->ep0->maxpacket = 512;
3410 dwc->gadget->speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03003411 break;
John Youn2da9ad72016-05-20 16:34:26 -07003412 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003413 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003414 dwc->gadget->ep0->maxpacket = 64;
3415 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003416 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003417 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003418 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003419 dwc->gadget->ep0->maxpacket = 64;
3420 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003421 break;
John Youn2da9ad72016-05-20 16:34:26 -07003422 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003423 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003424 dwc->gadget->ep0->maxpacket = 8;
3425 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003426 break;
3427 }
3428
Peter Chene81a7012020-08-21 10:55:48 +08003429 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003430
Pratyush Anand2b758352013-01-14 15:59:31 +05303431 /* Enable USB2 LPM Capability */
3432
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003433 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003434 !dwc->usb2_gadget_lpm_disable &&
John Youn2da9ad72016-05-20 16:34:26 -07003435 (speed != DWC3_DSTS_SUPERSPEED) &&
3436 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303437 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3438 reg |= DWC3_DCFG_LPM_CAP;
3439 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3440
3441 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3442 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3443
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003444 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3445 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303446
Huang Rui80caf7d2014-10-28 19:54:26 +08003447 /*
3448 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3449 * DCFG.LPMCap is set, core responses with an ACK and the
3450 * BESL value in the LPM token is less than or equal to LPM
3451 * NYET threshold.
3452 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003453 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003454 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003455
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003456 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003457 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003458
Thinh Nguyen5b738212019-10-23 19:15:43 -07003459 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003460 } else {
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003461 if (dwc->usb2_gadget_lpm_disable) {
3462 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3463 reg &= ~DWC3_DCFG_LPM_CAP;
3464 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3465 }
3466
Felipe Balbi356363b2013-12-19 16:37:05 -06003467 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3468 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003469 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303470 }
3471
Felipe Balbi72246da2011-08-19 18:10:58 +03003472 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003473 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003474 if (ret) {
3475 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3476 return;
3477 }
3478
3479 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003480 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003481 if (ret) {
3482 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3483 return;
3484 }
3485
3486 /*
3487 * Configure PHY via GUSB3PIPECTLn if required.
3488 *
3489 * Update GTXFIFOSIZn
3490 *
3491 * In both cases reset values should be sufficient.
3492 */
3493}
3494
3495static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3496{
Felipe Balbi72246da2011-08-19 18:10:58 +03003497 /*
3498 * TODO take core out of low power mode when that's
3499 * implemented.
3500 */
3501
Jiebing Liad14d4e2014-12-11 13:26:29 +08003502 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3503 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003504 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003505 spin_lock(&dwc->lock);
3506 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003507}
3508
3509static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3510 unsigned int evtinfo)
3511{
Felipe Balbifae2b902011-10-14 13:00:30 +03003512 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003513 unsigned int pwropt;
3514
3515 /*
3516 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3517 * Hibernation mode enabled which would show up when device detects
3518 * host-initiated U3 exit.
3519 *
3520 * In that case, device will generate a Link State Change Interrupt
3521 * from U3 to RESUME which is only necessary if Hibernation is
3522 * configured in.
3523 *
3524 * There are no functional changes due to such spurious event and we
3525 * just need to ignore it.
3526 *
3527 * Refers to:
3528 *
3529 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3530 * operational mode
3531 */
3532 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003533 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003534 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3535 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3536 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003537 return;
3538 }
3539 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003540
3541 /*
3542 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3543 * on the link partner, the USB session might do multiple entry/exit
3544 * of low power states before a transfer takes place.
3545 *
3546 * Due to this problem, we might experience lower throughput. The
3547 * suggested workaround is to disable DCTL[12:9] bits if we're
3548 * transitioning from U1/U2 to U0 and enable those bits again
3549 * after a transfer completes and there are no pending transfers
3550 * on any of the enabled endpoints.
3551 *
3552 * This is the first half of that workaround.
3553 *
3554 * Refers to:
3555 *
3556 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3557 * core send LGO_Ux entering U0
3558 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003559 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003560 if (next == DWC3_LINK_STATE_U0) {
3561 u32 u1u2;
3562 u32 reg;
3563
3564 switch (dwc->link_state) {
3565 case DWC3_LINK_STATE_U1:
3566 case DWC3_LINK_STATE_U2:
3567 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3568 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3569 | DWC3_DCTL_ACCEPTU2ENA
3570 | DWC3_DCTL_INITU1ENA
3571 | DWC3_DCTL_ACCEPTU1ENA);
3572
3573 if (!dwc->u1u2)
3574 dwc->u1u2 = reg & u1u2;
3575
3576 reg &= ~u1u2;
3577
Thinh Nguyen5b738212019-10-23 19:15:43 -07003578 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003579 break;
3580 default:
3581 /* do nothing */
3582 break;
3583 }
3584 }
3585 }
3586
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003587 switch (next) {
3588 case DWC3_LINK_STATE_U1:
3589 if (dwc->speed == USB_SPEED_SUPER)
3590 dwc3_suspend_gadget(dwc);
3591 break;
3592 case DWC3_LINK_STATE_U2:
3593 case DWC3_LINK_STATE_U3:
3594 dwc3_suspend_gadget(dwc);
3595 break;
3596 case DWC3_LINK_STATE_RESUME:
3597 dwc3_resume_gadget(dwc);
3598 break;
3599 default:
3600 /* do nothing */
3601 break;
3602 }
3603
Felipe Balbie57ebc12014-04-22 13:20:12 -05003604 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003605}
3606
Baolin Wang72704f82016-05-16 16:43:53 +08003607static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3608 unsigned int evtinfo)
3609{
3610 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3611
3612 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3613 dwc3_suspend_gadget(dwc);
3614
3615 dwc->link_state = next;
3616}
3617
Felipe Balbie1dadd32014-02-25 14:47:54 -06003618static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3619 unsigned int evtinfo)
3620{
3621 unsigned int is_ss = evtinfo & BIT(4);
3622
Felipe Balbibfad65e2017-04-19 14:59:27 +03003623 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003624 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3625 * have a known issue which can cause USB CV TD.9.23 to fail
3626 * randomly.
3627 *
3628 * Because of this issue, core could generate bogus hibernation
3629 * events which SW needs to ignore.
3630 *
3631 * Refers to:
3632 *
3633 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3634 * Device Fallback from SuperSpeed
3635 */
3636 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3637 return;
3638
3639 /* enter hibernation here */
3640}
3641
Felipe Balbi72246da2011-08-19 18:10:58 +03003642static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3643 const struct dwc3_event_devt *event)
3644{
3645 switch (event->type) {
3646 case DWC3_DEVICE_EVENT_DISCONNECT:
3647 dwc3_gadget_disconnect_interrupt(dwc);
3648 break;
3649 case DWC3_DEVICE_EVENT_RESET:
3650 dwc3_gadget_reset_interrupt(dwc);
3651 break;
3652 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3653 dwc3_gadget_conndone_interrupt(dwc);
3654 break;
3655 case DWC3_DEVICE_EVENT_WAKEUP:
3656 dwc3_gadget_wakeup_interrupt(dwc);
3657 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003658 case DWC3_DEVICE_EVENT_HIBER_REQ:
3659 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3660 "unexpected hibernation event\n"))
3661 break;
3662
3663 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3664 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003665 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3666 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3667 break;
3668 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003669 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003670 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003671 /*
3672 * Ignore suspend event until the gadget enters into
3673 * USB_STATE_CONFIGURED state.
3674 */
Peter Chene81a7012020-08-21 10:55:48 +08003675 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003676 dwc3_gadget_suspend_interrupt(dwc,
3677 event->event_info);
3678 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003679 break;
3680 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003681 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003682 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003683 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003684 break;
3685 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003686 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003687 }
3688}
3689
3690static void dwc3_process_event_entry(struct dwc3 *dwc,
3691 const union dwc3_event *event)
3692{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003693 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003694
Felipe Balbidfc5e802017-04-26 13:44:51 +03003695 if (!event->type.is_devspec)
3696 dwc3_endpoint_interrupt(dwc, &event->depevt);
3697 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003698 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003699 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003700 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003701}
3702
Felipe Balbidea520a2016-03-30 09:39:34 +03003703static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003704{
Felipe Balbidea520a2016-03-30 09:39:34 +03003705 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003706 irqreturn_t ret = IRQ_NONE;
3707 int left;
3708 u32 reg;
3709
Felipe Balbif42f2442013-06-12 21:25:08 +03003710 left = evt->count;
3711
3712 if (!(evt->flags & DWC3_EVENT_PENDING))
3713 return IRQ_NONE;
3714
3715 while (left > 0) {
3716 union dwc3_event event;
3717
John Younebbb2d52016-11-15 13:07:02 +02003718 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003719
3720 dwc3_process_event_entry(dwc, &event);
3721
3722 /*
3723 * FIXME we wrap around correctly to the next entry as
3724 * almost all entries are 4 bytes in size. There is one
3725 * entry which has 12 bytes which is a regular entry
3726 * followed by 8 bytes data. ATM I don't know how
3727 * things are organized if we get next to the a
3728 * boundary so I worry about that once we try to handle
3729 * that.
3730 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003731 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003732 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003733 }
3734
3735 evt->count = 0;
3736 evt->flags &= ~DWC3_EVENT_PENDING;
3737 ret = IRQ_HANDLED;
3738
3739 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003740 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003741 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003742 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003743
John Youncf40b862016-11-14 12:32:43 -08003744 if (dwc->imod_interval) {
3745 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3746 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3747 }
3748
Felipe Balbif42f2442013-06-12 21:25:08 +03003749 return ret;
3750}
3751
Felipe Balbidea520a2016-03-30 09:39:34 +03003752static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003753{
Felipe Balbidea520a2016-03-30 09:39:34 +03003754 struct dwc3_event_buffer *evt = _evt;
3755 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003756 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003757 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003758
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003759 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003760 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003761 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003762
3763 return ret;
3764}
3765
Felipe Balbidea520a2016-03-30 09:39:34 +03003766static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003767{
Felipe Balbidea520a2016-03-30 09:39:34 +03003768 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003769 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003770 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003771 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003772
Felipe Balbifc8bb912016-05-16 13:14:48 +03003773 if (pm_runtime_suspended(dwc->dev)) {
3774 pm_runtime_get(dwc->dev);
3775 disable_irq_nosync(dwc->irq_gadget);
3776 dwc->pending_events = true;
3777 return IRQ_HANDLED;
3778 }
3779
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003780 /*
3781 * With PCIe legacy interrupt, test shows that top-half irq handler can
3782 * be called again after HW interrupt deassertion. Check if bottom-half
3783 * irq event handler completes before caching new event to prevent
3784 * losing events.
3785 */
3786 if (evt->flags & DWC3_EVENT_PENDING)
3787 return IRQ_HANDLED;
3788
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003789 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003790 count &= DWC3_GEVNTCOUNT_MASK;
3791 if (!count)
3792 return IRQ_NONE;
3793
Felipe Balbib15a7622011-06-30 16:57:15 +03003794 evt->count = count;
3795 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003796
Felipe Balbie8adfc32013-06-12 21:11:14 +03003797 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003798 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003799 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003800 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003801
John Younebbb2d52016-11-15 13:07:02 +02003802 amount = min(count, evt->length - evt->lpos);
3803 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3804
3805 if (amount < count)
3806 memcpy(evt->cache, evt->buf, count - amount);
3807
John Youn65aca322016-11-15 13:08:59 +02003808 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3809
Felipe Balbib15a7622011-06-30 16:57:15 +03003810 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003811}
3812
Felipe Balbidea520a2016-03-30 09:39:34 +03003813static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003814{
Felipe Balbidea520a2016-03-30 09:39:34 +03003815 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003816
Felipe Balbidea520a2016-03-30 09:39:34 +03003817 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003818}
3819
Felipe Balbi6db38122016-10-03 11:27:01 +03003820static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3821{
3822 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3823 int irq;
3824
Hans de Goedef146b402019-10-05 23:04:48 +02003825 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003826 if (irq > 0)
3827 goto out;
3828
3829 if (irq == -EPROBE_DEFER)
3830 goto out;
3831
Hans de Goedef146b402019-10-05 23:04:48 +02003832 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003833 if (irq > 0)
3834 goto out;
3835
3836 if (irq == -EPROBE_DEFER)
3837 goto out;
3838
3839 irq = platform_get_irq(dwc3_pdev, 0);
3840 if (irq > 0)
3841 goto out;
3842
Felipe Balbi6db38122016-10-03 11:27:01 +03003843 if (!irq)
3844 irq = -EINVAL;
3845
3846out:
3847 return irq;
3848}
3849
Peter Chene81a7012020-08-21 10:55:48 +08003850static void dwc_gadget_release(struct device *dev)
3851{
3852 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3853
3854 kfree(gadget);
3855}
3856
Felipe Balbi72246da2011-08-19 18:10:58 +03003857/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003858 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003859 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003860 *
3861 * Returns 0 on success otherwise negative errno.
3862 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003863int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003864{
Felipe Balbi6db38122016-10-03 11:27:01 +03003865 int ret;
3866 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003867 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003868
Felipe Balbi6db38122016-10-03 11:27:01 +03003869 irq = dwc3_gadget_get_irq(dwc);
3870 if (irq < 0) {
3871 ret = irq;
3872 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003873 }
3874
3875 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003876
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303877 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3878 sizeof(*dwc->ep0_trb) * 2,
3879 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003880 if (!dwc->ep0_trb) {
3881 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3882 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003883 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003884 }
3885
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003886 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003887 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003888 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003889 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003890 }
3891
Felipe Balbi905dc042017-01-05 14:46:52 +02003892 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3893 &dwc->bounce_addr, GFP_KERNEL);
3894 if (!dwc->bounce) {
3895 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003896 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003897 }
3898
Baolin Wangbb014732016-10-14 17:11:33 +08003899 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003900 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3901 if (!dwc->gadget) {
3902 ret = -ENOMEM;
3903 goto err3;
3904 }
Baolin Wangbb014732016-10-14 17:11:33 +08003905
Peter Chene81a7012020-08-21 10:55:48 +08003906
3907 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3908 dev = &dwc->gadget->dev;
3909 dev->platform_data = dwc;
3910 dwc->gadget->ops = &dwc3_gadget_ops;
3911 dwc->gadget->speed = USB_SPEED_UNKNOWN;
3912 dwc->gadget->sg_supported = true;
3913 dwc->gadget->name = "dwc3-gadget";
Thinh Nguyen8f7cdbb2021-04-13 19:13:18 -07003914 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03003915
3916 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003917 * FIXME We might be setting max_speed to <SUPER, however versions
3918 * <2.20a of dwc3 have an issue with metastability (documented
3919 * elsewhere in this driver) which tells us we can't set max speed to
3920 * anything lower than SUPER.
3921 *
3922 * Because gadget.max_speed is only used by composite.c and function
3923 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3924 * to happen so we avoid sending SuperSpeed Capability descriptor
3925 * together with our BOS descriptor as that could confuse host into
3926 * thinking we can handle super speed.
3927 *
3928 * Note that, in fact, we won't even support GetBOS requests when speed
3929 * is less than super speed because we don't have means, yet, to tell
3930 * composite.c that we are USB 2.0 + LPM ECN.
3931 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003932 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003933 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003934 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003935 dwc->revision);
3936
Peter Chene81a7012020-08-21 10:55:48 +08003937 dwc->gadget->max_speed = dwc->maximum_speed;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003938
3939 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003940 * REVISIT: Here we should clear all pending IRQs to be
3941 * sure we're starting from a well known location.
3942 */
3943
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003944 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003945 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003946 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003947
3948 ret = usb_add_gadget(dwc->gadget);
3949 if (ret) {
3950 dev_err(dwc->dev, "failed to add gadget\n");
3951 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003952 }
3953
Peter Chene81a7012020-08-21 10:55:48 +08003954 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003955
Felipe Balbi72246da2011-08-19 18:10:58 +03003956 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003957
Peter Chene81a7012020-08-21 10:55:48 +08003958err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003959 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003960err4:
3961 usb_put_gadget(dwc->gadget);
Jack Pham851dee52021-05-28 09:04:05 -07003962 dwc->gadget = NULL;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003963err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003964 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3965 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003966
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003967err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003968 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003969
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003970err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303971 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003972 dwc->ep0_trb, dwc->ep0_trb_addr);
3973
Felipe Balbi72246da2011-08-19 18:10:58 +03003974err0:
3975 return ret;
3976}
3977
Felipe Balbi7415f172012-04-30 14:56:33 +03003978/* -------------------------------------------------------------------------- */
3979
Felipe Balbi72246da2011-08-19 18:10:58 +03003980void dwc3_gadget_exit(struct dwc3 *dwc)
3981{
Jack Pham851dee52021-05-28 09:04:05 -07003982 if (!dwc->gadget)
3983 return;
3984
Jack Pham1ea77502021-05-01 02:35:58 -07003985 usb_del_gadget(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003986 dwc3_gadget_free_endpoints(dwc);
Jack Pham1ea77502021-05-01 02:35:58 -07003987 usb_put_gadget(dwc->gadget);
Felipe Balbi905dc042017-01-05 14:46:52 +02003988 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003989 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003990 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303991 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003992 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003993}
Felipe Balbi7415f172012-04-30 14:56:33 +03003994
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003995int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003996{
Roger Quadros9772b472016-04-12 11:33:29 +03003997 if (!dwc->gadget_driver)
3998 return 0;
3999
Roger Quadros1551e352017-02-15 14:16:26 +02004000 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004001 dwc3_disconnect_gadget(dwc);
4002 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004003
4004 return 0;
4005}
4006
4007int dwc3_gadget_resume(struct dwc3 *dwc)
4008{
Felipe Balbi7415f172012-04-30 14:56:33 +03004009 int ret;
4010
Roger Quadros9772b472016-04-12 11:33:29 +03004011 if (!dwc->gadget_driver)
4012 return 0;
4013
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004014 ret = __dwc3_gadget_start(dwc);
4015 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004016 goto err0;
4017
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004018 ret = dwc3_gadget_run_stop(dwc, true, false);
4019 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004020 goto err1;
4021
Felipe Balbi7415f172012-04-30 14:56:33 +03004022 return 0;
4023
4024err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004025 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004026
4027err0:
4028 return ret;
4029}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004030
4031void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4032{
4033 if (dwc->pending_events) {
4034 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4035 dwc->pending_events = false;
4036 enable_irq(dwc->irq_gadget);
4037 }
4038}