blob: aebcf8ec071639da21f0b698807fa5813890a65e [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) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300311 int needs_wakeup;
312
313 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
314 dwc->link_state == DWC3_LINK_STATE_U2 ||
315 dwc->link_state == DWC3_LINK_STATE_U3);
316
317 if (unlikely(needs_wakeup)) {
318 ret = __dwc3_gadget_wakeup(dwc);
319 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
320 ret);
321 }
322 }
323
Felipe Balbi2eb88012016-04-12 16:53:39 +0300324 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
325 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
326 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
Felipe Balbi8897a762016-09-22 10:56:08 +0300328 /*
329 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
330 * not relying on XferNotReady, we can make use of a special "No
331 * Response Update Transfer" command where we should clear both CmdAct
332 * and CmdIOC bits.
333 *
334 * With this, we don't need to wait for command completion and can
335 * straight away issue further commands to the endpoint.
336 *
337 * NOTICE: We're making an assumption that control endpoints will never
338 * make use of Update Transfer command. This is a safe assumption
339 * because we can never have more than one request at a time with
340 * Control Endpoints. If anybody changes that assumption, this chunk
341 * needs to be updated accordingly.
342 */
343 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
344 !usb_endpoint_xfer_isoc(desc))
345 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
346 else
347 cmd |= DWC3_DEPCMD_CMDACT;
348
349 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300350 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300351 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300353 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000354
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000355 switch (cmd_status) {
356 case 0:
357 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300358 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000359 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700360 dev_WARN(dwc->dev, "No resource for %s\n",
361 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000362 ret = -EINVAL;
363 break;
364 case DEPEVT_TRANSFER_BUS_EXPIRY:
365 /*
366 * SW issues START TRANSFER command to
367 * isochronous ep with future frame interval. If
368 * future interval time has already passed when
369 * core receives the command, it will respond
370 * with an error status of 'Bus Expiry'.
371 *
372 * Instead of always returning -EINVAL, let's
373 * give a hint to the gadget driver that this is
374 * the case by returning -EAGAIN.
375 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000376 ret = -EAGAIN;
377 break;
378 default:
379 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
380 }
381
Felipe Balbic0ca3242016-04-04 09:11:51 +0300382 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300383 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300384 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
Felipe Balbif6bb2252016-05-23 13:53:34 +0300386 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300387 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300388 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300389 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300390
Felipe Balbi0933df12016-05-23 14:02:33 +0300391 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
392
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700393 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
394 if (ret == 0)
395 dep->flags |= DWC3_EP_TRANSFER_STARTED;
396
397 if (ret != -ETIMEDOUT)
398 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300399 }
400
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700401 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300402 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700403 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300404 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
405 }
406
Felipe Balbic0ca3242016-04-04 09:11:51 +0300407 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408}
409
John Youn50c763f2016-05-31 17:49:56 -0700410static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
411{
412 struct dwc3 *dwc = dep->dwc;
413 struct dwc3_gadget_ep_cmd_params params;
414 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
415
416 /*
417 * As of core revision 2.60a the recommended programming model
418 * is to set the ClearPendIN bit when issuing a Clear Stall EP
419 * command for IN endpoints. This is to prevent an issue where
420 * some (non-compliant) hosts may not send ACK TPs for pending
421 * IN transfers due to a mishandled error condition. Synopsys
422 * STAR 9000614252.
423 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700424 if (dep->direction &&
425 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800426 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700427 cmd |= DWC3_DEPCMD_CLEARPENDIN;
428
429 memset(&params, 0, sizeof(params));
430
Felipe Balbi2cd47182016-04-12 16:42:43 +0300431 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700432}
433
Felipe Balbi72246da2011-08-19 18:10:58 +0300434static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200435 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300436{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300437 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438
439 return dep->trb_pool_dma + offset;
440}
441
442static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
443{
444 struct dwc3 *dwc = dep->dwc;
445
446 if (dep->trb_pool)
447 return 0;
448
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530449 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300450 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
451 &dep->trb_pool_dma, GFP_KERNEL);
452 if (!dep->trb_pool) {
453 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
454 dep->name);
455 return -ENOMEM;
456 }
457
458 return 0;
459}
460
461static void dwc3_free_trb_pool(struct dwc3_ep *dep)
462{
463 struct dwc3 *dwc = dep->dwc;
464
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530465 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 dep->trb_pool, dep->trb_pool_dma);
467
468 dep->trb_pool = NULL;
469 dep->trb_pool_dma = 0;
470}
471
Felipe Balbi20d1d432018-04-09 12:49:02 +0300472static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
473{
474 struct dwc3_gadget_ep_cmd_params params;
475
476 memset(&params, 0x00, sizeof(params));
477
478 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
479
480 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
481 &params);
482}
John Younc4509602016-02-16 20:10:53 -0800483
484/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300485 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800486 * @dep: endpoint that is being enabled
487 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300488 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
489 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800490 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300491 * The assignment of transfer resources cannot perfectly follow the data book
492 * due to the fact that the controller driver does not have all knowledge of the
493 * configuration in advance. It is given this information piecemeal by the
494 * composite gadget framework after every SET_CONFIGURATION and
495 * SET_INTERFACE. Trying to follow the databook programming model in this
496 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800497 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300498 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
499 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
500 * incorrect in the scenario of multiple interfaces.
501 *
502 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800503 * endpoint on alt setting (8.1.6).
504 *
505 * The following simplified method is used instead:
506 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300507 * All hardware endpoints can be assigned a transfer resource and this setting
508 * will stay persistent until either a core reset or hibernation. So whenever we
509 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
510 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800511 * guaranteed that there are as many transfer resources as endpoints.
512 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300513 * This function is called for each endpoint when it is being enabled but is
514 * triggered only when called for EP0-out, which always happens first, and which
515 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800516 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300517static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300518{
519 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300520 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800522 int i;
523 int ret;
524
525 if (dep->number)
526 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527
528 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800529 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300530 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531
Felipe Balbi2cd47182016-04-12 16:42:43 +0300532 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800533 if (ret)
534 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535
John Younc4509602016-02-16 20:10:53 -0800536 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
537 struct dwc3_ep *dep = dwc->eps[i];
538
539 if (!dep)
540 continue;
541
Felipe Balbib07c2db2018-04-09 12:46:47 +0300542 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800543 if (ret)
544 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545 }
546
547 return 0;
548}
549
Felipe Balbib07c2db2018-04-09 12:46:47 +0300550static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300551{
John Youn39ebb052016-11-09 16:36:28 -0800552 const struct usb_ss_ep_comp_descriptor *comp_desc;
553 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300555 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556
John Youn39ebb052016-11-09 16:36:28 -0800557 comp_desc = dep->endpoint.comp_desc;
558 desc = dep->endpoint.desc;
559
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 memset(&params, 0x00, sizeof(params));
561
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300562 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900563 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
564
565 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800566 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300567 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300568
Felipe Balbi676e3492016-04-26 10:49:07 +0300569 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900570 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300571
Felipe Balbia2d23f02018-04-09 12:40:48 +0300572 params.param0 |= action;
573 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600574 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600575
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300576 if (usb_endpoint_xfer_control(desc))
577 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300578
579 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
580 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200582 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700584 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300585 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300586 dep->stream_capable = true;
587 }
588
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500589 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 /*
593 * We are doing 1:1 mapping for endpoints, meaning
594 * Physical Endpoints 2 maps to Logical Endpoint 2 and
595 * so on. We consider the direction bit as part of the physical
596 * endpoint number. So USB endpoint 0x81 is 0x03.
597 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300598 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300599
600 /*
601 * We must use the lower 16 TX FIFOs even though
602 * HW might have more
603 */
604 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300605 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
607 if (desc->bInterval) {
Thinh Nguyena1679af2021-02-08 13:53:10 -0800608 u8 bInterval_m1;
609
610 /*
611 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13, and it
612 * must be set to 0 when the controller operates in full-speed.
613 */
614 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
615 if (dwc->gadget->speed == USB_SPEED_FULL)
616 bInterval_m1 = 0;
617
Thinh Nguyen4b049f52021-02-08 13:53:16 -0800618 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
619 dwc->gadget->speed == USB_SPEED_FULL)
620 dep->interval = desc->bInterval;
621 else
622 dep->interval = 1 << (desc->bInterval - 1);
623
Thinh Nguyena1679af2021-02-08 13:53:10 -0800624 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 }
626
Felipe Balbi2cd47182016-04-12 16:42:43 +0300627 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300628}
629
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700630static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
631 bool interrupt);
632
Felipe Balbi72246da2011-08-19 18:10:58 +0300633/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300634 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300636 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300638 * Caller should take care of locking. Execute all necessary commands to
639 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300640 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300641static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300642{
John Youn39ebb052016-11-09 16:36:28 -0800643 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800645
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300647 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300648
649 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300650 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 if (ret)
652 return ret;
653 }
654
Felipe Balbib07c2db2018-04-09 12:46:47 +0300655 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 if (ret)
657 return ret;
658
659 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200660 struct dwc3_trb *trb_st_hw;
661 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300662
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 dep->type = usb_endpoint_type(desc);
664 dep->flags |= DWC3_EP_ENABLED;
665
666 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
667 reg |= DWC3_DALEPENA_EP(dep->number);
668 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
669
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300670 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200671 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300672
John Youn0d257442016-05-19 17:26:08 -0700673 /* Initialize the TRB ring */
674 dep->trb_dequeue = 0;
675 dep->trb_enqueue = 0;
676 memset(dep->trb_pool, 0,
677 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
678
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300679 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 trb_st_hw = &dep->trb_pool[0];
681
Felipe Balbif6bafc62012-02-06 11:04:53 +0200682 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200683 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
684 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
685 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
686 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 }
688
Felipe Balbia97ea992016-09-29 16:28:56 +0300689 /*
690 * Issue StartTransfer here with no-op TRB so we can always rely on No
691 * Response Update Transfer command.
692 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700693 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300694 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300695 struct dwc3_gadget_ep_cmd_params params;
696 struct dwc3_trb *trb;
697 dma_addr_t trb_dma;
698 u32 cmd;
699
700 memset(&params, 0, sizeof(params));
701 trb = &dep->trb_pool[0];
702 trb_dma = dwc3_trb_dma_offset(dep, trb);
703
704 params.param0 = upper_32_bits(trb_dma);
705 params.param1 = lower_32_bits(trb_dma);
706
707 cmd = DWC3_DEPCMD_STARTTRANSFER;
708
709 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
710 if (ret < 0)
711 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700712
713 if (dep->stream_capable) {
714 /*
715 * For streams, at start, there maybe a race where the
716 * host primes the endpoint before the function driver
717 * queues a request to initiate a stream. In that case,
718 * the controller will not see the prime to generate the
719 * ERDY and start stream. To workaround this, issue a
720 * no-op TRB as normal, but end it immediately. As a
721 * result, when the function driver queues the request,
722 * the next START_TRANSFER command will cause the
723 * controller to generate an ERDY to initiate the
724 * stream.
725 */
726 dwc3_stop_active_transfer(dep, true, true);
727
728 /*
729 * All stream eps will reinitiate stream on NoStream
730 * rejection until we can determine that the host can
731 * prime after the first transfer.
732 */
733 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
734 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300735 }
736
Felipe Balbi2870e502016-11-03 13:53:29 +0200737out:
738 trace_dwc3_gadget_ep_enable(dep);
739
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 return 0;
741}
742
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300744{
745 struct dwc3_request *req;
746
Felipe Balbic5353b22019-02-13 13:00:54 +0200747 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300748
Felipe Balbi0e146022016-06-21 10:32:02 +0300749 /* - giveback all requests to gadget driver */
750 while (!list_empty(&dep->started_list)) {
751 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200752
Felipe Balbi0e146022016-06-21 10:32:02 +0300753 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200754 }
755
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200756 while (!list_empty(&dep->pending_list)) {
757 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200759 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200761
762 while (!list_empty(&dep->cancelled_list)) {
763 req = next_request(&dep->cancelled_list);
764
765 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
766 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300767}
768
769/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300770 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300771 * @dep: the endpoint to disable
772 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300773 * This function undoes what __dwc3_gadget_ep_enable did and also removes
774 * requests which are currently being processed by the hardware and those which
775 * are not yet scheduled.
776 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200777 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300779static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
780{
781 struct dwc3 *dwc = dep->dwc;
782 u32 reg;
783
Felipe Balbi2870e502016-11-03 13:53:29 +0200784 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500785
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200786 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300787
Felipe Balbi687ef982014-04-16 10:30:33 -0500788 /* make sure HW endpoint isn't stalled */
789 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500790 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500791
Felipe Balbi72246da2011-08-19 18:10:58 +0300792 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
793 reg &= ~DWC3_DALEPENA_EP(dep->number);
794 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
795
Felipe Balbi879631a2011-09-30 10:58:47 +0300796 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200798 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300799
John Youn39ebb052016-11-09 16:36:28 -0800800 /* Clear out the ep descriptors for non-ep0 */
801 if (dep->number > 1) {
802 dep->endpoint.comp_desc = NULL;
803 dep->endpoint.desc = NULL;
804 }
805
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 return 0;
807}
808
809/* -------------------------------------------------------------------------- */
810
811static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
812 const struct usb_endpoint_descriptor *desc)
813{
814 return -EINVAL;
815}
816
817static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
818{
819 return -EINVAL;
820}
821
822/* -------------------------------------------------------------------------- */
823
824static int dwc3_gadget_ep_enable(struct usb_ep *ep,
825 const struct usb_endpoint_descriptor *desc)
826{
827 struct dwc3_ep *dep;
828 struct dwc3 *dwc;
829 unsigned long flags;
830 int ret;
831
832 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
833 pr_debug("dwc3: invalid parameters\n");
834 return -EINVAL;
835 }
836
837 if (!desc->wMaxPacketSize) {
838 pr_debug("dwc3: missing wMaxPacketSize\n");
839 return -EINVAL;
840 }
841
842 dep = to_dwc3_ep(ep);
843 dwc = dep->dwc;
844
Felipe Balbi95ca9612015-12-10 13:08:20 -0600845 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
846 "%s is already enabled\n",
847 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300848 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300849
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300851 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 spin_unlock_irqrestore(&dwc->lock, flags);
853
854 return ret;
855}
856
857static int dwc3_gadget_ep_disable(struct usb_ep *ep)
858{
859 struct dwc3_ep *dep;
860 struct dwc3 *dwc;
861 unsigned long flags;
862 int ret;
863
864 if (!ep) {
865 pr_debug("dwc3: invalid parameters\n");
866 return -EINVAL;
867 }
868
869 dep = to_dwc3_ep(ep);
870 dwc = dep->dwc;
871
Felipe Balbi95ca9612015-12-10 13:08:20 -0600872 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
873 "%s is already disabled\n",
874 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300875 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300876
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 spin_lock_irqsave(&dwc->lock, flags);
878 ret = __dwc3_gadget_ep_disable(dep);
879 spin_unlock_irqrestore(&dwc->lock, flags);
880
881 return ret;
882}
883
884static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300885 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300886{
887 struct dwc3_request *req;
888 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
890 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900891 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300892 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300893
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300894 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300895 req->epnum = dep->number;
896 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200897 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500899 trace_dwc3_alloc_request(req);
900
Felipe Balbi72246da2011-08-19 18:10:58 +0300901 return &req->request;
902}
903
904static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
905 struct usb_request *request)
906{
907 struct dwc3_request *req = to_dwc3_request(request);
908
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500909 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 kfree(req);
911}
912
Felipe Balbi42626912018-04-09 13:01:43 +0300913/**
914 * dwc3_ep_prev_trb - returns the previous TRB in the ring
915 * @dep: The endpoint with the TRB ring
916 * @index: The index of the current TRB in the ring
917 *
918 * Returns the TRB prior to the one pointed to by the index. If the
919 * index is 0, we will wrap backwards, skip the link TRB, and return
920 * the one just before that.
921 */
922static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
923{
924 u8 tmp = index;
925
926 if (!tmp)
927 tmp = DWC3_TRB_NUM - 1;
928
929 return &dep->trb_pool[tmp - 1];
930}
931
932static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
933{
934 struct dwc3_trb *tmp;
935 u8 trbs_left;
936
937 /*
938 * If enqueue & dequeue are equal than it is either full or empty.
939 *
940 * One way to know for sure is if the TRB right before us has HWO bit
941 * set or not. If it has, then we're definitely full and can't fit any
942 * more transfers in our ring.
943 */
944 if (dep->trb_enqueue == dep->trb_dequeue) {
945 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
946 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
947 return 0;
948
949 return DWC3_TRB_NUM - 1;
950 }
951
952 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
953 trbs_left &= (DWC3_TRB_NUM - 1);
954
955 if (dep->trb_dequeue < dep->trb_enqueue)
956 trbs_left--;
957
958 return trbs_left;
959}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300960
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200961static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300962 dma_addr_t dma, unsigned int length, unsigned int chain,
963 unsigned int node, unsigned int stream_id,
964 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700965 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200966{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300967 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800968 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300969 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200970
Felipe Balbif6bafc62012-02-06 11:04:53 +0200971 trb->size = DWC3_TRB_SIZE_LENGTH(length);
972 trb->bpl = lower_32_bits(dma);
973 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200974
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200975 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200976 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200977 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200978 break;
979
980 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300981 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530982 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300983
Manu Gautam40d829f2017-07-19 17:07:10 +0530984 /*
985 * USB Specification 2.0 Section 5.9.2 states that: "If
986 * there is only a single transaction in the microframe,
987 * only a DATA0 data packet PID is used. If there are
988 * two transactions per microframe, DATA1 is used for
989 * the first transaction data packet and DATA0 is used
990 * for the second transaction data packet. If there are
991 * three transactions per microframe, DATA2 is used for
992 * the first transaction data packet, DATA1 is used for
993 * the second, and DATA0 is used for the third."
994 *
995 * IOW, we should satisfy the following cases:
996 *
997 * 1) length <= maxpacket
998 * - DATA0
999 *
1000 * 2) maxpacket < length <= (2 * maxpacket)
1001 * - DATA1, DATA0
1002 *
1003 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1004 * - DATA2, DATA1, DATA0
1005 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001006 if (speed == USB_SPEED_HIGH) {
1007 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301008 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301009 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1010
1011 if (length <= (2 * maxp))
1012 mult--;
1013
1014 if (length <= maxp)
1015 mult--;
1016
1017 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001018 }
1019 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301020 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001021 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001022
1023 /* always enable Interrupt on Missed ISOC */
1024 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001025 break;
1026
1027 case USB_ENDPOINT_XFER_BULK:
1028 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001029 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001030 break;
1031 default:
1032 /*
1033 * This is only possible with faulty memory because we
1034 * checked it already :)
1035 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001036 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1037 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001038 }
1039
Tejas Joglekar244add82018-12-10 16:08:13 +05301040 /*
1041 * Enable Continue on Short Packet
1042 * when endpoint is not a stream capable
1043 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001044 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301045 if (!dep->stream_capable)
1046 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001047
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001048 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001049 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1050 }
1051
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001052 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001053 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001054
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301055 if (chain)
1056 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001057 else if (dep->stream_capable && is_last)
1058 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301059
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001060 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001061 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001062
1063 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001064
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301065 dwc3_ep_inc_enq(dep);
1066
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001067 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001068}
1069
John Youn361572b2016-05-19 17:26:17 -07001070/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001071 * dwc3_prepare_one_trb - setup one TRB from one request
1072 * @dep: endpoint for which this request is prepared
1073 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001074 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001075 * @chain: should this TRB be chained to the next?
1076 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001077 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001078 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001079 */
1080static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001081 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001082 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1083 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001084{
1085 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301086 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001087 unsigned int stream_id = req->request.stream_id;
1088 unsigned int short_not_ok = req->request.short_not_ok;
1089 unsigned int no_interrupt = req->request.no_interrupt;
1090 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301091
Thinh Nguyen2b803572020-09-24 01:21:30 -07001092 if (use_bounce_buffer)
1093 dma = dep->dwc->bounce_addr;
1094 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301095 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001096 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301097 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001098
1099 trb = &dep->trb_pool[dep->trb_enqueue];
1100
1101 if (!req->trb) {
1102 dwc3_gadget_move_started_request(req);
1103 req->trb = trb;
1104 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001105 }
1106
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001107 req->num_trbs++;
1108
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001109 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001110 stream_id, short_not_ok, no_interrupt, is_last,
1111 must_interrupt);
1112}
1113
1114static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1115{
1116 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1117 unsigned int rem = req->request.length % maxp;
1118
1119 if ((req->request.length && req->request.zero && !rem &&
1120 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1121 (!req->direction && rem))
1122 return true;
1123
1124 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001125}
1126
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001127/**
1128 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1129 * @dep: The endpoint that the request belongs to
1130 * @req: The request to prepare
1131 * @entry_length: The last SG entry size
1132 * @node: Indicates whether this is not the first entry (for isoc only)
1133 *
1134 * Return the number of TRBs prepared.
1135 */
1136static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1137 struct dwc3_request *req, unsigned int entry_length,
1138 unsigned int node)
1139{
1140 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1141 unsigned int rem = req->request.length % maxp;
1142 unsigned int num_trbs = 1;
1143
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001144 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001145 num_trbs++;
1146
1147 if (dwc3_calc_trbs_left(dep) < num_trbs)
1148 return 0;
1149
1150 req->needs_extra_trb = num_trbs > 1;
1151
1152 /* Prepare a normal TRB */
1153 if (req->direction || req->request.length)
1154 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001155 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001156
1157 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1158 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1159 dwc3_prepare_one_trb(dep, req,
1160 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001161 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001162
1163 return num_trbs;
1164}
1165
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001166static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001167 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001168{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301169 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001170 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001171 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001172 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301173 unsigned int remaining = req->request.num_mapped_sgs
1174 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001175 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001176 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301177
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001178 /*
1179 * If we resume preparing the request, then get the remaining length of
1180 * the request and resume where we left off.
1181 */
1182 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1183 length -= sg_dma_len(s);
1184
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301185 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001186 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001187 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001188 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001189 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001190
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001191 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1192
1193 length -= trb_length;
1194
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001195 /*
1196 * IOMMU driver is coalescing the list of sgs which shares a
1197 * page boundary into one and giving it to USB driver. With
1198 * this the number of sgs mapped is not equal to the number of
1199 * sgs passed. So mark the chain bit to false if it isthe last
1200 * mapped sg.
1201 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001202 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001203 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001204
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001205 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001206 break;
1207
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001208 if (last_sg) {
1209 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001210 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001211 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001212 /*
1213 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001214 * next SG entry. If not, set interrupt on this TRB to
1215 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001216 * free.
1217 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001218 if (num_trbs_left == 1 || (needs_extra_trb &&
1219 num_trbs_left <= 2 &&
1220 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001221 must_interrupt = true;
1222
1223 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1224 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001225 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001226
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301227 /*
1228 * There can be a situation where all sgs in sglist are not
1229 * queued because of insufficient trb number. To handle this
1230 * case, update start_sg to next sg to be queued, so that
1231 * we have free trbs we can continue queuing from where we
1232 * previously stopped
1233 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001234 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301235 req->start_sg = sg_next(s);
1236
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301237 req->num_queued_sgs++;
1238
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001239 /*
1240 * The number of pending SG entries may not correspond to the
1241 * number of mapped SG entries. If all the data are queued, then
1242 * don't include unused SG entries.
1243 */
1244 if (length == 0) {
1245 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1246 break;
1247 }
1248
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001249 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001250 break;
1251 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001252
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001253 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001254}
1255
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001256static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001257 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001258{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001259 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001260}
1261
Felipe Balbi72246da2011-08-19 18:10:58 +03001262/*
1263 * dwc3_prepare_trbs - setup TRBs from requests
1264 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001265 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001266 * The function goes through the requests list and sets up TRBs for the
1267 * transfers. The function returns once there are no more TRBs available or
1268 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001269 *
1270 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001271 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001272static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001273{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001274 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001275 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001276
1277 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1278
Felipe Balbid86c5a62016-10-25 13:48:52 +03001279 /*
1280 * We can get in a situation where there's a request in the started list
1281 * but there weren't enough TRBs to fully kick it in the first time
1282 * around, so it has been waiting for more TRBs to be freed up.
1283 *
1284 * In that case, we should check if we have a request with pending_sgs
1285 * in the started list and prepare TRBs for that request first,
1286 * otherwise we will prepare TRBs completely out of order and that will
1287 * break things.
1288 */
1289 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001290 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001291 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001292 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001293 return ret;
1294 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001295
1296 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001297 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001298
1299 /*
1300 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1301 * burst capability may try to read and use TRBs beyond the
1302 * active transfer instead of stopping.
1303 */
1304 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001305 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001306 }
1307
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001308 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001309 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001310
1311 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1312 dep->direction);
1313 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001314 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001315
1316 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301317 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301318 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001319 req->num_pending_sgs = req->request.num_mapped_sgs;
1320
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001321 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001322 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001323 if (req->num_pending_sgs)
1324 return ret;
1325 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001326 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001327 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001328
Thinh Nguyen490410b2020-09-24 01:21:55 -07001329 if (!ret || !dwc3_calc_trbs_left(dep))
1330 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001331
1332 /*
1333 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1334 * burst capability may try to read and use TRBs beyond the
1335 * active transfer instead of stopping.
1336 */
1337 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001338 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001340
1341 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001342}
1343
Thinh Nguyen8d990872020-03-29 16:12:57 -07001344static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1345
Felipe Balbi7fdca762017-09-05 14:41:34 +03001346static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001347{
1348 struct dwc3_gadget_ep_cmd_params params;
1349 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001350 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001351 int ret;
1352 u32 cmd;
1353
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001354 /*
1355 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1356 * This happens when we need to stop and restart a transfer such as in
1357 * the case of reinitiating a stream or retrying an isoc transfer.
1358 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001359 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001360 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001361 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001362
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001363 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001364
Thinh Nguyen23384842020-09-30 17:44:38 -07001365 /*
1366 * If there's no new TRB prepared and we don't need to restart a
1367 * transfer, there's no need to update the transfer.
1368 */
1369 if (!ret && !starting)
1370 return ret;
1371
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001372 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001373 if (!req) {
1374 dep->flags |= DWC3_EP_PENDING_REQUEST;
1375 return 0;
1376 }
1377
1378 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001379
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001380 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301381 params.param0 = upper_32_bits(req->trb_dma);
1382 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001383 cmd = DWC3_DEPCMD_STARTTRANSFER;
1384
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301385 if (dep->stream_capable)
1386 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1387
Felipe Balbi7fdca762017-09-05 14:41:34 +03001388 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1389 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301390 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001391 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1392 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301393 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001394
Felipe Balbi2cd47182016-04-12 16:42:43 +03001395 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001396 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001397 struct dwc3_request *tmp;
1398
1399 if (ret == -EAGAIN)
1400 return ret;
1401
1402 dwc3_stop_active_transfer(dep, true, true);
1403
1404 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1405 dwc3_gadget_move_cancelled_request(req);
1406
1407 /* If ep isn't started, then there's no end transfer pending */
1408 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1409 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1410
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 return ret;
1412 }
1413
Thinh Nguyene0d19562020-05-05 19:46:57 -07001414 if (dep->stream_capable && req->request.is_last)
1415 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1416
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 return 0;
1418}
1419
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001420static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1421{
1422 u32 reg;
1423
1424 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1425 return DWC3_DSTS_SOFFN(reg);
1426}
1427
Thinh Nguyend92021f2018-11-14 22:56:54 -08001428/**
1429 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1430 * @dep: isoc endpoint
1431 *
1432 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1433 * microframe number reported by the XferNotReady event for the future frame
1434 * number to start the isoc transfer.
1435 *
1436 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1437 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1438 * XferNotReady event are invalid. The driver uses this number to schedule the
1439 * isochronous transfer and passes it to the START TRANSFER command. Because
1440 * this number is invalid, the command may fail. If BIT[15:14] matches the
1441 * internal 16-bit microframe, the START TRANSFER command will pass and the
1442 * transfer will start at the scheduled time, if it is off by 1, the command
1443 * will still pass, but the transfer will start 2 seconds in the future. For all
1444 * other conditions, the START TRANSFER command will fail with bus-expiry.
1445 *
1446 * In order to workaround this issue, we can test for the correct combination of
1447 * BIT[15:14] by sending START TRANSFER commands with different values of
1448 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1449 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1450 * As the result, within the 4 possible combinations for BIT[15:14], there will
1451 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1452 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1453 * value is the correct combination.
1454 *
1455 * Since there are only 4 outcomes and the results are ordered, we can simply
1456 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1457 * deduce the smaller successful combination.
1458 *
1459 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1460 * of BIT[15:14]. The correct combination is as follow:
1461 *
1462 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1463 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1464 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1465 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1466 *
1467 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1468 * endpoints.
1469 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001470static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301471{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001472 int cmd_status = 0;
1473 bool test0;
1474 bool test1;
1475
1476 while (dep->combo_num < 2) {
1477 struct dwc3_gadget_ep_cmd_params params;
1478 u32 test_frame_number;
1479 u32 cmd;
1480
1481 /*
1482 * Check if we can start isoc transfer on the next interval or
1483 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1484 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001485 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001486 test_frame_number |= dep->combo_num << 14;
1487 test_frame_number += max_t(u32, 4, dep->interval);
1488
1489 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1490 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1491
1492 cmd = DWC3_DEPCMD_STARTTRANSFER;
1493 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1494 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1495
1496 /* Redo if some other failure beside bus-expiry is received */
1497 if (cmd_status && cmd_status != -EAGAIN) {
1498 dep->start_cmd_status = 0;
1499 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001500 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001501 }
1502
1503 /* Store the first test status */
1504 if (dep->combo_num == 0)
1505 dep->start_cmd_status = cmd_status;
1506
1507 dep->combo_num++;
1508
1509 /*
1510 * End the transfer if the START_TRANSFER command is successful
1511 * to wait for the next XferNotReady to test the command again
1512 */
1513 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001514 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001515 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001516 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301517 }
1518
Thinh Nguyend92021f2018-11-14 22:56:54 -08001519 /* test0 and test1 are both completed at this point */
1520 test0 = (dep->start_cmd_status == 0);
1521 test1 = (cmd_status == 0);
1522
1523 if (!test0 && test1)
1524 dep->combo_num = 1;
1525 else if (!test0 && !test1)
1526 dep->combo_num = 2;
1527 else if (test0 && !test1)
1528 dep->combo_num = 3;
1529 else if (test0 && test1)
1530 dep->combo_num = 0;
1531
Michael Grzeschikca143782020-07-01 20:24:51 +02001532 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001533 dep->frame_number |= dep->combo_num << 14;
1534 dep->frame_number += max_t(u32, 4, dep->interval);
1535
1536 /* Reinitialize test variables */
1537 dep->start_cmd_status = 0;
1538 dep->combo_num = 0;
1539
Felipe Balbi25abad62018-08-14 10:41:19 +03001540 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001541}
1542
Felipe Balbi25abad62018-08-14 10:41:19 +03001543static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301544{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001545 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001546 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001547 int ret;
1548 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001549
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001550 if (list_empty(&dep->pending_list) &&
1551 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301552 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001553 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301554 }
1555
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001556 if (!dwc->dis_start_transfer_quirk &&
1557 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1558 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001559 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001560 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001561 }
1562
Michael Olbrichc5a70922020-07-01 20:24:52 +02001563 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001564 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001565 u32 frame = __dwc3_gadget_get_frame(dwc);
1566 bool rollover = frame <
1567 (dep->frame_number & DWC3_FRNUMBER_MASK);
1568
1569 /*
1570 * frame_number is set from XferNotReady and may be already
1571 * out of date. DSTS only provides the lower 14 bit of the
1572 * current frame number. So add the upper two bits of
1573 * frame_number and handle a possible rollover.
1574 * This will provide the correct frame_number unless more than
1575 * rollover has happened since XferNotReady.
1576 */
1577
1578 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1579 frame;
1580 if (rollover)
1581 dep->frame_number += BIT(14);
1582 }
1583
Felipe Balbid5370102018-08-14 10:42:43 +03001584 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1585 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1586
1587 ret = __dwc3_gadget_kick_transfer(dep);
1588 if (ret != -EAGAIN)
1589 break;
1590 }
1591
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001592 /*
1593 * After a number of unsuccessful start attempts due to bus-expiry
1594 * status, issue END_TRANSFER command and retry on the next XferNotReady
1595 * event.
1596 */
1597 if (ret == -EAGAIN) {
1598 struct dwc3_gadget_ep_cmd_params params;
1599 u32 cmd;
1600
1601 cmd = DWC3_DEPCMD_ENDTRANSFER |
1602 DWC3_DEPCMD_CMDIOC |
1603 DWC3_DEPCMD_PARAM(dep->resource_index);
1604
1605 dep->resource_index = 0;
1606 memset(&params, 0, sizeof(params));
1607
1608 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1609 if (!ret)
1610 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1611 }
1612
Felipe Balbid5370102018-08-14 10:42:43 +03001613 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301614}
1615
Felipe Balbi72246da2011-08-19 18:10:58 +03001616static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1617{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001618 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001619
Wesley Chengae7e8612020-09-28 17:20:59 -07001620 if (!dep->endpoint.desc || !dwc->pullups_connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001621 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1622 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001623 return -ESHUTDOWN;
1624 }
1625
Felipe Balbi04fb3652017-05-17 15:57:45 +03001626 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1627 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001628 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001629
Felipe Balbib2b6d602019-01-11 12:58:52 +02001630 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1631 "%s: request %pK already in flight\n",
1632 dep->name, &req->request))
1633 return -EINVAL;
1634
Felipe Balbifc8bb912016-05-16 13:14:48 +03001635 pm_runtime_get(dwc->dev);
1636
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 req->request.actual = 0;
1638 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001639
Felipe Balbife84f522015-09-01 09:01:38 -05001640 trace_dwc3_ep_queue(req);
1641
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001642 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001643 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001644
Thinh Nguyene0d19562020-05-05 19:46:57 -07001645 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1646 return 0;
1647
Thinh Nguyenc5036722020-09-02 18:42:58 -07001648 /*
1649 * Start the transfer only after the END_TRANSFER is completed
1650 * and endpoint STALL is cleared.
1651 */
1652 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1653 (dep->flags & DWC3_EP_WEDGE) ||
1654 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001655 dep->flags |= DWC3_EP_DELAY_START;
1656 return 0;
1657 }
1658
Felipe Balbid889c232016-09-29 15:44:29 +03001659 /*
1660 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1661 * wait for a XferNotReady event so we will know what's the current
1662 * (micro-)frame number.
1663 *
1664 * Without this trick, we are very, very likely gonna get Bus Expiry
1665 * errors which will force us issue EndTransfer command.
1666 */
1667 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001668 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1669 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001670 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001671
1672 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001673 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001674 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001675 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001676 }
1677
Felipe Balbi7fdca762017-09-05 14:41:34 +03001678 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001679}
1680
1681static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1682 gfp_t gfp_flags)
1683{
1684 struct dwc3_request *req = to_dwc3_request(request);
1685 struct dwc3_ep *dep = to_dwc3_ep(ep);
1686 struct dwc3 *dwc = dep->dwc;
1687
1688 unsigned long flags;
1689
1690 int ret;
1691
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001692 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 ret = __dwc3_gadget_ep_queue(dep, req);
1694 spin_unlock_irqrestore(&dwc->lock, flags);
1695
1696 return ret;
1697}
1698
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001699static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1700{
1701 int i;
1702
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001703 /* If req->trb is not set, then the request has not started */
1704 if (!req->trb)
1705 return;
1706
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001707 /*
1708 * If request was already started, this means we had to
1709 * stop the transfer. With that we also need to ignore
1710 * all TRBs used by the request, however TRBs can only
1711 * be modified after completion of END_TRANSFER
1712 * command. So what we do here is that we wait for
1713 * END_TRANSFER completion and only after that, we jump
1714 * over TRBs by clearing HWO and incrementing dequeue
1715 * pointer.
1716 */
1717 for (i = 0; i < req->num_trbs; i++) {
1718 struct dwc3_trb *trb;
1719
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001720 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001721 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1722 dwc3_ep_inc_deq(dep);
1723 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001724
1725 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001726}
1727
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001728static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1729{
1730 struct dwc3_request *req;
1731 struct dwc3_request *tmp;
1732
1733 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1734 dwc3_gadget_ep_skip_trbs(dep, req);
1735 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1736 }
1737}
1738
Felipe Balbi72246da2011-08-19 18:10:58 +03001739static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1740 struct usb_request *request)
1741{
1742 struct dwc3_request *req = to_dwc3_request(request);
1743 struct dwc3_request *r = NULL;
1744
1745 struct dwc3_ep *dep = to_dwc3_ep(ep);
1746 struct dwc3 *dwc = dep->dwc;
1747
1748 unsigned long flags;
1749 int ret = 0;
1750
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001751 trace_dwc3_ep_dequeue(req);
1752
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 spin_lock_irqsave(&dwc->lock, flags);
1754
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001755 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001756 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001757 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 }
1759
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001761 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001762 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1763 goto out;
1764 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 }
1766
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001767 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001769 struct dwc3_request *t;
1770
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001772 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001773
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001774 /*
1775 * Remove any started request if the transfer is
1776 * cancelled.
1777 */
1778 list_for_each_entry_safe(r, t, &dep->started_list, list)
1779 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001780
Thinh Nguyena5c76822021-01-04 22:42:39 -08001781 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1782
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001783 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 }
1786
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001787 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1788 request, ep->name);
1789 ret = -EINVAL;
1790out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001791 spin_unlock_irqrestore(&dwc->lock, flags);
1792
1793 return ret;
1794}
1795
Felipe Balbi7a608552014-09-24 14:19:52 -05001796int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001797{
1798 struct dwc3_gadget_ep_cmd_params params;
1799 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001800 struct dwc3_request *req;
1801 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 int ret;
1803
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001804 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1805 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1806 return -EINVAL;
1807 }
1808
Felipe Balbi72246da2011-08-19 18:10:58 +03001809 memset(&params, 0x00, sizeof(params));
1810
1811 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001812 struct dwc3_trb *trb;
1813
Felipe Balbie319bd62020-08-13 08:35:38 +03001814 unsigned int transfer_in_flight;
1815 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001816
1817 if (dep->number > 1)
1818 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1819 else
1820 trb = &dwc->ep0_trb[dep->trb_enqueue];
1821
1822 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1823 started = !list_empty(&dep->started_list);
1824
1825 if (!protocol && ((dep->direction && transfer_in_flight) ||
1826 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001827 return -EAGAIN;
1828 }
1829
Felipe Balbi2cd47182016-04-12 16:42:43 +03001830 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1831 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001833 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001834 dep->name);
1835 else
1836 dep->flags |= DWC3_EP_STALL;
1837 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001838 /*
1839 * Don't issue CLEAR_STALL command to control endpoints. The
1840 * controller automatically clears the STALL when it receives
1841 * the SETUP token.
1842 */
1843 if (dep->number <= 1) {
1844 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1845 return 0;
1846 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001847
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001848 dwc3_stop_active_transfer(dep, true, true);
1849
1850 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1851 dwc3_gadget_move_cancelled_request(req);
1852
1853 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1854 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1855 return 0;
1856 }
1857
1858 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1859
John Youn50c763f2016-05-31 17:49:56 -07001860 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001861 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001862 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001864 return ret;
1865 }
1866
1867 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1868
Thinh Nguyenc5036722020-09-02 18:42:58 -07001869 if ((dep->flags & DWC3_EP_DELAY_START) &&
1870 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1871 __dwc3_gadget_kick_transfer(dep);
1872
1873 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001874 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001875
Felipe Balbi72246da2011-08-19 18:10:58 +03001876 return ret;
1877}
1878
1879static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1880{
1881 struct dwc3_ep *dep = to_dwc3_ep(ep);
1882 struct dwc3 *dwc = dep->dwc;
1883
1884 unsigned long flags;
1885
1886 int ret;
1887
1888 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001889 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 spin_unlock_irqrestore(&dwc->lock, flags);
1891
1892 return ret;
1893}
1894
1895static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1896{
1897 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001898 struct dwc3 *dwc = dep->dwc;
1899 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001900 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001901
Paul Zimmerman249a4562012-02-24 17:32:16 -08001902 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001903 dep->flags |= DWC3_EP_WEDGE;
1904
Pratyush Anand08f0d962012-06-25 22:40:43 +05301905 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001906 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301907 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001908 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001909 spin_unlock_irqrestore(&dwc->lock, flags);
1910
1911 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001912}
1913
1914/* -------------------------------------------------------------------------- */
1915
1916static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1917 .bLength = USB_DT_ENDPOINT_SIZE,
1918 .bDescriptorType = USB_DT_ENDPOINT,
1919 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1920};
1921
1922static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1923 .enable = dwc3_gadget_ep0_enable,
1924 .disable = dwc3_gadget_ep0_disable,
1925 .alloc_request = dwc3_gadget_ep_alloc_request,
1926 .free_request = dwc3_gadget_ep_free_request,
1927 .queue = dwc3_gadget_ep0_queue,
1928 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301929 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001930 .set_wedge = dwc3_gadget_ep_set_wedge,
1931};
1932
1933static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1934 .enable = dwc3_gadget_ep_enable,
1935 .disable = dwc3_gadget_ep_disable,
1936 .alloc_request = dwc3_gadget_ep_alloc_request,
1937 .free_request = dwc3_gadget_ep_free_request,
1938 .queue = dwc3_gadget_ep_queue,
1939 .dequeue = dwc3_gadget_ep_dequeue,
1940 .set_halt = dwc3_gadget_ep_set_halt,
1941 .set_wedge = dwc3_gadget_ep_set_wedge,
1942};
1943
1944/* -------------------------------------------------------------------------- */
1945
1946static int dwc3_gadget_get_frame(struct usb_gadget *g)
1947{
1948 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001949
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001950 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001951}
1952
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001953static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001954{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001955 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001956
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001957 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001958 u32 reg;
1959
Felipe Balbi72246da2011-08-19 18:10:58 +03001960 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001961
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 /*
1963 * According to the Databook Remote wakeup request should
1964 * be issued only when the device is in early suspend state.
1965 *
1966 * We can check that via USB Link State bits in DSTS register.
1967 */
1968 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1969
Felipe Balbi72246da2011-08-19 18:10:58 +03001970 link_state = DWC3_DSTS_USBLNKST(reg);
1971
1972 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001973 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001974 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1975 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001976 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001977 break;
1978 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001979 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001980 }
1981
Felipe Balbi8598bde2012-01-02 18:55:57 +02001982 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1983 if (ret < 0) {
1984 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001985 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001986 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001987
Paul Zimmerman802fde92012-04-27 13:10:52 +03001988 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001989 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001990 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001991 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001992 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1993 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1994 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001995
Paul Zimmerman1d046792012-02-15 18:56:56 -08001996 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001997 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001999 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002000 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2001
2002 /* in HS, means ON */
2003 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2004 break;
2005 }
2006
2007 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2008 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002009 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002010 }
2011
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002012 return 0;
2013}
2014
2015static int dwc3_gadget_wakeup(struct usb_gadget *g)
2016{
2017 struct dwc3 *dwc = gadget_to_dwc(g);
2018 unsigned long flags;
2019 int ret;
2020
2021 spin_lock_irqsave(&dwc->lock, flags);
2022 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002023 spin_unlock_irqrestore(&dwc->lock, flags);
2024
2025 return ret;
2026}
2027
2028static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2029 int is_selfpowered)
2030{
2031 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002032 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002033
Paul Zimmerman249a4562012-02-24 17:32:16 -08002034 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002035 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002036 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002037
2038 return 0;
2039}
2040
Wesley Chengae7e8612020-09-28 17:20:59 -07002041static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2042{
2043 u32 epnum;
2044
2045 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2046 struct dwc3_ep *dep;
2047
2048 dep = dwc->eps[epnum];
2049 if (!dep)
2050 continue;
2051
2052 dwc3_remove_requests(dwc, dep);
2053 }
2054}
2055
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002056static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2057{
2058 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2059 u32 reg;
2060
2061 if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2062 ssp_rate = dwc->max_ssp_rate;
2063
2064 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2065 reg &= ~DWC3_DCFG_SPEED_MASK;
2066 reg &= ~DWC3_DCFG_NUMLANES(~0);
2067
2068 if (ssp_rate == USB_SSP_GEN_1x2)
2069 reg |= DWC3_DCFG_SUPERSPEED;
2070 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2071 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2072
2073 if (ssp_rate != USB_SSP_GEN_2x1 &&
2074 dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2075 reg |= DWC3_DCFG_NUMLANES(1);
2076
2077 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2078}
2079
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002080static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2081{
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002082 enum usb_device_speed speed;
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002083 u32 reg;
2084
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002085 speed = dwc->gadget_max_speed;
2086 if (speed > dwc->maximum_speed)
2087 speed = dwc->maximum_speed;
2088
2089 if (speed == USB_SPEED_SUPER_PLUS &&
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002090 DWC3_IP_IS(DWC32)) {
2091 __dwc3_gadget_set_ssp_rate(dwc);
2092 return;
2093 }
2094
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002095 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2096 reg &= ~(DWC3_DCFG_SPEED_MASK);
2097
2098 /*
2099 * WORKAROUND: DWC3 revision < 2.20a have an issue
2100 * which would cause metastability state on Run/Stop
2101 * bit if we try to force the IP to USB2-only mode.
2102 *
2103 * Because of that, we cannot configure the IP to any
2104 * speed other than the SuperSpeed
2105 *
2106 * Refers to:
2107 *
2108 * STAR#9000525659: Clock Domain Crossing on DCTL in
2109 * USB 2.0 Mode
2110 */
2111 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2112 !dwc->dis_metastability_quirk) {
2113 reg |= DWC3_DCFG_SUPERSPEED;
2114 } else {
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002115 switch (speed) {
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002116 case USB_SPEED_LOW:
2117 reg |= DWC3_DCFG_LOWSPEED;
2118 break;
2119 case USB_SPEED_FULL:
2120 reg |= DWC3_DCFG_FULLSPEED;
2121 break;
2122 case USB_SPEED_HIGH:
2123 reg |= DWC3_DCFG_HIGHSPEED;
2124 break;
2125 case USB_SPEED_SUPER:
2126 reg |= DWC3_DCFG_SUPERSPEED;
2127 break;
2128 case USB_SPEED_SUPER_PLUS:
2129 if (DWC3_IP_IS(DWC3))
2130 reg |= DWC3_DCFG_SUPERSPEED;
2131 else
2132 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2133 break;
2134 default:
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002135 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002136
2137 if (DWC3_IP_IS(DWC3))
2138 reg |= DWC3_DCFG_SUPERSPEED;
2139 else
2140 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2141 }
2142 }
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002143
2144 if (DWC3_IP_IS(DWC32) &&
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002145 speed > USB_SPEED_UNKNOWN &&
2146 speed < USB_SPEED_SUPER_PLUS)
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002147 reg &= ~DWC3_DCFG_NUMLANES(~0);
2148
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002149 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2150}
2151
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002152static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002153{
2154 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002155 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002156
Felipe Balbifc8bb912016-05-16 13:14:48 +03002157 if (pm_runtime_suspended(dwc->dev))
2158 return 0;
2159
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002161 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002162 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002163 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2164 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2165 }
2166
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002167 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002168 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2169 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002170
2171 if (dwc->has_hibernation)
2172 reg |= DWC3_DCTL_KEEP_CONNECT;
2173
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002174 __dwc3_gadget_set_speed(dwc);
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002175 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002176 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002177 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002178
2179 if (dwc->has_hibernation && !suspend)
2180 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2181
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002182 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002183 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002184
Thinh Nguyen5b738212019-10-23 19:15:43 -07002185 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002186
2187 do {
2188 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002189 reg &= DWC3_DSTS_DEVCTRLHLT;
2190 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002191
2192 if (!timeout)
2193 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002194
Pratyush Anand6f17f742012-07-02 10:21:55 +05302195 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002196}
2197
Wesley Chengae7e8612020-09-28 17:20:59 -07002198static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2199static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chenga1383b32020-12-29 15:00:37 -08002200static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002201
Felipe Balbi72246da2011-08-19 18:10:58 +03002202static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2203{
2204 struct dwc3 *dwc = gadget_to_dwc(g);
2205 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302206 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002207
2208 is_on = !!is_on;
2209
Baolin Wangbb014732016-10-14 17:11:33 +08002210 /*
2211 * Per databook, when we want to stop the gadget, if a control transfer
2212 * is still in process, complete it and get the core into setup phase.
2213 */
2214 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2215 reinit_completion(&dwc->ep0_in_setup);
2216
2217 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2218 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2219 if (ret == 0) {
2220 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2221 return -ETIMEDOUT;
2222 }
2223 }
2224
Wesley Chengae7e8612020-09-28 17:20:59 -07002225 /*
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002226 * Check the return value for successful resume, or error. For a
2227 * successful resume, the DWC3 runtime PM resume routine will handle
2228 * the run stop sequence, so avoid duplicate operations here.
2229 */
2230 ret = pm_runtime_get_sync(dwc->dev);
2231 if (!ret || ret < 0) {
2232 pm_runtime_put(dwc->dev);
2233 return 0;
2234 }
2235
2236 /*
Wesley Chengae7e8612020-09-28 17:20:59 -07002237 * Synchronize any pending event handling before executing the controller
2238 * halt routine.
2239 */
2240 if (!is_on) {
2241 dwc3_gadget_disable_irq(dwc);
2242 synchronize_irq(dwc->irq_gadget);
2243 }
2244
Felipe Balbi72246da2011-08-19 18:10:58 +03002245 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002246
2247 if (!is_on) {
2248 u32 count;
2249
2250 /*
2251 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2252 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2253 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2254 * command for any active transfers" before clearing the RunStop
2255 * bit.
2256 */
2257 dwc3_stop_active_transfers(dwc);
2258 __dwc3_gadget_stop(dwc);
2259
2260 /*
2261 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2262 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2263 * "software needs to acknowledge the events that are generated
2264 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2265 * to be set to '1'."
2266 */
2267 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2268 count &= DWC3_GEVNTCOUNT_MASK;
2269 if (count > 0) {
2270 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2271 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2272 dwc->ev_buf->length;
2273 }
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002274 dwc->connected = false;
Wesley Chenga1383b32020-12-29 15:00:37 -08002275 } else {
2276 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002277 }
2278
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002279 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002280 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002281 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002282
Pratyush Anand6f17f742012-07-02 10:21:55 +05302283 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002284}
2285
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002286static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2287{
2288 u32 reg;
2289
2290 /* Enable all but Start and End of Frame IRQs */
Thinh Nguyen132ee0d2021-01-13 19:55:29 -08002291 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002292 DWC3_DEVTEN_CMDCMPLTEN |
2293 DWC3_DEVTEN_ERRTICERREN |
2294 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002295 DWC3_DEVTEN_CONNECTDONEEN |
2296 DWC3_DEVTEN_USBRSTEN |
2297 DWC3_DEVTEN_DISCONNEVTEN);
2298
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002299 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002300 reg |= DWC3_DEVTEN_ULSTCNGEN;
2301
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002302 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2303}
2304
2305static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2306{
2307 /* mask all interrupts */
2308 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2309}
2310
2311static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002312static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002313
Felipe Balbi4e994722016-05-13 14:09:59 +03002314/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002315 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2316 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002317 *
2318 * The following looks like complex but it's actually very simple. In order to
2319 * calculate the number of packets we can burst at once on OUT transfers, we're
2320 * gonna use RxFIFO size.
2321 *
2322 * To calculate RxFIFO size we need two numbers:
2323 * MDWIDTH = size, in bits, of the internal memory bus
2324 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2325 *
2326 * Given these two numbers, the formula is simple:
2327 *
2328 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2329 *
2330 * 24 bytes is for 3x SETUP packets
2331 * 16 bytes is a clock domain crossing tolerance
2332 *
2333 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2334 */
2335static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2336{
2337 u32 ram2_depth;
2338 u32 mdwidth;
2339 u32 nump;
2340 u32 reg;
2341
2342 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2343 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002344 if (DWC3_IP_IS(DWC32))
2345 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002346
2347 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2348 nump = min_t(u32, nump, 16);
2349
2350 /* update NumP */
2351 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2352 reg &= ~DWC3_DCFG_NUMP_MASK;
2353 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2354 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2355}
2356
Felipe Balbid7be2952016-05-04 15:49:37 +03002357static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002358{
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002360 int ret = 0;
2361 u32 reg;
2362
John Youncf40b862016-11-14 12:32:43 -08002363 /*
2364 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2365 * the core supports IMOD, disable it.
2366 */
2367 if (dwc->imod_interval) {
2368 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2369 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2370 } else if (dwc3_has_imod(dwc)) {
2371 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2372 }
2373
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002374 /*
2375 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2376 * field instead of letting dwc3 itself calculate that automatically.
2377 *
2378 * This way, we maximize the chances that we'll be able to get several
2379 * bursts of data without going through any sort of endpoint throttling.
2380 */
2381 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002382 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002383 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002384 else
2385 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002386
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002387 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2388
Felipe Balbi4e994722016-05-13 14:09:59 +03002389 dwc3_gadget_setup_nump(dwc);
2390
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 /* Start with SuperSpeed Default */
2392 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2393
2394 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002395 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002396 if (ret) {
2397 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002398 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002399 }
2400
2401 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002402 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002403 if (ret) {
2404 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002405 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002406 }
2407
2408 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002409 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002410 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 dwc3_ep0_out_start(dwc);
2412
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002413 dwc3_gadget_enable_irq(dwc);
2414
Felipe Balbid7be2952016-05-04 15:49:37 +03002415 return 0;
2416
2417err1:
2418 __dwc3_gadget_ep_disable(dwc->eps[0]);
2419
2420err0:
2421 return ret;
2422}
2423
2424static int dwc3_gadget_start(struct usb_gadget *g,
2425 struct usb_gadget_driver *driver)
2426{
2427 struct dwc3 *dwc = gadget_to_dwc(g);
2428 unsigned long flags;
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002429 int ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002430 int irq;
2431
Roger Quadros9522def2016-06-10 14:48:38 +03002432 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002433 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2434 IRQF_SHARED, "dwc3", dwc->ev_buf);
2435 if (ret) {
2436 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2437 irq, ret);
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002438 return ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002439 }
2440
2441 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002442 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002443 spin_unlock_irqrestore(&dwc->lock, flags);
2444
2445 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002446}
2447
Felipe Balbid7be2952016-05-04 15:49:37 +03002448static void __dwc3_gadget_stop(struct dwc3 *dwc)
2449{
2450 dwc3_gadget_disable_irq(dwc);
2451 __dwc3_gadget_ep_disable(dwc->eps[0]);
2452 __dwc3_gadget_ep_disable(dwc->eps[1]);
2453}
2454
Felipe Balbi22835b82014-10-17 12:05:12 -05002455static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002456{
2457 struct dwc3 *dwc = gadget_to_dwc(g);
2458 unsigned long flags;
2459
2460 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002461 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002462 spin_unlock_irqrestore(&dwc->lock, flags);
2463
Felipe Balbi3f308d12016-05-16 14:17:06 +03002464 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002465
Felipe Balbi72246da2011-08-19 18:10:58 +03002466 return 0;
2467}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002468
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302469static void dwc3_gadget_config_params(struct usb_gadget *g,
2470 struct usb_dcd_config_params *params)
2471{
2472 struct dwc3 *dwc = gadget_to_dwc(g);
2473
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002474 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2475 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2476
2477 /* Recommended BESL */
2478 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002479 /*
2480 * If the recommended BESL baseline is 0 or if the BESL deep is
2481 * less than 2, Microsoft's Windows 10 host usb stack will issue
2482 * a usb reset immediately after it receives the extended BOS
2483 * descriptor and the enumeration will fail. To maintain
2484 * compatibility with the Windows' usb stack, let's set the
2485 * recommended BESL baseline to 1 and clamp the BESL deep to be
2486 * within 2 to 15.
2487 */
2488 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002489 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002490 params->besl_deep =
2491 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002492 }
2493
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302494 /* U1 Device exit Latency */
2495 if (dwc->dis_u1_entry_quirk)
2496 params->bU1devExitLat = 0;
2497 else
2498 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2499
2500 /* U2 Device exit Latency */
2501 if (dwc->dis_u2_entry_quirk)
2502 params->bU2DevExitLat = 0;
2503 else
2504 params->bU2DevExitLat =
2505 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2506}
2507
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002508static void dwc3_gadget_set_speed(struct usb_gadget *g,
2509 enum usb_device_speed speed)
2510{
2511 struct dwc3 *dwc = gadget_to_dwc(g);
2512 unsigned long flags;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002513
2514 spin_lock_irqsave(&dwc->lock, flags);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002515 dwc->gadget_max_speed = speed;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002516 spin_unlock_irqrestore(&dwc->lock, flags);
2517}
2518
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002519static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
2520 enum usb_ssp_rate rate)
2521{
2522 struct dwc3 *dwc = gadget_to_dwc(g);
2523 unsigned long flags;
2524
2525 spin_lock_irqsave(&dwc->lock, flags);
2526 dwc->gadget_ssp_rate = rate;
2527 spin_unlock_irqrestore(&dwc->lock, flags);
2528}
2529
Wesley Cheng82c46b82020-12-29 15:03:29 -08002530static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2531{
2532 struct dwc3 *dwc = gadget_to_dwc(g);
2533
2534 if (dwc->usb2_phy)
2535 return usb_phy_set_power(dwc->usb2_phy, mA);
2536
2537 return 0;
2538}
2539
Felipe Balbi72246da2011-08-19 18:10:58 +03002540static const struct usb_gadget_ops dwc3_gadget_ops = {
2541 .get_frame = dwc3_gadget_get_frame,
2542 .wakeup = dwc3_gadget_wakeup,
2543 .set_selfpowered = dwc3_gadget_set_selfpowered,
2544 .pullup = dwc3_gadget_pullup,
2545 .udc_start = dwc3_gadget_start,
2546 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002547 .udc_set_speed = dwc3_gadget_set_speed,
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002548 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302549 .get_config_params = dwc3_gadget_config_params,
Wesley Cheng82c46b82020-12-29 15:03:29 -08002550 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002551};
2552
2553/* -------------------------------------------------------------------------- */
2554
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002555static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2556{
2557 struct dwc3 *dwc = dep->dwc;
2558
2559 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2560 dep->endpoint.maxburst = 1;
2561 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2562 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002563 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002564
2565 dep->endpoint.caps.type_control = true;
2566
2567 return 0;
2568}
2569
2570static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2571{
2572 struct dwc3 *dwc = dep->dwc;
2573 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002574 int size;
2575
2576 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002577 if (DWC3_IP_IS(DWC32))
2578 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2579
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002580 /* MDWIDTH is represented in bits, we need it in bytes */
2581 mdwidth /= 8;
2582
2583 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002584 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002585 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002586 else
2587 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002588
2589 /* FIFO Depth is in MDWDITH bytes. Multiply */
2590 size *= mdwidth;
2591
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002592 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002593 * To meet performance requirement, a minimum TxFIFO size of 3x
2594 * MaxPacketSize is recommended for endpoints that support burst and a
2595 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2596 * support burst. Use those numbers and we can calculate the max packet
2597 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002598 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002599 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2600 size /= 3;
2601 else
2602 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002603
2604 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2605
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002606 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002607 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2608 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002609 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002610 dep->endpoint.caps.type_iso = true;
2611 dep->endpoint.caps.type_bulk = true;
2612 dep->endpoint.caps.type_int = true;
2613
2614 return dwc3_alloc_trb_pool(dep);
2615}
2616
2617static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2618{
2619 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002620 int mdwidth;
2621 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002622
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002623 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002624 if (DWC3_IP_IS(DWC32))
2625 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002626
2627 /* MDWIDTH is represented in bits, convert to bytes */
2628 mdwidth /= 8;
2629
2630 /* All OUT endpoints share a single RxFIFO space */
2631 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002632 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002633 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002634 else
2635 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002636
2637 /* FIFO depth is in MDWDITH bytes */
2638 size *= mdwidth;
2639
2640 /*
2641 * To meet performance requirement, a minimum recommended RxFIFO size
2642 * is defined as follow:
2643 * RxFIFO size >= (3 x MaxPacketSize) +
2644 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2645 *
2646 * Then calculate the max packet limit as below.
2647 */
2648 size -= (3 * 8) + 16;
2649 if (size < 0)
2650 size = 0;
2651 else
2652 size /= 3;
2653
2654 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002655 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002656 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2657 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002658 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002659 dep->endpoint.caps.type_iso = true;
2660 dep->endpoint.caps.type_bulk = true;
2661 dep->endpoint.caps.type_int = true;
2662
2663 return dwc3_alloc_trb_pool(dep);
2664}
2665
2666static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002667{
2668 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002669 bool direction = epnum & 1;
2670 int ret;
2671 u8 num = epnum >> 1;
2672
2673 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2674 if (!dep)
2675 return -ENOMEM;
2676
2677 dep->dwc = dwc;
2678 dep->number = epnum;
2679 dep->direction = direction;
2680 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2681 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002682 dep->combo_num = 0;
2683 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002684
2685 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2686 direction ? "in" : "out");
2687
2688 dep->endpoint.name = dep->name;
2689
2690 if (!(dep->number > 1)) {
2691 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2692 dep->endpoint.comp_desc = NULL;
2693 }
2694
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002695 if (num == 0)
2696 ret = dwc3_gadget_init_control_endpoint(dep);
2697 else if (direction)
2698 ret = dwc3_gadget_init_in_endpoint(dep);
2699 else
2700 ret = dwc3_gadget_init_out_endpoint(dep);
2701
2702 if (ret)
2703 return ret;
2704
2705 dep->endpoint.caps.dir_in = direction;
2706 dep->endpoint.caps.dir_out = !direction;
2707
2708 INIT_LIST_HEAD(&dep->pending_list);
2709 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002710 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002711
2712 return 0;
2713}
2714
2715static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2716{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002717 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002718
Peter Chene81a7012020-08-21 10:55:48 +08002719 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002720
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002721 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002722 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002723
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002724 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2725 if (ret)
2726 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002727 }
2728
2729 return 0;
2730}
2731
2732static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2733{
2734 struct dwc3_ep *dep;
2735 u8 epnum;
2736
2737 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2738 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002739 if (!dep)
2740 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302741 /*
2742 * Physical endpoints 0 and 1 are special; they form the
2743 * bi-directional USB endpoint 0.
2744 *
2745 * For those two physical endpoints, we don't allocate a TRB
2746 * pool nor do we add them the endpoints list. Due to that, we
2747 * shouldn't do these two operations otherwise we would end up
2748 * with all sorts of bugs when removing dwc3.ko.
2749 */
2750 if (epnum != 0 && epnum != 1) {
2751 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002752 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302753 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002754
2755 kfree(dep);
2756 }
2757}
2758
Felipe Balbi72246da2011-08-19 18:10:58 +03002759/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002760
Felipe Balbi8f608e82018-03-27 10:53:29 +03002761static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2762 struct dwc3_request *req, struct dwc3_trb *trb,
2763 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302764{
2765 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302766
Felipe Balbidc55c672016-08-12 13:20:32 +03002767 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002768
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002769 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002770 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002771
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002772 /*
2773 * If we're in the middle of series of chained TRBs and we
2774 * receive a short transfer along the way, DWC3 will skip
2775 * through all TRBs including the last TRB in the chain (the
2776 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2777 * bit and SW has to do it manually.
2778 *
2779 * We're going to do that here to avoid problems of HW trying
2780 * to use bogus TRBs for transfers.
2781 */
2782 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2783 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2784
Felipe Balbic6267a52017-01-05 14:58:46 +02002785 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002786 * For isochronous transfers, the first TRB in a service interval must
2787 * have the Isoc-First type. Track and report its interval frame number.
2788 */
2789 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2790 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2791 unsigned int frame_number;
2792
2793 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2794 frame_number &= ~(dep->interval - 1);
2795 req->request.frame_number = frame_number;
2796 }
2797
2798 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002799 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2800 * this TRB points to the bounce buffer address, it's a MPS alignment
2801 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002802 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002803 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2804 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002805 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2806 return 1;
2807 }
2808
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302809 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002810 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302811
Felipe Balbi35b27192017-03-08 13:56:37 +02002812 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2813 return 1;
2814
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002815 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302816 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002817
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002818 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2819 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302820 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002821
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302822 return 0;
2823}
2824
Felipe Balbid3692952018-03-29 13:32:10 +03002825static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2826 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2827 int status)
2828{
2829 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2830 struct scatterlist *sg = req->sg;
2831 struct scatterlist *s;
2832 unsigned int pending = req->num_pending_sgs;
2833 unsigned int i;
2834 int ret = 0;
2835
2836 for_each_sg(sg, s, pending, i) {
2837 trb = &dep->trb_pool[dep->trb_dequeue];
2838
Felipe Balbid3692952018-03-29 13:32:10 +03002839 req->sg = sg_next(s);
2840 req->num_pending_sgs--;
2841
2842 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2843 trb, event, status, true);
2844 if (ret)
2845 break;
2846 }
2847
2848 return ret;
2849}
2850
2851static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2852 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2853 int status)
2854{
2855 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2856
2857 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2858 event, status, false);
2859}
2860
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002861static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2862{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002863 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002864}
2865
Felipe Balbif38e35d2018-04-06 15:56:35 +03002866static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2867 const struct dwc3_event_depevt *event,
2868 struct dwc3_request *req, int status)
2869{
2870 int ret;
2871
2872 if (req->num_pending_sgs)
2873 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2874 status);
2875 else
2876 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2877 status);
2878
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002879 req->request.actual = req->request.length - req->remaining;
2880
2881 if (!dwc3_gadget_ep_request_completed(req))
2882 goto out;
2883
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002884 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002885 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2886 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002887 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002888 }
2889
Felipe Balbif38e35d2018-04-06 15:56:35 +03002890 dwc3_gadget_giveback(dep, req, status);
2891
2892out:
2893 return ret;
2894}
2895
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002896static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002897 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002898{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002899 struct dwc3_request *req;
2900 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002901
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002902 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002903 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002904
Felipe Balbif38e35d2018-04-06 15:56:35 +03002905 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2906 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002907 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002908 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002909 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002910}
2911
Thinh Nguyend9feef92020-03-31 01:40:42 -07002912static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2913{
2914 struct dwc3_request *req;
2915
2916 if (!list_empty(&dep->pending_list))
2917 return true;
2918
2919 /*
2920 * We only need to check the first entry of the started list. We can
2921 * assume the completed requests are removed from the started list.
2922 */
2923 req = next_request(&dep->started_list);
2924 if (!req)
2925 return false;
2926
2927 return !dwc3_gadget_ep_request_completed(req);
2928}
2929
Felipe Balbiee3638b2018-03-27 11:26:53 +03002930static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2931 const struct dwc3_event_depevt *event)
2932{
Felipe Balbif62afb42018-04-11 10:34:34 +03002933 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002934}
2935
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002936static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2937 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002938{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002939 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002940 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002941
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002942 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002943
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002944 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2945 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002946
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002947 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2948 list_empty(&dep->started_list) &&
2949 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002950 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002951 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002952 if (__dwc3_gadget_kick_transfer(dep) == 0)
2953 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002954
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002955out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002956 /*
2957 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2958 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2959 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002960 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002961 u32 reg;
2962 int i;
2963
2964 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002965 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002966
2967 if (!(dep->flags & DWC3_EP_ENABLED))
2968 continue;
2969
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002970 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002971 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002972 }
2973
2974 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2975 reg |= dwc->u1u2;
2976 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2977
2978 dwc->u1u2 = 0;
2979 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002980
2981 return no_started_trb;
2982}
2983
2984static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2985 const struct dwc3_event_depevt *event)
2986{
2987 int status = 0;
2988
2989 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2990 dwc3_gadget_endpoint_frame_from_event(dep, event);
2991
2992 if (event->status & DEPEVT_STATUS_BUSERR)
2993 status = -ECONNRESET;
2994
2995 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2996 status = -EXDEV;
2997
2998 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002999}
3000
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003001static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3002 const struct dwc3_event_depevt *event)
3003{
3004 int status = 0;
3005
3006 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3007
3008 if (event->status & DEPEVT_STATUS_BUSERR)
3009 status = -ECONNRESET;
3010
Thinh Nguyene0d19562020-05-05 19:46:57 -07003011 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3012 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03003013}
3014
Felipe Balbi8f608e82018-03-27 10:53:29 +03003015static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3016 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03003017{
Felipe Balbiee3638b2018-03-27 11:26:53 +03003018 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07003019
3020 /*
3021 * The XferNotReady event is generated only once before the endpoint
3022 * starts. It will be generated again when END_TRANSFER command is
3023 * issued. For some controller versions, the XferNotReady event may be
3024 * generated while the END_TRANSFER command is still in process. Ignore
3025 * it and wait for the next XferNotReady event after the command is
3026 * completed.
3027 */
3028 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3029 return;
3030
Felipe Balbi25abad62018-08-14 10:41:19 +03003031 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03003032}
3033
Thinh Nguyen8266b082020-07-30 16:29:03 -07003034static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3035 const struct dwc3_event_depevt *event)
3036{
3037 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3038
3039 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3040 return;
3041
3042 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3043 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3044 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3045
3046 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3047 struct dwc3 *dwc = dep->dwc;
3048
3049 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3050 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3051 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3052
3053 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3054 if (dwc->delayed_status)
3055 __dwc3_gadget_ep0_set_halt(ep0, 1);
3056 return;
3057 }
3058
3059 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3060 if (dwc->delayed_status)
3061 dwc3_ep0_send_delayed_status(dwc);
3062 }
3063
3064 if ((dep->flags & DWC3_EP_DELAY_START) &&
3065 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3066 __dwc3_gadget_kick_transfer(dep);
3067
3068 dep->flags &= ~DWC3_EP_DELAY_START;
3069}
3070
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003071static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3072 const struct dwc3_event_depevt *event)
3073{
3074 struct dwc3 *dwc = dep->dwc;
3075
3076 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3077 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3078 goto out;
3079 }
3080
3081 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3082 switch (event->parameters) {
3083 case DEPEVT_STREAM_PRIME:
3084 /*
3085 * If the host can properly transition the endpoint state from
3086 * idle to prime after a NoStream rejection, there's no need to
3087 * force restarting the endpoint to reinitiate the stream. To
3088 * simplify the check, assume the host follows the USB spec if
3089 * it primed the endpoint more than once.
3090 */
3091 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3092 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3093 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3094 else
3095 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3096 }
3097
3098 break;
3099 case DEPEVT_STREAM_NOSTREAM:
3100 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3101 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3102 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3103 break;
3104
3105 /*
3106 * If the host rejects a stream due to no active stream, by the
3107 * USB and xHCI spec, the endpoint will be put back to idle
3108 * state. When the host is ready (buffer added/updated), it will
3109 * prime the endpoint to inform the usb device controller. This
3110 * triggers the device controller to issue ERDY to restart the
3111 * stream. However, some hosts don't follow this and keep the
3112 * endpoint in the idle state. No prime will come despite host
3113 * streams are updated, and the device controller will not be
3114 * triggered to generate ERDY to move the next stream data. To
3115 * workaround this and maintain compatibility with various
3116 * hosts, force to reinitate the stream until the host is ready
3117 * instead of waiting for the host to prime the endpoint.
3118 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003119 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3120 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3121
3122 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3123 } else {
3124 dep->flags |= DWC3_EP_DELAY_START;
3125 dwc3_stop_active_transfer(dep, true, true);
3126 return;
3127 }
3128 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003129 }
3130
3131out:
3132 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3133}
3134
Felipe Balbi72246da2011-08-19 18:10:58 +03003135static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3136 const struct dwc3_event_depevt *event)
3137{
3138 struct dwc3_ep *dep;
3139 u8 epnum = event->endpoint_number;
3140
3141 dep = dwc->eps[epnum];
3142
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003143 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003144 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003145 return;
3146
3147 /* Handle only EPCMDCMPLT when EP disabled */
3148 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3149 return;
3150 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003151
Felipe Balbi72246da2011-08-19 18:10:58 +03003152 if (epnum == 0 || epnum == 1) {
3153 dwc3_ep0_interrupt(dwc, event);
3154 return;
3155 }
3156
3157 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003158 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003159 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003160 break;
3161 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003162 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003163 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003164 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003165 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003166 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003167 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003168 dwc3_gadget_endpoint_transfer_complete(dep, event);
3169 break;
3170 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003171 dwc3_gadget_endpoint_stream_event(dep, event);
3172 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003173 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003174 break;
3175 }
3176}
3177
3178static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3179{
3180 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3181 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003182 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003183 spin_lock(&dwc->lock);
3184 }
3185}
3186
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003187static void dwc3_suspend_gadget(struct dwc3 *dwc)
3188{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003189 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003190 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003191 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003192 spin_lock(&dwc->lock);
3193 }
3194}
3195
3196static void dwc3_resume_gadget(struct dwc3 *dwc)
3197{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003198 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003199 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003200 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003201 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003202 }
3203}
3204
3205static void dwc3_reset_gadget(struct dwc3 *dwc)
3206{
3207 if (!dwc->gadget_driver)
3208 return;
3209
Peter Chene81a7012020-08-21 10:55:48 +08003210 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003211 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003212 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003213 spin_lock(&dwc->lock);
3214 }
3215}
3216
Felipe Balbic5353b22019-02-13 13:00:54 +02003217static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3218 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003219{
Felipe Balbi72246da2011-08-19 18:10:58 +03003220 struct dwc3_gadget_ep_cmd_params params;
3221 u32 cmd;
3222 int ret;
3223
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003224 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3225 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303226 return;
3227
Pratyush Anand57911502012-07-06 15:19:10 +05303228 /*
3229 * NOTICE: We are violating what the Databook says about the
3230 * EndTransfer command. Ideally we would _always_ wait for the
3231 * EndTransfer Command Completion IRQ, but that's causing too
3232 * much trouble synchronizing between us and gadget driver.
3233 *
3234 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003235 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303236 *
3237 * Note also that a similar handling was tested by Synopsys
3238 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003239 * In short, what we're doing is issuing EndTransfer with
3240 * CMDIOC bit set and delay kicking transfer until the
3241 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003242 *
3243 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3244 * supports a mode to work around the above limitation. The
3245 * software can poll the CMDACT bit in the DEPCMD register
3246 * after issuing a EndTransfer command. This mode is enabled
3247 * by writing GUCTL2[14]. This polling is already done in the
3248 * dwc3_send_gadget_ep_cmd() function so if the mode is
3249 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003250 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003251 *
3252 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303253 */
3254
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303255 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003256 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003257 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003258 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303259 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003260 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303261 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003262 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003263
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003264 /*
3265 * The END_TRANSFER command will cause the controller to generate a
3266 * NoStream Event, and it's not due to the host DP NoStream rejection.
3267 * Ignore the next NoStream event.
3268 */
3269 if (dep->stream_capable)
3270 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3271
Thinh Nguyend3abda52019-11-27 13:10:47 -08003272 if (!interrupt)
3273 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003274 else
3275 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003276}
3277
Felipe Balbi72246da2011-08-19 18:10:58 +03003278static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3279{
3280 u32 epnum;
3281
3282 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3283 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003284 int ret;
3285
3286 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003287 if (!dep)
3288 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003289
3290 if (!(dep->flags & DWC3_EP_STALL))
3291 continue;
3292
3293 dep->flags &= ~DWC3_EP_STALL;
3294
John Youn50c763f2016-05-31 17:49:56 -07003295 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003296 WARN_ON_ONCE(ret);
3297 }
3298}
3299
3300static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3301{
Felipe Balbic4430a22012-05-24 10:30:01 +03003302 int reg;
3303
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003304 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3305
Felipe Balbi72246da2011-08-19 18:10:58 +03003306 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3307 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003308 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003309 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003310
Felipe Balbi72246da2011-08-19 18:10:58 +03003311 dwc3_disconnect_gadget(dwc);
3312
Peter Chene81a7012020-08-21 10:55:48 +08003313 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003314 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003315 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003316
3317 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003318}
3319
Felipe Balbi72246da2011-08-19 18:10:58 +03003320static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3321{
3322 u32 reg;
3323
Felipe Balbifc8bb912016-05-16 13:14:48 +03003324 dwc->connected = true;
3325
Felipe Balbidf62df52011-10-14 15:11:49 +03003326 /*
3327 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3328 * would cause a missing Disconnect Event if there's a
3329 * pending Setup Packet in the FIFO.
3330 *
3331 * There's no suggested workaround on the official Bug
3332 * report, which states that "unless the driver/application
3333 * is doing any special handling of a disconnect event,
3334 * there is no functional issue".
3335 *
3336 * Unfortunately, it turns out that we _do_ some special
3337 * handling of a disconnect event, namely complete all
3338 * pending transfers, notify gadget driver of the
3339 * disconnection, and so on.
3340 *
3341 * Our suggested workaround is to follow the Disconnect
3342 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003343 * flag. Such flag gets set whenever we have a SETUP_PENDING
3344 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003345 * same endpoint.
3346 *
3347 * Refers to:
3348 *
3349 * STAR#9000466709: RTL: Device : Disconnect event not
3350 * generated if setup packet pending in FIFO
3351 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003352 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003353 if (dwc->setup_packet_pending)
3354 dwc3_gadget_disconnect_interrupt(dwc);
3355 }
3356
Felipe Balbi8e744752014-11-06 14:27:53 +08003357 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003358 /*
3359 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3360 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3361 * needs to ensure that it sends "a DEPENDXFER command for any active
3362 * transfers."
3363 */
3364 dwc3_stop_active_transfers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003365
3366 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3367 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003368 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003369 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003370 dwc3_clear_stall_all_ep(dwc);
3371
3372 /* Reset device address to zero */
3373 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3374 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3375 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003376}
3377
Felipe Balbi72246da2011-08-19 18:10:58 +03003378static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3379{
Felipe Balbi72246da2011-08-19 18:10:58 +03003380 struct dwc3_ep *dep;
3381 int ret;
3382 u32 reg;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003383 u8 lanes = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003384 u8 speed;
3385
Felipe Balbi72246da2011-08-19 18:10:58 +03003386 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3387 speed = reg & DWC3_DSTS_CONNECTSPD;
3388 dwc->speed = speed;
3389
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003390 if (DWC3_IP_IS(DWC32))
3391 lanes = DWC3_DSTS_CONNLANES(reg) + 1;
3392
3393 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
3394
John Youn5fb6fda2016-11-10 17:23:25 -08003395 /*
3396 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3397 * each time on Connect Done.
3398 *
3399 * Currently we always use the reset value. If any platform
3400 * wants to set this to a different value, we need to add a
3401 * setting and update GCTL.RAMCLKSEL here.
3402 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003403
3404 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003405 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003406 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003407 dwc->gadget->ep0->maxpacket = 512;
3408 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003409
3410 if (lanes > 1)
3411 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
3412 else
3413 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
John Youn75808622016-02-05 17:09:13 -08003414 break;
John Youn2da9ad72016-05-20 16:34:26 -07003415 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003416 /*
3417 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3418 * would cause a missing USB3 Reset event.
3419 *
3420 * In such situations, we should force a USB3 Reset
3421 * event by calling our dwc3_gadget_reset_interrupt()
3422 * routine.
3423 *
3424 * Refers to:
3425 *
3426 * STAR#9000483510: RTL: SS : USB3 reset event may
3427 * not be generated always when the link enters poll
3428 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003429 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003430 dwc3_gadget_reset_interrupt(dwc);
3431
Felipe Balbi72246da2011-08-19 18:10:58 +03003432 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003433 dwc->gadget->ep0->maxpacket = 512;
3434 dwc->gadget->speed = USB_SPEED_SUPER;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003435
3436 if (lanes > 1) {
3437 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3438 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
3439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003440 break;
John Youn2da9ad72016-05-20 16:34:26 -07003441 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003442 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003443 dwc->gadget->ep0->maxpacket = 64;
3444 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003445 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003446 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003447 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003448 dwc->gadget->ep0->maxpacket = 64;
3449 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003450 break;
John Youn2da9ad72016-05-20 16:34:26 -07003451 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003452 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003453 dwc->gadget->ep0->maxpacket = 8;
3454 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003455 break;
3456 }
3457
Peter Chene81a7012020-08-21 10:55:48 +08003458 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003459
Pratyush Anand2b758352013-01-14 15:59:31 +05303460 /* Enable USB2 LPM Capability */
3461
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003462 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003463 (speed != DWC3_DSTS_SUPERSPEED) &&
3464 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303465 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3466 reg |= DWC3_DCFG_LPM_CAP;
3467 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3468
3469 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3470 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3471
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003472 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3473 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303474
Huang Rui80caf7d2014-10-28 19:54:26 +08003475 /*
3476 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3477 * DCFG.LPMCap is set, core responses with an ACK and the
3478 * BESL value in the LPM token is less than or equal to LPM
3479 * NYET threshold.
3480 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003481 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003482 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003483
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003484 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003485 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003486
Thinh Nguyen5b738212019-10-23 19:15:43 -07003487 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003488 } else {
3489 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3490 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003491 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303492 }
3493
Felipe Balbi72246da2011-08-19 18:10:58 +03003494 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003495 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003496 if (ret) {
3497 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3498 return;
3499 }
3500
3501 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003502 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003503 if (ret) {
3504 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3505 return;
3506 }
3507
3508 /*
3509 * Configure PHY via GUSB3PIPECTLn if required.
3510 *
3511 * Update GTXFIFOSIZn
3512 *
3513 * In both cases reset values should be sufficient.
3514 */
3515}
3516
3517static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3518{
Felipe Balbi72246da2011-08-19 18:10:58 +03003519 /*
3520 * TODO take core out of low power mode when that's
3521 * implemented.
3522 */
3523
Jiebing Liad14d4e2014-12-11 13:26:29 +08003524 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3525 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003526 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003527 spin_lock(&dwc->lock);
3528 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003529}
3530
3531static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3532 unsigned int evtinfo)
3533{
Felipe Balbifae2b902011-10-14 13:00:30 +03003534 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003535 unsigned int pwropt;
3536
3537 /*
3538 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3539 * Hibernation mode enabled which would show up when device detects
3540 * host-initiated U3 exit.
3541 *
3542 * In that case, device will generate a Link State Change Interrupt
3543 * from U3 to RESUME which is only necessary if Hibernation is
3544 * configured in.
3545 *
3546 * There are no functional changes due to such spurious event and we
3547 * just need to ignore it.
3548 *
3549 * Refers to:
3550 *
3551 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3552 * operational mode
3553 */
3554 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003555 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003556 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3557 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3558 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003559 return;
3560 }
3561 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003562
3563 /*
3564 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3565 * on the link partner, the USB session might do multiple entry/exit
3566 * of low power states before a transfer takes place.
3567 *
3568 * Due to this problem, we might experience lower throughput. The
3569 * suggested workaround is to disable DCTL[12:9] bits if we're
3570 * transitioning from U1/U2 to U0 and enable those bits again
3571 * after a transfer completes and there are no pending transfers
3572 * on any of the enabled endpoints.
3573 *
3574 * This is the first half of that workaround.
3575 *
3576 * Refers to:
3577 *
3578 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3579 * core send LGO_Ux entering U0
3580 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003581 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003582 if (next == DWC3_LINK_STATE_U0) {
3583 u32 u1u2;
3584 u32 reg;
3585
3586 switch (dwc->link_state) {
3587 case DWC3_LINK_STATE_U1:
3588 case DWC3_LINK_STATE_U2:
3589 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3590 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3591 | DWC3_DCTL_ACCEPTU2ENA
3592 | DWC3_DCTL_INITU1ENA
3593 | DWC3_DCTL_ACCEPTU1ENA);
3594
3595 if (!dwc->u1u2)
3596 dwc->u1u2 = reg & u1u2;
3597
3598 reg &= ~u1u2;
3599
Thinh Nguyen5b738212019-10-23 19:15:43 -07003600 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003601 break;
3602 default:
3603 /* do nothing */
3604 break;
3605 }
3606 }
3607 }
3608
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003609 switch (next) {
3610 case DWC3_LINK_STATE_U1:
3611 if (dwc->speed == USB_SPEED_SUPER)
3612 dwc3_suspend_gadget(dwc);
3613 break;
3614 case DWC3_LINK_STATE_U2:
3615 case DWC3_LINK_STATE_U3:
3616 dwc3_suspend_gadget(dwc);
3617 break;
3618 case DWC3_LINK_STATE_RESUME:
3619 dwc3_resume_gadget(dwc);
3620 break;
3621 default:
3622 /* do nothing */
3623 break;
3624 }
3625
Felipe Balbie57ebc12014-04-22 13:20:12 -05003626 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003627}
3628
Baolin Wang72704f82016-05-16 16:43:53 +08003629static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3630 unsigned int evtinfo)
3631{
3632 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3633
3634 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3635 dwc3_suspend_gadget(dwc);
3636
3637 dwc->link_state = next;
3638}
3639
Felipe Balbie1dadd32014-02-25 14:47:54 -06003640static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3641 unsigned int evtinfo)
3642{
3643 unsigned int is_ss = evtinfo & BIT(4);
3644
Felipe Balbibfad65e2017-04-19 14:59:27 +03003645 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003646 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3647 * have a known issue which can cause USB CV TD.9.23 to fail
3648 * randomly.
3649 *
3650 * Because of this issue, core could generate bogus hibernation
3651 * events which SW needs to ignore.
3652 *
3653 * Refers to:
3654 *
3655 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3656 * Device Fallback from SuperSpeed
3657 */
3658 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3659 return;
3660
3661 /* enter hibernation here */
3662}
3663
Felipe Balbi72246da2011-08-19 18:10:58 +03003664static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3665 const struct dwc3_event_devt *event)
3666{
3667 switch (event->type) {
3668 case DWC3_DEVICE_EVENT_DISCONNECT:
3669 dwc3_gadget_disconnect_interrupt(dwc);
3670 break;
3671 case DWC3_DEVICE_EVENT_RESET:
3672 dwc3_gadget_reset_interrupt(dwc);
3673 break;
3674 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3675 dwc3_gadget_conndone_interrupt(dwc);
3676 break;
3677 case DWC3_DEVICE_EVENT_WAKEUP:
3678 dwc3_gadget_wakeup_interrupt(dwc);
3679 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003680 case DWC3_DEVICE_EVENT_HIBER_REQ:
3681 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3682 "unexpected hibernation event\n"))
3683 break;
3684
3685 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3686 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003687 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3688 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3689 break;
3690 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003691 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003692 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003693 /*
3694 * Ignore suspend event until the gadget enters into
3695 * USB_STATE_CONFIGURED state.
3696 */
Peter Chene81a7012020-08-21 10:55:48 +08003697 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003698 dwc3_gadget_suspend_interrupt(dwc,
3699 event->event_info);
3700 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003701 break;
3702 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003703 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003704 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003705 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003706 break;
3707 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003708 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003709 }
3710}
3711
3712static void dwc3_process_event_entry(struct dwc3 *dwc,
3713 const union dwc3_event *event)
3714{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003715 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003716
Felipe Balbidfc5e802017-04-26 13:44:51 +03003717 if (!event->type.is_devspec)
3718 dwc3_endpoint_interrupt(dwc, &event->depevt);
3719 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003720 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003721 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003722 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003723}
3724
Felipe Balbidea520a2016-03-30 09:39:34 +03003725static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003726{
Felipe Balbidea520a2016-03-30 09:39:34 +03003727 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003728 irqreturn_t ret = IRQ_NONE;
3729 int left;
3730 u32 reg;
3731
Felipe Balbif42f2442013-06-12 21:25:08 +03003732 left = evt->count;
3733
3734 if (!(evt->flags & DWC3_EVENT_PENDING))
3735 return IRQ_NONE;
3736
3737 while (left > 0) {
3738 union dwc3_event event;
3739
John Younebbb2d52016-11-15 13:07:02 +02003740 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003741
3742 dwc3_process_event_entry(dwc, &event);
3743
3744 /*
3745 * FIXME we wrap around correctly to the next entry as
3746 * almost all entries are 4 bytes in size. There is one
3747 * entry which has 12 bytes which is a regular entry
3748 * followed by 8 bytes data. ATM I don't know how
3749 * things are organized if we get next to the a
3750 * boundary so I worry about that once we try to handle
3751 * that.
3752 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003753 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003754 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003755 }
3756
3757 evt->count = 0;
3758 evt->flags &= ~DWC3_EVENT_PENDING;
3759 ret = IRQ_HANDLED;
3760
3761 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003762 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003763 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003764 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003765
John Youncf40b862016-11-14 12:32:43 -08003766 if (dwc->imod_interval) {
3767 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3768 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3769 }
3770
Felipe Balbif42f2442013-06-12 21:25:08 +03003771 return ret;
3772}
3773
Felipe Balbidea520a2016-03-30 09:39:34 +03003774static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003775{
Felipe Balbidea520a2016-03-30 09:39:34 +03003776 struct dwc3_event_buffer *evt = _evt;
3777 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003778 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003779 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003780
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003781 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003782 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003783 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003784
3785 return ret;
3786}
3787
Felipe Balbidea520a2016-03-30 09:39:34 +03003788static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003789{
Felipe Balbidea520a2016-03-30 09:39:34 +03003790 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003791 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003792 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003793 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003794
Felipe Balbifc8bb912016-05-16 13:14:48 +03003795 if (pm_runtime_suspended(dwc->dev)) {
3796 pm_runtime_get(dwc->dev);
3797 disable_irq_nosync(dwc->irq_gadget);
3798 dwc->pending_events = true;
3799 return IRQ_HANDLED;
3800 }
3801
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003802 /*
3803 * With PCIe legacy interrupt, test shows that top-half irq handler can
3804 * be called again after HW interrupt deassertion. Check if bottom-half
3805 * irq event handler completes before caching new event to prevent
3806 * losing events.
3807 */
3808 if (evt->flags & DWC3_EVENT_PENDING)
3809 return IRQ_HANDLED;
3810
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003811 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003812 count &= DWC3_GEVNTCOUNT_MASK;
3813 if (!count)
3814 return IRQ_NONE;
3815
Felipe Balbib15a7622011-06-30 16:57:15 +03003816 evt->count = count;
3817 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003818
Felipe Balbie8adfc32013-06-12 21:11:14 +03003819 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003820 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003821 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003822 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003823
John Younebbb2d52016-11-15 13:07:02 +02003824 amount = min(count, evt->length - evt->lpos);
3825 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3826
3827 if (amount < count)
3828 memcpy(evt->cache, evt->buf, count - amount);
3829
John Youn65aca322016-11-15 13:08:59 +02003830 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3831
Felipe Balbib15a7622011-06-30 16:57:15 +03003832 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003833}
3834
Felipe Balbidea520a2016-03-30 09:39:34 +03003835static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003836{
Felipe Balbidea520a2016-03-30 09:39:34 +03003837 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003838
Felipe Balbidea520a2016-03-30 09:39:34 +03003839 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003840}
3841
Felipe Balbi6db38122016-10-03 11:27:01 +03003842static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3843{
3844 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3845 int irq;
3846
Hans de Goedef146b40b2019-10-05 23:04:48 +02003847 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003848 if (irq > 0)
3849 goto out;
3850
3851 if (irq == -EPROBE_DEFER)
3852 goto out;
3853
Hans de Goedef146b40b2019-10-05 23:04:48 +02003854 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003855 if (irq > 0)
3856 goto out;
3857
3858 if (irq == -EPROBE_DEFER)
3859 goto out;
3860
3861 irq = platform_get_irq(dwc3_pdev, 0);
3862 if (irq > 0)
3863 goto out;
3864
Felipe Balbi6db38122016-10-03 11:27:01 +03003865 if (!irq)
3866 irq = -EINVAL;
3867
3868out:
3869 return irq;
3870}
3871
Peter Chene81a7012020-08-21 10:55:48 +08003872static void dwc_gadget_release(struct device *dev)
3873{
3874 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3875
3876 kfree(gadget);
3877}
3878
Felipe Balbi72246da2011-08-19 18:10:58 +03003879/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003880 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003881 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003882 *
3883 * Returns 0 on success otherwise negative errno.
3884 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003885int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003886{
Felipe Balbi6db38122016-10-03 11:27:01 +03003887 int ret;
3888 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003889 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003890
Felipe Balbi6db38122016-10-03 11:27:01 +03003891 irq = dwc3_gadget_get_irq(dwc);
3892 if (irq < 0) {
3893 ret = irq;
3894 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003895 }
3896
3897 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003898
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303899 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3900 sizeof(*dwc->ep0_trb) * 2,
3901 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003902 if (!dwc->ep0_trb) {
3903 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3904 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003905 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003906 }
3907
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003908 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003909 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003910 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003911 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003912 }
3913
Felipe Balbi905dc042017-01-05 14:46:52 +02003914 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3915 &dwc->bounce_addr, GFP_KERNEL);
3916 if (!dwc->bounce) {
3917 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003918 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003919 }
3920
Baolin Wangbb014732016-10-14 17:11:33 +08003921 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003922 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3923 if (!dwc->gadget) {
3924 ret = -ENOMEM;
3925 goto err3;
3926 }
Baolin Wangbb014732016-10-14 17:11:33 +08003927
Peter Chene81a7012020-08-21 10:55:48 +08003928
3929 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3930 dev = &dwc->gadget->dev;
3931 dev->platform_data = dwc;
3932 dwc->gadget->ops = &dwc3_gadget_ops;
3933 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003934 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
Peter Chene81a7012020-08-21 10:55:48 +08003935 dwc->gadget->sg_supported = true;
3936 dwc->gadget->name = "dwc3-gadget";
3937 dwc->gadget->lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003938
3939 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003940 * FIXME We might be setting max_speed to <SUPER, however versions
3941 * <2.20a of dwc3 have an issue with metastability (documented
3942 * elsewhere in this driver) which tells us we can't set max speed to
3943 * anything lower than SUPER.
3944 *
3945 * Because gadget.max_speed is only used by composite.c and function
3946 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3947 * to happen so we avoid sending SuperSpeed Capability descriptor
3948 * together with our BOS descriptor as that could confuse host into
3949 * thinking we can handle super speed.
3950 *
3951 * Note that, in fact, we won't even support GetBOS requests when speed
3952 * is less than super speed because we don't have means, yet, to tell
3953 * composite.c that we are USB 2.0 + LPM ECN.
3954 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003955 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003956 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003957 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003958 dwc->revision);
3959
Peter Chene81a7012020-08-21 10:55:48 +08003960 dwc->gadget->max_speed = dwc->maximum_speed;
Thinh Nguyen67848142021-01-19 17:36:21 -08003961 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003962
3963 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003964 * REVISIT: Here we should clear all pending IRQs to be
3965 * sure we're starting from a well known location.
3966 */
3967
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003968 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003969 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003970 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003971
3972 ret = usb_add_gadget(dwc->gadget);
3973 if (ret) {
3974 dev_err(dwc->dev, "failed to add gadget\n");
3975 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003976 }
3977
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08003978 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
3979 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
3980 else
3981 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003982
Felipe Balbi72246da2011-08-19 18:10:58 +03003983 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003984
Peter Chene81a7012020-08-21 10:55:48 +08003985err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003986 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003987err4:
3988 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003989err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003990 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3991 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003992
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003993err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003994 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003995
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003996err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303997 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003998 dwc->ep0_trb, dwc->ep0_trb_addr);
3999
Felipe Balbi72246da2011-08-19 18:10:58 +03004000err0:
4001 return ret;
4002}
4003
Felipe Balbi7415f172012-04-30 14:56:33 +03004004/* -------------------------------------------------------------------------- */
4005
Felipe Balbi72246da2011-08-19 18:10:58 +03004006void dwc3_gadget_exit(struct dwc3 *dwc)
4007{
Peter Chene81a7012020-08-21 10:55:48 +08004008 usb_del_gadget_udc(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004009 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02004010 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004011 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004012 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304013 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004014 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004015}
Felipe Balbi7415f172012-04-30 14:56:33 +03004016
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004017int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004018{
Roger Quadros9772b472016-04-12 11:33:29 +03004019 if (!dwc->gadget_driver)
4020 return 0;
4021
Roger Quadros1551e352017-02-15 14:16:26 +02004022 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004023 dwc3_disconnect_gadget(dwc);
4024 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004025
4026 return 0;
4027}
4028
4029int dwc3_gadget_resume(struct dwc3 *dwc)
4030{
Felipe Balbi7415f172012-04-30 14:56:33 +03004031 int ret;
4032
Roger Quadros9772b472016-04-12 11:33:29 +03004033 if (!dwc->gadget_driver)
4034 return 0;
4035
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004036 ret = __dwc3_gadget_start(dwc);
4037 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004038 goto err0;
4039
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004040 ret = dwc3_gadget_run_stop(dwc, true, false);
4041 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004042 goto err1;
4043
Felipe Balbi7415f172012-04-30 14:56:33 +03004044 return 0;
4045
4046err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004047 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004048
4049err0:
4050 return ret;
4051}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004052
4053void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4054{
4055 if (dwc->pending_events) {
4056 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4057 dwc->pending_events = false;
4058 enable_irq(dwc->irq_gadget);
4059 }
4060}