blob: 2a86ad4b12b346875aa0cff7749b535dae5c3646 [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) {
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800608 u8 bInterval_m1;
609
610 /*
611 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13, and it
612 * must be set to 0 when the controller operates in full-speed.
613 */
614 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
615 if (dwc->gadget->speed == USB_SPEED_FULL)
616 bInterval_m1 = 0;
617
Thinh Nguyen5b4cd962021-02-08 13:53:16 -0800618 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
619 dwc->gadget->speed == USB_SPEED_FULL)
620 dep->interval = desc->bInterval;
621 else
622 dep->interval = 1 << (desc->bInterval - 1);
623
Thinh Nguyen6b78b382021-02-08 13:53:10 -0800624 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 }
626
Felipe Balbi2cd47182016-04-12 16:42:43 +0300627 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300628}
629
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700630static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
631 bool interrupt);
632
Felipe Balbi72246da2011-08-19 18:10:58 +0300633/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300634 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300636 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300638 * Caller should take care of locking. Execute all necessary commands to
639 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300640 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300641static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300642{
John Youn39ebb052016-11-09 16:36:28 -0800643 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800645
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300647 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300648
649 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300650 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 if (ret)
652 return ret;
653 }
654
Felipe Balbib07c2db2018-04-09 12:46:47 +0300655 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 if (ret)
657 return ret;
658
659 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200660 struct dwc3_trb *trb_st_hw;
661 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300662
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 dep->type = usb_endpoint_type(desc);
664 dep->flags |= DWC3_EP_ENABLED;
665
666 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
667 reg |= DWC3_DALEPENA_EP(dep->number);
668 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
669
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300670 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200671 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300672
John Youn0d257442016-05-19 17:26:08 -0700673 /* Initialize the TRB ring */
674 dep->trb_dequeue = 0;
675 dep->trb_enqueue = 0;
676 memset(dep->trb_pool, 0,
677 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
678
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300679 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 trb_st_hw = &dep->trb_pool[0];
681
Felipe Balbif6bafc62012-02-06 11:04:53 +0200682 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200683 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
684 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
685 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
686 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 }
688
Felipe Balbia97ea992016-09-29 16:28:56 +0300689 /*
690 * Issue StartTransfer here with no-op TRB so we can always rely on No
691 * Response Update Transfer command.
692 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700693 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300694 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300695 struct dwc3_gadget_ep_cmd_params params;
696 struct dwc3_trb *trb;
697 dma_addr_t trb_dma;
698 u32 cmd;
699
700 memset(&params, 0, sizeof(params));
701 trb = &dep->trb_pool[0];
702 trb_dma = dwc3_trb_dma_offset(dep, trb);
703
704 params.param0 = upper_32_bits(trb_dma);
705 params.param1 = lower_32_bits(trb_dma);
706
707 cmd = DWC3_DEPCMD_STARTTRANSFER;
708
709 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
710 if (ret < 0)
711 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700712
713 if (dep->stream_capable) {
714 /*
715 * For streams, at start, there maybe a race where the
716 * host primes the endpoint before the function driver
717 * queues a request to initiate a stream. In that case,
718 * the controller will not see the prime to generate the
719 * ERDY and start stream. To workaround this, issue a
720 * no-op TRB as normal, but end it immediately. As a
721 * result, when the function driver queues the request,
722 * the next START_TRANSFER command will cause the
723 * controller to generate an ERDY to initiate the
724 * stream.
725 */
726 dwc3_stop_active_transfer(dep, true, true);
727
728 /*
729 * All stream eps will reinitiate stream on NoStream
730 * rejection until we can determine that the host can
731 * prime after the first transfer.
732 */
733 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
734 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300735 }
736
Felipe Balbi2870e502016-11-03 13:53:29 +0200737out:
738 trace_dwc3_gadget_ep_enable(dep);
739
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 return 0;
741}
742
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300744{
745 struct dwc3_request *req;
746
Felipe Balbic5353b22019-02-13 13:00:54 +0200747 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300748
Felipe Balbi0e146022016-06-21 10:32:02 +0300749 /* - giveback all requests to gadget driver */
750 while (!list_empty(&dep->started_list)) {
751 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200752
Felipe Balbi0e146022016-06-21 10:32:02 +0300753 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200754 }
755
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200756 while (!list_empty(&dep->pending_list)) {
757 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200759 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300760 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200761
762 while (!list_empty(&dep->cancelled_list)) {
763 req = next_request(&dep->cancelled_list);
764
765 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
766 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300767}
768
769/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300770 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300771 * @dep: the endpoint to disable
772 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300773 * This function undoes what __dwc3_gadget_ep_enable did and also removes
774 * requests which are currently being processed by the hardware and those which
775 * are not yet scheduled.
776 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200777 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300779static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
780{
781 struct dwc3 *dwc = dep->dwc;
782 u32 reg;
783
Felipe Balbi2870e502016-11-03 13:53:29 +0200784 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500785
Felipe Balbi687ef982014-04-16 10:30:33 -0500786 /* make sure HW endpoint isn't stalled */
787 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500788 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500789
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
791 reg &= ~DWC3_DALEPENA_EP(dep->number);
792 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
793
Felipe Balbi879631a2011-09-30 10:58:47 +0300794 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200796 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300797
John Youn39ebb052016-11-09 16:36:28 -0800798 /* Clear out the ep descriptors for non-ep0 */
799 if (dep->number > 1) {
800 dep->endpoint.comp_desc = NULL;
801 dep->endpoint.desc = NULL;
802 }
803
Wesley Chengc7bb96a2021-03-11 15:59:02 -0800804 dwc3_remove_requests(dwc, dep);
805
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 return 0;
807}
808
809/* -------------------------------------------------------------------------- */
810
811static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
812 const struct usb_endpoint_descriptor *desc)
813{
814 return -EINVAL;
815}
816
817static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
818{
819 return -EINVAL;
820}
821
822/* -------------------------------------------------------------------------- */
823
824static int dwc3_gadget_ep_enable(struct usb_ep *ep,
825 const struct usb_endpoint_descriptor *desc)
826{
827 struct dwc3_ep *dep;
828 struct dwc3 *dwc;
829 unsigned long flags;
830 int ret;
831
832 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
833 pr_debug("dwc3: invalid parameters\n");
834 return -EINVAL;
835 }
836
837 if (!desc->wMaxPacketSize) {
838 pr_debug("dwc3: missing wMaxPacketSize\n");
839 return -EINVAL;
840 }
841
842 dep = to_dwc3_ep(ep);
843 dwc = dep->dwc;
844
Felipe Balbi95ca9612015-12-10 13:08:20 -0600845 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
846 "%s is already enabled\n",
847 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300848 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300849
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300851 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 spin_unlock_irqrestore(&dwc->lock, flags);
853
854 return ret;
855}
856
857static int dwc3_gadget_ep_disable(struct usb_ep *ep)
858{
859 struct dwc3_ep *dep;
860 struct dwc3 *dwc;
861 unsigned long flags;
862 int ret;
863
864 if (!ep) {
865 pr_debug("dwc3: invalid parameters\n");
866 return -EINVAL;
867 }
868
869 dep = to_dwc3_ep(ep);
870 dwc = dep->dwc;
871
Felipe Balbi95ca9612015-12-10 13:08:20 -0600872 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
873 "%s is already disabled\n",
874 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300875 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300876
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 spin_lock_irqsave(&dwc->lock, flags);
878 ret = __dwc3_gadget_ep_disable(dep);
879 spin_unlock_irqrestore(&dwc->lock, flags);
880
881 return ret;
882}
883
884static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300885 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300886{
887 struct dwc3_request *req;
888 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
890 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900891 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300892 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300893
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300894 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300895 req->epnum = dep->number;
896 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200897 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500899 trace_dwc3_alloc_request(req);
900
Felipe Balbi72246da2011-08-19 18:10:58 +0300901 return &req->request;
902}
903
904static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
905 struct usb_request *request)
906{
907 struct dwc3_request *req = to_dwc3_request(request);
908
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500909 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 kfree(req);
911}
912
Felipe Balbi42626912018-04-09 13:01:43 +0300913/**
914 * dwc3_ep_prev_trb - returns the previous TRB in the ring
915 * @dep: The endpoint with the TRB ring
916 * @index: The index of the current TRB in the ring
917 *
918 * Returns the TRB prior to the one pointed to by the index. If the
919 * index is 0, we will wrap backwards, skip the link TRB, and return
920 * the one just before that.
921 */
922static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
923{
924 u8 tmp = index;
925
926 if (!tmp)
927 tmp = DWC3_TRB_NUM - 1;
928
929 return &dep->trb_pool[tmp - 1];
930}
931
932static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
933{
934 struct dwc3_trb *tmp;
935 u8 trbs_left;
936
937 /*
938 * If enqueue & dequeue are equal than it is either full or empty.
939 *
940 * One way to know for sure is if the TRB right before us has HWO bit
941 * set or not. If it has, then we're definitely full and can't fit any
942 * more transfers in our ring.
943 */
944 if (dep->trb_enqueue == dep->trb_dequeue) {
945 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
946 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
947 return 0;
948
949 return DWC3_TRB_NUM - 1;
950 }
951
952 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
953 trbs_left &= (DWC3_TRB_NUM - 1);
954
955 if (dep->trb_dequeue < dep->trb_enqueue)
956 trbs_left--;
957
958 return trbs_left;
959}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300960
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200961static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300962 dma_addr_t dma, unsigned int length, unsigned int chain,
963 unsigned int node, unsigned int stream_id,
964 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -0700965 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200966{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300967 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800968 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300969 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200970
Felipe Balbif6bafc62012-02-06 11:04:53 +0200971 trb->size = DWC3_TRB_SIZE_LENGTH(length);
972 trb->bpl = lower_32_bits(dma);
973 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200974
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200975 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200976 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200977 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200978 break;
979
980 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300981 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530982 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300983
Manu Gautam40d829f2017-07-19 17:07:10 +0530984 /*
985 * USB Specification 2.0 Section 5.9.2 states that: "If
986 * there is only a single transaction in the microframe,
987 * only a DATA0 data packet PID is used. If there are
988 * two transactions per microframe, DATA1 is used for
989 * the first transaction data packet and DATA0 is used
990 * for the second transaction data packet. If there are
991 * three transactions per microframe, DATA2 is used for
992 * the first transaction data packet, DATA1 is used for
993 * the second, and DATA0 is used for the third."
994 *
995 * IOW, we should satisfy the following cases:
996 *
997 * 1) length <= maxpacket
998 * - DATA0
999 *
1000 * 2) maxpacket < length <= (2 * maxpacket)
1001 * - DATA1, DATA0
1002 *
1003 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1004 * - DATA2, DATA1, DATA0
1005 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001006 if (speed == USB_SPEED_HIGH) {
1007 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301008 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301009 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1010
1011 if (length <= (2 * maxp))
1012 mult--;
1013
1014 if (length <= maxp)
1015 mult--;
1016
1017 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001018 }
1019 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301020 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001021 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001022
1023 /* always enable Interrupt on Missed ISOC */
1024 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001025 break;
1026
1027 case USB_ENDPOINT_XFER_BULK:
1028 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001029 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001030 break;
1031 default:
1032 /*
1033 * This is only possible with faulty memory because we
1034 * checked it already :)
1035 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001036 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1037 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001038 }
1039
Tejas Joglekar244add82018-12-10 16:08:13 +05301040 /*
1041 * Enable Continue on Short Packet
1042 * when endpoint is not a stream capable
1043 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001044 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301045 if (!dep->stream_capable)
1046 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001047
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001048 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001049 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1050 }
1051
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001052 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001053 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001054
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301055 if (chain)
1056 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001057 else if (dep->stream_capable && is_last)
1058 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301059
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001060 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001061 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001062
1063 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001064
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301065 dwc3_ep_inc_enq(dep);
1066
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001067 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001068}
1069
John Youn361572b2016-05-19 17:26:17 -07001070/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001071 * dwc3_prepare_one_trb - setup one TRB from one request
1072 * @dep: endpoint for which this request is prepared
1073 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001074 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001075 * @chain: should this TRB be chained to the next?
1076 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001077 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001078 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001079 */
1080static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001081 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001082 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1083 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001084{
1085 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301086 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001087 unsigned int stream_id = req->request.stream_id;
1088 unsigned int short_not_ok = req->request.short_not_ok;
1089 unsigned int no_interrupt = req->request.no_interrupt;
1090 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301091
Thinh Nguyen2b803572020-09-24 01:21:30 -07001092 if (use_bounce_buffer)
1093 dma = dep->dwc->bounce_addr;
1094 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301095 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001096 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301097 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001098
1099 trb = &dep->trb_pool[dep->trb_enqueue];
1100
1101 if (!req->trb) {
1102 dwc3_gadget_move_started_request(req);
1103 req->trb = trb;
1104 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001105 }
1106
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001107 req->num_trbs++;
1108
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001109 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001110 stream_id, short_not_ok, no_interrupt, is_last,
1111 must_interrupt);
1112}
1113
1114static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1115{
1116 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1117 unsigned int rem = req->request.length % maxp;
1118
1119 if ((req->request.length && req->request.zero && !rem &&
1120 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1121 (!req->direction && rem))
1122 return true;
1123
1124 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001125}
1126
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001127/**
1128 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1129 * @dep: The endpoint that the request belongs to
1130 * @req: The request to prepare
1131 * @entry_length: The last SG entry size
1132 * @node: Indicates whether this is not the first entry (for isoc only)
1133 *
1134 * Return the number of TRBs prepared.
1135 */
1136static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1137 struct dwc3_request *req, unsigned int entry_length,
1138 unsigned int node)
1139{
1140 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1141 unsigned int rem = req->request.length % maxp;
1142 unsigned int num_trbs = 1;
1143
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001144 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001145 num_trbs++;
1146
1147 if (dwc3_calc_trbs_left(dep) < num_trbs)
1148 return 0;
1149
1150 req->needs_extra_trb = num_trbs > 1;
1151
1152 /* Prepare a normal TRB */
1153 if (req->direction || req->request.length)
1154 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001155 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001156
1157 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1158 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1159 dwc3_prepare_one_trb(dep, req,
1160 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001161 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001162
1163 return num_trbs;
1164}
1165
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001166static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001167 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001168{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301169 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001170 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001171 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001172 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301173 unsigned int remaining = req->request.num_mapped_sgs
1174 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001175 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001176 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301177
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001178 /*
1179 * If we resume preparing the request, then get the remaining length of
1180 * the request and resume where we left off.
1181 */
1182 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1183 length -= sg_dma_len(s);
1184
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301185 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001186 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001187 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001188 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001189 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001190
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001191 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1192
1193 length -= trb_length;
1194
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001195 /*
1196 * IOMMU driver is coalescing the list of sgs which shares a
1197 * page boundary into one and giving it to USB driver. With
1198 * this the number of sgs mapped is not equal to the number of
1199 * sgs passed. So mark the chain bit to false if it isthe last
1200 * mapped sg.
1201 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001202 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001203 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001204
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001205 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001206 break;
1207
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001208 if (last_sg) {
1209 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001210 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001211 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001212 /*
1213 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001214 * next SG entry. If not, set interrupt on this TRB to
1215 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001216 * free.
1217 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001218 if (num_trbs_left == 1 || (needs_extra_trb &&
1219 num_trbs_left <= 2 &&
1220 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001221 must_interrupt = true;
1222
1223 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1224 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001225 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001226
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301227 /*
1228 * There can be a situation where all sgs in sglist are not
1229 * queued because of insufficient trb number. To handle this
1230 * case, update start_sg to next sg to be queued, so that
1231 * we have free trbs we can continue queuing from where we
1232 * previously stopped
1233 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001234 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301235 req->start_sg = sg_next(s);
1236
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301237 req->num_queued_sgs++;
1238
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001239 /*
1240 * The number of pending SG entries may not correspond to the
1241 * number of mapped SG entries. If all the data are queued, then
1242 * don't include unused SG entries.
1243 */
1244 if (length == 0) {
1245 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1246 break;
1247 }
1248
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001249 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001250 break;
1251 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001252
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001253 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001254}
1255
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001256static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001257 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001258{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001259 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001260}
1261
Felipe Balbi72246da2011-08-19 18:10:58 +03001262/*
1263 * dwc3_prepare_trbs - setup TRBs from requests
1264 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001265 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001266 * The function goes through the requests list and sets up TRBs for the
1267 * transfers. The function returns once there are no more TRBs available or
1268 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001269 *
1270 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001271 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001272static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001273{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001274 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001275 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001276
1277 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1278
Felipe Balbid86c5a62016-10-25 13:48:52 +03001279 /*
1280 * We can get in a situation where there's a request in the started list
1281 * but there weren't enough TRBs to fully kick it in the first time
1282 * around, so it has been waiting for more TRBs to be freed up.
1283 *
1284 * In that case, we should check if we have a request with pending_sgs
1285 * in the started list and prepare TRBs for that request first,
1286 * otherwise we will prepare TRBs completely out of order and that will
1287 * break things.
1288 */
1289 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001290 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001291 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001292 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001293 return ret;
1294 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001295
1296 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001297 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001298
1299 /*
1300 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1301 * burst capability may try to read and use TRBs beyond the
1302 * active transfer instead of stopping.
1303 */
1304 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001305 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001306 }
1307
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001308 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001309 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001310
1311 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1312 dep->direction);
1313 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001314 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001315
1316 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301317 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301318 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001319 req->num_pending_sgs = req->request.num_mapped_sgs;
1320
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001321 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001322 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001323 if (req->num_pending_sgs)
1324 return ret;
1325 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001326 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001327 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001328
Thinh Nguyen490410b2020-09-24 01:21:55 -07001329 if (!ret || !dwc3_calc_trbs_left(dep))
1330 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001331
1332 /*
1333 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1334 * burst capability may try to read and use TRBs beyond the
1335 * active transfer instead of stopping.
1336 */
1337 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001338 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001340
1341 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001342}
1343
Thinh Nguyen8d990872020-03-29 16:12:57 -07001344static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1345
Felipe Balbi7fdca762017-09-05 14:41:34 +03001346static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001347{
1348 struct dwc3_gadget_ep_cmd_params params;
1349 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001350 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001351 int ret;
1352 u32 cmd;
1353
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001354 /*
1355 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1356 * This happens when we need to stop and restart a transfer such as in
1357 * the case of reinitiating a stream or retrying an isoc transfer.
1358 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001359 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001360 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001361 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001362
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001363 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001364
Thinh Nguyen23384842020-09-30 17:44:38 -07001365 /*
1366 * If there's no new TRB prepared and we don't need to restart a
1367 * transfer, there's no need to update the transfer.
1368 */
1369 if (!ret && !starting)
1370 return ret;
1371
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001372 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001373 if (!req) {
1374 dep->flags |= DWC3_EP_PENDING_REQUEST;
1375 return 0;
1376 }
1377
1378 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001379
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001380 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301381 params.param0 = upper_32_bits(req->trb_dma);
1382 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001383 cmd = DWC3_DEPCMD_STARTTRANSFER;
1384
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301385 if (dep->stream_capable)
1386 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1387
Felipe Balbi7fdca762017-09-05 14:41:34 +03001388 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1389 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301390 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001391 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1392 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301393 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001394
Felipe Balbi2cd47182016-04-12 16:42:43 +03001395 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001396 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001397 struct dwc3_request *tmp;
1398
1399 if (ret == -EAGAIN)
1400 return ret;
1401
1402 dwc3_stop_active_transfer(dep, true, true);
1403
1404 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1405 dwc3_gadget_move_cancelled_request(req);
1406
1407 /* If ep isn't started, then there's no end transfer pending */
1408 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1409 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1410
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 return ret;
1412 }
1413
Thinh Nguyene0d19562020-05-05 19:46:57 -07001414 if (dep->stream_capable && req->request.is_last)
1415 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1416
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 return 0;
1418}
1419
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001420static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1421{
1422 u32 reg;
1423
1424 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1425 return DWC3_DSTS_SOFFN(reg);
1426}
1427
Thinh Nguyend92021f2018-11-14 22:56:54 -08001428/**
1429 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1430 * @dep: isoc endpoint
1431 *
1432 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1433 * microframe number reported by the XferNotReady event for the future frame
1434 * number to start the isoc transfer.
1435 *
1436 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1437 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1438 * XferNotReady event are invalid. The driver uses this number to schedule the
1439 * isochronous transfer and passes it to the START TRANSFER command. Because
1440 * this number is invalid, the command may fail. If BIT[15:14] matches the
1441 * internal 16-bit microframe, the START TRANSFER command will pass and the
1442 * transfer will start at the scheduled time, if it is off by 1, the command
1443 * will still pass, but the transfer will start 2 seconds in the future. For all
1444 * other conditions, the START TRANSFER command will fail with bus-expiry.
1445 *
1446 * In order to workaround this issue, we can test for the correct combination of
1447 * BIT[15:14] by sending START TRANSFER commands with different values of
1448 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1449 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1450 * As the result, within the 4 possible combinations for BIT[15:14], there will
1451 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1452 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1453 * value is the correct combination.
1454 *
1455 * Since there are only 4 outcomes and the results are ordered, we can simply
1456 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1457 * deduce the smaller successful combination.
1458 *
1459 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1460 * of BIT[15:14]. The correct combination is as follow:
1461 *
1462 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1463 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1464 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1465 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1466 *
1467 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1468 * endpoints.
1469 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001470static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301471{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001472 int cmd_status = 0;
1473 bool test0;
1474 bool test1;
1475
1476 while (dep->combo_num < 2) {
1477 struct dwc3_gadget_ep_cmd_params params;
1478 u32 test_frame_number;
1479 u32 cmd;
1480
1481 /*
1482 * Check if we can start isoc transfer on the next interval or
1483 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1484 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001485 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001486 test_frame_number |= dep->combo_num << 14;
1487 test_frame_number += max_t(u32, 4, dep->interval);
1488
1489 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1490 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1491
1492 cmd = DWC3_DEPCMD_STARTTRANSFER;
1493 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1494 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1495
1496 /* Redo if some other failure beside bus-expiry is received */
1497 if (cmd_status && cmd_status != -EAGAIN) {
1498 dep->start_cmd_status = 0;
1499 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001500 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001501 }
1502
1503 /* Store the first test status */
1504 if (dep->combo_num == 0)
1505 dep->start_cmd_status = cmd_status;
1506
1507 dep->combo_num++;
1508
1509 /*
1510 * End the transfer if the START_TRANSFER command is successful
1511 * to wait for the next XferNotReady to test the command again
1512 */
1513 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001514 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001515 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001516 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301517 }
1518
Thinh Nguyend92021f2018-11-14 22:56:54 -08001519 /* test0 and test1 are both completed at this point */
1520 test0 = (dep->start_cmd_status == 0);
1521 test1 = (cmd_status == 0);
1522
1523 if (!test0 && test1)
1524 dep->combo_num = 1;
1525 else if (!test0 && !test1)
1526 dep->combo_num = 2;
1527 else if (test0 && !test1)
1528 dep->combo_num = 3;
1529 else if (test0 && test1)
1530 dep->combo_num = 0;
1531
Michael Grzeschikca143782020-07-01 20:24:51 +02001532 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001533 dep->frame_number |= dep->combo_num << 14;
1534 dep->frame_number += max_t(u32, 4, dep->interval);
1535
1536 /* Reinitialize test variables */
1537 dep->start_cmd_status = 0;
1538 dep->combo_num = 0;
1539
Felipe Balbi25abad62018-08-14 10:41:19 +03001540 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001541}
1542
Felipe Balbi25abad62018-08-14 10:41:19 +03001543static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301544{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001545 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001546 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001547 int ret;
1548 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001549
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001550 if (list_empty(&dep->pending_list) &&
1551 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301552 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001553 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301554 }
1555
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001556 if (!dwc->dis_start_transfer_quirk &&
1557 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1558 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001559 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001560 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001561 }
1562
Michael Olbrichc5a70922020-07-01 20:24:52 +02001563 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001564 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001565 u32 frame = __dwc3_gadget_get_frame(dwc);
1566 bool rollover = frame <
1567 (dep->frame_number & DWC3_FRNUMBER_MASK);
1568
1569 /*
1570 * frame_number is set from XferNotReady and may be already
1571 * out of date. DSTS only provides the lower 14 bit of the
1572 * current frame number. So add the upper two bits of
1573 * frame_number and handle a possible rollover.
1574 * This will provide the correct frame_number unless more than
1575 * rollover has happened since XferNotReady.
1576 */
1577
1578 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1579 frame;
1580 if (rollover)
1581 dep->frame_number += BIT(14);
1582 }
1583
Felipe Balbid5370102018-08-14 10:42:43 +03001584 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1585 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1586
1587 ret = __dwc3_gadget_kick_transfer(dep);
1588 if (ret != -EAGAIN)
1589 break;
1590 }
1591
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001592 /*
1593 * After a number of unsuccessful start attempts due to bus-expiry
1594 * status, issue END_TRANSFER command and retry on the next XferNotReady
1595 * event.
1596 */
1597 if (ret == -EAGAIN) {
1598 struct dwc3_gadget_ep_cmd_params params;
1599 u32 cmd;
1600
1601 cmd = DWC3_DEPCMD_ENDTRANSFER |
1602 DWC3_DEPCMD_CMDIOC |
1603 DWC3_DEPCMD_PARAM(dep->resource_index);
1604
1605 dep->resource_index = 0;
1606 memset(&params, 0, sizeof(params));
1607
1608 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1609 if (!ret)
1610 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1611 }
1612
Felipe Balbid5370102018-08-14 10:42:43 +03001613 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301614}
1615
Felipe Balbi72246da2011-08-19 18:10:58 +03001616static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1617{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001618 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001619
Wesley Chengc7bb96a2021-03-11 15:59:02 -08001620 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001621 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1622 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001623 return -ESHUTDOWN;
1624 }
1625
Felipe Balbi04fb3652017-05-17 15:57:45 +03001626 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1627 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001628 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001629
Felipe Balbib2b6d602019-01-11 12:58:52 +02001630 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1631 "%s: request %pK already in flight\n",
1632 dep->name, &req->request))
1633 return -EINVAL;
1634
Felipe Balbifc8bb912016-05-16 13:14:48 +03001635 pm_runtime_get(dwc->dev);
1636
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 req->request.actual = 0;
1638 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001639
Felipe Balbife84f522015-09-01 09:01:38 -05001640 trace_dwc3_ep_queue(req);
1641
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001642 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001643 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001644
Thinh Nguyene0d19562020-05-05 19:46:57 -07001645 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1646 return 0;
1647
Thinh Nguyenc5036722020-09-02 18:42:58 -07001648 /*
1649 * Start the transfer only after the END_TRANSFER is completed
1650 * and endpoint STALL is cleared.
1651 */
1652 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1653 (dep->flags & DWC3_EP_WEDGE) ||
1654 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001655 dep->flags |= DWC3_EP_DELAY_START;
1656 return 0;
1657 }
1658
Felipe Balbid889c232016-09-29 15:44:29 +03001659 /*
1660 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1661 * wait for a XferNotReady event so we will know what's the current
1662 * (micro-)frame number.
1663 *
1664 * Without this trick, we are very, very likely gonna get Bus Expiry
1665 * errors which will force us issue EndTransfer command.
1666 */
1667 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001668 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1669 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001670 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001671
1672 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001673 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001674 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001675 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001676 }
1677
Felipe Balbi7fdca762017-09-05 14:41:34 +03001678 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001679}
1680
1681static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1682 gfp_t gfp_flags)
1683{
1684 struct dwc3_request *req = to_dwc3_request(request);
1685 struct dwc3_ep *dep = to_dwc3_ep(ep);
1686 struct dwc3 *dwc = dep->dwc;
1687
1688 unsigned long flags;
1689
1690 int ret;
1691
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001692 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 ret = __dwc3_gadget_ep_queue(dep, req);
1694 spin_unlock_irqrestore(&dwc->lock, flags);
1695
1696 return ret;
1697}
1698
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001699static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1700{
1701 int i;
1702
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001703 /* If req->trb is not set, then the request has not started */
1704 if (!req->trb)
1705 return;
1706
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001707 /*
1708 * If request was already started, this means we had to
1709 * stop the transfer. With that we also need to ignore
1710 * all TRBs used by the request, however TRBs can only
1711 * be modified after completion of END_TRANSFER
1712 * command. So what we do here is that we wait for
1713 * END_TRANSFER completion and only after that, we jump
1714 * over TRBs by clearing HWO and incrementing dequeue
1715 * pointer.
1716 */
1717 for (i = 0; i < req->num_trbs; i++) {
1718 struct dwc3_trb *trb;
1719
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001720 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001721 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1722 dwc3_ep_inc_deq(dep);
1723 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001724
1725 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001726}
1727
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001728static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1729{
1730 struct dwc3_request *req;
1731 struct dwc3_request *tmp;
1732
1733 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1734 dwc3_gadget_ep_skip_trbs(dep, req);
1735 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1736 }
1737}
1738
Felipe Balbi72246da2011-08-19 18:10:58 +03001739static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1740 struct usb_request *request)
1741{
1742 struct dwc3_request *req = to_dwc3_request(request);
1743 struct dwc3_request *r = NULL;
1744
1745 struct dwc3_ep *dep = to_dwc3_ep(ep);
1746 struct dwc3 *dwc = dep->dwc;
1747
1748 unsigned long flags;
1749 int ret = 0;
1750
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001751 trace_dwc3_ep_dequeue(req);
1752
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 spin_lock_irqsave(&dwc->lock, flags);
1754
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001755 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001756 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001757 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 }
1759
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001761 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001762 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1763 goto out;
1764 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 }
1766
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001767 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001769 struct dwc3_request *t;
1770
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001772 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001773
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001774 /*
1775 * Remove any started request if the transfer is
1776 * cancelled.
1777 */
1778 list_for_each_entry_safe(r, t, &dep->started_list, list)
1779 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001780
Thinh Nguyen8907a102021-01-04 22:42:39 -08001781 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
1782
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001783 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001784 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 }
1786
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001787 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1788 request, ep->name);
1789 ret = -EINVAL;
1790out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001791 spin_unlock_irqrestore(&dwc->lock, flags);
1792
1793 return ret;
1794}
1795
Felipe Balbi7a608552014-09-24 14:19:52 -05001796int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001797{
1798 struct dwc3_gadget_ep_cmd_params params;
1799 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001800 struct dwc3_request *req;
1801 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 int ret;
1803
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001804 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1805 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1806 return -EINVAL;
1807 }
1808
Felipe Balbi72246da2011-08-19 18:10:58 +03001809 memset(&params, 0x00, sizeof(params));
1810
1811 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001812 struct dwc3_trb *trb;
1813
Felipe Balbie319bd62020-08-13 08:35:38 +03001814 unsigned int transfer_in_flight;
1815 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001816
1817 if (dep->number > 1)
1818 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1819 else
1820 trb = &dwc->ep0_trb[dep->trb_enqueue];
1821
1822 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1823 started = !list_empty(&dep->started_list);
1824
1825 if (!protocol && ((dep->direction && transfer_in_flight) ||
1826 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001827 return -EAGAIN;
1828 }
1829
Felipe Balbi2cd47182016-04-12 16:42:43 +03001830 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1831 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001833 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001834 dep->name);
1835 else
1836 dep->flags |= DWC3_EP_STALL;
1837 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001838 /*
1839 * Don't issue CLEAR_STALL command to control endpoints. The
1840 * controller automatically clears the STALL when it receives
1841 * the SETUP token.
1842 */
1843 if (dep->number <= 1) {
1844 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1845 return 0;
1846 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001847
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001848 dwc3_stop_active_transfer(dep, true, true);
1849
1850 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1851 dwc3_gadget_move_cancelled_request(req);
1852
1853 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1854 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1855 return 0;
1856 }
1857
1858 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1859
John Youn50c763f2016-05-31 17:49:56 -07001860 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001861 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001862 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001864 return ret;
1865 }
1866
1867 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1868
Thinh Nguyenc5036722020-09-02 18:42:58 -07001869 if ((dep->flags & DWC3_EP_DELAY_START) &&
1870 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1871 __dwc3_gadget_kick_transfer(dep);
1872
1873 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001874 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001875
Felipe Balbi72246da2011-08-19 18:10:58 +03001876 return ret;
1877}
1878
1879static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1880{
1881 struct dwc3_ep *dep = to_dwc3_ep(ep);
1882 struct dwc3 *dwc = dep->dwc;
1883
1884 unsigned long flags;
1885
1886 int ret;
1887
1888 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001889 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 spin_unlock_irqrestore(&dwc->lock, flags);
1891
1892 return ret;
1893}
1894
1895static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1896{
1897 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001898 struct dwc3 *dwc = dep->dwc;
1899 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001900 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001901
Paul Zimmerman249a4562012-02-24 17:32:16 -08001902 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001903 dep->flags |= DWC3_EP_WEDGE;
1904
Pratyush Anand08f0d962012-06-25 22:40:43 +05301905 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001906 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301907 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001908 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001909 spin_unlock_irqrestore(&dwc->lock, flags);
1910
1911 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001912}
1913
1914/* -------------------------------------------------------------------------- */
1915
1916static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1917 .bLength = USB_DT_ENDPOINT_SIZE,
1918 .bDescriptorType = USB_DT_ENDPOINT,
1919 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1920};
1921
1922static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1923 .enable = dwc3_gadget_ep0_enable,
1924 .disable = dwc3_gadget_ep0_disable,
1925 .alloc_request = dwc3_gadget_ep_alloc_request,
1926 .free_request = dwc3_gadget_ep_free_request,
1927 .queue = dwc3_gadget_ep0_queue,
1928 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301929 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001930 .set_wedge = dwc3_gadget_ep_set_wedge,
1931};
1932
1933static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1934 .enable = dwc3_gadget_ep_enable,
1935 .disable = dwc3_gadget_ep_disable,
1936 .alloc_request = dwc3_gadget_ep_alloc_request,
1937 .free_request = dwc3_gadget_ep_free_request,
1938 .queue = dwc3_gadget_ep_queue,
1939 .dequeue = dwc3_gadget_ep_dequeue,
1940 .set_halt = dwc3_gadget_ep_set_halt,
1941 .set_wedge = dwc3_gadget_ep_set_wedge,
1942};
1943
1944/* -------------------------------------------------------------------------- */
1945
1946static int dwc3_gadget_get_frame(struct usb_gadget *g)
1947{
1948 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001949
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001950 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001951}
1952
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001953static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001954{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001955 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001956
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001957 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001958 u32 reg;
1959
Felipe Balbi72246da2011-08-19 18:10:58 +03001960 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001961
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 /*
1963 * According to the Databook Remote wakeup request should
1964 * be issued only when the device is in early suspend state.
1965 *
1966 * We can check that via USB Link State bits in DSTS register.
1967 */
1968 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1969
Felipe Balbi72246da2011-08-19 18:10:58 +03001970 link_state = DWC3_DSTS_USBLNKST(reg);
1971
1972 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001973 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001974 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1975 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001976 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001977 break;
1978 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001979 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001980 }
1981
Felipe Balbi8598bde2012-01-02 18:55:57 +02001982 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1983 if (ret < 0) {
1984 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001985 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001986 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001987
Paul Zimmerman802fde92012-04-27 13:10:52 +03001988 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001989 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001990 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001991 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001992 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1993 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1994 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001995
Paul Zimmerman1d046792012-02-15 18:56:56 -08001996 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001997 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001999 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002000 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2001
2002 /* in HS, means ON */
2003 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2004 break;
2005 }
2006
2007 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2008 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002009 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002010 }
2011
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002012 return 0;
2013}
2014
2015static int dwc3_gadget_wakeup(struct usb_gadget *g)
2016{
2017 struct dwc3 *dwc = gadget_to_dwc(g);
2018 unsigned long flags;
2019 int ret;
2020
2021 spin_lock_irqsave(&dwc->lock, flags);
2022 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002023 spin_unlock_irqrestore(&dwc->lock, flags);
2024
2025 return ret;
2026}
2027
2028static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2029 int is_selfpowered)
2030{
2031 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002032 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002033
Paul Zimmerman249a4562012-02-24 17:32:16 -08002034 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002035 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002036 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002037
2038 return 0;
2039}
2040
Wesley Chengae7e8612020-09-28 17:20:59 -07002041static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2042{
2043 u32 epnum;
2044
2045 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2046 struct dwc3_ep *dep;
2047
2048 dep = dwc->eps[epnum];
2049 if (!dep)
2050 continue;
2051
2052 dwc3_remove_requests(dwc, dep);
2053 }
2054}
2055
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002056static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002057{
2058 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002059 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002060
Felipe Balbifc8bb912016-05-16 13:14:48 +03002061 if (pm_runtime_suspended(dwc->dev))
2062 return 0;
2063
Felipe Balbi72246da2011-08-19 18:10:58 +03002064 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002065 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002066 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002067 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2068 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2069 }
2070
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002071 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002072 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2073 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002074
2075 if (dwc->has_hibernation)
2076 reg |= DWC3_DCTL_KEEP_CONNECT;
2077
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002078 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002079 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002080 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002081
2082 if (dwc->has_hibernation && !suspend)
2083 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2084
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002085 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002086 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002087
Thinh Nguyen5b738212019-10-23 19:15:43 -07002088 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
2090 do {
2091 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002092 reg &= DWC3_DSTS_DEVCTRLHLT;
2093 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002094
2095 if (!timeout)
2096 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002097
Pratyush Anand6f17f742012-07-02 10:21:55 +05302098 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002099}
2100
Wesley Chengae7e8612020-09-28 17:20:59 -07002101static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2102static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chengdd8363f2020-12-29 15:00:37 -08002103static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002104
Felipe Balbi72246da2011-08-19 18:10:58 +03002105static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2106{
2107 struct dwc3 *dwc = gadget_to_dwc(g);
2108 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302109 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002110
2111 is_on = !!is_on;
2112
Baolin Wangbb014732016-10-14 17:11:33 +08002113 /*
2114 * Per databook, when we want to stop the gadget, if a control transfer
2115 * is still in process, complete it and get the core into setup phase.
2116 */
2117 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2118 reinit_completion(&dwc->ep0_in_setup);
2119
2120 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2121 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2122 if (ret == 0) {
2123 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2124 return -ETIMEDOUT;
2125 }
2126 }
2127
Wesley Chengae7e8612020-09-28 17:20:59 -07002128 /*
Wesley Cheng395d2732020-12-29 15:05:35 -08002129 * Check the return value for successful resume, or error. For a
2130 * successful resume, the DWC3 runtime PM resume routine will handle
2131 * the run stop sequence, so avoid duplicate operations here.
2132 */
2133 ret = pm_runtime_get_sync(dwc->dev);
2134 if (!ret || ret < 0) {
2135 pm_runtime_put(dwc->dev);
2136 return 0;
2137 }
2138
2139 /*
Wesley Chengae7e8612020-09-28 17:20:59 -07002140 * Synchronize any pending event handling before executing the controller
2141 * halt routine.
2142 */
2143 if (!is_on) {
2144 dwc3_gadget_disable_irq(dwc);
2145 synchronize_irq(dwc->irq_gadget);
2146 }
2147
Felipe Balbi72246da2011-08-19 18:10:58 +03002148 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002149
2150 if (!is_on) {
2151 u32 count;
2152
Wesley Chengc7bb96a2021-03-11 15:59:02 -08002153 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002154 /*
2155 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2156 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2157 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2158 * command for any active transfers" before clearing the RunStop
2159 * bit.
2160 */
2161 dwc3_stop_active_transfers(dwc);
2162 __dwc3_gadget_stop(dwc);
2163
2164 /*
2165 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2166 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2167 * "software needs to acknowledge the events that are generated
2168 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2169 * to be set to '1'."
2170 */
2171 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2172 count &= DWC3_GEVNTCOUNT_MASK;
2173 if (count > 0) {
2174 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2175 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2176 dwc->ev_buf->length;
2177 }
Wesley Chengdd8363f2020-12-29 15:00:37 -08002178 } else {
2179 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002180 }
2181
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002182 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002183 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng395d2732020-12-29 15:05:35 -08002184 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002185
Pratyush Anand6f17f742012-07-02 10:21:55 +05302186 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002187}
2188
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002189static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2190{
2191 u32 reg;
2192
2193 /* Enable all but Start and End of Frame IRQs */
2194 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2195 DWC3_DEVTEN_EVNTOVERFLOWEN |
2196 DWC3_DEVTEN_CMDCMPLTEN |
2197 DWC3_DEVTEN_ERRTICERREN |
2198 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002199 DWC3_DEVTEN_CONNECTDONEEN |
2200 DWC3_DEVTEN_USBRSTEN |
2201 DWC3_DEVTEN_DISCONNEVTEN);
2202
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002203 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002204 reg |= DWC3_DEVTEN_ULSTCNGEN;
2205
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002206 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2207}
2208
2209static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2210{
2211 /* mask all interrupts */
2212 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2213}
2214
2215static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002216static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002217
Felipe Balbi4e994722016-05-13 14:09:59 +03002218/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002219 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2220 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002221 *
2222 * The following looks like complex but it's actually very simple. In order to
2223 * calculate the number of packets we can burst at once on OUT transfers, we're
2224 * gonna use RxFIFO size.
2225 *
2226 * To calculate RxFIFO size we need two numbers:
2227 * MDWIDTH = size, in bits, of the internal memory bus
2228 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2229 *
2230 * Given these two numbers, the formula is simple:
2231 *
2232 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2233 *
2234 * 24 bytes is for 3x SETUP packets
2235 * 16 bytes is a clock domain crossing tolerance
2236 *
2237 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2238 */
2239static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2240{
2241 u32 ram2_depth;
2242 u32 mdwidth;
2243 u32 nump;
2244 u32 reg;
2245
2246 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2247 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002248 if (DWC3_IP_IS(DWC32))
2249 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002250
2251 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2252 nump = min_t(u32, nump, 16);
2253
2254 /* update NumP */
2255 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2256 reg &= ~DWC3_DCFG_NUMP_MASK;
2257 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2258 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2259}
2260
Felipe Balbid7be2952016-05-04 15:49:37 +03002261static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002262{
Felipe Balbi72246da2011-08-19 18:10:58 +03002263 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002264 int ret = 0;
2265 u32 reg;
2266
John Youncf40b862016-11-14 12:32:43 -08002267 /*
2268 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2269 * the core supports IMOD, disable it.
2270 */
2271 if (dwc->imod_interval) {
2272 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2273 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2274 } else if (dwc3_has_imod(dwc)) {
2275 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2276 }
2277
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002278 /*
2279 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2280 * field instead of letting dwc3 itself calculate that automatically.
2281 *
2282 * This way, we maximize the chances that we'll be able to get several
2283 * bursts of data without going through any sort of endpoint throttling.
2284 */
2285 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002286 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002287 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002288 else
2289 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002290
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002291 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2292
Felipe Balbi4e994722016-05-13 14:09:59 +03002293 dwc3_gadget_setup_nump(dwc);
2294
Felipe Balbi72246da2011-08-19 18:10:58 +03002295 /* Start with SuperSpeed Default */
2296 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2297
2298 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002299 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002300 if (ret) {
2301 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002302 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002303 }
2304
2305 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002306 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002307 if (ret) {
2308 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002309 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002310 }
2311
2312 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002313 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002314 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002315 dwc3_ep0_out_start(dwc);
2316
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002317 dwc3_gadget_enable_irq(dwc);
2318
Felipe Balbid7be2952016-05-04 15:49:37 +03002319 return 0;
2320
2321err1:
2322 __dwc3_gadget_ep_disable(dwc->eps[0]);
2323
2324err0:
2325 return ret;
2326}
2327
2328static int dwc3_gadget_start(struct usb_gadget *g,
2329 struct usb_gadget_driver *driver)
2330{
2331 struct dwc3 *dwc = gadget_to_dwc(g);
2332 unsigned long flags;
2333 int ret = 0;
2334 int irq;
2335
Roger Quadros9522def2016-06-10 14:48:38 +03002336 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002337 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2338 IRQF_SHARED, "dwc3", dwc->ev_buf);
2339 if (ret) {
2340 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2341 irq, ret);
2342 goto err0;
2343 }
2344
2345 spin_lock_irqsave(&dwc->lock, flags);
2346 if (dwc->gadget_driver) {
2347 dev_err(dwc->dev, "%s is already bound to %s\n",
Peter Chene81a7012020-08-21 10:55:48 +08002348 dwc->gadget->name,
Felipe Balbid7be2952016-05-04 15:49:37 +03002349 dwc->gadget_driver->driver.name);
2350 ret = -EBUSY;
2351 goto err1;
2352 }
2353
2354 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002355 spin_unlock_irqrestore(&dwc->lock, flags);
2356
2357 return 0;
2358
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002359err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002360 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002361 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002362
2363err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002364 return ret;
2365}
2366
Felipe Balbid7be2952016-05-04 15:49:37 +03002367static void __dwc3_gadget_stop(struct dwc3 *dwc)
2368{
2369 dwc3_gadget_disable_irq(dwc);
2370 __dwc3_gadget_ep_disable(dwc->eps[0]);
2371 __dwc3_gadget_ep_disable(dwc->eps[1]);
2372}
2373
Felipe Balbi22835b82014-10-17 12:05:12 -05002374static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002375{
2376 struct dwc3 *dwc = gadget_to_dwc(g);
2377 unsigned long flags;
2378
2379 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002380 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002381 spin_unlock_irqrestore(&dwc->lock, flags);
2382
Felipe Balbi3f308d12016-05-16 14:17:06 +03002383 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002384
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 return 0;
2386}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002387
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302388static void dwc3_gadget_config_params(struct usb_gadget *g,
2389 struct usb_dcd_config_params *params)
2390{
2391 struct dwc3 *dwc = gadget_to_dwc(g);
2392
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002393 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2394 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2395
2396 /* Recommended BESL */
2397 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002398 /*
2399 * If the recommended BESL baseline is 0 or if the BESL deep is
2400 * less than 2, Microsoft's Windows 10 host usb stack will issue
2401 * a usb reset immediately after it receives the extended BOS
2402 * descriptor and the enumeration will fail. To maintain
2403 * compatibility with the Windows' usb stack, let's set the
2404 * recommended BESL baseline to 1 and clamp the BESL deep to be
2405 * within 2 to 15.
2406 */
2407 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002408 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002409 params->besl_deep =
2410 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002411 }
2412
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302413 /* U1 Device exit Latency */
2414 if (dwc->dis_u1_entry_quirk)
2415 params->bU1devExitLat = 0;
2416 else
2417 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2418
2419 /* U2 Device exit Latency */
2420 if (dwc->dis_u2_entry_quirk)
2421 params->bU2DevExitLat = 0;
2422 else
2423 params->bU2DevExitLat =
2424 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2425}
2426
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002427static void dwc3_gadget_set_speed(struct usb_gadget *g,
2428 enum usb_device_speed speed)
2429{
2430 struct dwc3 *dwc = gadget_to_dwc(g);
2431 unsigned long flags;
2432 u32 reg;
2433
2434 spin_lock_irqsave(&dwc->lock, flags);
2435 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2436 reg &= ~(DWC3_DCFG_SPEED_MASK);
2437
2438 /*
2439 * WORKAROUND: DWC3 revision < 2.20a have an issue
2440 * which would cause metastability state on Run/Stop
2441 * bit if we try to force the IP to USB2-only mode.
2442 *
2443 * Because of that, we cannot configure the IP to any
2444 * speed other than the SuperSpeed
2445 *
2446 * Refers to:
2447 *
2448 * STAR#9000525659: Clock Domain Crossing on DCTL in
2449 * USB 2.0 Mode
2450 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002451 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02002452 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002453 reg |= DWC3_DCFG_SUPERSPEED;
2454 } else {
2455 switch (speed) {
2456 case USB_SPEED_LOW:
2457 reg |= DWC3_DCFG_LOWSPEED;
2458 break;
2459 case USB_SPEED_FULL:
2460 reg |= DWC3_DCFG_FULLSPEED;
2461 break;
2462 case USB_SPEED_HIGH:
2463 reg |= DWC3_DCFG_HIGHSPEED;
2464 break;
2465 case USB_SPEED_SUPER:
2466 reg |= DWC3_DCFG_SUPERSPEED;
2467 break;
2468 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002469 if (DWC3_IP_IS(DWC3))
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002470 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002471 else
2472 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002473 break;
2474 default:
2475 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2476
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002477 if (DWC3_IP_IS(DWC3))
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002478 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002479 else
2480 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002481 }
2482 }
2483 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2484
2485 spin_unlock_irqrestore(&dwc->lock, flags);
2486}
2487
Felipe Balbi72246da2011-08-19 18:10:58 +03002488static const struct usb_gadget_ops dwc3_gadget_ops = {
2489 .get_frame = dwc3_gadget_get_frame,
2490 .wakeup = dwc3_gadget_wakeup,
2491 .set_selfpowered = dwc3_gadget_set_selfpowered,
2492 .pullup = dwc3_gadget_pullup,
2493 .udc_start = dwc3_gadget_start,
2494 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002495 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302496 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002497};
2498
2499/* -------------------------------------------------------------------------- */
2500
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002501static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2502{
2503 struct dwc3 *dwc = dep->dwc;
2504
2505 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2506 dep->endpoint.maxburst = 1;
2507 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2508 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002509 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002510
2511 dep->endpoint.caps.type_control = true;
2512
2513 return 0;
2514}
2515
2516static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2517{
2518 struct dwc3 *dwc = dep->dwc;
2519 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002520 int size;
2521
2522 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002523 if (DWC3_IP_IS(DWC32))
2524 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2525
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002526 /* MDWIDTH is represented in bits, we need it in bytes */
2527 mdwidth /= 8;
2528
2529 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002530 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002531 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002532 else
2533 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002534
2535 /* FIFO Depth is in MDWDITH bytes. Multiply */
2536 size *= mdwidth;
2537
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002538 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002539 * To meet performance requirement, a minimum TxFIFO size of 3x
2540 * MaxPacketSize is recommended for endpoints that support burst and a
2541 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2542 * support burst. Use those numbers and we can calculate the max packet
2543 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002544 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002545 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2546 size /= 3;
2547 else
2548 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002549
2550 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2551
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002552 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002553 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2554 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002555 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002556 dep->endpoint.caps.type_iso = true;
2557 dep->endpoint.caps.type_bulk = true;
2558 dep->endpoint.caps.type_int = true;
2559
2560 return dwc3_alloc_trb_pool(dep);
2561}
2562
2563static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2564{
2565 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002566 int mdwidth;
2567 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002568
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002569 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002570 if (DWC3_IP_IS(DWC32))
2571 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002572
2573 /* MDWIDTH is represented in bits, convert to bytes */
2574 mdwidth /= 8;
2575
2576 /* All OUT endpoints share a single RxFIFO space */
2577 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002578 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002579 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002580 else
2581 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002582
2583 /* FIFO depth is in MDWDITH bytes */
2584 size *= mdwidth;
2585
2586 /*
2587 * To meet performance requirement, a minimum recommended RxFIFO size
2588 * is defined as follow:
2589 * RxFIFO size >= (3 x MaxPacketSize) +
2590 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2591 *
2592 * Then calculate the max packet limit as below.
2593 */
2594 size -= (3 * 8) + 16;
2595 if (size < 0)
2596 size = 0;
2597 else
2598 size /= 3;
2599
2600 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002601 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002602 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2603 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002604 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002605 dep->endpoint.caps.type_iso = true;
2606 dep->endpoint.caps.type_bulk = true;
2607 dep->endpoint.caps.type_int = true;
2608
2609 return dwc3_alloc_trb_pool(dep);
2610}
2611
2612static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002613{
2614 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002615 bool direction = epnum & 1;
2616 int ret;
2617 u8 num = epnum >> 1;
2618
2619 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2620 if (!dep)
2621 return -ENOMEM;
2622
2623 dep->dwc = dwc;
2624 dep->number = epnum;
2625 dep->direction = direction;
2626 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2627 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002628 dep->combo_num = 0;
2629 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002630
2631 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2632 direction ? "in" : "out");
2633
2634 dep->endpoint.name = dep->name;
2635
2636 if (!(dep->number > 1)) {
2637 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2638 dep->endpoint.comp_desc = NULL;
2639 }
2640
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002641 if (num == 0)
2642 ret = dwc3_gadget_init_control_endpoint(dep);
2643 else if (direction)
2644 ret = dwc3_gadget_init_in_endpoint(dep);
2645 else
2646 ret = dwc3_gadget_init_out_endpoint(dep);
2647
2648 if (ret)
2649 return ret;
2650
2651 dep->endpoint.caps.dir_in = direction;
2652 dep->endpoint.caps.dir_out = !direction;
2653
2654 INIT_LIST_HEAD(&dep->pending_list);
2655 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002656 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002657
2658 return 0;
2659}
2660
2661static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2662{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002663 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002664
Peter Chene81a7012020-08-21 10:55:48 +08002665 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002666
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002667 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002668 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002669
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002670 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2671 if (ret)
2672 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002673 }
2674
2675 return 0;
2676}
2677
2678static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2679{
2680 struct dwc3_ep *dep;
2681 u8 epnum;
2682
2683 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2684 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002685 if (!dep)
2686 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302687 /*
2688 * Physical endpoints 0 and 1 are special; they form the
2689 * bi-directional USB endpoint 0.
2690 *
2691 * For those two physical endpoints, we don't allocate a TRB
2692 * pool nor do we add them the endpoints list. Due to that, we
2693 * shouldn't do these two operations otherwise we would end up
2694 * with all sorts of bugs when removing dwc3.ko.
2695 */
2696 if (epnum != 0 && epnum != 1) {
2697 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002698 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302699 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002700
2701 kfree(dep);
2702 }
2703}
2704
Felipe Balbi72246da2011-08-19 18:10:58 +03002705/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002706
Felipe Balbi8f608e82018-03-27 10:53:29 +03002707static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2708 struct dwc3_request *req, struct dwc3_trb *trb,
2709 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302710{
2711 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302712
Felipe Balbidc55c672016-08-12 13:20:32 +03002713 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002714
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002715 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002716 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002717
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002718 /*
2719 * If we're in the middle of series of chained TRBs and we
2720 * receive a short transfer along the way, DWC3 will skip
2721 * through all TRBs including the last TRB in the chain (the
2722 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2723 * bit and SW has to do it manually.
2724 *
2725 * We're going to do that here to avoid problems of HW trying
2726 * to use bogus TRBs for transfers.
2727 */
2728 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2729 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2730
Felipe Balbic6267a52017-01-05 14:58:46 +02002731 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002732 * For isochronous transfers, the first TRB in a service interval must
2733 * have the Isoc-First type. Track and report its interval frame number.
2734 */
2735 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2736 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2737 unsigned int frame_number;
2738
2739 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2740 frame_number &= ~(dep->interval - 1);
2741 req->request.frame_number = frame_number;
2742 }
2743
2744 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002745 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2746 * this TRB points to the bounce buffer address, it's a MPS alignment
2747 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002748 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002749 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2750 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002751 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2752 return 1;
2753 }
2754
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302755 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002756 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302757
Felipe Balbi35b27192017-03-08 13:56:37 +02002758 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2759 return 1;
2760
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002761 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302762 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002763
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002764 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2765 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302766 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002767
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302768 return 0;
2769}
2770
Felipe Balbid3692952018-03-29 13:32:10 +03002771static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2772 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2773 int status)
2774{
2775 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2776 struct scatterlist *sg = req->sg;
2777 struct scatterlist *s;
2778 unsigned int pending = req->num_pending_sgs;
2779 unsigned int i;
2780 int ret = 0;
2781
2782 for_each_sg(sg, s, pending, i) {
2783 trb = &dep->trb_pool[dep->trb_dequeue];
2784
Felipe Balbid3692952018-03-29 13:32:10 +03002785 req->sg = sg_next(s);
2786 req->num_pending_sgs--;
2787
2788 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2789 trb, event, status, true);
2790 if (ret)
2791 break;
2792 }
2793
2794 return ret;
2795}
2796
2797static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2798 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2799 int status)
2800{
2801 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2802
2803 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2804 event, status, false);
2805}
2806
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002807static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2808{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002809 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002810}
2811
Felipe Balbif38e35d2018-04-06 15:56:35 +03002812static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2813 const struct dwc3_event_depevt *event,
2814 struct dwc3_request *req, int status)
2815{
2816 int ret;
2817
2818 if (req->num_pending_sgs)
2819 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2820 status);
2821 else
2822 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2823 status);
2824
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002825 req->request.actual = req->request.length - req->remaining;
2826
2827 if (!dwc3_gadget_ep_request_completed(req))
2828 goto out;
2829
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002830 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002831 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2832 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002833 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002834 }
2835
Felipe Balbif38e35d2018-04-06 15:56:35 +03002836 dwc3_gadget_giveback(dep, req, status);
2837
2838out:
2839 return ret;
2840}
2841
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002842static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002843 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002844{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002845 struct dwc3_request *req;
2846 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002847
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002848 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002849 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002850
Felipe Balbif38e35d2018-04-06 15:56:35 +03002851 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2852 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002853 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002855 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002856}
2857
Thinh Nguyend9feef92020-03-31 01:40:42 -07002858static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2859{
2860 struct dwc3_request *req;
2861
2862 if (!list_empty(&dep->pending_list))
2863 return true;
2864
2865 /*
2866 * We only need to check the first entry of the started list. We can
2867 * assume the completed requests are removed from the started list.
2868 */
2869 req = next_request(&dep->started_list);
2870 if (!req)
2871 return false;
2872
2873 return !dwc3_gadget_ep_request_completed(req);
2874}
2875
Felipe Balbiee3638b2018-03-27 11:26:53 +03002876static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2877 const struct dwc3_event_depevt *event)
2878{
Felipe Balbif62afb42018-04-11 10:34:34 +03002879 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002880}
2881
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002882static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2883 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002884{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002885 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002886 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002887
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002888 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002889
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002890 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2891 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002892
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002893 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2894 list_empty(&dep->started_list) &&
2895 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002896 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002897 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002898 if (__dwc3_gadget_kick_transfer(dep) == 0)
2899 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002900
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002901out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002902 /*
2903 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2904 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2905 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002906 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002907 u32 reg;
2908 int i;
2909
2910 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002911 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002912
2913 if (!(dep->flags & DWC3_EP_ENABLED))
2914 continue;
2915
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002916 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002917 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002918 }
2919
2920 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2921 reg |= dwc->u1u2;
2922 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2923
2924 dwc->u1u2 = 0;
2925 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002926
2927 return no_started_trb;
2928}
2929
2930static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2931 const struct dwc3_event_depevt *event)
2932{
2933 int status = 0;
2934
2935 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2936 dwc3_gadget_endpoint_frame_from_event(dep, event);
2937
2938 if (event->status & DEPEVT_STATUS_BUSERR)
2939 status = -ECONNRESET;
2940
2941 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2942 status = -EXDEV;
2943
2944 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002945}
2946
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07002947static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
2948 const struct dwc3_event_depevt *event)
2949{
2950 int status = 0;
2951
2952 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2953
2954 if (event->status & DEPEVT_STATUS_BUSERR)
2955 status = -ECONNRESET;
2956
Thinh Nguyene0d19562020-05-05 19:46:57 -07002957 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
2958 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002959}
2960
Felipe Balbi8f608e82018-03-27 10:53:29 +03002961static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2962 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002963{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002964 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002965
2966 /*
2967 * The XferNotReady event is generated only once before the endpoint
2968 * starts. It will be generated again when END_TRANSFER command is
2969 * issued. For some controller versions, the XferNotReady event may be
2970 * generated while the END_TRANSFER command is still in process. Ignore
2971 * it and wait for the next XferNotReady event after the command is
2972 * completed.
2973 */
2974 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2975 return;
2976
Felipe Balbi25abad62018-08-14 10:41:19 +03002977 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002978}
2979
Thinh Nguyen8266b082020-07-30 16:29:03 -07002980static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
2981 const struct dwc3_event_depevt *event)
2982{
2983 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2984
2985 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
2986 return;
2987
2988 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2989 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2990 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2991
2992 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
2993 struct dwc3 *dwc = dep->dwc;
2994
2995 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
2996 if (dwc3_send_clear_stall_ep_cmd(dep)) {
2997 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
2998
2999 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3000 if (dwc->delayed_status)
3001 __dwc3_gadget_ep0_set_halt(ep0, 1);
3002 return;
3003 }
3004
3005 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3006 if (dwc->delayed_status)
3007 dwc3_ep0_send_delayed_status(dwc);
3008 }
3009
3010 if ((dep->flags & DWC3_EP_DELAY_START) &&
3011 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3012 __dwc3_gadget_kick_transfer(dep);
3013
3014 dep->flags &= ~DWC3_EP_DELAY_START;
3015}
3016
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003017static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3018 const struct dwc3_event_depevt *event)
3019{
3020 struct dwc3 *dwc = dep->dwc;
3021
3022 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3023 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3024 goto out;
3025 }
3026
3027 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3028 switch (event->parameters) {
3029 case DEPEVT_STREAM_PRIME:
3030 /*
3031 * If the host can properly transition the endpoint state from
3032 * idle to prime after a NoStream rejection, there's no need to
3033 * force restarting the endpoint to reinitiate the stream. To
3034 * simplify the check, assume the host follows the USB spec if
3035 * it primed the endpoint more than once.
3036 */
3037 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3038 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3039 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3040 else
3041 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3042 }
3043
3044 break;
3045 case DEPEVT_STREAM_NOSTREAM:
3046 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3047 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3048 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3049 break;
3050
3051 /*
3052 * If the host rejects a stream due to no active stream, by the
3053 * USB and xHCI spec, the endpoint will be put back to idle
3054 * state. When the host is ready (buffer added/updated), it will
3055 * prime the endpoint to inform the usb device controller. This
3056 * triggers the device controller to issue ERDY to restart the
3057 * stream. However, some hosts don't follow this and keep the
3058 * endpoint in the idle state. No prime will come despite host
3059 * streams are updated, and the device controller will not be
3060 * triggered to generate ERDY to move the next stream data. To
3061 * workaround this and maintain compatibility with various
3062 * hosts, force to reinitate the stream until the host is ready
3063 * instead of waiting for the host to prime the endpoint.
3064 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003065 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3066 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3067
3068 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3069 } else {
3070 dep->flags |= DWC3_EP_DELAY_START;
3071 dwc3_stop_active_transfer(dep, true, true);
3072 return;
3073 }
3074 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003075 }
3076
3077out:
3078 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3079}
3080
Felipe Balbi72246da2011-08-19 18:10:58 +03003081static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3082 const struct dwc3_event_depevt *event)
3083{
3084 struct dwc3_ep *dep;
3085 u8 epnum = event->endpoint_number;
3086
3087 dep = dwc->eps[epnum];
3088
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003089 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003090 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003091 return;
3092
3093 /* Handle only EPCMDCMPLT when EP disabled */
3094 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3095 return;
3096 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003097
Felipe Balbi72246da2011-08-19 18:10:58 +03003098 if (epnum == 0 || epnum == 1) {
3099 dwc3_ep0_interrupt(dwc, event);
3100 return;
3101 }
3102
3103 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003104 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003105 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003106 break;
3107 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003108 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003109 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003110 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003111 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003112 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003113 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003114 dwc3_gadget_endpoint_transfer_complete(dep, event);
3115 break;
3116 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003117 dwc3_gadget_endpoint_stream_event(dep, event);
3118 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003119 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003120 break;
3121 }
3122}
3123
3124static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3125{
3126 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3127 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003128 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003129 spin_lock(&dwc->lock);
3130 }
3131}
3132
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003133static void dwc3_suspend_gadget(struct dwc3 *dwc)
3134{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003135 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003136 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003137 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003138 spin_lock(&dwc->lock);
3139 }
3140}
3141
3142static void dwc3_resume_gadget(struct dwc3 *dwc)
3143{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003144 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003145 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003146 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003147 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003148 }
3149}
3150
3151static void dwc3_reset_gadget(struct dwc3 *dwc)
3152{
3153 if (!dwc->gadget_driver)
3154 return;
3155
Peter Chene81a7012020-08-21 10:55:48 +08003156 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003157 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003158 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003159 spin_lock(&dwc->lock);
3160 }
3161}
3162
Felipe Balbic5353b22019-02-13 13:00:54 +02003163static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3164 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003165{
Felipe Balbi72246da2011-08-19 18:10:58 +03003166 struct dwc3_gadget_ep_cmd_params params;
3167 u32 cmd;
3168 int ret;
3169
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003170 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3171 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303172 return;
3173
Pratyush Anand57911502012-07-06 15:19:10 +05303174 /*
3175 * NOTICE: We are violating what the Databook says about the
3176 * EndTransfer command. Ideally we would _always_ wait for the
3177 * EndTransfer Command Completion IRQ, but that's causing too
3178 * much trouble synchronizing between us and gadget driver.
3179 *
3180 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003181 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303182 *
3183 * Note also that a similar handling was tested by Synopsys
3184 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003185 * In short, what we're doing is issuing EndTransfer with
3186 * CMDIOC bit set and delay kicking transfer until the
3187 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003188 *
3189 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3190 * supports a mode to work around the above limitation. The
3191 * software can poll the CMDACT bit in the DEPCMD register
3192 * after issuing a EndTransfer command. This mode is enabled
3193 * by writing GUCTL2[14]. This polling is already done in the
3194 * dwc3_send_gadget_ep_cmd() function so if the mode is
3195 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003196 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003197 *
3198 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303199 */
3200
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303201 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003202 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003203 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003204 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303205 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003206 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303207 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003208 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003209
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003210 /*
3211 * The END_TRANSFER command will cause the controller to generate a
3212 * NoStream Event, and it's not due to the host DP NoStream rejection.
3213 * Ignore the next NoStream event.
3214 */
3215 if (dep->stream_capable)
3216 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3217
Thinh Nguyend3abda52019-11-27 13:10:47 -08003218 if (!interrupt)
3219 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003220 else
3221 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003222}
3223
Felipe Balbi72246da2011-08-19 18:10:58 +03003224static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3225{
3226 u32 epnum;
3227
3228 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3229 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003230 int ret;
3231
3232 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003233 if (!dep)
3234 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003235
3236 if (!(dep->flags & DWC3_EP_STALL))
3237 continue;
3238
3239 dep->flags &= ~DWC3_EP_STALL;
3240
John Youn50c763f2016-05-31 17:49:56 -07003241 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003242 WARN_ON_ONCE(ret);
3243 }
3244}
3245
3246static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3247{
Felipe Balbic4430a22012-05-24 10:30:01 +03003248 int reg;
3249
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003250 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3251
Felipe Balbi72246da2011-08-19 18:10:58 +03003252 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3253 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003254 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003255 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003256
Felipe Balbi72246da2011-08-19 18:10:58 +03003257 dwc3_disconnect_gadget(dwc);
3258
Peter Chene81a7012020-08-21 10:55:48 +08003259 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003260 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003261 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003262
3263 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003264}
3265
Felipe Balbi72246da2011-08-19 18:10:58 +03003266static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3267{
3268 u32 reg;
3269
Felipe Balbidf62df52011-10-14 15:11:49 +03003270 /*
3271 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3272 * would cause a missing Disconnect Event if there's a
3273 * pending Setup Packet in the FIFO.
3274 *
3275 * There's no suggested workaround on the official Bug
3276 * report, which states that "unless the driver/application
3277 * is doing any special handling of a disconnect event,
3278 * there is no functional issue".
3279 *
3280 * Unfortunately, it turns out that we _do_ some special
3281 * handling of a disconnect event, namely complete all
3282 * pending transfers, notify gadget driver of the
3283 * disconnection, and so on.
3284 *
3285 * Our suggested workaround is to follow the Disconnect
3286 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003287 * flag. Such flag gets set whenever we have a SETUP_PENDING
3288 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003289 * same endpoint.
3290 *
3291 * Refers to:
3292 *
3293 * STAR#9000466709: RTL: Device : Disconnect event not
3294 * generated if setup packet pending in FIFO
3295 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003296 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003297 if (dwc->setup_packet_pending)
3298 dwc3_gadget_disconnect_interrupt(dwc);
3299 }
3300
Felipe Balbi8e744752014-11-06 14:27:53 +08003301 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003302 /*
3303 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3304 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3305 * needs to ensure that it sends "a DEPENDXFER command for any active
3306 * transfers."
3307 */
3308 dwc3_stop_active_transfers(dwc);
Wesley Chengc7bb96a2021-03-11 15:59:02 -08003309 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003310
3311 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3312 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003313 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003314 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003315 dwc3_clear_stall_all_ep(dwc);
3316
3317 /* Reset device address to zero */
3318 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3319 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3320 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003321}
3322
Felipe Balbi72246da2011-08-19 18:10:58 +03003323static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3324{
Felipe Balbi72246da2011-08-19 18:10:58 +03003325 struct dwc3_ep *dep;
3326 int ret;
3327 u32 reg;
3328 u8 speed;
3329
Felipe Balbi72246da2011-08-19 18:10:58 +03003330 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3331 speed = reg & DWC3_DSTS_CONNECTSPD;
3332 dwc->speed = speed;
3333
John Youn5fb6fda2016-11-10 17:23:25 -08003334 /*
3335 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3336 * each time on Connect Done.
3337 *
3338 * Currently we always use the reset value. If any platform
3339 * wants to set this to a different value, we need to add a
3340 * setting and update GCTL.RAMCLKSEL here.
3341 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003342
3343 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003344 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003345 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_PLUS;
John Youn75808622016-02-05 17:09:13 -08003348 break;
John Youn2da9ad72016-05-20 16:34:26 -07003349 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003350 /*
3351 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3352 * would cause a missing USB3 Reset event.
3353 *
3354 * In such situations, we should force a USB3 Reset
3355 * event by calling our dwc3_gadget_reset_interrupt()
3356 * routine.
3357 *
3358 * Refers to:
3359 *
3360 * STAR#9000483510: RTL: SS : USB3 reset event may
3361 * not be generated always when the link enters poll
3362 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003363 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003364 dwc3_gadget_reset_interrupt(dwc);
3365
Felipe Balbi72246da2011-08-19 18:10:58 +03003366 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003367 dwc->gadget->ep0->maxpacket = 512;
3368 dwc->gadget->speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03003369 break;
John Youn2da9ad72016-05-20 16:34:26 -07003370 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003371 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003372 dwc->gadget->ep0->maxpacket = 64;
3373 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003374 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003375 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003376 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003377 dwc->gadget->ep0->maxpacket = 64;
3378 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003379 break;
John Youn2da9ad72016-05-20 16:34:26 -07003380 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003381 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003382 dwc->gadget->ep0->maxpacket = 8;
3383 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003384 break;
3385 }
3386
Peter Chene81a7012020-08-21 10:55:48 +08003387 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003388
Pratyush Anand2b758352013-01-14 15:59:31 +05303389 /* Enable USB2 LPM Capability */
3390
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003391 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003392 (speed != DWC3_DSTS_SUPERSPEED) &&
3393 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303394 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3395 reg |= DWC3_DCFG_LPM_CAP;
3396 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3397
3398 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3399 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3400
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003401 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3402 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303403
Huang Rui80caf7d2014-10-28 19:54:26 +08003404 /*
3405 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3406 * DCFG.LPMCap is set, core responses with an ACK and the
3407 * BESL value in the LPM token is less than or equal to LPM
3408 * NYET threshold.
3409 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003410 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003411 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003412
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003413 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003414 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003415
Thinh Nguyen5b738212019-10-23 19:15:43 -07003416 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003417 } else {
3418 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3419 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003420 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303421 }
3422
Felipe Balbi72246da2011-08-19 18:10:58 +03003423 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003424 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003425 if (ret) {
3426 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3427 return;
3428 }
3429
3430 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003431 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003432 if (ret) {
3433 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3434 return;
3435 }
3436
3437 /*
3438 * Configure PHY via GUSB3PIPECTLn if required.
3439 *
3440 * Update GTXFIFOSIZn
3441 *
3442 * In both cases reset values should be sufficient.
3443 */
3444}
3445
3446static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3447{
Felipe Balbi72246da2011-08-19 18:10:58 +03003448 /*
3449 * TODO take core out of low power mode when that's
3450 * implemented.
3451 */
3452
Jiebing Liad14d4e2014-12-11 13:26:29 +08003453 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3454 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003455 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003456 spin_lock(&dwc->lock);
3457 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003458}
3459
3460static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3461 unsigned int evtinfo)
3462{
Felipe Balbifae2b902011-10-14 13:00:30 +03003463 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003464 unsigned int pwropt;
3465
3466 /*
3467 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3468 * Hibernation mode enabled which would show up when device detects
3469 * host-initiated U3 exit.
3470 *
3471 * In that case, device will generate a Link State Change Interrupt
3472 * from U3 to RESUME which is only necessary if Hibernation is
3473 * configured in.
3474 *
3475 * There are no functional changes due to such spurious event and we
3476 * just need to ignore it.
3477 *
3478 * Refers to:
3479 *
3480 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3481 * operational mode
3482 */
3483 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003484 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003485 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3486 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3487 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003488 return;
3489 }
3490 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003491
3492 /*
3493 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3494 * on the link partner, the USB session might do multiple entry/exit
3495 * of low power states before a transfer takes place.
3496 *
3497 * Due to this problem, we might experience lower throughput. The
3498 * suggested workaround is to disable DCTL[12:9] bits if we're
3499 * transitioning from U1/U2 to U0 and enable those bits again
3500 * after a transfer completes and there are no pending transfers
3501 * on any of the enabled endpoints.
3502 *
3503 * This is the first half of that workaround.
3504 *
3505 * Refers to:
3506 *
3507 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3508 * core send LGO_Ux entering U0
3509 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003510 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003511 if (next == DWC3_LINK_STATE_U0) {
3512 u32 u1u2;
3513 u32 reg;
3514
3515 switch (dwc->link_state) {
3516 case DWC3_LINK_STATE_U1:
3517 case DWC3_LINK_STATE_U2:
3518 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3519 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3520 | DWC3_DCTL_ACCEPTU2ENA
3521 | DWC3_DCTL_INITU1ENA
3522 | DWC3_DCTL_ACCEPTU1ENA);
3523
3524 if (!dwc->u1u2)
3525 dwc->u1u2 = reg & u1u2;
3526
3527 reg &= ~u1u2;
3528
Thinh Nguyen5b738212019-10-23 19:15:43 -07003529 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003530 break;
3531 default:
3532 /* do nothing */
3533 break;
3534 }
3535 }
3536 }
3537
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003538 switch (next) {
3539 case DWC3_LINK_STATE_U1:
3540 if (dwc->speed == USB_SPEED_SUPER)
3541 dwc3_suspend_gadget(dwc);
3542 break;
3543 case DWC3_LINK_STATE_U2:
3544 case DWC3_LINK_STATE_U3:
3545 dwc3_suspend_gadget(dwc);
3546 break;
3547 case DWC3_LINK_STATE_RESUME:
3548 dwc3_resume_gadget(dwc);
3549 break;
3550 default:
3551 /* do nothing */
3552 break;
3553 }
3554
Felipe Balbie57ebc12014-04-22 13:20:12 -05003555 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003556}
3557
Baolin Wang72704f82016-05-16 16:43:53 +08003558static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3559 unsigned int evtinfo)
3560{
3561 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3562
3563 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3564 dwc3_suspend_gadget(dwc);
3565
3566 dwc->link_state = next;
3567}
3568
Felipe Balbie1dadd32014-02-25 14:47:54 -06003569static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3570 unsigned int evtinfo)
3571{
3572 unsigned int is_ss = evtinfo & BIT(4);
3573
Felipe Balbibfad65e2017-04-19 14:59:27 +03003574 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003575 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3576 * have a known issue which can cause USB CV TD.9.23 to fail
3577 * randomly.
3578 *
3579 * Because of this issue, core could generate bogus hibernation
3580 * events which SW needs to ignore.
3581 *
3582 * Refers to:
3583 *
3584 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3585 * Device Fallback from SuperSpeed
3586 */
3587 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3588 return;
3589
3590 /* enter hibernation here */
3591}
3592
Felipe Balbi72246da2011-08-19 18:10:58 +03003593static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3594 const struct dwc3_event_devt *event)
3595{
3596 switch (event->type) {
3597 case DWC3_DEVICE_EVENT_DISCONNECT:
3598 dwc3_gadget_disconnect_interrupt(dwc);
3599 break;
3600 case DWC3_DEVICE_EVENT_RESET:
3601 dwc3_gadget_reset_interrupt(dwc);
3602 break;
3603 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3604 dwc3_gadget_conndone_interrupt(dwc);
3605 break;
3606 case DWC3_DEVICE_EVENT_WAKEUP:
3607 dwc3_gadget_wakeup_interrupt(dwc);
3608 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003609 case DWC3_DEVICE_EVENT_HIBER_REQ:
3610 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3611 "unexpected hibernation event\n"))
3612 break;
3613
3614 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3615 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003616 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3617 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3618 break;
3619 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003620 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003621 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003622 /*
3623 * Ignore suspend event until the gadget enters into
3624 * USB_STATE_CONFIGURED state.
3625 */
Peter Chene81a7012020-08-21 10:55:48 +08003626 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003627 dwc3_gadget_suspend_interrupt(dwc,
3628 event->event_info);
3629 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003630 break;
3631 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003632 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003633 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003634 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003635 break;
3636 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003637 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003638 }
3639}
3640
3641static void dwc3_process_event_entry(struct dwc3 *dwc,
3642 const union dwc3_event *event)
3643{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003644 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003645
Felipe Balbidfc5e802017-04-26 13:44:51 +03003646 if (!event->type.is_devspec)
3647 dwc3_endpoint_interrupt(dwc, &event->depevt);
3648 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003649 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003650 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003651 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003652}
3653
Felipe Balbidea520a2016-03-30 09:39:34 +03003654static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003655{
Felipe Balbidea520a2016-03-30 09:39:34 +03003656 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003657 irqreturn_t ret = IRQ_NONE;
3658 int left;
3659 u32 reg;
3660
Felipe Balbif42f2442013-06-12 21:25:08 +03003661 left = evt->count;
3662
3663 if (!(evt->flags & DWC3_EVENT_PENDING))
3664 return IRQ_NONE;
3665
3666 while (left > 0) {
3667 union dwc3_event event;
3668
John Younebbb2d52016-11-15 13:07:02 +02003669 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003670
3671 dwc3_process_event_entry(dwc, &event);
3672
3673 /*
3674 * FIXME we wrap around correctly to the next entry as
3675 * almost all entries are 4 bytes in size. There is one
3676 * entry which has 12 bytes which is a regular entry
3677 * followed by 8 bytes data. ATM I don't know how
3678 * things are organized if we get next to the a
3679 * boundary so I worry about that once we try to handle
3680 * that.
3681 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003682 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003683 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003684 }
3685
3686 evt->count = 0;
3687 evt->flags &= ~DWC3_EVENT_PENDING;
3688 ret = IRQ_HANDLED;
3689
3690 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003691 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003692 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003693 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003694
John Youncf40b862016-11-14 12:32:43 -08003695 if (dwc->imod_interval) {
3696 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3697 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3698 }
3699
Felipe Balbif42f2442013-06-12 21:25:08 +03003700 return ret;
3701}
3702
Felipe Balbidea520a2016-03-30 09:39:34 +03003703static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003704{
Felipe Balbidea520a2016-03-30 09:39:34 +03003705 struct dwc3_event_buffer *evt = _evt;
3706 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003707 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003708 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003709
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003710 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003711 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003712 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003713
3714 return ret;
3715}
3716
Felipe Balbidea520a2016-03-30 09:39:34 +03003717static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003718{
Felipe Balbidea520a2016-03-30 09:39:34 +03003719 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003720 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003721 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003722 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003723
Felipe Balbifc8bb912016-05-16 13:14:48 +03003724 if (pm_runtime_suspended(dwc->dev)) {
3725 pm_runtime_get(dwc->dev);
3726 disable_irq_nosync(dwc->irq_gadget);
3727 dwc->pending_events = true;
3728 return IRQ_HANDLED;
3729 }
3730
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003731 /*
3732 * With PCIe legacy interrupt, test shows that top-half irq handler can
3733 * be called again after HW interrupt deassertion. Check if bottom-half
3734 * irq event handler completes before caching new event to prevent
3735 * losing events.
3736 */
3737 if (evt->flags & DWC3_EVENT_PENDING)
3738 return IRQ_HANDLED;
3739
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003740 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003741 count &= DWC3_GEVNTCOUNT_MASK;
3742 if (!count)
3743 return IRQ_NONE;
3744
Felipe Balbib15a7622011-06-30 16:57:15 +03003745 evt->count = count;
3746 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003747
Felipe Balbie8adfc32013-06-12 21:11:14 +03003748 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003749 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003750 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003751 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003752
John Younebbb2d52016-11-15 13:07:02 +02003753 amount = min(count, evt->length - evt->lpos);
3754 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3755
3756 if (amount < count)
3757 memcpy(evt->cache, evt->buf, count - amount);
3758
John Youn65aca322016-11-15 13:08:59 +02003759 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3760
Felipe Balbib15a7622011-06-30 16:57:15 +03003761 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003762}
3763
Felipe Balbidea520a2016-03-30 09:39:34 +03003764static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003765{
Felipe Balbidea520a2016-03-30 09:39:34 +03003766 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003767
Felipe Balbidea520a2016-03-30 09:39:34 +03003768 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003769}
3770
Felipe Balbi6db38122016-10-03 11:27:01 +03003771static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3772{
3773 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3774 int irq;
3775
Hans de Goedef146b402019-10-05 23:04:48 +02003776 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003777 if (irq > 0)
3778 goto out;
3779
3780 if (irq == -EPROBE_DEFER)
3781 goto out;
3782
Hans de Goedef146b402019-10-05 23:04:48 +02003783 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003784 if (irq > 0)
3785 goto out;
3786
3787 if (irq == -EPROBE_DEFER)
3788 goto out;
3789
3790 irq = platform_get_irq(dwc3_pdev, 0);
3791 if (irq > 0)
3792 goto out;
3793
Felipe Balbi6db38122016-10-03 11:27:01 +03003794 if (!irq)
3795 irq = -EINVAL;
3796
3797out:
3798 return irq;
3799}
3800
Peter Chene81a7012020-08-21 10:55:48 +08003801static void dwc_gadget_release(struct device *dev)
3802{
3803 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3804
3805 kfree(gadget);
3806}
3807
Felipe Balbi72246da2011-08-19 18:10:58 +03003808/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003809 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003810 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003811 *
3812 * Returns 0 on success otherwise negative errno.
3813 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003814int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003815{
Felipe Balbi6db38122016-10-03 11:27:01 +03003816 int ret;
3817 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003818 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003819
Felipe Balbi6db38122016-10-03 11:27:01 +03003820 irq = dwc3_gadget_get_irq(dwc);
3821 if (irq < 0) {
3822 ret = irq;
3823 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003824 }
3825
3826 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003827
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303828 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3829 sizeof(*dwc->ep0_trb) * 2,
3830 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003831 if (!dwc->ep0_trb) {
3832 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3833 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003834 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003835 }
3836
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003837 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003838 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003839 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003840 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003841 }
3842
Felipe Balbi905dc042017-01-05 14:46:52 +02003843 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3844 &dwc->bounce_addr, GFP_KERNEL);
3845 if (!dwc->bounce) {
3846 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003847 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003848 }
3849
Baolin Wangbb014732016-10-14 17:11:33 +08003850 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003851 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3852 if (!dwc->gadget) {
3853 ret = -ENOMEM;
3854 goto err3;
3855 }
Baolin Wangbb014732016-10-14 17:11:33 +08003856
Peter Chene81a7012020-08-21 10:55:48 +08003857
3858 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3859 dev = &dwc->gadget->dev;
3860 dev->platform_data = dwc;
3861 dwc->gadget->ops = &dwc3_gadget_ops;
3862 dwc->gadget->speed = USB_SPEED_UNKNOWN;
3863 dwc->gadget->sg_supported = true;
3864 dwc->gadget->name = "dwc3-gadget";
3865 dwc->gadget->lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003866
3867 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003868 * FIXME We might be setting max_speed to <SUPER, however versions
3869 * <2.20a of dwc3 have an issue with metastability (documented
3870 * elsewhere in this driver) which tells us we can't set max speed to
3871 * anything lower than SUPER.
3872 *
3873 * Because gadget.max_speed is only used by composite.c and function
3874 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3875 * to happen so we avoid sending SuperSpeed Capability descriptor
3876 * together with our BOS descriptor as that could confuse host into
3877 * thinking we can handle super speed.
3878 *
3879 * Note that, in fact, we won't even support GetBOS requests when speed
3880 * is less than super speed because we don't have means, yet, to tell
3881 * composite.c that we are USB 2.0 + LPM ECN.
3882 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003883 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003884 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003885 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003886 dwc->revision);
3887
Peter Chene81a7012020-08-21 10:55:48 +08003888 dwc->gadget->max_speed = dwc->maximum_speed;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003889
3890 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003891 * REVISIT: Here we should clear all pending IRQs to be
3892 * sure we're starting from a well known location.
3893 */
3894
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003895 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003896 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003897 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003898
3899 ret = usb_add_gadget(dwc->gadget);
3900 if (ret) {
3901 dev_err(dwc->dev, "failed to add gadget\n");
3902 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003903 }
3904
Peter Chene81a7012020-08-21 10:55:48 +08003905 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003906
Felipe Balbi72246da2011-08-19 18:10:58 +03003907 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003908
Peter Chene81a7012020-08-21 10:55:48 +08003909err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003910 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003911err4:
3912 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003913err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003914 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3915 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003916
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003917err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003918 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003919
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003920err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303921 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003922 dwc->ep0_trb, dwc->ep0_trb_addr);
3923
Felipe Balbi72246da2011-08-19 18:10:58 +03003924err0:
3925 return ret;
3926}
3927
Felipe Balbi7415f172012-04-30 14:56:33 +03003928/* -------------------------------------------------------------------------- */
3929
Felipe Balbi72246da2011-08-19 18:10:58 +03003930void dwc3_gadget_exit(struct dwc3 *dwc)
3931{
Peter Chene81a7012020-08-21 10:55:48 +08003932 usb_del_gadget_udc(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003933 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003934 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003935 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003936 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303937 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003938 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003939}
Felipe Balbi7415f172012-04-30 14:56:33 +03003940
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003941int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003942{
Roger Quadros9772b472016-04-12 11:33:29 +03003943 if (!dwc->gadget_driver)
3944 return 0;
3945
Roger Quadros1551e352017-02-15 14:16:26 +02003946 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003947 dwc3_disconnect_gadget(dwc);
3948 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003949
3950 return 0;
3951}
3952
3953int dwc3_gadget_resume(struct dwc3 *dwc)
3954{
Felipe Balbi7415f172012-04-30 14:56:33 +03003955 int ret;
3956
Roger Quadros9772b472016-04-12 11:33:29 +03003957 if (!dwc->gadget_driver)
3958 return 0;
3959
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003960 ret = __dwc3_gadget_start(dwc);
3961 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003962 goto err0;
3963
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003964 ret = dwc3_gadget_run_stop(dwc, true, false);
3965 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003966 goto err1;
3967
Felipe Balbi7415f172012-04-30 14:56:33 +03003968 return 0;
3969
3970err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003971 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003972
3973err0:
3974 return ret;
3975}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003976
3977void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3978{
3979 if (dwc->pending_events) {
3980 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3981 dwc->pending_events = false;
3982 enable_irq(dwc->irq_gadget);
3983 }
3984}