blob: 7fd4b22f4e977c582cbd36654810fef12892c12e [file] [log] [blame]
Felipe Balbibfad65e2017-04-19 14:59:27 +03001/*
Felipe Balbi72246da2011-08-19 18:10:58 +03002 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030039 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020040 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030043 * Caller should take care of locking. This function will return 0 on
44 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020045 */
46int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
47{
48 u32 reg;
49
50 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
51 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
52
53 switch (mode) {
54 case TEST_J:
55 case TEST_K:
56 case TEST_SE0_NAK:
57 case TEST_PACKET:
58 case TEST_FORCE_EN:
59 reg |= mode << 1;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66
67 return 0;
68}
69
Felipe Balbi8598bde2012-01-02 18:55:57 +020070/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030071 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * @dwc: pointer to our context structure
73 *
74 * Caller should take care of locking. This function will
75 * return the link state on success (>= 0) or -ETIMEDOUT.
76 */
77int dwc3_gadget_get_link_state(struct dwc3 *dwc)
78{
79 u32 reg;
80
81 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
82
83 return DWC3_DSTS_USBLNKST(reg);
84}
85
86/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030087 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * @dwc: pointer to our context structure
89 * @state: the state to put link into
90 *
91 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080092 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020093 */
94int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
95{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 u32 reg;
98
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 /*
100 * Wait until device controller is ready. Only applies to 1.94a and
101 * later RTL.
102 */
103 if (dwc->revision >= DWC3_REVISION_194A) {
104 while (--retries) {
105 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
106 if (reg & DWC3_DSTS_DCNRD)
107 udelay(5);
108 else
109 break;
110 }
111
112 if (retries <= 0)
113 return -ETIMEDOUT;
114 }
115
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
117 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
118
119 /* set requested state */
120 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
121 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
122
Paul Zimmerman802fde92012-04-27 13:10:52 +0300123 /*
124 * The following code is racy when called from dwc3_gadget_wakeup,
125 * and is not needed, at least on newer versions
126 */
127 if (dwc->revision >= DWC3_REVISION_194A)
128 return 0;
129
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300131 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200132 while (--retries) {
133 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 if (DWC3_DSTS_USBLNKST(reg) == state)
136 return 0;
137
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800138 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 }
140
Felipe Balbi8598bde2012-01-02 18:55:57 +0200141 return -ETIMEDOUT;
142}
143
John Youndca01192016-05-19 17:26:05 -0700144/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300145 * dwc3_ep_inc_trb - increment a trb index.
146 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700147 *
148 * The index should never point to the link TRB. After incrementing,
149 * if it is point to the link TRB, wrap around to the beginning. The
150 * link TRB is always at the last TRB entry.
151 */
152static void dwc3_ep_inc_trb(u8 *index)
153{
154 (*index)++;
155 if (*index == (DWC3_TRB_NUM - 1))
156 *index = 0;
157}
158
Felipe Balbibfad65e2017-04-19 14:59:27 +0300159/**
160 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
161 * @dep: The endpoint whose enqueue pointer we're incrementing
162 */
Felipe Balbief966b92016-04-05 13:09:51 +0300163static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200164{
John Youndca01192016-05-19 17:26:05 -0700165 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300166}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167
Felipe Balbibfad65e2017-04-19 14:59:27 +0300168/**
169 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
170 * @dep: The endpoint whose enqueue pointer we're incrementing
171 */
Felipe Balbief966b92016-04-05 13:09:51 +0300172static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
Felipe Balbibfad65e2017-04-19 14:59:27 +0300177/**
178 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
179 * @dep: The endpoint to whom the request belongs to
180 * @req: The request we're giving back
181 * @status: completion code for the request
182 *
183 * Must be called with controller's lock held and interrupts disabled. This
184 * function will unmap @req and call its ->complete() callback to notify upper
185 * layers that it has completed.
186 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300187void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
188 int status)
189{
190 struct dwc3 *dwc = dep->dwc;
191
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300192 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300193 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200194 req->trb = NULL;
Felipe Balbie62c5bc52016-10-25 13:47:21 +0300195 req->remaining = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
197 if (req->request.status == -EINPROGRESS)
198 req->request.status = status;
199
Felipe Balbi4199c5f2017-04-07 14:09:13 +0300200 usb_gadget_unmap_request_by_dev(dwc->sysdev,
201 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300202
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500203 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300204
205 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200206 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300207 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300208
209 if (dep->number > 1)
210 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211}
212
Felipe Balbibfad65e2017-04-19 14:59:27 +0300213/**
214 * dwc3_send_gadget_generic_command - issue a generic command for the controller
215 * @dwc: pointer to the controller context
216 * @cmd: the command to be issued
217 * @param: command parameter
218 *
219 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
220 * and wait for its completion.
221 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500222int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300223{
224 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300225 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300226 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300227 u32 reg;
228
229 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
230 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
231
232 do {
233 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
234 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300235 status = DWC3_DGCMD_STATUS(reg);
236 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300237 ret = -EINVAL;
238 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300239 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100240 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300241
242 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300243 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300244 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300245 }
246
Felipe Balbi71f7e702016-05-23 14:16:19 +0300247 trace_dwc3_gadget_generic_cmd(cmd, param, status);
248
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300249 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300250}
251
Felipe Balbic36d8e92016-04-04 12:46:33 +0300252static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
253
Felipe Balbibfad65e2017-04-19 14:59:27 +0300254/**
255 * dwc3_send_gadget_ep_cmd - issue an endpoint command
256 * @dep: the endpoint to which the command is going to be issued
257 * @cmd: the command to be issued
258 * @params: parameters to the command
259 *
260 * Caller should handle locking. This function will issue @cmd with given
261 * @params to @dep and wait for its completion.
262 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300263int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
264 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300265{
Felipe Balbi8897a762016-09-22 10:56:08 +0300266 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300267 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200268 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300269 u32 reg;
270
Felipe Balbi0933df12016-05-23 14:02:33 +0300271 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300272 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300273 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300274
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300275 /*
276 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
277 * we're issuing an endpoint command, we must check if
278 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
279 *
280 * We will also set SUSPHY bit to what it was before returning as stated
281 * by the same section on Synopsys databook.
282 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300283 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
284 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
285 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
286 susphy = true;
287 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
288 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
289 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300290 }
291
Felipe Balbi59999142016-09-22 12:25:28 +0300292 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300293 int needs_wakeup;
294
295 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
296 dwc->link_state == DWC3_LINK_STATE_U2 ||
297 dwc->link_state == DWC3_LINK_STATE_U3);
298
299 if (unlikely(needs_wakeup)) {
300 ret = __dwc3_gadget_wakeup(dwc);
301 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
302 ret);
303 }
304 }
305
Felipe Balbi2eb88012016-04-12 16:53:39 +0300306 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300309
Felipe Balbi8897a762016-09-22 10:56:08 +0300310 /*
311 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
312 * not relying on XferNotReady, we can make use of a special "No
313 * Response Update Transfer" command where we should clear both CmdAct
314 * and CmdIOC bits.
315 *
316 * With this, we don't need to wait for command completion and can
317 * straight away issue further commands to the endpoint.
318 *
319 * NOTICE: We're making an assumption that control endpoints will never
320 * make use of Update Transfer command. This is a safe assumption
321 * because we can never have more than one request at a time with
322 * Control Endpoints. If anybody changes that assumption, this chunk
323 * needs to be updated accordingly.
324 */
325 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
326 !usb_endpoint_xfer_isoc(desc))
327 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
328 else
329 cmd |= DWC3_DEPCMD_CMDACT;
330
331 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300332 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300333 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300334 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300335 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000336
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000337 switch (cmd_status) {
338 case 0:
339 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300340 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000341 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000342 ret = -EINVAL;
343 break;
344 case DEPEVT_TRANSFER_BUS_EXPIRY:
345 /*
346 * SW issues START TRANSFER command to
347 * isochronous ep with future frame interval. If
348 * future interval time has already passed when
349 * core receives the command, it will respond
350 * with an error status of 'Bus Expiry'.
351 *
352 * Instead of always returning -EINVAL, let's
353 * give a hint to the gadget driver that this is
354 * the case by returning -EAGAIN.
355 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000356 ret = -EAGAIN;
357 break;
358 default:
359 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
360 }
361
Felipe Balbic0ca3242016-04-04 09:11:51 +0300362 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300363 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300364 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300365
Felipe Balbif6bb2252016-05-23 13:53:34 +0300366 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300367 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300368 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300369 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300370
Felipe Balbi0933df12016-05-23 14:02:33 +0300371 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
372
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300373 if (ret == 0) {
374 switch (DWC3_DEPCMD_CMD(cmd)) {
375 case DWC3_DEPCMD_STARTTRANSFER:
376 dep->flags |= DWC3_EP_TRANSFER_STARTED;
377 break;
378 case DWC3_DEPCMD_ENDTRANSFER:
379 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
380 break;
381 default:
382 /* nothing */
383 break;
384 }
385 }
386
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300387 if (unlikely(susphy)) {
388 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
389 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
390 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
391 }
392
Felipe Balbic0ca3242016-04-04 09:11:51 +0300393 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300394}
395
John Youn50c763f2016-05-31 17:49:56 -0700396static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
397{
398 struct dwc3 *dwc = dep->dwc;
399 struct dwc3_gadget_ep_cmd_params params;
400 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
401
402 /*
403 * As of core revision 2.60a the recommended programming model
404 * is to set the ClearPendIN bit when issuing a Clear Stall EP
405 * command for IN endpoints. This is to prevent an issue where
406 * some (non-compliant) hosts may not send ACK TPs for pending
407 * IN transfers due to a mishandled error condition. Synopsys
408 * STAR 9000614252.
409 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800410 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
411 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700412 cmd |= DWC3_DEPCMD_CLEARPENDIN;
413
414 memset(&params, 0, sizeof(params));
415
Felipe Balbi2cd47182016-04-12 16:42:43 +0300416 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700417}
418
Felipe Balbi72246da2011-08-19 18:10:58 +0300419static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200420 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300421{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300422 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423
424 return dep->trb_pool_dma + offset;
425}
426
427static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
428{
429 struct dwc3 *dwc = dep->dwc;
430
431 if (dep->trb_pool)
432 return 0;
433
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530434 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300435 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
436 &dep->trb_pool_dma, GFP_KERNEL);
437 if (!dep->trb_pool) {
438 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
439 dep->name);
440 return -ENOMEM;
441 }
442
443 return 0;
444}
445
446static void dwc3_free_trb_pool(struct dwc3_ep *dep)
447{
448 struct dwc3 *dwc = dep->dwc;
449
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530450 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300451 dep->trb_pool, dep->trb_pool_dma);
452
453 dep->trb_pool = NULL;
454 dep->trb_pool_dma = 0;
455}
456
John Younc4509602016-02-16 20:10:53 -0800457static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
458
459/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300460 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800461 * @dwc: pointer to our controller context structure
462 * @dep: endpoint that is being enabled
463 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300464 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
465 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800466 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300467 * The assignment of transfer resources cannot perfectly follow the data book
468 * due to the fact that the controller driver does not have all knowledge of the
469 * configuration in advance. It is given this information piecemeal by the
470 * composite gadget framework after every SET_CONFIGURATION and
471 * SET_INTERFACE. Trying to follow the databook programming model in this
472 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800473 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300474 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
475 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
476 * incorrect in the scenario of multiple interfaces.
477 *
478 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800479 * endpoint on alt setting (8.1.6).
480 *
481 * The following simplified method is used instead:
482 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300483 * All hardware endpoints can be assigned a transfer resource and this setting
484 * will stay persistent until either a core reset or hibernation. So whenever we
485 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
486 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800487 * guaranteed that there are as many transfer resources as endpoints.
488 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300489 * This function is called for each endpoint when it is being enabled but is
490 * triggered only when called for EP0-out, which always happens first, and which
491 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800492 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300493static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
494{
495 struct dwc3_gadget_ep_cmd_params params;
496 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800497 int i;
498 int ret;
499
500 if (dep->number)
501 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300502
503 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800504 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300505
Felipe Balbi2cd47182016-04-12 16:42:43 +0300506 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800507 if (ret)
508 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300509
John Younc4509602016-02-16 20:10:53 -0800510 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
511 struct dwc3_ep *dep = dwc->eps[i];
512
513 if (!dep)
514 continue;
515
516 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
517 if (ret)
518 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300519 }
520
521 return 0;
522}
523
524static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300525 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300526{
John Youn39ebb052016-11-09 16:36:28 -0800527 const struct usb_ss_ep_comp_descriptor *comp_desc;
528 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300529 struct dwc3_gadget_ep_cmd_params params;
530
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300531 if (dev_WARN_ONCE(dwc->dev, modify && restore,
532 "Can't modify and restore\n"))
533 return -EINVAL;
534
John Youn39ebb052016-11-09 16:36:28 -0800535 comp_desc = dep->endpoint.comp_desc;
536 desc = dep->endpoint.desc;
537
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 memset(&params, 0x00, sizeof(params));
539
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300540 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900541 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
542
543 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800544 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300545 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300546 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900547 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300549 if (modify) {
550 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
551 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600552 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
553 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300554 } else {
555 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600556 }
557
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300558 if (usb_endpoint_xfer_control(desc))
559 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300560
561 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
562 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200564 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300565 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
566 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300567 dep->stream_capable = true;
568 }
569
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500570 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300571 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300572
573 /*
574 * We are doing 1:1 mapping for endpoints, meaning
575 * Physical Endpoints 2 maps to Logical Endpoint 2 and
576 * so on. We consider the direction bit as part of the physical
577 * endpoint number. So USB endpoint 0x81 is 0x03.
578 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300579 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
581 /*
582 * We must use the lower 16 TX FIFOs even though
583 * HW might have more
584 */
585 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300586 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300587
588 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300589 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 dep->interval = 1 << (desc->bInterval - 1);
591 }
592
Felipe Balbi2cd47182016-04-12 16:42:43 +0300593 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594}
595
596static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
597{
598 struct dwc3_gadget_ep_cmd_params params;
599
600 memset(&params, 0x00, sizeof(params));
601
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300602 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300603
Felipe Balbi2cd47182016-04-12 16:42:43 +0300604 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
605 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300606}
607
608/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300609 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 * @dep: endpoint to be initialized
Felipe Balbibfad65e2017-04-19 14:59:27 +0300611 * @modify: if true, modify existing endpoint configuration
612 * @restore: if true, restore endpoint configuration from scratch buffer
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300614 * Caller should take care of locking. Execute all necessary commands to
615 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 */
617static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300618 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300619{
John Youn39ebb052016-11-09 16:36:28 -0800620 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800622
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300624 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300625
626 if (!(dep->flags & DWC3_EP_ENABLED)) {
627 ret = dwc3_gadget_start_config(dwc, dep);
628 if (ret)
629 return ret;
630 }
631
John Youn39ebb052016-11-09 16:36:28 -0800632 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 if (ret)
634 return ret;
635
636 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200637 struct dwc3_trb *trb_st_hw;
638 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300639
Felipe Balbi72246da2011-08-19 18:10:58 +0300640 dep->type = usb_endpoint_type(desc);
641 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800642 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300643
644 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
645 reg |= DWC3_DALEPENA_EP(dep->number);
646 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
647
Baolin Wang76a638f2016-10-31 19:38:36 +0800648 init_waitqueue_head(&dep->wait_end_transfer);
649
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300650 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200651 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300652
John Youn0d257442016-05-19 17:26:08 -0700653 /* Initialize the TRB ring */
654 dep->trb_dequeue = 0;
655 dep->trb_enqueue = 0;
656 memset(dep->trb_pool, 0,
657 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
658
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300659 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300660 trb_st_hw = &dep->trb_pool[0];
661
Felipe Balbif6bafc62012-02-06 11:04:53 +0200662 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200663 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
664 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
665 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
666 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300667 }
668
Felipe Balbia97ea992016-09-29 16:28:56 +0300669 /*
670 * Issue StartTransfer here with no-op TRB so we can always rely on No
671 * Response Update Transfer command.
672 */
673 if (usb_endpoint_xfer_bulk(desc)) {
674 struct dwc3_gadget_ep_cmd_params params;
675 struct dwc3_trb *trb;
676 dma_addr_t trb_dma;
677 u32 cmd;
678
679 memset(&params, 0, sizeof(params));
680 trb = &dep->trb_pool[0];
681 trb_dma = dwc3_trb_dma_offset(dep, trb);
682
683 params.param0 = upper_32_bits(trb_dma);
684 params.param1 = lower_32_bits(trb_dma);
685
686 cmd = DWC3_DEPCMD_STARTTRANSFER;
687
688 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
689 if (ret < 0)
690 return ret;
691
692 dep->flags |= DWC3_EP_BUSY;
693
694 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
695 WARN_ON_ONCE(!dep->resource_index);
696 }
697
Felipe Balbi2870e502016-11-03 13:53:29 +0200698
699out:
700 trace_dwc3_gadget_ep_enable(dep);
701
Felipe Balbi72246da2011-08-19 18:10:58 +0300702 return 0;
703}
704
Paul Zimmermanb992e682012-04-27 14:17:35 +0300705static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200706static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300707{
708 struct dwc3_request *req;
709
Felipe Balbi0e146022016-06-21 10:32:02 +0300710 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300711
Felipe Balbi0e146022016-06-21 10:32:02 +0300712 /* - giveback all requests to gadget driver */
713 while (!list_empty(&dep->started_list)) {
714 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200715
Felipe Balbi0e146022016-06-21 10:32:02 +0300716 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200717 }
718
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200719 while (!list_empty(&dep->pending_list)) {
720 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300721
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200722 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300724}
725
726/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300727 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 * @dep: the endpoint to disable
729 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300730 * This function undoes what __dwc3_gadget_ep_enable did and also removes
731 * requests which are currently being processed by the hardware and those which
732 * are not yet scheduled.
733 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200734 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300735 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300736static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
737{
738 struct dwc3 *dwc = dep->dwc;
739 u32 reg;
740
Felipe Balbi2870e502016-11-03 13:53:29 +0200741 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500742
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200743 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
Felipe Balbi687ef982014-04-16 10:30:33 -0500745 /* make sure HW endpoint isn't stalled */
746 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500747 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500748
Felipe Balbi72246da2011-08-19 18:10:58 +0300749 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
750 reg &= ~DWC3_DALEPENA_EP(dep->number);
751 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
752
Felipe Balbi879631a2011-09-30 10:58:47 +0300753 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800755 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300756
John Youn39ebb052016-11-09 16:36:28 -0800757 /* Clear out the ep descriptors for non-ep0 */
758 if (dep->number > 1) {
759 dep->endpoint.comp_desc = NULL;
760 dep->endpoint.desc = NULL;
761 }
762
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 return 0;
764}
765
766/* -------------------------------------------------------------------------- */
767
768static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
769 const struct usb_endpoint_descriptor *desc)
770{
771 return -EINVAL;
772}
773
774static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
775{
776 return -EINVAL;
777}
778
779/* -------------------------------------------------------------------------- */
780
781static int dwc3_gadget_ep_enable(struct usb_ep *ep,
782 const struct usb_endpoint_descriptor *desc)
783{
784 struct dwc3_ep *dep;
785 struct dwc3 *dwc;
786 unsigned long flags;
787 int ret;
788
789 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
790 pr_debug("dwc3: invalid parameters\n");
791 return -EINVAL;
792 }
793
794 if (!desc->wMaxPacketSize) {
795 pr_debug("dwc3: missing wMaxPacketSize\n");
796 return -EINVAL;
797 }
798
799 dep = to_dwc3_ep(ep);
800 dwc = dep->dwc;
801
Felipe Balbi95ca9612015-12-10 13:08:20 -0600802 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
803 "%s is already enabled\n",
804 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300805 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800808 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300809 spin_unlock_irqrestore(&dwc->lock, flags);
810
811 return ret;
812}
813
814static int dwc3_gadget_ep_disable(struct usb_ep *ep)
815{
816 struct dwc3_ep *dep;
817 struct dwc3 *dwc;
818 unsigned long flags;
819 int ret;
820
821 if (!ep) {
822 pr_debug("dwc3: invalid parameters\n");
823 return -EINVAL;
824 }
825
826 dep = to_dwc3_ep(ep);
827 dwc = dep->dwc;
828
Felipe Balbi95ca9612015-12-10 13:08:20 -0600829 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
830 "%s is already disabled\n",
831 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300832 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
Felipe Balbi72246da2011-08-19 18:10:58 +0300834 spin_lock_irqsave(&dwc->lock, flags);
835 ret = __dwc3_gadget_ep_disable(dep);
836 spin_unlock_irqrestore(&dwc->lock, flags);
837
838 return ret;
839}
840
841static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
842 gfp_t gfp_flags)
843{
844 struct dwc3_request *req;
845 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300846
847 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900848 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300850
851 req->epnum = dep->number;
852 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300853
Felipe Balbi68d34c82016-05-30 13:34:58 +0300854 dep->allocated_requests++;
855
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500856 trace_dwc3_alloc_request(req);
857
Felipe Balbi72246da2011-08-19 18:10:58 +0300858 return &req->request;
859}
860
861static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
862 struct usb_request *request)
863{
864 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300865 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300866
Felipe Balbi68d34c82016-05-30 13:34:58 +0300867 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500868 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300869 kfree(req);
870}
871
Felipe Balbi2c78c022016-08-12 13:13:10 +0300872static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
873
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200874static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
875 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
876 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200877{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300878 struct dwc3 *dwc = dep->dwc;
879 struct usb_gadget *gadget = &dwc->gadget;
880 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200881
Felipe Balbief966b92016-04-05 13:09:51 +0300882 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530883
Felipe Balbif6bafc62012-02-06 11:04:53 +0200884 trb->size = DWC3_TRB_SIZE_LENGTH(length);
885 trb->bpl = lower_32_bits(dma);
886 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200887
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200888 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200889 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200890 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200891 break;
892
893 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300894 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530895 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300896
897 if (speed == USB_SPEED_HIGH) {
898 struct usb_ep *ep = &dep->endpoint;
899 trb->size |= DWC3_TRB_SIZE_PCM1(ep->mult - 1);
900 }
901 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530902 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300903 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200904
905 /* always enable Interrupt on Missed ISOC */
906 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200907 break;
908
909 case USB_ENDPOINT_XFER_BULK:
910 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200911 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200912 break;
913 default:
914 /*
915 * This is only possible with faulty memory because we
916 * checked it already :)
917 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300918 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
919 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200920 }
921
Felipe Balbica4d44e2016-03-10 13:53:27 +0200922 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300923 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300924 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600925
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200926 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300927 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
928 }
929
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200930 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300931 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300932 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200933
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530934 if (chain)
935 trb->ctrl |= DWC3_TRB_CTRL_CHN;
936
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200937 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200938 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200939
940 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500941
942 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200943}
944
John Youn361572b2016-05-19 17:26:17 -0700945/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200946 * dwc3_prepare_one_trb - setup one TRB from one request
947 * @dep: endpoint for which this request is prepared
948 * @req: dwc3_request pointer
949 * @chain: should this TRB be chained to the next?
950 * @node: only for isochronous endpoints. First TRB needs different type.
951 */
952static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
953 struct dwc3_request *req, unsigned chain, unsigned node)
954{
955 struct dwc3_trb *trb;
956 unsigned length = req->request.length;
957 unsigned stream_id = req->request.stream_id;
958 unsigned short_not_ok = req->request.short_not_ok;
959 unsigned no_interrupt = req->request.no_interrupt;
960 dma_addr_t dma = req->request.dma;
961
962 trb = &dep->trb_pool[dep->trb_enqueue];
963
964 if (!req->trb) {
965 dwc3_gadget_move_started_request(req);
966 req->trb = trb;
967 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
968 dep->queued_requests++;
969 }
970
971 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
972 stream_id, short_not_ok, no_interrupt);
973}
974
975/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300976 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -0700977 * @dep: The endpoint with the TRB ring
978 * @index: The index of the current TRB in the ring
979 *
980 * Returns the TRB prior to the one pointed to by the index. If the
981 * index is 0, we will wrap backwards, skip the link TRB, and return
982 * the one just before that.
983 */
984static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
985{
Felipe Balbi45438a02016-08-11 12:26:59 +0300986 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -0700987
Felipe Balbi45438a02016-08-11 12:26:59 +0300988 if (!tmp)
989 tmp = DWC3_TRB_NUM - 1;
990
991 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -0700992}
993
Felipe Balbic4233572016-05-12 14:08:34 +0300994static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
995{
996 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700997 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300998
999 /*
1000 * If enqueue & dequeue are equal than it is either full or empty.
1001 *
1002 * One way to know for sure is if the TRB right before us has HWO bit
1003 * set or not. If it has, then we're definitely full and can't fit any
1004 * more transfers in our ring.
1005 */
1006 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001007 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001008 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001009 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001010
1011 return DWC3_TRB_NUM - 1;
1012 }
1013
John Youn9d7aba72016-08-26 18:43:01 -07001014 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001015 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001016
John Youn9d7aba72016-08-26 18:43:01 -07001017 if (dep->trb_dequeue < dep->trb_enqueue)
1018 trbs_left--;
1019
John Youn32db3d92016-05-19 17:26:12 -07001020 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001021}
1022
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001023static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001024 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001025{
Felipe Balbi1f512112016-08-12 13:17:27 +03001026 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001027 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001028 int i;
1029
Felipe Balbi1f512112016-08-12 13:17:27 +03001030 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001031 unsigned int length = req->request.length;
1032 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1033 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001034 unsigned chain = true;
1035
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001036 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001037 chain = false;
1038
Felipe Balbic6267a52017-01-05 14:58:46 +02001039 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1040 struct dwc3 *dwc = dep->dwc;
1041 struct dwc3_trb *trb;
1042
1043 req->unaligned = true;
1044
1045 /* prepare normal TRB */
1046 dwc3_prepare_one_trb(dep, req, true, i);
1047
1048 /* Now prepare one extra TRB to align transfer size */
1049 trb = &dep->trb_pool[dep->trb_enqueue];
1050 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1051 maxp - rem, false, 0,
1052 req->request.stream_id,
1053 req->request.short_not_ok,
1054 req->request.no_interrupt);
1055 } else {
1056 dwc3_prepare_one_trb(dep, req, chain, i);
1057 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001058
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001059 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 break;
1061 }
1062}
1063
1064static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001065 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001066{
Felipe Balbic6267a52017-01-05 14:58:46 +02001067 unsigned int length = req->request.length;
1068 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1069 unsigned int rem = length % maxp;
1070
1071 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1072 struct dwc3 *dwc = dep->dwc;
1073 struct dwc3_trb *trb;
1074
1075 req->unaligned = true;
1076
1077 /* prepare normal TRB */
1078 dwc3_prepare_one_trb(dep, req, true, 0);
1079
1080 /* Now prepare one extra TRB to align transfer size */
1081 trb = &dep->trb_pool[dep->trb_enqueue];
1082 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1083 false, 0, req->request.stream_id,
1084 req->request.short_not_ok,
1085 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001086 } else if (req->request.zero && req->request.length &&
1087 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1088 struct dwc3 *dwc = dep->dwc;
1089 struct dwc3_trb *trb;
1090
1091 req->zero = true;
1092
1093 /* prepare normal TRB */
1094 dwc3_prepare_one_trb(dep, req, true, 0);
1095
1096 /* Now prepare one extra TRB to handle ZLP */
1097 trb = &dep->trb_pool[dep->trb_enqueue];
1098 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1099 false, 0, req->request.stream_id,
1100 req->request.short_not_ok,
1101 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001102 } else {
1103 dwc3_prepare_one_trb(dep, req, false, 0);
1104 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001105}
1106
Felipe Balbi72246da2011-08-19 18:10:58 +03001107/*
1108 * dwc3_prepare_trbs - setup TRBs from requests
1109 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001111 * The function goes through the requests list and sets up TRBs for the
1112 * transfers. The function returns once there are no more TRBs available or
1113 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001114 */
Felipe Balbic4233572016-05-12 14:08:34 +03001115static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001116{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001117 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001118
1119 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1120
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001121 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001122 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001123
Felipe Balbid86c5a62016-10-25 13:48:52 +03001124 /*
1125 * We can get in a situation where there's a request in the started list
1126 * but there weren't enough TRBs to fully kick it in the first time
1127 * around, so it has been waiting for more TRBs to be freed up.
1128 *
1129 * In that case, we should check if we have a request with pending_sgs
1130 * in the started list and prepare TRBs for that request first,
1131 * otherwise we will prepare TRBs completely out of order and that will
1132 * break things.
1133 */
1134 list_for_each_entry(req, &dep->started_list, list) {
1135 if (req->num_pending_sgs > 0)
1136 dwc3_prepare_one_trb_sg(dep, req);
1137
1138 if (!dwc3_calc_trbs_left(dep))
1139 return;
1140 }
1141
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001142 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001143 struct dwc3 *dwc = dep->dwc;
1144 int ret;
1145
1146 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1147 dep->direction);
1148 if (ret)
1149 return;
1150
1151 req->sg = req->request.sg;
1152 req->num_pending_sgs = req->request.num_mapped_sgs;
1153
Felipe Balbi1f512112016-08-12 13:17:27 +03001154 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001155 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001156 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001157 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001158
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001159 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001160 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001161 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001162}
1163
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001164static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001165{
1166 struct dwc3_gadget_ep_cmd_params params;
1167 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001168 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001169 int ret;
1170 u32 cmd;
1171
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001172 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001173
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001174 dwc3_prepare_trbs(dep);
1175 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001176 if (!req) {
1177 dep->flags |= DWC3_EP_PENDING_REQUEST;
1178 return 0;
1179 }
1180
1181 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001182
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001183 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301184 params.param0 = upper_32_bits(req->trb_dma);
1185 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001186 cmd = DWC3_DEPCMD_STARTTRANSFER |
1187 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301188 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001189 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1190 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301191 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001192
Felipe Balbi2cd47182016-04-12 16:42:43 +03001193 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001194 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001195 /*
1196 * FIXME we need to iterate over the list of requests
1197 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001198 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001200 if (req->trb)
1201 memset(req->trb, 0, sizeof(struct dwc3_trb));
Janusz Dziedzic8ab89da2016-11-09 11:01:31 +01001202 dep->queued_requests--;
Felipe Balbi15b8d9332016-09-22 10:59:12 +03001203 dwc3_gadget_giveback(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001204 return ret;
1205 }
1206
1207 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001208
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001209 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001210 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001211 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001212 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001213
Felipe Balbi72246da2011-08-19 18:10:58 +03001214 return 0;
1215}
1216
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001217static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1218{
1219 u32 reg;
1220
1221 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1222 return DWC3_DSTS_SOFFN(reg);
1223}
1224
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301225static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1226 struct dwc3_ep *dep, u32 cur_uf)
1227{
1228 u32 uf;
1229
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001230 if (list_empty(&dep->pending_list)) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001231 dev_info(dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001232 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301233 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301234 return;
1235 }
1236
John Younaf771d72017-01-26 11:58:40 -08001237 /*
1238 * Schedule the first trb for one interval in the future or at
1239 * least 4 microframes.
1240 */
1241 uf = cur_uf + max_t(u32, 4, dep->interval);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301242
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001243 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301244}
1245
1246static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1247 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1248{
1249 u32 cur_uf, mask;
1250
1251 mask = ~(dep->interval - 1);
1252 cur_uf = event->parameters & mask;
1253
1254 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1255}
1256
Felipe Balbi72246da2011-08-19 18:10:58 +03001257static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1258{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001259 struct dwc3 *dwc = dep->dwc;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001260 int ret = 0;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001261
Felipe Balbibb423982015-11-16 15:31:21 -06001262 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001263 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1264 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001265 return -ESHUTDOWN;
1266 }
1267
Felipe Balbi04fb3652017-05-17 15:57:45 +03001268 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1269 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001270 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001271
Felipe Balbifc8bb912016-05-16 13:14:48 +03001272 pm_runtime_get(dwc->dev);
1273
Felipe Balbi72246da2011-08-19 18:10:58 +03001274 req->request.actual = 0;
1275 req->request.status = -EINPROGRESS;
1276 req->direction = dep->direction;
1277 req->epnum = dep->number;
1278
Felipe Balbife84f522015-09-01 09:01:38 -05001279 trace_dwc3_ep_queue(req);
1280
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001281 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001282
Felipe Balbid889c232016-09-29 15:44:29 +03001283 /*
1284 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1285 * wait for a XferNotReady event so we will know what's the current
1286 * (micro-)frame number.
1287 *
1288 * Without this trick, we are very, very likely gonna get Bus Expiry
1289 * errors which will force us issue EndTransfer command.
1290 */
1291 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001292 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1293 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1294 dwc3_stop_active_transfer(dwc, dep->number, true);
1295 dep->flags = DWC3_EP_ENABLED;
1296 } else {
1297 u32 cur_uf;
1298
1299 cur_uf = __dwc3_gadget_get_frame(dwc);
1300 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
Janusz Dziedzic87aba102016-11-09 11:01:34 +01001301 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001302 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001303 }
1304 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001305 }
1306
Felipe Balbi594e1212016-08-24 14:38:10 +03001307 if (!dwc3_calc_trbs_left(dep))
1308 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001309
Felipe Balbi08a36b52016-08-11 14:27:52 +03001310 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001311 if (ret == -EBUSY)
1312 ret = 0;
1313
1314 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001315}
1316
1317static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1318 gfp_t gfp_flags)
1319{
1320 struct dwc3_request *req = to_dwc3_request(request);
1321 struct dwc3_ep *dep = to_dwc3_ep(ep);
1322 struct dwc3 *dwc = dep->dwc;
1323
1324 unsigned long flags;
1325
1326 int ret;
1327
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001328 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 ret = __dwc3_gadget_ep_queue(dep, req);
1330 spin_unlock_irqrestore(&dwc->lock, flags);
1331
1332 return ret;
1333}
1334
1335static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1336 struct usb_request *request)
1337{
1338 struct dwc3_request *req = to_dwc3_request(request);
1339 struct dwc3_request *r = NULL;
1340
1341 struct dwc3_ep *dep = to_dwc3_ep(ep);
1342 struct dwc3 *dwc = dep->dwc;
1343
1344 unsigned long flags;
1345 int ret = 0;
1346
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001347 trace_dwc3_ep_dequeue(req);
1348
Felipe Balbi72246da2011-08-19 18:10:58 +03001349 spin_lock_irqsave(&dwc->lock, flags);
1350
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001351 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 if (r == req)
1353 break;
1354 }
1355
1356 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001357 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001358 if (r == req)
1359 break;
1360 }
1361 if (r == req) {
1362 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001363 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001364
1365 /*
1366 * If request was already started, this means we had to
1367 * stop the transfer. With that we also need to ignore
1368 * all TRBs used by the request, however TRBs can only
1369 * be modified after completion of END_TRANSFER
1370 * command. So what we do here is that we wait for
1371 * END_TRANSFER completion and only after that, we jump
1372 * over TRBs by clearing HWO and incrementing dequeue
1373 * pointer.
1374 *
1375 * Note that we have 2 possible types of transfers here:
1376 *
1377 * i) Linear buffer request
1378 * ii) SG-list based request
1379 *
1380 * SG-list based requests will have r->num_pending_sgs
1381 * set to a valid number (> 0). Linear requests,
1382 * normally use a single TRB.
1383 *
1384 * For each of these two cases, if r->unaligned flag is
1385 * set, one extra TRB has been used to align transfer
1386 * size to wMaxPacketSize.
1387 *
1388 * All of these cases need to be taken into
1389 * consideration so we don't mess up our TRB ring
1390 * pointers.
1391 */
1392 wait_event_lock_irq(dep->wait_end_transfer,
1393 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1394 dwc->lock);
1395
1396 if (!r->trb)
1397 goto out1;
1398
1399 if (r->num_pending_sgs) {
1400 struct dwc3_trb *trb;
1401 int i = 0;
1402
1403 for (i = 0; i < r->num_pending_sgs; i++) {
1404 trb = r->trb + i;
1405 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1406 dwc3_ep_inc_deq(dep);
1407 }
1408
Felipe Balbid6e5a542017-04-07 16:34:38 +03001409 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001410 trb = r->trb + r->num_pending_sgs + 1;
1411 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1412 dwc3_ep_inc_deq(dep);
1413 }
1414 } else {
1415 struct dwc3_trb *trb = r->trb;
1416
1417 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1418 dwc3_ep_inc_deq(dep);
1419
Felipe Balbid6e5a542017-04-07 16:34:38 +03001420 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001421 trb = r->trb + 1;
1422 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1423 dwc3_ep_inc_deq(dep);
1424 }
1425 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301426 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001427 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001428 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 request, ep->name);
1430 ret = -EINVAL;
1431 goto out0;
1432 }
1433
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301434out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001435 /* giveback the request */
Felipe Balbicf3113d2017-02-17 11:12:44 +02001436 dep->queued_requests--;
Felipe Balbi72246da2011-08-19 18:10:58 +03001437 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1438
1439out0:
1440 spin_unlock_irqrestore(&dwc->lock, flags);
1441
1442 return ret;
1443}
1444
Felipe Balbi7a608552014-09-24 14:19:52 -05001445int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001446{
1447 struct dwc3_gadget_ep_cmd_params params;
1448 struct dwc3 *dwc = dep->dwc;
1449 int ret;
1450
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001451 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1452 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1453 return -EINVAL;
1454 }
1455
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 memset(&params, 0x00, sizeof(params));
1457
1458 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001459 struct dwc3_trb *trb;
1460
1461 unsigned transfer_in_flight;
1462 unsigned started;
1463
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001464 if (dep->flags & DWC3_EP_STALL)
1465 return 0;
1466
Felipe Balbi69450c42016-05-30 13:37:02 +03001467 if (dep->number > 1)
1468 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1469 else
1470 trb = &dwc->ep0_trb[dep->trb_enqueue];
1471
1472 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1473 started = !list_empty(&dep->started_list);
1474
1475 if (!protocol && ((dep->direction && transfer_in_flight) ||
1476 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001477 return -EAGAIN;
1478 }
1479
Felipe Balbi2cd47182016-04-12 16:42:43 +03001480 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1481 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001482 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001483 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001484 dep->name);
1485 else
1486 dep->flags |= DWC3_EP_STALL;
1487 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001488 if (!(dep->flags & DWC3_EP_STALL))
1489 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001490
John Youn50c763f2016-05-31 17:49:56 -07001491 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001492 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001493 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 dep->name);
1495 else
Alan Sterna535d812013-11-01 12:05:12 -04001496 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001498
Felipe Balbi72246da2011-08-19 18:10:58 +03001499 return ret;
1500}
1501
1502static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1503{
1504 struct dwc3_ep *dep = to_dwc3_ep(ep);
1505 struct dwc3 *dwc = dep->dwc;
1506
1507 unsigned long flags;
1508
1509 int ret;
1510
1511 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001512 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001513 spin_unlock_irqrestore(&dwc->lock, flags);
1514
1515 return ret;
1516}
1517
1518static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1519{
1520 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001521 struct dwc3 *dwc = dep->dwc;
1522 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001523 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001524
Paul Zimmerman249a4562012-02-24 17:32:16 -08001525 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 dep->flags |= DWC3_EP_WEDGE;
1527
Pratyush Anand08f0d962012-06-25 22:40:43 +05301528 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001529 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301530 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001531 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001532 spin_unlock_irqrestore(&dwc->lock, flags);
1533
1534 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001535}
1536
1537/* -------------------------------------------------------------------------- */
1538
1539static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1540 .bLength = USB_DT_ENDPOINT_SIZE,
1541 .bDescriptorType = USB_DT_ENDPOINT,
1542 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1543};
1544
1545static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1546 .enable = dwc3_gadget_ep0_enable,
1547 .disable = dwc3_gadget_ep0_disable,
1548 .alloc_request = dwc3_gadget_ep_alloc_request,
1549 .free_request = dwc3_gadget_ep_free_request,
1550 .queue = dwc3_gadget_ep0_queue,
1551 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301552 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001553 .set_wedge = dwc3_gadget_ep_set_wedge,
1554};
1555
1556static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1557 .enable = dwc3_gadget_ep_enable,
1558 .disable = dwc3_gadget_ep_disable,
1559 .alloc_request = dwc3_gadget_ep_alloc_request,
1560 .free_request = dwc3_gadget_ep_free_request,
1561 .queue = dwc3_gadget_ep_queue,
1562 .dequeue = dwc3_gadget_ep_dequeue,
1563 .set_halt = dwc3_gadget_ep_set_halt,
1564 .set_wedge = dwc3_gadget_ep_set_wedge,
1565};
1566
1567/* -------------------------------------------------------------------------- */
1568
1569static int dwc3_gadget_get_frame(struct usb_gadget *g)
1570{
1571 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001572
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001573 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001574}
1575
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001576static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001577{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001578 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001579
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001580 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001581 u32 reg;
1582
Felipe Balbi72246da2011-08-19 18:10:58 +03001583 u8 link_state;
1584 u8 speed;
1585
Felipe Balbi72246da2011-08-19 18:10:58 +03001586 /*
1587 * According to the Databook Remote wakeup request should
1588 * be issued only when the device is in early suspend state.
1589 *
1590 * We can check that via USB Link State bits in DSTS register.
1591 */
1592 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1593
1594 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001595 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001596 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001597 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001598
1599 link_state = DWC3_DSTS_USBLNKST(reg);
1600
1601 switch (link_state) {
1602 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1603 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1604 break;
1605 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001606 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 }
1608
Felipe Balbi8598bde2012-01-02 18:55:57 +02001609 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1610 if (ret < 0) {
1611 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001612 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001613 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001614
Paul Zimmerman802fde92012-04-27 13:10:52 +03001615 /* Recent versions do this automatically */
1616 if (dwc->revision < DWC3_REVISION_194A) {
1617 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001618 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001619 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1620 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1621 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001622
Paul Zimmerman1d046792012-02-15 18:56:56 -08001623 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001624 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001625
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001626 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001627 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1628
1629 /* in HS, means ON */
1630 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1631 break;
1632 }
1633
1634 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1635 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001636 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 }
1638
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001639 return 0;
1640}
1641
1642static int dwc3_gadget_wakeup(struct usb_gadget *g)
1643{
1644 struct dwc3 *dwc = gadget_to_dwc(g);
1645 unsigned long flags;
1646 int ret;
1647
1648 spin_lock_irqsave(&dwc->lock, flags);
1649 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 spin_unlock_irqrestore(&dwc->lock, flags);
1651
1652 return ret;
1653}
1654
1655static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1656 int is_selfpowered)
1657{
1658 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001659 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001660
Paul Zimmerman249a4562012-02-24 17:32:16 -08001661 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001662 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001663 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001664
1665 return 0;
1666}
1667
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001668static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001669{
1670 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001671 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001672
Felipe Balbifc8bb912016-05-16 13:14:48 +03001673 if (pm_runtime_suspended(dwc->dev))
1674 return 0;
1675
Felipe Balbi72246da2011-08-19 18:10:58 +03001676 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001677 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001678 if (dwc->revision <= DWC3_REVISION_187A) {
1679 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1680 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1681 }
1682
1683 if (dwc->revision >= DWC3_REVISION_194A)
1684 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1685 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001686
1687 if (dwc->has_hibernation)
1688 reg |= DWC3_DCTL_KEEP_CONNECT;
1689
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001690 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001691 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001692 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001693
1694 if (dwc->has_hibernation && !suspend)
1695 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1696
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001697 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001698 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001699
1700 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1701
1702 do {
1703 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001704 reg &= DWC3_DSTS_DEVCTRLHLT;
1705 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001706
1707 if (!timeout)
1708 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001709
Pratyush Anand6f17f742012-07-02 10:21:55 +05301710 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001711}
1712
1713static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1714{
1715 struct dwc3 *dwc = gadget_to_dwc(g);
1716 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301717 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001718
1719 is_on = !!is_on;
1720
Baolin Wangbb014732016-10-14 17:11:33 +08001721 /*
1722 * Per databook, when we want to stop the gadget, if a control transfer
1723 * is still in process, complete it and get the core into setup phase.
1724 */
1725 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1726 reinit_completion(&dwc->ep0_in_setup);
1727
1728 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1729 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1730 if (ret == 0) {
1731 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1732 return -ETIMEDOUT;
1733 }
1734 }
1735
Felipe Balbi72246da2011-08-19 18:10:58 +03001736 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001737 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001738 spin_unlock_irqrestore(&dwc->lock, flags);
1739
Pratyush Anand6f17f742012-07-02 10:21:55 +05301740 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741}
1742
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001743static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1744{
1745 u32 reg;
1746
1747 /* Enable all but Start and End of Frame IRQs */
1748 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1749 DWC3_DEVTEN_EVNTOVERFLOWEN |
1750 DWC3_DEVTEN_CMDCMPLTEN |
1751 DWC3_DEVTEN_ERRTICERREN |
1752 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001753 DWC3_DEVTEN_CONNECTDONEEN |
1754 DWC3_DEVTEN_USBRSTEN |
1755 DWC3_DEVTEN_DISCONNEVTEN);
1756
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001757 if (dwc->revision < DWC3_REVISION_250A)
1758 reg |= DWC3_DEVTEN_ULSTCNGEN;
1759
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001760 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1761}
1762
1763static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1764{
1765 /* mask all interrupts */
1766 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1767}
1768
1769static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001770static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001771
Felipe Balbi4e994722016-05-13 14:09:59 +03001772/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001773 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1774 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001775 *
1776 * The following looks like complex but it's actually very simple. In order to
1777 * calculate the number of packets we can burst at once on OUT transfers, we're
1778 * gonna use RxFIFO size.
1779 *
1780 * To calculate RxFIFO size we need two numbers:
1781 * MDWIDTH = size, in bits, of the internal memory bus
1782 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1783 *
1784 * Given these two numbers, the formula is simple:
1785 *
1786 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1787 *
1788 * 24 bytes is for 3x SETUP packets
1789 * 16 bytes is a clock domain crossing tolerance
1790 *
1791 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1792 */
1793static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1794{
1795 u32 ram2_depth;
1796 u32 mdwidth;
1797 u32 nump;
1798 u32 reg;
1799
1800 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1801 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1802
1803 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1804 nump = min_t(u32, nump, 16);
1805
1806 /* update NumP */
1807 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1808 reg &= ~DWC3_DCFG_NUMP_MASK;
1809 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1810 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1811}
1812
Felipe Balbid7be2952016-05-04 15:49:37 +03001813static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001814{
Felipe Balbi72246da2011-08-19 18:10:58 +03001815 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001816 int ret = 0;
1817 u32 reg;
1818
John Youncf40b862016-11-14 12:32:43 -08001819 /*
1820 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1821 * the core supports IMOD, disable it.
1822 */
1823 if (dwc->imod_interval) {
1824 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1825 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1826 } else if (dwc3_has_imod(dwc)) {
1827 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1828 }
1829
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001830 /*
1831 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1832 * field instead of letting dwc3 itself calculate that automatically.
1833 *
1834 * This way, we maximize the chances that we'll be able to get several
1835 * bursts of data without going through any sort of endpoint throttling.
1836 */
1837 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1838 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1839 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1840
Felipe Balbi4e994722016-05-13 14:09:59 +03001841 dwc3_gadget_setup_nump(dwc);
1842
Felipe Balbi72246da2011-08-19 18:10:58 +03001843 /* Start with SuperSpeed Default */
1844 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1845
1846 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001847 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 if (ret) {
1849 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001850 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001851 }
1852
1853 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001854 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001855 if (ret) {
1856 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001857 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 }
1859
1860 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001861 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 dwc3_ep0_out_start(dwc);
1863
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001864 dwc3_gadget_enable_irq(dwc);
1865
Felipe Balbid7be2952016-05-04 15:49:37 +03001866 return 0;
1867
1868err1:
1869 __dwc3_gadget_ep_disable(dwc->eps[0]);
1870
1871err0:
1872 return ret;
1873}
1874
1875static int dwc3_gadget_start(struct usb_gadget *g,
1876 struct usb_gadget_driver *driver)
1877{
1878 struct dwc3 *dwc = gadget_to_dwc(g);
1879 unsigned long flags;
1880 int ret = 0;
1881 int irq;
1882
Roger Quadros9522def2016-06-10 14:48:38 +03001883 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001884 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1885 IRQF_SHARED, "dwc3", dwc->ev_buf);
1886 if (ret) {
1887 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1888 irq, ret);
1889 goto err0;
1890 }
1891
1892 spin_lock_irqsave(&dwc->lock, flags);
1893 if (dwc->gadget_driver) {
1894 dev_err(dwc->dev, "%s is already bound to %s\n",
1895 dwc->gadget.name,
1896 dwc->gadget_driver->driver.name);
1897 ret = -EBUSY;
1898 goto err1;
1899 }
1900
1901 dwc->gadget_driver = driver;
1902
Felipe Balbifc8bb912016-05-16 13:14:48 +03001903 if (pm_runtime_active(dwc->dev))
1904 __dwc3_gadget_start(dwc);
1905
Felipe Balbi72246da2011-08-19 18:10:58 +03001906 spin_unlock_irqrestore(&dwc->lock, flags);
1907
1908 return 0;
1909
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001910err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001911 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001912 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001913
1914err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001915 return ret;
1916}
1917
Felipe Balbid7be2952016-05-04 15:49:37 +03001918static void __dwc3_gadget_stop(struct dwc3 *dwc)
1919{
1920 dwc3_gadget_disable_irq(dwc);
1921 __dwc3_gadget_ep_disable(dwc->eps[0]);
1922 __dwc3_gadget_ep_disable(dwc->eps[1]);
1923}
1924
Felipe Balbi22835b82014-10-17 12:05:12 -05001925static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001926{
1927 struct dwc3 *dwc = gadget_to_dwc(g);
1928 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001929 int epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03001930
1931 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001932
1933 if (pm_runtime_suspended(dwc->dev))
1934 goto out;
1935
Felipe Balbid7be2952016-05-04 15:49:37 +03001936 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001937
1938 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1939 struct dwc3_ep *dep = dwc->eps[epnum];
1940
1941 if (!dep)
1942 continue;
1943
1944 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1945 continue;
1946
1947 wait_event_lock_irq(dep->wait_end_transfer,
1948 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1949 dwc->lock);
1950 }
1951
1952out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001953 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001954 spin_unlock_irqrestore(&dwc->lock, flags);
1955
Felipe Balbi3f308d12016-05-16 14:17:06 +03001956 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001957
Felipe Balbi72246da2011-08-19 18:10:58 +03001958 return 0;
1959}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001960
Felipe Balbi7d8d0632017-06-06 16:05:23 +03001961static void dwc3_gadget_set_speed(struct usb_gadget *g,
1962 enum usb_device_speed speed)
1963{
1964 struct dwc3 *dwc = gadget_to_dwc(g);
1965 unsigned long flags;
1966 u32 reg;
1967
1968 spin_lock_irqsave(&dwc->lock, flags);
1969 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1970 reg &= ~(DWC3_DCFG_SPEED_MASK);
1971
1972 /*
1973 * WORKAROUND: DWC3 revision < 2.20a have an issue
1974 * which would cause metastability state on Run/Stop
1975 * bit if we try to force the IP to USB2-only mode.
1976 *
1977 * Because of that, we cannot configure the IP to any
1978 * speed other than the SuperSpeed
1979 *
1980 * Refers to:
1981 *
1982 * STAR#9000525659: Clock Domain Crossing on DCTL in
1983 * USB 2.0 Mode
1984 */
1985 if (dwc->revision < DWC3_REVISION_220A) {
1986 reg |= DWC3_DCFG_SUPERSPEED;
1987 } else {
1988 switch (speed) {
1989 case USB_SPEED_LOW:
1990 reg |= DWC3_DCFG_LOWSPEED;
1991 break;
1992 case USB_SPEED_FULL:
1993 reg |= DWC3_DCFG_FULLSPEED;
1994 break;
1995 case USB_SPEED_HIGH:
1996 reg |= DWC3_DCFG_HIGHSPEED;
1997 break;
1998 case USB_SPEED_SUPER:
1999 reg |= DWC3_DCFG_SUPERSPEED;
2000 break;
2001 case USB_SPEED_SUPER_PLUS:
2002 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2003 break;
2004 default:
2005 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2006
2007 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2008 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2009 else
2010 reg |= DWC3_DCFG_SUPERSPEED;
2011 }
2012 }
2013 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2014
2015 spin_unlock_irqrestore(&dwc->lock, flags);
2016}
2017
Felipe Balbi72246da2011-08-19 18:10:58 +03002018static const struct usb_gadget_ops dwc3_gadget_ops = {
2019 .get_frame = dwc3_gadget_get_frame,
2020 .wakeup = dwc3_gadget_wakeup,
2021 .set_selfpowered = dwc3_gadget_set_selfpowered,
2022 .pullup = dwc3_gadget_pullup,
2023 .udc_start = dwc3_gadget_start,
2024 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002025 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002026};
2027
2028/* -------------------------------------------------------------------------- */
2029
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002030static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
Felipe Balbi72246da2011-08-19 18:10:58 +03002031{
2032 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002033 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002034
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002035 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2036
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002037 for (epnum = 0; epnum < total; epnum++) {
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002038 bool direction = epnum & 1;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002039 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002040
Felipe Balbi72246da2011-08-19 18:10:58 +03002041 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002042 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002043 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002044
2045 dep->dwc = dwc;
2046 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002047 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002048 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002049 dwc->eps[epnum] = dep;
2050
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002051 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002052 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002053
Felipe Balbi72246da2011-08-19 18:10:58 +03002054 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002055
2056 if (!(dep->number > 1)) {
2057 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2058 dep->endpoint.comp_desc = NULL;
2059 }
2060
Felipe Balbi74674cb2016-04-13 16:44:39 +03002061 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002062
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002063 if (num == 0) {
Robert Baldygae117e742013-12-13 12:23:38 +01002064 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302065 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002067 if (!direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002068 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002069 } else if (direction) {
2070 int mdwidth;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002071 int kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002072 int size;
2073 int ret;
Felipe Balbi28781782017-01-23 18:01:59 +02002074
2075 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2076 /* MDWIDTH is represented in bits, we need it in bytes */
2077 mdwidth /= 8;
2078
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002079 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
Felipe Balbi28781782017-01-23 18:01:59 +02002080 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2081
2082 /* FIFO Depth is in MDWDITH bytes. Multiply */
2083 size *= mdwidth;
2084
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002085 kbytes = size / 1024;
2086 if (kbytes == 0)
2087 kbytes = 1;
Felipe Balbi28781782017-01-23 18:01:59 +02002088
2089 /*
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002090 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
Felipe Balbi28781782017-01-23 18:01:59 +02002091 * internal overhead. We don't really know how these are used,
2092 * but documentation say it exists.
2093 */
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002094 size -= mdwidth * (kbytes + 1);
2095 size /= kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002096
2097 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2098
2099 dep->endpoint.max_streams = 15;
2100 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2101 list_add_tail(&dep->endpoint.ep_list,
2102 &dwc->gadget.ep_list);
2103
2104 ret = dwc3_alloc_trb_pool(dep);
2105 if (ret)
2106 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002107 } else {
2108 int ret;
2109
Robert Baldygae117e742013-12-13 12:23:38 +01002110 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002111 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002112 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2113 list_add_tail(&dep->endpoint.ep_list,
2114 &dwc->gadget.ep_list);
2115
2116 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002117 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002118 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002119 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002120
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002121 if (num == 0) {
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002122 dep->endpoint.caps.type_control = true;
2123 } else {
2124 dep->endpoint.caps.type_iso = true;
2125 dep->endpoint.caps.type_bulk = true;
2126 dep->endpoint.caps.type_int = true;
2127 }
2128
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002129 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002130 dep->endpoint.caps.dir_out = !direction;
2131
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002132 INIT_LIST_HEAD(&dep->pending_list);
2133 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002134 }
2135
2136 return 0;
2137}
2138
2139static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2140{
2141 struct dwc3_ep *dep;
2142 u8 epnum;
2143
2144 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2145 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002146 if (!dep)
2147 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302148 /*
2149 * Physical endpoints 0 and 1 are special; they form the
2150 * bi-directional USB endpoint 0.
2151 *
2152 * For those two physical endpoints, we don't allocate a TRB
2153 * pool nor do we add them the endpoints list. Due to that, we
2154 * shouldn't do these two operations otherwise we would end up
2155 * with all sorts of bugs when removing dwc3.ko.
2156 */
2157 if (epnum != 0 && epnum != 1) {
2158 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002159 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302160 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002161
2162 kfree(dep);
2163 }
2164}
2165
Felipe Balbi72246da2011-08-19 18:10:58 +03002166/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002167
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302168static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2169 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002170 const struct dwc3_event_depevt *event, int status,
2171 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302172{
2173 unsigned int count;
2174 unsigned int s_pkt = 0;
2175 unsigned int trb_status;
2176
Felipe Balbidc55c672016-08-12 13:20:32 +03002177 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002178
2179 if (req->trb == trb)
2180 dep->queued_requests--;
2181
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002182 trace_dwc3_complete_trb(dep, trb);
2183
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002184 /*
2185 * If we're in the middle of series of chained TRBs and we
2186 * receive a short transfer along the way, DWC3 will skip
2187 * through all TRBs including the last TRB in the chain (the
2188 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2189 * bit and SW has to do it manually.
2190 *
2191 * We're going to do that here to avoid problems of HW trying
2192 * to use bogus TRBs for transfers.
2193 */
2194 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2195 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2196
Felipe Balbic6267a52017-01-05 14:58:46 +02002197 /*
2198 * If we're dealing with unaligned size OUT transfer, we will be left
2199 * with one TRB pending in the ring. We need to manually clear HWO bit
2200 * from that TRB.
2201 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002202 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002203 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2204 return 1;
2205 }
2206
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302207 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002208 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302209
Felipe Balbi35b27192017-03-08 13:56:37 +02002210 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2211 return 1;
2212
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302213 if (dep->direction) {
2214 if (count) {
2215 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2216 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302217 /*
2218 * If missed isoc occurred and there is
2219 * no request queued then issue END
2220 * TRANSFER, so that core generates
2221 * next xfernotready and we will issue
2222 * a fresh START TRANSFER.
2223 * If there are still queued request
2224 * then wait, do not issue either END
2225 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002226 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302227 * giveback.If any future queued request
2228 * is successfully transferred then we
2229 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002230 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302231 */
2232 dep->flags |= DWC3_EP_MISSED_ISOC;
2233 } else {
2234 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2235 dep->name);
2236 status = -ECONNRESET;
2237 }
2238 } else {
2239 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2240 }
2241 } else {
2242 if (count && (event->status & DEPEVT_STATUS_SHORT))
2243 s_pkt = 1;
2244 }
2245
Felipe Balbi7c705df2016-08-10 12:35:30 +03002246 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302247 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002248
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302249 if ((event->status & DEPEVT_STATUS_IOC) &&
2250 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2251 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002252
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302253 return 0;
2254}
2255
Felipe Balbi72246da2011-08-19 18:10:58 +03002256static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2257 const struct dwc3_event_depevt *event, int status)
2258{
Felipe Balbi31162af2016-08-11 14:38:37 +03002259 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002260 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002261 bool ioc = false;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002262 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002263
Felipe Balbi31162af2016-08-11 14:38:37 +03002264 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002265 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002266 int chain;
2267
Felipe Balbi1f512112016-08-12 13:17:27 +03002268 length = req->request.length;
2269 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002270 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002271 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002272 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002273 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002274 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002275
Felipe Balbi1f512112016-08-12 13:17:27 +03002276 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002277 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002278
Felipe Balbi7282c4e2016-10-25 13:50:46 +03002279 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2280 break;
2281
Felipe Balbi1f512112016-08-12 13:17:27 +03002282 req->sg = sg_next(s);
2283 req->num_pending_sgs--;
2284
Felipe Balbi31162af2016-08-11 14:38:37 +03002285 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2286 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002287 if (ret)
2288 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002289 }
2290 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002291 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002292 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002293 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002294 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002295
Felipe Balbid6e5a542017-04-07 16:34:38 +03002296 if (req->unaligned || req->zero) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002297 trb = &dep->trb_pool[dep->trb_dequeue];
2298 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2299 event, status, false);
2300 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002301 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002302 }
2303
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002304 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002305
Felipe Balbiff377ae2016-10-25 13:54:00 +03002306 if ((req->request.actual < length) && req->num_pending_sgs)
Felipe Balbi1f512112016-08-12 13:17:27 +03002307 return __dwc3_gadget_kick_transfer(dep, 0);
2308
Ville Syrjäläd115d702015-08-31 19:48:28 +03002309 dwc3_gadget_giveback(dep, req, status);
2310
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002311 if (ret) {
2312 if ((event->status & DEPEVT_STATUS_IOC) &&
2313 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2314 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002315 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002316 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002317 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002318
Felipe Balbi4cb42212016-05-18 12:37:21 +03002319 /*
2320 * Our endpoint might get disabled by another thread during
2321 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2322 * early on so DWC3_EP_BUSY flag gets cleared
2323 */
2324 if (!dep->endpoint.desc)
2325 return 1;
2326
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302327 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002328 list_empty(&dep->started_list)) {
2329 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302330 /*
2331 * If there is no entry in request list then do
2332 * not issue END TRANSFER now. Just set PENDING
2333 * flag, so that END TRANSFER is issued when an
2334 * entry is added into request list.
2335 */
2336 dep->flags = DWC3_EP_PENDING_REQUEST;
2337 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002338 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302339 dep->flags = DWC3_EP_ENABLED;
2340 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302341 return 1;
2342 }
2343
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002344 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2345 return 0;
2346
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 return 1;
2348}
2349
2350static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002351 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002352{
2353 unsigned status = 0;
2354 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002355 u32 is_xfer_complete;
2356
2357 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002358
2359 if (event->status & DEPEVT_STATUS_BUSERR)
2360 status = -ECONNRESET;
2361
Paul Zimmerman1d046792012-02-15 18:56:56 -08002362 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002363 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002364 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002365 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002366
2367 /*
2368 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2369 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2370 */
2371 if (dwc->revision < DWC3_REVISION_183A) {
2372 u32 reg;
2373 int i;
2374
2375 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002376 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002377
2378 if (!(dep->flags & DWC3_EP_ENABLED))
2379 continue;
2380
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002381 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002382 return;
2383 }
2384
2385 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2386 reg |= dwc->u1u2;
2387 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2388
2389 dwc->u1u2 = 0;
2390 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002391
Felipe Balbi4cb42212016-05-18 12:37:21 +03002392 /*
2393 * Our endpoint might get disabled by another thread during
2394 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2395 * early on so DWC3_EP_BUSY flag gets cleared
2396 */
2397 if (!dep->endpoint.desc)
2398 return;
2399
Felipe Balbie6e709b2015-09-28 15:16:56 -05002400 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002401 int ret;
2402
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002403 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002404 if (!ret || ret == -EBUSY)
2405 return;
2406 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002407}
2408
Felipe Balbi72246da2011-08-19 18:10:58 +03002409static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2410 const struct dwc3_event_depevt *event)
2411{
2412 struct dwc3_ep *dep;
2413 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002414 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002415
2416 dep = dwc->eps[epnum];
2417
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002418 if (!(dep->flags & DWC3_EP_ENABLED)) {
2419 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2420 return;
2421
2422 /* Handle only EPCMDCMPLT when EP disabled */
2423 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2424 return;
2425 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002426
Felipe Balbi72246da2011-08-19 18:10:58 +03002427 if (epnum == 0 || epnum == 1) {
2428 dwc3_ep0_interrupt(dwc, event);
2429 return;
2430 }
2431
2432 switch (event->endpoint_event) {
2433 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002434 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002435
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002436 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8566cd12016-09-26 11:16:39 +03002437 dev_err(dwc->dev, "XferComplete for Isochronous endpoint\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 return;
2439 }
2440
Jingoo Han029d97f2014-07-04 15:00:51 +09002441 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002442 break;
2443 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002444 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002445 break;
2446 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002447 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002448 dwc3_gadget_start_isoc(dwc, dep, event);
2449 } else {
2450 int ret;
2451
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002452 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002453 if (!ret || ret == -EBUSY)
2454 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03002455 }
2456
2457 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002458 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002459 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002460 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2461 dep->name);
2462 return;
2463 }
Felipe Balbi879631a2011-09-30 10:58:47 +03002464 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002465 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002466 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2467
2468 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2469 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2470 wake_up(&dep->wait_end_transfer);
2471 }
2472 break;
2473 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002474 break;
2475 }
2476}
2477
2478static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2479{
2480 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2481 spin_unlock(&dwc->lock);
2482 dwc->gadget_driver->disconnect(&dwc->gadget);
2483 spin_lock(&dwc->lock);
2484 }
2485}
2486
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002487static void dwc3_suspend_gadget(struct dwc3 *dwc)
2488{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002489 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002490 spin_unlock(&dwc->lock);
2491 dwc->gadget_driver->suspend(&dwc->gadget);
2492 spin_lock(&dwc->lock);
2493 }
2494}
2495
2496static void dwc3_resume_gadget(struct dwc3 *dwc)
2497{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002498 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002499 spin_unlock(&dwc->lock);
2500 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002501 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002502 }
2503}
2504
2505static void dwc3_reset_gadget(struct dwc3 *dwc)
2506{
2507 if (!dwc->gadget_driver)
2508 return;
2509
2510 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2511 spin_unlock(&dwc->lock);
2512 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002513 spin_lock(&dwc->lock);
2514 }
2515}
2516
Paul Zimmermanb992e682012-04-27 14:17:35 +03002517static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002518{
2519 struct dwc3_ep *dep;
2520 struct dwc3_gadget_ep_cmd_params params;
2521 u32 cmd;
2522 int ret;
2523
2524 dep = dwc->eps[epnum];
2525
Baolin Wang76a638f2016-10-31 19:38:36 +08002526 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2527 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302528 return;
2529
Pratyush Anand57911502012-07-06 15:19:10 +05302530 /*
2531 * NOTICE: We are violating what the Databook says about the
2532 * EndTransfer command. Ideally we would _always_ wait for the
2533 * EndTransfer Command Completion IRQ, but that's causing too
2534 * much trouble synchronizing between us and gadget driver.
2535 *
2536 * We have discussed this with the IP Provider and it was
2537 * suggested to giveback all requests here, but give HW some
2538 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002539 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302540 *
2541 * Note also that a similar handling was tested by Synopsys
2542 * (thanks a lot Paul) and nothing bad has come out of it.
2543 * In short, what we're doing is:
2544 *
2545 * - Issue EndTransfer WITH CMDIOC bit set
2546 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002547 *
2548 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2549 * supports a mode to work around the above limitation. The
2550 * software can poll the CMDACT bit in the DEPCMD register
2551 * after issuing a EndTransfer command. This mode is enabled
2552 * by writing GUCTL2[14]. This polling is already done in the
2553 * dwc3_send_gadget_ep_cmd() function so if the mode is
2554 * enabled, the EndTransfer command will have completed upon
2555 * returning from this function and we don't need to delay for
2556 * 100us.
2557 *
2558 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302559 */
2560
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302561 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002562 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2563 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002564 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302565 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002566 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302567 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002568 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002569 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002570
Baolin Wang76a638f2016-10-31 19:38:36 +08002571 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2572 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002573 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002574 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002575}
2576
Felipe Balbi72246da2011-08-19 18:10:58 +03002577static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2578{
2579 u32 epnum;
2580
2581 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2582 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002583 int ret;
2584
2585 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002586 if (!dep)
2587 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002588
2589 if (!(dep->flags & DWC3_EP_STALL))
2590 continue;
2591
2592 dep->flags &= ~DWC3_EP_STALL;
2593
John Youn50c763f2016-05-31 17:49:56 -07002594 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002595 WARN_ON_ONCE(ret);
2596 }
2597}
2598
2599static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2600{
Felipe Balbic4430a22012-05-24 10:30:01 +03002601 int reg;
2602
Felipe Balbi72246da2011-08-19 18:10:58 +03002603 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2604 reg &= ~DWC3_DCTL_INITU1ENA;
2605 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2606
2607 reg &= ~DWC3_DCTL_INITU2ENA;
2608 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002609
Felipe Balbi72246da2011-08-19 18:10:58 +03002610 dwc3_disconnect_gadget(dwc);
2611
2612 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002613 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002614 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002615
2616 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002617}
2618
Felipe Balbi72246da2011-08-19 18:10:58 +03002619static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2620{
2621 u32 reg;
2622
Felipe Balbifc8bb912016-05-16 13:14:48 +03002623 dwc->connected = true;
2624
Felipe Balbidf62df52011-10-14 15:11:49 +03002625 /*
2626 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2627 * would cause a missing Disconnect Event if there's a
2628 * pending Setup Packet in the FIFO.
2629 *
2630 * There's no suggested workaround on the official Bug
2631 * report, which states that "unless the driver/application
2632 * is doing any special handling of a disconnect event,
2633 * there is no functional issue".
2634 *
2635 * Unfortunately, it turns out that we _do_ some special
2636 * handling of a disconnect event, namely complete all
2637 * pending transfers, notify gadget driver of the
2638 * disconnection, and so on.
2639 *
2640 * Our suggested workaround is to follow the Disconnect
2641 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002642 * flag. Such flag gets set whenever we have a SETUP_PENDING
2643 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002644 * same endpoint.
2645 *
2646 * Refers to:
2647 *
2648 * STAR#9000466709: RTL: Device : Disconnect event not
2649 * generated if setup packet pending in FIFO
2650 */
2651 if (dwc->revision < DWC3_REVISION_188A) {
2652 if (dwc->setup_packet_pending)
2653 dwc3_gadget_disconnect_interrupt(dwc);
2654 }
2655
Felipe Balbi8e744752014-11-06 14:27:53 +08002656 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002657
2658 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2659 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2660 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002661 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002662 dwc3_clear_stall_all_ep(dwc);
2663
2664 /* Reset device address to zero */
2665 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2666 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2667 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002668}
2669
Felipe Balbi72246da2011-08-19 18:10:58 +03002670static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2671{
Felipe Balbi72246da2011-08-19 18:10:58 +03002672 struct dwc3_ep *dep;
2673 int ret;
2674 u32 reg;
2675 u8 speed;
2676
Felipe Balbi72246da2011-08-19 18:10:58 +03002677 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2678 speed = reg & DWC3_DSTS_CONNECTSPD;
2679 dwc->speed = speed;
2680
John Youn5fb6fda2016-11-10 17:23:25 -08002681 /*
2682 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2683 * each time on Connect Done.
2684 *
2685 * Currently we always use the reset value. If any platform
2686 * wants to set this to a different value, we need to add a
2687 * setting and update GCTL.RAMCLKSEL here.
2688 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002689
2690 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002691 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002692 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2693 dwc->gadget.ep0->maxpacket = 512;
2694 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2695 break;
John Youn2da9ad72016-05-20 16:34:26 -07002696 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002697 /*
2698 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2699 * would cause a missing USB3 Reset event.
2700 *
2701 * In such situations, we should force a USB3 Reset
2702 * event by calling our dwc3_gadget_reset_interrupt()
2703 * routine.
2704 *
2705 * Refers to:
2706 *
2707 * STAR#9000483510: RTL: SS : USB3 reset event may
2708 * not be generated always when the link enters poll
2709 */
2710 if (dwc->revision < DWC3_REVISION_190A)
2711 dwc3_gadget_reset_interrupt(dwc);
2712
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2714 dwc->gadget.ep0->maxpacket = 512;
2715 dwc->gadget.speed = USB_SPEED_SUPER;
2716 break;
John Youn2da9ad72016-05-20 16:34:26 -07002717 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002718 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2719 dwc->gadget.ep0->maxpacket = 64;
2720 dwc->gadget.speed = USB_SPEED_HIGH;
2721 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002722 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2724 dwc->gadget.ep0->maxpacket = 64;
2725 dwc->gadget.speed = USB_SPEED_FULL;
2726 break;
John Youn2da9ad72016-05-20 16:34:26 -07002727 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002728 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2729 dwc->gadget.ep0->maxpacket = 8;
2730 dwc->gadget.speed = USB_SPEED_LOW;
2731 break;
2732 }
2733
Pratyush Anand2b758352013-01-14 15:59:31 +05302734 /* Enable USB2 LPM Capability */
2735
John Younee5cd412016-02-05 17:08:45 -08002736 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002737 (speed != DWC3_DSTS_SUPERSPEED) &&
2738 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302739 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2740 reg |= DWC3_DCFG_LPM_CAP;
2741 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2742
2743 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2744 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2745
Huang Rui460d0982014-10-31 11:11:18 +08002746 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302747
Huang Rui80caf7d2014-10-28 19:54:26 +08002748 /*
2749 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2750 * DCFG.LPMCap is set, core responses with an ACK and the
2751 * BESL value in the LPM token is less than or equal to LPM
2752 * NYET threshold.
2753 */
2754 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2755 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002756 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002757
2758 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2759 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2760
Pratyush Anand2b758352013-01-14 15:59:31 +05302761 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002762 } else {
2763 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2764 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2765 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302766 }
2767
Felipe Balbi72246da2011-08-19 18:10:58 +03002768 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002769 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002770 if (ret) {
2771 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2772 return;
2773 }
2774
2775 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002776 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002777 if (ret) {
2778 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2779 return;
2780 }
2781
2782 /*
2783 * Configure PHY via GUSB3PIPECTLn if required.
2784 *
2785 * Update GTXFIFOSIZn
2786 *
2787 * In both cases reset values should be sufficient.
2788 */
2789}
2790
2791static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2792{
Felipe Balbi72246da2011-08-19 18:10:58 +03002793 /*
2794 * TODO take core out of low power mode when that's
2795 * implemented.
2796 */
2797
Jiebing Liad14d4e2014-12-11 13:26:29 +08002798 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2799 spin_unlock(&dwc->lock);
2800 dwc->gadget_driver->resume(&dwc->gadget);
2801 spin_lock(&dwc->lock);
2802 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002803}
2804
2805static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2806 unsigned int evtinfo)
2807{
Felipe Balbifae2b902011-10-14 13:00:30 +03002808 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002809 unsigned int pwropt;
2810
2811 /*
2812 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2813 * Hibernation mode enabled which would show up when device detects
2814 * host-initiated U3 exit.
2815 *
2816 * In that case, device will generate a Link State Change Interrupt
2817 * from U3 to RESUME which is only necessary if Hibernation is
2818 * configured in.
2819 *
2820 * There are no functional changes due to such spurious event and we
2821 * just need to ignore it.
2822 *
2823 * Refers to:
2824 *
2825 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2826 * operational mode
2827 */
2828 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2829 if ((dwc->revision < DWC3_REVISION_250A) &&
2830 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2831 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2832 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002833 return;
2834 }
2835 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002836
2837 /*
2838 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2839 * on the link partner, the USB session might do multiple entry/exit
2840 * of low power states before a transfer takes place.
2841 *
2842 * Due to this problem, we might experience lower throughput. The
2843 * suggested workaround is to disable DCTL[12:9] bits if we're
2844 * transitioning from U1/U2 to U0 and enable those bits again
2845 * after a transfer completes and there are no pending transfers
2846 * on any of the enabled endpoints.
2847 *
2848 * This is the first half of that workaround.
2849 *
2850 * Refers to:
2851 *
2852 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2853 * core send LGO_Ux entering U0
2854 */
2855 if (dwc->revision < DWC3_REVISION_183A) {
2856 if (next == DWC3_LINK_STATE_U0) {
2857 u32 u1u2;
2858 u32 reg;
2859
2860 switch (dwc->link_state) {
2861 case DWC3_LINK_STATE_U1:
2862 case DWC3_LINK_STATE_U2:
2863 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2864 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2865 | DWC3_DCTL_ACCEPTU2ENA
2866 | DWC3_DCTL_INITU1ENA
2867 | DWC3_DCTL_ACCEPTU1ENA);
2868
2869 if (!dwc->u1u2)
2870 dwc->u1u2 = reg & u1u2;
2871
2872 reg &= ~u1u2;
2873
2874 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2875 break;
2876 default:
2877 /* do nothing */
2878 break;
2879 }
2880 }
2881 }
2882
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002883 switch (next) {
2884 case DWC3_LINK_STATE_U1:
2885 if (dwc->speed == USB_SPEED_SUPER)
2886 dwc3_suspend_gadget(dwc);
2887 break;
2888 case DWC3_LINK_STATE_U2:
2889 case DWC3_LINK_STATE_U3:
2890 dwc3_suspend_gadget(dwc);
2891 break;
2892 case DWC3_LINK_STATE_RESUME:
2893 dwc3_resume_gadget(dwc);
2894 break;
2895 default:
2896 /* do nothing */
2897 break;
2898 }
2899
Felipe Balbie57ebc12014-04-22 13:20:12 -05002900 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002901}
2902
Baolin Wang72704f82016-05-16 16:43:53 +08002903static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2904 unsigned int evtinfo)
2905{
2906 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2907
2908 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2909 dwc3_suspend_gadget(dwc);
2910
2911 dwc->link_state = next;
2912}
2913
Felipe Balbie1dadd32014-02-25 14:47:54 -06002914static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2915 unsigned int evtinfo)
2916{
2917 unsigned int is_ss = evtinfo & BIT(4);
2918
Felipe Balbibfad65e2017-04-19 14:59:27 +03002919 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002920 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2921 * have a known issue which can cause USB CV TD.9.23 to fail
2922 * randomly.
2923 *
2924 * Because of this issue, core could generate bogus hibernation
2925 * events which SW needs to ignore.
2926 *
2927 * Refers to:
2928 *
2929 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2930 * Device Fallback from SuperSpeed
2931 */
2932 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2933 return;
2934
2935 /* enter hibernation here */
2936}
2937
Felipe Balbi72246da2011-08-19 18:10:58 +03002938static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2939 const struct dwc3_event_devt *event)
2940{
2941 switch (event->type) {
2942 case DWC3_DEVICE_EVENT_DISCONNECT:
2943 dwc3_gadget_disconnect_interrupt(dwc);
2944 break;
2945 case DWC3_DEVICE_EVENT_RESET:
2946 dwc3_gadget_reset_interrupt(dwc);
2947 break;
2948 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2949 dwc3_gadget_conndone_interrupt(dwc);
2950 break;
2951 case DWC3_DEVICE_EVENT_WAKEUP:
2952 dwc3_gadget_wakeup_interrupt(dwc);
2953 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002954 case DWC3_DEVICE_EVENT_HIBER_REQ:
2955 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2956 "unexpected hibernation event\n"))
2957 break;
2958
2959 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2960 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002961 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2962 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2963 break;
2964 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002965 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002966 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002967 /*
2968 * Ignore suspend event until the gadget enters into
2969 * USB_STATE_CONFIGURED state.
2970 */
2971 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2972 dwc3_gadget_suspend_interrupt(dwc,
2973 event->event_info);
2974 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002975 break;
2976 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002977 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002978 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002979 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002980 break;
2981 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002982 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002983 }
2984}
2985
2986static void dwc3_process_event_entry(struct dwc3 *dwc,
2987 const union dwc3_event *event)
2988{
Felipe Balbi43c96be2016-09-26 13:23:34 +03002989 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002990
Felipe Balbidfc5e802017-04-26 13:44:51 +03002991 if (!event->type.is_devspec)
2992 dwc3_endpoint_interrupt(dwc, &event->depevt);
2993 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03002994 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03002995 else
Felipe Balbi72246da2011-08-19 18:10:58 +03002996 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03002997}
2998
Felipe Balbidea520a2016-03-30 09:39:34 +03002999static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003000{
Felipe Balbidea520a2016-03-30 09:39:34 +03003001 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003002 irqreturn_t ret = IRQ_NONE;
3003 int left;
3004 u32 reg;
3005
Felipe Balbif42f2442013-06-12 21:25:08 +03003006 left = evt->count;
3007
3008 if (!(evt->flags & DWC3_EVENT_PENDING))
3009 return IRQ_NONE;
3010
3011 while (left > 0) {
3012 union dwc3_event event;
3013
John Younebbb2d52016-11-15 13:07:02 +02003014 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003015
3016 dwc3_process_event_entry(dwc, &event);
3017
3018 /*
3019 * FIXME we wrap around correctly to the next entry as
3020 * almost all entries are 4 bytes in size. There is one
3021 * entry which has 12 bytes which is a regular entry
3022 * followed by 8 bytes data. ATM I don't know how
3023 * things are organized if we get next to the a
3024 * boundary so I worry about that once we try to handle
3025 * that.
3026 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003027 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003028 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003029 }
3030
3031 evt->count = 0;
3032 evt->flags &= ~DWC3_EVENT_PENDING;
3033 ret = IRQ_HANDLED;
3034
3035 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003036 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003037 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003038 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003039
John Youncf40b862016-11-14 12:32:43 -08003040 if (dwc->imod_interval) {
3041 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3042 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3043 }
3044
Felipe Balbif42f2442013-06-12 21:25:08 +03003045 return ret;
3046}
3047
Felipe Balbidea520a2016-03-30 09:39:34 +03003048static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003049{
Felipe Balbidea520a2016-03-30 09:39:34 +03003050 struct dwc3_event_buffer *evt = _evt;
3051 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003052 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003053 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003054
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003055 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003056 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003057 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003058
3059 return ret;
3060}
3061
Felipe Balbidea520a2016-03-30 09:39:34 +03003062static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003063{
Felipe Balbidea520a2016-03-30 09:39:34 +03003064 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003065 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003066 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003067 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003068
Felipe Balbifc8bb912016-05-16 13:14:48 +03003069 if (pm_runtime_suspended(dwc->dev)) {
3070 pm_runtime_get(dwc->dev);
3071 disable_irq_nosync(dwc->irq_gadget);
3072 dwc->pending_events = true;
3073 return IRQ_HANDLED;
3074 }
3075
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003076 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003077 count &= DWC3_GEVNTCOUNT_MASK;
3078 if (!count)
3079 return IRQ_NONE;
3080
Felipe Balbib15a7622011-06-30 16:57:15 +03003081 evt->count = count;
3082 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003083
Felipe Balbie8adfc32013-06-12 21:11:14 +03003084 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003085 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003086 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003087 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003088
John Younebbb2d52016-11-15 13:07:02 +02003089 amount = min(count, evt->length - evt->lpos);
3090 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3091
3092 if (amount < count)
3093 memcpy(evt->cache, evt->buf, count - amount);
3094
John Youn65aca322016-11-15 13:08:59 +02003095 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3096
Felipe Balbib15a7622011-06-30 16:57:15 +03003097 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003098}
3099
Felipe Balbidea520a2016-03-30 09:39:34 +03003100static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003101{
Felipe Balbidea520a2016-03-30 09:39:34 +03003102 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003103
Felipe Balbidea520a2016-03-30 09:39:34 +03003104 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003105}
3106
Felipe Balbi6db38122016-10-03 11:27:01 +03003107static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3108{
3109 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3110 int irq;
3111
3112 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3113 if (irq > 0)
3114 goto out;
3115
3116 if (irq == -EPROBE_DEFER)
3117 goto out;
3118
3119 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3120 if (irq > 0)
3121 goto out;
3122
3123 if (irq == -EPROBE_DEFER)
3124 goto out;
3125
3126 irq = platform_get_irq(dwc3_pdev, 0);
3127 if (irq > 0)
3128 goto out;
3129
3130 if (irq != -EPROBE_DEFER)
3131 dev_err(dwc->dev, "missing peripheral IRQ\n");
3132
3133 if (!irq)
3134 irq = -EINVAL;
3135
3136out:
3137 return irq;
3138}
3139
Felipe Balbi72246da2011-08-19 18:10:58 +03003140/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003141 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003142 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003143 *
3144 * Returns 0 on success otherwise negative errno.
3145 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003146int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003147{
Felipe Balbi6db38122016-10-03 11:27:01 +03003148 int ret;
3149 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003150
Felipe Balbi6db38122016-10-03 11:27:01 +03003151 irq = dwc3_gadget_get_irq(dwc);
3152 if (irq < 0) {
3153 ret = irq;
3154 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003155 }
3156
3157 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003158
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303159 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3160 sizeof(*dwc->ep0_trb) * 2,
3161 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003162 if (!dwc->ep0_trb) {
3163 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3164 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003165 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003166 }
3167
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003168 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003169 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003170 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003171 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003172 }
3173
Felipe Balbi905dc042017-01-05 14:46:52 +02003174 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3175 &dwc->bounce_addr, GFP_KERNEL);
3176 if (!dwc->bounce) {
3177 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003178 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003179 }
3180
Baolin Wangbb014732016-10-14 17:11:33 +08003181 init_completion(&dwc->ep0_in_setup);
3182
Felipe Balbi72246da2011-08-19 18:10:58 +03003183 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003184 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003185 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003186 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003187 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003188
3189 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003190 * FIXME We might be setting max_speed to <SUPER, however versions
3191 * <2.20a of dwc3 have an issue with metastability (documented
3192 * elsewhere in this driver) which tells us we can't set max speed to
3193 * anything lower than SUPER.
3194 *
3195 * Because gadget.max_speed is only used by composite.c and function
3196 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3197 * to happen so we avoid sending SuperSpeed Capability descriptor
3198 * together with our BOS descriptor as that could confuse host into
3199 * thinking we can handle super speed.
3200 *
3201 * Note that, in fact, we won't even support GetBOS requests when speed
3202 * is less than super speed because we don't have means, yet, to tell
3203 * composite.c that we are USB 2.0 + LPM ECN.
3204 */
3205 if (dwc->revision < DWC3_REVISION_220A)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003206 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003207 dwc->revision);
3208
3209 dwc->gadget.max_speed = dwc->maximum_speed;
3210
3211 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003212 * REVISIT: Here we should clear all pending IRQs to be
3213 * sure we're starting from a well known location.
3214 */
3215
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003216 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003217 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003218 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003219
Felipe Balbi72246da2011-08-19 18:10:58 +03003220 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3221 if (ret) {
3222 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003223 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003224 }
3225
3226 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003227
3228err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003229 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003230
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003231err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003232 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3233 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003234
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003235err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003236 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003237
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003238err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303239 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003240 dwc->ep0_trb, dwc->ep0_trb_addr);
3241
Felipe Balbi72246da2011-08-19 18:10:58 +03003242err0:
3243 return ret;
3244}
3245
Felipe Balbi7415f172012-04-30 14:56:33 +03003246/* -------------------------------------------------------------------------- */
3247
Felipe Balbi72246da2011-08-19 18:10:58 +03003248void dwc3_gadget_exit(struct dwc3 *dwc)
3249{
Felipe Balbi72246da2011-08-19 18:10:58 +03003250 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003251 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003252 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003253 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003254 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303255 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003256 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003257}
Felipe Balbi7415f172012-04-30 14:56:33 +03003258
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003259int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003260{
Roger Quadros9772b472016-04-12 11:33:29 +03003261 if (!dwc->gadget_driver)
3262 return 0;
3263
Roger Quadros1551e352017-02-15 14:16:26 +02003264 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003265 dwc3_disconnect_gadget(dwc);
3266 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003267
3268 return 0;
3269}
3270
3271int dwc3_gadget_resume(struct dwc3 *dwc)
3272{
Felipe Balbi7415f172012-04-30 14:56:33 +03003273 int ret;
3274
Roger Quadros9772b472016-04-12 11:33:29 +03003275 if (!dwc->gadget_driver)
3276 return 0;
3277
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003278 ret = __dwc3_gadget_start(dwc);
3279 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003280 goto err0;
3281
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003282 ret = dwc3_gadget_run_stop(dwc, true, false);
3283 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003284 goto err1;
3285
Felipe Balbi7415f172012-04-30 14:56:33 +03003286 return 0;
3287
3288err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003289 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003290
3291err0:
3292 return ret;
3293}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003294
3295void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3296{
3297 if (dwc->pending_events) {
3298 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3299 dwc->pending_events = false;
3300 enable_irq(dwc->irq_gadget);
3301 }
3302}