blob: 00b3f19c4d3efab260d6d6d178058219bbce533b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
Alexander A. Klimov10623b82020-07-11 15:58:04 +02005 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbid5370102018-08-14 10:42:43 +030030#define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
Felipe Balbif62afb42018-04-11 10:34:34 +030031 & ~((d)->interval - 1))
32
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020033/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030034 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020035 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030038 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 */
41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48 switch (mode) {
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +020049 case USB_TEST_J:
50 case USB_TEST_K:
51 case USB_TEST_SE0_NAK:
52 case USB_TEST_PACKET:
53 case USB_TEST_FORCE_ENABLE:
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020054 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
Thinh Nguyen5b738212019-10-23 19:15:43 -070060 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020061
62 return 0;
63}
64
Felipe Balbi8598bde2012-01-02 18:55:57 +020065/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030066 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030067 * @dwc: pointer to our context structure
68 *
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
71 */
72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78 return DWC3_DSTS_USBLNKST(reg);
79}
80
81/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030082 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020083 * @dwc: pointer to our context structure
84 * @state: the state to put link into
85 *
86 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080087 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 */
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080091 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 u32 reg;
93
Paul Zimmerman802fde92012-04-27 13:10:52 +030094 /*
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
97 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -070098 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
Felipe Balbi8598bde2012-01-02 18:55:57 +0200111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
Thinh Nguyen2e708fa2019-10-23 19:15:55 -0700114 /* set no action before sending new link state change */
115 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 /* set requested state */
118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120
Paul Zimmerman802fde92012-04-27 13:10:52 +0300121 /*
122 * The following code is racy when called from dwc3_gadget_wakeup,
123 * and is not needed, at least on newer versions
124 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +0300126 return 0;
127
Felipe Balbi8598bde2012-01-02 18:55:57 +0200128 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300129 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 while (--retries) {
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 if (DWC3_DSTS_USBLNKST(reg) == state)
134 return 0;
135
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800136 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 }
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 return -ETIMEDOUT;
140}
141
John Youndca01192016-05-19 17:26:05 -0700142/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300143 * dwc3_ep_inc_trb - increment a trb index.
144 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700145 *
146 * The index should never point to the link TRB. After incrementing,
147 * if it is point to the link TRB, wrap around to the beginning. The
148 * link TRB is always at the last TRB entry.
149 */
150static void dwc3_ep_inc_trb(u8 *index)
151{
152 (*index)++;
153 if (*index == (DWC3_TRB_NUM - 1))
154 *index = 0;
155}
156
Felipe Balbibfad65e2017-04-19 14:59:27 +0300157/**
158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159 * @dep: The endpoint whose enqueue pointer we're incrementing
160 */
Felipe Balbief966b92016-04-05 13:09:51 +0300161static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162{
John Youndca01192016-05-19 17:26:05 -0700163 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300164}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200165
Felipe Balbibfad65e2017-04-19 14:59:27 +0300166/**
167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168 * @dep: The endpoint whose enqueue pointer we're incrementing
169 */
Felipe Balbief966b92016-04-05 13:09:51 +0300170static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171{
John Youndca01192016-05-19 17:26:05 -0700172 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200173}
174
Wei Yongjun69102512018-03-29 02:20:10 +0000175static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300176 struct dwc3_request *req, int status)
177{
178 struct dwc3 *dwc = dep->dwc;
179
Felipe Balbic91815b2018-03-26 13:14:47 +0300180 list_del(&req->list);
181 req->remaining = 0;
Jack Phambd6742242019-01-10 12:39:55 -0800182 req->needs_extra_trb = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300183
184 if (req->request.status == -EINPROGRESS)
185 req->request.status = status;
186
187 if (req->trb)
188 usb_gadget_unmap_request_by_dev(dwc->sysdev,
189 &req->request, req->direction);
190
191 req->trb = NULL;
192 trace_dwc3_gadget_giveback(req);
193
194 if (dep->number > 1)
195 pm_runtime_put(dwc->dev);
196}
197
Felipe Balbibfad65e2017-04-19 14:59:27 +0300198/**
199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
200 * @dep: The endpoint to whom the request belongs to
201 * @req: The request we're giving back
202 * @status: completion code for the request
203 *
204 * Must be called with controller's lock held and interrupts disabled. This
205 * function will unmap @req and call its ->complete() callback to notify upper
206 * layers that it has completed.
207 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300208void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
209 int status)
210{
211 struct dwc3 *dwc = dep->dwc;
212
Felipe Balbic91815b2018-03-26 13:14:47 +0300213 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbia3af5e32019-01-11 12:57:09 +0200214 req->status = DWC3_REQUEST_STATUS_COMPLETED;
Felipe Balbi72246da2011-08-19 18:10:58 +0300215
216 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200217 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 spin_lock(&dwc->lock);
219}
220
Felipe Balbibfad65e2017-04-19 14:59:27 +0300221/**
222 * dwc3_send_gadget_generic_command - issue a generic command for the controller
223 * @dwc: pointer to the controller context
224 * @cmd: the command to be issued
225 * @param: command parameter
226 *
227 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
228 * and wait for its completion.
229 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300230int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231 u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300232{
233 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300234 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236 u32 reg;
237
238 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
239 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240
241 do {
242 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
243 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300244 status = DWC3_DGCMD_STATUS(reg);
245 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 ret = -EINVAL;
247 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300248 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100249 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250
251 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300252 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300253 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300254 }
255
Felipe Balbi71f7e702016-05-23 14:16:19 +0300256 trace_dwc3_gadget_generic_cmd(cmd, param, status);
257
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300258 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300259}
260
Felipe Balbic36d8e92016-04-04 12:46:33 +0300261static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262
Felipe Balbibfad65e2017-04-19 14:59:27 +0300263/**
264 * dwc3_send_gadget_ep_cmd - issue an endpoint command
265 * @dep: the endpoint to which the command is going to be issued
266 * @cmd: the command to be issued
267 * @params: parameters to the command
268 *
269 * Caller should handle locking. This function will issue @cmd with given
270 * @params to @dep and wait for its completion.
271 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300272int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
Felipe Balbi2cd47182016-04-12 16:42:43 +0300273 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300274{
Felipe Balbi8897a762016-09-22 10:56:08 +0300275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300276 struct dwc3 *dwc = dep->dwc;
Yu Chen1c0e69a2020-05-21 16:46:43 +0800277 u32 timeout = 5000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700278 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 u32 reg;
280
Felipe Balbi0933df12016-05-23 14:02:33 +0300281 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300282 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300283
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700285 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
286 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
287 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300288 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700289 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
290 * settings. Restore them after the command is completed.
291 *
292 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300293 */
Peter Chene81a7012020-08-21 10:55:48 +0800294 if (dwc->gadget->speed <= USB_SPEED_HIGH) {
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300295 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
296 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700297 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300298 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300299 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700300
301 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
302 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
303 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
304 }
305
306 if (saved_config)
307 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300308 }
309
Felipe Balbi59999142016-09-22 12:25:28 +0300310 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Thinh Nguyenc560e762021-04-19 19:11:12 -0700311 int link_state;
Felipe Balbic36d8e92016-04-04 12:46:33 +0300312
Thinh Nguyen63c4c322021-10-25 16:35:06 -0700313 /*
314 * Initiate remote wakeup if the link state is in U3 when
315 * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
316 * link state is in U1/U2, no remote wakeup is needed. The Start
317 * Transfer command will initiate the link recovery.
318 */
Thinh Nguyenc560e762021-04-19 19:11:12 -0700319 link_state = dwc3_gadget_get_link_state(dwc);
Thinh Nguyen63c4c322021-10-25 16:35:06 -0700320 switch (link_state) {
321 case DWC3_LINK_STATE_U2:
322 if (dwc->gadget->speed >= USB_SPEED_SUPER)
323 break;
324
325 fallthrough;
326 case DWC3_LINK_STATE_U3:
Felipe Balbic36d8e92016-04-04 12:46:33 +0300327 ret = __dwc3_gadget_wakeup(dwc);
328 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
329 ret);
Thinh Nguyen63c4c322021-10-25 16:35:06 -0700330 break;
Felipe Balbic36d8e92016-04-04 12:46:33 +0300331 }
332 }
333
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
335 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
336 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300337
Felipe Balbi8897a762016-09-22 10:56:08 +0300338 /*
339 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
340 * not relying on XferNotReady, we can make use of a special "No
341 * Response Update Transfer" command where we should clear both CmdAct
342 * and CmdIOC bits.
343 *
344 * With this, we don't need to wait for command completion and can
345 * straight away issue further commands to the endpoint.
346 *
347 * NOTICE: We're making an assumption that control endpoints will never
348 * make use of Update Transfer command. This is a safe assumption
349 * because we can never have more than one request at a time with
350 * Control Endpoints. If anybody changes that assumption, this chunk
351 * needs to be updated accordingly.
352 */
353 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
354 !usb_endpoint_xfer_isoc(desc))
355 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
356 else
357 cmd |= DWC3_DEPCMD_CMDACT;
358
359 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Thinh Nguyenbc271172021-11-29 18:53:09 -0800360
361 if (!(cmd & DWC3_DEPCMD_CMDACT)) {
362 ret = 0;
363 goto skip_status;
364 }
365
Felipe Balbi72246da2011-08-19 18:10:58 +0300366 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300367 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300369 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000370
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000371 switch (cmd_status) {
372 case 0:
373 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300374 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000375 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700376 dev_WARN(dwc->dev, "No resource for %s\n",
377 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000378 ret = -EINVAL;
379 break;
380 case DEPEVT_TRANSFER_BUS_EXPIRY:
381 /*
382 * SW issues START TRANSFER command to
383 * isochronous ep with future frame interval. If
384 * future interval time has already passed when
385 * core receives the command, it will respond
386 * with an error status of 'Bus Expiry'.
387 *
388 * Instead of always returning -EINVAL, let's
389 * give a hint to the gadget driver that this is
390 * the case by returning -EAGAIN.
391 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000392 ret = -EAGAIN;
393 break;
394 default:
395 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
396 }
397
Felipe Balbic0ca3242016-04-04 09:11:51 +0300398 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300399 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300400 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300401
Felipe Balbif6bb2252016-05-23 13:53:34 +0300402 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300403 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300404 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300405 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300406
Thinh Nguyenbc271172021-11-29 18:53:09 -0800407skip_status:
Felipe Balbi0933df12016-05-23 14:02:33 +0300408 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
409
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700410 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
411 if (ret == 0)
412 dep->flags |= DWC3_EP_TRANSFER_STARTED;
413
414 if (ret != -ETIMEDOUT)
415 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300416 }
417
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700418 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300419 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700420 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300421 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
422 }
423
Felipe Balbic0ca3242016-04-04 09:11:51 +0300424 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300425}
426
John Youn50c763f2016-05-31 17:49:56 -0700427static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
428{
429 struct dwc3 *dwc = dep->dwc;
430 struct dwc3_gadget_ep_cmd_params params;
431 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
432
433 /*
434 * As of core revision 2.60a the recommended programming model
435 * is to set the ClearPendIN bit when issuing a Clear Stall EP
436 * command for IN endpoints. This is to prevent an issue where
437 * some (non-compliant) hosts may not send ACK TPs for pending
438 * IN transfers due to a mishandled error condition. Synopsys
439 * STAR 9000614252.
440 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700441 if (dep->direction &&
442 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800443 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700444 cmd |= DWC3_DEPCMD_CLEARPENDIN;
445
446 memset(&params, 0, sizeof(params));
447
Felipe Balbi2cd47182016-04-12 16:42:43 +0300448 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700449}
450
Felipe Balbi72246da2011-08-19 18:10:58 +0300451static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200452 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300453{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300454 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
456 return dep->trb_pool_dma + offset;
457}
458
459static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
460{
461 struct dwc3 *dwc = dep->dwc;
462
463 if (dep->trb_pool)
464 return 0;
465
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530466 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300467 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
468 &dep->trb_pool_dma, GFP_KERNEL);
469 if (!dep->trb_pool) {
470 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
471 dep->name);
472 return -ENOMEM;
473 }
474
475 return 0;
476}
477
478static void dwc3_free_trb_pool(struct dwc3_ep *dep)
479{
480 struct dwc3 *dwc = dep->dwc;
481
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530482 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 dep->trb_pool, dep->trb_pool_dma);
484
485 dep->trb_pool = NULL;
486 dep->trb_pool_dma = 0;
487}
488
Felipe Balbi20d1d432018-04-09 12:49:02 +0300489static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
490{
491 struct dwc3_gadget_ep_cmd_params params;
492
493 memset(&params, 0x00, sizeof(params));
494
495 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
496
497 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
498 &params);
499}
John Younc4509602016-02-16 20:10:53 -0800500
501/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300502 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800503 * @dep: endpoint that is being enabled
504 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300505 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
506 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800507 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300508 * The assignment of transfer resources cannot perfectly follow the data book
509 * due to the fact that the controller driver does not have all knowledge of the
510 * configuration in advance. It is given this information piecemeal by the
511 * composite gadget framework after every SET_CONFIGURATION and
512 * SET_INTERFACE. Trying to follow the databook programming model in this
513 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800514 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300515 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
516 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
517 * incorrect in the scenario of multiple interfaces.
518 *
519 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800520 * endpoint on alt setting (8.1.6).
521 *
522 * The following simplified method is used instead:
523 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300524 * All hardware endpoints can be assigned a transfer resource and this setting
525 * will stay persistent until either a core reset or hibernation. So whenever we
526 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
527 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800528 * guaranteed that there are as many transfer resources as endpoints.
529 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300530 * This function is called for each endpoint when it is being enabled but is
531 * triggered only when called for EP0-out, which always happens first, and which
532 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800533 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300534static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300535{
536 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300537 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800539 int i;
540 int ret;
541
542 if (dep->number)
543 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
545 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800546 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300547 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
Felipe Balbi2cd47182016-04-12 16:42:43 +0300549 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800550 if (ret)
551 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300552
John Younc4509602016-02-16 20:10:53 -0800553 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
554 struct dwc3_ep *dep = dwc->eps[i];
555
556 if (!dep)
557 continue;
558
Felipe Balbib07c2db2018-04-09 12:46:47 +0300559 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800560 if (ret)
561 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562 }
563
564 return 0;
565}
566
Felipe Balbib07c2db2018-04-09 12:46:47 +0300567static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300568{
John Youn39ebb052016-11-09 16:36:28 -0800569 const struct usb_ss_ep_comp_descriptor *comp_desc;
570 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300571 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300572 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
John Youn39ebb052016-11-09 16:36:28 -0800574 comp_desc = dep->endpoint.comp_desc;
575 desc = dep->endpoint.desc;
576
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 memset(&params, 0x00, sizeof(params));
578
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300579 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900580 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
581
582 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800583 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300584 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300585
Felipe Balbi676e3492016-04-26 10:49:07 +0300586 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900587 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
Felipe Balbia2d23f02018-04-09 12:40:48 +0300589 params.param0 |= action;
590 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600591 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600592
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300593 if (usb_endpoint_xfer_control(desc))
594 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300595
596 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
597 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200599 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300600 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700601 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300602 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300603 dep->stream_capable = true;
604 }
605
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500606 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300607 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300608
609 /*
610 * We are doing 1:1 mapping for endpoints, meaning
611 * Physical Endpoints 2 maps to Logical Endpoint 2 and
612 * so on. We consider the direction bit as part of the physical
613 * endpoint number. So USB endpoint 0x81 is 0x03.
614 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300615 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 /*
618 * We must use the lower 16 TX FIFOs even though
619 * HW might have more
620 */
621 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300622 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623
624 if (desc->bInterval) {
Thinh Nguyena1679af2021-02-08 13:53:10 -0800625 u8 bInterval_m1;
626
627 /*
Thinh Nguyen3232a3c2021-04-15 00:41:58 -0700628 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
629 *
630 * NOTE: The programming guide incorrectly stated bInterval_m1
631 * must be set to 0 when operating in fullspeed. Internally the
632 * controller does not have this limitation. See DWC_usb3x
633 * programming guide section 3.2.2.1.
Thinh Nguyena1679af2021-02-08 13:53:10 -0800634 */
635 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
Thinh Nguyena1679af2021-02-08 13:53:10 -0800636
Thinh Nguyen4b049f52021-02-08 13:53:16 -0800637 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
638 dwc->gadget->speed == USB_SPEED_FULL)
639 dep->interval = desc->bInterval;
640 else
641 dep->interval = 1 << (desc->bInterval - 1);
642
Thinh Nguyena1679af2021-02-08 13:53:10 -0800643 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 }
645
Felipe Balbi2cd47182016-04-12 16:42:43 +0300646 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300647}
648
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700649static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
650 bool interrupt);
651
Felipe Balbi72246da2011-08-19 18:10:58 +0300652/**
Wesley Cheng9f607a32021-07-10 02:13:12 -0700653 * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
654 * @dwc: pointer to the DWC3 context
655 * @nfifos: number of fifos to calculate for
656 *
657 * Calculates the size value based on the equation below:
658 *
659 * DWC3 revision 280A and prior:
660 * fifo_size = mult * (max_packet / mdwidth) + 1;
661 *
662 * DWC3 revision 290A and onwards:
663 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
664 *
665 * The max packet size is set to 1024, as the txfifo requirements mainly apply
666 * to super speed USB use cases. However, it is safe to overestimate the fifo
667 * allocations for other scenarios, i.e. high speed USB.
668 */
669static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
670{
671 int max_packet = 1024;
672 int fifo_size;
673 int mdwidth;
674
675 mdwidth = dwc3_mdwidth(dwc);
676
677 /* MDWIDTH is represented in bits, we need it in bytes */
678 mdwidth >>= 3;
679
680 if (DWC3_VER_IS_PRIOR(DWC3, 290A))
681 fifo_size = mult * (max_packet / mdwidth) + 1;
682 else
683 fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
684 return fifo_size;
685}
686
687/**
688 * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation
689 * @dwc: pointer to the DWC3 context
690 *
691 * Iterates through all the endpoint registers and clears the previous txfifo
692 * allocations.
693 */
694void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
695{
696 struct dwc3_ep *dep;
697 int fifo_depth;
698 int size;
699 int num;
700
701 if (!dwc->do_fifo_resize)
702 return;
703
704 /* Read ep0IN related TXFIFO size */
705 dep = dwc->eps[1];
706 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
707 if (DWC3_IP_IS(DWC3))
708 fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
709 else
710 fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
711
712 dwc->last_fifo_depth = fifo_depth;
713 /* Clear existing TXFIFO for all IN eps except ep0 */
714 for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
715 num += 2) {
716 dep = dwc->eps[num];
717 /* Don't change TXFRAMNUM on usb31 version */
718 size = DWC3_IP_IS(DWC3) ? 0 :
719 dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
720 DWC31_GTXFIFOSIZ_TXFRAMNUM;
721
722 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
Jack Pham876a75cb2021-10-21 11:01:28 -0700723 dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
Wesley Cheng9f607a32021-07-10 02:13:12 -0700724 }
725 dwc->num_ep_resized = 0;
726}
727
728/*
729 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
730 * @dwc: pointer to our context structure
731 *
732 * This function will a best effort FIFO allocation in order
733 * to improve FIFO usage and throughput, while still allowing
734 * us to enable as many endpoints as possible.
735 *
736 * Keep in mind that this operation will be highly dependent
737 * on the configured size for RAM1 - which contains TxFifo -,
738 * the amount of endpoints enabled on coreConsultant tool, and
739 * the width of the Master Bus.
740 *
741 * In general, FIFO depths are represented with the following equation:
742 *
743 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
744 *
745 * In conjunction with dwc3_gadget_check_config(), this resizing logic will
746 * ensure that all endpoints will have enough internal memory for one max
747 * packet per endpoint.
748 */
749static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
750{
751 struct dwc3 *dwc = dep->dwc;
752 int fifo_0_start;
753 int ram1_depth;
754 int fifo_size;
755 int min_depth;
756 int num_in_ep;
757 int remaining;
758 int num_fifos = 1;
759 int fifo;
760 int tmp;
761
762 if (!dwc->do_fifo_resize)
763 return 0;
764
765 /* resize IN endpoints except ep0 */
766 if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
767 return 0;
768
Jack Pham876a75cb2021-10-21 11:01:28 -0700769 /* bail if already resized */
770 if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
771 return 0;
772
Wesley Cheng9f607a32021-07-10 02:13:12 -0700773 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
774
775 if ((dep->endpoint.maxburst > 1 &&
776 usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
777 usb_endpoint_xfer_isoc(dep->endpoint.desc))
778 num_fifos = 3;
779
780 if (dep->endpoint.maxburst > 6 &&
781 usb_endpoint_xfer_bulk(dep->endpoint.desc) && DWC3_IP_IS(DWC31))
782 num_fifos = dwc->tx_fifo_resize_max_num;
783
784 /* FIFO size for a single buffer */
785 fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
786
787 /* Calculate the number of remaining EPs w/o any FIFO */
788 num_in_ep = dwc->max_cfg_eps;
789 num_in_ep -= dwc->num_ep_resized;
790
791 /* Reserve at least one FIFO for the number of IN EPs */
792 min_depth = num_in_ep * (fifo + 1);
793 remaining = ram1_depth - min_depth - dwc->last_fifo_depth;
794 remaining = max_t(int, 0, remaining);
795 /*
796 * We've already reserved 1 FIFO per EP, so check what we can fit in
797 * addition to it. If there is not enough remaining space, allocate
798 * all the remaining space to the EP.
799 */
800 fifo_size = (num_fifos - 1) * fifo;
801 if (remaining < fifo_size)
802 fifo_size = remaining;
803
804 fifo_size += fifo;
805 /* Last increment according to the TX FIFO size equation */
806 fifo_size++;
807
808 /* Check if TXFIFOs start at non-zero addr */
809 tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
810 fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
811
812 fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
813 if (DWC3_IP_IS(DWC3))
814 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
815 else
816 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
817
818 /* Check fifo size allocation doesn't exceed available RAM size. */
819 if (dwc->last_fifo_depth >= ram1_depth) {
820 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
821 dwc->last_fifo_depth, ram1_depth,
822 dep->endpoint.name, fifo_size);
823 if (DWC3_IP_IS(DWC3))
824 fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
825 else
826 fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
827
828 dwc->last_fifo_depth -= fifo_size;
829 return -ENOMEM;
830 }
831
832 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
Jack Pham876a75cb2021-10-21 11:01:28 -0700833 dep->flags |= DWC3_EP_TXFIFO_RESIZED;
Wesley Cheng9f607a32021-07-10 02:13:12 -0700834 dwc->num_ep_resized++;
835
836 return 0;
837}
838
839/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300840 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300841 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300842 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300844 * Caller should take care of locking. Execute all necessary commands to
845 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300846 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300847static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300848{
John Youn39ebb052016-11-09 16:36:28 -0800849 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300850 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800851
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300853 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300854
855 if (!(dep->flags & DWC3_EP_ENABLED)) {
Wesley Cheng9f607a32021-07-10 02:13:12 -0700856 ret = dwc3_gadget_resize_tx_fifos(dep);
857 if (ret)
858 return ret;
859
Felipe Balbib07c2db2018-04-09 12:46:47 +0300860 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300861 if (ret)
862 return ret;
863 }
864
Felipe Balbib07c2db2018-04-09 12:46:47 +0300865 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300866 if (ret)
867 return ret;
868
869 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200870 struct dwc3_trb *trb_st_hw;
871 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300872
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 dep->type = usb_endpoint_type(desc);
874 dep->flags |= DWC3_EP_ENABLED;
875
876 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
877 reg |= DWC3_DALEPENA_EP(dep->number);
878 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
879
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300880 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200881 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300882
John Youn0d257442016-05-19 17:26:08 -0700883 /* Initialize the TRB ring */
884 dep->trb_dequeue = 0;
885 dep->trb_enqueue = 0;
886 memset(dep->trb_pool, 0,
887 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
888
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300889 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300890 trb_st_hw = &dep->trb_pool[0];
891
Felipe Balbif6bafc62012-02-06 11:04:53 +0200892 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200893 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
894 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
895 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
896 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300897 }
898
Felipe Balbia97ea992016-09-29 16:28:56 +0300899 /*
900 * Issue StartTransfer here with no-op TRB so we can always rely on No
901 * Response Update Transfer command.
902 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700903 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300904 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300905 struct dwc3_gadget_ep_cmd_params params;
906 struct dwc3_trb *trb;
907 dma_addr_t trb_dma;
908 u32 cmd;
909
910 memset(&params, 0, sizeof(params));
911 trb = &dep->trb_pool[0];
912 trb_dma = dwc3_trb_dma_offset(dep, trb);
913
914 params.param0 = upper_32_bits(trb_dma);
915 params.param1 = lower_32_bits(trb_dma);
916
917 cmd = DWC3_DEPCMD_STARTTRANSFER;
918
919 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
920 if (ret < 0)
921 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700922
923 if (dep->stream_capable) {
924 /*
925 * For streams, at start, there maybe a race where the
926 * host primes the endpoint before the function driver
927 * queues a request to initiate a stream. In that case,
928 * the controller will not see the prime to generate the
929 * ERDY and start stream. To workaround this, issue a
930 * no-op TRB as normal, but end it immediately. As a
931 * result, when the function driver queues the request,
932 * the next START_TRANSFER command will cause the
933 * controller to generate an ERDY to initiate the
934 * stream.
935 */
936 dwc3_stop_active_transfer(dep, true, true);
937
938 /*
939 * All stream eps will reinitiate stream on NoStream
940 * rejection until we can determine that the host can
941 * prime after the first transfer.
Thinh Nguyenddae7972021-04-22 16:51:43 -0700942 *
943 * However, if the controller is capable of
944 * TXF_FLUSH_BYPASS, then IN direction endpoints will
945 * automatically restart the stream without the driver
946 * initiation.
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700947 */
Thinh Nguyenddae7972021-04-22 16:51:43 -0700948 if (!dep->direction ||
949 !(dwc->hwparams.hwparams9 &
950 DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
951 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700952 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300953 }
954
Felipe Balbi2870e502016-11-03 13:53:29 +0200955out:
956 trace_dwc3_gadget_ep_enable(dep);
957
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 return 0;
959}
960
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200961static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300962{
963 struct dwc3_request *req;
964
Felipe Balbic5353b22019-02-13 13:00:54 +0200965 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300966
Felipe Balbi0e146022016-06-21 10:32:02 +0300967 /* - giveback all requests to gadget driver */
968 while (!list_empty(&dep->started_list)) {
969 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200970
Felipe Balbi0e146022016-06-21 10:32:02 +0300971 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200972 }
973
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200974 while (!list_empty(&dep->pending_list)) {
975 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200977 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300978 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200979
980 while (!list_empty(&dep->cancelled_list)) {
981 req = next_request(&dep->cancelled_list);
982
983 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
984 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300985}
986
987/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300988 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 * @dep: the endpoint to disable
990 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300991 * This function undoes what __dwc3_gadget_ep_enable did and also removes
992 * requests which are currently being processed by the hardware and those which
993 * are not yet scheduled.
994 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200995 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300997static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
998{
999 struct dwc3 *dwc = dep->dwc;
1000 u32 reg;
1001
Felipe Balbi2870e502016-11-03 13:53:29 +02001002 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -05001003
Felipe Balbi687ef982014-04-16 10:30:33 -05001004 /* make sure HW endpoint isn't stalled */
1005 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -05001006 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -05001007
Felipe Balbi72246da2011-08-19 18:10:58 +03001008 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
1009 reg &= ~DWC3_DALEPENA_EP(dep->number);
1010 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
1011
John Youn39ebb052016-11-09 16:36:28 -08001012 /* Clear out the ep descriptors for non-ep0 */
1013 if (dep->number > 1) {
1014 dep->endpoint.comp_desc = NULL;
1015 dep->endpoint.desc = NULL;
1016 }
1017
Wesley Chengf09ddcf2021-03-11 15:59:02 -08001018 dwc3_remove_requests(dwc, dep);
1019
Wesley Cheng5aef62972021-03-24 11:31:04 -07001020 dep->stream_capable = false;
1021 dep->type = 0;
Jack Pham876a75cb2021-10-21 11:01:28 -07001022 dep->flags &= DWC3_EP_TXFIFO_RESIZED;
Wesley Cheng5aef62972021-03-24 11:31:04 -07001023
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 return 0;
1025}
1026
1027/* -------------------------------------------------------------------------- */
1028
1029static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
1030 const struct usb_endpoint_descriptor *desc)
1031{
1032 return -EINVAL;
1033}
1034
1035static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
1036{
1037 return -EINVAL;
1038}
1039
1040/* -------------------------------------------------------------------------- */
1041
1042static int dwc3_gadget_ep_enable(struct usb_ep *ep,
1043 const struct usb_endpoint_descriptor *desc)
1044{
1045 struct dwc3_ep *dep;
1046 struct dwc3 *dwc;
1047 unsigned long flags;
1048 int ret;
1049
1050 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
1051 pr_debug("dwc3: invalid parameters\n");
1052 return -EINVAL;
1053 }
1054
1055 if (!desc->wMaxPacketSize) {
1056 pr_debug("dwc3: missing wMaxPacketSize\n");
1057 return -EINVAL;
1058 }
1059
1060 dep = to_dwc3_ep(ep);
1061 dwc = dep->dwc;
1062
Felipe Balbi95ca9612015-12-10 13:08:20 -06001063 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
1064 "%s is already enabled\n",
1065 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +03001066 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +03001067
Felipe Balbi72246da2011-08-19 18:10:58 +03001068 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +03001069 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001070 spin_unlock_irqrestore(&dwc->lock, flags);
1071
1072 return ret;
1073}
1074
1075static int dwc3_gadget_ep_disable(struct usb_ep *ep)
1076{
1077 struct dwc3_ep *dep;
1078 struct dwc3 *dwc;
1079 unsigned long flags;
1080 int ret;
1081
1082 if (!ep) {
1083 pr_debug("dwc3: invalid parameters\n");
1084 return -EINVAL;
1085 }
1086
1087 dep = to_dwc3_ep(ep);
1088 dwc = dep->dwc;
1089
Felipe Balbi95ca9612015-12-10 13:08:20 -06001090 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
1091 "%s is already disabled\n",
1092 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +03001093 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001094
Felipe Balbi72246da2011-08-19 18:10:58 +03001095 spin_lock_irqsave(&dwc->lock, flags);
1096 ret = __dwc3_gadget_ep_disable(dep);
1097 spin_unlock_irqrestore(&dwc->lock, flags);
1098
1099 return ret;
1100}
1101
1102static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001103 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +03001104{
1105 struct dwc3_request *req;
1106 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001107
1108 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +09001109 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001111
Felipe Balbi31a2f5a2018-05-07 15:19:31 +03001112 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001113 req->epnum = dep->number;
1114 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +02001115 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +03001116
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001117 trace_dwc3_alloc_request(req);
1118
Felipe Balbi72246da2011-08-19 18:10:58 +03001119 return &req->request;
1120}
1121
1122static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1123 struct usb_request *request)
1124{
1125 struct dwc3_request *req = to_dwc3_request(request);
1126
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001127 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001128 kfree(req);
1129}
1130
Felipe Balbi42626912018-04-09 13:01:43 +03001131/**
1132 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1133 * @dep: The endpoint with the TRB ring
1134 * @index: The index of the current TRB in the ring
1135 *
1136 * Returns the TRB prior to the one pointed to by the index. If the
1137 * index is 0, we will wrap backwards, skip the link TRB, and return
1138 * the one just before that.
1139 */
1140static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1141{
1142 u8 tmp = index;
1143
1144 if (!tmp)
1145 tmp = DWC3_TRB_NUM - 1;
1146
1147 return &dep->trb_pool[tmp - 1];
1148}
1149
1150static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1151{
Felipe Balbi42626912018-04-09 13:01:43 +03001152 u8 trbs_left;
1153
1154 /*
Thinh Nguyen51f19542021-08-19 03:17:03 +02001155 * If the enqueue & dequeue are equal then the TRB ring is either full
1156 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
1157 * pending to be processed by the driver.
Felipe Balbi42626912018-04-09 13:01:43 +03001158 */
1159 if (dep->trb_enqueue == dep->trb_dequeue) {
Thinh Nguyen51f19542021-08-19 03:17:03 +02001160 /*
1161 * If there is any request remained in the started_list at
1162 * this point, that means there is no TRB available.
1163 */
1164 if (!list_empty(&dep->started_list))
Felipe Balbi42626912018-04-09 13:01:43 +03001165 return 0;
1166
1167 return DWC3_TRB_NUM - 1;
1168 }
1169
1170 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1171 trbs_left &= (DWC3_TRB_NUM - 1);
1172
1173 if (dep->trb_dequeue < dep->trb_enqueue)
1174 trbs_left--;
1175
1176 return trbs_left;
1177}
Felipe Balbi2c78c022016-08-12 13:13:10 +03001178
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001179static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +03001180 dma_addr_t dma, unsigned int length, unsigned int chain,
1181 unsigned int node, unsigned int stream_id,
1182 unsigned int short_not_ok, unsigned int no_interrupt,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001183 unsigned int is_last, bool must_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +02001184{
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001185 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +08001186 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001187 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +02001188
Felipe Balbif6bafc62012-02-06 11:04:53 +02001189 trb->size = DWC3_TRB_SIZE_LENGTH(length);
1190 trb->bpl = lower_32_bits(dma);
1191 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +02001192
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001193 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +02001194 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001195 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +02001196 break;
1197
1198 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001199 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301200 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001201
Manu Gautam40d829f2017-07-19 17:07:10 +05301202 /*
1203 * USB Specification 2.0 Section 5.9.2 states that: "If
1204 * there is only a single transaction in the microframe,
1205 * only a DATA0 data packet PID is used. If there are
1206 * two transactions per microframe, DATA1 is used for
1207 * the first transaction data packet and DATA0 is used
1208 * for the second transaction data packet. If there are
1209 * three transactions per microframe, DATA2 is used for
1210 * the first transaction data packet, DATA1 is used for
1211 * the second, and DATA0 is used for the third."
1212 *
1213 * IOW, we should satisfy the following cases:
1214 *
1215 * 1) length <= maxpacket
1216 * - DATA0
1217 *
1218 * 2) maxpacket < length <= (2 * maxpacket)
1219 * - DATA1, DATA0
1220 *
1221 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1222 * - DATA2, DATA1, DATA0
1223 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001224 if (speed == USB_SPEED_HIGH) {
1225 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +05301226 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +05301227 unsigned int maxp = usb_endpoint_maxp(ep->desc);
1228
1229 if (length <= (2 * maxp))
1230 mult--;
1231
1232 if (length <= maxp)
1233 mult--;
1234
1235 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001236 }
1237 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301238 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001239 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001240
1241 /* always enable Interrupt on Missed ISOC */
1242 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001243 break;
1244
1245 case USB_ENDPOINT_XFER_BULK:
1246 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001247 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001248 break;
1249 default:
1250 /*
1251 * This is only possible with faulty memory because we
1252 * checked it already :)
1253 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001254 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1255 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001256 }
1257
Tejas Joglekar244add82018-12-10 16:08:13 +05301258 /*
1259 * Enable Continue on Short Packet
1260 * when endpoint is not a stream capable
1261 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001262 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301263 if (!dep->stream_capable)
1264 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001265
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001266 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001267 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1268 }
1269
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001270 if ((!no_interrupt && !chain) || must_interrupt)
Felipe Balbic9508c82016-10-05 14:26:23 +03001271 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001272
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301273 if (chain)
1274 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001275 else if (dep->stream_capable && is_last)
1276 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301277
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001278 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001279 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001280
1281 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001282
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301283 dwc3_ep_inc_enq(dep);
1284
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001285 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001286}
1287
John Youn361572b2016-05-19 17:26:17 -07001288/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001289 * dwc3_prepare_one_trb - setup one TRB from one request
1290 * @dep: endpoint for which this request is prepared
1291 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001292 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001293 * @chain: should this TRB be chained to the next?
1294 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001295 * @use_bounce_buffer: set to use bounce buffer
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001296 * @must_interrupt: set to interrupt on TRB completion
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001297 */
1298static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001299 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001300 unsigned int chain, unsigned int node, bool use_bounce_buffer,
1301 bool must_interrupt)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001302{
1303 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301304 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001305 unsigned int stream_id = req->request.stream_id;
1306 unsigned int short_not_ok = req->request.short_not_ok;
1307 unsigned int no_interrupt = req->request.no_interrupt;
1308 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301309
Thinh Nguyen2b803572020-09-24 01:21:30 -07001310 if (use_bounce_buffer)
1311 dma = dep->dwc->bounce_addr;
1312 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301313 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001314 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301315 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001316
1317 trb = &dep->trb_pool[dep->trb_enqueue];
1318
1319 if (!req->trb) {
1320 dwc3_gadget_move_started_request(req);
1321 req->trb = trb;
1322 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001323 }
1324
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001325 req->num_trbs++;
1326
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001327 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001328 stream_id, short_not_ok, no_interrupt, is_last,
1329 must_interrupt);
1330}
1331
1332static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1333{
1334 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1335 unsigned int rem = req->request.length % maxp;
1336
1337 if ((req->request.length && req->request.zero && !rem &&
1338 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1339 (!req->direction && rem))
1340 return true;
1341
1342 return false;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001343}
1344
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001345/**
1346 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1347 * @dep: The endpoint that the request belongs to
1348 * @req: The request to prepare
1349 * @entry_length: The last SG entry size
1350 * @node: Indicates whether this is not the first entry (for isoc only)
1351 *
1352 * Return the number of TRBs prepared.
1353 */
1354static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1355 struct dwc3_request *req, unsigned int entry_length,
1356 unsigned int node)
1357{
1358 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1359 unsigned int rem = req->request.length % maxp;
1360 unsigned int num_trbs = 1;
1361
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001362 if (dwc3_needs_extra_trb(dep, req))
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001363 num_trbs++;
1364
1365 if (dwc3_calc_trbs_left(dep) < num_trbs)
1366 return 0;
1367
1368 req->needs_extra_trb = num_trbs > 1;
1369
1370 /* Prepare a normal TRB */
1371 if (req->direction || req->request.length)
1372 dwc3_prepare_one_trb(dep, req, entry_length,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001373 req->needs_extra_trb, node, false, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001374
1375 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1376 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1377 dwc3_prepare_one_trb(dep, req,
1378 req->direction ? 0 : maxp - rem,
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001379 false, 1, true, false);
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001380
1381 return num_trbs;
1382}
1383
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001384static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001385 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001386{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301387 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001388 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001389 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001390 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301391 unsigned int remaining = req->request.num_mapped_sgs
1392 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001393 unsigned int num_trbs = req->num_trbs;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001394 bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301395
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001396 /*
1397 * If we resume preparing the request, then get the remaining length of
1398 * the request and resume where we left off.
1399 */
1400 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1401 length -= sg_dma_len(s);
1402
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301403 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001404 unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001405 unsigned int trb_length;
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001406 bool must_interrupt = false;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001407 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001408
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001409 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1410
1411 length -= trb_length;
1412
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001413 /*
1414 * IOMMU driver is coalescing the list of sgs which shares a
1415 * page boundary into one and giving it to USB driver. With
1416 * this the number of sgs mapped is not equal to the number of
1417 * sgs passed. So mark the chain bit to false if it isthe last
1418 * mapped sg.
1419 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001420 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001421 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001422
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001423 if (!num_trbs_left)
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001424 break;
1425
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001426 if (last_sg) {
1427 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001428 break;
Felipe Balbic6267a52017-01-05 14:58:46 +02001429 } else {
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001430 /*
1431 * Look ahead to check if we have enough TRBs for the
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001432 * next SG entry. If not, set interrupt on this TRB to
1433 * resume preparing the next SG entry when more TRBs are
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001434 * free.
1435 */
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001436 if (num_trbs_left == 1 || (needs_extra_trb &&
1437 num_trbs_left <= 2 &&
1438 sg_dma_len(sg_next(s)) >= length))
Thinh Nguyenf9cc5812020-09-30 17:44:19 -07001439 must_interrupt = true;
1440
1441 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1442 must_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001443 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001444
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301445 /*
1446 * There can be a situation where all sgs in sglist are not
1447 * queued because of insufficient trb number. To handle this
1448 * case, update start_sg to next sg to be queued, so that
1449 * we have free trbs we can continue queuing from where we
1450 * previously stopped
1451 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001452 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301453 req->start_sg = sg_next(s);
1454
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301455 req->num_queued_sgs++;
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07001456 req->num_pending_sgs--;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301457
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001458 /*
1459 * The number of pending SG entries may not correspond to the
1460 * number of mapped SG entries. If all the data are queued, then
1461 * don't include unused SG entries.
1462 */
1463 if (length == 0) {
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07001464 req->num_pending_sgs = 0;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001465 break;
1466 }
1467
Thinh Nguyen8dbbe482020-09-30 17:44:25 -07001468 if (must_interrupt)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001469 break;
1470 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001471
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001472 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001473}
1474
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001475static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001476 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001477{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001478 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001479}
1480
Felipe Balbi72246da2011-08-19 18:10:58 +03001481/*
1482 * dwc3_prepare_trbs - setup TRBs from requests
1483 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001484 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001485 * The function goes through the requests list and sets up TRBs for the
1486 * transfers. The function returns once there are no more TRBs available or
1487 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001488 *
1489 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001490 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001491static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001492{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001493 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001494 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001495
1496 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1497
Felipe Balbid86c5a62016-10-25 13:48:52 +03001498 /*
1499 * We can get in a situation where there's a request in the started list
1500 * but there weren't enough TRBs to fully kick it in the first time
1501 * around, so it has been waiting for more TRBs to be freed up.
1502 *
1503 * In that case, we should check if we have a request with pending_sgs
1504 * in the started list and prepare TRBs for that request first,
1505 * otherwise we will prepare TRBs completely out of order and that will
1506 * break things.
1507 */
1508 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001509 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001510 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001511 if (!ret || req->num_pending_sgs)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001512 return ret;
1513 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001514
1515 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001516 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001517
1518 /*
1519 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1520 * burst capability may try to read and use TRBs beyond the
1521 * active transfer instead of stopping.
1522 */
1523 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001524 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001525 }
1526
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001527 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001528 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001529
1530 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1531 dep->direction);
1532 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001533 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001534
1535 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301536 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301537 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001538 req->num_pending_sgs = req->request.num_mapped_sgs;
1539
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001540 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001541 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001542 if (req->num_pending_sgs)
1543 return ret;
1544 } else {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001545 ret = dwc3_prepare_trbs_linear(dep, req);
Thinh Nguyen346a15c2020-09-30 17:44:32 -07001546 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001547
Thinh Nguyen490410b2020-09-24 01:21:55 -07001548 if (!ret || !dwc3_calc_trbs_left(dep))
1549 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001550
1551 /*
1552 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1553 * burst capability may try to read and use TRBs beyond the
1554 * active transfer instead of stopping.
1555 */
1556 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001557 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001559
1560 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001561}
1562
Thinh Nguyen8d990872020-03-29 16:12:57 -07001563static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1564
Felipe Balbi7fdca762017-09-05 14:41:34 +03001565static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001566{
1567 struct dwc3_gadget_ep_cmd_params params;
1568 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001569 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001570 int ret;
1571 u32 cmd;
1572
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001573 /*
1574 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1575 * This happens when we need to stop and restart a transfer such as in
1576 * the case of reinitiating a stream or retrying an isoc transfer.
1577 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001578 ret = dwc3_prepare_trbs(dep);
Thinh Nguyend72ecc02020-09-29 00:18:48 -07001579 if (ret < 0)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001580 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001581
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001582 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001583
Thinh Nguyen23384842020-09-30 17:44:38 -07001584 /*
1585 * If there's no new TRB prepared and we don't need to restart a
1586 * transfer, there's no need to update the transfer.
1587 */
1588 if (!ret && !starting)
1589 return ret;
1590
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001591 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 if (!req) {
1593 dep->flags |= DWC3_EP_PENDING_REQUEST;
1594 return 0;
1595 }
1596
1597 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001598
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001599 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301600 params.param0 = upper_32_bits(req->trb_dma);
1601 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001602 cmd = DWC3_DEPCMD_STARTTRANSFER;
1603
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301604 if (dep->stream_capable)
1605 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1606
Felipe Balbi7fdca762017-09-05 14:41:34 +03001607 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1608 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301609 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001610 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1611 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301612 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001613
Felipe Balbi2cd47182016-04-12 16:42:43 +03001614 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001616 struct dwc3_request *tmp;
1617
1618 if (ret == -EAGAIN)
1619 return ret;
1620
1621 dwc3_stop_active_transfer(dep, true, true);
1622
1623 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08001624 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
Thinh Nguyen8d990872020-03-29 16:12:57 -07001625
1626 /* If ep isn't started, then there's no end transfer pending */
1627 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1628 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1629
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 return ret;
1631 }
1632
Thinh Nguyene0d19562020-05-05 19:46:57 -07001633 if (dep->stream_capable && req->request.is_last)
1634 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1635
Felipe Balbi72246da2011-08-19 18:10:58 +03001636 return 0;
1637}
1638
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001639static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1640{
1641 u32 reg;
1642
1643 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1644 return DWC3_DSTS_SOFFN(reg);
1645}
1646
Thinh Nguyend92021f2018-11-14 22:56:54 -08001647/**
1648 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1649 * @dep: isoc endpoint
1650 *
1651 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1652 * microframe number reported by the XferNotReady event for the future frame
1653 * number to start the isoc transfer.
1654 *
1655 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1656 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1657 * XferNotReady event are invalid. The driver uses this number to schedule the
1658 * isochronous transfer and passes it to the START TRANSFER command. Because
1659 * this number is invalid, the command may fail. If BIT[15:14] matches the
1660 * internal 16-bit microframe, the START TRANSFER command will pass and the
1661 * transfer will start at the scheduled time, if it is off by 1, the command
1662 * will still pass, but the transfer will start 2 seconds in the future. For all
1663 * other conditions, the START TRANSFER command will fail with bus-expiry.
1664 *
1665 * In order to workaround this issue, we can test for the correct combination of
1666 * BIT[15:14] by sending START TRANSFER commands with different values of
1667 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1668 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1669 * As the result, within the 4 possible combinations for BIT[15:14], there will
1670 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1671 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1672 * value is the correct combination.
1673 *
1674 * Since there are only 4 outcomes and the results are ordered, we can simply
1675 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1676 * deduce the smaller successful combination.
1677 *
1678 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1679 * of BIT[15:14]. The correct combination is as follow:
1680 *
1681 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1682 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1683 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1684 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1685 *
1686 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1687 * endpoints.
1688 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001689static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301690{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001691 int cmd_status = 0;
1692 bool test0;
1693 bool test1;
1694
1695 while (dep->combo_num < 2) {
1696 struct dwc3_gadget_ep_cmd_params params;
1697 u32 test_frame_number;
1698 u32 cmd;
1699
1700 /*
1701 * Check if we can start isoc transfer on the next interval or
1702 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1703 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001704 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001705 test_frame_number |= dep->combo_num << 14;
1706 test_frame_number += max_t(u32, 4, dep->interval);
1707
1708 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1709 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1710
1711 cmd = DWC3_DEPCMD_STARTTRANSFER;
1712 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1713 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1714
1715 /* Redo if some other failure beside bus-expiry is received */
1716 if (cmd_status && cmd_status != -EAGAIN) {
1717 dep->start_cmd_status = 0;
1718 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001719 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001720 }
1721
1722 /* Store the first test status */
1723 if (dep->combo_num == 0)
1724 dep->start_cmd_status = cmd_status;
1725
1726 dep->combo_num++;
1727
1728 /*
1729 * End the transfer if the START_TRANSFER command is successful
1730 * to wait for the next XferNotReady to test the command again
1731 */
1732 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001733 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001734 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001735 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301736 }
1737
Thinh Nguyend92021f2018-11-14 22:56:54 -08001738 /* test0 and test1 are both completed at this point */
1739 test0 = (dep->start_cmd_status == 0);
1740 test1 = (cmd_status == 0);
1741
1742 if (!test0 && test1)
1743 dep->combo_num = 1;
1744 else if (!test0 && !test1)
1745 dep->combo_num = 2;
1746 else if (test0 && !test1)
1747 dep->combo_num = 3;
1748 else if (test0 && test1)
1749 dep->combo_num = 0;
1750
Michael Grzeschikca143782020-07-01 20:24:51 +02001751 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001752 dep->frame_number |= dep->combo_num << 14;
1753 dep->frame_number += max_t(u32, 4, dep->interval);
1754
1755 /* Reinitialize test variables */
1756 dep->start_cmd_status = 0;
1757 dep->combo_num = 0;
1758
Felipe Balbi25abad62018-08-14 10:41:19 +03001759 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001760}
1761
Felipe Balbi25abad62018-08-14 10:41:19 +03001762static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301763{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001764 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001765 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001766 int ret;
1767 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001768
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001769 if (list_empty(&dep->pending_list) &&
1770 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301771 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001772 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301773 }
1774
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001775 if (!dwc->dis_start_transfer_quirk &&
1776 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1777 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001778 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001779 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001780 }
1781
Michael Olbrichc5a70922020-07-01 20:24:52 +02001782 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001783 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001784 u32 frame = __dwc3_gadget_get_frame(dwc);
1785 bool rollover = frame <
1786 (dep->frame_number & DWC3_FRNUMBER_MASK);
1787
1788 /*
1789 * frame_number is set from XferNotReady and may be already
1790 * out of date. DSTS only provides the lower 14 bit of the
1791 * current frame number. So add the upper two bits of
1792 * frame_number and handle a possible rollover.
1793 * This will provide the correct frame_number unless more than
1794 * rollover has happened since XferNotReady.
1795 */
1796
1797 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1798 frame;
1799 if (rollover)
1800 dep->frame_number += BIT(14);
1801 }
1802
Felipe Balbid5370102018-08-14 10:42:43 +03001803 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1804 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1805
1806 ret = __dwc3_gadget_kick_transfer(dep);
1807 if (ret != -EAGAIN)
1808 break;
1809 }
1810
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001811 /*
1812 * After a number of unsuccessful start attempts due to bus-expiry
1813 * status, issue END_TRANSFER command and retry on the next XferNotReady
1814 * event.
1815 */
1816 if (ret == -EAGAIN) {
1817 struct dwc3_gadget_ep_cmd_params params;
1818 u32 cmd;
1819
1820 cmd = DWC3_DEPCMD_ENDTRANSFER |
1821 DWC3_DEPCMD_CMDIOC |
1822 DWC3_DEPCMD_PARAM(dep->resource_index);
1823
1824 dep->resource_index = 0;
1825 memset(&params, 0, sizeof(params));
1826
1827 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1828 if (!ret)
1829 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1830 }
1831
Felipe Balbid5370102018-08-14 10:42:43 +03001832 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301833}
1834
Felipe Balbi72246da2011-08-19 18:10:58 +03001835static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1836{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001837 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001838
Wesley Chengf09ddcf2021-03-11 15:59:02 -08001839 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
Wesley Chengb851f7c2021-10-18 12:26:47 -07001840 dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001841 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001842 return -ESHUTDOWN;
1843 }
1844
Felipe Balbi04fb3652017-05-17 15:57:45 +03001845 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1846 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001847 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001848
Felipe Balbib2b6d602019-01-11 12:58:52 +02001849 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1850 "%s: request %pK already in flight\n",
1851 dep->name, &req->request))
1852 return -EINVAL;
1853
Felipe Balbifc8bb912016-05-16 13:14:48 +03001854 pm_runtime_get(dwc->dev);
1855
Felipe Balbi72246da2011-08-19 18:10:58 +03001856 req->request.actual = 0;
1857 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001858
Felipe Balbife84f522015-09-01 09:01:38 -05001859 trace_dwc3_ep_queue(req);
1860
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001861 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001862 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001863
Thinh Nguyene0d19562020-05-05 19:46:57 -07001864 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1865 return 0;
1866
Thinh Nguyenc5036722020-09-02 18:42:58 -07001867 /*
1868 * Start the transfer only after the END_TRANSFER is completed
1869 * and endpoint STALL is cleared.
1870 */
1871 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1872 (dep->flags & DWC3_EP_WEDGE) ||
1873 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001874 dep->flags |= DWC3_EP_DELAY_START;
1875 return 0;
1876 }
1877
Felipe Balbid889c232016-09-29 15:44:29 +03001878 /*
1879 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1880 * wait for a XferNotReady event so we will know what's the current
1881 * (micro-)frame number.
1882 *
1883 * Without this trick, we are very, very likely gonna get Bus Expiry
1884 * errors which will force us issue EndTransfer command.
1885 */
1886 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001887 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1888 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001889 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001890
1891 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001892 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001893 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001894 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001895 }
1896
Wesley Cheng18ffa982021-05-07 10:55:19 -07001897 __dwc3_gadget_kick_transfer(dep);
1898
1899 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001900}
1901
1902static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1903 gfp_t gfp_flags)
1904{
1905 struct dwc3_request *req = to_dwc3_request(request);
1906 struct dwc3_ep *dep = to_dwc3_ep(ep);
1907 struct dwc3 *dwc = dep->dwc;
1908
1909 unsigned long flags;
1910
1911 int ret;
1912
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001913 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001914 ret = __dwc3_gadget_ep_queue(dep, req);
1915 spin_unlock_irqrestore(&dwc->lock, flags);
1916
1917 return ret;
1918}
1919
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001920static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1921{
1922 int i;
1923
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001924 /* If req->trb is not set, then the request has not started */
1925 if (!req->trb)
1926 return;
1927
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001928 /*
1929 * If request was already started, this means we had to
1930 * stop the transfer. With that we also need to ignore
1931 * all TRBs used by the request, however TRBs can only
1932 * be modified after completion of END_TRANSFER
1933 * command. So what we do here is that we wait for
1934 * END_TRANSFER completion and only after that, we jump
1935 * over TRBs by clearing HWO and incrementing dequeue
1936 * pointer.
1937 */
1938 for (i = 0; i < req->num_trbs; i++) {
1939 struct dwc3_trb *trb;
1940
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001941 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001942 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1943 dwc3_ep_inc_deq(dep);
1944 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001945
1946 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001947}
1948
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001949static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1950{
1951 struct dwc3_request *req;
1952 struct dwc3_request *tmp;
Ray Chi04dd6e72021-03-28 02:17:42 +08001953 struct dwc3 *dwc = dep->dwc;
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001954
Greg Kroah-Hartman664cc972021-08-10 09:10:15 +02001955 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001956 dwc3_gadget_ep_skip_trbs(dep, req);
Ray Chi04dd6e72021-03-28 02:17:42 +08001957 switch (req->status) {
1958 case DWC3_REQUEST_STATUS_DISCONNECTED:
1959 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
1960 break;
1961 case DWC3_REQUEST_STATUS_DEQUEUED:
1962 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1963 break;
1964 case DWC3_REQUEST_STATUS_STALLED:
1965 dwc3_gadget_giveback(dep, req, -EPIPE);
1966 break;
1967 default:
1968 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
1969 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1970 break;
1971 }
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001972 }
1973}
1974
Felipe Balbi72246da2011-08-19 18:10:58 +03001975static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1976 struct usb_request *request)
1977{
1978 struct dwc3_request *req = to_dwc3_request(request);
1979 struct dwc3_request *r = NULL;
1980
1981 struct dwc3_ep *dep = to_dwc3_ep(ep);
1982 struct dwc3 *dwc = dep->dwc;
1983
1984 unsigned long flags;
1985 int ret = 0;
1986
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001987 trace_dwc3_ep_dequeue(req);
1988
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 spin_lock_irqsave(&dwc->lock, flags);
1990
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001991 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001992 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001993 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001994 }
1995
Felipe Balbi72246da2011-08-19 18:10:58 +03001996 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001997 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001998 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1999 goto out;
2000 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002001 }
2002
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08002003 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002004 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08002005 struct dwc3_request *t;
2006
Felipe Balbi72246da2011-08-19 18:10:58 +03002007 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02002008 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02002009
Thinh Nguyena7027ca2020-03-05 13:24:08 -08002010 /*
2011 * Remove any started request if the transfer is
2012 * cancelled.
2013 */
2014 list_for_each_entry_safe(r, t, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08002015 dwc3_gadget_move_cancelled_request(r,
2016 DWC3_REQUEST_STATUS_DEQUEUED);
Felipe Balbicf3113d2017-02-17 11:12:44 +02002017
Thinh Nguyena5c76822021-01-04 22:42:39 -08002018 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2019
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08002020 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03002021 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002022 }
2023
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08002024 dev_err(dwc->dev, "request %pK was not queued to %s\n",
2025 request, ep->name);
2026 ret = -EINVAL;
2027out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002028 spin_unlock_irqrestore(&dwc->lock, flags);
2029
2030 return ret;
2031}
2032
Felipe Balbi7a608552014-09-24 14:19:52 -05002033int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03002034{
2035 struct dwc3_gadget_ep_cmd_params params;
2036 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08002037 struct dwc3_request *req;
2038 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002039 int ret;
2040
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05002041 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2042 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
2043 return -EINVAL;
2044 }
2045
Felipe Balbi72246da2011-08-19 18:10:58 +03002046 memset(&params, 0x00, sizeof(params));
2047
2048 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03002049 struct dwc3_trb *trb;
2050
Felipe Balbie319bd62020-08-13 08:35:38 +03002051 unsigned int transfer_in_flight;
2052 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03002053
2054 if (dep->number > 1)
2055 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
2056 else
2057 trb = &dwc->ep0_trb[dep->trb_enqueue];
2058
2059 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
2060 started = !list_empty(&dep->started_list);
2061
2062 if (!protocol && ((dep->direction && transfer_in_flight) ||
2063 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05002064 return -EAGAIN;
2065 }
2066
Felipe Balbi2cd47182016-04-12 16:42:43 +03002067 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
2068 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03002069 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03002070 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002071 dep->name);
2072 else
2073 dep->flags |= DWC3_EP_STALL;
2074 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08002075 /*
2076 * Don't issue CLEAR_STALL command to control endpoints. The
2077 * controller automatically clears the STALL when it receives
2078 * the SETUP token.
2079 */
2080 if (dep->number <= 1) {
2081 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2082 return 0;
2083 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03002084
Thinh Nguyend97c78a2020-09-02 18:43:04 -07002085 dwc3_stop_active_transfer(dep, true, true);
2086
2087 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
Ray Chi04dd6e72021-03-28 02:17:42 +08002088 dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
Thinh Nguyend97c78a2020-09-02 18:43:04 -07002089
2090 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
2091 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
2092 return 0;
2093 }
2094
2095 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2096
John Youn50c763f2016-05-31 17:49:56 -07002097 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08002098 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03002099 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002100 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08002101 return ret;
2102 }
2103
2104 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2105
Thinh Nguyenc5036722020-09-02 18:42:58 -07002106 if ((dep->flags & DWC3_EP_DELAY_START) &&
2107 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2108 __dwc3_gadget_kick_transfer(dep);
2109
2110 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03002111 }
Paul Zimmerman52754552011-09-30 10:58:44 +03002112
Felipe Balbi72246da2011-08-19 18:10:58 +03002113 return ret;
2114}
2115
2116static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2117{
2118 struct dwc3_ep *dep = to_dwc3_ep(ep);
2119 struct dwc3 *dwc = dep->dwc;
2120
2121 unsigned long flags;
2122
2123 int ret;
2124
2125 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05002126 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002127 spin_unlock_irqrestore(&dwc->lock, flags);
2128
2129 return ret;
2130}
2131
2132static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
2133{
2134 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002135 struct dwc3 *dwc = dep->dwc;
2136 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05002137 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002138
Paul Zimmerman249a4562012-02-24 17:32:16 -08002139 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002140 dep->flags |= DWC3_EP_WEDGE;
2141
Pratyush Anand08f0d962012-06-25 22:40:43 +05302142 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05002143 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05302144 else
Felipe Balbi7a608552014-09-24 14:19:52 -05002145 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05002146 spin_unlock_irqrestore(&dwc->lock, flags);
2147
2148 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002149}
2150
2151/* -------------------------------------------------------------------------- */
2152
2153static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
2154 .bLength = USB_DT_ENDPOINT_SIZE,
2155 .bDescriptorType = USB_DT_ENDPOINT,
2156 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
2157};
2158
2159static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
2160 .enable = dwc3_gadget_ep0_enable,
2161 .disable = dwc3_gadget_ep0_disable,
2162 .alloc_request = dwc3_gadget_ep_alloc_request,
2163 .free_request = dwc3_gadget_ep_free_request,
2164 .queue = dwc3_gadget_ep0_queue,
2165 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05302166 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03002167 .set_wedge = dwc3_gadget_ep_set_wedge,
2168};
2169
2170static const struct usb_ep_ops dwc3_gadget_ep_ops = {
2171 .enable = dwc3_gadget_ep_enable,
2172 .disable = dwc3_gadget_ep_disable,
2173 .alloc_request = dwc3_gadget_ep_alloc_request,
2174 .free_request = dwc3_gadget_ep_free_request,
2175 .queue = dwc3_gadget_ep_queue,
2176 .dequeue = dwc3_gadget_ep_dequeue,
2177 .set_halt = dwc3_gadget_ep_set_halt,
2178 .set_wedge = dwc3_gadget_ep_set_wedge,
2179};
2180
2181/* -------------------------------------------------------------------------- */
2182
2183static int dwc3_gadget_get_frame(struct usb_gadget *g)
2184{
2185 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03002186
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03002187 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002188}
2189
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002190static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002191{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002192 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03002193
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002194 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 u32 reg;
2196
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03002198
Felipe Balbi72246da2011-08-19 18:10:58 +03002199 /*
2200 * According to the Databook Remote wakeup request should
2201 * be issued only when the device is in early suspend state.
2202 *
2203 * We can check that via USB Link State bits in DSTS register.
2204 */
2205 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2206
Felipe Balbi72246da2011-08-19 18:10:58 +03002207 link_state = DWC3_DSTS_USBLNKST(reg);
2208
2209 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08002210 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03002211 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
2212 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyenc560e762021-04-19 19:11:12 -07002213 case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
2214 case DWC3_LINK_STATE_U1:
Thinh Nguyend0550cd2020-01-31 16:25:50 -08002215 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03002216 break;
2217 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002218 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002219 }
2220
Felipe Balbi8598bde2012-01-02 18:55:57 +02002221 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2222 if (ret < 0) {
2223 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002224 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02002225 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002226
Paul Zimmerman802fde92012-04-27 13:10:52 +03002227 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002228 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002229 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03002230 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002231 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2232 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2233 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002234
Paul Zimmerman1d046792012-02-15 18:56:56 -08002235 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002236 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03002237
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01002238 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002239 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2240
2241 /* in HS, means ON */
2242 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2243 break;
2244 }
2245
2246 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2247 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002248 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002249 }
2250
Felipe Balbi218ef7b2016-04-04 11:24:04 +03002251 return 0;
2252}
2253
2254static int dwc3_gadget_wakeup(struct usb_gadget *g)
2255{
2256 struct dwc3 *dwc = gadget_to_dwc(g);
2257 unsigned long flags;
2258 int ret;
2259
2260 spin_lock_irqsave(&dwc->lock, flags);
2261 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002262 spin_unlock_irqrestore(&dwc->lock, flags);
2263
2264 return ret;
2265}
2266
2267static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2268 int is_selfpowered)
2269{
2270 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08002271 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03002272
Paul Zimmerman249a4562012-02-24 17:32:16 -08002273 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08002274 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08002275 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002276
2277 return 0;
2278}
2279
Wesley Chengae7e8612020-09-28 17:20:59 -07002280static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2281{
2282 u32 epnum;
2283
2284 for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2285 struct dwc3_ep *dep;
2286
2287 dep = dwc->eps[epnum];
2288 if (!dep)
2289 continue;
2290
2291 dwc3_remove_requests(dwc, dep);
2292 }
2293}
2294
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002295static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2296{
2297 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2298 u32 reg;
2299
2300 if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2301 ssp_rate = dwc->max_ssp_rate;
2302
2303 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2304 reg &= ~DWC3_DCFG_SPEED_MASK;
2305 reg &= ~DWC3_DCFG_NUMLANES(~0);
2306
2307 if (ssp_rate == USB_SSP_GEN_1x2)
2308 reg |= DWC3_DCFG_SUPERSPEED;
2309 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2310 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2311
2312 if (ssp_rate != USB_SSP_GEN_2x1 &&
2313 dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2314 reg |= DWC3_DCFG_NUMLANES(1);
2315
2316 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2317}
2318
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002319static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2320{
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002321 enum usb_device_speed speed;
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002322 u32 reg;
2323
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002324 speed = dwc->gadget_max_speed;
Thinh Nguyen93f1d432021-03-08 18:16:50 -08002325 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002326 speed = dwc->maximum_speed;
2327
2328 if (speed == USB_SPEED_SUPER_PLUS &&
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002329 DWC3_IP_IS(DWC32)) {
2330 __dwc3_gadget_set_ssp_rate(dwc);
2331 return;
2332 }
2333
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002334 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2335 reg &= ~(DWC3_DCFG_SPEED_MASK);
2336
2337 /*
2338 * WORKAROUND: DWC3 revision < 2.20a have an issue
2339 * which would cause metastability state on Run/Stop
2340 * bit if we try to force the IP to USB2-only mode.
2341 *
2342 * Because of that, we cannot configure the IP to any
2343 * speed other than the SuperSpeed
2344 *
2345 * Refers to:
2346 *
2347 * STAR#9000525659: Clock Domain Crossing on DCTL in
2348 * USB 2.0 Mode
2349 */
2350 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2351 !dwc->dis_metastability_quirk) {
2352 reg |= DWC3_DCFG_SUPERSPEED;
2353 } else {
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002354 switch (speed) {
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002355 case USB_SPEED_FULL:
2356 reg |= DWC3_DCFG_FULLSPEED;
2357 break;
2358 case USB_SPEED_HIGH:
2359 reg |= DWC3_DCFG_HIGHSPEED;
2360 break;
2361 case USB_SPEED_SUPER:
2362 reg |= DWC3_DCFG_SUPERSPEED;
2363 break;
2364 case USB_SPEED_SUPER_PLUS:
2365 if (DWC3_IP_IS(DWC3))
2366 reg |= DWC3_DCFG_SUPERSPEED;
2367 else
2368 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2369 break;
2370 default:
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002371 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002372
2373 if (DWC3_IP_IS(DWC3))
2374 reg |= DWC3_DCFG_SUPERSPEED;
2375 else
2376 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2377 }
2378 }
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002379
2380 if (DWC3_IP_IS(DWC32) &&
Thinh Nguyen450b9e92021-01-19 17:36:40 -08002381 speed > USB_SPEED_UNKNOWN &&
2382 speed < USB_SPEED_SUPER_PLUS)
Thinh Nguyenf551037c2021-01-19 17:36:34 -08002383 reg &= ~DWC3_DCFG_NUMLANES(~0);
2384
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002385 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2386}
2387
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002388static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002389{
2390 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002391 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002392
Felipe Balbifc8bb912016-05-16 13:14:48 +03002393 if (pm_runtime_suspended(dwc->dev))
2394 return 0;
2395
Felipe Balbi72246da2011-08-19 18:10:58 +03002396 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002397 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002398 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002399 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2400 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2401 }
2402
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002403 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002404 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2405 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002406
2407 if (dwc->has_hibernation)
2408 reg |= DWC3_DCTL_KEEP_CONNECT;
2409
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002410 __dwc3_gadget_set_speed(dwc);
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002411 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002412 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002413 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002414
2415 if (dwc->has_hibernation && !suspend)
2416 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2417
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002418 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002419 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002420
Thinh Nguyen5b738212019-10-23 19:15:43 -07002421 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002422
2423 do {
2424 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002425 reg &= DWC3_DSTS_DEVCTRLHLT;
2426 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002427
2428 if (!timeout)
2429 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002430
Pratyush Anand6f17f742012-07-02 10:21:55 +05302431 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002432}
2433
Wesley Chengae7e8612020-09-28 17:20:59 -07002434static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2435static void __dwc3_gadget_stop(struct dwc3 *dwc);
Wesley Chenga1383b32020-12-29 15:00:37 -08002436static int __dwc3_gadget_start(struct dwc3 *dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002437
Felipe Balbi72246da2011-08-19 18:10:58 +03002438static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2439{
2440 struct dwc3 *dwc = gadget_to_dwc(g);
2441 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302442 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002443
2444 is_on = !!is_on;
Wesley Cheng8217f072021-09-16 19:18:52 -07002445 dwc->softconnect = is_on;
Baolin Wangbb014732016-10-14 17:11:33 +08002446 /*
2447 * Per databook, when we want to stop the gadget, if a control transfer
2448 * is still in process, complete it and get the core into setup phase.
2449 */
2450 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2451 reinit_completion(&dwc->ep0_in_setup);
2452
2453 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2454 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
Wesley Cheng4a1e25c2021-08-24 21:28:55 -07002455 if (ret == 0)
2456 dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
Baolin Wangbb014732016-10-14 17:11:33 +08002457 }
2458
Wesley Chengae7e8612020-09-28 17:20:59 -07002459 /*
Wesley Chengcb10f682021-08-03 23:24:05 -07002460 * Avoid issuing a runtime resume if the device is already in the
2461 * suspended state during gadget disconnect. DWC3 gadget was already
2462 * halted/stopped during runtime suspend.
2463 */
2464 if (!is_on) {
2465 pm_runtime_barrier(dwc->dev);
2466 if (pm_runtime_suspended(dwc->dev))
2467 return 0;
2468 }
2469
2470 /*
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002471 * Check the return value for successful resume, or error. For a
2472 * successful resume, the DWC3 runtime PM resume routine will handle
2473 * the run stop sequence, so avoid duplicate operations here.
2474 */
2475 ret = pm_runtime_get_sync(dwc->dev);
2476 if (!ret || ret < 0) {
2477 pm_runtime_put(dwc->dev);
2478 return 0;
2479 }
2480
2481 /*
Wesley Cheng82129372021-05-20 21:23:57 -07002482 * Synchronize and disable any further event handling while controller
2483 * is being enabled/disabled.
Wesley Chengae7e8612020-09-28 17:20:59 -07002484 */
Wesley Cheng82129372021-05-20 21:23:57 -07002485 disable_irq(dwc->irq_gadget);
Wesley Chengae7e8612020-09-28 17:20:59 -07002486
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 spin_lock_irqsave(&dwc->lock, flags);
Wesley Chengae7e8612020-09-28 17:20:59 -07002488
2489 if (!is_on) {
2490 u32 count;
2491
Wesley Chengf09ddcf2021-03-11 15:59:02 -08002492 dwc->connected = false;
Wesley Chengae7e8612020-09-28 17:20:59 -07002493 /*
2494 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2495 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2496 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2497 * command for any active transfers" before clearing the RunStop
2498 * bit.
2499 */
2500 dwc3_stop_active_transfers(dwc);
2501 __dwc3_gadget_stop(dwc);
2502
2503 /*
2504 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
2505 * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the
2506 * "software needs to acknowledge the events that are generated
2507 * (by writing to GEVNTCOUNTn) while it is waiting for this bit
2508 * to be set to '1'."
2509 */
2510 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
2511 count &= DWC3_GEVNTCOUNT_MASK;
2512 if (count > 0) {
2513 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
2514 dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) %
2515 dwc->ev_buf->length;
2516 }
Wesley Chenga1383b32020-12-29 15:00:37 -08002517 } else {
2518 __dwc3_gadget_start(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07002519 }
2520
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002521 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002522 spin_unlock_irqrestore(&dwc->lock, flags);
Wesley Cheng82129372021-05-20 21:23:57 -07002523 enable_irq(dwc->irq_gadget);
2524
Wesley Cheng77adb8b2020-12-29 15:05:35 -08002525 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002526
Pratyush Anand6f17f742012-07-02 10:21:55 +05302527 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002528}
2529
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002530static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2531{
2532 u32 reg;
2533
2534 /* Enable all but Start and End of Frame IRQs */
Thinh Nguyen132ee0d2021-01-13 19:55:29 -08002535 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002536 DWC3_DEVTEN_CMDCMPLTEN |
2537 DWC3_DEVTEN_ERRTICERREN |
2538 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002539 DWC3_DEVTEN_CONNECTDONEEN |
2540 DWC3_DEVTEN_USBRSTEN |
2541 DWC3_DEVTEN_DISCONNEVTEN);
2542
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002543 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002544 reg |= DWC3_DEVTEN_ULSTCNGEN;
2545
Jack Phamd1d90dd2021-04-28 02:01:10 -07002546 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2547 if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
Jack Pham6f26ebb2021-04-28 02:01:11 -07002548 reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
Jack Phamd1d90dd2021-04-28 02:01:10 -07002549
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002550 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2551}
2552
2553static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2554{
2555 /* mask all interrupts */
2556 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2557}
2558
2559static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002560static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002561
Felipe Balbi4e994722016-05-13 14:09:59 +03002562/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002563 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2564 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002565 *
2566 * The following looks like complex but it's actually very simple. In order to
2567 * calculate the number of packets we can burst at once on OUT transfers, we're
2568 * gonna use RxFIFO size.
2569 *
2570 * To calculate RxFIFO size we need two numbers:
2571 * MDWIDTH = size, in bits, of the internal memory bus
2572 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2573 *
2574 * Given these two numbers, the formula is simple:
2575 *
2576 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2577 *
2578 * 24 bytes is for 3x SETUP packets
2579 * 16 bytes is a clock domain crossing tolerance
2580 *
2581 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2582 */
2583static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2584{
2585 u32 ram2_depth;
2586 u32 mdwidth;
2587 u32 nump;
2588 u32 reg;
2589
2590 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
Thinh Nguyend00be772021-03-27 17:54:01 -07002591 mdwidth = dwc3_mdwidth(dwc);
Felipe Balbi4e994722016-05-13 14:09:59 +03002592
2593 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2594 nump = min_t(u32, nump, 16);
2595
2596 /* update NumP */
2597 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2598 reg &= ~DWC3_DCFG_NUMP_MASK;
2599 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2600 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2601}
2602
Felipe Balbid7be2952016-05-04 15:49:37 +03002603static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002604{
Felipe Balbi72246da2011-08-19 18:10:58 +03002605 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002606 int ret = 0;
2607 u32 reg;
2608
John Youncf40b862016-11-14 12:32:43 -08002609 /*
2610 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2611 * the core supports IMOD, disable it.
2612 */
2613 if (dwc->imod_interval) {
2614 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2615 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2616 } else if (dwc3_has_imod(dwc)) {
2617 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2618 }
2619
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002620 /*
2621 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2622 * field instead of letting dwc3 itself calculate that automatically.
2623 *
2624 * This way, we maximize the chances that we'll be able to get several
2625 * bursts of data without going through any sort of endpoint throttling.
2626 */
2627 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002628 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002629 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002630 else
2631 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002632
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002633 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2634
Felipe Balbi4e994722016-05-13 14:09:59 +03002635 dwc3_gadget_setup_nump(dwc);
2636
Thinh Nguyene66bbfb2021-04-12 20:00:45 -07002637 /*
2638 * Currently the controller handles single stream only. So, Ignore
2639 * Packet Pending bit for stream selection and don't search for another
2640 * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2641 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2642 * the stream performance.
2643 */
2644 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2645 reg |= DWC3_DCFG_IGNSTRMPP;
2646 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2647
Felipe Balbi72246da2011-08-19 18:10:58 +03002648 /* Start with SuperSpeed Default */
2649 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2650
2651 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002652 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002653 if (ret) {
2654 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002655 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002656 }
2657
2658 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002659 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002660 if (ret) {
2661 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002662 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002663 }
2664
2665 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002666 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002667 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Wesley Cheng4a1e25c2021-08-24 21:28:55 -07002668 dwc->delayed_status = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002669 dwc3_ep0_out_start(dwc);
2670
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002671 dwc3_gadget_enable_irq(dwc);
2672
Felipe Balbid7be2952016-05-04 15:49:37 +03002673 return 0;
2674
2675err1:
2676 __dwc3_gadget_ep_disable(dwc->eps[0]);
2677
2678err0:
2679 return ret;
2680}
2681
2682static int dwc3_gadget_start(struct usb_gadget *g,
2683 struct usb_gadget_driver *driver)
2684{
2685 struct dwc3 *dwc = gadget_to_dwc(g);
2686 unsigned long flags;
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002687 int ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002688 int irq;
2689
Roger Quadros9522def2016-06-10 14:48:38 +03002690 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002691 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2692 IRQF_SHARED, "dwc3", dwc->ev_buf);
2693 if (ret) {
2694 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2695 irq, ret);
Thinh Nguyen8cf90452021-02-05 01:53:47 -08002696 return ret;
Felipe Balbid7be2952016-05-04 15:49:37 +03002697 }
2698
2699 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002700 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 spin_unlock_irqrestore(&dwc->lock, flags);
2702
2703 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002704}
2705
Felipe Balbid7be2952016-05-04 15:49:37 +03002706static void __dwc3_gadget_stop(struct dwc3 *dwc)
2707{
2708 dwc3_gadget_disable_irq(dwc);
2709 __dwc3_gadget_ep_disable(dwc->eps[0]);
2710 __dwc3_gadget_ep_disable(dwc->eps[1]);
2711}
2712
Felipe Balbi22835b82014-10-17 12:05:12 -05002713static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002714{
2715 struct dwc3 *dwc = gadget_to_dwc(g);
2716 unsigned long flags;
2717
2718 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002719 dwc->gadget_driver = NULL;
Wesley Cheng9f607a32021-07-10 02:13:12 -07002720 dwc->max_cfg_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002721 spin_unlock_irqrestore(&dwc->lock, flags);
2722
Felipe Balbi3f308d12016-05-16 14:17:06 +03002723 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002724
Felipe Balbi72246da2011-08-19 18:10:58 +03002725 return 0;
2726}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002727
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302728static void dwc3_gadget_config_params(struct usb_gadget *g,
2729 struct usb_dcd_config_params *params)
2730{
2731 struct dwc3 *dwc = gadget_to_dwc(g);
2732
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002733 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2734 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2735
2736 /* Recommended BESL */
2737 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002738 /*
2739 * If the recommended BESL baseline is 0 or if the BESL deep is
2740 * less than 2, Microsoft's Windows 10 host usb stack will issue
2741 * a usb reset immediately after it receives the extended BOS
2742 * descriptor and the enumeration will fail. To maintain
2743 * compatibility with the Windows' usb stack, let's set the
2744 * recommended BESL baseline to 1 and clamp the BESL deep to be
2745 * within 2 to 15.
2746 */
2747 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002748 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002749 params->besl_deep =
2750 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002751 }
2752
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302753 /* U1 Device exit Latency */
2754 if (dwc->dis_u1_entry_quirk)
2755 params->bU1devExitLat = 0;
2756 else
2757 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2758
2759 /* U2 Device exit Latency */
2760 if (dwc->dis_u2_entry_quirk)
2761 params->bU2DevExitLat = 0;
2762 else
2763 params->bU2DevExitLat =
2764 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2765}
2766
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002767static void dwc3_gadget_set_speed(struct usb_gadget *g,
2768 enum usb_device_speed speed)
2769{
2770 struct dwc3 *dwc = gadget_to_dwc(g);
2771 unsigned long flags;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002772
2773 spin_lock_irqsave(&dwc->lock, flags);
Wesley Cheng7c9a2592020-12-29 15:05:36 -08002774 dwc->gadget_max_speed = speed;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002775 spin_unlock_irqrestore(&dwc->lock, flags);
2776}
2777
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002778static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
2779 enum usb_ssp_rate rate)
2780{
2781 struct dwc3 *dwc = gadget_to_dwc(g);
2782 unsigned long flags;
2783
2784 spin_lock_irqsave(&dwc->lock, flags);
Thinh Nguyencdb651b2021-03-08 18:16:44 -08002785 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002786 dwc->gadget_ssp_rate = rate;
2787 spin_unlock_irqrestore(&dwc->lock, flags);
2788}
2789
Wesley Cheng82c46b82020-12-29 15:03:29 -08002790static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
2791{
2792 struct dwc3 *dwc = gadget_to_dwc(g);
Ray Chi99288de2021-02-22 19:51:49 +08002793 union power_supply_propval val = {0};
2794 int ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002795
2796 if (dwc->usb2_phy)
2797 return usb_phy_set_power(dwc->usb2_phy, mA);
2798
Ray Chi99288de2021-02-22 19:51:49 +08002799 if (!dwc->usb_psy)
2800 return -EOPNOTSUPP;
2801
Ray Chi8a5b5c32021-03-28 02:28:08 +08002802 val.intval = 1000 * mA;
Ray Chi99288de2021-02-22 19:51:49 +08002803 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
2804
2805 return ret;
Wesley Cheng82c46b82020-12-29 15:03:29 -08002806}
2807
Wesley Cheng9f607a32021-07-10 02:13:12 -07002808/**
2809 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
2810 * @g: pointer to the USB gadget
2811 *
2812 * Used to record the maximum number of endpoints being used in a USB composite
2813 * device. (across all configurations) This is to be used in the calculation
2814 * of the TXFIFO sizes when resizing internal memory for individual endpoints.
2815 * It will help ensured that the resizing logic reserves enough space for at
2816 * least one max packet.
2817 */
2818static int dwc3_gadget_check_config(struct usb_gadget *g)
2819{
2820 struct dwc3 *dwc = gadget_to_dwc(g);
2821 struct usb_ep *ep;
2822 int fifo_size = 0;
2823 int ram1_depth;
2824 int ep_num = 0;
2825
2826 if (!dwc->do_fifo_resize)
2827 return 0;
2828
2829 list_for_each_entry(ep, &g->ep_list, ep_list) {
2830 /* Only interested in the IN endpoints */
2831 if (ep->claimed && (ep->address & USB_DIR_IN))
2832 ep_num++;
2833 }
2834
2835 if (ep_num <= dwc->max_cfg_eps)
2836 return 0;
2837
2838 /* Update the max number of eps in the composition */
2839 dwc->max_cfg_eps = ep_num;
2840
2841 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
2842 /* Based on the equation, increment by one for every ep */
2843 fifo_size += dwc->max_cfg_eps;
2844
2845 /* Check if we can fit a single fifo per endpoint */
2846 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
2847 if (fifo_size > ram1_depth)
2848 return -ENOMEM;
2849
2850 return 0;
2851}
2852
Linyu Yuan40edb522021-06-29 09:51:18 +08002853static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
2854{
2855 struct dwc3 *dwc = gadget_to_dwc(g);
2856 unsigned long flags;
2857
2858 spin_lock_irqsave(&dwc->lock, flags);
2859 dwc->async_callbacks = enable;
2860 spin_unlock_irqrestore(&dwc->lock, flags);
2861}
2862
Felipe Balbi72246da2011-08-19 18:10:58 +03002863static const struct usb_gadget_ops dwc3_gadget_ops = {
2864 .get_frame = dwc3_gadget_get_frame,
2865 .wakeup = dwc3_gadget_wakeup,
2866 .set_selfpowered = dwc3_gadget_set_selfpowered,
2867 .pullup = dwc3_gadget_pullup,
2868 .udc_start = dwc3_gadget_start,
2869 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002870 .udc_set_speed = dwc3_gadget_set_speed,
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08002871 .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302872 .get_config_params = dwc3_gadget_config_params,
Wesley Cheng82c46b82020-12-29 15:03:29 -08002873 .vbus_draw = dwc3_gadget_vbus_draw,
Wesley Cheng9f607a32021-07-10 02:13:12 -07002874 .check_config = dwc3_gadget_check_config,
Linyu Yuan40edb522021-06-29 09:51:18 +08002875 .udc_async_callbacks = dwc3_gadget_async_callbacks,
Felipe Balbi72246da2011-08-19 18:10:58 +03002876};
2877
2878/* -------------------------------------------------------------------------- */
2879
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002880static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2881{
2882 struct dwc3 *dwc = dep->dwc;
2883
2884 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2885 dep->endpoint.maxburst = 1;
2886 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2887 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002888 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002889
2890 dep->endpoint.caps.type_control = true;
2891
2892 return 0;
2893}
2894
2895static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2896{
2897 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002898 u32 mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002899 int size;
2900
Thinh Nguyend00be772021-03-27 17:54:01 -07002901 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002902
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002903 /* MDWIDTH is represented in bits, we need it in bytes */
2904 mdwidth /= 8;
2905
2906 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002907 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002908 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002909 else
2910 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002911
2912 /* FIFO Depth is in MDWDITH bytes. Multiply */
2913 size *= mdwidth;
2914
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002915 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002916 * To meet performance requirement, a minimum TxFIFO size of 3x
2917 * MaxPacketSize is recommended for endpoints that support burst and a
2918 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2919 * support burst. Use those numbers and we can calculate the max packet
2920 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002921 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002922 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2923 size /= 3;
2924 else
2925 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002926
2927 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2928
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002929 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002930 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2931 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002932 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002933 dep->endpoint.caps.type_iso = true;
2934 dep->endpoint.caps.type_bulk = true;
2935 dep->endpoint.caps.type_int = true;
2936
2937 return dwc3_alloc_trb_pool(dep);
2938}
2939
2940static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2941{
2942 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend00be772021-03-27 17:54:01 -07002943 u32 mdwidth;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002944 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002945
Thinh Nguyend00be772021-03-27 17:54:01 -07002946 mdwidth = dwc3_mdwidth(dwc);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002947
2948 /* MDWIDTH is represented in bits, convert to bytes */
2949 mdwidth /= 8;
2950
2951 /* All OUT endpoints share a single RxFIFO space */
2952 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002953 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002954 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002955 else
2956 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002957
2958 /* FIFO depth is in MDWDITH bytes */
2959 size *= mdwidth;
2960
2961 /*
2962 * To meet performance requirement, a minimum recommended RxFIFO size
2963 * is defined as follow:
2964 * RxFIFO size >= (3 x MaxPacketSize) +
2965 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2966 *
2967 * Then calculate the max packet limit as below.
2968 */
2969 size -= (3 * 8) + 16;
2970 if (size < 0)
2971 size = 0;
2972 else
2973 size /= 3;
2974
2975 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Thinh Nguyene0a93d92020-09-29 15:26:29 -07002976 dep->endpoint.max_streams = 16;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002977 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2978 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002979 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002980 dep->endpoint.caps.type_iso = true;
2981 dep->endpoint.caps.type_bulk = true;
2982 dep->endpoint.caps.type_int = true;
2983
2984 return dwc3_alloc_trb_pool(dep);
2985}
2986
2987static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002988{
2989 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002990 bool direction = epnum & 1;
2991 int ret;
2992 u8 num = epnum >> 1;
2993
2994 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2995 if (!dep)
2996 return -ENOMEM;
2997
2998 dep->dwc = dwc;
2999 dep->number = epnum;
3000 dep->direction = direction;
3001 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
3002 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08003003 dep->combo_num = 0;
3004 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003005
3006 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
3007 direction ? "in" : "out");
3008
3009 dep->endpoint.name = dep->name;
3010
3011 if (!(dep->number > 1)) {
3012 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
3013 dep->endpoint.comp_desc = NULL;
3014 }
3015
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003016 if (num == 0)
3017 ret = dwc3_gadget_init_control_endpoint(dep);
3018 else if (direction)
3019 ret = dwc3_gadget_init_in_endpoint(dep);
3020 else
3021 ret = dwc3_gadget_init_out_endpoint(dep);
3022
3023 if (ret)
3024 return ret;
3025
3026 dep->endpoint.caps.dir_in = direction;
3027 dep->endpoint.caps.dir_out = !direction;
3028
3029 INIT_LIST_HEAD(&dep->pending_list);
3030 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03003031 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003032
Jack Pham5ff90af2021-05-29 12:29:32 -07003033 dwc3_debugfs_create_endpoint_dir(dep);
3034
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003035 return 0;
3036}
3037
3038static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
3039{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00003040 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03003041
Peter Chene81a7012020-08-21 10:55:48 +08003042 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003043
Andy Shevchenko46b780d2017-06-12 15:11:25 +03003044 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003045 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03003046
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03003047 ret = dwc3_gadget_init_endpoint(dwc, epnum);
3048 if (ret)
3049 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03003050 }
3051
3052 return 0;
3053}
3054
3055static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
3056{
3057 struct dwc3_ep *dep;
3058 u8 epnum;
3059
3060 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3061 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003062 if (!dep)
3063 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05303064 /*
3065 * Physical endpoints 0 and 1 are special; they form the
3066 * bi-directional USB endpoint 0.
3067 *
3068 * For those two physical endpoints, we don't allocate a TRB
3069 * pool nor do we add them the endpoints list. Due to that, we
3070 * shouldn't do these two operations otherwise we would end up
3071 * with all sorts of bugs when removing dwc3.ko.
3072 */
3073 if (epnum != 0 && epnum != 1) {
3074 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003075 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05303076 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003077
Greg Kroah-Hartman8562d5b2021-06-09 11:39:24 +02003078 debugfs_remove_recursive(debugfs_lookup(dep->name,
3079 debugfs_lookup(dev_name(dep->dwc->dev),
3080 usb_debug_root)));
Felipe Balbi72246da2011-08-19 18:10:58 +03003081 kfree(dep);
3082 }
3083}
3084
Felipe Balbi72246da2011-08-19 18:10:58 +03003085/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02003086
Felipe Balbi8f608e82018-03-27 10:53:29 +03003087static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
3088 struct dwc3_request *req, struct dwc3_trb *trb,
3089 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303090{
3091 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303092
Felipe Balbidc55c672016-08-12 13:20:32 +03003093 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03003094
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003095 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03003096 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003097
Felipe Balbie5b36ae2016-08-10 11:13:26 +03003098 /*
3099 * If we're in the middle of series of chained TRBs and we
3100 * receive a short transfer along the way, DWC3 will skip
3101 * through all TRBs including the last TRB in the chain (the
3102 * where CHN bit is zero. DWC3 will also avoid clearing HWO
3103 * bit and SW has to do it manually.
3104 *
3105 * We're going to do that here to avoid problems of HW trying
3106 * to use bogus TRBs for transfers.
3107 */
3108 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
3109 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3110
Felipe Balbic6267a52017-01-05 14:58:46 +02003111 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08003112 * For isochronous transfers, the first TRB in a service interval must
3113 * have the Isoc-First type. Track and report its interval frame number.
3114 */
3115 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3116 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
3117 unsigned int frame_number;
3118
3119 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
3120 frame_number &= ~(dep->interval - 1);
3121 req->request.frame_number = frame_number;
3122 }
3123
3124 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07003125 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3126 * this TRB points to the bounce buffer address, it's a MPS alignment
3127 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02003128 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07003129 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3130 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02003131 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3132 return 1;
3133 }
3134
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303135 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03003136 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303137
Felipe Balbi35b27192017-03-08 13:56:37 +02003138 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
3139 return 1;
3140
Felipe Balbid80fe1b2018-04-06 11:04:21 +03003141 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303142 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03003143
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00003144 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
3145 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303146 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03003147
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05303148 return 0;
3149}
3150
Felipe Balbid3692952018-03-29 13:32:10 +03003151static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
3152 struct dwc3_request *req, const struct dwc3_event_depevt *event,
3153 int status)
3154{
3155 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3156 struct scatterlist *sg = req->sg;
3157 struct scatterlist *s;
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07003158 unsigned int num_queued = req->num_queued_sgs;
Felipe Balbid3692952018-03-29 13:32:10 +03003159 unsigned int i;
3160 int ret = 0;
3161
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07003162 for_each_sg(sg, s, num_queued, i) {
Felipe Balbid3692952018-03-29 13:32:10 +03003163 trb = &dep->trb_pool[dep->trb_dequeue];
3164
Felipe Balbid3692952018-03-29 13:32:10 +03003165 req->sg = sg_next(s);
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07003166 req->num_queued_sgs--;
Felipe Balbid3692952018-03-29 13:32:10 +03003167
3168 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
3169 trb, event, status, true);
3170 if (ret)
3171 break;
3172 }
3173
3174 return ret;
3175}
3176
3177static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
3178 struct dwc3_request *req, const struct dwc3_event_depevt *event,
3179 int status)
3180{
3181 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3182
3183 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
3184 event, status, false);
3185}
3186
Felipe Balbie0c42ce2018-04-06 15:37:30 +03003187static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
3188{
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07003189 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03003190}
3191
Felipe Balbif38e35d2018-04-06 15:56:35 +03003192static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
3193 const struct dwc3_event_depevt *event,
3194 struct dwc3_request *req, int status)
3195{
3196 int ret;
3197
Thinh Nguyen25dda9f2021-05-12 20:17:09 -07003198 if (req->request.num_mapped_sgs)
Felipe Balbif38e35d2018-04-06 15:56:35 +03003199 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
3200 status);
3201 else
3202 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3203 status);
3204
Thinh Nguyen690e5c22020-09-24 01:21:24 -07003205 req->request.actual = req->request.length - req->remaining;
3206
3207 if (!dwc3_gadget_ep_request_completed(req))
3208 goto out;
3209
Felipe Balbi1a22ec62018-08-01 13:15:05 +03003210 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03003211 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3212 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03003213 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03003214 }
3215
Felipe Balbif38e35d2018-04-06 15:56:35 +03003216 dwc3_gadget_giveback(dep, req, status);
3217
3218out:
3219 return ret;
3220}
3221
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03003222static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03003223 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03003224{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03003225 struct dwc3_request *req;
3226 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03003227
Greg Kroah-Hartman664cc972021-08-10 09:10:15 +02003228 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03003229 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03003230
Felipe Balbif38e35d2018-04-06 15:56:35 +03003231 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
3232 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03003233 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03003234 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03003235 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003236}
3237
Thinh Nguyend9feef92020-03-31 01:40:42 -07003238static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
3239{
3240 struct dwc3_request *req;
Wesley Cheng02fa4b92021-03-19 02:31:24 -07003241 struct dwc3 *dwc = dep->dwc;
3242
3243 if (!dep->endpoint.desc || !dwc->pullups_connected ||
3244 !dwc->connected)
3245 return false;
Thinh Nguyend9feef92020-03-31 01:40:42 -07003246
3247 if (!list_empty(&dep->pending_list))
3248 return true;
3249
3250 /*
3251 * We only need to check the first entry of the started list. We can
3252 * assume the completed requests are removed from the started list.
3253 */
3254 req = next_request(&dep->started_list);
3255 if (!req)
3256 return false;
3257
3258 return !dwc3_gadget_ep_request_completed(req);
3259}
3260
Felipe Balbiee3638b2018-03-27 11:26:53 +03003261static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
3262 const struct dwc3_event_depevt *event)
3263{
Felipe Balbif62afb42018-04-11 10:34:34 +03003264 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03003265}
3266
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003267static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
3268 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03003269{
Felipe Balbi8f608e82018-03-27 10:53:29 +03003270 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003271 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03003272
Albert Wang26288442021-11-09 17:26:42 +08003273 if (!dep->endpoint.desc)
3274 return no_started_trb;
3275
Felipe Balbi5f2e7972018-03-29 11:10:45 +03003276 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03003277
Thinh Nguyenb6842d42020-05-05 19:46:33 -07003278 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3279 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03003280
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02003281 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3282 list_empty(&dep->started_list) &&
3283 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03003284 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07003285 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003286 if (__dwc3_gadget_kick_transfer(dep) == 0)
3287 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03003288
Thinh Nguyenb6842d42020-05-05 19:46:33 -07003289out:
Felipe Balbifae2b902011-10-14 13:00:30 +03003290 /*
3291 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3292 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3293 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003294 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003295 u32 reg;
3296 int i;
3297
3298 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05003299 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03003300
3301 if (!(dep->flags & DWC3_EP_ENABLED))
3302 continue;
3303
Felipe Balbiaa3342c2016-03-14 11:01:31 +02003304 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003305 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03003306 }
3307
3308 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3309 reg |= dwc->u1u2;
3310 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3311
3312 dwc->u1u2 = 0;
3313 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003314
3315 return no_started_trb;
3316}
3317
3318static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3319 const struct dwc3_event_depevt *event)
3320{
3321 int status = 0;
3322
Albert Wang26288442021-11-09 17:26:42 +08003323 if (!dep->endpoint.desc)
3324 return;
3325
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07003326 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3327 dwc3_gadget_endpoint_frame_from_event(dep, event);
3328
3329 if (event->status & DEPEVT_STATUS_BUSERR)
3330 status = -ECONNRESET;
3331
3332 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3333 status = -EXDEV;
3334
3335 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03003336}
3337
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003338static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3339 const struct dwc3_event_depevt *event)
3340{
3341 int status = 0;
3342
3343 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3344
3345 if (event->status & DEPEVT_STATUS_BUSERR)
3346 status = -ECONNRESET;
3347
Thinh Nguyene0d19562020-05-05 19:46:57 -07003348 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3349 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03003350}
3351
Felipe Balbi8f608e82018-03-27 10:53:29 +03003352static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3353 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03003354{
Felipe Balbiee3638b2018-03-27 11:26:53 +03003355 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07003356
3357 /*
3358 * The XferNotReady event is generated only once before the endpoint
3359 * starts. It will be generated again when END_TRANSFER command is
3360 * issued. For some controller versions, the XferNotReady event may be
3361 * generated while the END_TRANSFER command is still in process. Ignore
3362 * it and wait for the next XferNotReady event after the command is
3363 * completed.
3364 */
3365 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3366 return;
3367
Felipe Balbi25abad62018-08-14 10:41:19 +03003368 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03003369}
3370
Thinh Nguyen8266b082020-07-30 16:29:03 -07003371static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3372 const struct dwc3_event_depevt *event)
3373{
3374 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3375
3376 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3377 return;
3378
Thinh Nguyend74dc3e2021-10-25 16:21:10 -07003379 /*
3380 * The END_TRANSFER command will cause the controller to generate a
3381 * NoStream Event, and it's not due to the host DP NoStream rejection.
3382 * Ignore the next NoStream event.
3383 */
3384 if (dep->stream_capable)
3385 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3386
Thinh Nguyen8266b082020-07-30 16:29:03 -07003387 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3388 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3389 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3390
3391 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3392 struct dwc3 *dwc = dep->dwc;
3393
3394 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3395 if (dwc3_send_clear_stall_ep_cmd(dep)) {
3396 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3397
3398 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3399 if (dwc->delayed_status)
3400 __dwc3_gadget_ep0_set_halt(ep0, 1);
3401 return;
3402 }
3403
3404 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3405 if (dwc->delayed_status)
3406 dwc3_ep0_send_delayed_status(dwc);
3407 }
3408
3409 if ((dep->flags & DWC3_EP_DELAY_START) &&
3410 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3411 __dwc3_gadget_kick_transfer(dep);
3412
3413 dep->flags &= ~DWC3_EP_DELAY_START;
3414}
3415
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003416static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3417 const struct dwc3_event_depevt *event)
3418{
3419 struct dwc3 *dwc = dep->dwc;
3420
3421 if (event->status == DEPEVT_STREAMEVT_FOUND) {
3422 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3423 goto out;
3424 }
3425
3426 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3427 switch (event->parameters) {
3428 case DEPEVT_STREAM_PRIME:
3429 /*
3430 * If the host can properly transition the endpoint state from
3431 * idle to prime after a NoStream rejection, there's no need to
3432 * force restarting the endpoint to reinitiate the stream. To
3433 * simplify the check, assume the host follows the USB spec if
3434 * it primed the endpoint more than once.
3435 */
3436 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3437 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3438 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3439 else
3440 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3441 }
3442
3443 break;
3444 case DEPEVT_STREAM_NOSTREAM:
3445 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3446 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3447 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3448 break;
3449
3450 /*
3451 * If the host rejects a stream due to no active stream, by the
3452 * USB and xHCI spec, the endpoint will be put back to idle
3453 * state. When the host is ready (buffer added/updated), it will
3454 * prime the endpoint to inform the usb device controller. This
3455 * triggers the device controller to issue ERDY to restart the
3456 * stream. However, some hosts don't follow this and keep the
3457 * endpoint in the idle state. No prime will come despite host
3458 * streams are updated, and the device controller will not be
3459 * triggered to generate ERDY to move the next stream data. To
3460 * workaround this and maintain compatibility with various
3461 * hosts, force to reinitate the stream until the host is ready
3462 * instead of waiting for the host to prime the endpoint.
3463 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07003464 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3465 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3466
3467 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3468 } else {
3469 dep->flags |= DWC3_EP_DELAY_START;
3470 dwc3_stop_active_transfer(dep, true, true);
3471 return;
3472 }
3473 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003474 }
3475
3476out:
3477 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3478}
3479
Felipe Balbi72246da2011-08-19 18:10:58 +03003480static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3481 const struct dwc3_event_depevt *event)
3482{
3483 struct dwc3_ep *dep;
3484 u8 epnum = event->endpoint_number;
3485
3486 dep = dwc->eps[epnum];
3487
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003488 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02003489 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01003490 return;
3491
3492 /* Handle only EPCMDCMPLT when EP disabled */
3493 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
3494 return;
3495 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03003496
Felipe Balbi72246da2011-08-19 18:10:58 +03003497 if (epnum == 0 || epnum == 1) {
3498 dwc3_ep0_interrupt(dwc, event);
3499 return;
3500 }
3501
3502 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003503 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003504 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003505 break;
3506 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003507 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003508 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003509 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003510 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003511 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003512 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003513 dwc3_gadget_endpoint_transfer_complete(dep, event);
3514 break;
3515 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003516 dwc3_gadget_endpoint_stream_event(dep, event);
3517 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003518 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003519 break;
3520 }
3521}
3522
3523static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3524{
Linyu Yuan40edb522021-06-29 09:51:18 +08003525 if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003526 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003527 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003528 spin_lock(&dwc->lock);
3529 }
3530}
3531
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003532static void dwc3_suspend_gadget(struct dwc3 *dwc)
3533{
Linyu Yuan40edb522021-06-29 09:51:18 +08003534 if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003535 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003536 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003537 spin_lock(&dwc->lock);
3538 }
3539}
3540
3541static void dwc3_resume_gadget(struct dwc3 *dwc)
3542{
Linyu Yuan40edb522021-06-29 09:51:18 +08003543 if (dwc->async_callbacks && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003544 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003545 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003546 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003547 }
3548}
3549
3550static void dwc3_reset_gadget(struct dwc3 *dwc)
3551{
3552 if (!dwc->gadget_driver)
3553 return;
3554
Linyu Yuan40edb522021-06-29 09:51:18 +08003555 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003556 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003557 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003558 spin_lock(&dwc->lock);
3559 }
3560}
3561
Felipe Balbic5353b22019-02-13 13:00:54 +02003562static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3563 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003564{
Felipe Balbi72246da2011-08-19 18:10:58 +03003565 struct dwc3_gadget_ep_cmd_params params;
3566 u32 cmd;
3567 int ret;
3568
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003569 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3570 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303571 return;
3572
Pratyush Anand57911502012-07-06 15:19:10 +05303573 /*
3574 * NOTICE: We are violating what the Databook says about the
3575 * EndTransfer command. Ideally we would _always_ wait for the
3576 * EndTransfer Command Completion IRQ, but that's causing too
3577 * much trouble synchronizing between us and gadget driver.
3578 *
3579 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003580 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303581 *
3582 * Note also that a similar handling was tested by Synopsys
3583 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003584 * In short, what we're doing is issuing EndTransfer with
3585 * CMDIOC bit set and delay kicking transfer until the
3586 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003587 *
3588 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3589 * supports a mode to work around the above limitation. The
3590 * software can poll the CMDACT bit in the DEPCMD register
3591 * after issuing a EndTransfer command. This mode is enabled
3592 * by writing GUCTL2[14]. This polling is already done in the
3593 * dwc3_send_gadget_ep_cmd() function so if the mode is
3594 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003595 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003596 *
3597 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303598 */
3599
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303600 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003601 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003602 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003603 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303604 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003605 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303606 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003607 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003608
Thinh Nguyend3abda52019-11-27 13:10:47 -08003609 if (!interrupt)
3610 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003611 else
3612 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003613}
3614
Felipe Balbi72246da2011-08-19 18:10:58 +03003615static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3616{
3617 u32 epnum;
3618
3619 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3620 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003621 int ret;
3622
3623 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003624 if (!dep)
3625 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003626
3627 if (!(dep->flags & DWC3_EP_STALL))
3628 continue;
3629
3630 dep->flags &= ~DWC3_EP_STALL;
3631
John Youn50c763f2016-05-31 17:49:56 -07003632 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003633 WARN_ON_ONCE(ret);
3634 }
3635}
3636
3637static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3638{
Felipe Balbic4430a22012-05-24 10:30:01 +03003639 int reg;
3640
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003641 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3642
Felipe Balbi72246da2011-08-19 18:10:58 +03003643 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3644 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003645 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003646 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003647
Felipe Balbi72246da2011-08-19 18:10:58 +03003648 dwc3_disconnect_gadget(dwc);
3649
Peter Chene81a7012020-08-21 10:55:48 +08003650 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003651 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003652 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003653
3654 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003655}
3656
Felipe Balbi72246da2011-08-19 18:10:58 +03003657static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3658{
3659 u32 reg;
3660
Felipe Balbidf62df52011-10-14 15:11:49 +03003661 /*
Wesley Cheng71ca43f2021-03-19 02:31:25 -07003662 * Ideally, dwc3_reset_gadget() would trigger the function
3663 * drivers to stop any active transfers through ep disable.
3664 * However, for functions which defer ep disable, such as mass
3665 * storage, we will need to rely on the call to stop active
3666 * transfers here, and avoid allowing of request queuing.
3667 */
3668 dwc->connected = false;
3669
3670 /*
Felipe Balbidf62df52011-10-14 15:11:49 +03003671 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3672 * would cause a missing Disconnect Event if there's a
3673 * pending Setup Packet in the FIFO.
3674 *
3675 * There's no suggested workaround on the official Bug
3676 * report, which states that "unless the driver/application
3677 * is doing any special handling of a disconnect event,
3678 * there is no functional issue".
3679 *
3680 * Unfortunately, it turns out that we _do_ some special
3681 * handling of a disconnect event, namely complete all
3682 * pending transfers, notify gadget driver of the
3683 * disconnection, and so on.
3684 *
3685 * Our suggested workaround is to follow the Disconnect
3686 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003687 * flag. Such flag gets set whenever we have a SETUP_PENDING
3688 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003689 * same endpoint.
3690 *
3691 * Refers to:
3692 *
3693 * STAR#9000466709: RTL: Device : Disconnect event not
3694 * generated if setup packet pending in FIFO
3695 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003696 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003697 if (dwc->setup_packet_pending)
3698 dwc3_gadget_disconnect_interrupt(dwc);
3699 }
3700
Felipe Balbi8e744752014-11-06 14:27:53 +08003701 dwc3_reset_gadget(dwc);
Wesley Chengae7e8612020-09-28 17:20:59 -07003702 /*
3703 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
3704 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
3705 * needs to ensure that it sends "a DEPENDXFER command for any active
3706 * transfers."
3707 */
3708 dwc3_stop_active_transfers(dwc);
Wesley Chengf09ddcf2021-03-11 15:59:02 -08003709 dwc->connected = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003710
3711 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3712 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003713 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003714 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003715 dwc3_clear_stall_all_ep(dwc);
3716
3717 /* Reset device address to zero */
3718 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3719 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3720 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003721}
3722
Felipe Balbi72246da2011-08-19 18:10:58 +03003723static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3724{
Felipe Balbi72246da2011-08-19 18:10:58 +03003725 struct dwc3_ep *dep;
3726 int ret;
3727 u32 reg;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003728 u8 lanes = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003729 u8 speed;
3730
Felipe Balbi72246da2011-08-19 18:10:58 +03003731 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3732 speed = reg & DWC3_DSTS_CONNECTSPD;
3733 dwc->speed = speed;
3734
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003735 if (DWC3_IP_IS(DWC32))
3736 lanes = DWC3_DSTS_CONNLANES(reg) + 1;
3737
3738 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
3739
John Youn5fb6fda2016-11-10 17:23:25 -08003740 /*
3741 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3742 * each time on Connect Done.
3743 *
3744 * Currently we always use the reset value. If any platform
3745 * wants to set this to a different value, we need to add a
3746 * setting and update GCTL.RAMCLKSEL here.
3747 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003748
3749 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003750 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003751 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003752 dwc->gadget->ep0->maxpacket = 512;
3753 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003754
3755 if (lanes > 1)
3756 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
3757 else
3758 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
John Youn75808622016-02-05 17:09:13 -08003759 break;
John Youn2da9ad72016-05-20 16:34:26 -07003760 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003761 /*
3762 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3763 * would cause a missing USB3 Reset event.
3764 *
3765 * In such situations, we should force a USB3 Reset
3766 * event by calling our dwc3_gadget_reset_interrupt()
3767 * routine.
3768 *
3769 * Refers to:
3770 *
3771 * STAR#9000483510: RTL: SS : USB3 reset event may
3772 * not be generated always when the link enters poll
3773 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003774 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003775 dwc3_gadget_reset_interrupt(dwc);
3776
Felipe Balbi72246da2011-08-19 18:10:58 +03003777 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003778 dwc->gadget->ep0->maxpacket = 512;
3779 dwc->gadget->speed = USB_SPEED_SUPER;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08003780
3781 if (lanes > 1) {
3782 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
3783 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
3784 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003785 break;
John Youn2da9ad72016-05-20 16:34:26 -07003786 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003787 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003788 dwc->gadget->ep0->maxpacket = 64;
3789 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003790 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003791 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003792 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003793 dwc->gadget->ep0->maxpacket = 64;
3794 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003795 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003796 }
3797
Peter Chene81a7012020-08-21 10:55:48 +08003798 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003799
Pratyush Anand2b758352013-01-14 15:59:31 +05303800 /* Enable USB2 LPM Capability */
3801
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003802 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003803 !dwc->usb2_gadget_lpm_disable &&
John Youn2da9ad72016-05-20 16:34:26 -07003804 (speed != DWC3_DSTS_SUPERSPEED) &&
3805 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303806 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3807 reg |= DWC3_DCFG_LPM_CAP;
3808 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3809
3810 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3811 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3812
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003813 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3814 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303815
Huang Rui80caf7d2014-10-28 19:54:26 +08003816 /*
3817 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3818 * DCFG.LPMCap is set, core responses with an ACK and the
3819 * BESL value in the LPM token is less than or equal to LPM
3820 * NYET threshold.
3821 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003822 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003823 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003824
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003825 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003826 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003827
Thinh Nguyen5b738212019-10-23 19:15:43 -07003828 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003829 } else {
Thinh Nguyen475e8be2021-04-13 19:13:18 -07003830 if (dwc->usb2_gadget_lpm_disable) {
3831 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3832 reg &= ~DWC3_DCFG_LPM_CAP;
3833 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3834 }
3835
Felipe Balbi356363b2013-12-19 16:37:05 -06003836 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3837 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003838 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303839 }
3840
Felipe Balbi72246da2011-08-19 18:10:58 +03003841 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003842 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003843 if (ret) {
3844 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3845 return;
3846 }
3847
3848 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003849 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003850 if (ret) {
3851 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3852 return;
3853 }
3854
3855 /*
3856 * Configure PHY via GUSB3PIPECTLn if required.
3857 *
3858 * Update GTXFIFOSIZn
3859 *
3860 * In both cases reset values should be sufficient.
3861 */
3862}
3863
3864static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3865{
Felipe Balbi72246da2011-08-19 18:10:58 +03003866 /*
3867 * TODO take core out of low power mode when that's
3868 * implemented.
3869 */
3870
Linyu Yuan40edb522021-06-29 09:51:18 +08003871 if (dwc->async_callbacks && dwc->gadget_driver->resume) {
Jiebing Liad14d4e2014-12-11 13:26:29 +08003872 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003873 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003874 spin_lock(&dwc->lock);
3875 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003876}
3877
3878static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3879 unsigned int evtinfo)
3880{
Felipe Balbifae2b902011-10-14 13:00:30 +03003881 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003882 unsigned int pwropt;
3883
3884 /*
3885 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3886 * Hibernation mode enabled which would show up when device detects
3887 * host-initiated U3 exit.
3888 *
3889 * In that case, device will generate a Link State Change Interrupt
3890 * from U3 to RESUME which is only necessary if Hibernation is
3891 * configured in.
3892 *
3893 * There are no functional changes due to such spurious event and we
3894 * just need to ignore it.
3895 *
3896 * Refers to:
3897 *
3898 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3899 * operational mode
3900 */
3901 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003902 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003903 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3904 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3905 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003906 return;
3907 }
3908 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003909
3910 /*
3911 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3912 * on the link partner, the USB session might do multiple entry/exit
3913 * of low power states before a transfer takes place.
3914 *
3915 * Due to this problem, we might experience lower throughput. The
3916 * suggested workaround is to disable DCTL[12:9] bits if we're
3917 * transitioning from U1/U2 to U0 and enable those bits again
3918 * after a transfer completes and there are no pending transfers
3919 * on any of the enabled endpoints.
3920 *
3921 * This is the first half of that workaround.
3922 *
3923 * Refers to:
3924 *
3925 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3926 * core send LGO_Ux entering U0
3927 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003928 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003929 if (next == DWC3_LINK_STATE_U0) {
3930 u32 u1u2;
3931 u32 reg;
3932
3933 switch (dwc->link_state) {
3934 case DWC3_LINK_STATE_U1:
3935 case DWC3_LINK_STATE_U2:
3936 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3937 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3938 | DWC3_DCTL_ACCEPTU2ENA
3939 | DWC3_DCTL_INITU1ENA
3940 | DWC3_DCTL_ACCEPTU1ENA);
3941
3942 if (!dwc->u1u2)
3943 dwc->u1u2 = reg & u1u2;
3944
3945 reg &= ~u1u2;
3946
Thinh Nguyen5b738212019-10-23 19:15:43 -07003947 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003948 break;
3949 default:
3950 /* do nothing */
3951 break;
3952 }
3953 }
3954 }
3955
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003956 switch (next) {
3957 case DWC3_LINK_STATE_U1:
3958 if (dwc->speed == USB_SPEED_SUPER)
3959 dwc3_suspend_gadget(dwc);
3960 break;
3961 case DWC3_LINK_STATE_U2:
3962 case DWC3_LINK_STATE_U3:
3963 dwc3_suspend_gadget(dwc);
3964 break;
3965 case DWC3_LINK_STATE_RESUME:
3966 dwc3_resume_gadget(dwc);
3967 break;
3968 default:
3969 /* do nothing */
3970 break;
3971 }
3972
Felipe Balbie57ebc12014-04-22 13:20:12 -05003973 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003974}
3975
Baolin Wang72704f82016-05-16 16:43:53 +08003976static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3977 unsigned int evtinfo)
3978{
3979 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3980
3981 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3982 dwc3_suspend_gadget(dwc);
3983
3984 dwc->link_state = next;
3985}
3986
Felipe Balbie1dadd32014-02-25 14:47:54 -06003987static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3988 unsigned int evtinfo)
3989{
3990 unsigned int is_ss = evtinfo & BIT(4);
3991
Felipe Balbibfad65e2017-04-19 14:59:27 +03003992 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003993 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3994 * have a known issue which can cause USB CV TD.9.23 to fail
3995 * randomly.
3996 *
3997 * Because of this issue, core could generate bogus hibernation
3998 * events which SW needs to ignore.
3999 *
4000 * Refers to:
4001 *
4002 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
4003 * Device Fallback from SuperSpeed
4004 */
4005 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
4006 return;
4007
4008 /* enter hibernation here */
4009}
4010
Felipe Balbi72246da2011-08-19 18:10:58 +03004011static void dwc3_gadget_interrupt(struct dwc3 *dwc,
4012 const struct dwc3_event_devt *event)
4013{
4014 switch (event->type) {
4015 case DWC3_DEVICE_EVENT_DISCONNECT:
4016 dwc3_gadget_disconnect_interrupt(dwc);
4017 break;
4018 case DWC3_DEVICE_EVENT_RESET:
4019 dwc3_gadget_reset_interrupt(dwc);
4020 break;
4021 case DWC3_DEVICE_EVENT_CONNECT_DONE:
4022 dwc3_gadget_conndone_interrupt(dwc);
4023 break;
4024 case DWC3_DEVICE_EVENT_WAKEUP:
4025 dwc3_gadget_wakeup_interrupt(dwc);
4026 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06004027 case DWC3_DEVICE_EVENT_HIBER_REQ:
4028 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
4029 "unexpected hibernation event\n"))
4030 break;
4031
4032 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
4033 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03004034 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
4035 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
4036 break;
Jack Pham6f26ebb2021-04-28 02:01:11 -07004037 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08004038 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07004039 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08004040 /*
4041 * Ignore suspend event until the gadget enters into
4042 * USB_STATE_CONFIGURED state.
4043 */
Peter Chene81a7012020-08-21 10:55:48 +08004044 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08004045 dwc3_gadget_suspend_interrupt(dwc,
4046 event->event_info);
4047 }
Felipe Balbi72246da2011-08-19 18:10:58 +03004048 break;
4049 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03004050 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03004051 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03004052 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03004053 break;
4054 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06004055 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03004056 }
4057}
4058
4059static void dwc3_process_event_entry(struct dwc3 *dwc,
4060 const union dwc3_event *event)
4061{
Felipe Balbi43c96be2016-09-26 13:23:34 +03004062 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05004063
Felipe Balbidfc5e802017-04-26 13:44:51 +03004064 if (!event->type.is_devspec)
4065 dwc3_endpoint_interrupt(dwc, &event->depevt);
4066 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03004067 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03004068 else
Felipe Balbi72246da2011-08-19 18:10:58 +03004069 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03004070}
4071
Felipe Balbidea520a2016-03-30 09:39:34 +03004072static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03004073{
Felipe Balbidea520a2016-03-30 09:39:34 +03004074 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03004075 irqreturn_t ret = IRQ_NONE;
4076 int left;
4077 u32 reg;
4078
Felipe Balbif42f2442013-06-12 21:25:08 +03004079 left = evt->count;
4080
4081 if (!(evt->flags & DWC3_EVENT_PENDING))
4082 return IRQ_NONE;
4083
4084 while (left > 0) {
4085 union dwc3_event event;
4086
John Younebbb2d52016-11-15 13:07:02 +02004087 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03004088
4089 dwc3_process_event_entry(dwc, &event);
4090
4091 /*
4092 * FIXME we wrap around correctly to the next entry as
4093 * almost all entries are 4 bytes in size. There is one
4094 * entry which has 12 bytes which is a regular entry
4095 * followed by 8 bytes data. ATM I don't know how
4096 * things are organized if we get next to the a
4097 * boundary so I worry about that once we try to handle
4098 * that.
4099 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02004100 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03004101 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03004102 }
4103
4104 evt->count = 0;
4105 evt->flags &= ~DWC3_EVENT_PENDING;
4106 ret = IRQ_HANDLED;
4107
4108 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03004109 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03004110 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03004111 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03004112
John Youncf40b862016-11-14 12:32:43 -08004113 if (dwc->imod_interval) {
4114 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
4115 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
4116 }
4117
Felipe Balbif42f2442013-06-12 21:25:08 +03004118 return ret;
4119}
4120
Felipe Balbidea520a2016-03-30 09:39:34 +03004121static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03004122{
Felipe Balbidea520a2016-03-30 09:39:34 +03004123 struct dwc3_event_buffer *evt = _evt;
4124 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05004125 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03004126 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03004127
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05004128 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03004129 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05004130 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03004131
4132 return ret;
4133}
4134
Felipe Balbidea520a2016-03-30 09:39:34 +03004135static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03004136{
Felipe Balbidea520a2016-03-30 09:39:34 +03004137 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02004138 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03004139 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03004140 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03004141
Felipe Balbifc8bb912016-05-16 13:14:48 +03004142 if (pm_runtime_suspended(dwc->dev)) {
4143 pm_runtime_get(dwc->dev);
4144 disable_irq_nosync(dwc->irq_gadget);
4145 dwc->pending_events = true;
4146 return IRQ_HANDLED;
4147 }
4148
Thinh Nguyend325a1d2017-05-11 17:26:47 -07004149 /*
4150 * With PCIe legacy interrupt, test shows that top-half irq handler can
4151 * be called again after HW interrupt deassertion. Check if bottom-half
4152 * irq event handler completes before caching new event to prevent
4153 * losing events.
4154 */
4155 if (evt->flags & DWC3_EVENT_PENDING)
4156 return IRQ_HANDLED;
4157
Felipe Balbi660e9bd2016-03-30 09:26:24 +03004158 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03004159 count &= DWC3_GEVNTCOUNT_MASK;
4160 if (!count)
4161 return IRQ_NONE;
4162
Felipe Balbib15a7622011-06-30 16:57:15 +03004163 evt->count = count;
4164 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03004165
Felipe Balbie8adfc32013-06-12 21:11:14 +03004166 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03004167 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03004168 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03004169 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03004170
John Younebbb2d52016-11-15 13:07:02 +02004171 amount = min(count, evt->length - evt->lpos);
4172 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
4173
4174 if (amount < count)
4175 memcpy(evt->cache, evt->buf, count - amount);
4176
John Youn65aca322016-11-15 13:08:59 +02004177 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
4178
Felipe Balbib15a7622011-06-30 16:57:15 +03004179 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03004180}
4181
Felipe Balbidea520a2016-03-30 09:39:34 +03004182static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03004183{
Felipe Balbidea520a2016-03-30 09:39:34 +03004184 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03004185
Felipe Balbidea520a2016-03-30 09:39:34 +03004186 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03004187}
4188
Felipe Balbi6db38122016-10-03 11:27:01 +03004189static int dwc3_gadget_get_irq(struct dwc3 *dwc)
4190{
4191 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
4192 int irq;
4193
Hans de Goedef146b40b2019-10-05 23:04:48 +02004194 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03004195 if (irq > 0)
4196 goto out;
4197
4198 if (irq == -EPROBE_DEFER)
4199 goto out;
4200
Hans de Goedef146b40b2019-10-05 23:04:48 +02004201 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03004202 if (irq > 0)
4203 goto out;
4204
4205 if (irq == -EPROBE_DEFER)
4206 goto out;
4207
4208 irq = platform_get_irq(dwc3_pdev, 0);
4209 if (irq > 0)
4210 goto out;
4211
Felipe Balbi6db38122016-10-03 11:27:01 +03004212 if (!irq)
4213 irq = -EINVAL;
4214
4215out:
4216 return irq;
4217}
4218
Peter Chene81a7012020-08-21 10:55:48 +08004219static void dwc_gadget_release(struct device *dev)
4220{
4221 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4222
4223 kfree(gadget);
4224}
4225
Felipe Balbi72246da2011-08-19 18:10:58 +03004226/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03004227 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08004228 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03004229 *
4230 * Returns 0 on success otherwise negative errno.
4231 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05004232int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03004233{
Felipe Balbi6db38122016-10-03 11:27:01 +03004234 int ret;
4235 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08004236 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03004237
Felipe Balbi6db38122016-10-03 11:27:01 +03004238 irq = dwc3_gadget_get_irq(dwc);
4239 if (irq < 0) {
4240 ret = irq;
4241 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03004242 }
4243
4244 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03004245
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304246 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
4247 sizeof(*dwc->ep0_trb) * 2,
4248 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03004249 if (!dwc->ep0_trb) {
4250 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
4251 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004252 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03004253 }
4254
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004255 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03004256 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03004257 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004258 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03004259 }
4260
Felipe Balbi905dc042017-01-05 14:46:52 +02004261 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
4262 &dwc->bounce_addr, GFP_KERNEL);
4263 if (!dwc->bounce) {
4264 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03004265 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02004266 }
4267
Baolin Wangbb014732016-10-14 17:11:33 +08004268 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08004269 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4270 if (!dwc->gadget) {
4271 ret = -ENOMEM;
4272 goto err3;
4273 }
Baolin Wangbb014732016-10-14 17:11:33 +08004274
Peter Chene81a7012020-08-21 10:55:48 +08004275
Andy Shevchenko268bbde2021-10-04 17:18:39 +03004276 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
Peter Chene81a7012020-08-21 10:55:48 +08004277 dev = &dwc->gadget->dev;
4278 dev->platform_data = dwc;
4279 dwc->gadget->ops = &dwc3_gadget_ops;
4280 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Thinh Nguyenf551037c2021-01-19 17:36:34 -08004281 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
Peter Chene81a7012020-08-21 10:55:48 +08004282 dwc->gadget->sg_supported = true;
4283 dwc->gadget->name = "dwc3-gadget";
Thinh Nguyen475e8be2021-04-13 19:13:18 -07004284 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
Felipe Balbi72246da2011-08-19 18:10:58 +03004285
4286 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004287 * FIXME We might be setting max_speed to <SUPER, however versions
4288 * <2.20a of dwc3 have an issue with metastability (documented
4289 * elsewhere in this driver) which tells us we can't set max speed to
4290 * anything lower than SUPER.
4291 *
4292 * Because gadget.max_speed is only used by composite.c and function
4293 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4294 * to happen so we avoid sending SuperSpeed Capability descriptor
4295 * together with our BOS descriptor as that could confuse host into
4296 * thinking we can handle super speed.
4297 *
4298 * Note that, in fact, we won't even support GetBOS requests when speed
4299 * is less than super speed because we don't have means, yet, to tell
4300 * composite.c that we are USB 2.0 + LPM ECN.
4301 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07004302 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02004303 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02004304 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004305 dwc->revision);
4306
Peter Chene81a7012020-08-21 10:55:48 +08004307 dwc->gadget->max_speed = dwc->maximum_speed;
Thinh Nguyen67848142021-01-19 17:36:21 -08004308 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06004309
4310 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03004311 * REVISIT: Here we should clear all pending IRQs to be
4312 * sure we're starting from a well known location.
4313 */
4314
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00004315 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03004316 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03004317 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08004318
4319 ret = usb_add_gadget(dwc->gadget);
4320 if (ret) {
4321 dev_err(dwc->dev, "failed to add gadget\n");
4322 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03004323 }
4324
Thinh Nguyen072cab8a2021-01-19 17:36:28 -08004325 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4326 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4327 else
4328 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02004329
Felipe Balbi72246da2011-08-19 18:10:58 +03004330 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03004331
Peter Chene81a7012020-08-21 10:55:48 +08004332err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004333 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08004334err4:
4335 usb_put_gadget(dwc->gadget);
Jack Pham03715ea2021-05-28 09:04:05 -07004336 dwc->gadget = NULL;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004337err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03004338 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4339 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03004340
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004341err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004342 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03004343
Felipe Balbi7d5e6502017-04-07 13:34:21 +03004344err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304345 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03004346 dwc->ep0_trb, dwc->ep0_trb_addr);
4347
Felipe Balbi72246da2011-08-19 18:10:58 +03004348err0:
4349 return ret;
4350}
4351
Felipe Balbi7415f172012-04-30 14:56:33 +03004352/* -------------------------------------------------------------------------- */
4353
Felipe Balbi72246da2011-08-19 18:10:58 +03004354void dwc3_gadget_exit(struct dwc3 *dwc)
4355{
Jack Pham03715ea2021-05-28 09:04:05 -07004356 if (!dwc->gadget)
4357 return;
4358
Jack Phambb9c74a2021-05-01 02:35:58 -07004359 usb_del_gadget(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03004360 dwc3_gadget_free_endpoints(dwc);
Jack Phambb9c74a2021-05-01 02:35:58 -07004361 usb_put_gadget(dwc->gadget);
Felipe Balbi905dc042017-01-05 14:46:52 +02004362 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004363 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02004364 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05304365 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03004366 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03004367}
Felipe Balbi7415f172012-04-30 14:56:33 +03004368
Felipe Balbi0b0231a2014-10-07 10:19:23 -05004369int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03004370{
Roger Quadros9772b472016-04-12 11:33:29 +03004371 if (!dwc->gadget_driver)
4372 return 0;
4373
Roger Quadros1551e352017-02-15 14:16:26 +02004374 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004375 dwc3_disconnect_gadget(dwc);
4376 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004377
4378 return 0;
4379}
4380
4381int dwc3_gadget_resume(struct dwc3 *dwc)
4382{
Felipe Balbi7415f172012-04-30 14:56:33 +03004383 int ret;
4384
Wesley Cheng8217f072021-09-16 19:18:52 -07004385 if (!dwc->gadget_driver || !dwc->softconnect)
Roger Quadros9772b472016-04-12 11:33:29 +03004386 return 0;
4387
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004388 ret = __dwc3_gadget_start(dwc);
4389 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004390 goto err0;
4391
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004392 ret = dwc3_gadget_run_stop(dwc, true, false);
4393 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03004394 goto err1;
4395
Felipe Balbi7415f172012-04-30 14:56:33 +03004396 return 0;
4397
4398err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03004399 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03004400
4401err0:
4402 return ret;
4403}
Felipe Balbifc8bb912016-05-16 13:14:48 +03004404
4405void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4406{
4407 if (dwc->pending_events) {
4408 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4409 dwc->pending_events = false;
4410 enable_irq(dwc->irq_gadget);
4411 }
4412}