blob: da1f2ad2ad90b560d196155e1dc6e8e4780a1bac [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 Balbi6cb2e4e2016-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) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300608 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 dep->interval = 1 << (desc->bInterval - 1);
610 }
611
Felipe Balbi2cd47182016-04-12 16:42:43 +0300612 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300613}
614
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700615static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
616 bool interrupt);
617
Felipe Balbi72246da2011-08-19 18:10:58 +0300618/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300619 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300620 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300621 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300623 * Caller should take care of locking. Execute all necessary commands to
624 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300626static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300627{
John Youn39ebb052016-11-09 16:36:28 -0800628 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800630
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300632 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633
634 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300635 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 if (ret)
637 return ret;
638 }
639
Felipe Balbib07c2db2018-04-09 12:46:47 +0300640 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 if (ret)
642 return ret;
643
644 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200645 struct dwc3_trb *trb_st_hw;
646 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300647
Felipe Balbi72246da2011-08-19 18:10:58 +0300648 dep->type = usb_endpoint_type(desc);
649 dep->flags |= DWC3_EP_ENABLED;
650
651 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
652 reg |= DWC3_DALEPENA_EP(dep->number);
653 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
654
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300655 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200656 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657
John Youn0d257442016-05-19 17:26:08 -0700658 /* Initialize the TRB ring */
659 dep->trb_dequeue = 0;
660 dep->trb_enqueue = 0;
661 memset(dep->trb_pool, 0,
662 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
663
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300664 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 trb_st_hw = &dep->trb_pool[0];
666
Felipe Balbif6bafc62012-02-06 11:04:53 +0200667 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200668 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
669 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
670 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
671 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300672 }
673
Felipe Balbia97ea992016-09-29 16:28:56 +0300674 /*
675 * Issue StartTransfer here with no-op TRB so we can always rely on No
676 * Response Update Transfer command.
677 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700678 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300679 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300680 struct dwc3_gadget_ep_cmd_params params;
681 struct dwc3_trb *trb;
682 dma_addr_t trb_dma;
683 u32 cmd;
684
685 memset(&params, 0, sizeof(params));
686 trb = &dep->trb_pool[0];
687 trb_dma = dwc3_trb_dma_offset(dep, trb);
688
689 params.param0 = upper_32_bits(trb_dma);
690 params.param1 = lower_32_bits(trb_dma);
691
692 cmd = DWC3_DEPCMD_STARTTRANSFER;
693
694 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
695 if (ret < 0)
696 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700697
698 if (dep->stream_capable) {
699 /*
700 * For streams, at start, there maybe a race where the
701 * host primes the endpoint before the function driver
702 * queues a request to initiate a stream. In that case,
703 * the controller will not see the prime to generate the
704 * ERDY and start stream. To workaround this, issue a
705 * no-op TRB as normal, but end it immediately. As a
706 * result, when the function driver queues the request,
707 * the next START_TRANSFER command will cause the
708 * controller to generate an ERDY to initiate the
709 * stream.
710 */
711 dwc3_stop_active_transfer(dep, true, true);
712
713 /*
714 * All stream eps will reinitiate stream on NoStream
715 * rejection until we can determine that the host can
716 * prime after the first transfer.
717 */
718 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
719 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300720 }
721
Felipe Balbi2870e502016-11-03 13:53:29 +0200722out:
723 trace_dwc3_gadget_ep_enable(dep);
724
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 return 0;
726}
727
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200728static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300729{
730 struct dwc3_request *req;
731
Felipe Balbic5353b22019-02-13 13:00:54 +0200732 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300733
Felipe Balbi0e146022016-06-21 10:32:02 +0300734 /* - giveback all requests to gadget driver */
735 while (!list_empty(&dep->started_list)) {
736 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200737
Felipe Balbi0e146022016-06-21 10:32:02 +0300738 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200739 }
740
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200741 while (!list_empty(&dep->pending_list)) {
742 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300745 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200746
747 while (!list_empty(&dep->cancelled_list)) {
748 req = next_request(&dep->cancelled_list);
749
750 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
751 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300752}
753
754/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300755 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 * @dep: the endpoint to disable
757 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300758 * This function undoes what __dwc3_gadget_ep_enable did and also removes
759 * requests which are currently being processed by the hardware and those which
760 * are not yet scheduled.
761 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200762 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300764static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
765{
766 struct dwc3 *dwc = dep->dwc;
767 u32 reg;
768
Felipe Balbi2870e502016-11-03 13:53:29 +0200769 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500770
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200771 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300772
Felipe Balbi687ef982014-04-16 10:30:33 -0500773 /* make sure HW endpoint isn't stalled */
774 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500775 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500776
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
778 reg &= ~DWC3_DALEPENA_EP(dep->number);
779 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
780
Felipe Balbi879631a2011-09-30 10:58:47 +0300781 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200783 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300784
John Youn39ebb052016-11-09 16:36:28 -0800785 /* Clear out the ep descriptors for non-ep0 */
786 if (dep->number > 1) {
787 dep->endpoint.comp_desc = NULL;
788 dep->endpoint.desc = NULL;
789 }
790
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 return 0;
792}
793
794/* -------------------------------------------------------------------------- */
795
796static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
797 const struct usb_endpoint_descriptor *desc)
798{
799 return -EINVAL;
800}
801
802static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
803{
804 return -EINVAL;
805}
806
807/* -------------------------------------------------------------------------- */
808
809static int dwc3_gadget_ep_enable(struct usb_ep *ep,
810 const struct usb_endpoint_descriptor *desc)
811{
812 struct dwc3_ep *dep;
813 struct dwc3 *dwc;
814 unsigned long flags;
815 int ret;
816
817 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
818 pr_debug("dwc3: invalid parameters\n");
819 return -EINVAL;
820 }
821
822 if (!desc->wMaxPacketSize) {
823 pr_debug("dwc3: missing wMaxPacketSize\n");
824 return -EINVAL;
825 }
826
827 dep = to_dwc3_ep(ep);
828 dwc = dep->dwc;
829
Felipe Balbi95ca9612015-12-10 13:08:20 -0600830 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
831 "%s is already enabled\n",
832 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300833 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300834
Felipe Balbi72246da2011-08-19 18:10:58 +0300835 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300836 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 spin_unlock_irqrestore(&dwc->lock, flags);
838
839 return ret;
840}
841
842static int dwc3_gadget_ep_disable(struct usb_ep *ep)
843{
844 struct dwc3_ep *dep;
845 struct dwc3 *dwc;
846 unsigned long flags;
847 int ret;
848
849 if (!ep) {
850 pr_debug("dwc3: invalid parameters\n");
851 return -EINVAL;
852 }
853
854 dep = to_dwc3_ep(ep);
855 dwc = dep->dwc;
856
Felipe Balbi95ca9612015-12-10 13:08:20 -0600857 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
858 "%s is already disabled\n",
859 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300861
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 spin_lock_irqsave(&dwc->lock, flags);
863 ret = __dwc3_gadget_ep_disable(dep);
864 spin_unlock_irqrestore(&dwc->lock, flags);
865
866 return ret;
867}
868
869static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300870 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300871{
872 struct dwc3_request *req;
873 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300874
875 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900876 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300878
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300879 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 req->epnum = dep->number;
881 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200882 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300883
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500884 trace_dwc3_alloc_request(req);
885
Felipe Balbi72246da2011-08-19 18:10:58 +0300886 return &req->request;
887}
888
889static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
890 struct usb_request *request)
891{
892 struct dwc3_request *req = to_dwc3_request(request);
893
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500894 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300895 kfree(req);
896}
897
Felipe Balbi42626912018-04-09 13:01:43 +0300898/**
899 * dwc3_ep_prev_trb - returns the previous TRB in the ring
900 * @dep: The endpoint with the TRB ring
901 * @index: The index of the current TRB in the ring
902 *
903 * Returns the TRB prior to the one pointed to by the index. If the
904 * index is 0, we will wrap backwards, skip the link TRB, and return
905 * the one just before that.
906 */
907static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
908{
909 u8 tmp = index;
910
911 if (!tmp)
912 tmp = DWC3_TRB_NUM - 1;
913
914 return &dep->trb_pool[tmp - 1];
915}
916
917static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
918{
919 struct dwc3_trb *tmp;
920 u8 trbs_left;
921
922 /*
923 * If enqueue & dequeue are equal than it is either full or empty.
924 *
925 * One way to know for sure is if the TRB right before us has HWO bit
926 * set or not. If it has, then we're definitely full and can't fit any
927 * more transfers in our ring.
928 */
929 if (dep->trb_enqueue == dep->trb_dequeue) {
930 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
931 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
932 return 0;
933
934 return DWC3_TRB_NUM - 1;
935 }
936
937 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
938 trbs_left &= (DWC3_TRB_NUM - 1);
939
940 if (dep->trb_dequeue < dep->trb_enqueue)
941 trbs_left--;
942
943 return trbs_left;
944}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300945
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200946static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300947 dma_addr_t dma, unsigned int length, unsigned int chain,
948 unsigned int node, unsigned int stream_id,
949 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700950 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200951{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300952 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800953 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300954 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200955
Felipe Balbif6bafc62012-02-06 11:04:53 +0200956 trb->size = DWC3_TRB_SIZE_LENGTH(length);
957 trb->bpl = lower_32_bits(dma);
958 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200959
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200960 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200961 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200962 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200963 break;
964
965 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300966 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300968
Manu Gautam40d829f2017-07-19 17:07:10 +0530969 /*
970 * USB Specification 2.0 Section 5.9.2 states that: "If
971 * there is only a single transaction in the microframe,
972 * only a DATA0 data packet PID is used. If there are
973 * two transactions per microframe, DATA1 is used for
974 * the first transaction data packet and DATA0 is used
975 * for the second transaction data packet. If there are
976 * three transactions per microframe, DATA2 is used for
977 * the first transaction data packet, DATA1 is used for
978 * the second, and DATA0 is used for the third."
979 *
980 * IOW, we should satisfy the following cases:
981 *
982 * 1) length <= maxpacket
983 * - DATA0
984 *
985 * 2) maxpacket < length <= (2 * maxpacket)
986 * - DATA1, DATA0
987 *
988 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
989 * - DATA2, DATA1, DATA0
990 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300991 if (speed == USB_SPEED_HIGH) {
992 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530993 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530994 unsigned int maxp = usb_endpoint_maxp(ep->desc);
995
996 if (length <= (2 * maxp))
997 mult--;
998
999 if (length <= maxp)
1000 mult--;
1001
1002 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001003 }
1004 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301005 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001006 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001007
1008 /* always enable Interrupt on Missed ISOC */
1009 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001010 break;
1011
1012 case USB_ENDPOINT_XFER_BULK:
1013 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001014 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001015 break;
1016 default:
1017 /*
1018 * This is only possible with faulty memory because we
1019 * checked it already :)
1020 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001021 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1022 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001023 }
1024
Tejas Joglekar244add82018-12-10 16:08:13 +05301025 /*
1026 * Enable Continue on Short Packet
1027 * when endpoint is not a stream capable
1028 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001029 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301030 if (!dep->stream_capable)
1031 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001032
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001033 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001034 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1035 }
1036
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001037 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001038 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001039
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301040 if (chain)
1041 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001042 else if (dep->stream_capable && is_last)
1043 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301044
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001045 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001046 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001047
1048 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001049
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301050 dwc3_ep_inc_enq(dep);
1051
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001052 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001053}
1054
John Youn361572b2016-05-19 17:26:17 -07001055/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001056 * dwc3_prepare_one_trb - setup one TRB from one request
1057 * @dep: endpoint for which this request is prepared
1058 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001059 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001060 * @chain: should this TRB be chained to the next?
1061 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001062 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001063 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001064 */
1065static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001066 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001067 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1068 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001069{
1070 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301071 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001072 unsigned int stream_id = req->request.stream_id;
1073 unsigned int short_not_ok = req->request.short_not_ok;
1074 unsigned int no_interrupt = req->request.no_interrupt;
1075 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301076
Thinh Nguyen2b803572020-09-24 01:21:30 -07001077 if (use_bounce_buffer)
1078 dma = dep->dwc->bounce_addr;
1079 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301080 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001081 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301082 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001083
1084 trb = &dep->trb_pool[dep->trb_enqueue];
1085
1086 if (!req->trb) {
1087 dwc3_gadget_move_started_request(req);
1088 req->trb = trb;
1089 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001090 }
1091
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001092 req->num_trbs++;
1093
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001094 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001095 stream_id, short_not_ok, no_interrupt, is_last,
1096 must_interrupt);
1097}
1098
1099static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1100{
1101 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1102 unsigned int rem = req->request.length % maxp;
1103
1104 if ((req->request.length && req->request.zero && !rem &&
1105 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1106 (!req->direction && rem))
1107 return true;
1108
1109 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001110}
1111
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001112/**
1113 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1114 * @dep: The endpoint that the request belongs to
1115 * @req: The request to prepare
1116 * @entry_length: The last SG entry size
1117 * @node: Indicates whether this is not the first entry (for isoc only)
1118 *
1119 * Return the number of TRBs prepared.
1120 */
1121static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1122 struct dwc3_request *req, unsigned int entry_length,
1123 unsigned int node)
1124{
1125 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1126 unsigned int rem = req->request.length % maxp;
1127 unsigned int num_trbs = 1;
1128
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001129 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001130 num_trbs++;
1131
1132 if (dwc3_calc_trbs_left(dep) < num_trbs)
1133 return 0;
1134
1135 req->needs_extra_trb = num_trbs > 1;
1136
1137 /* Prepare a normal TRB */
1138 if (req->direction || req->request.length)
1139 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001140 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001141
1142 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1143 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1144 dwc3_prepare_one_trb(dep, req,
1145 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001146 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001147
1148 return num_trbs;
1149}
1150
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001151static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001152 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001153{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301154 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001155 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001156 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001157 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301158 unsigned int remaining = req->request.num_mapped_sgs
1159 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001160 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001161 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301162
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001163 /*
1164 * If we resume preparing the request, then get the remaining length of
1165 * the request and resume where we left off.
1166 */
1167 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1168 length -= sg_dma_len(s);
1169
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301170 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001171 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001172 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001173 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001174 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001175
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001176 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1177
1178 length -= trb_length;
1179
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001180 /*
1181 * IOMMU driver is coalescing the list of sgs which shares a
1182 * page boundary into one and giving it to USB driver. With
1183 * this the number of sgs mapped is not equal to the number of
1184 * sgs passed. So mark the chain bit to false if it isthe last
1185 * mapped sg.
1186 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001187 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001188 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001189
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001190 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001191 break;
1192
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001193 if (last_sg) {
1194 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001195 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001196 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001197 /*
1198 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001199 * next SG entry. If not, set interrupt on this TRB to
1200 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001201 * free.
1202 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001203 if (num_trbs_left == 1 || (needs_extra_trb &&
1204 num_trbs_left <= 2 &&
1205 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001206 must_interrupt = true;
1207
1208 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1209 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001210 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001211
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301212 /*
1213 * There can be a situation where all sgs in sglist are not
1214 * queued because of insufficient trb number. To handle this
1215 * case, update start_sg to next sg to be queued, so that
1216 * we have free trbs we can continue queuing from where we
1217 * previously stopped
1218 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001219 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301220 req->start_sg = sg_next(s);
1221
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301222 req->num_queued_sgs++;
1223
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001224 /*
1225 * The number of pending SG entries may not correspond to the
1226 * number of mapped SG entries. If all the data are queued, then
1227 * don't include unused SG entries.
1228 */
1229 if (length == 0) {
1230 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1231 break;
1232 }
1233
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001234 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001235 break;
1236 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001237
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001238 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001239}
1240
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001241static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001242 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001243{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001244 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001245}
1246
Felipe Balbi72246da2011-08-19 18:10:58 +03001247/*
1248 * dwc3_prepare_trbs - setup TRBs from requests
1249 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001251 * The function goes through the requests list and sets up TRBs for the
1252 * transfers. The function returns once there are no more TRBs available or
1253 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001254 *
1255 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001257static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001258{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001259 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001260 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001261
1262 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1263
Felipe Balbid86c5a62016-10-25 13:48:52 +03001264 /*
1265 * We can get in a situation where there's a request in the started list
1266 * but there weren't enough TRBs to fully kick it in the first time
1267 * around, so it has been waiting for more TRBs to be freed up.
1268 *
1269 * In that case, we should check if we have a request with pending_sgs
1270 * in the started list and prepare TRBs for that request first,
1271 * otherwise we will prepare TRBs completely out of order and that will
1272 * break things.
1273 */
1274 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001275 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001276 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001277 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001278 return ret;
1279 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001280
1281 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001282 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001283
1284 /*
1285 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1286 * burst capability may try to read and use TRBs beyond the
1287 * active transfer instead of stopping.
1288 */
1289 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001290 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001291 }
1292
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001293 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001294 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001295
1296 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1297 dep->direction);
1298 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001299 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001300
1301 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301302 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301303 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001304 req->num_pending_sgs = req->request.num_mapped_sgs;
1305
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001306 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001307 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001308 if (req->num_pending_sgs)
1309 return ret;
1310 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001311 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001312 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001313
Thinh Nguyen490410b2020-09-24 01:21:55 -07001314 if (!ret || !dwc3_calc_trbs_left(dep))
1315 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001316
1317 /*
1318 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1319 * burst capability may try to read and use TRBs beyond the
1320 * active transfer instead of stopping.
1321 */
1322 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001323 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001324 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001325
1326 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001327}
1328
Thinh Nguyen8d990872020-03-29 16:12:57 -07001329static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1330
Felipe Balbi7fdca762017-09-05 14:41:34 +03001331static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001332{
1333 struct dwc3_gadget_ep_cmd_params params;
1334 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001335 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 int ret;
1337 u32 cmd;
1338
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001339 /*
1340 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1341 * This happens when we need to stop and restart a transfer such as in
1342 * the case of reinitiating a stream or retrying an isoc transfer.
1343 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001344 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001345 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001346 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001347
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001348 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001349
Thinh Nguyen23384842020-09-30 17:44:38 -07001350 /*
1351 * If there's no new TRB prepared and we don't need to restart a
1352 * transfer, there's no need to update the transfer.
1353 */
1354 if (!ret && !starting)
1355 return ret;
1356
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001357 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001358 if (!req) {
1359 dep->flags |= DWC3_EP_PENDING_REQUEST;
1360 return 0;
1361 }
1362
1363 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001364
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001365 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301366 params.param0 = upper_32_bits(req->trb_dma);
1367 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001368 cmd = DWC3_DEPCMD_STARTTRANSFER;
1369
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301370 if (dep->stream_capable)
1371 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1372
Felipe Balbi7fdca762017-09-05 14:41:34 +03001373 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1374 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301375 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001376 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1377 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301378 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001379
Felipe Balbi2cd47182016-04-12 16:42:43 +03001380 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001381 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001382 struct dwc3_request *tmp;
1383
1384 if (ret == -EAGAIN)
1385 return ret;
1386
1387 dwc3_stop_active_transfer(dep, true, true);
1388
1389 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1390 dwc3_gadget_move_cancelled_request(req);
1391
1392 /* If ep isn't started, then there's no end transfer pending */
1393 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1394 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1395
Felipe Balbi72246da2011-08-19 18:10:58 +03001396 return ret;
1397 }
1398
Thinh Nguyene0d19562020-05-05 19:46:57 -07001399 if (dep->stream_capable && req->request.is_last)
1400 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1401
Felipe Balbi72246da2011-08-19 18:10:58 +03001402 return 0;
1403}
1404
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001405static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1406{
1407 u32 reg;
1408
1409 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1410 return DWC3_DSTS_SOFFN(reg);
1411}
1412
Thinh Nguyend92021f2018-11-14 22:56:54 -08001413/**
1414 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1415 * @dep: isoc endpoint
1416 *
1417 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1418 * microframe number reported by the XferNotReady event for the future frame
1419 * number to start the isoc transfer.
1420 *
1421 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1422 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1423 * XferNotReady event are invalid. The driver uses this number to schedule the
1424 * isochronous transfer and passes it to the START TRANSFER command. Because
1425 * this number is invalid, the command may fail. If BIT[15:14] matches the
1426 * internal 16-bit microframe, the START TRANSFER command will pass and the
1427 * transfer will start at the scheduled time, if it is off by 1, the command
1428 * will still pass, but the transfer will start 2 seconds in the future. For all
1429 * other conditions, the START TRANSFER command will fail with bus-expiry.
1430 *
1431 * In order to workaround this issue, we can test for the correct combination of
1432 * BIT[15:14] by sending START TRANSFER commands with different values of
1433 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1434 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1435 * As the result, within the 4 possible combinations for BIT[15:14], there will
1436 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1437 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1438 * value is the correct combination.
1439 *
1440 * Since there are only 4 outcomes and the results are ordered, we can simply
1441 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1442 * deduce the smaller successful combination.
1443 *
1444 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1445 * of BIT[15:14]. The correct combination is as follow:
1446 *
1447 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1448 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1449 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1450 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1451 *
1452 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1453 * endpoints.
1454 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001455static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301456{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001457 int cmd_status = 0;
1458 bool test0;
1459 bool test1;
1460
1461 while (dep->combo_num < 2) {
1462 struct dwc3_gadget_ep_cmd_params params;
1463 u32 test_frame_number;
1464 u32 cmd;
1465
1466 /*
1467 * Check if we can start isoc transfer on the next interval or
1468 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1469 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001470 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001471 test_frame_number |= dep->combo_num << 14;
1472 test_frame_number += max_t(u32, 4, dep->interval);
1473
1474 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1475 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1476
1477 cmd = DWC3_DEPCMD_STARTTRANSFER;
1478 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1479 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1480
1481 /* Redo if some other failure beside bus-expiry is received */
1482 if (cmd_status && cmd_status != -EAGAIN) {
1483 dep->start_cmd_status = 0;
1484 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001485 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001486 }
1487
1488 /* Store the first test status */
1489 if (dep->combo_num == 0)
1490 dep->start_cmd_status = cmd_status;
1491
1492 dep->combo_num++;
1493
1494 /*
1495 * End the transfer if the START_TRANSFER command is successful
1496 * to wait for the next XferNotReady to test the command again
1497 */
1498 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001499 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001500 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001501 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301502 }
1503
Thinh Nguyend92021f2018-11-14 22:56:54 -08001504 /* test0 and test1 are both completed at this point */
1505 test0 = (dep->start_cmd_status == 0);
1506 test1 = (cmd_status == 0);
1507
1508 if (!test0 && test1)
1509 dep->combo_num = 1;
1510 else if (!test0 && !test1)
1511 dep->combo_num = 2;
1512 else if (test0 && !test1)
1513 dep->combo_num = 3;
1514 else if (test0 && test1)
1515 dep->combo_num = 0;
1516
Michael Grzeschikca143782020-07-01 20:24:51 +02001517 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001518 dep->frame_number |= dep->combo_num << 14;
1519 dep->frame_number += max_t(u32, 4, dep->interval);
1520
1521 /* Reinitialize test variables */
1522 dep->start_cmd_status = 0;
1523 dep->combo_num = 0;
1524
Felipe Balbi25abad62018-08-14 10:41:19 +03001525 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001526}
1527
Felipe Balbi25abad62018-08-14 10:41:19 +03001528static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301529{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001530 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001531 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001532 int ret;
1533 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001534
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001535 if (list_empty(&dep->pending_list) &&
1536 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301537 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001538 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301539 }
1540
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001541 if (!dwc->dis_start_transfer_quirk &&
1542 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1543 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001544 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001545 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001546 }
1547
Michael Olbrichc5a70922020-07-01 20:24:52 +02001548 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001549 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001550 u32 frame = __dwc3_gadget_get_frame(dwc);
1551 bool rollover = frame <
1552 (dep->frame_number & DWC3_FRNUMBER_MASK);
1553
1554 /*
1555 * frame_number is set from XferNotReady and may be already
1556 * out of date. DSTS only provides the lower 14 bit of the
1557 * current frame number. So add the upper two bits of
1558 * frame_number and handle a possible rollover.
1559 * This will provide the correct frame_number unless more than
1560 * rollover has happened since XferNotReady.
1561 */
1562
1563 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1564 frame;
1565 if (rollover)
1566 dep->frame_number += BIT(14);
1567 }
1568
Felipe Balbid5370102018-08-14 10:42:43 +03001569 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1570 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1571
1572 ret = __dwc3_gadget_kick_transfer(dep);
1573 if (ret != -EAGAIN)
1574 break;
1575 }
1576
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001577 /*
1578 * After a number of unsuccessful start attempts due to bus-expiry
1579 * status, issue END_TRANSFER command and retry on the next XferNotReady
1580 * event.
1581 */
1582 if (ret == -EAGAIN) {
1583 struct dwc3_gadget_ep_cmd_params params;
1584 u32 cmd;
1585
1586 cmd = DWC3_DEPCMD_ENDTRANSFER |
1587 DWC3_DEPCMD_CMDIOC |
1588 DWC3_DEPCMD_PARAM(dep->resource_index);
1589
1590 dep->resource_index = 0;
1591 memset(&params, 0, sizeof(params));
1592
1593 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1594 if (!ret)
1595 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1596 }
1597
Felipe Balbid5370102018-08-14 10:42:43 +03001598 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301599}
1600
Felipe Balbi72246da2011-08-19 18:10:58 +03001601static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1602{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001603 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001604
Wesley Chengae7e8612020-09-28 17:20:59 -07001605 if (!dep->endpoint.desc || !dwc->pullups_connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001606 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1607 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001608 return -ESHUTDOWN;
1609 }
1610
Felipe Balbi04fb3652017-05-17 15:57:45 +03001611 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1612 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001613 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001614
Felipe Balbib2b6d602019-01-11 12:58:52 +02001615 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1616 "%s: request %pK already in flight\n",
1617 dep->name, &req->request))
1618 return -EINVAL;
1619
Felipe Balbifc8bb912016-05-16 13:14:48 +03001620 pm_runtime_get(dwc->dev);
1621
Felipe Balbi72246da2011-08-19 18:10:58 +03001622 req->request.actual = 0;
1623 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001624
Felipe Balbife84f522015-09-01 09:01:38 -05001625 trace_dwc3_ep_queue(req);
1626
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001627 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001628 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001629
Thinh Nguyene0d19562020-05-05 19:46:57 -07001630 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1631 return 0;
1632
Thinh Nguyenc5036722020-09-02 18:42:58 -07001633 /*
1634 * Start the transfer only after the END_TRANSFER is completed
1635 * and endpoint STALL is cleared.
1636 */
1637 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1638 (dep->flags & DWC3_EP_WEDGE) ||
1639 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001640 dep->flags |= DWC3_EP_DELAY_START;
1641 return 0;
1642 }
1643
Felipe Balbid889c232016-09-29 15:44:29 +03001644 /*
1645 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1646 * wait for a XferNotReady event so we will know what's the current
1647 * (micro-)frame number.
1648 *
1649 * Without this trick, we are very, very likely gonna get Bus Expiry
1650 * errors which will force us issue EndTransfer command.
1651 */
1652 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001653 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1654 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001655 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001656
1657 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001658 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001659 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001660 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001661 }
1662
Felipe Balbi7fdca762017-09-05 14:41:34 +03001663 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001664}
1665
1666static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1667 gfp_t gfp_flags)
1668{
1669 struct dwc3_request *req = to_dwc3_request(request);
1670 struct dwc3_ep *dep = to_dwc3_ep(ep);
1671 struct dwc3 *dwc = dep->dwc;
1672
1673 unsigned long flags;
1674
1675 int ret;
1676
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001677 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001678 ret = __dwc3_gadget_ep_queue(dep, req);
1679 spin_unlock_irqrestore(&dwc->lock, flags);
1680
1681 return ret;
1682}
1683
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001684static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1685{
1686 int i;
1687
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001688 /* If req->trb is not set, then the request has not started */
1689 if (!req->trb)
1690 return;
1691
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001692 /*
1693 * If request was already started, this means we had to
1694 * stop the transfer. With that we also need to ignore
1695 * all TRBs used by the request, however TRBs can only
1696 * be modified after completion of END_TRANSFER
1697 * command. So what we do here is that we wait for
1698 * END_TRANSFER completion and only after that, we jump
1699 * over TRBs by clearing HWO and incrementing dequeue
1700 * pointer.
1701 */
1702 for (i = 0; i < req->num_trbs; i++) {
1703 struct dwc3_trb *trb;
1704
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001705 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001706 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1707 dwc3_ep_inc_deq(dep);
1708 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001709
1710 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001711}
1712
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001713static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1714{
1715 struct dwc3_request *req;
1716 struct dwc3_request *tmp;
1717
1718 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1719 dwc3_gadget_ep_skip_trbs(dep, req);
1720 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1721 }
1722}
1723
Felipe Balbi72246da2011-08-19 18:10:58 +03001724static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1725 struct usb_request *request)
1726{
1727 struct dwc3_request *req = to_dwc3_request(request);
1728 struct dwc3_request *r = NULL;
1729
1730 struct dwc3_ep *dep = to_dwc3_ep(ep);
1731 struct dwc3 *dwc = dep->dwc;
1732
1733 unsigned long flags;
1734 int ret = 0;
1735
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001736 trace_dwc3_ep_dequeue(req);
1737
Felipe Balbi72246da2011-08-19 18:10:58 +03001738 spin_lock_irqsave(&dwc->lock, flags);
1739
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001740 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001741 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001742 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743 }
1744
Felipe Balbi72246da2011-08-19 18:10:58 +03001745 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001747 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1748 goto out;
1749 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001750 }
1751
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001752 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001754 struct dwc3_request *t;
1755
Felipe Balbi72246da2011-08-19 18:10:58 +03001756 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001757 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001758
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001759 /*
1760 * Remove any started request if the transfer is
1761 * cancelled.
1762 */
1763 list_for_each_entry_safe(r, t, &dep->started_list, list)
1764 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001765
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001766 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001767 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 }
1769
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001770 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1771 request, ep->name);
1772 ret = -EINVAL;
1773out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001774 spin_unlock_irqrestore(&dwc->lock, flags);
1775
1776 return ret;
1777}
1778
Felipe Balbi7a608552014-09-24 14:19:52 -05001779int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001780{
1781 struct dwc3_gadget_ep_cmd_params params;
1782 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001783 struct dwc3_request *req;
1784 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 int ret;
1786
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001787 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1788 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1789 return -EINVAL;
1790 }
1791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792 memset(&params, 0x00, sizeof(params));
1793
1794 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001795 struct dwc3_trb *trb;
1796
Felipe Balbie319bd62020-08-13 08:35:38 +03001797 unsigned int transfer_in_flight;
1798 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001799
1800 if (dep->number > 1)
1801 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1802 else
1803 trb = &dwc->ep0_trb[dep->trb_enqueue];
1804
1805 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1806 started = !list_empty(&dep->started_list);
1807
1808 if (!protocol && ((dep->direction && transfer_in_flight) ||
1809 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001810 return -EAGAIN;
1811 }
1812
Felipe Balbi2cd47182016-04-12 16:42:43 +03001813 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1814 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001815 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001816 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001817 dep->name);
1818 else
1819 dep->flags |= DWC3_EP_STALL;
1820 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001821 /*
1822 * Don't issue CLEAR_STALL command to control endpoints. The
1823 * controller automatically clears the STALL when it receives
1824 * the SETUP token.
1825 */
1826 if (dep->number <= 1) {
1827 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1828 return 0;
1829 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001830
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001831 dwc3_stop_active_transfer(dep, true, true);
1832
1833 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1834 dwc3_gadget_move_cancelled_request(req);
1835
1836 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1837 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1838 return 0;
1839 }
1840
1841 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1842
John Youn50c763f2016-05-31 17:49:56 -07001843 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001844 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001845 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001847 return ret;
1848 }
1849
1850 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1851
Thinh Nguyenc5036722020-09-02 18:42:58 -07001852 if ((dep->flags & DWC3_EP_DELAY_START) &&
1853 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1854 __dwc3_gadget_kick_transfer(dep);
1855
1856 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001858
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 return ret;
1860}
1861
1862static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1863{
1864 struct dwc3_ep *dep = to_dwc3_ep(ep);
1865 struct dwc3 *dwc = dep->dwc;
1866
1867 unsigned long flags;
1868
1869 int ret;
1870
1871 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001872 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001873 spin_unlock_irqrestore(&dwc->lock, flags);
1874
1875 return ret;
1876}
1877
1878static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1879{
1880 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001881 struct dwc3 *dwc = dep->dwc;
1882 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001883 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001884
Paul Zimmerman249a4562012-02-24 17:32:16 -08001885 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001886 dep->flags |= DWC3_EP_WEDGE;
1887
Pratyush Anand08f0d962012-06-25 22:40:43 +05301888 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001889 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301890 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001891 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001892 spin_unlock_irqrestore(&dwc->lock, flags);
1893
1894 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001895}
1896
1897/* -------------------------------------------------------------------------- */
1898
1899static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1900 .bLength = USB_DT_ENDPOINT_SIZE,
1901 .bDescriptorType = USB_DT_ENDPOINT,
1902 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1903};
1904
1905static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1906 .enable = dwc3_gadget_ep0_enable,
1907 .disable = dwc3_gadget_ep0_disable,
1908 .alloc_request = dwc3_gadget_ep_alloc_request,
1909 .free_request = dwc3_gadget_ep_free_request,
1910 .queue = dwc3_gadget_ep0_queue,
1911 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301912 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001913 .set_wedge = dwc3_gadget_ep_set_wedge,
1914};
1915
1916static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1917 .enable = dwc3_gadget_ep_enable,
1918 .disable = dwc3_gadget_ep_disable,
1919 .alloc_request = dwc3_gadget_ep_alloc_request,
1920 .free_request = dwc3_gadget_ep_free_request,
1921 .queue = dwc3_gadget_ep_queue,
1922 .dequeue = dwc3_gadget_ep_dequeue,
1923 .set_halt = dwc3_gadget_ep_set_halt,
1924 .set_wedge = dwc3_gadget_ep_set_wedge,
1925};
1926
1927/* -------------------------------------------------------------------------- */
1928
1929static int dwc3_gadget_get_frame(struct usb_gadget *g)
1930{
1931 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001932
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001933 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001934}
1935
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001936static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001937{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001938 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001939
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001940 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001941 u32 reg;
1942
Felipe Balbi72246da2011-08-19 18:10:58 +03001943 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001944
Felipe Balbi72246da2011-08-19 18:10:58 +03001945 /*
1946 * According to the Databook Remote wakeup request should
1947 * be issued only when the device is in early suspend state.
1948 *
1949 * We can check that via USB Link State bits in DSTS register.
1950 */
1951 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1952
Felipe Balbi72246da2011-08-19 18:10:58 +03001953 link_state = DWC3_DSTS_USBLNKST(reg);
1954
1955 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001956 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001957 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1958 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001959 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001960 break;
1961 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001962 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001963 }
1964
Felipe Balbi8598bde2012-01-02 18:55:57 +02001965 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1966 if (ret < 0) {
1967 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001968 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001969 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001970
Paul Zimmerman802fde92012-04-27 13:10:52 +03001971 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001972 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001973 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001974 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001975 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1976 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1977 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001978
Paul Zimmerman1d046792012-02-15 18:56:56 -08001979 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001980 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001981
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001982 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1984
1985 /* in HS, means ON */
1986 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1987 break;
1988 }
1989
1990 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1991 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001992 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001993 }
1994
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001995 return 0;
1996}
1997
1998static int dwc3_gadget_wakeup(struct usb_gadget *g)
1999{
2000 struct dwc3 *dwc = gadget_to_dwc(g);
2001 unsigned long flags;
2002 int ret;
2003
2004 spin_lock_irqsave(&dwc->lock, flags);
2005 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 spin_unlock_irqrestore(&dwc->lock, flags);
2007
2008 return ret;
2009}
2010
2011static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2012 int is_selfpowered)
2013{
2014 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002015 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002016
Paul Zimmerman249a4562012-02-24 17:32:16 -08002017 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002018 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002019 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002020
2021 return 0;
2022}
2023
Wesley Chengae7e8612020-09-28 17:20:59 -07002024static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2025{
2026 u32 epnum;
2027
2028 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2029 struct dwc3_ep *dep;
2030
2031 dep = dwc->eps[epnum];
2032 if (!dep)
2033 continue;
2034
2035 dwc3_remove_requests(dwc, dep);
2036 }
2037}
2038
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002039static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002040{
2041 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002042 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002043
Felipe Balbifc8bb912016-05-16 13:14:48 +03002044 if (pm_runtime_suspended(dwc->dev))
2045 return 0;
2046
Felipe Balbi72246da2011-08-19 18:10:58 +03002047 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002048 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002049 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002050 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2051 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2052 }
2053
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002054 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002055 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2056 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002057
2058 if (dwc->has_hibernation)
2059 reg |= DWC3_DCTL_KEEP_CONNECT;
2060
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002061 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002062 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002063 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002064
2065 if (dwc->has_hibernation && !suspend)
2066 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2067
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002068 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002069 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002070
Thinh Nguyen5b738212019-10-23 19:15:43 -07002071 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002072
2073 do {
2074 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002075 reg &= DWC3_DSTS_DEVCTRLHLT;
2076 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002077
2078 if (!timeout)
2079 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002080
Pratyush Anand6f17f742012-07-02 10:21:55 +05302081 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002082}
2083
Wesley Chengae7e8612020-09-28 17:20:59 -07002084static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2085static void __dwc3_gadget_stop(struct dwc3 *dwc);
2086
Felipe Balbi72246da2011-08-19 18:10:58 +03002087static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2088{
2089 struct dwc3 *dwc = gadget_to_dwc(g);
2090 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302091 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002092
2093 is_on = !!is_on;
2094
Baolin Wangbb014732016-10-14 17:11:33 +08002095 /*
2096 * Per databook, when we want to stop the gadget, if a control transfer
2097 * is still in process, complete it and get the core into setup phase.
2098 */
2099 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2100 reinit_completion(&dwc->ep0_in_setup);
2101
2102 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2103 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2104 if (ret == 0) {
2105 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2106 return -ETIMEDOUT;
2107 }
2108 }
2109
Wesley Chengae7e8612020-09-28 17:20:59 -07002110 /*
2111 * Synchronize any pending event handling before executing the controller
2112 * halt routine.
2113 */
2114 if (!is_on) {
2115 dwc3_gadget_disable_irq(dwc);
2116 synchronize_irq(dwc->irq_gadget);
2117 }
2118
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002120
2121 if (!is_on) {
2122 u32 count;
2123
2124 /*
2125 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2126 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2127 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2128 * command for any active transfers" before clearing the RunStop
2129 * bit.
2130 */
2131 dwc3_stop_active_transfers(dwc);
2132 __dwc3_gadget_stop(dwc);
2133
2134 /*
2135 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2136 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2137 * "software needs to acknowledge the events that are generated
2138 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2139 * to be set to '1'."
2140 */
2141 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2142 count &= DWC3_GEVNTCOUNT_MASK;
2143 if (count > 0) {
2144 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2145 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2146 dwc->ev_buf->length;
2147 }
2148 }
2149
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002150 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 spin_unlock_irqrestore(&dwc->lock, flags);
2152
Pratyush Anand6f17f742012-07-02 10:21:55 +05302153 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002154}
2155
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002156static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2157{
2158 u32 reg;
2159
2160 /* Enable all but Start and End of Frame IRQs */
2161 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2162 DWC3_DEVTEN_EVNTOVERFLOWEN |
2163 DWC3_DEVTEN_CMDCMPLTEN |
2164 DWC3_DEVTEN_ERRTICERREN |
2165 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002166 DWC3_DEVTEN_CONNECTDONEEN |
2167 DWC3_DEVTEN_USBRSTEN |
2168 DWC3_DEVTEN_DISCONNEVTEN);
2169
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002170 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002171 reg |= DWC3_DEVTEN_ULSTCNGEN;
2172
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002173 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2174}
2175
2176static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2177{
2178 /* mask all interrupts */
2179 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2180}
2181
2182static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002183static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002184
Felipe Balbi4e994722016-05-13 14:09:59 +03002185/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002186 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2187 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002188 *
2189 * The following looks like complex but it's actually very simple. In order to
2190 * calculate the number of packets we can burst at once on OUT transfers, we're
2191 * gonna use RxFIFO size.
2192 *
2193 * To calculate RxFIFO size we need two numbers:
2194 * MDWIDTH = size, in bits, of the internal memory bus
2195 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2196 *
2197 * Given these two numbers, the formula is simple:
2198 *
2199 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2200 *
2201 * 24 bytes is for 3x SETUP packets
2202 * 16 bytes is a clock domain crossing tolerance
2203 *
2204 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2205 */
2206static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2207{
2208 u32 ram2_depth;
2209 u32 mdwidth;
2210 u32 nump;
2211 u32 reg;
2212
2213 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2214 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002215 if (DWC3_IP_IS(DWC32))
2216 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002217
2218 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2219 nump = min_t(u32, nump, 16);
2220
2221 /* update NumP */
2222 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2223 reg &= ~DWC3_DCFG_NUMP_MASK;
2224 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2225 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2226}
2227
Felipe Balbid7be2952016-05-04 15:49:37 +03002228static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002229{
Felipe Balbi72246da2011-08-19 18:10:58 +03002230 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002231 int ret = 0;
2232 u32 reg;
2233
John Youncf40b862016-11-14 12:32:43 -08002234 /*
2235 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2236 * the core supports IMOD, disable it.
2237 */
2238 if (dwc->imod_interval) {
2239 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2240 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2241 } else if (dwc3_has_imod(dwc)) {
2242 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2243 }
2244
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002245 /*
2246 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2247 * field instead of letting dwc3 itself calculate that automatically.
2248 *
2249 * This way, we maximize the chances that we'll be able to get several
2250 * bursts of data without going through any sort of endpoint throttling.
2251 */
2252 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002253 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002254 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002255 else
2256 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002257
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002258 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2259
Felipe Balbi4e994722016-05-13 14:09:59 +03002260 dwc3_gadget_setup_nump(dwc);
2261
Felipe Balbi72246da2011-08-19 18:10:58 +03002262 /* Start with SuperSpeed Default */
2263 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2264
2265 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002266 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002267 if (ret) {
2268 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002269 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002270 }
2271
2272 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002273 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002274 if (ret) {
2275 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002276 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002277 }
2278
2279 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002280 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002281 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002282 dwc3_ep0_out_start(dwc);
2283
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002284 dwc3_gadget_enable_irq(dwc);
2285
Felipe Balbid7be2952016-05-04 15:49:37 +03002286 return 0;
2287
2288err1:
2289 __dwc3_gadget_ep_disable(dwc->eps[0]);
2290
2291err0:
2292 return ret;
2293}
2294
2295static int dwc3_gadget_start(struct usb_gadget *g,
2296 struct usb_gadget_driver *driver)
2297{
2298 struct dwc3 *dwc = gadget_to_dwc(g);
2299 unsigned long flags;
2300 int ret = 0;
2301 int irq;
2302
Roger Quadros9522def2016-06-10 14:48:38 +03002303 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002304 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2305 IRQF_SHARED, "dwc3", dwc->ev_buf);
2306 if (ret) {
2307 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2308 irq, ret);
2309 goto err0;
2310 }
2311
2312 spin_lock_irqsave(&dwc->lock, flags);
2313 if (dwc->gadget_driver) {
2314 dev_err(dwc->dev, "%s is already bound to %s\n",
Peter Chene81a7012020-08-21 10:55:48 +08002315 dwc->gadget->name,
Felipe Balbid7be2952016-05-04 15:49:37 +03002316 dwc->gadget_driver->driver.name);
2317 ret = -EBUSY;
2318 goto err1;
2319 }
2320
2321 dwc->gadget_driver = driver;
2322
Felipe Balbifc8bb912016-05-16 13:14:48 +03002323 if (pm_runtime_active(dwc->dev))
2324 __dwc3_gadget_start(dwc);
2325
Felipe Balbi72246da2011-08-19 18:10:58 +03002326 spin_unlock_irqrestore(&dwc->lock, flags);
2327
2328 return 0;
2329
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002330err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002332 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002333
2334err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002335 return ret;
2336}
2337
Felipe Balbid7be2952016-05-04 15:49:37 +03002338static void __dwc3_gadget_stop(struct dwc3 *dwc)
2339{
2340 dwc3_gadget_disable_irq(dwc);
2341 __dwc3_gadget_ep_disable(dwc->eps[0]);
2342 __dwc3_gadget_ep_disable(dwc->eps[1]);
2343}
2344
Felipe Balbi22835b82014-10-17 12:05:12 -05002345static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002346{
2347 struct dwc3 *dwc = gadget_to_dwc(g);
2348 unsigned long flags;
2349
2350 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08002351
2352 if (pm_runtime_suspended(dwc->dev))
2353 goto out;
2354
Felipe Balbid7be2952016-05-04 15:49:37 +03002355 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08002356
Baolin Wang76a638f2016-10-31 19:38:36 +08002357out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002358 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 spin_unlock_irqrestore(&dwc->lock, flags);
2360
Felipe Balbi3f308d12016-05-16 14:17:06 +03002361 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002362
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 return 0;
2364}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002365
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302366static void dwc3_gadget_config_params(struct usb_gadget *g,
2367 struct usb_dcd_config_params *params)
2368{
2369 struct dwc3 *dwc = gadget_to_dwc(g);
2370
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002371 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2372 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2373
2374 /* Recommended BESL */
2375 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002376 /*
2377 * If the recommended BESL baseline is 0 or if the BESL deep is
2378 * less than 2, Microsoft's Windows 10 host usb stack will issue
2379 * a usb reset immediately after it receives the extended BOS
2380 * descriptor and the enumeration will fail. To maintain
2381 * compatibility with the Windows' usb stack, let's set the
2382 * recommended BESL baseline to 1 and clamp the BESL deep to be
2383 * within 2 to 15.
2384 */
2385 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002386 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002387 params->besl_deep =
2388 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002389 }
2390
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302391 /* U1 Device exit Latency */
2392 if (dwc->dis_u1_entry_quirk)
2393 params->bU1devExitLat = 0;
2394 else
2395 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2396
2397 /* U2 Device exit Latency */
2398 if (dwc->dis_u2_entry_quirk)
2399 params->bU2DevExitLat = 0;
2400 else
2401 params->bU2DevExitLat =
2402 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2403}
2404
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002405static void dwc3_gadget_set_speed(struct usb_gadget *g,
2406 enum usb_device_speed speed)
2407{
2408 struct dwc3 *dwc = gadget_to_dwc(g);
2409 unsigned long flags;
2410 u32 reg;
2411
2412 spin_lock_irqsave(&dwc->lock, flags);
2413 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2414 reg &= ~(DWC3_DCFG_SPEED_MASK);
2415
2416 /*
2417 * WORKAROUND: DWC3 revision < 2.20a have an issue
2418 * which would cause metastability state on Run/Stop
2419 * bit if we try to force the IP to USB2-only mode.
2420 *
2421 * Because of that, we cannot configure the IP to any
2422 * speed other than the SuperSpeed
2423 *
2424 * Refers to:
2425 *
2426 * STAR#9000525659: Clock Domain Crossing on DCTL in
2427 * USB 2.0 Mode
2428 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002429 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02002430 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002431 reg |= DWC3_DCFG_SUPERSPEED;
2432 } else {
2433 switch (speed) {
2434 case USB_SPEED_LOW:
2435 reg |= DWC3_DCFG_LOWSPEED;
2436 break;
2437 case USB_SPEED_FULL:
2438 reg |= DWC3_DCFG_FULLSPEED;
2439 break;
2440 case USB_SPEED_HIGH:
2441 reg |= DWC3_DCFG_HIGHSPEED;
2442 break;
2443 case USB_SPEED_SUPER:
2444 reg |= DWC3_DCFG_SUPERSPEED;
2445 break;
2446 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002447 if (DWC3_IP_IS(DWC3))
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002448 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002449 else
2450 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002451 break;
2452 default:
2453 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2454
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002455 if (DWC3_IP_IS(DWC3))
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002456 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002457 else
2458 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002459 }
2460 }
2461 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2462
2463 spin_unlock_irqrestore(&dwc->lock, flags);
2464}
2465
Felipe Balbi72246da2011-08-19 18:10:58 +03002466static const struct usb_gadget_ops dwc3_gadget_ops = {
2467 .get_frame = dwc3_gadget_get_frame,
2468 .wakeup = dwc3_gadget_wakeup,
2469 .set_selfpowered = dwc3_gadget_set_selfpowered,
2470 .pullup = dwc3_gadget_pullup,
2471 .udc_start = dwc3_gadget_start,
2472 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002473 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302474 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002475};
2476
2477/* -------------------------------------------------------------------------- */
2478
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002479static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2480{
2481 struct dwc3 *dwc = dep->dwc;
2482
2483 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2484 dep->endpoint.maxburst = 1;
2485 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2486 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002487 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002488
2489 dep->endpoint.caps.type_control = true;
2490
2491 return 0;
2492}
2493
2494static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2495{
2496 struct dwc3 *dwc = dep->dwc;
2497 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002498 int size;
2499
2500 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002501 if (DWC3_IP_IS(DWC32))
2502 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2503
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002504 /* MDWIDTH is represented in bits, we need it in bytes */
2505 mdwidth /= 8;
2506
2507 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002508 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002509 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002510 else
2511 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002512
2513 /* FIFO Depth is in MDWDITH bytes. Multiply */
2514 size *= mdwidth;
2515
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002516 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002517 * To meet performance requirement, a minimum TxFIFO size of 3x
2518 * MaxPacketSize is recommended for endpoints that support burst and a
2519 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2520 * support burst. Use those numbers and we can calculate the max packet
2521 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002522 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002523 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2524 size /= 3;
2525 else
2526 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002527
2528 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2529
2530 dep->endpoint.max_streams = 15;
2531 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2532 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002533 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002534 dep->endpoint.caps.type_iso = true;
2535 dep->endpoint.caps.type_bulk = true;
2536 dep->endpoint.caps.type_int = true;
2537
2538 return dwc3_alloc_trb_pool(dep);
2539}
2540
2541static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2542{
2543 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002544 int mdwidth;
2545 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002546
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002547 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002548 if (DWC3_IP_IS(DWC32))
2549 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002550
2551 /* MDWIDTH is represented in bits, convert to bytes */
2552 mdwidth /= 8;
2553
2554 /* All OUT endpoints share a single RxFIFO space */
2555 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002556 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002557 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002558 else
2559 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002560
2561 /* FIFO depth is in MDWDITH bytes */
2562 size *= mdwidth;
2563
2564 /*
2565 * To meet performance requirement, a minimum recommended RxFIFO size
2566 * is defined as follow:
2567 * RxFIFO size >= (3 x MaxPacketSize) +
2568 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2569 *
2570 * Then calculate the max packet limit as below.
2571 */
2572 size -= (3 * 8) + 16;
2573 if (size < 0)
2574 size = 0;
2575 else
2576 size /= 3;
2577
2578 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002579 dep->endpoint.max_streams = 15;
2580 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2581 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002582 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002583 dep->endpoint.caps.type_iso = true;
2584 dep->endpoint.caps.type_bulk = true;
2585 dep->endpoint.caps.type_int = true;
2586
2587 return dwc3_alloc_trb_pool(dep);
2588}
2589
2590static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002591{
2592 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002593 bool direction = epnum & 1;
2594 int ret;
2595 u8 num = epnum >> 1;
2596
2597 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2598 if (!dep)
2599 return -ENOMEM;
2600
2601 dep->dwc = dwc;
2602 dep->number = epnum;
2603 dep->direction = direction;
2604 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2605 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002606 dep->combo_num = 0;
2607 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002608
2609 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2610 direction ? "in" : "out");
2611
2612 dep->endpoint.name = dep->name;
2613
2614 if (!(dep->number > 1)) {
2615 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2616 dep->endpoint.comp_desc = NULL;
2617 }
2618
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002619 if (num == 0)
2620 ret = dwc3_gadget_init_control_endpoint(dep);
2621 else if (direction)
2622 ret = dwc3_gadget_init_in_endpoint(dep);
2623 else
2624 ret = dwc3_gadget_init_out_endpoint(dep);
2625
2626 if (ret)
2627 return ret;
2628
2629 dep->endpoint.caps.dir_in = direction;
2630 dep->endpoint.caps.dir_out = !direction;
2631
2632 INIT_LIST_HEAD(&dep->pending_list);
2633 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002634 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002635
2636 return 0;
2637}
2638
2639static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2640{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002641 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002642
Peter Chene81a7012020-08-21 10:55:48 +08002643 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002644
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002645 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002646 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002647
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002648 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2649 if (ret)
2650 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002651 }
2652
2653 return 0;
2654}
2655
2656static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2657{
2658 struct dwc3_ep *dep;
2659 u8 epnum;
2660
2661 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2662 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002663 if (!dep)
2664 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302665 /*
2666 * Physical endpoints 0 and 1 are special; they form the
2667 * bi-directional USB endpoint 0.
2668 *
2669 * For those two physical endpoints, we don't allocate a TRB
2670 * pool nor do we add them the endpoints list. Due to that, we
2671 * shouldn't do these two operations otherwise we would end up
2672 * with all sorts of bugs when removing dwc3.ko.
2673 */
2674 if (epnum != 0 && epnum != 1) {
2675 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002676 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302677 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002678
2679 kfree(dep);
2680 }
2681}
2682
Felipe Balbi72246da2011-08-19 18:10:58 +03002683/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002684
Felipe Balbi8f608e82018-03-27 10:53:29 +03002685static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2686 struct dwc3_request *req, struct dwc3_trb *trb,
2687 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302688{
2689 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302690
Felipe Balbidc55c672016-08-12 13:20:32 +03002691 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002692
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002693 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002694 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002695
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002696 /*
2697 * If we're in the middle of series of chained TRBs and we
2698 * receive a short transfer along the way, DWC3 will skip
2699 * through all TRBs including the last TRB in the chain (the
2700 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2701 * bit and SW has to do it manually.
2702 *
2703 * We're going to do that here to avoid problems of HW trying
2704 * to use bogus TRBs for transfers.
2705 */
2706 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2707 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2708
Felipe Balbic6267a52017-01-05 14:58:46 +02002709 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002710 * For isochronous transfers, the first TRB in a service interval must
2711 * have the Isoc-First type. Track and report its interval frame number.
2712 */
2713 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2714 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2715 unsigned int frame_number;
2716
2717 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2718 frame_number &= ~(dep->interval - 1);
2719 req->request.frame_number = frame_number;
2720 }
2721
2722 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002723 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2724 * this TRB points to the bounce buffer address, it's a MPS alignment
2725 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002726 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002727 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2728 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002729 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2730 return 1;
2731 }
2732
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302733 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002734 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302735
Felipe Balbi35b27192017-03-08 13:56:37 +02002736 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2737 return 1;
2738
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002739 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302740 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002741
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002742 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2743 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302744 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002745
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302746 return 0;
2747}
2748
Felipe Balbid3692952018-03-29 13:32:10 +03002749static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2750 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2751 int status)
2752{
2753 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2754 struct scatterlist *sg = req->sg;
2755 struct scatterlist *s;
2756 unsigned int pending = req->num_pending_sgs;
2757 unsigned int i;
2758 int ret = 0;
2759
2760 for_each_sg(sg, s, pending, i) {
2761 trb = &dep->trb_pool[dep->trb_dequeue];
2762
Felipe Balbid3692952018-03-29 13:32:10 +03002763 req->sg = sg_next(s);
2764 req->num_pending_sgs--;
2765
2766 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2767 trb, event, status, true);
2768 if (ret)
2769 break;
2770 }
2771
2772 return ret;
2773}
2774
2775static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2776 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2777 int status)
2778{
2779 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2780
2781 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2782 event, status, false);
2783}
2784
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002785static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2786{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002787 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002788}
2789
Felipe Balbif38e35d2018-04-06 15:56:35 +03002790static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2791 const struct dwc3_event_depevt *event,
2792 struct dwc3_request *req, int status)
2793{
2794 int ret;
2795
2796 if (req->num_pending_sgs)
2797 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2798 status);
2799 else
2800 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2801 status);
2802
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002803 req->request.actual = req->request.length - req->remaining;
2804
2805 if (!dwc3_gadget_ep_request_completed(req))
2806 goto out;
2807
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002808 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002809 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2810 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002811 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002812 }
2813
Felipe Balbif38e35d2018-04-06 15:56:35 +03002814 dwc3_gadget_giveback(dep, req, status);
2815
2816out:
2817 return ret;
2818}
2819
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002820static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002821 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002822{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002823 struct dwc3_request *req;
2824 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002825
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002826 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002827 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002828
Felipe Balbif38e35d2018-04-06 15:56:35 +03002829 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2830 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002831 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002832 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002833 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002834}
2835
Thinh Nguyend9feef92020-03-31 01:40:42 -07002836static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2837{
2838 struct dwc3_request *req;
2839
2840 if (!list_empty(&dep->pending_list))
2841 return true;
2842
2843 /*
2844 * We only need to check the first entry of the started list. We can
2845 * assume the completed requests are removed from the started list.
2846 */
2847 req = next_request(&dep->started_list);
2848 if (!req)
2849 return false;
2850
2851 return !dwc3_gadget_ep_request_completed(req);
2852}
2853
Felipe Balbiee3638b2018-03-27 11:26:53 +03002854static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2855 const struct dwc3_event_depevt *event)
2856{
Felipe Balbif62afb42018-04-11 10:34:34 +03002857 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002858}
2859
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002860static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2861 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002862{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002863 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002864 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002865
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002866 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002867
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002868 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2869 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002870
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002871 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2872 list_empty(&dep->started_list) &&
2873 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002874 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002875 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002876 if (__dwc3_gadget_kick_transfer(dep) == 0)
2877 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002878
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002879out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002880 /*
2881 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2882 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2883 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002884 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002885 u32 reg;
2886 int i;
2887
2888 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002889 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002890
2891 if (!(dep->flags & DWC3_EP_ENABLED))
2892 continue;
2893
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002894 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002895 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002896 }
2897
2898 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2899 reg |= dwc->u1u2;
2900 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2901
2902 dwc->u1u2 = 0;
2903 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002904
2905 return no_started_trb;
2906}
2907
2908static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2909 const struct dwc3_event_depevt *event)
2910{
2911 int status = 0;
2912
2913 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2914 dwc3_gadget_endpoint_frame_from_event(dep, event);
2915
2916 if (event->status & DEPEVT_STATUS_BUSERR)
2917 status = -ECONNRESET;
2918
2919 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2920 status = -EXDEV;
2921
2922 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002923}
2924
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07002925static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
2926 const struct dwc3_event_depevt *event)
2927{
2928 int status = 0;
2929
2930 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2931
2932 if (event->status & DEPEVT_STATUS_BUSERR)
2933 status = -ECONNRESET;
2934
Thinh Nguyene0d19562020-05-05 19:46:57 -07002935 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
2936 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002937}
2938
Felipe Balbi8f608e82018-03-27 10:53:29 +03002939static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2940 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002941{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002942 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002943
2944 /*
2945 * The XferNotReady event is generated only once before the endpoint
2946 * starts. It will be generated again when END_TRANSFER command is
2947 * issued. For some controller versions, the XferNotReady event may be
2948 * generated while the END_TRANSFER command is still in process. Ignore
2949 * it and wait for the next XferNotReady event after the command is
2950 * completed.
2951 */
2952 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2953 return;
2954
Felipe Balbi25abad62018-08-14 10:41:19 +03002955 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002956}
2957
Thinh Nguyen8266b082020-07-30 16:29:03 -07002958static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
2959 const struct dwc3_event_depevt *event)
2960{
2961 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2962
2963 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
2964 return;
2965
2966 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2967 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2968 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2969
2970 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
2971 struct dwc3 *dwc = dep->dwc;
2972
2973 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
2974 if (dwc3_send_clear_stall_ep_cmd(dep)) {
2975 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
2976
2977 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
2978 if (dwc->delayed_status)
2979 __dwc3_gadget_ep0_set_halt(ep0, 1);
2980 return;
2981 }
2982
2983 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2984 if (dwc->delayed_status)
2985 dwc3_ep0_send_delayed_status(dwc);
2986 }
2987
2988 if ((dep->flags & DWC3_EP_DELAY_START) &&
2989 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2990 __dwc3_gadget_kick_transfer(dep);
2991
2992 dep->flags &= ~DWC3_EP_DELAY_START;
2993}
2994
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07002995static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
2996 const struct dwc3_event_depevt *event)
2997{
2998 struct dwc3 *dwc = dep->dwc;
2999
3000 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3001 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3002 goto out;
3003 }
3004
3005 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3006 switch (event->parameters) {
3007 case DEPEVT_STREAM_PRIME:
3008 /*
3009 * If the host can properly transition the endpoint state from
3010 * idle to prime after a NoStream rejection, there's no need to
3011 * force restarting the endpoint to reinitiate the stream. To
3012 * simplify the check, assume the host follows the USB spec if
3013 * it primed the endpoint more than once.
3014 */
3015 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3016 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3017 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3018 else
3019 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3020 }
3021
3022 break;
3023 case DEPEVT_STREAM_NOSTREAM:
3024 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3025 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3026 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3027 break;
3028
3029 /*
3030 * If the host rejects a stream due to no active stream, by the
3031 * USB and xHCI spec, the endpoint will be put back to idle
3032 * state. When the host is ready (buffer added/updated), it will
3033 * prime the endpoint to inform the usb device controller. This
3034 * triggers the device controller to issue ERDY to restart the
3035 * stream. However, some hosts don't follow this and keep the
3036 * endpoint in the idle state. No prime will come despite host
3037 * streams are updated, and the device controller will not be
3038 * triggered to generate ERDY to move the next stream data. To
3039 * workaround this and maintain compatibility with various
3040 * hosts, force to reinitate the stream until the host is ready
3041 * instead of waiting for the host to prime the endpoint.
3042 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003043 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3044 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3045
3046 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3047 } else {
3048 dep->flags |= DWC3_EP_DELAY_START;
3049 dwc3_stop_active_transfer(dep, true, true);
3050 return;
3051 }
3052 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003053 }
3054
3055out:
3056 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3057}
3058
Felipe Balbi72246da2011-08-19 18:10:58 +03003059static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3060 const struct dwc3_event_depevt *event)
3061{
3062 struct dwc3_ep *dep;
3063 u8 epnum = event->endpoint_number;
3064
3065 dep = dwc->eps[epnum];
3066
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003067 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003068 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003069 return;
3070
3071 /* Handle only EPCMDCMPLT when EP disabled */
3072 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3073 return;
3074 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003075
Felipe Balbi72246da2011-08-19 18:10:58 +03003076 if (epnum == 0 || epnum == 1) {
3077 dwc3_ep0_interrupt(dwc, event);
3078 return;
3079 }
3080
3081 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003082 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003083 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003084 break;
3085 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003086 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003087 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003088 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003089 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003090 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003091 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003092 dwc3_gadget_endpoint_transfer_complete(dep, event);
3093 break;
3094 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003095 dwc3_gadget_endpoint_stream_event(dep, event);
3096 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003097 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003098 break;
3099 }
3100}
3101
3102static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3103{
3104 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3105 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003106 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003107 spin_lock(&dwc->lock);
3108 }
3109}
3110
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003111static void dwc3_suspend_gadget(struct dwc3 *dwc)
3112{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003113 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003114 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003115 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003116 spin_lock(&dwc->lock);
3117 }
3118}
3119
3120static void dwc3_resume_gadget(struct dwc3 *dwc)
3121{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003122 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003123 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003124 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003125 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003126 }
3127}
3128
3129static void dwc3_reset_gadget(struct dwc3 *dwc)
3130{
3131 if (!dwc->gadget_driver)
3132 return;
3133
Peter Chene81a7012020-08-21 10:55:48 +08003134 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003135 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003136 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003137 spin_lock(&dwc->lock);
3138 }
3139}
3140
Felipe Balbic5353b22019-02-13 13:00:54 +02003141static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3142 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003143{
Felipe Balbi72246da2011-08-19 18:10:58 +03003144 struct dwc3_gadget_ep_cmd_params params;
3145 u32 cmd;
3146 int ret;
3147
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003148 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3149 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303150 return;
3151
Pratyush Anand57911502012-07-06 15:19:10 +05303152 /*
3153 * NOTICE: We are violating what the Databook says about the
3154 * EndTransfer command. Ideally we would _always_ wait for the
3155 * EndTransfer Command Completion IRQ, but that's causing too
3156 * much trouble synchronizing between us and gadget driver.
3157 *
3158 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003159 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303160 *
3161 * Note also that a similar handling was tested by Synopsys
3162 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003163 * In short, what we're doing is issuing EndTransfer with
3164 * CMDIOC bit set and delay kicking transfer until the
3165 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003166 *
3167 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3168 * supports a mode to work around the above limitation. The
3169 * software can poll the CMDACT bit in the DEPCMD register
3170 * after issuing a EndTransfer command. This mode is enabled
3171 * by writing GUCTL2[14]. This polling is already done in the
3172 * dwc3_send_gadget_ep_cmd() function so if the mode is
3173 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003174 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003175 *
3176 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303177 */
3178
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303179 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003180 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003181 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003182 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303183 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003184 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303185 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003186 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003187
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003188 /*
3189 * The END_TRANSFER command will cause the controller to generate a
3190 * NoStream Event, and it's not due to the host DP NoStream rejection.
3191 * Ignore the next NoStream event.
3192 */
3193 if (dep->stream_capable)
3194 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3195
Thinh Nguyend3abda52019-11-27 13:10:47 -08003196 if (!interrupt)
3197 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003198 else
3199 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003200}
3201
Felipe Balbi72246da2011-08-19 18:10:58 +03003202static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3203{
3204 u32 epnum;
3205
3206 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3207 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003208 int ret;
3209
3210 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003211 if (!dep)
3212 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003213
3214 if (!(dep->flags & DWC3_EP_STALL))
3215 continue;
3216
3217 dep->flags &= ~DWC3_EP_STALL;
3218
John Youn50c763f2016-05-31 17:49:56 -07003219 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003220 WARN_ON_ONCE(ret);
3221 }
3222}
3223
3224static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3225{
Felipe Balbic4430a22012-05-24 10:30:01 +03003226 int reg;
3227
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003228 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3229
Felipe Balbi72246da2011-08-19 18:10:58 +03003230 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3231 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003232 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003233 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003234
Felipe Balbi72246da2011-08-19 18:10:58 +03003235 dwc3_disconnect_gadget(dwc);
3236
Peter Chene81a7012020-08-21 10:55:48 +08003237 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003238 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003239 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003240
3241 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003242}
3243
Felipe Balbi72246da2011-08-19 18:10:58 +03003244static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3245{
3246 u32 reg;
3247
Felipe Balbifc8bb912016-05-16 13:14:48 +03003248 dwc->connected = true;
3249
Felipe Balbidf62df52011-10-14 15:11:49 +03003250 /*
3251 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3252 * would cause a missing Disconnect Event if there's a
3253 * pending Setup Packet in the FIFO.
3254 *
3255 * There's no suggested workaround on the official Bug
3256 * report, which states that "unless the driver/application
3257 * is doing any special handling of a disconnect event,
3258 * there is no functional issue".
3259 *
3260 * Unfortunately, it turns out that we _do_ some special
3261 * handling of a disconnect event, namely complete all
3262 * pending transfers, notify gadget driver of the
3263 * disconnection, and so on.
3264 *
3265 * Our suggested workaround is to follow the Disconnect
3266 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003267 * flag. Such flag gets set whenever we have a SETUP_PENDING
3268 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003269 * same endpoint.
3270 *
3271 * Refers to:
3272 *
3273 * STAR#9000466709: RTL: Device : Disconnect event not
3274 * generated if setup packet pending in FIFO
3275 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003276 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003277 if (dwc->setup_packet_pending)
3278 dwc3_gadget_disconnect_interrupt(dwc);
3279 }
3280
Felipe Balbi8e744752014-11-06 14:27:53 +08003281 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003282 /*
3283 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3284 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3285 * needs to ensure that it sends "a DEPENDXFER command for any active
3286 * transfers."
3287 */
3288 dwc3_stop_active_transfers(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003289
3290 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3291 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003292 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003293 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003294 dwc3_clear_stall_all_ep(dwc);
3295
3296 /* Reset device address to zero */
3297 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3298 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3299 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003300}
3301
Felipe Balbi72246da2011-08-19 18:10:58 +03003302static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3303{
Felipe Balbi72246da2011-08-19 18:10:58 +03003304 struct dwc3_ep *dep;
3305 int ret;
3306 u32 reg;
3307 u8 speed;
3308
Felipe Balbi72246da2011-08-19 18:10:58 +03003309 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3310 speed = reg & DWC3_DSTS_CONNECTSPD;
3311 dwc->speed = speed;
3312
John Youn5fb6fda2016-11-10 17:23:25 -08003313 /*
3314 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3315 * each time on Connect Done.
3316 *
3317 * Currently we always use the reset value. If any platform
3318 * wants to set this to a different value, we need to add a
3319 * setting and update GCTL.RAMCLKSEL here.
3320 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003321
3322 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003323 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003324 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003325 dwc->gadget->ep0->maxpacket = 512;
3326 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
John Youn75808622016-02-05 17:09:13 -08003327 break;
John Youn2da9ad72016-05-20 16:34:26 -07003328 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003329 /*
3330 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3331 * would cause a missing USB3 Reset event.
3332 *
3333 * In such situations, we should force a USB3 Reset
3334 * event by calling our dwc3_gadget_reset_interrupt()
3335 * routine.
3336 *
3337 * Refers to:
3338 *
3339 * STAR#9000483510: RTL: SS : USB3 reset event may
3340 * not be generated always when the link enters poll
3341 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003342 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003343 dwc3_gadget_reset_interrupt(dwc);
3344
Felipe Balbi72246da2011-08-19 18:10:58 +03003345 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003346 dwc->gadget->ep0->maxpacket = 512;
3347 dwc->gadget->speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03003348 break;
John Youn2da9ad72016-05-20 16:34:26 -07003349 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003350 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003351 dwc->gadget->ep0->maxpacket = 64;
3352 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003353 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003354 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003355 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003356 dwc->gadget->ep0->maxpacket = 64;
3357 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003358 break;
John Youn2da9ad72016-05-20 16:34:26 -07003359 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003360 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003361 dwc->gadget->ep0->maxpacket = 8;
3362 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003363 break;
3364 }
3365
Peter Chene81a7012020-08-21 10:55:48 +08003366 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003367
Pratyush Anand2b758352013-01-14 15:59:31 +05303368 /* Enable USB2 LPM Capability */
3369
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003370 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003371 (speed != DWC3_DSTS_SUPERSPEED) &&
3372 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303373 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3374 reg |= DWC3_DCFG_LPM_CAP;
3375 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3376
3377 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3378 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3379
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003380 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3381 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303382
Huang Rui80caf7d2014-10-28 19:54:26 +08003383 /*
3384 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3385 * DCFG.LPMCap is set, core responses with an ACK and the
3386 * BESL value in the LPM token is less than or equal to LPM
3387 * NYET threshold.
3388 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003389 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003390 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003391
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003392 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003393 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003394
Thinh Nguyen5b738212019-10-23 19:15:43 -07003395 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003396 } else {
3397 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3398 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003399 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303400 }
3401
Felipe Balbi72246da2011-08-19 18:10:58 +03003402 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003403 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003404 if (ret) {
3405 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3406 return;
3407 }
3408
3409 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003410 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003411 if (ret) {
3412 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3413 return;
3414 }
3415
3416 /*
3417 * Configure PHY via GUSB3PIPECTLn if required.
3418 *
3419 * Update GTXFIFOSIZn
3420 *
3421 * In both cases reset values should be sufficient.
3422 */
3423}
3424
3425static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3426{
Felipe Balbi72246da2011-08-19 18:10:58 +03003427 /*
3428 * TODO take core out of low power mode when that's
3429 * implemented.
3430 */
3431
Jiebing Liad14d4e2014-12-11 13:26:29 +08003432 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3433 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003434 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003435 spin_lock(&dwc->lock);
3436 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003437}
3438
3439static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3440 unsigned int evtinfo)
3441{
Felipe Balbifae2b902011-10-14 13:00:30 +03003442 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003443 unsigned int pwropt;
3444
3445 /*
3446 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3447 * Hibernation mode enabled which would show up when device detects
3448 * host-initiated U3 exit.
3449 *
3450 * In that case, device will generate a Link State Change Interrupt
3451 * from U3 to RESUME which is only necessary if Hibernation is
3452 * configured in.
3453 *
3454 * There are no functional changes due to such spurious event and we
3455 * just need to ignore it.
3456 *
3457 * Refers to:
3458 *
3459 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3460 * operational mode
3461 */
3462 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003463 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003464 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3465 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3466 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003467 return;
3468 }
3469 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003470
3471 /*
3472 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3473 * on the link partner, the USB session might do multiple entry/exit
3474 * of low power states before a transfer takes place.
3475 *
3476 * Due to this problem, we might experience lower throughput. The
3477 * suggested workaround is to disable DCTL[12:9] bits if we're
3478 * transitioning from U1/U2 to U0 and enable those bits again
3479 * after a transfer completes and there are no pending transfers
3480 * on any of the enabled endpoints.
3481 *
3482 * This is the first half of that workaround.
3483 *
3484 * Refers to:
3485 *
3486 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3487 * core send LGO_Ux entering U0
3488 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003489 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003490 if (next == DWC3_LINK_STATE_U0) {
3491 u32 u1u2;
3492 u32 reg;
3493
3494 switch (dwc->link_state) {
3495 case DWC3_LINK_STATE_U1:
3496 case DWC3_LINK_STATE_U2:
3497 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3498 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3499 | DWC3_DCTL_ACCEPTU2ENA
3500 | DWC3_DCTL_INITU1ENA
3501 | DWC3_DCTL_ACCEPTU1ENA);
3502
3503 if (!dwc->u1u2)
3504 dwc->u1u2 = reg & u1u2;
3505
3506 reg &= ~u1u2;
3507
Thinh Nguyen5b738212019-10-23 19:15:43 -07003508 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003509 break;
3510 default:
3511 /* do nothing */
3512 break;
3513 }
3514 }
3515 }
3516
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003517 switch (next) {
3518 case DWC3_LINK_STATE_U1:
3519 if (dwc->speed == USB_SPEED_SUPER)
3520 dwc3_suspend_gadget(dwc);
3521 break;
3522 case DWC3_LINK_STATE_U2:
3523 case DWC3_LINK_STATE_U3:
3524 dwc3_suspend_gadget(dwc);
3525 break;
3526 case DWC3_LINK_STATE_RESUME:
3527 dwc3_resume_gadget(dwc);
3528 break;
3529 default:
3530 /* do nothing */
3531 break;
3532 }
3533
Felipe Balbie57ebc12014-04-22 13:20:12 -05003534 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003535}
3536
Baolin Wang72704f82016-05-16 16:43:53 +08003537static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3538 unsigned int evtinfo)
3539{
3540 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3541
3542 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3543 dwc3_suspend_gadget(dwc);
3544
3545 dwc->link_state = next;
3546}
3547
Felipe Balbie1dadd32014-02-25 14:47:54 -06003548static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3549 unsigned int evtinfo)
3550{
3551 unsigned int is_ss = evtinfo & BIT(4);
3552
Felipe Balbibfad65e2017-04-19 14:59:27 +03003553 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003554 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3555 * have a known issue which can cause USB CV TD.9.23 to fail
3556 * randomly.
3557 *
3558 * Because of this issue, core could generate bogus hibernation
3559 * events which SW needs to ignore.
3560 *
3561 * Refers to:
3562 *
3563 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3564 * Device Fallback from SuperSpeed
3565 */
3566 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3567 return;
3568
3569 /* enter hibernation here */
3570}
3571
Felipe Balbi72246da2011-08-19 18:10:58 +03003572static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3573 const struct dwc3_event_devt *event)
3574{
3575 switch (event->type) {
3576 case DWC3_DEVICE_EVENT_DISCONNECT:
3577 dwc3_gadget_disconnect_interrupt(dwc);
3578 break;
3579 case DWC3_DEVICE_EVENT_RESET:
3580 dwc3_gadget_reset_interrupt(dwc);
3581 break;
3582 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3583 dwc3_gadget_conndone_interrupt(dwc);
3584 break;
3585 case DWC3_DEVICE_EVENT_WAKEUP:
3586 dwc3_gadget_wakeup_interrupt(dwc);
3587 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003588 case DWC3_DEVICE_EVENT_HIBER_REQ:
3589 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3590 "unexpected hibernation event\n"))
3591 break;
3592
3593 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3594 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003595 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3596 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3597 break;
3598 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003599 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003600 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003601 /*
3602 * Ignore suspend event until the gadget enters into
3603 * USB_STATE_CONFIGURED state.
3604 */
Peter Chene81a7012020-08-21 10:55:48 +08003605 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003606 dwc3_gadget_suspend_interrupt(dwc,
3607 event->event_info);
3608 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003609 break;
3610 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003611 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003612 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003613 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003614 break;
3615 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003616 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003617 }
3618}
3619
3620static void dwc3_process_event_entry(struct dwc3 *dwc,
3621 const union dwc3_event *event)
3622{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003623 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003624
Felipe Balbidfc5e802017-04-26 13:44:51 +03003625 if (!event->type.is_devspec)
3626 dwc3_endpoint_interrupt(dwc, &event->depevt);
3627 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003628 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003629 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003630 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003631}
3632
Felipe Balbidea520a2016-03-30 09:39:34 +03003633static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003634{
Felipe Balbidea520a2016-03-30 09:39:34 +03003635 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003636 irqreturn_t ret = IRQ_NONE;
3637 int left;
3638 u32 reg;
3639
Felipe Balbif42f2442013-06-12 21:25:08 +03003640 left = evt->count;
3641
3642 if (!(evt->flags & DWC3_EVENT_PENDING))
3643 return IRQ_NONE;
3644
3645 while (left > 0) {
3646 union dwc3_event event;
3647
John Younebbb2d52016-11-15 13:07:02 +02003648 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003649
3650 dwc3_process_event_entry(dwc, &event);
3651
3652 /*
3653 * FIXME we wrap around correctly to the next entry as
3654 * almost all entries are 4 bytes in size. There is one
3655 * entry which has 12 bytes which is a regular entry
3656 * followed by 8 bytes data. ATM I don't know how
3657 * things are organized if we get next to the a
3658 * boundary so I worry about that once we try to handle
3659 * that.
3660 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003661 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003662 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003663 }
3664
3665 evt->count = 0;
3666 evt->flags &= ~DWC3_EVENT_PENDING;
3667 ret = IRQ_HANDLED;
3668
3669 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003670 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003671 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003672 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003673
John Youncf40b862016-11-14 12:32:43 -08003674 if (dwc->imod_interval) {
3675 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3676 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3677 }
3678
Felipe Balbif42f2442013-06-12 21:25:08 +03003679 return ret;
3680}
3681
Felipe Balbidea520a2016-03-30 09:39:34 +03003682static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003683{
Felipe Balbidea520a2016-03-30 09:39:34 +03003684 struct dwc3_event_buffer *evt = _evt;
3685 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003686 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003687 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003688
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003689 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003690 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003691 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003692
3693 return ret;
3694}
3695
Felipe Balbidea520a2016-03-30 09:39:34 +03003696static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003697{
Felipe Balbidea520a2016-03-30 09:39:34 +03003698 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003699 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003700 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003701 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003702
Felipe Balbifc8bb912016-05-16 13:14:48 +03003703 if (pm_runtime_suspended(dwc->dev)) {
3704 pm_runtime_get(dwc->dev);
3705 disable_irq_nosync(dwc->irq_gadget);
3706 dwc->pending_events = true;
3707 return IRQ_HANDLED;
3708 }
3709
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003710 /*
3711 * With PCIe legacy interrupt, test shows that top-half irq handler can
3712 * be called again after HW interrupt deassertion. Check if bottom-half
3713 * irq event handler completes before caching new event to prevent
3714 * losing events.
3715 */
3716 if (evt->flags & DWC3_EVENT_PENDING)
3717 return IRQ_HANDLED;
3718
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003719 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003720 count &= DWC3_GEVNTCOUNT_MASK;
3721 if (!count)
3722 return IRQ_NONE;
3723
Felipe Balbib15a7622011-06-30 16:57:15 +03003724 evt->count = count;
3725 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003726
Felipe Balbie8adfc32013-06-12 21:11:14 +03003727 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003728 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003729 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003730 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003731
John Younebbb2d52016-11-15 13:07:02 +02003732 amount = min(count, evt->length - evt->lpos);
3733 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3734
3735 if (amount < count)
3736 memcpy(evt->cache, evt->buf, count - amount);
3737
John Youn65aca322016-11-15 13:08:59 +02003738 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3739
Felipe Balbib15a7622011-06-30 16:57:15 +03003740 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003741}
3742
Felipe Balbidea520a2016-03-30 09:39:34 +03003743static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003744{
Felipe Balbidea520a2016-03-30 09:39:34 +03003745 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003746
Felipe Balbidea520a2016-03-30 09:39:34 +03003747 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003748}
3749
Felipe Balbi6db38122016-10-03 11:27:01 +03003750static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3751{
3752 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3753 int irq;
3754
Hans de Goedef146b402019-10-05 23:04:48 +02003755 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003756 if (irq > 0)
3757 goto out;
3758
3759 if (irq == -EPROBE_DEFER)
3760 goto out;
3761
Hans de Goedef146b402019-10-05 23:04:48 +02003762 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003763 if (irq > 0)
3764 goto out;
3765
3766 if (irq == -EPROBE_DEFER)
3767 goto out;
3768
3769 irq = platform_get_irq(dwc3_pdev, 0);
3770 if (irq > 0)
3771 goto out;
3772
Felipe Balbi6db38122016-10-03 11:27:01 +03003773 if (!irq)
3774 irq = -EINVAL;
3775
3776out:
3777 return irq;
3778}
3779
Peter Chene81a7012020-08-21 10:55:48 +08003780static void dwc_gadget_release(struct device *dev)
3781{
3782 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3783
3784 kfree(gadget);
3785}
3786
Felipe Balbi72246da2011-08-19 18:10:58 +03003787/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003788 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003789 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003790 *
3791 * Returns 0 on success otherwise negative errno.
3792 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003793int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003794{
Felipe Balbi6db38122016-10-03 11:27:01 +03003795 int ret;
3796 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003797 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003798
Felipe Balbi6db38122016-10-03 11:27:01 +03003799 irq = dwc3_gadget_get_irq(dwc);
3800 if (irq < 0) {
3801 ret = irq;
3802 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003803 }
3804
3805 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003806
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303807 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3808 sizeof(*dwc->ep0_trb) * 2,
3809 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003810 if (!dwc->ep0_trb) {
3811 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3812 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003813 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003814 }
3815
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003816 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003817 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003818 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003819 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003820 }
3821
Felipe Balbi905dc042017-01-05 14:46:52 +02003822 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3823 &dwc->bounce_addr, GFP_KERNEL);
3824 if (!dwc->bounce) {
3825 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003826 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003827 }
3828
Baolin Wangbb014732016-10-14 17:11:33 +08003829 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003830 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3831 if (!dwc->gadget) {
3832 ret = -ENOMEM;
3833 goto err3;
3834 }
Baolin Wangbb014732016-10-14 17:11:33 +08003835
Peter Chene81a7012020-08-21 10:55:48 +08003836
3837 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3838 dev = &dwc->gadget->dev;
3839 dev->platform_data = dwc;
3840 dwc->gadget->ops = &dwc3_gadget_ops;
3841 dwc->gadget->speed = USB_SPEED_UNKNOWN;
3842 dwc->gadget->sg_supported = true;
3843 dwc->gadget->name = "dwc3-gadget";
3844 dwc->gadget->lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003845
3846 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003847 * FIXME We might be setting max_speed to <SUPER, however versions
3848 * <2.20a of dwc3 have an issue with metastability (documented
3849 * elsewhere in this driver) which tells us we can't set max speed to
3850 * anything lower than SUPER.
3851 *
3852 * Because gadget.max_speed is only used by composite.c and function
3853 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3854 * to happen so we avoid sending SuperSpeed Capability descriptor
3855 * together with our BOS descriptor as that could confuse host into
3856 * thinking we can handle super speed.
3857 *
3858 * Note that, in fact, we won't even support GetBOS requests when speed
3859 * is less than super speed because we don't have means, yet, to tell
3860 * composite.c that we are USB 2.0 + LPM ECN.
3861 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003862 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003863 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003864 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003865 dwc->revision);
3866
Peter Chene81a7012020-08-21 10:55:48 +08003867 dwc->gadget->max_speed = dwc->maximum_speed;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003868
3869 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003870 * REVISIT: Here we should clear all pending IRQs to be
3871 * sure we're starting from a well known location.
3872 */
3873
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003874 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003875 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003876 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003877
3878 ret = usb_add_gadget(dwc->gadget);
3879 if (ret) {
3880 dev_err(dwc->dev, "failed to add gadget\n");
3881 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003882 }
3883
Peter Chene81a7012020-08-21 10:55:48 +08003884 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003885
Felipe Balbi72246da2011-08-19 18:10:58 +03003886 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003887
Peter Chene81a7012020-08-21 10:55:48 +08003888err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003889 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003890err4:
3891 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003892err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003893 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3894 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003895
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003896err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003897 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003898
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003899err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303900 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003901 dwc->ep0_trb, dwc->ep0_trb_addr);
3902
Felipe Balbi72246da2011-08-19 18:10:58 +03003903err0:
3904 return ret;
3905}
3906
Felipe Balbi7415f172012-04-30 14:56:33 +03003907/* -------------------------------------------------------------------------- */
3908
Felipe Balbi72246da2011-08-19 18:10:58 +03003909void dwc3_gadget_exit(struct dwc3 *dwc)
3910{
Peter Chene81a7012020-08-21 10:55:48 +08003911 usb_del_gadget_udc(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003912 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003913 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003914 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003915 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303916 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003917 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003918}
Felipe Balbi7415f172012-04-30 14:56:33 +03003919
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003920int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003921{
Roger Quadros9772b472016-04-12 11:33:29 +03003922 if (!dwc->gadget_driver)
3923 return 0;
3924
Roger Quadros1551e352017-02-15 14:16:26 +02003925 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003926 dwc3_disconnect_gadget(dwc);
3927 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003928
3929 return 0;
3930}
3931
3932int dwc3_gadget_resume(struct dwc3 *dwc)
3933{
Felipe Balbi7415f172012-04-30 14:56:33 +03003934 int ret;
3935
Roger Quadros9772b472016-04-12 11:33:29 +03003936 if (!dwc->gadget_driver)
3937 return 0;
3938
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003939 ret = __dwc3_gadget_start(dwc);
3940 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003941 goto err0;
3942
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003943 ret = dwc3_gadget_run_stop(dwc, true, false);
3944 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003945 goto err1;
3946
Felipe Balbi7415f172012-04-30 14:56:33 +03003947 return 0;
3948
3949err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003950 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003951
3952err0:
3953 return ret;
3954}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003955
3956void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3957{
3958 if (dwc->pending_events) {
3959 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3960 dwc->pending_events = false;
3961 enable_irq(dwc->irq_gadget);
3962 }
3963}