blob: 82bc075ba97c673a5cc166341f4ad22334efbf79 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
Alexander A. Klimov10623b82020-07-11 15:58:04 +02005 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbid5370102018-08-14 10:42:43 +030030#define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
Felipe Balbif62afb42018-04-11 10:34:34 +030031 & ~((d)->interval - 1))
32
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020033/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030034 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020035 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030038 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 */
41int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42{
43 u32 reg;
44
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47
48 switch (mode) {
Greg Kroah-Hartman62fb45d2020-06-18 16:42:06 +020049 case USB_TEST_J:
50 case USB_TEST_K:
51 case USB_TEST_SE0_NAK:
52 case USB_TEST_PACKET:
53 case USB_TEST_FORCE_ENABLE:
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020054 reg |= mode << 1;
55 break;
56 default:
57 return -EINVAL;
58 }
59
Thinh Nguyen5b738212019-10-23 19:15:43 -070060 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020061
62 return 0;
63}
64
Felipe Balbi8598bde2012-01-02 18:55:57 +020065/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030066 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030067 * @dwc: pointer to our context structure
68 *
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
71 */
72int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77
78 return DWC3_DSTS_USBLNKST(reg);
79}
80
81/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030082 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020083 * @dwc: pointer to our context structure
84 * @state: the state to put link into
85 *
86 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080087 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 */
89int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080091 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 u32 reg;
93
Paul Zimmerman802fde92012-04-27 13:10:52 +030094 /*
95 * Wait until device controller is ready. Only applies to 1.94a and
96 * later RTL.
97 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -070098 if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 while (--retries) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
102 udelay(5);
103 else
104 break;
105 }
106
107 if (retries <= 0)
108 return -ETIMEDOUT;
109 }
110
Felipe Balbi8598bde2012-01-02 18:55:57 +0200111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113
Thinh Nguyen2e708fa2019-10-23 19:15:55 -0700114 /* set no action before sending new link state change */
115 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 /* set requested state */
118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120
Paul Zimmerman802fde92012-04-27 13:10:52 +0300121 /*
122 * The following code is racy when called from dwc3_gadget_wakeup,
123 * and is not needed, at least on newer versions
124 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700125 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +0300126 return 0;
127
Felipe Balbi8598bde2012-01-02 18:55:57 +0200128 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300129 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 while (--retries) {
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 if (DWC3_DSTS_USBLNKST(reg) == state)
134 return 0;
135
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800136 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 }
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 return -ETIMEDOUT;
140}
141
John Youndca01192016-05-19 17:26:05 -0700142/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300143 * dwc3_ep_inc_trb - increment a trb index.
144 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700145 *
146 * The index should never point to the link TRB. After incrementing,
147 * if it is point to the link TRB, wrap around to the beginning. The
148 * link TRB is always at the last TRB entry.
149 */
150static void dwc3_ep_inc_trb(u8 *index)
151{
152 (*index)++;
153 if (*index == (DWC3_TRB_NUM - 1))
154 *index = 0;
155}
156
Felipe Balbibfad65e2017-04-19 14:59:27 +0300157/**
158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159 * @dep: The endpoint whose enqueue pointer we're incrementing
160 */
Felipe Balbief966b92016-04-05 13:09:51 +0300161static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162{
John Youndca01192016-05-19 17:26:05 -0700163 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300164}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200165
Felipe Balbibfad65e2017-04-19 14:59:27 +0300166/**
167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168 * @dep: The endpoint whose enqueue pointer we're incrementing
169 */
Felipe Balbief966b92016-04-05 13:09:51 +0300170static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171{
John Youndca01192016-05-19 17:26:05 -0700172 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200173}
174
Wei Yongjun69102512018-03-29 02:20:10 +0000175static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
Felipe Balbic91815b2018-03-26 13:14:47 +0300176 struct dwc3_request *req, int status)
177{
178 struct dwc3 *dwc = dep->dwc;
179
Felipe Balbic91815b2018-03-26 13:14:47 +0300180 list_del(&req->list);
181 req->remaining = 0;
Jack Phambd6742242019-01-10 12:39:55 -0800182 req->needs_extra_trb = false;
Felipe Balbic91815b2018-03-26 13:14:47 +0300183
184 if (req->request.status == -EINPROGRESS)
185 req->request.status = status;
186
187 if (req->trb)
188 usb_gadget_unmap_request_by_dev(dwc->sysdev,
189 &req->request, req->direction);
190
191 req->trb = NULL;
192 trace_dwc3_gadget_giveback(req);
193
194 if (dep->number > 1)
195 pm_runtime_put(dwc->dev);
196}
197
Felipe Balbibfad65e2017-04-19 14:59:27 +0300198/**
199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
200 * @dep: The endpoint to whom the request belongs to
201 * @req: The request we're giving back
202 * @status: completion code for the request
203 *
204 * Must be called with controller's lock held and interrupts disabled. This
205 * function will unmap @req and call its ->complete() callback to notify upper
206 * layers that it has completed.
207 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300208void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
209 int status)
210{
211 struct dwc3 *dwc = dep->dwc;
212
Felipe Balbic91815b2018-03-26 13:14:47 +0300213 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbia3af5e32019-01-11 12:57:09 +0200214 req->status = DWC3_REQUEST_STATUS_COMPLETED;
Felipe Balbi72246da2011-08-19 18:10:58 +0300215
216 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200217 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 spin_lock(&dwc->lock);
219}
220
Felipe Balbibfad65e2017-04-19 14:59:27 +0300221/**
222 * dwc3_send_gadget_generic_command - issue a generic command for the controller
223 * @dwc: pointer to the controller context
224 * @cmd: the command to be issued
225 * @param: command parameter
226 *
227 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
228 * and wait for its completion.
229 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300230int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231 u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300232{
233 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300234 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300235 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300236 u32 reg;
237
238 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
239 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240
241 do {
242 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
243 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300244 status = DWC3_DGCMD_STATUS(reg);
245 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 ret = -EINVAL;
247 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300248 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100249 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250
251 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300252 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300253 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300254 }
255
Felipe Balbi71f7e702016-05-23 14:16:19 +0300256 trace_dwc3_gadget_generic_cmd(cmd, param, status);
257
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300258 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300259}
260
Felipe Balbic36d8e92016-04-04 12:46:33 +0300261static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262
Felipe Balbibfad65e2017-04-19 14:59:27 +0300263/**
264 * dwc3_send_gadget_ep_cmd - issue an endpoint command
265 * @dep: the endpoint to which the command is going to be issued
266 * @cmd: the command to be issued
267 * @params: parameters to the command
268 *
269 * Caller should handle locking. This function will issue @cmd with given
270 * @params to @dep and wait for its completion.
271 */
Felipe Balbie319bd62020-08-13 08:35:38 +0300272int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
Felipe Balbi2cd47182016-04-12 16:42:43 +0300273 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300274{
Felipe Balbi8897a762016-09-22 10:56:08 +0300275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300276 struct dwc3 *dwc = dep->dwc;
Yu Chen1c0e69a2020-05-21 16:46:43 +0800277 u32 timeout = 5000;
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700278 u32 saved_config = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300279 u32 reg;
280
Felipe Balbi0933df12016-05-23 14:02:33 +0300281 int cmd_status = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300282 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300283
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300284 /*
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700285 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
286 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
287 * endpoint command.
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300288 *
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700289 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
290 * settings. Restore them after the command is completed.
291 *
292 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300293 */
Peter Chene81a7012020-08-21 10:55:48 +0800294 if (dwc->gadget->speed <= USB_SPEED_HIGH) {
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300295 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
296 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700297 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300298 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300299 }
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700300
301 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
302 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
303 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
304 }
305
306 if (saved_config)
307 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300308 }
309
Felipe Balbi59999142016-09-22 12:25:28 +0300310 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300311 int needs_wakeup;
312
313 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
314 dwc->link_state == DWC3_LINK_STATE_U2 ||
315 dwc->link_state == DWC3_LINK_STATE_U3);
316
317 if (unlikely(needs_wakeup)) {
318 ret = __dwc3_gadget_wakeup(dwc);
319 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
320 ret);
321 }
322 }
323
Felipe Balbi2eb88012016-04-12 16:53:39 +0300324 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
325 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
326 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
Felipe Balbi8897a762016-09-22 10:56:08 +0300328 /*
329 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
330 * not relying on XferNotReady, we can make use of a special "No
331 * Response Update Transfer" command where we should clear both CmdAct
332 * and CmdIOC bits.
333 *
334 * With this, we don't need to wait for command completion and can
335 * straight away issue further commands to the endpoint.
336 *
337 * NOTICE: We're making an assumption that control endpoints will never
338 * make use of Update Transfer command. This is a safe assumption
339 * because we can never have more than one request at a time with
340 * Control Endpoints. If anybody changes that assumption, this chunk
341 * needs to be updated accordingly.
342 */
343 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
344 !usb_endpoint_xfer_isoc(desc))
345 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
346 else
347 cmd |= DWC3_DEPCMD_CMDACT;
348
349 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300350 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300351 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300353 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000354
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000355 switch (cmd_status) {
356 case 0:
357 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300358 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000359 case DEPEVT_TRANSFER_NO_RESOURCE:
Thinh Nguyenf7ac582e2020-03-29 16:13:16 -0700360 dev_WARN(dwc->dev, "No resource for %s\n",
361 dep->name);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000362 ret = -EINVAL;
363 break;
364 case DEPEVT_TRANSFER_BUS_EXPIRY:
365 /*
366 * SW issues START TRANSFER command to
367 * isochronous ep with future frame interval. If
368 * future interval time has already passed when
369 * core receives the command, it will respond
370 * with an error status of 'Bus Expiry'.
371 *
372 * Instead of always returning -EINVAL, let's
373 * give a hint to the gadget driver that this is
374 * the case by returning -EAGAIN.
375 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000376 ret = -EAGAIN;
377 break;
378 default:
379 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
380 }
381
Felipe Balbic0ca3242016-04-04 09:11:51 +0300382 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300383 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300384 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300385
Felipe Balbif6bb2252016-05-23 13:53:34 +0300386 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300387 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300388 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300389 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300390
Felipe Balbi0933df12016-05-23 14:02:33 +0300391 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
392
Thinh Nguyen9bc33952020-03-29 16:13:04 -0700393 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
394 if (ret == 0)
395 dep->flags |= DWC3_EP_TRANSFER_STARTED;
396
397 if (ret != -ETIMEDOUT)
398 dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +0300399 }
400
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700401 if (saved_config) {
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300402 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
Thinh Nguyen87dd9612018-09-11 12:42:05 -0700403 reg |= saved_config;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300404 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
405 }
406
Felipe Balbic0ca3242016-04-04 09:11:51 +0300407 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408}
409
John Youn50c763f2016-05-31 17:49:56 -0700410static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
411{
412 struct dwc3 *dwc = dep->dwc;
413 struct dwc3_gadget_ep_cmd_params params;
414 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
415
416 /*
417 * As of core revision 2.60a the recommended programming model
418 * is to set the ClearPendIN bit when issuing a Clear Stall EP
419 * command for IN endpoints. This is to prevent an issue where
420 * some (non-compliant) hosts may not send ACK TPs for pending
421 * IN transfers due to a mishandled error condition. Synopsys
422 * STAR 9000614252.
423 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -0700424 if (dep->direction &&
425 !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
Peter Chene81a7012020-08-21 10:55:48 +0800426 (dwc->gadget->speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700427 cmd |= DWC3_DEPCMD_CLEARPENDIN;
428
429 memset(&params, 0, sizeof(params));
430
Felipe Balbi2cd47182016-04-12 16:42:43 +0300431 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700432}
433
Felipe Balbi72246da2011-08-19 18:10:58 +0300434static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200435 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300436{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300437 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438
439 return dep->trb_pool_dma + offset;
440}
441
442static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
443{
444 struct dwc3 *dwc = dep->dwc;
445
446 if (dep->trb_pool)
447 return 0;
448
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530449 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300450 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
451 &dep->trb_pool_dma, GFP_KERNEL);
452 if (!dep->trb_pool) {
453 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
454 dep->name);
455 return -ENOMEM;
456 }
457
458 return 0;
459}
460
461static void dwc3_free_trb_pool(struct dwc3_ep *dep)
462{
463 struct dwc3 *dwc = dep->dwc;
464
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530465 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 dep->trb_pool, dep->trb_pool_dma);
467
468 dep->trb_pool = NULL;
469 dep->trb_pool_dma = 0;
470}
471
Felipe Balbi20d1d432018-04-09 12:49:02 +0300472static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
473{
474 struct dwc3_gadget_ep_cmd_params params;
475
476 memset(&params, 0x00, sizeof(params));
477
478 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
479
480 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
481 &params);
482}
John Younc4509602016-02-16 20:10:53 -0800483
484/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300485 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800486 * @dep: endpoint that is being enabled
487 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300488 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
489 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800490 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300491 * The assignment of transfer resources cannot perfectly follow the data book
492 * due to the fact that the controller driver does not have all knowledge of the
493 * configuration in advance. It is given this information piecemeal by the
494 * composite gadget framework after every SET_CONFIGURATION and
495 * SET_INTERFACE. Trying to follow the databook programming model in this
496 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800497 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300498 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
499 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
500 * incorrect in the scenario of multiple interfaces.
501 *
502 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800503 * endpoint on alt setting (8.1.6).
504 *
505 * The following simplified method is used instead:
506 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300507 * All hardware endpoints can be assigned a transfer resource and this setting
508 * will stay persistent until either a core reset or hibernation. So whenever we
509 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
510 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800511 * guaranteed that there are as many transfer resources as endpoints.
512 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300513 * This function is called for each endpoint when it is being enabled but is
514 * triggered only when called for EP0-out, which always happens first, and which
515 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800516 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300517static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300518{
519 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300520 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800522 int i;
523 int ret;
524
525 if (dep->number)
526 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527
528 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800529 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300530 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300531
Felipe Balbi2cd47182016-04-12 16:42:43 +0300532 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800533 if (ret)
534 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300535
John Younc4509602016-02-16 20:10:53 -0800536 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
537 struct dwc3_ep *dep = dwc->eps[i];
538
539 if (!dep)
540 continue;
541
Felipe Balbib07c2db2018-04-09 12:46:47 +0300542 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800543 if (ret)
544 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545 }
546
547 return 0;
548}
549
Felipe Balbib07c2db2018-04-09 12:46:47 +0300550static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300551{
John Youn39ebb052016-11-09 16:36:28 -0800552 const struct usb_ss_ep_comp_descriptor *comp_desc;
553 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300555 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556
John Youn39ebb052016-11-09 16:36:28 -0800557 comp_desc = dep->endpoint.comp_desc;
558 desc = dep->endpoint.desc;
559
Felipe Balbi72246da2011-08-19 18:10:58 +0300560 memset(&params, 0x00, sizeof(params));
561
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300562 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900563 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
564
565 /* Burst size is only needed in SuperSpeed mode */
Peter Chene81a7012020-08-21 10:55:48 +0800566 if (dwc->gadget->speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300567 u32 burst = dep->endpoint.maxburst;
Felipe Balbie319bd62020-08-13 08:35:38 +0300568
Felipe Balbi676e3492016-04-26 10:49:07 +0300569 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900570 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300571
Felipe Balbia2d23f02018-04-09 12:40:48 +0300572 params.param0 |= action;
573 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600574 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600575
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300576 if (usb_endpoint_xfer_control(desc))
577 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300578
579 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
580 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200582 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
Thinh Nguyen548f8b32020-05-05 19:46:45 -0700584 | DWC3_DEPCFG_XFER_COMPLETE_EN
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300585 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300586 dep->stream_capable = true;
587 }
588
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500589 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 /*
593 * We are doing 1:1 mapping for endpoints, meaning
594 * Physical Endpoints 2 maps to Logical Endpoint 2 and
595 * so on. We consider the direction bit as part of the physical
596 * endpoint number. So USB endpoint 0x81 is 0x03.
597 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300598 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300599
600 /*
601 * We must use the lower 16 TX FIFOs even though
602 * HW might have more
603 */
604 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300605 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
607 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300608 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 dep->interval = 1 << (desc->bInterval - 1);
610 }
611
Felipe Balbi2cd47182016-04-12 16:42:43 +0300612 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300613}
614
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700615static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
616 bool interrupt);
617
Felipe Balbi72246da2011-08-19 18:10:58 +0300618/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300619 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300620 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300621 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300623 * Caller should take care of locking. Execute all necessary commands to
624 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300626static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300627{
John Youn39ebb052016-11-09 16:36:28 -0800628 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800630
Felipe Balbi72246da2011-08-19 18:10:58 +0300631 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300632 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633
634 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300635 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 if (ret)
637 return ret;
638 }
639
Felipe Balbib07c2db2018-04-09 12:46:47 +0300640 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 if (ret)
642 return ret;
643
644 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200645 struct dwc3_trb *trb_st_hw;
646 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300647
Felipe Balbi72246da2011-08-19 18:10:58 +0300648 dep->type = usb_endpoint_type(desc);
649 dep->flags |= DWC3_EP_ENABLED;
650
651 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
652 reg |= DWC3_DALEPENA_EP(dep->number);
653 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
654
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300655 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200656 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657
John Youn0d257442016-05-19 17:26:08 -0700658 /* Initialize the TRB ring */
659 dep->trb_dequeue = 0;
660 dep->trb_enqueue = 0;
661 memset(dep->trb_pool, 0,
662 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
663
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300664 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 trb_st_hw = &dep->trb_pool[0];
666
Felipe Balbif6bafc62012-02-06 11:04:53 +0200667 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200668 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
669 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
670 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
671 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300672 }
673
Felipe Balbia97ea992016-09-29 16:28:56 +0300674 /*
675 * Issue StartTransfer here with no-op TRB so we can always rely on No
676 * Response Update Transfer command.
677 */
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700678 if (usb_endpoint_xfer_bulk(desc) ||
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300679 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300680 struct dwc3_gadget_ep_cmd_params params;
681 struct dwc3_trb *trb;
682 dma_addr_t trb_dma;
683 u32 cmd;
684
685 memset(&params, 0, sizeof(params));
686 trb = &dep->trb_pool[0];
687 trb_dma = dwc3_trb_dma_offset(dep, trb);
688
689 params.param0 = upper_32_bits(trb_dma);
690 params.param1 = lower_32_bits(trb_dma);
691
692 cmd = DWC3_DEPCMD_STARTTRANSFER;
693
694 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
695 if (ret < 0)
696 return ret;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -0700697
698 if (dep->stream_capable) {
699 /*
700 * For streams, at start, there maybe a race where the
701 * host primes the endpoint before the function driver
702 * queues a request to initiate a stream. In that case,
703 * the controller will not see the prime to generate the
704 * ERDY and start stream. To workaround this, issue a
705 * no-op TRB as normal, but end it immediately. As a
706 * result, when the function driver queues the request,
707 * the next START_TRANSFER command will cause the
708 * controller to generate an ERDY to initiate the
709 * stream.
710 */
711 dwc3_stop_active_transfer(dep, true, true);
712
713 /*
714 * All stream eps will reinitiate stream on NoStream
715 * rejection until we can determine that the host can
716 * prime after the first transfer.
717 */
718 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
719 }
Felipe Balbia97ea992016-09-29 16:28:56 +0300720 }
721
Felipe Balbi2870e502016-11-03 13:53:29 +0200722out:
723 trace_dwc3_gadget_ep_enable(dep);
724
Felipe Balbi72246da2011-08-19 18:10:58 +0300725 return 0;
726}
727
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200728static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300729{
730 struct dwc3_request *req;
731
Felipe Balbic5353b22019-02-13 13:00:54 +0200732 dwc3_stop_active_transfer(dep, true, false);
Felipe Balbi69450c42016-05-30 13:37:02 +0300733
Felipe Balbi0e146022016-06-21 10:32:02 +0300734 /* - giveback all requests to gadget driver */
735 while (!list_empty(&dep->started_list)) {
736 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200737
Felipe Balbi0e146022016-06-21 10:32:02 +0300738 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200739 }
740
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200741 while (!list_empty(&dep->pending_list)) {
742 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300745 }
Felipe Balbid8eca642019-10-31 11:07:13 +0200746
747 while (!list_empty(&dep->cancelled_list)) {
748 req = next_request(&dep->cancelled_list);
749
750 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
751 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300752}
753
754/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300755 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300756 * @dep: the endpoint to disable
757 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300758 * This function undoes what __dwc3_gadget_ep_enable did and also removes
759 * requests which are currently being processed by the hardware and those which
760 * are not yet scheduled.
761 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200762 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300764static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
765{
766 struct dwc3 *dwc = dep->dwc;
767 u32 reg;
768
Felipe Balbi2870e502016-11-03 13:53:29 +0200769 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500770
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200771 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300772
Felipe Balbi687ef982014-04-16 10:30:33 -0500773 /* make sure HW endpoint isn't stalled */
774 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500775 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500776
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
778 reg &= ~DWC3_DALEPENA_EP(dep->number);
779 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
780
Felipe Balbi879631a2011-09-30 10:58:47 +0300781 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 dep->type = 0;
Felipe Balbi3aec9912019-01-21 13:08:44 +0200783 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300784
John Youn39ebb052016-11-09 16:36:28 -0800785 /* Clear out the ep descriptors for non-ep0 */
786 if (dep->number > 1) {
787 dep->endpoint.comp_desc = NULL;
788 dep->endpoint.desc = NULL;
789 }
790
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 return 0;
792}
793
794/* -------------------------------------------------------------------------- */
795
796static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
797 const struct usb_endpoint_descriptor *desc)
798{
799 return -EINVAL;
800}
801
802static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
803{
804 return -EINVAL;
805}
806
807/* -------------------------------------------------------------------------- */
808
809static int dwc3_gadget_ep_enable(struct usb_ep *ep,
810 const struct usb_endpoint_descriptor *desc)
811{
812 struct dwc3_ep *dep;
813 struct dwc3 *dwc;
814 unsigned long flags;
815 int ret;
816
817 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
818 pr_debug("dwc3: invalid parameters\n");
819 return -EINVAL;
820 }
821
822 if (!desc->wMaxPacketSize) {
823 pr_debug("dwc3: missing wMaxPacketSize\n");
824 return -EINVAL;
825 }
826
827 dep = to_dwc3_ep(ep);
828 dwc = dep->dwc;
829
Felipe Balbi95ca9612015-12-10 13:08:20 -0600830 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
831 "%s is already enabled\n",
832 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300833 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300834
Felipe Balbi72246da2011-08-19 18:10:58 +0300835 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300836 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 spin_unlock_irqrestore(&dwc->lock, flags);
838
839 return ret;
840}
841
842static int dwc3_gadget_ep_disable(struct usb_ep *ep)
843{
844 struct dwc3_ep *dep;
845 struct dwc3 *dwc;
846 unsigned long flags;
847 int ret;
848
849 if (!ep) {
850 pr_debug("dwc3: invalid parameters\n");
851 return -EINVAL;
852 }
853
854 dep = to_dwc3_ep(ep);
855 dwc = dep->dwc;
856
Felipe Balbi95ca9612015-12-10 13:08:20 -0600857 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
858 "%s is already disabled\n",
859 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300861
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 spin_lock_irqsave(&dwc->lock, flags);
863 ret = __dwc3_gadget_ep_disable(dep);
864 spin_unlock_irqrestore(&dwc->lock, flags);
865
866 return ret;
867}
868
869static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300870 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300871{
872 struct dwc3_request *req;
873 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300874
875 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900876 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300878
Felipe Balbi31a2f5a2018-05-07 15:19:31 +0300879 req->direction = dep->direction;
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 req->epnum = dep->number;
881 req->dep = dep;
Felipe Balbia3af5e32019-01-11 12:57:09 +0200882 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300883
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500884 trace_dwc3_alloc_request(req);
885
Felipe Balbi72246da2011-08-19 18:10:58 +0300886 return &req->request;
887}
888
889static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
890 struct usb_request *request)
891{
892 struct dwc3_request *req = to_dwc3_request(request);
893
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500894 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300895 kfree(req);
896}
897
Felipe Balbi42626912018-04-09 13:01:43 +0300898/**
899 * dwc3_ep_prev_trb - returns the previous TRB in the ring
900 * @dep: The endpoint with the TRB ring
901 * @index: The index of the current TRB in the ring
902 *
903 * Returns the TRB prior to the one pointed to by the index. If the
904 * index is 0, we will wrap backwards, skip the link TRB, and return
905 * the one just before that.
906 */
907static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
908{
909 u8 tmp = index;
910
911 if (!tmp)
912 tmp = DWC3_TRB_NUM - 1;
913
914 return &dep->trb_pool[tmp - 1];
915}
916
917static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
918{
919 struct dwc3_trb *tmp;
920 u8 trbs_left;
921
922 /*
923 * If enqueue & dequeue are equal than it is either full or empty.
924 *
925 * One way to know for sure is if the TRB right before us has HWO bit
926 * set or not. If it has, then we're definitely full and can't fit any
927 * more transfers in our ring.
928 */
929 if (dep->trb_enqueue == dep->trb_dequeue) {
930 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
931 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
932 return 0;
933
934 return DWC3_TRB_NUM - 1;
935 }
936
937 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
938 trbs_left &= (DWC3_TRB_NUM - 1);
939
940 if (dep->trb_dequeue < dep->trb_enqueue)
941 trbs_left--;
942
943 return trbs_left;
944}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300945
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200946static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
Felipe Balbie319bd62020-08-13 08:35:38 +0300947 dma_addr_t dma, unsigned int length, unsigned int chain,
948 unsigned int node, unsigned int stream_id,
949 unsigned int short_not_ok, unsigned int no_interrupt,
950 unsigned int is_last)
Felipe Balbic71fc372011-11-22 11:37:34 +0200951{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300952 struct dwc3 *dwc = dep->dwc;
Peter Chene81a7012020-08-21 10:55:48 +0800953 struct usb_gadget *gadget = dwc->gadget;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300954 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200955
Felipe Balbif6bafc62012-02-06 11:04:53 +0200956 trb->size = DWC3_TRB_SIZE_LENGTH(length);
957 trb->bpl = lower_32_bits(dma);
958 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200959
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200960 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200961 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200962 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200963 break;
964
965 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300966 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530967 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300968
Manu Gautam40d829f2017-07-19 17:07:10 +0530969 /*
970 * USB Specification 2.0 Section 5.9.2 states that: "If
971 * there is only a single transaction in the microframe,
972 * only a DATA0 data packet PID is used. If there are
973 * two transactions per microframe, DATA1 is used for
974 * the first transaction data packet and DATA0 is used
975 * for the second transaction data packet. If there are
976 * three transactions per microframe, DATA2 is used for
977 * the first transaction data packet, DATA1 is used for
978 * the second, and DATA0 is used for the third."
979 *
980 * IOW, we should satisfy the following cases:
981 *
982 * 1) length <= maxpacket
983 * - DATA0
984 *
985 * 2) maxpacket < length <= (2 * maxpacket)
986 * - DATA1, DATA0
987 *
988 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
989 * - DATA2, DATA1, DATA0
990 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300991 if (speed == USB_SPEED_HIGH) {
992 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530993 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530994 unsigned int maxp = usb_endpoint_maxp(ep->desc);
995
996 if (length <= (2 * maxp))
997 mult--;
998
999 if (length <= maxp)
1000 mult--;
1001
1002 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001003 }
1004 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301005 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +03001006 }
Felipe Balbica4d44e2016-03-10 13:53:27 +02001007
1008 /* always enable Interrupt on Missed ISOC */
1009 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +02001010 break;
1011
1012 case USB_ENDPOINT_XFER_BULK:
1013 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001014 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001015 break;
1016 default:
1017 /*
1018 * This is only possible with faulty memory because we
1019 * checked it already :)
1020 */
Felipe Balbi0a695d42016-10-07 11:20:01 +03001021 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1022 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +02001023 }
1024
Tejas Joglekar244add82018-12-10 16:08:13 +05301025 /*
1026 * Enable Continue on Short Packet
1027 * when endpoint is not a stream capable
1028 */
Felipe Balbic9508c82016-10-05 14:26:23 +03001029 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Tejas Joglekar244add82018-12-10 16:08:13 +05301030 if (!dep->stream_capable)
1031 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001032
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001033 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +03001034 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1035 }
1036
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001037 if ((!no_interrupt && !chain) ||
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301038 (dwc3_calc_trbs_left(dep) == 1))
Felipe Balbic9508c82016-10-05 14:26:23 +03001039 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001040
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301041 if (chain)
1042 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001043 else if (dep->stream_capable && is_last)
1044 trb->ctrl |= DWC3_TRB_CTRL_LST;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301045
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001046 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001047 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001048
1049 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001050
Anurag Kumar Vulishab7a4fbe2018-12-01 16:43:29 +05301051 dwc3_ep_inc_enq(dep);
1052
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001053 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001054}
1055
John Youn361572b2016-05-19 17:26:17 -07001056/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001057 * dwc3_prepare_one_trb - setup one TRB from one request
1058 * @dep: endpoint for which this request is prepared
1059 * @req: dwc3_request pointer
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001060 * @trb_length: buffer size of the TRB
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001061 * @chain: should this TRB be chained to the next?
1062 * @node: only for isochronous endpoints. First TRB needs different type.
Thinh Nguyen2b803572020-09-24 01:21:30 -07001063 * @use_bounce_buffer: set to use bounce buffer
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001064 */
1065static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001066 struct dwc3_request *req, unsigned int trb_length,
Thinh Nguyen2b803572020-09-24 01:21:30 -07001067 unsigned int chain, unsigned int node, bool use_bounce_buffer)
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001068{
1069 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301070 dma_addr_t dma;
Felipe Balbie319bd62020-08-13 08:35:38 +03001071 unsigned int stream_id = req->request.stream_id;
1072 unsigned int short_not_ok = req->request.short_not_ok;
1073 unsigned int no_interrupt = req->request.no_interrupt;
1074 unsigned int is_last = req->request.is_last;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301075
Thinh Nguyen2b803572020-09-24 01:21:30 -07001076 if (use_bounce_buffer)
1077 dma = dep->dwc->bounce_addr;
1078 else if (req->request.num_sgs > 0)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301079 dma = sg_dma_address(req->start_sg);
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001080 else
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301081 dma = req->request.dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001082
1083 trb = &dep->trb_pool[dep->trb_enqueue];
1084
1085 if (!req->trb) {
1086 dwc3_gadget_move_started_request(req);
1087 req->trb = trb;
1088 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001089 }
1090
Felipe Balbi09fe1f82018-08-01 13:32:07 +03001091 req->num_trbs++;
1092
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001093 __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node,
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07001094 stream_id, short_not_ok, no_interrupt, is_last);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001095}
1096
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001097/**
1098 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1099 * @dep: The endpoint that the request belongs to
1100 * @req: The request to prepare
1101 * @entry_length: The last SG entry size
1102 * @node: Indicates whether this is not the first entry (for isoc only)
1103 *
1104 * Return the number of TRBs prepared.
1105 */
1106static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1107 struct dwc3_request *req, unsigned int entry_length,
1108 unsigned int node)
1109{
1110 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1111 unsigned int rem = req->request.length % maxp;
1112 unsigned int num_trbs = 1;
1113
1114 if ((req->request.length && req->request.zero && !rem &&
1115 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1116 (!req->direction && rem))
1117 num_trbs++;
1118
1119 if (dwc3_calc_trbs_left(dep) < num_trbs)
1120 return 0;
1121
1122 req->needs_extra_trb = num_trbs > 1;
1123
1124 /* Prepare a normal TRB */
1125 if (req->direction || req->request.length)
1126 dwc3_prepare_one_trb(dep, req, entry_length,
1127 req->needs_extra_trb, node, false);
1128
1129 /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1130 if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1131 dwc3_prepare_one_trb(dep, req,
1132 req->direction ? 0 : maxp - rem,
1133 false, 1, true);
1134
1135 return num_trbs;
1136}
1137
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001138static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001139 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001140{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301141 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001142 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001143 int i;
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001144 unsigned int length = req->request.length;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301145 unsigned int remaining = req->request.num_mapped_sgs
1146 - req->num_queued_sgs;
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001147 unsigned int num_trbs = req->num_trbs;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301148
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001149 /*
1150 * If we resume preparing the request, then get the remaining length of
1151 * the request and resume where we left off.
1152 */
1153 for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1154 length -= sg_dma_len(s);
1155
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301156 for_each_sg(sg, s, remaining, i) {
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001157 unsigned int trb_length;
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001158 bool last_sg = false;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001159
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001160 trb_length = min_t(unsigned int, length, sg_dma_len(s));
1161
1162 length -= trb_length;
1163
Pratham Pratapdad2aff2020-03-02 21:44:43 +00001164 /*
1165 * IOMMU driver is coalescing the list of sgs which shares a
1166 * page boundary into one and giving it to USB driver. With
1167 * this the number of sgs mapped is not equal to the number of
1168 * sgs passed. So mark the chain bit to false if it isthe last
1169 * mapped sg.
1170 */
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001171 if ((i == remaining - 1) || !length)
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001172 last_sg = true;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001173
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001174 if (!dwc3_calc_trbs_left(dep))
1175 break;
1176
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001177 if (last_sg) {
1178 if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001179 goto out;
Felipe Balbic6267a52017-01-05 14:58:46 +02001180 } else {
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001181 dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false);
Felipe Balbic6267a52017-01-05 14:58:46 +02001182 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001183
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301184 /*
1185 * There can be a situation where all sgs in sglist are not
1186 * queued because of insufficient trb number. To handle this
1187 * case, update start_sg to next sg to be queued, so that
1188 * we have free trbs we can continue queuing from where we
1189 * previously stopped
1190 */
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001191 if (!last_sg)
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301192 req->start_sg = sg_next(s);
1193
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301194 req->num_queued_sgs++;
1195
Thinh Nguyen5d187c02020-08-06 19:46:23 -07001196 /*
1197 * The number of pending SG entries may not correspond to the
1198 * number of mapped SG entries. If all the data are queued, then
1199 * don't include unused SG entries.
1200 */
1201 if (length == 0) {
1202 req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs;
1203 break;
1204 }
1205
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001206 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001207 break;
1208 }
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001209
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001210 return req->num_trbs - num_trbs;
1211
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001212out:
Thinh Nguyen30892cb2020-09-24 01:22:01 -07001213 /*
1214 * If we run out of TRBs for MPS alignment setup, then set IOC on the
1215 * previous TRB to get notified for TRB completion to resume when more
1216 * TRBs are available.
1217 *
1218 * Note: normally we shouldn't update the TRB after the HWO bit is set.
1219 * However, the controller doesn't update its internal cache to handle
1220 * the newly prepared TRBs until UPDATE_TRANSFER or START_TRANSFER
1221 * command is executed. At this point, it doesn't happen yet, so we
1222 * should be fine modifying it here.
1223 */
1224 if (i) {
1225 struct dwc3_trb *trb;
1226
1227 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1228 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1229 }
1230
Thinh Nguyen13111fc2020-09-24 01:21:49 -07001231 return req->num_trbs - num_trbs;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001232}
1233
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001234static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001235 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001236{
Thinh Nguyencb1b3992020-09-24 01:22:07 -07001237 return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001238}
1239
Felipe Balbi72246da2011-08-19 18:10:58 +03001240/*
1241 * dwc3_prepare_trbs - setup TRBs from requests
1242 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001243 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001244 * The function goes through the requests list and sets up TRBs for the
1245 * transfers. The function returns once there are no more TRBs available or
1246 * it runs out of requests.
Thinh Nguyen490410b2020-09-24 01:21:55 -07001247 *
1248 * Returns the number of TRBs prepared or negative errno.
Felipe Balbi72246da2011-08-19 18:10:58 +03001249 */
Thinh Nguyen490410b2020-09-24 01:21:55 -07001250static int dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001251{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001252 struct dwc3_request *req, *n;
Thinh Nguyen490410b2020-09-24 01:21:55 -07001253 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001254
1255 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1256
Felipe Balbid86c5a62016-10-25 13:48:52 +03001257 /*
1258 * We can get in a situation where there's a request in the started list
1259 * but there weren't enough TRBs to fully kick it in the first time
1260 * around, so it has been waiting for more TRBs to be freed up.
1261 *
1262 * In that case, we should check if we have a request with pending_sgs
1263 * in the started list and prepare TRBs for that request first,
1264 * otherwise we will prepare TRBs completely out of order and that will
1265 * break things.
1266 */
1267 list_for_each_entry(req, &dep->started_list, list) {
Thinh Nguyen490410b2020-09-24 01:21:55 -07001268 if (req->num_pending_sgs > 0) {
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001269 ret = dwc3_prepare_trbs_sg(dep, req);
Thinh Nguyen490410b2020-09-24 01:21:55 -07001270 if (!ret)
1271 return ret;
1272 }
Felipe Balbid86c5a62016-10-25 13:48:52 +03001273
1274 if (!dwc3_calc_trbs_left(dep))
Thinh Nguyen490410b2020-09-24 01:21:55 -07001275 return ret;
Thinh Nguyen63c7bb22020-05-15 16:40:46 -07001276
1277 /*
1278 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1279 * burst capability may try to read and use TRBs beyond the
1280 * active transfer instead of stopping.
1281 */
1282 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001283 return ret;
Felipe Balbid86c5a62016-10-25 13:48:52 +03001284 }
1285
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001286 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001287 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001288
1289 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1290 dep->direction);
1291 if (ret)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001292 return ret;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001293
1294 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301295 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301296 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001297 req->num_pending_sgs = req->request.num_mapped_sgs;
1298
Felipe Balbi1f512112016-08-12 13:17:27 +03001299 if (req->num_pending_sgs > 0)
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001300 ret = dwc3_prepare_trbs_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001301 else
Thinh Nguyen7f2958d2020-09-24 01:22:14 -07001302 ret = dwc3_prepare_trbs_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001303
Thinh Nguyen490410b2020-09-24 01:21:55 -07001304 if (!ret || !dwc3_calc_trbs_left(dep))
1305 return ret;
Thinh Nguyenaefe3d22020-05-05 19:47:03 -07001306
1307 /*
1308 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1309 * burst capability may try to read and use TRBs beyond the
1310 * active transfer instead of stopping.
1311 */
1312 if (dep->stream_capable && req->request.is_last)
Thinh Nguyen490410b2020-09-24 01:21:55 -07001313 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 }
Thinh Nguyen490410b2020-09-24 01:21:55 -07001315
1316 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001317}
1318
Thinh Nguyen8d990872020-03-29 16:12:57 -07001319static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1320
Felipe Balbi7fdca762017-09-05 14:41:34 +03001321static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001322{
1323 struct dwc3_gadget_ep_cmd_params params;
1324 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001325 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 int ret;
1327 u32 cmd;
1328
Thinh Nguyen490410b2020-09-24 01:21:55 -07001329 ret = dwc3_prepare_trbs(dep);
1330 if (ret <= 0)
1331 return ret;
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001332
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001333 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001334
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001335 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 if (!req) {
1337 dep->flags |= DWC3_EP_PENDING_REQUEST;
1338 return 0;
1339 }
1340
1341 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001342
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001343 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301344 params.param0 = upper_32_bits(req->trb_dma);
1345 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001346 cmd = DWC3_DEPCMD_STARTTRANSFER;
1347
Anurag Kumar Vulishaa7351802018-12-01 16:43:25 +05301348 if (dep->stream_capable)
1349 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1350
Felipe Balbi7fdca762017-09-05 14:41:34 +03001351 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1352 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301353 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001354 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1355 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301356 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001357
Felipe Balbi2cd47182016-04-12 16:42:43 +03001358 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 if (ret < 0) {
Thinh Nguyen8d990872020-03-29 16:12:57 -07001360 struct dwc3_request *tmp;
1361
1362 if (ret == -EAGAIN)
1363 return ret;
1364
1365 dwc3_stop_active_transfer(dep, true, true);
1366
1367 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1368 dwc3_gadget_move_cancelled_request(req);
1369
1370 /* If ep isn't started, then there's no end transfer pending */
1371 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1372 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1373
Felipe Balbi72246da2011-08-19 18:10:58 +03001374 return ret;
1375 }
1376
Thinh Nguyene0d19562020-05-05 19:46:57 -07001377 if (dep->stream_capable && req->request.is_last)
1378 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1379
Felipe Balbi72246da2011-08-19 18:10:58 +03001380 return 0;
1381}
1382
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001383static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1384{
1385 u32 reg;
1386
1387 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1388 return DWC3_DSTS_SOFFN(reg);
1389}
1390
Thinh Nguyend92021f2018-11-14 22:56:54 -08001391/**
1392 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1393 * @dep: isoc endpoint
1394 *
1395 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1396 * microframe number reported by the XferNotReady event for the future frame
1397 * number to start the isoc transfer.
1398 *
1399 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1400 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1401 * XferNotReady event are invalid. The driver uses this number to schedule the
1402 * isochronous transfer and passes it to the START TRANSFER command. Because
1403 * this number is invalid, the command may fail. If BIT[15:14] matches the
1404 * internal 16-bit microframe, the START TRANSFER command will pass and the
1405 * transfer will start at the scheduled time, if it is off by 1, the command
1406 * will still pass, but the transfer will start 2 seconds in the future. For all
1407 * other conditions, the START TRANSFER command will fail with bus-expiry.
1408 *
1409 * In order to workaround this issue, we can test for the correct combination of
1410 * BIT[15:14] by sending START TRANSFER commands with different values of
1411 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1412 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1413 * As the result, within the 4 possible combinations for BIT[15:14], there will
1414 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1415 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1416 * value is the correct combination.
1417 *
1418 * Since there are only 4 outcomes and the results are ordered, we can simply
1419 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1420 * deduce the smaller successful combination.
1421 *
1422 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1423 * of BIT[15:14]. The correct combination is as follow:
1424 *
1425 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1426 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1427 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1428 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1429 *
1430 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1431 * endpoints.
1432 */
Felipe Balbi25abad62018-08-14 10:41:19 +03001433static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301434{
Thinh Nguyend92021f2018-11-14 22:56:54 -08001435 int cmd_status = 0;
1436 bool test0;
1437 bool test1;
1438
1439 while (dep->combo_num < 2) {
1440 struct dwc3_gadget_ep_cmd_params params;
1441 u32 test_frame_number;
1442 u32 cmd;
1443
1444 /*
1445 * Check if we can start isoc transfer on the next interval or
1446 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1447 */
Michael Grzeschikca143782020-07-01 20:24:51 +02001448 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001449 test_frame_number |= dep->combo_num << 14;
1450 test_frame_number += max_t(u32, 4, dep->interval);
1451
1452 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1453 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1454
1455 cmd = DWC3_DEPCMD_STARTTRANSFER;
1456 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1457 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1458
1459 /* Redo if some other failure beside bus-expiry is received */
1460 if (cmd_status && cmd_status != -EAGAIN) {
1461 dep->start_cmd_status = 0;
1462 dep->combo_num = 0;
Felipe Balbi25abad62018-08-14 10:41:19 +03001463 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001464 }
1465
1466 /* Store the first test status */
1467 if (dep->combo_num == 0)
1468 dep->start_cmd_status = cmd_status;
1469
1470 dep->combo_num++;
1471
1472 /*
1473 * End the transfer if the START_TRANSFER command is successful
1474 * to wait for the next XferNotReady to test the command again
1475 */
1476 if (cmd_status == 0) {
Felipe Balbic5353b22019-02-13 13:00:54 +02001477 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbi25abad62018-08-14 10:41:19 +03001478 return 0;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001479 }
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301480 }
1481
Thinh Nguyend92021f2018-11-14 22:56:54 -08001482 /* test0 and test1 are both completed at this point */
1483 test0 = (dep->start_cmd_status == 0);
1484 test1 = (cmd_status == 0);
1485
1486 if (!test0 && test1)
1487 dep->combo_num = 1;
1488 else if (!test0 && !test1)
1489 dep->combo_num = 2;
1490 else if (test0 && !test1)
1491 dep->combo_num = 3;
1492 else if (test0 && test1)
1493 dep->combo_num = 0;
1494
Michael Grzeschikca143782020-07-01 20:24:51 +02001495 dep->frame_number &= DWC3_FRNUMBER_MASK;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001496 dep->frame_number |= dep->combo_num << 14;
1497 dep->frame_number += max_t(u32, 4, dep->interval);
1498
1499 /* Reinitialize test variables */
1500 dep->start_cmd_status = 0;
1501 dep->combo_num = 0;
1502
Felipe Balbi25abad62018-08-14 10:41:19 +03001503 return __dwc3_gadget_kick_transfer(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001504}
1505
Felipe Balbi25abad62018-08-14 10:41:19 +03001506static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301507{
Michael Olbrichc5a70922020-07-01 20:24:52 +02001508 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001509 struct dwc3 *dwc = dep->dwc;
Felipe Balbid5370102018-08-14 10:42:43 +03001510 int ret;
1511 int i;
Thinh Nguyend92021f2018-11-14 22:56:54 -08001512
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001513 if (list_empty(&dep->pending_list) &&
1514 list_empty(&dep->started_list)) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301515 dep->flags |= DWC3_EP_PENDING_REQUEST;
Felipe Balbi25abad62018-08-14 10:41:19 +03001516 return -EAGAIN;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301517 }
1518
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001519 if (!dwc->dis_start_transfer_quirk &&
1520 (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1521 DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
Peter Chene81a7012020-08-21 10:55:48 +08001522 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
Felipe Balbi25abad62018-08-14 10:41:19 +03001523 return dwc3_gadget_start_isoc_quirk(dep);
Thinh Nguyend92021f2018-11-14 22:56:54 -08001524 }
1525
Michael Olbrichc5a70922020-07-01 20:24:52 +02001526 if (desc->bInterval <= 14 &&
Peter Chene81a7012020-08-21 10:55:48 +08001527 dwc->gadget->speed >= USB_SPEED_HIGH) {
Michael Olbrichc5a70922020-07-01 20:24:52 +02001528 u32 frame = __dwc3_gadget_get_frame(dwc);
1529 bool rollover = frame <
1530 (dep->frame_number & DWC3_FRNUMBER_MASK);
1531
1532 /*
1533 * frame_number is set from XferNotReady and may be already
1534 * out of date. DSTS only provides the lower 14 bit of the
1535 * current frame number. So add the upper two bits of
1536 * frame_number and handle a possible rollover.
1537 * This will provide the correct frame_number unless more than
1538 * rollover has happened since XferNotReady.
1539 */
1540
1541 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1542 frame;
1543 if (rollover)
1544 dep->frame_number += BIT(14);
1545 }
1546
Felipe Balbid5370102018-08-14 10:42:43 +03001547 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1548 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1549
1550 ret = __dwc3_gadget_kick_transfer(dep);
1551 if (ret != -EAGAIN)
1552 break;
1553 }
1554
Thinh Nguyen36f05d32020-03-29 16:13:10 -07001555 /*
1556 * After a number of unsuccessful start attempts due to bus-expiry
1557 * status, issue END_TRANSFER command and retry on the next XferNotReady
1558 * event.
1559 */
1560 if (ret == -EAGAIN) {
1561 struct dwc3_gadget_ep_cmd_params params;
1562 u32 cmd;
1563
1564 cmd = DWC3_DEPCMD_ENDTRANSFER |
1565 DWC3_DEPCMD_CMDIOC |
1566 DWC3_DEPCMD_PARAM(dep->resource_index);
1567
1568 dep->resource_index = 0;
1569 memset(&params, 0, sizeof(params));
1570
1571 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1572 if (!ret)
1573 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1574 }
1575
Felipe Balbid5370102018-08-14 10:42:43 +03001576 return ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301577}
1578
Felipe Balbi72246da2011-08-19 18:10:58 +03001579static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1580{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001581 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001582
Felipe Balbibb423982015-11-16 15:31:21 -06001583 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001584 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1585 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001586 return -ESHUTDOWN;
1587 }
1588
Felipe Balbi04fb3652017-05-17 15:57:45 +03001589 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1590 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001591 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001592
Felipe Balbib2b6d602019-01-11 12:58:52 +02001593 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1594 "%s: request %pK already in flight\n",
1595 dep->name, &req->request))
1596 return -EINVAL;
1597
Felipe Balbifc8bb912016-05-16 13:14:48 +03001598 pm_runtime_get(dwc->dev);
1599
Felipe Balbi72246da2011-08-19 18:10:58 +03001600 req->request.actual = 0;
1601 req->request.status = -EINPROGRESS;
Felipe Balbi72246da2011-08-19 18:10:58 +03001602
Felipe Balbife84f522015-09-01 09:01:38 -05001603 trace_dwc3_ep_queue(req);
1604
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001605 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbia3af5e32019-01-11 12:57:09 +02001606 req->status = DWC3_REQUEST_STATUS_QUEUED;
Felipe Balbi72246da2011-08-19 18:10:58 +03001607
Thinh Nguyene0d19562020-05-05 19:46:57 -07001608 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1609 return 0;
1610
Thinh Nguyenc5036722020-09-02 18:42:58 -07001611 /*
1612 * Start the transfer only after the END_TRANSFER is completed
1613 * and endpoint STALL is cleared.
1614 */
1615 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1616 (dep->flags & DWC3_EP_WEDGE) ||
1617 (dep->flags & DWC3_EP_STALL)) {
Thinh Nguyenda10bcd2019-12-18 18:14:50 -08001618 dep->flags |= DWC3_EP_DELAY_START;
1619 return 0;
1620 }
1621
Felipe Balbid889c232016-09-29 15:44:29 +03001622 /*
1623 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1624 * wait for a XferNotReady event so we will know what's the current
1625 * (micro-)frame number.
1626 *
1627 * Without this trick, we are very, very likely gonna get Bus Expiry
1628 * errors which will force us issue EndTransfer command.
1629 */
1630 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001631 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1632 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001633 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001634
1635 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
Felipe Balbie319bd62020-08-13 08:35:38 +03001636 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Felipe Balbi25abad62018-08-14 10:41:19 +03001637 return __dwc3_gadget_start_isoc(dep);
Felipe Balbi08a36b52016-08-11 14:27:52 +03001638 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001639 }
1640
Felipe Balbi7fdca762017-09-05 14:41:34 +03001641 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001642}
1643
1644static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1645 gfp_t gfp_flags)
1646{
1647 struct dwc3_request *req = to_dwc3_request(request);
1648 struct dwc3_ep *dep = to_dwc3_ep(ep);
1649 struct dwc3 *dwc = dep->dwc;
1650
1651 unsigned long flags;
1652
1653 int ret;
1654
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001655 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001656 ret = __dwc3_gadget_ep_queue(dep, req);
1657 spin_unlock_irqrestore(&dwc->lock, flags);
1658
1659 return ret;
1660}
1661
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001662static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1663{
1664 int i;
1665
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001666 /* If req->trb is not set, then the request has not started */
1667 if (!req->trb)
1668 return;
1669
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001670 /*
1671 * If request was already started, this means we had to
1672 * stop the transfer. With that we also need to ignore
1673 * all TRBs used by the request, however TRBs can only
1674 * be modified after completion of END_TRANSFER
1675 * command. So what we do here is that we wait for
1676 * END_TRANSFER completion and only after that, we jump
1677 * over TRBs by clearing HWO and incrementing dequeue
1678 * pointer.
1679 */
1680 for (i = 0; i < req->num_trbs; i++) {
1681 struct dwc3_trb *trb;
1682
Thinh Nguyen2dedea02020-03-05 13:24:01 -08001683 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001684 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1685 dwc3_ep_inc_deq(dep);
1686 }
Thinh Nguyenc7152762019-02-12 19:39:27 -08001687
1688 req->num_trbs = 0;
Felipe Balbi7746a8d2018-08-01 13:42:29 +03001689}
1690
Felipe Balbid4f1afe2018-08-01 13:54:25 +03001691static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1692{
1693 struct dwc3_request *req;
1694 struct dwc3_request *tmp;
1695
1696 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1697 dwc3_gadget_ep_skip_trbs(dep, req);
1698 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1699 }
1700}
1701
Felipe Balbi72246da2011-08-19 18:10:58 +03001702static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1703 struct usb_request *request)
1704{
1705 struct dwc3_request *req = to_dwc3_request(request);
1706 struct dwc3_request *r = NULL;
1707
1708 struct dwc3_ep *dep = to_dwc3_ep(ep);
1709 struct dwc3 *dwc = dep->dwc;
1710
1711 unsigned long flags;
1712 int ret = 0;
1713
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001714 trace_dwc3_ep_dequeue(req);
1715
Felipe Balbi72246da2011-08-19 18:10:58 +03001716 spin_lock_irqsave(&dwc->lock, flags);
1717
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001718 list_for_each_entry(r, &dep->cancelled_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001719 if (r == req)
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001720 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 }
1722
Felipe Balbi72246da2011-08-19 18:10:58 +03001723 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001724 if (r == req) {
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001725 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1726 goto out;
1727 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001728 }
1729
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001730 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001731 if (r == req) {
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001732 struct dwc3_request *t;
1733
Felipe Balbi72246da2011-08-19 18:10:58 +03001734 /* wait until it is processed */
Felipe Balbic5353b22019-02-13 13:00:54 +02001735 dwc3_stop_active_transfer(dep, true, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001736
Thinh Nguyena7027ca2020-03-05 13:24:08 -08001737 /*
1738 * Remove any started request if the transfer is
1739 * cancelled.
1740 */
1741 list_for_each_entry_safe(r, t, &dep->started_list, list)
1742 dwc3_gadget_move_cancelled_request(r);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001743
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001744 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +03001745 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 }
1747
Thinh Nguyenfcd2def2020-03-05 13:24:20 -08001748 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1749 request, ep->name);
1750 ret = -EINVAL;
1751out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 spin_unlock_irqrestore(&dwc->lock, flags);
1753
1754 return ret;
1755}
1756
Felipe Balbi7a608552014-09-24 14:19:52 -05001757int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001758{
1759 struct dwc3_gadget_ep_cmd_params params;
1760 struct dwc3 *dwc = dep->dwc;
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001761 struct dwc3_request *req;
1762 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 int ret;
1764
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001765 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1766 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1767 return -EINVAL;
1768 }
1769
Felipe Balbi72246da2011-08-19 18:10:58 +03001770 memset(&params, 0x00, sizeof(params));
1771
1772 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001773 struct dwc3_trb *trb;
1774
Felipe Balbie319bd62020-08-13 08:35:38 +03001775 unsigned int transfer_in_flight;
1776 unsigned int started;
Felipe Balbi69450c42016-05-30 13:37:02 +03001777
1778 if (dep->number > 1)
1779 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1780 else
1781 trb = &dwc->ep0_trb[dep->trb_enqueue];
1782
1783 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1784 started = !list_empty(&dep->started_list);
1785
1786 if (!protocol && ((dep->direction && transfer_in_flight) ||
1787 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001788 return -EAGAIN;
1789 }
1790
Felipe Balbi2cd47182016-04-12 16:42:43 +03001791 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1792 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001793 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001794 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 dep->name);
1796 else
1797 dep->flags |= DWC3_EP_STALL;
1798 } else {
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001799 /*
1800 * Don't issue CLEAR_STALL command to control endpoints. The
1801 * controller automatically clears the STALL when it receives
1802 * the SETUP token.
1803 */
1804 if (dep->number <= 1) {
1805 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1806 return 0;
1807 }
Felipe Balbi2cd47182016-04-12 16:42:43 +03001808
Thinh Nguyend97c78a2020-09-02 18:43:04 -07001809 dwc3_stop_active_transfer(dep, true, true);
1810
1811 list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1812 dwc3_gadget_move_cancelled_request(req);
1813
1814 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) {
1815 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
1816 return 0;
1817 }
1818
1819 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1820
John Youn50c763f2016-05-31 17:49:56 -07001821 ret = dwc3_send_clear_stall_ep_cmd(dep);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001822 if (ret) {
Dan Carpenter3f892042014-03-07 14:20:22 +03001823 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001824 dep->name);
Thinh Nguyencb11ea52020-03-05 13:23:55 -08001825 return ret;
1826 }
1827
1828 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1829
Thinh Nguyenc5036722020-09-02 18:42:58 -07001830 if ((dep->flags & DWC3_EP_DELAY_START) &&
1831 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
1832 __dwc3_gadget_kick_transfer(dep);
1833
1834 dep->flags &= ~DWC3_EP_DELAY_START;
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001836
Felipe Balbi72246da2011-08-19 18:10:58 +03001837 return ret;
1838}
1839
1840static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1841{
1842 struct dwc3_ep *dep = to_dwc3_ep(ep);
1843 struct dwc3 *dwc = dep->dwc;
1844
1845 unsigned long flags;
1846
1847 int ret;
1848
1849 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001850 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001851 spin_unlock_irqrestore(&dwc->lock, flags);
1852
1853 return ret;
1854}
1855
1856static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1857{
1858 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001859 struct dwc3 *dwc = dep->dwc;
1860 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001861 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001862
Paul Zimmerman249a4562012-02-24 17:32:16 -08001863 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001864 dep->flags |= DWC3_EP_WEDGE;
1865
Pratyush Anand08f0d962012-06-25 22:40:43 +05301866 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001867 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301868 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001869 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001870 spin_unlock_irqrestore(&dwc->lock, flags);
1871
1872 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001873}
1874
1875/* -------------------------------------------------------------------------- */
1876
1877static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1878 .bLength = USB_DT_ENDPOINT_SIZE,
1879 .bDescriptorType = USB_DT_ENDPOINT,
1880 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1881};
1882
1883static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1884 .enable = dwc3_gadget_ep0_enable,
1885 .disable = dwc3_gadget_ep0_disable,
1886 .alloc_request = dwc3_gadget_ep_alloc_request,
1887 .free_request = dwc3_gadget_ep_free_request,
1888 .queue = dwc3_gadget_ep0_queue,
1889 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301890 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001891 .set_wedge = dwc3_gadget_ep_set_wedge,
1892};
1893
1894static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1895 .enable = dwc3_gadget_ep_enable,
1896 .disable = dwc3_gadget_ep_disable,
1897 .alloc_request = dwc3_gadget_ep_alloc_request,
1898 .free_request = dwc3_gadget_ep_free_request,
1899 .queue = dwc3_gadget_ep_queue,
1900 .dequeue = dwc3_gadget_ep_dequeue,
1901 .set_halt = dwc3_gadget_ep_set_halt,
1902 .set_wedge = dwc3_gadget_ep_set_wedge,
1903};
1904
1905/* -------------------------------------------------------------------------- */
1906
1907static int dwc3_gadget_get_frame(struct usb_gadget *g)
1908{
1909 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001910
Felipe Balbi6cb2e4e2016-10-21 13:07:09 +03001911 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001912}
1913
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001914static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001915{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001916 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001917
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001918 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001919 u32 reg;
1920
Felipe Balbi72246da2011-08-19 18:10:58 +03001921 u8 link_state;
Felipe Balbi72246da2011-08-19 18:10:58 +03001922
Felipe Balbi72246da2011-08-19 18:10:58 +03001923 /*
1924 * According to the Databook Remote wakeup request should
1925 * be issued only when the device is in early suspend state.
1926 *
1927 * We can check that via USB Link State bits in DSTS register.
1928 */
1929 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1930
Felipe Balbi72246da2011-08-19 18:10:58 +03001931 link_state = DWC3_DSTS_USBLNKST(reg);
1932
1933 switch (link_state) {
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001934 case DWC3_LINK_STATE_RESET:
Felipe Balbi72246da2011-08-19 18:10:58 +03001935 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1936 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
Thinh Nguyend0550cd2020-01-31 16:25:50 -08001937 case DWC3_LINK_STATE_RESUME:
Felipe Balbi72246da2011-08-19 18:10:58 +03001938 break;
1939 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001940 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001941 }
1942
Felipe Balbi8598bde2012-01-02 18:55:57 +02001943 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1944 if (ret < 0) {
1945 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001946 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001947 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001948
Paul Zimmerman802fde92012-04-27 13:10:52 +03001949 /* Recent versions do this automatically */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07001950 if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001951 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001952 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001953 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1954 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1955 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001956
Paul Zimmerman1d046792012-02-15 18:56:56 -08001957 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001958 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001959
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001960 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001961 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1962
1963 /* in HS, means ON */
1964 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1965 break;
1966 }
1967
1968 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1969 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001970 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001971 }
1972
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001973 return 0;
1974}
1975
1976static int dwc3_gadget_wakeup(struct usb_gadget *g)
1977{
1978 struct dwc3 *dwc = gadget_to_dwc(g);
1979 unsigned long flags;
1980 int ret;
1981
1982 spin_lock_irqsave(&dwc->lock, flags);
1983 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001984 spin_unlock_irqrestore(&dwc->lock, flags);
1985
1986 return ret;
1987}
1988
1989static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1990 int is_selfpowered)
1991{
1992 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001993 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001994
Paul Zimmerman249a4562012-02-24 17:32:16 -08001995 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001996 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001997 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
1999 return 0;
2000}
2001
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002002static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002003{
2004 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02002005 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03002006
Felipe Balbifc8bb912016-05-16 13:14:48 +03002007 if (pm_runtime_suspended(dwc->dev))
2008 return 0;
2009
Felipe Balbi72246da2011-08-19 18:10:58 +03002010 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002011 if (is_on) {
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002012 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03002013 reg &= ~DWC3_DCTL_TRGTULST_MASK;
2014 reg |= DWC3_DCTL_TRGTULST_RX_DET;
2015 }
2016
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002017 if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
Paul Zimmerman802fde92012-04-27 13:10:52 +03002018 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2019 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002020
2021 if (dwc->has_hibernation)
2022 reg |= DWC3_DCTL_KEEP_CONNECT;
2023
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002024 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002025 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03002026 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002027
2028 if (dwc->has_hibernation && !suspend)
2029 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2030
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02002031 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02002032 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002033
Thinh Nguyen5b738212019-10-23 19:15:43 -07002034 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002035
2036 do {
2037 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03002038 reg &= DWC3_DSTS_DEVCTRLHLT;
2039 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03002040
2041 if (!timeout)
2042 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03002043
Pratyush Anand6f17f742012-07-02 10:21:55 +05302044 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002045}
2046
2047static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2048{
2049 struct dwc3 *dwc = gadget_to_dwc(g);
2050 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05302051 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002052
2053 is_on = !!is_on;
2054
Baolin Wangbb014732016-10-14 17:11:33 +08002055 /*
2056 * Per databook, when we want to stop the gadget, if a control transfer
2057 * is still in process, complete it and get the core into setup phase.
2058 */
2059 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
2060 reinit_completion(&dwc->ep0_in_setup);
2061
2062 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2063 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2064 if (ret == 0) {
2065 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
2066 return -ETIMEDOUT;
2067 }
2068 }
2069
Felipe Balbi72246da2011-08-19 18:10:58 +03002070 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002071 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002072 spin_unlock_irqrestore(&dwc->lock, flags);
2073
Pratyush Anand6f17f742012-07-02 10:21:55 +05302074 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002075}
2076
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002077static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2078{
2079 u32 reg;
2080
2081 /* Enable all but Start and End of Frame IRQs */
2082 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2083 DWC3_DEVTEN_EVNTOVERFLOWEN |
2084 DWC3_DEVTEN_CMDCMPLTEN |
2085 DWC3_DEVTEN_ERRTICERREN |
2086 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002087 DWC3_DEVTEN_CONNECTDONEEN |
2088 DWC3_DEVTEN_USBRSTEN |
2089 DWC3_DEVTEN_DISCONNEVTEN);
2090
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002091 if (DWC3_VER_IS_PRIOR(DWC3, 250A))
Felipe Balbi799e9dc2016-09-23 11:20:40 +03002092 reg |= DWC3_DEVTEN_ULSTCNGEN;
2093
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002094 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2095}
2096
2097static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2098{
2099 /* mask all interrupts */
2100 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2101}
2102
2103static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03002104static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002105
Felipe Balbi4e994722016-05-13 14:09:59 +03002106/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03002107 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2108 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03002109 *
2110 * The following looks like complex but it's actually very simple. In order to
2111 * calculate the number of packets we can burst at once on OUT transfers, we're
2112 * gonna use RxFIFO size.
2113 *
2114 * To calculate RxFIFO size we need two numbers:
2115 * MDWIDTH = size, in bits, of the internal memory bus
2116 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2117 *
2118 * Given these two numbers, the formula is simple:
2119 *
2120 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2121 *
2122 * 24 bytes is for 3x SETUP packets
2123 * 16 bytes is a clock domain crossing tolerance
2124 *
2125 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2126 */
2127static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2128{
2129 u32 ram2_depth;
2130 u32 mdwidth;
2131 u32 nump;
2132 u32 reg;
2133
2134 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2135 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002136 if (DWC3_IP_IS(DWC32))
2137 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Felipe Balbi4e994722016-05-13 14:09:59 +03002138
2139 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2140 nump = min_t(u32, nump, 16);
2141
2142 /* update NumP */
2143 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2144 reg &= ~DWC3_DCFG_NUMP_MASK;
2145 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2146 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2147}
2148
Felipe Balbid7be2952016-05-04 15:49:37 +03002149static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002150{
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002152 int ret = 0;
2153 u32 reg;
2154
John Youncf40b862016-11-14 12:32:43 -08002155 /*
2156 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2157 * the core supports IMOD, disable it.
2158 */
2159 if (dwc->imod_interval) {
2160 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2161 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2162 } else if (dwc3_has_imod(dwc)) {
2163 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2164 }
2165
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002166 /*
2167 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2168 * field instead of letting dwc3 itself calculate that automatically.
2169 *
2170 * This way, we maximize the chances that we'll be able to get several
2171 * bursts of data without going through any sort of endpoint throttling.
2172 */
2173 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002174 if (DWC3_IP_IS(DWC3))
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002175 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002176 else
2177 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07002178
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002179 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2180
Felipe Balbi4e994722016-05-13 14:09:59 +03002181 dwc3_gadget_setup_nump(dwc);
2182
Felipe Balbi72246da2011-08-19 18:10:58 +03002183 /* Start with SuperSpeed Default */
2184 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2185
2186 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002187 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002188 if (ret) {
2189 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002190 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002191 }
2192
2193 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002194 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 if (ret) {
2196 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03002197 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002198 }
2199
2200 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002201 dwc->ep0state = EP0_SETUP_PHASE;
Zeng Tao88b1bb12018-12-26 19:22:00 +08002202 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002203 dwc3_ep0_out_start(dwc);
2204
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002205 dwc3_gadget_enable_irq(dwc);
2206
Felipe Balbid7be2952016-05-04 15:49:37 +03002207 return 0;
2208
2209err1:
2210 __dwc3_gadget_ep_disable(dwc->eps[0]);
2211
2212err0:
2213 return ret;
2214}
2215
2216static int dwc3_gadget_start(struct usb_gadget *g,
2217 struct usb_gadget_driver *driver)
2218{
2219 struct dwc3 *dwc = gadget_to_dwc(g);
2220 unsigned long flags;
2221 int ret = 0;
2222 int irq;
2223
Roger Quadros9522def2016-06-10 14:48:38 +03002224 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03002225 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2226 IRQF_SHARED, "dwc3", dwc->ev_buf);
2227 if (ret) {
2228 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2229 irq, ret);
2230 goto err0;
2231 }
2232
2233 spin_lock_irqsave(&dwc->lock, flags);
2234 if (dwc->gadget_driver) {
2235 dev_err(dwc->dev, "%s is already bound to %s\n",
Peter Chene81a7012020-08-21 10:55:48 +08002236 dwc->gadget->name,
Felipe Balbid7be2952016-05-04 15:49:37 +03002237 dwc->gadget_driver->driver.name);
2238 ret = -EBUSY;
2239 goto err1;
2240 }
2241
2242 dwc->gadget_driver = driver;
2243
Felipe Balbifc8bb912016-05-16 13:14:48 +03002244 if (pm_runtime_active(dwc->dev))
2245 __dwc3_gadget_start(dwc);
2246
Felipe Balbi72246da2011-08-19 18:10:58 +03002247 spin_unlock_irqrestore(&dwc->lock, flags);
2248
2249 return 0;
2250
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002251err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002252 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002253 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002254
2255err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03002256 return ret;
2257}
2258
Felipe Balbid7be2952016-05-04 15:49:37 +03002259static void __dwc3_gadget_stop(struct dwc3 *dwc)
2260{
2261 dwc3_gadget_disable_irq(dwc);
2262 __dwc3_gadget_ep_disable(dwc->eps[0]);
2263 __dwc3_gadget_ep_disable(dwc->eps[1]);
2264}
2265
Felipe Balbi22835b82014-10-17 12:05:12 -05002266static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002267{
2268 struct dwc3 *dwc = gadget_to_dwc(g);
2269 unsigned long flags;
2270
2271 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08002272
2273 if (pm_runtime_suspended(dwc->dev))
2274 goto out;
2275
Felipe Balbid7be2952016-05-04 15:49:37 +03002276 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08002277
Baolin Wang76a638f2016-10-31 19:38:36 +08002278out:
Felipe Balbi72246da2011-08-19 18:10:58 +03002279 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03002280 spin_unlock_irqrestore(&dwc->lock, flags);
2281
Felipe Balbi3f308d12016-05-16 14:17:06 +03002282 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002283
Felipe Balbi72246da2011-08-19 18:10:58 +03002284 return 0;
2285}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002286
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302287static void dwc3_gadget_config_params(struct usb_gadget *g,
2288 struct usb_dcd_config_params *params)
2289{
2290 struct dwc3 *dwc = gadget_to_dwc(g);
2291
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002292 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
2293 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
2294
2295 /* Recommended BESL */
2296 if (!dwc->dis_enblslpm_quirk) {
Thinh Nguyen17b63702019-08-29 18:00:16 -07002297 /*
2298 * If the recommended BESL baseline is 0 or if the BESL deep is
2299 * less than 2, Microsoft's Windows 10 host usb stack will issue
2300 * a usb reset immediately after it receives the extended BOS
2301 * descriptor and the enumeration will fail. To maintain
2302 * compatibility with the Windows' usb stack, let's set the
2303 * recommended BESL baseline to 1 and clamp the BESL deep to be
2304 * within 2 to 15.
2305 */
2306 params->besl_baseline = 1;
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002307 if (dwc->is_utmi_l1_suspend)
Thinh Nguyen17b63702019-08-29 18:00:16 -07002308 params->besl_deep =
2309 clamp_t(u8, dwc->hird_threshold, 2, 15);
Thinh Nguyen54fb5ba2019-08-19 18:36:06 -07002310 }
2311
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302312 /* U1 Device exit Latency */
2313 if (dwc->dis_u1_entry_quirk)
2314 params->bU1devExitLat = 0;
2315 else
2316 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
2317
2318 /* U2 Device exit Latency */
2319 if (dwc->dis_u2_entry_quirk)
2320 params->bU2DevExitLat = 0;
2321 else
2322 params->bU2DevExitLat =
2323 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
2324}
2325
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002326static void dwc3_gadget_set_speed(struct usb_gadget *g,
2327 enum usb_device_speed speed)
2328{
2329 struct dwc3 *dwc = gadget_to_dwc(g);
2330 unsigned long flags;
2331 u32 reg;
2332
2333 spin_lock_irqsave(&dwc->lock, flags);
2334 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 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002350 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02002351 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002352 reg |= DWC3_DCFG_SUPERSPEED;
2353 } else {
2354 switch (speed) {
2355 case USB_SPEED_LOW:
2356 reg |= DWC3_DCFG_LOWSPEED;
2357 break;
2358 case USB_SPEED_FULL:
2359 reg |= DWC3_DCFG_FULLSPEED;
2360 break;
2361 case USB_SPEED_HIGH:
2362 reg |= DWC3_DCFG_HIGHSPEED;
2363 break;
2364 case USB_SPEED_SUPER:
2365 reg |= DWC3_DCFG_SUPERSPEED;
2366 break;
2367 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002368 if (DWC3_IP_IS(DWC3))
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002369 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002370 else
2371 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002372 break;
2373 default:
2374 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2375
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002376 if (DWC3_IP_IS(DWC3))
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002377 reg |= DWC3_DCFG_SUPERSPEED;
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002378 else
2379 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002380 }
2381 }
2382 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2383
2384 spin_unlock_irqrestore(&dwc->lock, flags);
2385}
2386
Felipe Balbi72246da2011-08-19 18:10:58 +03002387static const struct usb_gadget_ops dwc3_gadget_ops = {
2388 .get_frame = dwc3_gadget_get_frame,
2389 .wakeup = dwc3_gadget_wakeup,
2390 .set_selfpowered = dwc3_gadget_set_selfpowered,
2391 .pullup = dwc3_gadget_pullup,
2392 .udc_start = dwc3_gadget_start,
2393 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002394 .udc_set_speed = dwc3_gadget_set_speed,
Anurag Kumar Vulisha729dcff2019-05-10 12:37:28 +05302395 .get_config_params = dwc3_gadget_config_params,
Felipe Balbi72246da2011-08-19 18:10:58 +03002396};
2397
2398/* -------------------------------------------------------------------------- */
2399
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002400static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2401{
2402 struct dwc3 *dwc = dep->dwc;
2403
2404 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2405 dep->endpoint.maxburst = 1;
2406 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2407 if (!dep->direction)
Peter Chene81a7012020-08-21 10:55:48 +08002408 dwc->gadget->ep0 = &dep->endpoint;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002409
2410 dep->endpoint.caps.type_control = true;
2411
2412 return 0;
2413}
2414
2415static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2416{
2417 struct dwc3 *dwc = dep->dwc;
2418 int mdwidth;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002419 int size;
2420
2421 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002422 if (DWC3_IP_IS(DWC32))
2423 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
2424
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002425 /* MDWIDTH is represented in bits, we need it in bytes */
2426 mdwidth /= 8;
2427
2428 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002429 if (DWC3_IP_IS(DWC3))
Thinh Nguyen586f4332020-01-31 16:59:21 -08002430 size = DWC3_GTXFIFOSIZ_TXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002431 else
2432 size = DWC31_GTXFIFOSIZ_TXFDEP(size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002433
2434 /* FIFO Depth is in MDWDITH bytes. Multiply */
2435 size *= mdwidth;
2436
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002437 /*
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002438 * To meet performance requirement, a minimum TxFIFO size of 3x
2439 * MaxPacketSize is recommended for endpoints that support burst and a
2440 * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't
2441 * support burst. Use those numbers and we can calculate the max packet
2442 * limit as below.
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002443 */
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002444 if (dwc->maximum_speed >= USB_SPEED_SUPER)
2445 size /= 3;
2446 else
2447 size /= 2;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002448
2449 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2450
2451 dep->endpoint.max_streams = 15;
2452 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2453 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002454 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002455 dep->endpoint.caps.type_iso = true;
2456 dep->endpoint.caps.type_bulk = true;
2457 dep->endpoint.caps.type_int = true;
2458
2459 return dwc3_alloc_trb_pool(dep);
2460}
2461
2462static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2463{
2464 struct dwc3 *dwc = dep->dwc;
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002465 int mdwidth;
2466 int size;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002467
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002468 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
Thinh Nguyen4244ba02020-04-11 19:20:07 -07002469 if (DWC3_IP_IS(DWC32))
2470 mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002471
2472 /* MDWIDTH is represented in bits, convert to bytes */
2473 mdwidth /= 8;
2474
2475 /* All OUT endpoints share a single RxFIFO space */
2476 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002477 if (DWC3_IP_IS(DWC3))
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002478 size = DWC3_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002479 else
2480 size = DWC31_GRXFIFOSIZ_RXFDEP(size);
Thinh Nguyend94ea5312020-01-31 16:59:27 -08002481
2482 /* FIFO depth is in MDWDITH bytes */
2483 size *= mdwidth;
2484
2485 /*
2486 * To meet performance requirement, a minimum recommended RxFIFO size
2487 * is defined as follow:
2488 * RxFIFO size >= (3 x MaxPacketSize) +
2489 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
2490 *
2491 * Then calculate the max packet limit as below.
2492 */
2493 size -= (3 * 8) + 16;
2494 if (size < 0)
2495 size = 0;
2496 else
2497 size /= 3;
2498
2499 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002500 dep->endpoint.max_streams = 15;
2501 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2502 list_add_tail(&dep->endpoint.ep_list,
Peter Chene81a7012020-08-21 10:55:48 +08002503 &dwc->gadget->ep_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002504 dep->endpoint.caps.type_iso = true;
2505 dep->endpoint.caps.type_bulk = true;
2506 dep->endpoint.caps.type_int = true;
2507
2508 return dwc3_alloc_trb_pool(dep);
2509}
2510
2511static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002512{
2513 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002514 bool direction = epnum & 1;
2515 int ret;
2516 u8 num = epnum >> 1;
2517
2518 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2519 if (!dep)
2520 return -ENOMEM;
2521
2522 dep->dwc = dwc;
2523 dep->number = epnum;
2524 dep->direction = direction;
2525 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2526 dwc->eps[epnum] = dep;
Thinh Nguyend92021f2018-11-14 22:56:54 -08002527 dep->combo_num = 0;
2528 dep->start_cmd_status = 0;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002529
2530 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2531 direction ? "in" : "out");
2532
2533 dep->endpoint.name = dep->name;
2534
2535 if (!(dep->number > 1)) {
2536 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2537 dep->endpoint.comp_desc = NULL;
2538 }
2539
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002540 if (num == 0)
2541 ret = dwc3_gadget_init_control_endpoint(dep);
2542 else if (direction)
2543 ret = dwc3_gadget_init_in_endpoint(dep);
2544 else
2545 ret = dwc3_gadget_init_out_endpoint(dep);
2546
2547 if (ret)
2548 return ret;
2549
2550 dep->endpoint.caps.dir_in = direction;
2551 dep->endpoint.caps.dir_out = !direction;
2552
2553 INIT_LIST_HEAD(&dep->pending_list);
2554 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbid5443bb2018-08-01 13:53:29 +03002555 INIT_LIST_HEAD(&dep->cancelled_list);
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002556
2557 return 0;
2558}
2559
2560static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2561{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002562 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002563
Peter Chene81a7012020-08-21 10:55:48 +08002564 INIT_LIST_HEAD(&dwc->gadget->ep_list);
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002565
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002566 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002567 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002568
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002569 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2570 if (ret)
2571 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 }
2573
2574 return 0;
2575}
2576
2577static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2578{
2579 struct dwc3_ep *dep;
2580 u8 epnum;
2581
2582 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2583 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002584 if (!dep)
2585 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302586 /*
2587 * Physical endpoints 0 and 1 are special; they form the
2588 * bi-directional USB endpoint 0.
2589 *
2590 * For those two physical endpoints, we don't allocate a TRB
2591 * pool nor do we add them the endpoints list. Due to that, we
2592 * shouldn't do these two operations otherwise we would end up
2593 * with all sorts of bugs when removing dwc3.ko.
2594 */
2595 if (epnum != 0 && epnum != 1) {
2596 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002597 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302598 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002599
2600 kfree(dep);
2601 }
2602}
2603
Felipe Balbi72246da2011-08-19 18:10:58 +03002604/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002605
Felipe Balbi8f608e82018-03-27 10:53:29 +03002606static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2607 struct dwc3_request *req, struct dwc3_trb *trb,
2608 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302609{
2610 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302611
Felipe Balbidc55c672016-08-12 13:20:32 +03002612 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002613
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002614 trace_dwc3_complete_trb(dep, trb);
Felipe Balbi09fe1f82018-08-01 13:32:07 +03002615 req->num_trbs--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002616
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002617 /*
2618 * If we're in the middle of series of chained TRBs and we
2619 * receive a short transfer along the way, DWC3 will skip
2620 * through all TRBs including the last TRB in the chain (the
2621 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2622 * bit and SW has to do it manually.
2623 *
2624 * We're going to do that here to avoid problems of HW trying
2625 * to use bogus TRBs for transfers.
2626 */
2627 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2628 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2629
Felipe Balbic6267a52017-01-05 14:58:46 +02002630 /*
Thinh Nguyen6abfa0f2018-11-15 19:03:27 -08002631 * For isochronous transfers, the first TRB in a service interval must
2632 * have the Isoc-First type. Track and report its interval frame number.
2633 */
2634 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2635 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2636 unsigned int frame_number;
2637
2638 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2639 frame_number &= ~(dep->interval - 1);
2640 req->request.frame_number = frame_number;
2641 }
2642
2643 /*
Thinh Nguyena2841f42020-09-24 01:21:36 -07002644 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
2645 * this TRB points to the bounce buffer address, it's a MPS alignment
2646 * TRB. Don't add it to req->remaining calculation.
Felipe Balbic6267a52017-01-05 14:58:46 +02002647 */
Thinh Nguyena2841f42020-09-24 01:21:36 -07002648 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
2649 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002650 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2651 return 1;
2652 }
2653
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302654 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002655 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302656
Felipe Balbi35b27192017-03-08 13:56:37 +02002657 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2658 return 1;
2659
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002660 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302661 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002662
Anurag Kumar Vulisha5ee85892020-01-27 19:30:46 +00002663 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
2664 (trb->ctrl & DWC3_TRB_CTRL_LST))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302665 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002666
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302667 return 0;
2668}
2669
Felipe Balbid3692952018-03-29 13:32:10 +03002670static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2671 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2672 int status)
2673{
2674 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2675 struct scatterlist *sg = req->sg;
2676 struct scatterlist *s;
2677 unsigned int pending = req->num_pending_sgs;
2678 unsigned int i;
2679 int ret = 0;
2680
2681 for_each_sg(sg, s, pending, i) {
2682 trb = &dep->trb_pool[dep->trb_dequeue];
2683
Felipe Balbid3692952018-03-29 13:32:10 +03002684 req->sg = sg_next(s);
2685 req->num_pending_sgs--;
2686
2687 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2688 trb, event, status, true);
2689 if (ret)
2690 break;
2691 }
2692
2693 return ret;
2694}
2695
2696static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2697 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2698 int status)
2699{
2700 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2701
2702 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2703 event, status, false);
2704}
2705
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002706static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2707{
Thinh Nguyen49e05902020-03-31 01:40:35 -07002708 return req->num_pending_sgs == 0;
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002709}
2710
Felipe Balbif38e35d2018-04-06 15:56:35 +03002711static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2712 const struct dwc3_event_depevt *event,
2713 struct dwc3_request *req, int status)
2714{
2715 int ret;
2716
2717 if (req->num_pending_sgs)
2718 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2719 status);
2720 else
2721 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2722 status);
2723
Thinh Nguyen690e5c22020-09-24 01:21:24 -07002724 req->request.actual = req->request.length - req->remaining;
2725
2726 if (!dwc3_gadget_ep_request_completed(req))
2727 goto out;
2728
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002729 if (req->needs_extra_trb) {
Felipe Balbif38e35d2018-04-06 15:56:35 +03002730 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2731 status);
Felipe Balbi1a22ec62018-08-01 13:15:05 +03002732 req->needs_extra_trb = false;
Felipe Balbif38e35d2018-04-06 15:56:35 +03002733 }
2734
Felipe Balbif38e35d2018-04-06 15:56:35 +03002735 dwc3_gadget_giveback(dep, req, status);
2736
2737out:
2738 return ret;
2739}
2740
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002741static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002742 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002743{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002744 struct dwc3_request *req;
2745 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002746
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002747 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002748 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002749
Felipe Balbif38e35d2018-04-06 15:56:35 +03002750 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2751 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002752 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002753 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002754 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002755}
2756
Thinh Nguyend9feef92020-03-31 01:40:42 -07002757static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
2758{
2759 struct dwc3_request *req;
2760
2761 if (!list_empty(&dep->pending_list))
2762 return true;
2763
2764 /*
2765 * We only need to check the first entry of the started list. We can
2766 * assume the completed requests are removed from the started list.
2767 */
2768 req = next_request(&dep->started_list);
2769 if (!req)
2770 return false;
2771
2772 return !dwc3_gadget_ep_request_completed(req);
2773}
2774
Felipe Balbiee3638b2018-03-27 11:26:53 +03002775static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2776 const struct dwc3_event_depevt *event)
2777{
Felipe Balbif62afb42018-04-11 10:34:34 +03002778 dep->frame_number = event->parameters;
Felipe Balbiee3638b2018-03-27 11:26:53 +03002779}
2780
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002781static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
2782 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002783{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002784 struct dwc3 *dwc = dep->dwc;
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002785 bool no_started_trb = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002786
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002787 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002788
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002789 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2790 goto out;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002791
Michael Grzeschikf5e46aa2020-07-01 20:24:53 +02002792 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2793 list_empty(&dep->started_list) &&
2794 (list_empty(&dep->pending_list) || status == -EXDEV))
Felipe Balbifae2b902011-10-14 13:00:30 +03002795 dwc3_stop_active_transfer(dep, true, true);
Thinh Nguyend9feef92020-03-31 01:40:42 -07002796 else if (dwc3_gadget_ep_should_continue(dep))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002797 if (__dwc3_gadget_kick_transfer(dep) == 0)
2798 no_started_trb = false;
Felipe Balbifae2b902011-10-14 13:00:30 +03002799
Thinh Nguyenb6842d42020-05-05 19:46:33 -07002800out:
Felipe Balbifae2b902011-10-14 13:00:30 +03002801 /*
2802 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2803 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2804 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07002805 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03002806 u32 reg;
2807 int i;
2808
2809 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002810 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002811
2812 if (!(dep->flags & DWC3_EP_ENABLED))
2813 continue;
2814
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002815 if (!list_empty(&dep->started_list))
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002816 return no_started_trb;
Felipe Balbifae2b902011-10-14 13:00:30 +03002817 }
2818
2819 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2820 reg |= dwc->u1u2;
2821 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2822
2823 dwc->u1u2 = 0;
2824 }
Thinh Nguyen2e6e9e42020-05-05 19:46:39 -07002825
2826 return no_started_trb;
2827}
2828
2829static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2830 const struct dwc3_event_depevt *event)
2831{
2832 int status = 0;
2833
2834 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2835 dwc3_gadget_endpoint_frame_from_event(dep, event);
2836
2837 if (event->status & DEPEVT_STATUS_BUSERR)
2838 status = -ECONNRESET;
2839
2840 if (event->status & DEPEVT_STATUS_MISSED_ISOC)
2841 status = -EXDEV;
2842
2843 dwc3_gadget_endpoint_trbs_complete(dep, event, status);
Felipe Balbi72246da2011-08-19 18:10:58 +03002844}
2845
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07002846static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
2847 const struct dwc3_event_depevt *event)
2848{
2849 int status = 0;
2850
2851 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2852
2853 if (event->status & DEPEVT_STATUS_BUSERR)
2854 status = -ECONNRESET;
2855
Thinh Nguyene0d19562020-05-05 19:46:57 -07002856 if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
2857 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002858}
2859
Felipe Balbi8f608e82018-03-27 10:53:29 +03002860static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2861 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002862{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002863 dwc3_gadget_endpoint_frame_from_event(dep, event);
Thinh Nguyen36f05d32020-03-29 16:13:10 -07002864
2865 /*
2866 * The XferNotReady event is generated only once before the endpoint
2867 * starts. It will be generated again when END_TRANSFER command is
2868 * issued. For some controller versions, the XferNotReady event may be
2869 * generated while the END_TRANSFER command is still in process. Ignore
2870 * it and wait for the next XferNotReady event after the command is
2871 * completed.
2872 */
2873 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2874 return;
2875
Felipe Balbi25abad62018-08-14 10:41:19 +03002876 (void) __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002877}
2878
Thinh Nguyen8266b082020-07-30 16:29:03 -07002879static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
2880 const struct dwc3_event_depevt *event)
2881{
2882 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2883
2884 if (cmd != DWC3_DEPCMD_ENDTRANSFER)
2885 return;
2886
2887 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2888 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2889 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2890
2891 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
2892 struct dwc3 *dwc = dep->dwc;
2893
2894 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
2895 if (dwc3_send_clear_stall_ep_cmd(dep)) {
2896 struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
2897
2898 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
2899 if (dwc->delayed_status)
2900 __dwc3_gadget_ep0_set_halt(ep0, 1);
2901 return;
2902 }
2903
2904 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2905 if (dwc->delayed_status)
2906 dwc3_ep0_send_delayed_status(dwc);
2907 }
2908
2909 if ((dep->flags & DWC3_EP_DELAY_START) &&
2910 !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2911 __dwc3_gadget_kick_transfer(dep);
2912
2913 dep->flags &= ~DWC3_EP_DELAY_START;
2914}
2915
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07002916static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
2917 const struct dwc3_event_depevt *event)
2918{
2919 struct dwc3 *dwc = dep->dwc;
2920
2921 if (event->status == DEPEVT_STREAMEVT_FOUND) {
2922 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
2923 goto out;
2924 }
2925
2926 /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
2927 switch (event->parameters) {
2928 case DEPEVT_STREAM_PRIME:
2929 /*
2930 * If the host can properly transition the endpoint state from
2931 * idle to prime after a NoStream rejection, there's no need to
2932 * force restarting the endpoint to reinitiate the stream. To
2933 * simplify the check, assume the host follows the USB spec if
2934 * it primed the endpoint more than once.
2935 */
2936 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
2937 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
2938 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
2939 else
2940 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
2941 }
2942
2943 break;
2944 case DEPEVT_STREAM_NOSTREAM:
2945 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
2946 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
2947 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
2948 break;
2949
2950 /*
2951 * If the host rejects a stream due to no active stream, by the
2952 * USB and xHCI spec, the endpoint will be put back to idle
2953 * state. When the host is ready (buffer added/updated), it will
2954 * prime the endpoint to inform the usb device controller. This
2955 * triggers the device controller to issue ERDY to restart the
2956 * stream. However, some hosts don't follow this and keep the
2957 * endpoint in the idle state. No prime will come despite host
2958 * streams are updated, and the device controller will not be
2959 * triggered to generate ERDY to move the next stream data. To
2960 * workaround this and maintain compatibility with various
2961 * hosts, force to reinitate the stream until the host is ready
2962 * instead of waiting for the host to prime the endpoint.
2963 */
Thinh Nguyenb10e1c22020-05-05 19:47:15 -07002964 if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
2965 unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
2966
2967 dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
2968 } else {
2969 dep->flags |= DWC3_EP_DELAY_START;
2970 dwc3_stop_active_transfer(dep, true, true);
2971 return;
2972 }
2973 break;
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07002974 }
2975
2976out:
2977 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
2978}
2979
Felipe Balbi72246da2011-08-19 18:10:58 +03002980static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2981 const struct dwc3_event_depevt *event)
2982{
2983 struct dwc3_ep *dep;
2984 u8 epnum = event->endpoint_number;
2985
2986 dep = dwc->eps[epnum];
2987
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002988 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbi3aec9912019-01-21 13:08:44 +02002989 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED))
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002990 return;
2991
2992 /* Handle only EPCMDCMPLT when EP disabled */
2993 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2994 return;
2995 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002996
Felipe Balbi72246da2011-08-19 18:10:58 +03002997 if (epnum == 0 || epnum == 1) {
2998 dwc3_ep0_interrupt(dwc, event);
2999 return;
3000 }
3001
3002 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003003 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003004 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003005 break;
3006 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03003007 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03003008 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003009 case DWC3_DEPEVT_EPCMDCMPLT:
Thinh Nguyen8266b082020-07-30 16:29:03 -07003010 dwc3_gadget_endpoint_command_complete(dep, event);
Baolin Wang76a638f2016-10-31 19:38:36 +08003011 break;
Felipe Balbi742a4ff2018-03-26 13:26:56 +03003012 case DWC3_DEPEVT_XFERCOMPLETE:
Thinh Nguyen3eaecd02020-05-05 19:46:51 -07003013 dwc3_gadget_endpoint_transfer_complete(dep, event);
3014 break;
3015 case DWC3_DEPEVT_STREAMEVT:
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003016 dwc3_gadget_endpoint_stream_event(dep, event);
3017 break;
Baolin Wang76a638f2016-10-31 19:38:36 +08003018 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03003019 break;
3020 }
3021}
3022
3023static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3024{
3025 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
3026 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003027 dwc->gadget_driver->disconnect(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003028 spin_lock(&dwc->lock);
3029 }
3030}
3031
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003032static void dwc3_suspend_gadget(struct dwc3 *dwc)
3033{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003034 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003035 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003036 dwc->gadget_driver->suspend(dwc->gadget);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003037 spin_lock(&dwc->lock);
3038 }
3039}
3040
3041static void dwc3_resume_gadget(struct dwc3 *dwc)
3042{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03003043 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003044 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003045 dwc->gadget_driver->resume(dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06003046 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08003047 }
3048}
3049
3050static void dwc3_reset_gadget(struct dwc3 *dwc)
3051{
3052 if (!dwc->gadget_driver)
3053 return;
3054
Peter Chene81a7012020-08-21 10:55:48 +08003055 if (dwc->gadget->speed != USB_SPEED_UNKNOWN) {
Felipe Balbi8e744752014-11-06 14:27:53 +08003056 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003057 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003058 spin_lock(&dwc->lock);
3059 }
3060}
3061
Felipe Balbic5353b22019-02-13 13:00:54 +02003062static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3063 bool interrupt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003064{
Felipe Balbi72246da2011-08-19 18:10:58 +03003065 struct dwc3_gadget_ep_cmd_params params;
3066 u32 cmd;
3067 int ret;
3068
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003069 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3070 (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303071 return;
3072
Pratyush Anand57911502012-07-06 15:19:10 +05303073 /*
3074 * NOTICE: We are violating what the Databook says about the
3075 * EndTransfer command. Ideally we would _always_ wait for the
3076 * EndTransfer Command Completion IRQ, but that's causing too
3077 * much trouble synchronizing between us and gadget driver.
3078 *
3079 * We have discussed this with the IP Provider and it was
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003080 * suggested to giveback all requests here.
Pratyush Anand57911502012-07-06 15:19:10 +05303081 *
3082 * Note also that a similar handling was tested by Synopsys
3083 * (thanks a lot Paul) and nothing bad has come out of it.
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003084 * In short, what we're doing is issuing EndTransfer with
3085 * CMDIOC bit set and delay kicking transfer until the
3086 * EndTransfer command had completed.
John Youn06281d42016-08-22 15:39:13 -07003087 *
3088 * As of IP version 3.10a of the DWC_usb3 IP, the controller
3089 * supports a mode to work around the above limitation. The
3090 * software can poll the CMDACT bit in the DEPCMD register
3091 * after issuing a EndTransfer command. This mode is enabled
3092 * by writing GUCTL2[14]. This polling is already done in the
3093 * dwc3_send_gadget_ep_cmd() function so if the mode is
3094 * enabled, the EndTransfer command will have completed upon
Thinh Nguyencf2f8b62019-12-18 18:14:56 -08003095 * returning from this function.
John Youn06281d42016-08-22 15:39:13 -07003096 *
3097 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05303098 */
3099
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303100 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03003101 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
Felipe Balbic5353b22019-02-13 13:00:54 +02003102 cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
Felipe Balbib4996a82012-06-06 12:04:13 +03003103 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303104 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03003105 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05303106 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03003107 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07003108
Thinh Nguyen140ca4c2020-05-05 19:47:09 -07003109 /*
3110 * The END_TRANSFER command will cause the controller to generate a
3111 * NoStream Event, and it's not due to the host DP NoStream rejection.
3112 * Ignore the next NoStream event.
3113 */
3114 if (dep->stream_capable)
3115 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3116
Thinh Nguyend3abda52019-11-27 13:10:47 -08003117 if (!interrupt)
3118 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
Thinh Nguyenc58d8bf2019-12-18 18:14:44 -08003119 else
3120 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003121}
3122
Felipe Balbi72246da2011-08-19 18:10:58 +03003123static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
3124{
3125 u32 epnum;
3126
3127 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3128 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03003129 int ret;
3130
3131 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03003132 if (!dep)
3133 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03003134
3135 if (!(dep->flags & DWC3_EP_STALL))
3136 continue;
3137
3138 dep->flags &= ~DWC3_EP_STALL;
3139
John Youn50c763f2016-05-31 17:49:56 -07003140 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03003141 WARN_ON_ONCE(ret);
3142 }
3143}
3144
3145static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
3146{
Felipe Balbic4430a22012-05-24 10:30:01 +03003147 int reg;
3148
Thinh Nguyen1b6009ea2019-10-23 19:15:49 -07003149 dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
3150
Felipe Balbi72246da2011-08-19 18:10:58 +03003151 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3152 reg &= ~DWC3_DCTL_INITU1ENA;
Felipe Balbi72246da2011-08-19 18:10:58 +03003153 reg &= ~DWC3_DCTL_INITU2ENA;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003154 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003155
Felipe Balbi72246da2011-08-19 18:10:58 +03003156 dwc3_disconnect_gadget(dwc);
3157
Peter Chene81a7012020-08-21 10:55:48 +08003158 dwc->gadget->speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03003159 dwc->setup_packet_pending = false;
Peter Chene81a7012020-08-21 10:55:48 +08003160 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03003161
3162 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003163}
3164
Felipe Balbi72246da2011-08-19 18:10:58 +03003165static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
3166{
3167 u32 reg;
3168
Felipe Balbifc8bb912016-05-16 13:14:48 +03003169 dwc->connected = true;
3170
Felipe Balbidf62df52011-10-14 15:11:49 +03003171 /*
3172 * WORKAROUND: DWC3 revisions <1.88a have an issue which
3173 * would cause a missing Disconnect Event if there's a
3174 * pending Setup Packet in the FIFO.
3175 *
3176 * There's no suggested workaround on the official Bug
3177 * report, which states that "unless the driver/application
3178 * is doing any special handling of a disconnect event,
3179 * there is no functional issue".
3180 *
3181 * Unfortunately, it turns out that we _do_ some special
3182 * handling of a disconnect event, namely complete all
3183 * pending transfers, notify gadget driver of the
3184 * disconnection, and so on.
3185 *
3186 * Our suggested workaround is to follow the Disconnect
3187 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06003188 * flag. Such flag gets set whenever we have a SETUP_PENDING
3189 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03003190 * same endpoint.
3191 *
3192 * Refers to:
3193 *
3194 * STAR#9000466709: RTL: Device : Disconnect event not
3195 * generated if setup packet pending in FIFO
3196 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003197 if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
Felipe Balbidf62df52011-10-14 15:11:49 +03003198 if (dwc->setup_packet_pending)
3199 dwc3_gadget_disconnect_interrupt(dwc);
3200 }
3201
Felipe Balbi8e744752014-11-06 14:27:53 +08003202 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003203
3204 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3205 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003206 dwc3_gadget_dctl_write_safe(dwc, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003207 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003208 dwc3_clear_stall_all_ep(dwc);
3209
3210 /* Reset device address to zero */
3211 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3212 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3213 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03003214}
3215
Felipe Balbi72246da2011-08-19 18:10:58 +03003216static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3217{
Felipe Balbi72246da2011-08-19 18:10:58 +03003218 struct dwc3_ep *dep;
3219 int ret;
3220 u32 reg;
3221 u8 speed;
3222
Felipe Balbi72246da2011-08-19 18:10:58 +03003223 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3224 speed = reg & DWC3_DSTS_CONNECTSPD;
3225 dwc->speed = speed;
3226
John Youn5fb6fda2016-11-10 17:23:25 -08003227 /*
3228 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3229 * each time on Connect Done.
3230 *
3231 * Currently we always use the reset value. If any platform
3232 * wants to set this to a different value, we need to add a
3233 * setting and update GCTL.RAMCLKSEL here.
3234 */
Felipe Balbi72246da2011-08-19 18:10:58 +03003235
3236 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003237 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003238 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003239 dwc->gadget->ep0->maxpacket = 512;
3240 dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
John Youn75808622016-02-05 17:09:13 -08003241 break;
John Youn2da9ad72016-05-20 16:34:26 -07003242 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003243 /*
3244 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3245 * would cause a missing USB3 Reset event.
3246 *
3247 * In such situations, we should force a USB3 Reset
3248 * event by calling our dwc3_gadget_reset_interrupt()
3249 * routine.
3250 *
3251 * Refers to:
3252 *
3253 * STAR#9000483510: RTL: SS : USB3 reset event may
3254 * not be generated always when the link enters poll
3255 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003256 if (DWC3_VER_IS_PRIOR(DWC3, 190A))
Felipe Balbi05870c52011-10-14 14:51:38 +03003257 dwc3_gadget_reset_interrupt(dwc);
3258
Felipe Balbi72246da2011-08-19 18:10:58 +03003259 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Peter Chene81a7012020-08-21 10:55:48 +08003260 dwc->gadget->ep0->maxpacket = 512;
3261 dwc->gadget->speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03003262 break;
John Youn2da9ad72016-05-20 16:34:26 -07003263 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003264 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003265 dwc->gadget->ep0->maxpacket = 64;
3266 dwc->gadget->speed = USB_SPEED_HIGH;
Felipe Balbi72246da2011-08-19 18:10:58 +03003267 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02003268 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003269 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
Peter Chene81a7012020-08-21 10:55:48 +08003270 dwc->gadget->ep0->maxpacket = 64;
3271 dwc->gadget->speed = USB_SPEED_FULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03003272 break;
John Youn2da9ad72016-05-20 16:34:26 -07003273 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003274 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
Peter Chene81a7012020-08-21 10:55:48 +08003275 dwc->gadget->ep0->maxpacket = 8;
3276 dwc->gadget->speed = USB_SPEED_LOW;
Felipe Balbi72246da2011-08-19 18:10:58 +03003277 break;
3278 }
3279
Peter Chene81a7012020-08-21 10:55:48 +08003280 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
Thinh Nguyen61800262018-01-12 18:18:05 -08003281
Pratyush Anand2b758352013-01-14 15:59:31 +05303282 /* Enable USB2 LPM Capability */
3283
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003284 if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003285 (speed != DWC3_DSTS_SUPERSPEED) &&
3286 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303287 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3288 reg |= DWC3_DCFG_LPM_CAP;
3289 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3290
3291 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3292 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3293
Thinh Nguyen16fe4f32019-08-19 18:35:58 -07003294 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
3295 (dwc->is_utmi_l1_suspend << 4));
Pratyush Anand2b758352013-01-14 15:59:31 +05303296
Huang Rui80caf7d2014-10-28 19:54:26 +08003297 /*
3298 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3299 * DCFG.LPMCap is set, core responses with an ACK and the
3300 * BESL value in the LPM token is less than or equal to LPM
3301 * NYET threshold.
3302 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003303 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09003304 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08003305
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003306 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
Thinh Nguyen2e487d22019-04-25 13:55:30 -07003307 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
Huang Rui80caf7d2014-10-28 19:54:26 +08003308
Thinh Nguyen5b738212019-10-23 19:15:43 -07003309 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003310 } else {
3311 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3312 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
Thinh Nguyen5b738212019-10-23 19:15:43 -07003313 dwc3_gadget_dctl_write_safe(dwc, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303314 }
3315
Felipe Balbi72246da2011-08-19 18:10:58 +03003316 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003317 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003318 if (ret) {
3319 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3320 return;
3321 }
3322
3323 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03003324 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03003325 if (ret) {
3326 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3327 return;
3328 }
3329
3330 /*
3331 * Configure PHY via GUSB3PIPECTLn if required.
3332 *
3333 * Update GTXFIFOSIZn
3334 *
3335 * In both cases reset values should be sufficient.
3336 */
3337}
3338
3339static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
3340{
Felipe Balbi72246da2011-08-19 18:10:58 +03003341 /*
3342 * TODO take core out of low power mode when that's
3343 * implemented.
3344 */
3345
Jiebing Liad14d4e2014-12-11 13:26:29 +08003346 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
3347 spin_unlock(&dwc->lock);
Peter Chene81a7012020-08-21 10:55:48 +08003348 dwc->gadget_driver->resume(dwc->gadget);
Jiebing Liad14d4e2014-12-11 13:26:29 +08003349 spin_lock(&dwc->lock);
3350 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003351}
3352
3353static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3354 unsigned int evtinfo)
3355{
Felipe Balbifae2b902011-10-14 13:00:30 +03003356 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003357 unsigned int pwropt;
3358
3359 /*
3360 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3361 * Hibernation mode enabled which would show up when device detects
3362 * host-initiated U3 exit.
3363 *
3364 * In that case, device will generate a Link State Change Interrupt
3365 * from U3 to RESUME which is only necessary if Hibernation is
3366 * configured in.
3367 *
3368 * There are no functional changes due to such spurious event and we
3369 * just need to ignore it.
3370 *
3371 * Refers to:
3372 *
3373 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3374 * operational mode
3375 */
3376 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003377 if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003378 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3379 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3380 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003381 return;
3382 }
3383 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003384
3385 /*
3386 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3387 * on the link partner, the USB session might do multiple entry/exit
3388 * of low power states before a transfer takes place.
3389 *
3390 * Due to this problem, we might experience lower throughput. The
3391 * suggested workaround is to disable DCTL[12:9] bits if we're
3392 * transitioning from U1/U2 to U0 and enable those bits again
3393 * after a transfer completes and there are no pending transfers
3394 * on any of the enabled endpoints.
3395 *
3396 * This is the first half of that workaround.
3397 *
3398 * Refers to:
3399 *
3400 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3401 * core send LGO_Ux entering U0
3402 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003403 if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
Felipe Balbifae2b902011-10-14 13:00:30 +03003404 if (next == DWC3_LINK_STATE_U0) {
3405 u32 u1u2;
3406 u32 reg;
3407
3408 switch (dwc->link_state) {
3409 case DWC3_LINK_STATE_U1:
3410 case DWC3_LINK_STATE_U2:
3411 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3412 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3413 | DWC3_DCTL_ACCEPTU2ENA
3414 | DWC3_DCTL_INITU1ENA
3415 | DWC3_DCTL_ACCEPTU1ENA);
3416
3417 if (!dwc->u1u2)
3418 dwc->u1u2 = reg & u1u2;
3419
3420 reg &= ~u1u2;
3421
Thinh Nguyen5b738212019-10-23 19:15:43 -07003422 dwc3_gadget_dctl_write_safe(dwc, reg);
Felipe Balbifae2b902011-10-14 13:00:30 +03003423 break;
3424 default:
3425 /* do nothing */
3426 break;
3427 }
3428 }
3429 }
3430
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003431 switch (next) {
3432 case DWC3_LINK_STATE_U1:
3433 if (dwc->speed == USB_SPEED_SUPER)
3434 dwc3_suspend_gadget(dwc);
3435 break;
3436 case DWC3_LINK_STATE_U2:
3437 case DWC3_LINK_STATE_U3:
3438 dwc3_suspend_gadget(dwc);
3439 break;
3440 case DWC3_LINK_STATE_RESUME:
3441 dwc3_resume_gadget(dwc);
3442 break;
3443 default:
3444 /* do nothing */
3445 break;
3446 }
3447
Felipe Balbie57ebc12014-04-22 13:20:12 -05003448 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03003449}
3450
Baolin Wang72704f82016-05-16 16:43:53 +08003451static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3452 unsigned int evtinfo)
3453{
3454 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3455
3456 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3457 dwc3_suspend_gadget(dwc);
3458
3459 dwc->link_state = next;
3460}
3461
Felipe Balbie1dadd32014-02-25 14:47:54 -06003462static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3463 unsigned int evtinfo)
3464{
3465 unsigned int is_ss = evtinfo & BIT(4);
3466
Felipe Balbibfad65e2017-04-19 14:59:27 +03003467 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06003468 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3469 * have a known issue which can cause USB CV TD.9.23 to fail
3470 * randomly.
3471 *
3472 * Because of this issue, core could generate bogus hibernation
3473 * events which SW needs to ignore.
3474 *
3475 * Refers to:
3476 *
3477 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3478 * Device Fallback from SuperSpeed
3479 */
3480 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3481 return;
3482
3483 /* enter hibernation here */
3484}
3485
Felipe Balbi72246da2011-08-19 18:10:58 +03003486static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3487 const struct dwc3_event_devt *event)
3488{
3489 switch (event->type) {
3490 case DWC3_DEVICE_EVENT_DISCONNECT:
3491 dwc3_gadget_disconnect_interrupt(dwc);
3492 break;
3493 case DWC3_DEVICE_EVENT_RESET:
3494 dwc3_gadget_reset_interrupt(dwc);
3495 break;
3496 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3497 dwc3_gadget_conndone_interrupt(dwc);
3498 break;
3499 case DWC3_DEVICE_EVENT_WAKEUP:
3500 dwc3_gadget_wakeup_interrupt(dwc);
3501 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003502 case DWC3_DEVICE_EVENT_HIBER_REQ:
3503 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3504 "unexpected hibernation event\n"))
3505 break;
3506
3507 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3508 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003509 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3510 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3511 break;
3512 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08003513 /* It changed to be suspend event for version 2.30a and above */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003514 if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
Baolin Wang72704f82016-05-16 16:43:53 +08003515 /*
3516 * Ignore suspend event until the gadget enters into
3517 * USB_STATE_CONFIGURED state.
3518 */
Peter Chene81a7012020-08-21 10:55:48 +08003519 if (dwc->gadget->state >= USB_STATE_CONFIGURED)
Baolin Wang72704f82016-05-16 16:43:53 +08003520 dwc3_gadget_suspend_interrupt(dwc,
3521 event->event_info);
3522 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003523 break;
3524 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03003525 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03003526 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03003527 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03003528 break;
3529 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06003530 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003531 }
3532}
3533
3534static void dwc3_process_event_entry(struct dwc3 *dwc,
3535 const union dwc3_event *event)
3536{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003537 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003538
Felipe Balbidfc5e802017-04-26 13:44:51 +03003539 if (!event->type.is_devspec)
3540 dwc3_endpoint_interrupt(dwc, &event->depevt);
3541 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003542 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003543 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003544 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003545}
3546
Felipe Balbidea520a2016-03-30 09:39:34 +03003547static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003548{
Felipe Balbidea520a2016-03-30 09:39:34 +03003549 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003550 irqreturn_t ret = IRQ_NONE;
3551 int left;
3552 u32 reg;
3553
Felipe Balbif42f2442013-06-12 21:25:08 +03003554 left = evt->count;
3555
3556 if (!(evt->flags & DWC3_EVENT_PENDING))
3557 return IRQ_NONE;
3558
3559 while (left > 0) {
3560 union dwc3_event event;
3561
John Younebbb2d52016-11-15 13:07:02 +02003562 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003563
3564 dwc3_process_event_entry(dwc, &event);
3565
3566 /*
3567 * FIXME we wrap around correctly to the next entry as
3568 * almost all entries are 4 bytes in size. There is one
3569 * entry which has 12 bytes which is a regular entry
3570 * followed by 8 bytes data. ATM I don't know how
3571 * things are organized if we get next to the a
3572 * boundary so I worry about that once we try to handle
3573 * that.
3574 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003575 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003576 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003577 }
3578
3579 evt->count = 0;
3580 evt->flags &= ~DWC3_EVENT_PENDING;
3581 ret = IRQ_HANDLED;
3582
3583 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003584 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003585 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003586 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003587
John Youncf40b862016-11-14 12:32:43 -08003588 if (dwc->imod_interval) {
3589 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3590 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3591 }
3592
Felipe Balbif42f2442013-06-12 21:25:08 +03003593 return ret;
3594}
3595
Felipe Balbidea520a2016-03-30 09:39:34 +03003596static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003597{
Felipe Balbidea520a2016-03-30 09:39:34 +03003598 struct dwc3_event_buffer *evt = _evt;
3599 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003600 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003601 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003602
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003603 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003604 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003605 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003606
3607 return ret;
3608}
3609
Felipe Balbidea520a2016-03-30 09:39:34 +03003610static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003611{
Felipe Balbidea520a2016-03-30 09:39:34 +03003612 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003613 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003614 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003615 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003616
Felipe Balbifc8bb912016-05-16 13:14:48 +03003617 if (pm_runtime_suspended(dwc->dev)) {
3618 pm_runtime_get(dwc->dev);
3619 disable_irq_nosync(dwc->irq_gadget);
3620 dwc->pending_events = true;
3621 return IRQ_HANDLED;
3622 }
3623
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003624 /*
3625 * With PCIe legacy interrupt, test shows that top-half irq handler can
3626 * be called again after HW interrupt deassertion. Check if bottom-half
3627 * irq event handler completes before caching new event to prevent
3628 * losing events.
3629 */
3630 if (evt->flags & DWC3_EVENT_PENDING)
3631 return IRQ_HANDLED;
3632
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003633 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003634 count &= DWC3_GEVNTCOUNT_MASK;
3635 if (!count)
3636 return IRQ_NONE;
3637
Felipe Balbib15a7622011-06-30 16:57:15 +03003638 evt->count = count;
3639 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003640
Felipe Balbie8adfc32013-06-12 21:11:14 +03003641 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003642 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003643 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003644 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003645
John Younebbb2d52016-11-15 13:07:02 +02003646 amount = min(count, evt->length - evt->lpos);
3647 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3648
3649 if (amount < count)
3650 memcpy(evt->cache, evt->buf, count - amount);
3651
John Youn65aca322016-11-15 13:08:59 +02003652 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3653
Felipe Balbib15a7622011-06-30 16:57:15 +03003654 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003655}
3656
Felipe Balbidea520a2016-03-30 09:39:34 +03003657static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003658{
Felipe Balbidea520a2016-03-30 09:39:34 +03003659 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003660
Felipe Balbidea520a2016-03-30 09:39:34 +03003661 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003662}
3663
Felipe Balbi6db38122016-10-03 11:27:01 +03003664static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3665{
3666 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3667 int irq;
3668
Hans de Goedef146b402019-10-05 23:04:48 +02003669 irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
Felipe Balbi6db38122016-10-03 11:27:01 +03003670 if (irq > 0)
3671 goto out;
3672
3673 if (irq == -EPROBE_DEFER)
3674 goto out;
3675
Hans de Goedef146b402019-10-05 23:04:48 +02003676 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
Felipe Balbi6db38122016-10-03 11:27:01 +03003677 if (irq > 0)
3678 goto out;
3679
3680 if (irq == -EPROBE_DEFER)
3681 goto out;
3682
3683 irq = platform_get_irq(dwc3_pdev, 0);
3684 if (irq > 0)
3685 goto out;
3686
Felipe Balbi6db38122016-10-03 11:27:01 +03003687 if (!irq)
3688 irq = -EINVAL;
3689
3690out:
3691 return irq;
3692}
3693
Peter Chene81a7012020-08-21 10:55:48 +08003694static void dwc_gadget_release(struct device *dev)
3695{
3696 struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
3697
3698 kfree(gadget);
3699}
3700
Felipe Balbi72246da2011-08-19 18:10:58 +03003701/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003702 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003703 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003704 *
3705 * Returns 0 on success otherwise negative errno.
3706 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003707int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003708{
Felipe Balbi6db38122016-10-03 11:27:01 +03003709 int ret;
3710 int irq;
Peter Chene81a7012020-08-21 10:55:48 +08003711 struct device *dev;
Roger Quadros9522def2016-06-10 14:48:38 +03003712
Felipe Balbi6db38122016-10-03 11:27:01 +03003713 irq = dwc3_gadget_get_irq(dwc);
3714 if (irq < 0) {
3715 ret = irq;
3716 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003717 }
3718
3719 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003720
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303721 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3722 sizeof(*dwc->ep0_trb) * 2,
3723 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003724 if (!dwc->ep0_trb) {
3725 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3726 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003727 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003728 }
3729
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003730 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003731 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003732 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003733 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003734 }
3735
Felipe Balbi905dc042017-01-05 14:46:52 +02003736 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3737 &dwc->bounce_addr, GFP_KERNEL);
3738 if (!dwc->bounce) {
3739 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003740 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003741 }
3742
Baolin Wangbb014732016-10-14 17:11:33 +08003743 init_completion(&dwc->ep0_in_setup);
Peter Chene81a7012020-08-21 10:55:48 +08003744 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
3745 if (!dwc->gadget) {
3746 ret = -ENOMEM;
3747 goto err3;
3748 }
Baolin Wangbb014732016-10-14 17:11:33 +08003749
Peter Chene81a7012020-08-21 10:55:48 +08003750
3751 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
3752 dev = &dwc->gadget->dev;
3753 dev->platform_data = dwc;
3754 dwc->gadget->ops = &dwc3_gadget_ops;
3755 dwc->gadget->speed = USB_SPEED_UNKNOWN;
3756 dwc->gadget->sg_supported = true;
3757 dwc->gadget->name = "dwc3-gadget";
3758 dwc->gadget->lpm_capable = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003759
3760 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003761 * FIXME We might be setting max_speed to <SUPER, however versions
3762 * <2.20a of dwc3 have an issue with metastability (documented
3763 * elsewhere in this driver) which tells us we can't set max speed to
3764 * anything lower than SUPER.
3765 *
3766 * Because gadget.max_speed is only used by composite.c and function
3767 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3768 * to happen so we avoid sending SuperSpeed Capability descriptor
3769 * together with our BOS descriptor as that could confuse host into
3770 * thinking we can handle super speed.
3771 *
3772 * Note that, in fact, we won't even support GetBOS requests when speed
3773 * is less than super speed because we don't have means, yet, to tell
3774 * composite.c that we are USB 2.0 + LPM ECN.
3775 */
Thinh Nguyen9af21dd2020-04-11 19:20:01 -07003776 if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
Roger Quadros42bf02e2017-10-31 15:11:55 +02003777 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003778 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003779 dwc->revision);
3780
Peter Chene81a7012020-08-21 10:55:48 +08003781 dwc->gadget->max_speed = dwc->maximum_speed;
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003782
3783 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003784 * REVISIT: Here we should clear all pending IRQs to be
3785 * sure we're starting from a well known location.
3786 */
3787
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003788 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003789 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003790 goto err4;
Peter Chene81a7012020-08-21 10:55:48 +08003791
3792 ret = usb_add_gadget(dwc->gadget);
3793 if (ret) {
3794 dev_err(dwc->dev, "failed to add gadget\n");
3795 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003796 }
3797
Peter Chene81a7012020-08-21 10:55:48 +08003798 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
Roger Quadros169e3b62019-01-10 17:04:28 +02003799
Felipe Balbi72246da2011-08-19 18:10:58 +03003800 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003801
Peter Chene81a7012020-08-21 10:55:48 +08003802err5:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003803 dwc3_gadget_free_endpoints(dwc);
Peter Chene81a7012020-08-21 10:55:48 +08003804err4:
3805 usb_put_gadget(dwc->gadget);
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003806err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003807 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3808 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003809
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003810err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003811 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003812
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003813err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303814 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003815 dwc->ep0_trb, dwc->ep0_trb_addr);
3816
Felipe Balbi72246da2011-08-19 18:10:58 +03003817err0:
3818 return ret;
3819}
3820
Felipe Balbi7415f172012-04-30 14:56:33 +03003821/* -------------------------------------------------------------------------- */
3822
Felipe Balbi72246da2011-08-19 18:10:58 +03003823void dwc3_gadget_exit(struct dwc3 *dwc)
3824{
Peter Chene81a7012020-08-21 10:55:48 +08003825 usb_del_gadget_udc(dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003826 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003827 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003828 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003829 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303830 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003831 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003832}
Felipe Balbi7415f172012-04-30 14:56:33 +03003833
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003834int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003835{
Roger Quadros9772b472016-04-12 11:33:29 +03003836 if (!dwc->gadget_driver)
3837 return 0;
3838
Roger Quadros1551e352017-02-15 14:16:26 +02003839 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003840 dwc3_disconnect_gadget(dwc);
3841 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003842
3843 return 0;
3844}
3845
3846int dwc3_gadget_resume(struct dwc3 *dwc)
3847{
Felipe Balbi7415f172012-04-30 14:56:33 +03003848 int ret;
3849
Roger Quadros9772b472016-04-12 11:33:29 +03003850 if (!dwc->gadget_driver)
3851 return 0;
3852
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003853 ret = __dwc3_gadget_start(dwc);
3854 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003855 goto err0;
3856
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003857 ret = dwc3_gadget_run_stop(dwc, true, false);
3858 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003859 goto err1;
3860
Felipe Balbi7415f172012-04-30 14:56:33 +03003861 return 0;
3862
3863err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003864 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003865
3866err0:
3867 return ret;
3868}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003869
3870void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3871{
3872 if (dwc->pending_events) {
3873 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3874 dwc->pending_events = false;
3875 enable_irq(dwc->irq_gadget);
3876 }
3877}