blob: e9e0e280543164f0bae66e3e83db2481c0b24b2f [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 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://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 Balbi04a9bfc2012-01-02 18:25:43 +020030/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030031 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020032 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
34 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030035 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037 */
38int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
39{
40 u32 reg;
41
42 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
43 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
44
45 switch (mode) {
46 case TEST_J:
47 case TEST_K:
48 case TEST_SE0_NAK:
49 case TEST_PACKET:
50 case TEST_FORCE_EN:
51 reg |= mode << 1;
52 break;
53 default:
54 return -EINVAL;
55 }
56
57 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
58
59 return 0;
60}
61
Felipe Balbi8598bde2012-01-02 18:55:57 +020062/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030063 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030064 * @dwc: pointer to our context structure
65 *
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
68 */
69int dwc3_gadget_get_link_state(struct dwc3 *dwc)
70{
71 u32 reg;
72
73 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
74
75 return DWC3_DSTS_USBLNKST(reg);
76}
77
78/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030079 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020080 * @dwc: pointer to our context structure
81 * @state: the state to put link into
82 *
83 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080084 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020085 */
86int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
87{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080088 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020089 u32 reg;
90
Paul Zimmerman802fde92012-04-27 13:10:52 +030091 /*
92 * Wait until device controller is ready. Only applies to 1.94a and
93 * later RTL.
94 */
95 if (dwc->revision >= DWC3_REVISION_194A) {
96 while (--retries) {
97 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
98 if (reg & DWC3_DSTS_DCNRD)
99 udelay(5);
100 else
101 break;
102 }
103
104 if (retries <= 0)
105 return -ETIMEDOUT;
106 }
107
Felipe Balbi8598bde2012-01-02 18:55:57 +0200108 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
109 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
110
111 /* set requested state */
112 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
113 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
114
Paul Zimmerman802fde92012-04-27 13:10:52 +0300115 /*
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
118 */
119 if (dwc->revision >= DWC3_REVISION_194A)
120 return 0;
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300123 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200124 while (--retries) {
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
126
Felipe Balbi8598bde2012-01-02 18:55:57 +0200127 if (DWC3_DSTS_USBLNKST(reg) == state)
128 return 0;
129
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800130 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 }
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 return -ETIMEDOUT;
134}
135
John Youndca01192016-05-19 17:26:05 -0700136/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700139 *
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
143 */
144static void dwc3_ep_inc_trb(u8 *index)
145{
146 (*index)++;
147 if (*index == (DWC3_TRB_NUM - 1))
148 *index = 0;
149}
150
Felipe Balbibfad65e2017-04-19 14:59:27 +0300151/**
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
154 */
Felipe Balbief966b92016-04-05 13:09:51 +0300155static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200156{
John Youndca01192016-05-19 17:26:05 -0700157 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbibfad65e2017-04-19 14:59:27 +0300160/**
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
Felipe Balbief966b92016-04-05 13:09:51 +0300164static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
165{
John Youndca01192016-05-19 17:26:05 -0700166 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167}
168
Felipe Balbic91815b2018-03-26 13:14:47 +0300169void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
170 struct dwc3_request *req, int status)
171{
172 struct dwc3 *dwc = dep->dwc;
173
174 req->started = false;
175 list_del(&req->list);
176 req->remaining = 0;
177
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
180
181 if (req->trb)
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
183 &req->request, req->direction);
184
185 req->trb = NULL;
186 trace_dwc3_gadget_giveback(req);
187
188 if (dep->number > 1)
189 pm_runtime_put(dwc->dev);
190}
191
Felipe Balbibfad65e2017-04-19 14:59:27 +0300192/**
193 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
194 * @dep: The endpoint to whom the request belongs to
195 * @req: The request we're giving back
196 * @status: completion code for the request
197 *
198 * Must be called with controller's lock held and interrupts disabled. This
199 * function will unmap @req and call its ->complete() callback to notify upper
200 * layers that it has completed.
201 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300202void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
203 int status)
204{
205 struct dwc3 *dwc = dep->dwc;
206
Felipe Balbic91815b2018-03-26 13:14:47 +0300207 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300208
209 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 spin_lock(&dwc->lock);
212}
213
Felipe Balbibfad65e2017-04-19 14:59:27 +0300214/**
215 * dwc3_send_gadget_generic_command - issue a generic command for the controller
216 * @dwc: pointer to the controller context
217 * @cmd: the command to be issued
218 * @param: command parameter
219 *
220 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
221 * and wait for its completion.
222 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500223int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300224{
225 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300226 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300227 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300228 u32 reg;
229
230 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
231 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
232
233 do {
234 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
235 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300236 status = DWC3_DGCMD_STATUS(reg);
237 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300238 ret = -EINVAL;
239 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300240 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100241 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300242
243 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300244 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300245 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 }
247
Felipe Balbi71f7e702016-05-23 14:16:19 +0300248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
249
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300251}
252
Felipe Balbic36d8e92016-04-04 12:46:33 +0300253static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
254
Felipe Balbibfad65e2017-04-19 14:59:27 +0300255/**
256 * dwc3_send_gadget_ep_cmd - issue an endpoint command
257 * @dep: the endpoint to which the command is going to be issued
258 * @cmd: the command to be issued
259 * @params: parameters to the command
260 *
261 * Caller should handle locking. This function will issue @cmd with given
262 * @params to @dep and wait for its completion.
263 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300264int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
Felipe Balbi8897a762016-09-22 10:56:08 +0300267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300268 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000269 u32 timeout = 1000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 u32 reg;
271
Felipe Balbi0933df12016-05-23 14:02:33 +0300272 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300273 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300274 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300276 /*
277 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
278 * we're issuing an endpoint command, we must check if
279 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
280 *
281 * We will also set SUSPHY bit to what it was before returning as stated
282 * by the same section on Synopsys databook.
283 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300284 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
285 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
286 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
287 susphy = true;
288 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
289 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
290 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300291 }
292
Felipe Balbi59999142016-09-22 12:25:28 +0300293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300294 int needs_wakeup;
295
296 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
297 dwc->link_state == DWC3_LINK_STATE_U2 ||
298 dwc->link_state == DWC3_LINK_STATE_U3);
299
300 if (unlikely(needs_wakeup)) {
301 ret = __dwc3_gadget_wakeup(dwc);
302 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
303 ret);
304 }
305 }
306
Felipe Balbi2eb88012016-04-12 16:53:39 +0300307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300310
Felipe Balbi8897a762016-09-22 10:56:08 +0300311 /*
312 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
313 * not relying on XferNotReady, we can make use of a special "No
314 * Response Update Transfer" command where we should clear both CmdAct
315 * and CmdIOC bits.
316 *
317 * With this, we don't need to wait for command completion and can
318 * straight away issue further commands to the endpoint.
319 *
320 * NOTICE: We're making an assumption that control endpoints will never
321 * make use of Update Transfer command. This is a safe assumption
322 * because we can never have more than one request at a time with
323 * Control Endpoints. If anybody changes that assumption, this chunk
324 * needs to be updated accordingly.
325 */
326 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
327 !usb_endpoint_xfer_isoc(desc))
328 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
329 else
330 cmd |= DWC3_DEPCMD_CMDACT;
331
332 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000337
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000338 switch (cmd_status) {
339 case 0:
340 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300341 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000342 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000343 ret = -EINVAL;
344 break;
345 case DEPEVT_TRANSFER_BUS_EXPIRY:
346 /*
347 * SW issues START TRANSFER command to
348 * isochronous ep with future frame interval. If
349 * future interval time has already passed when
350 * core receives the command, it will respond
351 * with an error status of 'Bus Expiry'.
352 *
353 * Instead of always returning -EINVAL, let's
354 * give a hint to the gadget driver that this is
355 * the case by returning -EAGAIN.
356 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000357 ret = -EAGAIN;
358 break;
359 default:
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
361 }
362
Felipe Balbic0ca3242016-04-04 09:11:51 +0300363 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300365 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366
Felipe Balbif6bb2252016-05-23 13:53:34 +0300367 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300368 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300369 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300370 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300371
Felipe Balbi0933df12016-05-23 14:02:33 +0300372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
373
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300374 if (ret == 0) {
375 switch (DWC3_DEPCMD_CMD(cmd)) {
376 case DWC3_DEPCMD_STARTTRANSFER:
377 dep->flags |= DWC3_EP_TRANSFER_STARTED;
378 break;
379 case DWC3_DEPCMD_ENDTRANSFER:
380 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
381 break;
382 default:
383 /* nothing */
384 break;
385 }
386 }
387
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300388 if (unlikely(susphy)) {
389 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
392 }
393
Felipe Balbic0ca3242016-04-04 09:11:51 +0300394 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395}
396
John Youn50c763f2016-05-31 17:49:56 -0700397static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400 struct dwc3_gadget_ep_cmd_params params;
401 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
402
403 /*
404 * As of core revision 2.60a the recommended programming model
405 * is to set the ClearPendIN bit when issuing a Clear Stall EP
406 * command for IN endpoints. This is to prevent an issue where
407 * some (non-compliant) hosts may not send ACK TPs for pending
408 * IN transfers due to a mishandled error condition. Synopsys
409 * STAR 9000614252.
410 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
414
415 memset(&params, 0, sizeof(params));
416
Felipe Balbi2cd47182016-04-12 16:42:43 +0300417 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700418}
419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200421 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300422{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300423 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
425 return dep->trb_pool_dma + offset;
426}
427
428static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
429{
430 struct dwc3 *dwc = dep->dwc;
431
432 if (dep->trb_pool)
433 return 0;
434
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
437 &dep->trb_pool_dma, GFP_KERNEL);
438 if (!dep->trb_pool) {
439 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
440 dep->name);
441 return -ENOMEM;
442 }
443
444 return 0;
445}
446
447static void dwc3_free_trb_pool(struct dwc3_ep *dep)
448{
449 struct dwc3 *dwc = dep->dwc;
450
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 dep->trb_pool, dep->trb_pool_dma);
453
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
456}
457
Felipe Balbi20d1d432018-04-09 12:49:02 +0300458static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
459{
460 struct dwc3_gadget_ep_cmd_params params;
461
462 memset(&params, 0x00, sizeof(params));
463
464 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
465
466 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
467 &params);
468}
John Younc4509602016-02-16 20:10:53 -0800469
470/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300471 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800472 * @dwc: pointer to our controller context structure
473 * @dep: endpoint that is being enabled
474 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300475 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
476 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800477 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300478 * The assignment of transfer resources cannot perfectly follow the data book
479 * due to the fact that the controller driver does not have all knowledge of the
480 * configuration in advance. It is given this information piecemeal by the
481 * composite gadget framework after every SET_CONFIGURATION and
482 * SET_INTERFACE. Trying to follow the databook programming model in this
483 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800484 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300485 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
486 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
487 * incorrect in the scenario of multiple interfaces.
488 *
489 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800490 * endpoint on alt setting (8.1.6).
491 *
492 * The following simplified method is used instead:
493 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300494 * All hardware endpoints can be assigned a transfer resource and this setting
495 * will stay persistent until either a core reset or hibernation. So whenever we
496 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
497 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800498 * guaranteed that there are as many transfer resources as endpoints.
499 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300500 * This function is called for each endpoint when it is being enabled but is
501 * triggered only when called for EP0-out, which always happens first, and which
502 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800503 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300504static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300505{
506 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300507 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300508 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800509 int i;
510 int ret;
511
512 if (dep->number)
513 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
515 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800516 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300517 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300518
Felipe Balbi2cd47182016-04-12 16:42:43 +0300519 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800520 if (ret)
521 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300522
John Younc4509602016-02-16 20:10:53 -0800523 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
524 struct dwc3_ep *dep = dwc->eps[i];
525
526 if (!dep)
527 continue;
528
Felipe Balbib07c2db2018-04-09 12:46:47 +0300529 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800530 if (ret)
531 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 }
533
534 return 0;
535}
536
Felipe Balbib07c2db2018-04-09 12:46:47 +0300537static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300538{
John Youn39ebb052016-11-09 16:36:28 -0800539 const struct usb_ss_ep_comp_descriptor *comp_desc;
540 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300542 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
John Youn39ebb052016-11-09 16:36:28 -0800544 comp_desc = dep->endpoint.comp_desc;
545 desc = dep->endpoint.desc;
546
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 memset(&params, 0x00, sizeof(params));
548
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300549 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900550 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
551
552 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800553 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300554 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300555 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900556 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbia2d23f02018-04-09 12:40:48 +0300558 params.param0 |= action;
559 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600560 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600561
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300562 if (usb_endpoint_xfer_control(desc))
563 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300564
565 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
566 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200568 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300569 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
570 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300571 dep->stream_capable = true;
572 }
573
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500574 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300575 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300576
577 /*
578 * We are doing 1:1 mapping for endpoints, meaning
579 * Physical Endpoints 2 maps to Logical Endpoint 2 and
580 * so on. We consider the direction bit as part of the physical
581 * endpoint number. So USB endpoint 0x81 is 0x03.
582 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584
585 /*
586 * We must use the lower 16 TX FIFOs even though
587 * HW might have more
588 */
589 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300593 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 dep->interval = 1 << (desc->bInterval - 1);
595 }
596
Felipe Balbi2cd47182016-04-12 16:42:43 +0300597 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300598}
599
Felipe Balbi72246da2011-08-19 18:10:58 +0300600/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300601 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300602 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300603 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300605 * Caller should take care of locking. Execute all necessary commands to
606 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300608static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300609{
John Youn39ebb052016-11-09 16:36:28 -0800610 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800612
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300614 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
616 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300617 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 if (ret)
619 return ret;
620 }
621
Felipe Balbib07c2db2018-04-09 12:46:47 +0300622 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 if (ret)
624 return ret;
625
626 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200627 struct dwc3_trb *trb_st_hw;
628 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629
Felipe Balbi72246da2011-08-19 18:10:58 +0300630 dep->type = usb_endpoint_type(desc);
631 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800632 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633
634 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
635 reg |= DWC3_DALEPENA_EP(dep->number);
636 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
637
Baolin Wang76a638f2016-10-31 19:38:36 +0800638 init_waitqueue_head(&dep->wait_end_transfer);
639
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300640 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200641 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300642
John Youn0d257442016-05-19 17:26:08 -0700643 /* Initialize the TRB ring */
644 dep->trb_dequeue = 0;
645 dep->trb_enqueue = 0;
646 memset(dep->trb_pool, 0,
647 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
648
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300649 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 trb_st_hw = &dep->trb_pool[0];
651
Felipe Balbif6bafc62012-02-06 11:04:53 +0200652 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200653 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 }
658
Felipe Balbia97ea992016-09-29 16:28:56 +0300659 /*
660 * Issue StartTransfer here with no-op TRB so we can always rely on No
661 * Response Update Transfer command.
662 */
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300663 if (usb_endpoint_xfer_bulk(desc) ||
664 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300665 struct dwc3_gadget_ep_cmd_params params;
666 struct dwc3_trb *trb;
667 dma_addr_t trb_dma;
668 u32 cmd;
669
670 memset(&params, 0, sizeof(params));
671 trb = &dep->trb_pool[0];
672 trb_dma = dwc3_trb_dma_offset(dep, trb);
673
674 params.param0 = upper_32_bits(trb_dma);
675 params.param1 = lower_32_bits(trb_dma);
676
677 cmd = DWC3_DEPCMD_STARTTRANSFER;
678
679 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
680 if (ret < 0)
681 return ret;
682
Felipe Balbia97ea992016-09-29 16:28:56 +0300683 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
684 WARN_ON_ONCE(!dep->resource_index);
685 }
686
Felipe Balbi2870e502016-11-03 13:53:29 +0200687out:
688 trace_dwc3_gadget_ep_enable(dep);
689
Felipe Balbi72246da2011-08-19 18:10:58 +0300690 return 0;
691}
692
Felipe Balbi8f608e82018-03-27 10:53:29 +0300693static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200694static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300695{
696 struct dwc3_request *req;
697
Felipe Balbi8f608e82018-03-27 10:53:29 +0300698 dwc3_stop_active_transfer(dep, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300699
Felipe Balbi0e146022016-06-21 10:32:02 +0300700 /* - giveback all requests to gadget driver */
701 while (!list_empty(&dep->started_list)) {
702 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200703
Felipe Balbi0e146022016-06-21 10:32:02 +0300704 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200705 }
706
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200707 while (!list_empty(&dep->pending_list)) {
708 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300709
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200710 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300711 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300712}
713
714/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300715 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 * @dep: the endpoint to disable
717 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300718 * This function undoes what __dwc3_gadget_ep_enable did and also removes
719 * requests which are currently being processed by the hardware and those which
720 * are not yet scheduled.
721 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200722 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300724static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
725{
726 struct dwc3 *dwc = dep->dwc;
727 u32 reg;
728
Felipe Balbi2870e502016-11-03 13:53:29 +0200729 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500730
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200731 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300732
Felipe Balbi687ef982014-04-16 10:30:33 -0500733 /* make sure HW endpoint isn't stalled */
734 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500735 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500736
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
738 reg &= ~DWC3_DALEPENA_EP(dep->number);
739 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
740
Felipe Balbi879631a2011-09-30 10:58:47 +0300741 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300742 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800743 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
John Youn39ebb052016-11-09 16:36:28 -0800745 /* Clear out the ep descriptors for non-ep0 */
746 if (dep->number > 1) {
747 dep->endpoint.comp_desc = NULL;
748 dep->endpoint.desc = NULL;
749 }
750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 return 0;
752}
753
754/* -------------------------------------------------------------------------- */
755
756static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
757 const struct usb_endpoint_descriptor *desc)
758{
759 return -EINVAL;
760}
761
762static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
763{
764 return -EINVAL;
765}
766
767/* -------------------------------------------------------------------------- */
768
769static int dwc3_gadget_ep_enable(struct usb_ep *ep,
770 const struct usb_endpoint_descriptor *desc)
771{
772 struct dwc3_ep *dep;
773 struct dwc3 *dwc;
774 unsigned long flags;
775 int ret;
776
777 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
778 pr_debug("dwc3: invalid parameters\n");
779 return -EINVAL;
780 }
781
782 if (!desc->wMaxPacketSize) {
783 pr_debug("dwc3: missing wMaxPacketSize\n");
784 return -EINVAL;
785 }
786
787 dep = to_dwc3_ep(ep);
788 dwc = dep->dwc;
789
Felipe Balbi95ca9612015-12-10 13:08:20 -0600790 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
791 "%s is already enabled\n",
792 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300793 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300794
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300796 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 spin_unlock_irqrestore(&dwc->lock, flags);
798
799 return ret;
800}
801
802static int dwc3_gadget_ep_disable(struct usb_ep *ep)
803{
804 struct dwc3_ep *dep;
805 struct dwc3 *dwc;
806 unsigned long flags;
807 int ret;
808
809 if (!ep) {
810 pr_debug("dwc3: invalid parameters\n");
811 return -EINVAL;
812 }
813
814 dep = to_dwc3_ep(ep);
815 dwc = dep->dwc;
816
Felipe Balbi95ca9612015-12-10 13:08:20 -0600817 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
818 "%s is already disabled\n",
819 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300820 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300821
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 spin_lock_irqsave(&dwc->lock, flags);
823 ret = __dwc3_gadget_ep_disable(dep);
824 spin_unlock_irqrestore(&dwc->lock, flags);
825
826 return ret;
827}
828
829static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300830 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300831{
832 struct dwc3_request *req;
833 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300834
835 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900836 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838
839 req->epnum = dep->number;
840 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300841
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500842 trace_dwc3_alloc_request(req);
843
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 return &req->request;
845}
846
847static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
848 struct usb_request *request)
849{
850 struct dwc3_request *req = to_dwc3_request(request);
851
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500852 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 kfree(req);
854}
855
Felipe Balbi42626912018-04-09 13:01:43 +0300856/**
857 * dwc3_ep_prev_trb - returns the previous TRB in the ring
858 * @dep: The endpoint with the TRB ring
859 * @index: The index of the current TRB in the ring
860 *
861 * Returns the TRB prior to the one pointed to by the index. If the
862 * index is 0, we will wrap backwards, skip the link TRB, and return
863 * the one just before that.
864 */
865static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
866{
867 u8 tmp = index;
868
869 if (!tmp)
870 tmp = DWC3_TRB_NUM - 1;
871
872 return &dep->trb_pool[tmp - 1];
873}
874
875static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
876{
877 struct dwc3_trb *tmp;
878 u8 trbs_left;
879
880 /*
881 * If enqueue & dequeue are equal than it is either full or empty.
882 *
883 * One way to know for sure is if the TRB right before us has HWO bit
884 * set or not. If it has, then we're definitely full and can't fit any
885 * more transfers in our ring.
886 */
887 if (dep->trb_enqueue == dep->trb_dequeue) {
888 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
889 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
890 return 0;
891
892 return DWC3_TRB_NUM - 1;
893 }
894
895 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
896 trbs_left &= (DWC3_TRB_NUM - 1);
897
898 if (dep->trb_dequeue < dep->trb_enqueue)
899 trbs_left--;
900
901 return trbs_left;
902}
Felipe Balbi2c78c022016-08-12 13:13:10 +0300903
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200904static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
905 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
906 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200907{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300908 struct dwc3 *dwc = dep->dwc;
909 struct usb_gadget *gadget = &dwc->gadget;
910 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200911
Felipe Balbief966b92016-04-05 13:09:51 +0300912 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530913
Felipe Balbif6bafc62012-02-06 11:04:53 +0200914 trb->size = DWC3_TRB_SIZE_LENGTH(length);
915 trb->bpl = lower_32_bits(dma);
916 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200917
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200918 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200919 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200920 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200921 break;
922
923 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300924 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530925 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300926
Manu Gautam40d829f2017-07-19 17:07:10 +0530927 /*
928 * USB Specification 2.0 Section 5.9.2 states that: "If
929 * there is only a single transaction in the microframe,
930 * only a DATA0 data packet PID is used. If there are
931 * two transactions per microframe, DATA1 is used for
932 * the first transaction data packet and DATA0 is used
933 * for the second transaction data packet. If there are
934 * three transactions per microframe, DATA2 is used for
935 * the first transaction data packet, DATA1 is used for
936 * the second, and DATA0 is used for the third."
937 *
938 * IOW, we should satisfy the following cases:
939 *
940 * 1) length <= maxpacket
941 * - DATA0
942 *
943 * 2) maxpacket < length <= (2 * maxpacket)
944 * - DATA1, DATA0
945 *
946 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
947 * - DATA2, DATA1, DATA0
948 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300949 if (speed == USB_SPEED_HIGH) {
950 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530951 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530952 unsigned int maxp = usb_endpoint_maxp(ep->desc);
953
954 if (length <= (2 * maxp))
955 mult--;
956
957 if (length <= maxp)
958 mult--;
959
960 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300961 }
962 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530963 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300964 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200965
966 /* always enable Interrupt on Missed ISOC */
967 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200968 break;
969
970 case USB_ENDPOINT_XFER_BULK:
971 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200972 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200973 break;
974 default:
975 /*
976 * This is only possible with faulty memory because we
977 * checked it already :)
978 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300979 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
980 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200981 }
982
Felipe Balbica4d44e2016-03-10 13:53:27 +0200983 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300984 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300985 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600986
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200987 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300988 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
989 }
990
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200991 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300992 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300993 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200994
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530995 if (chain)
996 trb->ctrl |= DWC3_TRB_CTRL_CHN;
997
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200998 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200999 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001000
1001 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001002
1003 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001004}
1005
John Youn361572b2016-05-19 17:26:17 -07001006/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001007 * dwc3_prepare_one_trb - setup one TRB from one request
1008 * @dep: endpoint for which this request is prepared
1009 * @req: dwc3_request pointer
1010 * @chain: should this TRB be chained to the next?
1011 * @node: only for isochronous endpoints. First TRB needs different type.
1012 */
1013static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1014 struct dwc3_request *req, unsigned chain, unsigned node)
1015{
1016 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301017 unsigned int length;
1018 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001019 unsigned stream_id = req->request.stream_id;
1020 unsigned short_not_ok = req->request.short_not_ok;
1021 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301022
1023 if (req->request.num_sgs > 0) {
1024 length = sg_dma_len(req->start_sg);
1025 dma = sg_dma_address(req->start_sg);
1026 } else {
1027 length = req->request.length;
1028 dma = req->request.dma;
1029 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001030
1031 trb = &dep->trb_pool[dep->trb_enqueue];
1032
1033 if (!req->trb) {
1034 dwc3_gadget_move_started_request(req);
1035 req->trb = trb;
1036 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001037 }
1038
1039 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1040 stream_id, short_not_ok, no_interrupt);
1041}
1042
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001043static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001044 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001045{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301046 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001047 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001048 int i;
1049
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301050 unsigned int remaining = req->request.num_mapped_sgs
1051 - req->num_queued_sgs;
1052
1053 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001054 unsigned int length = req->request.length;
1055 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1056 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001057 unsigned chain = true;
1058
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001059 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 chain = false;
1061
Felipe Balbic6267a52017-01-05 14:58:46 +02001062 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1063 struct dwc3 *dwc = dep->dwc;
1064 struct dwc3_trb *trb;
1065
1066 req->unaligned = true;
1067
1068 /* prepare normal TRB */
1069 dwc3_prepare_one_trb(dep, req, true, i);
1070
1071 /* Now prepare one extra TRB to align transfer size */
1072 trb = &dep->trb_pool[dep->trb_enqueue];
1073 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1074 maxp - rem, false, 0,
1075 req->request.stream_id,
1076 req->request.short_not_ok,
1077 req->request.no_interrupt);
1078 } else {
1079 dwc3_prepare_one_trb(dep, req, chain, i);
1080 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001081
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301082 /*
1083 * There can be a situation where all sgs in sglist are not
1084 * queued because of insufficient trb number. To handle this
1085 * case, update start_sg to next sg to be queued, so that
1086 * we have free trbs we can continue queuing from where we
1087 * previously stopped
1088 */
1089 if (chain)
1090 req->start_sg = sg_next(s);
1091
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301092 req->num_queued_sgs++;
1093
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001094 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001095 break;
1096 }
1097}
1098
1099static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001100 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001101{
Felipe Balbic6267a52017-01-05 14:58:46 +02001102 unsigned int length = req->request.length;
1103 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1104 unsigned int rem = length % maxp;
1105
1106 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1107 struct dwc3 *dwc = dep->dwc;
1108 struct dwc3_trb *trb;
1109
1110 req->unaligned = true;
1111
1112 /* prepare normal TRB */
1113 dwc3_prepare_one_trb(dep, req, true, 0);
1114
1115 /* Now prepare one extra TRB to align transfer size */
1116 trb = &dep->trb_pool[dep->trb_enqueue];
1117 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1118 false, 0, req->request.stream_id,
1119 req->request.short_not_ok,
1120 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001121 } else if (req->request.zero && req->request.length &&
1122 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1123 struct dwc3 *dwc = dep->dwc;
1124 struct dwc3_trb *trb;
1125
1126 req->zero = true;
1127
1128 /* prepare normal TRB */
1129 dwc3_prepare_one_trb(dep, req, true, 0);
1130
1131 /* Now prepare one extra TRB to handle ZLP */
1132 trb = &dep->trb_pool[dep->trb_enqueue];
1133 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1134 false, 0, req->request.stream_id,
1135 req->request.short_not_ok,
1136 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001137 } else {
1138 dwc3_prepare_one_trb(dep, req, false, 0);
1139 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001140}
1141
Felipe Balbi72246da2011-08-19 18:10:58 +03001142/*
1143 * dwc3_prepare_trbs - setup TRBs from requests
1144 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001146 * The function goes through the requests list and sets up TRBs for the
1147 * transfers. The function returns once there are no more TRBs available or
1148 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001149 */
Felipe Balbic4233572016-05-12 14:08:34 +03001150static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001151{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001152 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001153
1154 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1155
Felipe Balbid86c5a62016-10-25 13:48:52 +03001156 /*
1157 * We can get in a situation where there's a request in the started list
1158 * but there weren't enough TRBs to fully kick it in the first time
1159 * around, so it has been waiting for more TRBs to be freed up.
1160 *
1161 * In that case, we should check if we have a request with pending_sgs
1162 * in the started list and prepare TRBs for that request first,
1163 * otherwise we will prepare TRBs completely out of order and that will
1164 * break things.
1165 */
1166 list_for_each_entry(req, &dep->started_list, list) {
1167 if (req->num_pending_sgs > 0)
1168 dwc3_prepare_one_trb_sg(dep, req);
1169
1170 if (!dwc3_calc_trbs_left(dep))
1171 return;
1172 }
1173
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001174 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001175 struct dwc3 *dwc = dep->dwc;
1176 int ret;
1177
1178 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1179 dep->direction);
1180 if (ret)
1181 return;
1182
1183 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301184 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301185 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001186 req->num_pending_sgs = req->request.num_mapped_sgs;
1187
Felipe Balbi1f512112016-08-12 13:17:27 +03001188 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001189 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001190 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001191 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001192
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001193 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001194 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001195 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001196}
1197
Felipe Balbi7fdca762017-09-05 14:41:34 +03001198static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001199{
1200 struct dwc3_gadget_ep_cmd_params params;
1201 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001202 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001203 int ret;
1204 u32 cmd;
1205
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001206 if (!dwc3_calc_trbs_left(dep))
1207 return 0;
1208
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001209 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001210
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001211 dwc3_prepare_trbs(dep);
1212 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001213 if (!req) {
1214 dep->flags |= DWC3_EP_PENDING_REQUEST;
1215 return 0;
1216 }
1217
1218 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001219
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001220 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301221 params.param0 = upper_32_bits(req->trb_dma);
1222 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001223 cmd = DWC3_DEPCMD_STARTTRANSFER;
1224
1225 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1226 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301227 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001228 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1229 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301230 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001231
Felipe Balbi2cd47182016-04-12 16:42:43 +03001232 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001233 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001234 /*
1235 * FIXME we need to iterate over the list of requests
1236 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001237 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001238 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001239 if (req->trb)
1240 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001241 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 return ret;
1243 }
1244
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001245 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001246 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001247 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001248 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001249
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 return 0;
1251}
1252
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001253static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1254{
1255 u32 reg;
1256
1257 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1258 return DWC3_DSTS_SOFFN(reg);
1259}
1260
Felipe Balbi5828cad2018-03-27 11:14:31 +03001261static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301262{
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001263 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001264 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001265 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301266 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301267 return;
1268 }
1269
John Younaf771d72017-01-26 11:58:40 -08001270 /*
1271 * Schedule the first trb for one interval in the future or at
1272 * least 4 microframes.
1273 */
Felipe Balbi5828cad2018-03-27 11:14:31 +03001274 dep->frame_number += max_t(u32, 4, dep->interval);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001275 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301276}
1277
Felipe Balbi72246da2011-08-19 18:10:58 +03001278static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1279{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001280 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001281
Felipe Balbibb423982015-11-16 15:31:21 -06001282 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001283 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1284 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001285 return -ESHUTDOWN;
1286 }
1287
Felipe Balbi04fb3652017-05-17 15:57:45 +03001288 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1289 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001290 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001291
Felipe Balbifc8bb912016-05-16 13:14:48 +03001292 pm_runtime_get(dwc->dev);
1293
Felipe Balbi72246da2011-08-19 18:10:58 +03001294 req->request.actual = 0;
1295 req->request.status = -EINPROGRESS;
1296 req->direction = dep->direction;
1297 req->epnum = dep->number;
1298
Felipe Balbife84f522015-09-01 09:01:38 -05001299 trace_dwc3_ep_queue(req);
1300
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001301 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001302
Felipe Balbid889c232016-09-29 15:44:29 +03001303 /*
1304 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1305 * wait for a XferNotReady event so we will know what's the current
1306 * (micro-)frame number.
1307 *
1308 * Without this trick, we are very, very likely gonna get Bus Expiry
1309 * errors which will force us issue EndTransfer command.
1310 */
1311 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001312 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1313 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001314 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001315
1316 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1317 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1318 __dwc3_gadget_start_isoc(dep);
1319 return 0;
1320 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001321 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001322 }
1323
Felipe Balbi7fdca762017-09-05 14:41:34 +03001324 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001325}
1326
1327static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1328 gfp_t gfp_flags)
1329{
1330 struct dwc3_request *req = to_dwc3_request(request);
1331 struct dwc3_ep *dep = to_dwc3_ep(ep);
1332 struct dwc3 *dwc = dep->dwc;
1333
1334 unsigned long flags;
1335
1336 int ret;
1337
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001338 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 ret = __dwc3_gadget_ep_queue(dep, req);
1340 spin_unlock_irqrestore(&dwc->lock, flags);
1341
1342 return ret;
1343}
1344
1345static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1346 struct usb_request *request)
1347{
1348 struct dwc3_request *req = to_dwc3_request(request);
1349 struct dwc3_request *r = NULL;
1350
1351 struct dwc3_ep *dep = to_dwc3_ep(ep);
1352 struct dwc3 *dwc = dep->dwc;
1353
1354 unsigned long flags;
1355 int ret = 0;
1356
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001357 trace_dwc3_ep_dequeue(req);
1358
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 spin_lock_irqsave(&dwc->lock, flags);
1360
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001361 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001362 if (r == req)
1363 break;
1364 }
1365
1366 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001367 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 if (r == req)
1369 break;
1370 }
1371 if (r == req) {
1372 /* wait until it is processed */
Felipe Balbi8f608e82018-03-27 10:53:29 +03001373 dwc3_stop_active_transfer(dep, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001374
1375 /*
1376 * If request was already started, this means we had to
1377 * stop the transfer. With that we also need to ignore
1378 * all TRBs used by the request, however TRBs can only
1379 * be modified after completion of END_TRANSFER
1380 * command. So what we do here is that we wait for
1381 * END_TRANSFER completion and only after that, we jump
1382 * over TRBs by clearing HWO and incrementing dequeue
1383 * pointer.
1384 *
1385 * Note that we have 2 possible types of transfers here:
1386 *
1387 * i) Linear buffer request
1388 * ii) SG-list based request
1389 *
1390 * SG-list based requests will have r->num_pending_sgs
1391 * set to a valid number (> 0). Linear requests,
1392 * normally use a single TRB.
1393 *
1394 * For each of these two cases, if r->unaligned flag is
1395 * set, one extra TRB has been used to align transfer
1396 * size to wMaxPacketSize.
1397 *
1398 * All of these cases need to be taken into
1399 * consideration so we don't mess up our TRB ring
1400 * pointers.
1401 */
1402 wait_event_lock_irq(dep->wait_end_transfer,
1403 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1404 dwc->lock);
1405
1406 if (!r->trb)
1407 goto out1;
1408
1409 if (r->num_pending_sgs) {
1410 struct dwc3_trb *trb;
1411 int i = 0;
1412
1413 for (i = 0; i < r->num_pending_sgs; i++) {
1414 trb = r->trb + i;
1415 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1416 dwc3_ep_inc_deq(dep);
1417 }
1418
Felipe Balbid6e5a542017-04-07 16:34:38 +03001419 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001420 trb = r->trb + r->num_pending_sgs + 1;
1421 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1422 dwc3_ep_inc_deq(dep);
1423 }
1424 } else {
1425 struct dwc3_trb *trb = r->trb;
1426
1427 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1428 dwc3_ep_inc_deq(dep);
1429
Felipe Balbid6e5a542017-04-07 16:34:38 +03001430 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001431 trb = r->trb + 1;
1432 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1433 dwc3_ep_inc_deq(dep);
1434 }
1435 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301436 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001437 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001438 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001439 request, ep->name);
1440 ret = -EINVAL;
1441 goto out0;
1442 }
1443
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301444out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001445 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001446
Felipe Balbi72246da2011-08-19 18:10:58 +03001447 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1448
1449out0:
1450 spin_unlock_irqrestore(&dwc->lock, flags);
1451
1452 return ret;
1453}
1454
Felipe Balbi7a608552014-09-24 14:19:52 -05001455int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001456{
1457 struct dwc3_gadget_ep_cmd_params params;
1458 struct dwc3 *dwc = dep->dwc;
1459 int ret;
1460
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001461 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1462 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1463 return -EINVAL;
1464 }
1465
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 memset(&params, 0x00, sizeof(params));
1467
1468 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001469 struct dwc3_trb *trb;
1470
1471 unsigned transfer_in_flight;
1472 unsigned started;
1473
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001474 if (dep->flags & DWC3_EP_STALL)
1475 return 0;
1476
Felipe Balbi69450c42016-05-30 13:37:02 +03001477 if (dep->number > 1)
1478 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1479 else
1480 trb = &dwc->ep0_trb[dep->trb_enqueue];
1481
1482 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1483 started = !list_empty(&dep->started_list);
1484
1485 if (!protocol && ((dep->direction && transfer_in_flight) ||
1486 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001487 return -EAGAIN;
1488 }
1489
Felipe Balbi2cd47182016-04-12 16:42:43 +03001490 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1491 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001492 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001493 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 dep->name);
1495 else
1496 dep->flags |= DWC3_EP_STALL;
1497 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001498 if (!(dep->flags & DWC3_EP_STALL))
1499 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001500
John Youn50c763f2016-05-31 17:49:56 -07001501 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001502 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001503 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001504 dep->name);
1505 else
Alan Sterna535d812013-11-01 12:05:12 -04001506 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001507 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001508
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 return ret;
1510}
1511
1512static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1513{
1514 struct dwc3_ep *dep = to_dwc3_ep(ep);
1515 struct dwc3 *dwc = dep->dwc;
1516
1517 unsigned long flags;
1518
1519 int ret;
1520
1521 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001522 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001523 spin_unlock_irqrestore(&dwc->lock, flags);
1524
1525 return ret;
1526}
1527
1528static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1529{
1530 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001531 struct dwc3 *dwc = dep->dwc;
1532 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001533 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001534
Paul Zimmerman249a4562012-02-24 17:32:16 -08001535 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 dep->flags |= DWC3_EP_WEDGE;
1537
Pratyush Anand08f0d962012-06-25 22:40:43 +05301538 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001539 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301540 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001541 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001542 spin_unlock_irqrestore(&dwc->lock, flags);
1543
1544 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001545}
1546
1547/* -------------------------------------------------------------------------- */
1548
1549static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1550 .bLength = USB_DT_ENDPOINT_SIZE,
1551 .bDescriptorType = USB_DT_ENDPOINT,
1552 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1553};
1554
1555static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1556 .enable = dwc3_gadget_ep0_enable,
1557 .disable = dwc3_gadget_ep0_disable,
1558 .alloc_request = dwc3_gadget_ep_alloc_request,
1559 .free_request = dwc3_gadget_ep_free_request,
1560 .queue = dwc3_gadget_ep0_queue,
1561 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301562 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 .set_wedge = dwc3_gadget_ep_set_wedge,
1564};
1565
1566static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1567 .enable = dwc3_gadget_ep_enable,
1568 .disable = dwc3_gadget_ep_disable,
1569 .alloc_request = dwc3_gadget_ep_alloc_request,
1570 .free_request = dwc3_gadget_ep_free_request,
1571 .queue = dwc3_gadget_ep_queue,
1572 .dequeue = dwc3_gadget_ep_dequeue,
1573 .set_halt = dwc3_gadget_ep_set_halt,
1574 .set_wedge = dwc3_gadget_ep_set_wedge,
1575};
1576
1577/* -------------------------------------------------------------------------- */
1578
1579static int dwc3_gadget_get_frame(struct usb_gadget *g)
1580{
1581 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001582
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001583 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001584}
1585
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001586static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001587{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001588 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001589
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001590 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001591 u32 reg;
1592
Felipe Balbi72246da2011-08-19 18:10:58 +03001593 u8 link_state;
1594 u8 speed;
1595
Felipe Balbi72246da2011-08-19 18:10:58 +03001596 /*
1597 * According to the Databook Remote wakeup request should
1598 * be issued only when the device is in early suspend state.
1599 *
1600 * We can check that via USB Link State bits in DSTS register.
1601 */
1602 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1603
1604 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001605 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001606 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001607 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001608
1609 link_state = DWC3_DSTS_USBLNKST(reg);
1610
1611 switch (link_state) {
1612 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1613 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1614 break;
1615 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001616 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001617 }
1618
Felipe Balbi8598bde2012-01-02 18:55:57 +02001619 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1620 if (ret < 0) {
1621 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001622 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001623 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001624
Paul Zimmerman802fde92012-04-27 13:10:52 +03001625 /* Recent versions do this automatically */
1626 if (dwc->revision < DWC3_REVISION_194A) {
1627 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001628 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001629 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1630 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1631 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001632
Paul Zimmerman1d046792012-02-15 18:56:56 -08001633 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001634 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001635
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001636 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1638
1639 /* in HS, means ON */
1640 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1641 break;
1642 }
1643
1644 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1645 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001646 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001647 }
1648
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001649 return 0;
1650}
1651
1652static int dwc3_gadget_wakeup(struct usb_gadget *g)
1653{
1654 struct dwc3 *dwc = gadget_to_dwc(g);
1655 unsigned long flags;
1656 int ret;
1657
1658 spin_lock_irqsave(&dwc->lock, flags);
1659 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 spin_unlock_irqrestore(&dwc->lock, flags);
1661
1662 return ret;
1663}
1664
1665static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1666 int is_selfpowered)
1667{
1668 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001669 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001670
Paul Zimmerman249a4562012-02-24 17:32:16 -08001671 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001672 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001673 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001674
1675 return 0;
1676}
1677
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001678static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001679{
1680 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001681 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001682
Felipe Balbifc8bb912016-05-16 13:14:48 +03001683 if (pm_runtime_suspended(dwc->dev))
1684 return 0;
1685
Felipe Balbi72246da2011-08-19 18:10:58 +03001686 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001687 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001688 if (dwc->revision <= DWC3_REVISION_187A) {
1689 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1690 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1691 }
1692
1693 if (dwc->revision >= DWC3_REVISION_194A)
1694 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1695 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001696
1697 if (dwc->has_hibernation)
1698 reg |= DWC3_DCTL_KEEP_CONNECT;
1699
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001700 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001701 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001703
1704 if (dwc->has_hibernation && !suspend)
1705 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1706
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001707 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001708 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001709
1710 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1711
1712 do {
1713 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001714 reg &= DWC3_DSTS_DEVCTRLHLT;
1715 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001716
1717 if (!timeout)
1718 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001719
Pratyush Anand6f17f742012-07-02 10:21:55 +05301720 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721}
1722
1723static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1724{
1725 struct dwc3 *dwc = gadget_to_dwc(g);
1726 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301727 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001728
1729 is_on = !!is_on;
1730
Baolin Wangbb014732016-10-14 17:11:33 +08001731 /*
1732 * Per databook, when we want to stop the gadget, if a control transfer
1733 * is still in process, complete it and get the core into setup phase.
1734 */
1735 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1736 reinit_completion(&dwc->ep0_in_setup);
1737
1738 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1739 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1740 if (ret == 0) {
1741 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1742 return -ETIMEDOUT;
1743 }
1744 }
1745
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001747 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001748 spin_unlock_irqrestore(&dwc->lock, flags);
1749
Pratyush Anand6f17f742012-07-02 10:21:55 +05301750 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001751}
1752
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001753static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1754{
1755 u32 reg;
1756
1757 /* Enable all but Start and End of Frame IRQs */
1758 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1759 DWC3_DEVTEN_EVNTOVERFLOWEN |
1760 DWC3_DEVTEN_CMDCMPLTEN |
1761 DWC3_DEVTEN_ERRTICERREN |
1762 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001763 DWC3_DEVTEN_CONNECTDONEEN |
1764 DWC3_DEVTEN_USBRSTEN |
1765 DWC3_DEVTEN_DISCONNEVTEN);
1766
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001767 if (dwc->revision < DWC3_REVISION_250A)
1768 reg |= DWC3_DEVTEN_ULSTCNGEN;
1769
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001770 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1771}
1772
1773static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1774{
1775 /* mask all interrupts */
1776 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1777}
1778
1779static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001780static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001781
Felipe Balbi4e994722016-05-13 14:09:59 +03001782/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001783 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1784 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001785 *
1786 * The following looks like complex but it's actually very simple. In order to
1787 * calculate the number of packets we can burst at once on OUT transfers, we're
1788 * gonna use RxFIFO size.
1789 *
1790 * To calculate RxFIFO size we need two numbers:
1791 * MDWIDTH = size, in bits, of the internal memory bus
1792 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1793 *
1794 * Given these two numbers, the formula is simple:
1795 *
1796 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1797 *
1798 * 24 bytes is for 3x SETUP packets
1799 * 16 bytes is a clock domain crossing tolerance
1800 *
1801 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1802 */
1803static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1804{
1805 u32 ram2_depth;
1806 u32 mdwidth;
1807 u32 nump;
1808 u32 reg;
1809
1810 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1811 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1812
1813 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1814 nump = min_t(u32, nump, 16);
1815
1816 /* update NumP */
1817 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1818 reg &= ~DWC3_DCFG_NUMP_MASK;
1819 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1820 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1821}
1822
Felipe Balbid7be2952016-05-04 15:49:37 +03001823static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001824{
Felipe Balbi72246da2011-08-19 18:10:58 +03001825 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 int ret = 0;
1827 u32 reg;
1828
John Youncf40b862016-11-14 12:32:43 -08001829 /*
1830 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1831 * the core supports IMOD, disable it.
1832 */
1833 if (dwc->imod_interval) {
1834 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1835 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1836 } else if (dwc3_has_imod(dwc)) {
1837 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1838 }
1839
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001840 /*
1841 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1842 * field instead of letting dwc3 itself calculate that automatically.
1843 *
1844 * This way, we maximize the chances that we'll be able to get several
1845 * bursts of data without going through any sort of endpoint throttling.
1846 */
1847 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001848 if (dwc3_is_usb31(dwc))
1849 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1850 else
1851 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1852
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001853 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1854
Felipe Balbi4e994722016-05-13 14:09:59 +03001855 dwc3_gadget_setup_nump(dwc);
1856
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 /* Start with SuperSpeed Default */
1858 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1859
1860 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001861 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 if (ret) {
1863 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001864 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001865 }
1866
1867 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001868 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001869 if (ret) {
1870 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001871 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001872 }
1873
1874 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001875 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001876 dwc3_ep0_out_start(dwc);
1877
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001878 dwc3_gadget_enable_irq(dwc);
1879
Felipe Balbid7be2952016-05-04 15:49:37 +03001880 return 0;
1881
1882err1:
1883 __dwc3_gadget_ep_disable(dwc->eps[0]);
1884
1885err0:
1886 return ret;
1887}
1888
1889static int dwc3_gadget_start(struct usb_gadget *g,
1890 struct usb_gadget_driver *driver)
1891{
1892 struct dwc3 *dwc = gadget_to_dwc(g);
1893 unsigned long flags;
1894 int ret = 0;
1895 int irq;
1896
Roger Quadros9522def2016-06-10 14:48:38 +03001897 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001898 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1899 IRQF_SHARED, "dwc3", dwc->ev_buf);
1900 if (ret) {
1901 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1902 irq, ret);
1903 goto err0;
1904 }
1905
1906 spin_lock_irqsave(&dwc->lock, flags);
1907 if (dwc->gadget_driver) {
1908 dev_err(dwc->dev, "%s is already bound to %s\n",
1909 dwc->gadget.name,
1910 dwc->gadget_driver->driver.name);
1911 ret = -EBUSY;
1912 goto err1;
1913 }
1914
1915 dwc->gadget_driver = driver;
1916
Felipe Balbifc8bb912016-05-16 13:14:48 +03001917 if (pm_runtime_active(dwc->dev))
1918 __dwc3_gadget_start(dwc);
1919
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 spin_unlock_irqrestore(&dwc->lock, flags);
1921
1922 return 0;
1923
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001924err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001925 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001926 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001927
1928err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001929 return ret;
1930}
1931
Felipe Balbid7be2952016-05-04 15:49:37 +03001932static void __dwc3_gadget_stop(struct dwc3 *dwc)
1933{
1934 dwc3_gadget_disable_irq(dwc);
1935 __dwc3_gadget_ep_disable(dwc->eps[0]);
1936 __dwc3_gadget_ep_disable(dwc->eps[1]);
1937}
1938
Felipe Balbi22835b82014-10-17 12:05:12 -05001939static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001940{
1941 struct dwc3 *dwc = gadget_to_dwc(g);
1942 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001943 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02001944 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001945
1946 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001947
1948 if (pm_runtime_suspended(dwc->dev))
1949 goto out;
1950
Felipe Balbid7be2952016-05-04 15:49:37 +03001951 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001952
1953 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1954 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02001955 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08001956
1957 if (!dep)
1958 continue;
1959
1960 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1961 continue;
1962
Roger Quadros498f0472018-03-09 14:47:04 +02001963 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
1964 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1965 dwc->lock, msecs_to_jiffies(5));
1966
1967 if (ret <= 0) {
1968 /* Timed out or interrupted! There's nothing much
1969 * we can do so we just log here and print which
1970 * endpoints timed out at the end.
1971 */
1972 tmo_eps |= 1 << epnum;
1973 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
1974 }
1975 }
1976
1977 if (tmo_eps) {
1978 dev_err(dwc->dev,
1979 "end transfer timed out on endpoints 0x%x [bitmap]\n",
1980 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08001981 }
1982
1983out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001984 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 spin_unlock_irqrestore(&dwc->lock, flags);
1986
Felipe Balbi3f308d12016-05-16 14:17:06 +03001987 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001988
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 return 0;
1990}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001991
Felipe Balbi7d8d0632017-06-06 16:05:23 +03001992static void dwc3_gadget_set_speed(struct usb_gadget *g,
1993 enum usb_device_speed speed)
1994{
1995 struct dwc3 *dwc = gadget_to_dwc(g);
1996 unsigned long flags;
1997 u32 reg;
1998
1999 spin_lock_irqsave(&dwc->lock, flags);
2000 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2001 reg &= ~(DWC3_DCFG_SPEED_MASK);
2002
2003 /*
2004 * WORKAROUND: DWC3 revision < 2.20a have an issue
2005 * which would cause metastability state on Run/Stop
2006 * bit if we try to force the IP to USB2-only mode.
2007 *
2008 * Because of that, we cannot configure the IP to any
2009 * speed other than the SuperSpeed
2010 *
2011 * Refers to:
2012 *
2013 * STAR#9000525659: Clock Domain Crossing on DCTL in
2014 * USB 2.0 Mode
2015 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002016 if (dwc->revision < DWC3_REVISION_220A &&
2017 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002018 reg |= DWC3_DCFG_SUPERSPEED;
2019 } else {
2020 switch (speed) {
2021 case USB_SPEED_LOW:
2022 reg |= DWC3_DCFG_LOWSPEED;
2023 break;
2024 case USB_SPEED_FULL:
2025 reg |= DWC3_DCFG_FULLSPEED;
2026 break;
2027 case USB_SPEED_HIGH:
2028 reg |= DWC3_DCFG_HIGHSPEED;
2029 break;
2030 case USB_SPEED_SUPER:
2031 reg |= DWC3_DCFG_SUPERSPEED;
2032 break;
2033 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002034 if (dwc3_is_usb31(dwc))
2035 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2036 else
2037 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002038 break;
2039 default:
2040 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2041
2042 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2043 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2044 else
2045 reg |= DWC3_DCFG_SUPERSPEED;
2046 }
2047 }
2048 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2049
2050 spin_unlock_irqrestore(&dwc->lock, flags);
2051}
2052
Felipe Balbi72246da2011-08-19 18:10:58 +03002053static const struct usb_gadget_ops dwc3_gadget_ops = {
2054 .get_frame = dwc3_gadget_get_frame,
2055 .wakeup = dwc3_gadget_wakeup,
2056 .set_selfpowered = dwc3_gadget_set_selfpowered,
2057 .pullup = dwc3_gadget_pullup,
2058 .udc_start = dwc3_gadget_start,
2059 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002060 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002061};
2062
2063/* -------------------------------------------------------------------------- */
2064
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002065static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2066{
2067 struct dwc3 *dwc = dep->dwc;
2068
2069 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2070 dep->endpoint.maxburst = 1;
2071 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2072 if (!dep->direction)
2073 dwc->gadget.ep0 = &dep->endpoint;
2074
2075 dep->endpoint.caps.type_control = true;
2076
2077 return 0;
2078}
2079
2080static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2081{
2082 struct dwc3 *dwc = dep->dwc;
2083 int mdwidth;
2084 int kbytes;
2085 int size;
2086
2087 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2088 /* MDWIDTH is represented in bits, we need it in bytes */
2089 mdwidth /= 8;
2090
2091 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2092 if (dwc3_is_usb31(dwc))
2093 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2094 else
2095 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2096
2097 /* FIFO Depth is in MDWDITH bytes. Multiply */
2098 size *= mdwidth;
2099
2100 kbytes = size / 1024;
2101 if (kbytes == 0)
2102 kbytes = 1;
2103
2104 /*
2105 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2106 * internal overhead. We don't really know how these are used,
2107 * but documentation say it exists.
2108 */
2109 size -= mdwidth * (kbytes + 1);
2110 size /= kbytes;
2111
2112 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2113
2114 dep->endpoint.max_streams = 15;
2115 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2116 list_add_tail(&dep->endpoint.ep_list,
2117 &dwc->gadget.ep_list);
2118 dep->endpoint.caps.type_iso = true;
2119 dep->endpoint.caps.type_bulk = true;
2120 dep->endpoint.caps.type_int = true;
2121
2122 return dwc3_alloc_trb_pool(dep);
2123}
2124
2125static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2126{
2127 struct dwc3 *dwc = dep->dwc;
2128
2129 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2130 dep->endpoint.max_streams = 15;
2131 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2132 list_add_tail(&dep->endpoint.ep_list,
2133 &dwc->gadget.ep_list);
2134 dep->endpoint.caps.type_iso = true;
2135 dep->endpoint.caps.type_bulk = true;
2136 dep->endpoint.caps.type_int = true;
2137
2138 return dwc3_alloc_trb_pool(dep);
2139}
2140
2141static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002142{
2143 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002144 bool direction = epnum & 1;
2145 int ret;
2146 u8 num = epnum >> 1;
2147
2148 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2149 if (!dep)
2150 return -ENOMEM;
2151
2152 dep->dwc = dwc;
2153 dep->number = epnum;
2154 dep->direction = direction;
2155 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2156 dwc->eps[epnum] = dep;
2157
2158 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2159 direction ? "in" : "out");
2160
2161 dep->endpoint.name = dep->name;
2162
2163 if (!(dep->number > 1)) {
2164 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2165 dep->endpoint.comp_desc = NULL;
2166 }
2167
2168 spin_lock_init(&dep->lock);
2169
2170 if (num == 0)
2171 ret = dwc3_gadget_init_control_endpoint(dep);
2172 else if (direction)
2173 ret = dwc3_gadget_init_in_endpoint(dep);
2174 else
2175 ret = dwc3_gadget_init_out_endpoint(dep);
2176
2177 if (ret)
2178 return ret;
2179
2180 dep->endpoint.caps.dir_in = direction;
2181 dep->endpoint.caps.dir_out = !direction;
2182
2183 INIT_LIST_HEAD(&dep->pending_list);
2184 INIT_LIST_HEAD(&dep->started_list);
2185
2186 return 0;
2187}
2188
2189static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2190{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002191 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002192
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002193 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2194
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002195 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002196 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002197
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002198 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2199 if (ret)
2200 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002201 }
2202
2203 return 0;
2204}
2205
2206static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2207{
2208 struct dwc3_ep *dep;
2209 u8 epnum;
2210
2211 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2212 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002213 if (!dep)
2214 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302215 /*
2216 * Physical endpoints 0 and 1 are special; they form the
2217 * bi-directional USB endpoint 0.
2218 *
2219 * For those two physical endpoints, we don't allocate a TRB
2220 * pool nor do we add them the endpoints list. Due to that, we
2221 * shouldn't do these two operations otherwise we would end up
2222 * with all sorts of bugs when removing dwc3.ko.
2223 */
2224 if (epnum != 0 && epnum != 1) {
2225 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002226 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302227 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002228
2229 kfree(dep);
2230 }
2231}
2232
Felipe Balbi72246da2011-08-19 18:10:58 +03002233/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002234
Felipe Balbi8f608e82018-03-27 10:53:29 +03002235static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2236 struct dwc3_request *req, struct dwc3_trb *trb,
2237 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302238{
2239 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302240
Felipe Balbidc55c672016-08-12 13:20:32 +03002241 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002242
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002243 trace_dwc3_complete_trb(dep, trb);
2244
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002245 /*
2246 * If we're in the middle of series of chained TRBs and we
2247 * receive a short transfer along the way, DWC3 will skip
2248 * through all TRBs including the last TRB in the chain (the
2249 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2250 * bit and SW has to do it manually.
2251 *
2252 * We're going to do that here to avoid problems of HW trying
2253 * to use bogus TRBs for transfers.
2254 */
2255 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2256 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2257
Felipe Balbic6267a52017-01-05 14:58:46 +02002258 /*
2259 * If we're dealing with unaligned size OUT transfer, we will be left
2260 * with one TRB pending in the ring. We need to manually clear HWO bit
2261 * from that TRB.
2262 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002263 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002264 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2265 return 1;
2266 }
2267
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302268 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002269 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302270
Felipe Balbi35b27192017-03-08 13:56:37 +02002271 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2272 return 1;
2273
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002274 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302275 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002276
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002277 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302278 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002279
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302280 return 0;
2281}
2282
Felipe Balbid3692952018-03-29 13:32:10 +03002283static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2284 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2285 int status)
2286{
2287 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2288 struct scatterlist *sg = req->sg;
2289 struct scatterlist *s;
2290 unsigned int pending = req->num_pending_sgs;
2291 unsigned int i;
2292 int ret = 0;
2293
2294 for_each_sg(sg, s, pending, i) {
2295 trb = &dep->trb_pool[dep->trb_dequeue];
2296
2297 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2298 break;
2299
2300 req->sg = sg_next(s);
2301 req->num_pending_sgs--;
2302
2303 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2304 trb, event, status, true);
2305 if (ret)
2306 break;
2307 }
2308
2309 return ret;
2310}
2311
2312static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2313 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2314 int status)
2315{
2316 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2317
2318 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2319 event, status, false);
2320}
2321
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002322static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2323{
2324 return req->request.actual == req->request.length;
2325}
2326
Felipe Balbif38e35d2018-04-06 15:56:35 +03002327static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2328 const struct dwc3_event_depevt *event,
2329 struct dwc3_request *req, int status)
2330{
2331 int ret;
2332
2333 if (req->num_pending_sgs)
2334 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2335 status);
2336 else
2337 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2338 status);
2339
2340 if (req->unaligned || req->zero) {
2341 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2342 status);
2343 req->unaligned = false;
2344 req->zero = false;
2345 }
2346
2347 req->request.actual = req->request.length - req->remaining;
2348
2349 if (!dwc3_gadget_ep_request_completed(req) &&
2350 req->num_pending_sgs) {
2351 __dwc3_gadget_kick_transfer(dep);
2352 goto out;
2353 }
2354
2355 dwc3_gadget_giveback(dep, req, status);
2356
2357out:
2358 return ret;
2359}
2360
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002361static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002362 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002363{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002364 struct dwc3_request *req;
2365 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002366
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002367 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002368 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002369
Felipe Balbif38e35d2018-04-06 15:56:35 +03002370 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2371 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002372 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002373 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002374 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002375}
2376
Felipe Balbiee3638b2018-03-27 11:26:53 +03002377static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2378 const struct dwc3_event_depevt *event)
2379{
2380 u32 cur_uf, mask;
2381
2382 mask = ~(dep->interval - 1);
2383 cur_uf = event->parameters & mask;
2384 dep->frame_number = cur_uf;
2385}
2386
Felipe Balbi8f608e82018-03-27 10:53:29 +03002387static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2388 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002389{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002390 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002391 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002392 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002393
Felipe Balbiee3638b2018-03-27 11:26:53 +03002394 dwc3_gadget_endpoint_frame_from_event(dep, event);
2395
Felipe Balbi72246da2011-08-19 18:10:58 +03002396 if (event->status & DEPEVT_STATUS_BUSERR)
2397 status = -ECONNRESET;
2398
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002399 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2400 status = -EXDEV;
Felipe Balbid5133202018-04-11 10:32:52 +03002401
2402 if (list_empty(&dep->started_list))
2403 stop = true;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002404 }
2405
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002406 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002407
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002408 if (stop) {
2409 dwc3_stop_active_transfer(dep, true);
2410 dep->flags = DWC3_EP_ENABLED;
2411 }
2412
Felipe Balbifae2b902011-10-14 13:00:30 +03002413 /*
2414 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2415 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2416 */
2417 if (dwc->revision < DWC3_REVISION_183A) {
2418 u32 reg;
2419 int i;
2420
2421 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002422 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002423
2424 if (!(dep->flags & DWC3_EP_ENABLED))
2425 continue;
2426
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002427 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002428 return;
2429 }
2430
2431 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2432 reg |= dwc->u1u2;
2433 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2434
2435 dwc->u1u2 = 0;
2436 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002437}
2438
Felipe Balbi8f608e82018-03-27 10:53:29 +03002439static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2440 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002441{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002442 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002443 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002444}
2445
Felipe Balbi72246da2011-08-19 18:10:58 +03002446static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2447 const struct dwc3_event_depevt *event)
2448{
2449 struct dwc3_ep *dep;
2450 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002451 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002452
2453 dep = dwc->eps[epnum];
2454
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002455 if (!(dep->flags & DWC3_EP_ENABLED)) {
2456 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2457 return;
2458
2459 /* Handle only EPCMDCMPLT when EP disabled */
2460 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2461 return;
2462 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002463
Felipe Balbi72246da2011-08-19 18:10:58 +03002464 if (epnum == 0 || epnum == 1) {
2465 dwc3_ep0_interrupt(dwc, event);
2466 return;
2467 }
2468
2469 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002470 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002471 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002472 break;
2473 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002474 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002475 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002476 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002477 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2478
2479 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2480 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2481 wake_up(&dep->wait_end_transfer);
2482 }
2483 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002484 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002485 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002486 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 break;
2488 }
2489}
2490
2491static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2492{
2493 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2494 spin_unlock(&dwc->lock);
2495 dwc->gadget_driver->disconnect(&dwc->gadget);
2496 spin_lock(&dwc->lock);
2497 }
2498}
2499
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002500static void dwc3_suspend_gadget(struct dwc3 *dwc)
2501{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002502 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002503 spin_unlock(&dwc->lock);
2504 dwc->gadget_driver->suspend(&dwc->gadget);
2505 spin_lock(&dwc->lock);
2506 }
2507}
2508
2509static void dwc3_resume_gadget(struct dwc3 *dwc)
2510{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002511 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002512 spin_unlock(&dwc->lock);
2513 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002514 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002515 }
2516}
2517
2518static void dwc3_reset_gadget(struct dwc3 *dwc)
2519{
2520 if (!dwc->gadget_driver)
2521 return;
2522
2523 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2524 spin_unlock(&dwc->lock);
2525 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002526 spin_lock(&dwc->lock);
2527 }
2528}
2529
Felipe Balbi8f608e82018-03-27 10:53:29 +03002530static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002531{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002532 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002533 struct dwc3_gadget_ep_cmd_params params;
2534 u32 cmd;
2535 int ret;
2536
Baolin Wang76a638f2016-10-31 19:38:36 +08002537 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2538 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302539 return;
2540
Pratyush Anand57911502012-07-06 15:19:10 +05302541 /*
2542 * NOTICE: We are violating what the Databook says about the
2543 * EndTransfer command. Ideally we would _always_ wait for the
2544 * EndTransfer Command Completion IRQ, but that's causing too
2545 * much trouble synchronizing between us and gadget driver.
2546 *
2547 * We have discussed this with the IP Provider and it was
2548 * suggested to giveback all requests here, but give HW some
2549 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002550 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302551 *
2552 * Note also that a similar handling was tested by Synopsys
2553 * (thanks a lot Paul) and nothing bad has come out of it.
2554 * In short, what we're doing is:
2555 *
2556 * - Issue EndTransfer WITH CMDIOC bit set
2557 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002558 *
2559 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2560 * supports a mode to work around the above limitation. The
2561 * software can poll the CMDACT bit in the DEPCMD register
2562 * after issuing a EndTransfer command. This mode is enabled
2563 * by writing GUCTL2[14]. This polling is already done in the
2564 * dwc3_send_gadget_ep_cmd() function so if the mode is
2565 * enabled, the EndTransfer command will have completed upon
2566 * returning from this function and we don't need to delay for
2567 * 100us.
2568 *
2569 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302570 */
2571
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302572 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002573 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2574 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002575 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302576 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002577 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302578 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002579 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002580
Baolin Wang76a638f2016-10-31 19:38:36 +08002581 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2582 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002583 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002584 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002585}
2586
Felipe Balbi72246da2011-08-19 18:10:58 +03002587static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2588{
2589 u32 epnum;
2590
2591 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2592 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002593 int ret;
2594
2595 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002596 if (!dep)
2597 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002598
2599 if (!(dep->flags & DWC3_EP_STALL))
2600 continue;
2601
2602 dep->flags &= ~DWC3_EP_STALL;
2603
John Youn50c763f2016-05-31 17:49:56 -07002604 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002605 WARN_ON_ONCE(ret);
2606 }
2607}
2608
2609static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2610{
Felipe Balbic4430a22012-05-24 10:30:01 +03002611 int reg;
2612
Felipe Balbi72246da2011-08-19 18:10:58 +03002613 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2614 reg &= ~DWC3_DCTL_INITU1ENA;
2615 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2616
2617 reg &= ~DWC3_DCTL_INITU2ENA;
2618 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002619
Felipe Balbi72246da2011-08-19 18:10:58 +03002620 dwc3_disconnect_gadget(dwc);
2621
2622 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002623 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002624 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002625
2626 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002627}
2628
Felipe Balbi72246da2011-08-19 18:10:58 +03002629static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2630{
2631 u32 reg;
2632
Felipe Balbifc8bb912016-05-16 13:14:48 +03002633 dwc->connected = true;
2634
Felipe Balbidf62df52011-10-14 15:11:49 +03002635 /*
2636 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2637 * would cause a missing Disconnect Event if there's a
2638 * pending Setup Packet in the FIFO.
2639 *
2640 * There's no suggested workaround on the official Bug
2641 * report, which states that "unless the driver/application
2642 * is doing any special handling of a disconnect event,
2643 * there is no functional issue".
2644 *
2645 * Unfortunately, it turns out that we _do_ some special
2646 * handling of a disconnect event, namely complete all
2647 * pending transfers, notify gadget driver of the
2648 * disconnection, and so on.
2649 *
2650 * Our suggested workaround is to follow the Disconnect
2651 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002652 * flag. Such flag gets set whenever we have a SETUP_PENDING
2653 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002654 * same endpoint.
2655 *
2656 * Refers to:
2657 *
2658 * STAR#9000466709: RTL: Device : Disconnect event not
2659 * generated if setup packet pending in FIFO
2660 */
2661 if (dwc->revision < DWC3_REVISION_188A) {
2662 if (dwc->setup_packet_pending)
2663 dwc3_gadget_disconnect_interrupt(dwc);
2664 }
2665
Felipe Balbi8e744752014-11-06 14:27:53 +08002666 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002667
2668 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2669 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2670 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002671 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002672 dwc3_clear_stall_all_ep(dwc);
2673
2674 /* Reset device address to zero */
2675 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2676 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2677 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002678}
2679
Felipe Balbi72246da2011-08-19 18:10:58 +03002680static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2681{
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 struct dwc3_ep *dep;
2683 int ret;
2684 u32 reg;
2685 u8 speed;
2686
Felipe Balbi72246da2011-08-19 18:10:58 +03002687 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2688 speed = reg & DWC3_DSTS_CONNECTSPD;
2689 dwc->speed = speed;
2690
John Youn5fb6fda2016-11-10 17:23:25 -08002691 /*
2692 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2693 * each time on Connect Done.
2694 *
2695 * Currently we always use the reset value. If any platform
2696 * wants to set this to a different value, we need to add a
2697 * setting and update GCTL.RAMCLKSEL here.
2698 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002699
2700 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002701 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002702 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2703 dwc->gadget.ep0->maxpacket = 512;
2704 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2705 break;
John Youn2da9ad72016-05-20 16:34:26 -07002706 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002707 /*
2708 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2709 * would cause a missing USB3 Reset event.
2710 *
2711 * In such situations, we should force a USB3 Reset
2712 * event by calling our dwc3_gadget_reset_interrupt()
2713 * routine.
2714 *
2715 * Refers to:
2716 *
2717 * STAR#9000483510: RTL: SS : USB3 reset event may
2718 * not be generated always when the link enters poll
2719 */
2720 if (dwc->revision < DWC3_REVISION_190A)
2721 dwc3_gadget_reset_interrupt(dwc);
2722
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2724 dwc->gadget.ep0->maxpacket = 512;
2725 dwc->gadget.speed = USB_SPEED_SUPER;
2726 break;
John Youn2da9ad72016-05-20 16:34:26 -07002727 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002728 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2729 dwc->gadget.ep0->maxpacket = 64;
2730 dwc->gadget.speed = USB_SPEED_HIGH;
2731 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002732 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002733 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2734 dwc->gadget.ep0->maxpacket = 64;
2735 dwc->gadget.speed = USB_SPEED_FULL;
2736 break;
John Youn2da9ad72016-05-20 16:34:26 -07002737 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002738 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2739 dwc->gadget.ep0->maxpacket = 8;
2740 dwc->gadget.speed = USB_SPEED_LOW;
2741 break;
2742 }
2743
Thinh Nguyen61800262018-01-12 18:18:05 -08002744 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2745
Pratyush Anand2b758352013-01-14 15:59:31 +05302746 /* Enable USB2 LPM Capability */
2747
John Younee5cd412016-02-05 17:08:45 -08002748 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002749 (speed != DWC3_DSTS_SUPERSPEED) &&
2750 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302751 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2752 reg |= DWC3_DCFG_LPM_CAP;
2753 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2754
2755 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2756 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2757
Huang Rui460d0982014-10-31 11:11:18 +08002758 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302759
Huang Rui80caf7d2014-10-28 19:54:26 +08002760 /*
2761 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2762 * DCFG.LPMCap is set, core responses with an ACK and the
2763 * BESL value in the LPM token is less than or equal to LPM
2764 * NYET threshold.
2765 */
2766 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2767 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002768 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002769
2770 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2771 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2772
Pratyush Anand2b758352013-01-14 15:59:31 +05302773 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002774 } else {
2775 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2776 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2777 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302778 }
2779
Felipe Balbi72246da2011-08-19 18:10:58 +03002780 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002781 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 if (ret) {
2783 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2784 return;
2785 }
2786
2787 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002788 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002789 if (ret) {
2790 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2791 return;
2792 }
2793
2794 /*
2795 * Configure PHY via GUSB3PIPECTLn if required.
2796 *
2797 * Update GTXFIFOSIZn
2798 *
2799 * In both cases reset values should be sufficient.
2800 */
2801}
2802
2803static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2804{
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 /*
2806 * TODO take core out of low power mode when that's
2807 * implemented.
2808 */
2809
Jiebing Liad14d4e2014-12-11 13:26:29 +08002810 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2811 spin_unlock(&dwc->lock);
2812 dwc->gadget_driver->resume(&dwc->gadget);
2813 spin_lock(&dwc->lock);
2814 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002815}
2816
2817static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2818 unsigned int evtinfo)
2819{
Felipe Balbifae2b902011-10-14 13:00:30 +03002820 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002821 unsigned int pwropt;
2822
2823 /*
2824 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2825 * Hibernation mode enabled which would show up when device detects
2826 * host-initiated U3 exit.
2827 *
2828 * In that case, device will generate a Link State Change Interrupt
2829 * from U3 to RESUME which is only necessary if Hibernation is
2830 * configured in.
2831 *
2832 * There are no functional changes due to such spurious event and we
2833 * just need to ignore it.
2834 *
2835 * Refers to:
2836 *
2837 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2838 * operational mode
2839 */
2840 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2841 if ((dwc->revision < DWC3_REVISION_250A) &&
2842 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2843 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2844 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002845 return;
2846 }
2847 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002848
2849 /*
2850 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2851 * on the link partner, the USB session might do multiple entry/exit
2852 * of low power states before a transfer takes place.
2853 *
2854 * Due to this problem, we might experience lower throughput. The
2855 * suggested workaround is to disable DCTL[12:9] bits if we're
2856 * transitioning from U1/U2 to U0 and enable those bits again
2857 * after a transfer completes and there are no pending transfers
2858 * on any of the enabled endpoints.
2859 *
2860 * This is the first half of that workaround.
2861 *
2862 * Refers to:
2863 *
2864 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2865 * core send LGO_Ux entering U0
2866 */
2867 if (dwc->revision < DWC3_REVISION_183A) {
2868 if (next == DWC3_LINK_STATE_U0) {
2869 u32 u1u2;
2870 u32 reg;
2871
2872 switch (dwc->link_state) {
2873 case DWC3_LINK_STATE_U1:
2874 case DWC3_LINK_STATE_U2:
2875 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2876 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2877 | DWC3_DCTL_ACCEPTU2ENA
2878 | DWC3_DCTL_INITU1ENA
2879 | DWC3_DCTL_ACCEPTU1ENA);
2880
2881 if (!dwc->u1u2)
2882 dwc->u1u2 = reg & u1u2;
2883
2884 reg &= ~u1u2;
2885
2886 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2887 break;
2888 default:
2889 /* do nothing */
2890 break;
2891 }
2892 }
2893 }
2894
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002895 switch (next) {
2896 case DWC3_LINK_STATE_U1:
2897 if (dwc->speed == USB_SPEED_SUPER)
2898 dwc3_suspend_gadget(dwc);
2899 break;
2900 case DWC3_LINK_STATE_U2:
2901 case DWC3_LINK_STATE_U3:
2902 dwc3_suspend_gadget(dwc);
2903 break;
2904 case DWC3_LINK_STATE_RESUME:
2905 dwc3_resume_gadget(dwc);
2906 break;
2907 default:
2908 /* do nothing */
2909 break;
2910 }
2911
Felipe Balbie57ebc12014-04-22 13:20:12 -05002912 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002913}
2914
Baolin Wang72704f82016-05-16 16:43:53 +08002915static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2916 unsigned int evtinfo)
2917{
2918 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2919
2920 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2921 dwc3_suspend_gadget(dwc);
2922
2923 dwc->link_state = next;
2924}
2925
Felipe Balbie1dadd32014-02-25 14:47:54 -06002926static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2927 unsigned int evtinfo)
2928{
2929 unsigned int is_ss = evtinfo & BIT(4);
2930
Felipe Balbibfad65e2017-04-19 14:59:27 +03002931 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002932 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2933 * have a known issue which can cause USB CV TD.9.23 to fail
2934 * randomly.
2935 *
2936 * Because of this issue, core could generate bogus hibernation
2937 * events which SW needs to ignore.
2938 *
2939 * Refers to:
2940 *
2941 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2942 * Device Fallback from SuperSpeed
2943 */
2944 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2945 return;
2946
2947 /* enter hibernation here */
2948}
2949
Felipe Balbi72246da2011-08-19 18:10:58 +03002950static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2951 const struct dwc3_event_devt *event)
2952{
2953 switch (event->type) {
2954 case DWC3_DEVICE_EVENT_DISCONNECT:
2955 dwc3_gadget_disconnect_interrupt(dwc);
2956 break;
2957 case DWC3_DEVICE_EVENT_RESET:
2958 dwc3_gadget_reset_interrupt(dwc);
2959 break;
2960 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2961 dwc3_gadget_conndone_interrupt(dwc);
2962 break;
2963 case DWC3_DEVICE_EVENT_WAKEUP:
2964 dwc3_gadget_wakeup_interrupt(dwc);
2965 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002966 case DWC3_DEVICE_EVENT_HIBER_REQ:
2967 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2968 "unexpected hibernation event\n"))
2969 break;
2970
2971 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2972 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002973 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2974 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2975 break;
2976 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002977 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002978 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002979 /*
2980 * Ignore suspend event until the gadget enters into
2981 * USB_STATE_CONFIGURED state.
2982 */
2983 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2984 dwc3_gadget_suspend_interrupt(dwc,
2985 event->event_info);
2986 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002987 break;
2988 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002989 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002990 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002991 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002992 break;
2993 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002994 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002995 }
2996}
2997
2998static void dwc3_process_event_entry(struct dwc3 *dwc,
2999 const union dwc3_event *event)
3000{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003001 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003002
Felipe Balbidfc5e802017-04-26 13:44:51 +03003003 if (!event->type.is_devspec)
3004 dwc3_endpoint_interrupt(dwc, &event->depevt);
3005 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003007 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003008 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003009}
3010
Felipe Balbidea520a2016-03-30 09:39:34 +03003011static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003012{
Felipe Balbidea520a2016-03-30 09:39:34 +03003013 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003014 irqreturn_t ret = IRQ_NONE;
3015 int left;
3016 u32 reg;
3017
Felipe Balbif42f2442013-06-12 21:25:08 +03003018 left = evt->count;
3019
3020 if (!(evt->flags & DWC3_EVENT_PENDING))
3021 return IRQ_NONE;
3022
3023 while (left > 0) {
3024 union dwc3_event event;
3025
John Younebbb2d52016-11-15 13:07:02 +02003026 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003027
3028 dwc3_process_event_entry(dwc, &event);
3029
3030 /*
3031 * FIXME we wrap around correctly to the next entry as
3032 * almost all entries are 4 bytes in size. There is one
3033 * entry which has 12 bytes which is a regular entry
3034 * followed by 8 bytes data. ATM I don't know how
3035 * things are organized if we get next to the a
3036 * boundary so I worry about that once we try to handle
3037 * that.
3038 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003039 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003040 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003041 }
3042
3043 evt->count = 0;
3044 evt->flags &= ~DWC3_EVENT_PENDING;
3045 ret = IRQ_HANDLED;
3046
3047 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003048 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003049 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003050 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003051
John Youncf40b862016-11-14 12:32:43 -08003052 if (dwc->imod_interval) {
3053 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3054 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3055 }
3056
Felipe Balbif42f2442013-06-12 21:25:08 +03003057 return ret;
3058}
3059
Felipe Balbidea520a2016-03-30 09:39:34 +03003060static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003061{
Felipe Balbidea520a2016-03-30 09:39:34 +03003062 struct dwc3_event_buffer *evt = _evt;
3063 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003064 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003065 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003066
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003067 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003068 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003069 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003070
3071 return ret;
3072}
3073
Felipe Balbidea520a2016-03-30 09:39:34 +03003074static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003075{
Felipe Balbidea520a2016-03-30 09:39:34 +03003076 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003077 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003078 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003079 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003080
Felipe Balbifc8bb912016-05-16 13:14:48 +03003081 if (pm_runtime_suspended(dwc->dev)) {
3082 pm_runtime_get(dwc->dev);
3083 disable_irq_nosync(dwc->irq_gadget);
3084 dwc->pending_events = true;
3085 return IRQ_HANDLED;
3086 }
3087
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003088 /*
3089 * With PCIe legacy interrupt, test shows that top-half irq handler can
3090 * be called again after HW interrupt deassertion. Check if bottom-half
3091 * irq event handler completes before caching new event to prevent
3092 * losing events.
3093 */
3094 if (evt->flags & DWC3_EVENT_PENDING)
3095 return IRQ_HANDLED;
3096
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003097 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003098 count &= DWC3_GEVNTCOUNT_MASK;
3099 if (!count)
3100 return IRQ_NONE;
3101
Felipe Balbib15a7622011-06-30 16:57:15 +03003102 evt->count = count;
3103 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003104
Felipe Balbie8adfc32013-06-12 21:11:14 +03003105 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003106 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003107 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003108 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003109
John Younebbb2d52016-11-15 13:07:02 +02003110 amount = min(count, evt->length - evt->lpos);
3111 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3112
3113 if (amount < count)
3114 memcpy(evt->cache, evt->buf, count - amount);
3115
John Youn65aca322016-11-15 13:08:59 +02003116 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3117
Felipe Balbib15a7622011-06-30 16:57:15 +03003118 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003119}
3120
Felipe Balbidea520a2016-03-30 09:39:34 +03003121static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003122{
Felipe Balbidea520a2016-03-30 09:39:34 +03003123 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003124
Felipe Balbidea520a2016-03-30 09:39:34 +03003125 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003126}
3127
Felipe Balbi6db38122016-10-03 11:27:01 +03003128static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3129{
3130 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3131 int irq;
3132
3133 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3134 if (irq > 0)
3135 goto out;
3136
3137 if (irq == -EPROBE_DEFER)
3138 goto out;
3139
3140 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3141 if (irq > 0)
3142 goto out;
3143
3144 if (irq == -EPROBE_DEFER)
3145 goto out;
3146
3147 irq = platform_get_irq(dwc3_pdev, 0);
3148 if (irq > 0)
3149 goto out;
3150
3151 if (irq != -EPROBE_DEFER)
3152 dev_err(dwc->dev, "missing peripheral IRQ\n");
3153
3154 if (!irq)
3155 irq = -EINVAL;
3156
3157out:
3158 return irq;
3159}
3160
Felipe Balbi72246da2011-08-19 18:10:58 +03003161/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003162 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003163 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003164 *
3165 * Returns 0 on success otherwise negative errno.
3166 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003167int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003168{
Felipe Balbi6db38122016-10-03 11:27:01 +03003169 int ret;
3170 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003171
Felipe Balbi6db38122016-10-03 11:27:01 +03003172 irq = dwc3_gadget_get_irq(dwc);
3173 if (irq < 0) {
3174 ret = irq;
3175 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003176 }
3177
3178 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003179
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303180 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3181 sizeof(*dwc->ep0_trb) * 2,
3182 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003183 if (!dwc->ep0_trb) {
3184 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3185 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003186 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003187 }
3188
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003189 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003190 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003191 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003192 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003193 }
3194
Felipe Balbi905dc042017-01-05 14:46:52 +02003195 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3196 &dwc->bounce_addr, GFP_KERNEL);
3197 if (!dwc->bounce) {
3198 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003199 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003200 }
3201
Baolin Wangbb014732016-10-14 17:11:33 +08003202 init_completion(&dwc->ep0_in_setup);
3203
Felipe Balbi72246da2011-08-19 18:10:58 +03003204 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003205 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003206 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003208 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003209
3210 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003211 * FIXME We might be setting max_speed to <SUPER, however versions
3212 * <2.20a of dwc3 have an issue with metastability (documented
3213 * elsewhere in this driver) which tells us we can't set max speed to
3214 * anything lower than SUPER.
3215 *
3216 * Because gadget.max_speed is only used by composite.c and function
3217 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3218 * to happen so we avoid sending SuperSpeed Capability descriptor
3219 * together with our BOS descriptor as that could confuse host into
3220 * thinking we can handle super speed.
3221 *
3222 * Note that, in fact, we won't even support GetBOS requests when speed
3223 * is less than super speed because we don't have means, yet, to tell
3224 * composite.c that we are USB 2.0 + LPM ECN.
3225 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003226 if (dwc->revision < DWC3_REVISION_220A &&
3227 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003228 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003229 dwc->revision);
3230
3231 dwc->gadget.max_speed = dwc->maximum_speed;
3232
3233 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003234 * REVISIT: Here we should clear all pending IRQs to be
3235 * sure we're starting from a well known location.
3236 */
3237
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003238 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003239 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003240 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003241
Felipe Balbi72246da2011-08-19 18:10:58 +03003242 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3243 if (ret) {
3244 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003245 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003246 }
3247
3248 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003249
3250err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003251 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003252
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003253err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003254 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3255 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003256
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003257err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003258 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003259
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003260err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303261 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003262 dwc->ep0_trb, dwc->ep0_trb_addr);
3263
Felipe Balbi72246da2011-08-19 18:10:58 +03003264err0:
3265 return ret;
3266}
3267
Felipe Balbi7415f172012-04-30 14:56:33 +03003268/* -------------------------------------------------------------------------- */
3269
Felipe Balbi72246da2011-08-19 18:10:58 +03003270void dwc3_gadget_exit(struct dwc3 *dwc)
3271{
Felipe Balbi72246da2011-08-19 18:10:58 +03003272 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003273 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003274 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003275 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003276 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303277 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003278 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003279}
Felipe Balbi7415f172012-04-30 14:56:33 +03003280
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003281int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003282{
Roger Quadros9772b472016-04-12 11:33:29 +03003283 if (!dwc->gadget_driver)
3284 return 0;
3285
Roger Quadros1551e352017-02-15 14:16:26 +02003286 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003287 dwc3_disconnect_gadget(dwc);
3288 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003289
3290 return 0;
3291}
3292
3293int dwc3_gadget_resume(struct dwc3 *dwc)
3294{
Felipe Balbi7415f172012-04-30 14:56:33 +03003295 int ret;
3296
Roger Quadros9772b472016-04-12 11:33:29 +03003297 if (!dwc->gadget_driver)
3298 return 0;
3299
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003300 ret = __dwc3_gadget_start(dwc);
3301 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003302 goto err0;
3303
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003304 ret = dwc3_gadget_run_stop(dwc, true, false);
3305 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003306 goto err1;
3307
Felipe Balbi7415f172012-04-30 14:56:33 +03003308 return 0;
3309
3310err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003311 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003312
3313err0:
3314 return ret;
3315}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003316
3317void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3318{
3319 if (dwc->pending_events) {
3320 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3321 dwc->pending_events = false;
3322 enable_irq(dwc->irq_gadget);
3323 }
3324}