blob: e230b0a2a2f3900c20f3b5d4db375c276e7382f3 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020030/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030031 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020032 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
34 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030035 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037 */
38int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
39{
40 u32 reg;
41
42 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
43 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
44
45 switch (mode) {
46 case TEST_J:
47 case TEST_K:
48 case TEST_SE0_NAK:
49 case TEST_PACKET:
50 case TEST_FORCE_EN:
51 reg |= mode << 1;
52 break;
53 default:
54 return -EINVAL;
55 }
56
57 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
58
59 return 0;
60}
61
Felipe Balbi8598bde2012-01-02 18:55:57 +020062/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030063 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030064 * @dwc: pointer to our context structure
65 *
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
68 */
69int dwc3_gadget_get_link_state(struct dwc3 *dwc)
70{
71 u32 reg;
72
73 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
74
75 return DWC3_DSTS_USBLNKST(reg);
76}
77
78/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030079 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020080 * @dwc: pointer to our context structure
81 * @state: the state to put link into
82 *
83 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080084 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020085 */
86int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
87{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080088 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020089 u32 reg;
90
Paul Zimmerman802fde92012-04-27 13:10:52 +030091 /*
92 * Wait until device controller is ready. Only applies to 1.94a and
93 * later RTL.
94 */
95 if (dwc->revision >= DWC3_REVISION_194A) {
96 while (--retries) {
97 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
98 if (reg & DWC3_DSTS_DCNRD)
99 udelay(5);
100 else
101 break;
102 }
103
104 if (retries <= 0)
105 return -ETIMEDOUT;
106 }
107
Felipe Balbi8598bde2012-01-02 18:55:57 +0200108 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
109 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
110
111 /* set requested state */
112 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
113 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
114
Paul Zimmerman802fde92012-04-27 13:10:52 +0300115 /*
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
118 */
119 if (dwc->revision >= DWC3_REVISION_194A)
120 return 0;
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300123 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200124 while (--retries) {
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
126
Felipe Balbi8598bde2012-01-02 18:55:57 +0200127 if (DWC3_DSTS_USBLNKST(reg) == state)
128 return 0;
129
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800130 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 }
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 return -ETIMEDOUT;
134}
135
John Youndca01192016-05-19 17:26:05 -0700136/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700139 *
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
143 */
144static void dwc3_ep_inc_trb(u8 *index)
145{
146 (*index)++;
147 if (*index == (DWC3_TRB_NUM - 1))
148 *index = 0;
149}
150
Felipe Balbibfad65e2017-04-19 14:59:27 +0300151/**
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
154 */
Felipe Balbief966b92016-04-05 13:09:51 +0300155static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200156{
John Youndca01192016-05-19 17:26:05 -0700157 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbibfad65e2017-04-19 14:59:27 +0300160/**
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
Felipe Balbief966b92016-04-05 13:09:51 +0300164static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
165{
John Youndca01192016-05-19 17:26:05 -0700166 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167}
168
Felipe Balbic91815b2018-03-26 13:14:47 +0300169void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
170 struct dwc3_request *req, int status)
171{
172 struct dwc3 *dwc = dep->dwc;
173
174 req->started = false;
175 list_del(&req->list);
176 req->remaining = 0;
177
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
180
181 if (req->trb)
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
183 &req->request, req->direction);
184
185 req->trb = NULL;
186 trace_dwc3_gadget_giveback(req);
187
188 if (dep->number > 1)
189 pm_runtime_put(dwc->dev);
190}
191
Felipe Balbibfad65e2017-04-19 14:59:27 +0300192/**
193 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
194 * @dep: The endpoint to whom the request belongs to
195 * @req: The request we're giving back
196 * @status: completion code for the request
197 *
198 * Must be called with controller's lock held and interrupts disabled. This
199 * function will unmap @req and call its ->complete() callback to notify upper
200 * layers that it has completed.
201 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300202void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
203 int status)
204{
205 struct dwc3 *dwc = dep->dwc;
206
Felipe Balbic91815b2018-03-26 13:14:47 +0300207 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300208
209 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 spin_lock(&dwc->lock);
212}
213
Felipe Balbibfad65e2017-04-19 14:59:27 +0300214/**
215 * dwc3_send_gadget_generic_command - issue a generic command for the controller
216 * @dwc: pointer to the controller context
217 * @cmd: the command to be issued
218 * @param: command parameter
219 *
220 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
221 * and wait for its completion.
222 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500223int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300224{
225 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300226 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300227 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300228 u32 reg;
229
230 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
231 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
232
233 do {
234 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
235 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300236 status = DWC3_DGCMD_STATUS(reg);
237 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300238 ret = -EINVAL;
239 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300240 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100241 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300242
243 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300244 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300245 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 }
247
Felipe Balbi71f7e702016-05-23 14:16:19 +0300248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
249
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300251}
252
Felipe Balbic36d8e92016-04-04 12:46:33 +0300253static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
254
Felipe Balbibfad65e2017-04-19 14:59:27 +0300255/**
256 * dwc3_send_gadget_ep_cmd - issue an endpoint command
257 * @dep: the endpoint to which the command is going to be issued
258 * @cmd: the command to be issued
259 * @params: parameters to the command
260 *
261 * Caller should handle locking. This function will issue @cmd with given
262 * @params to @dep and wait for its completion.
263 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300264int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
Felipe Balbi8897a762016-09-22 10:56:08 +0300267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300268 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000269 u32 timeout = 1000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 u32 reg;
271
Felipe Balbi0933df12016-05-23 14:02:33 +0300272 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300273 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300274 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300276 /*
277 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
278 * we're issuing an endpoint command, we must check if
279 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
280 *
281 * We will also set SUSPHY bit to what it was before returning as stated
282 * by the same section on Synopsys databook.
283 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300284 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
285 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
286 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
287 susphy = true;
288 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
289 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
290 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300291 }
292
Felipe Balbi59999142016-09-22 12:25:28 +0300293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300294 int needs_wakeup;
295
296 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
297 dwc->link_state == DWC3_LINK_STATE_U2 ||
298 dwc->link_state == DWC3_LINK_STATE_U3);
299
300 if (unlikely(needs_wakeup)) {
301 ret = __dwc3_gadget_wakeup(dwc);
302 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
303 ret);
304 }
305 }
306
Felipe Balbi2eb88012016-04-12 16:53:39 +0300307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300310
Felipe Balbi8897a762016-09-22 10:56:08 +0300311 /*
312 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
313 * not relying on XferNotReady, we can make use of a special "No
314 * Response Update Transfer" command where we should clear both CmdAct
315 * and CmdIOC bits.
316 *
317 * With this, we don't need to wait for command completion and can
318 * straight away issue further commands to the endpoint.
319 *
320 * NOTICE: We're making an assumption that control endpoints will never
321 * make use of Update Transfer command. This is a safe assumption
322 * because we can never have more than one request at a time with
323 * Control Endpoints. If anybody changes that assumption, this chunk
324 * needs to be updated accordingly.
325 */
326 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
327 !usb_endpoint_xfer_isoc(desc))
328 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
329 else
330 cmd |= DWC3_DEPCMD_CMDACT;
331
332 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000337
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000338 switch (cmd_status) {
339 case 0:
340 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300341 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000342 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000343 ret = -EINVAL;
344 break;
345 case DEPEVT_TRANSFER_BUS_EXPIRY:
346 /*
347 * SW issues START TRANSFER command to
348 * isochronous ep with future frame interval. If
349 * future interval time has already passed when
350 * core receives the command, it will respond
351 * with an error status of 'Bus Expiry'.
352 *
353 * Instead of always returning -EINVAL, let's
354 * give a hint to the gadget driver that this is
355 * the case by returning -EAGAIN.
356 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000357 ret = -EAGAIN;
358 break;
359 default:
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
361 }
362
Felipe Balbic0ca3242016-04-04 09:11:51 +0300363 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300365 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366
Felipe Balbif6bb2252016-05-23 13:53:34 +0300367 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300368 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300369 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300370 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300371
Felipe Balbi0933df12016-05-23 14:02:33 +0300372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
373
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300374 if (ret == 0) {
375 switch (DWC3_DEPCMD_CMD(cmd)) {
376 case DWC3_DEPCMD_STARTTRANSFER:
377 dep->flags |= DWC3_EP_TRANSFER_STARTED;
378 break;
379 case DWC3_DEPCMD_ENDTRANSFER:
380 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
381 break;
382 default:
383 /* nothing */
384 break;
385 }
386 }
387
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300388 if (unlikely(susphy)) {
389 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
392 }
393
Felipe Balbic0ca3242016-04-04 09:11:51 +0300394 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395}
396
John Youn50c763f2016-05-31 17:49:56 -0700397static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400 struct dwc3_gadget_ep_cmd_params params;
401 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
402
403 /*
404 * As of core revision 2.60a the recommended programming model
405 * is to set the ClearPendIN bit when issuing a Clear Stall EP
406 * command for IN endpoints. This is to prevent an issue where
407 * some (non-compliant) hosts may not send ACK TPs for pending
408 * IN transfers due to a mishandled error condition. Synopsys
409 * STAR 9000614252.
410 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
414
415 memset(&params, 0, sizeof(params));
416
Felipe Balbi2cd47182016-04-12 16:42:43 +0300417 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700418}
419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200421 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300422{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300423 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
425 return dep->trb_pool_dma + offset;
426}
427
428static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
429{
430 struct dwc3 *dwc = dep->dwc;
431
432 if (dep->trb_pool)
433 return 0;
434
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
437 &dep->trb_pool_dma, GFP_KERNEL);
438 if (!dep->trb_pool) {
439 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
440 dep->name);
441 return -ENOMEM;
442 }
443
444 return 0;
445}
446
447static void dwc3_free_trb_pool(struct dwc3_ep *dep)
448{
449 struct dwc3 *dwc = dep->dwc;
450
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 dep->trb_pool, dep->trb_pool_dma);
453
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
456}
457
John Younc4509602016-02-16 20:10:53 -0800458static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
459
460/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300461 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800462 * @dwc: pointer to our controller context structure
463 * @dep: endpoint that is being enabled
464 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300465 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
466 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800467 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300468 * The assignment of transfer resources cannot perfectly follow the data book
469 * due to the fact that the controller driver does not have all knowledge of the
470 * configuration in advance. It is given this information piecemeal by the
471 * composite gadget framework after every SET_CONFIGURATION and
472 * SET_INTERFACE. Trying to follow the databook programming model in this
473 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800474 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300475 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
476 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
477 * incorrect in the scenario of multiple interfaces.
478 *
479 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800480 * endpoint on alt setting (8.1.6).
481 *
482 * The following simplified method is used instead:
483 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300484 * All hardware endpoints can be assigned a transfer resource and this setting
485 * will stay persistent until either a core reset or hibernation. So whenever we
486 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
487 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800488 * guaranteed that there are as many transfer resources as endpoints.
489 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300490 * This function is called for each endpoint when it is being enabled but is
491 * triggered only when called for EP0-out, which always happens first, and which
492 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800493 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300494static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
495{
496 struct dwc3_gadget_ep_cmd_params params;
497 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800498 int i;
499 int ret;
500
501 if (dep->number)
502 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
504 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800505 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
Felipe Balbi2cd47182016-04-12 16:42:43 +0300507 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800508 if (ret)
509 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300510
John Younc4509602016-02-16 20:10:53 -0800511 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
512 struct dwc3_ep *dep = dwc->eps[i];
513
514 if (!dep)
515 continue;
516
517 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
518 if (ret)
519 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520 }
521
522 return 0;
523}
524
525static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300526 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300527{
John Youn39ebb052016-11-09 16:36:28 -0800528 const struct usb_ss_ep_comp_descriptor *comp_desc;
529 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300530 struct dwc3_gadget_ep_cmd_params params;
531
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300532 if (dev_WARN_ONCE(dwc->dev, modify && restore,
533 "Can't modify and restore\n"))
534 return -EINVAL;
535
John Youn39ebb052016-11-09 16:36:28 -0800536 comp_desc = dep->endpoint.comp_desc;
537 desc = dep->endpoint.desc;
538
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 memset(&params, 0x00, sizeof(params));
540
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300541 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900542 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
543
544 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800545 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300546 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300547 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900548 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300550 if (modify) {
551 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
552 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600553 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
554 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300555 } else {
556 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600557 }
558
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300559 if (usb_endpoint_xfer_control(desc))
560 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300561
562 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
563 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200565 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300566 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
567 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300568 dep->stream_capable = true;
569 }
570
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500571 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300572 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300573
574 /*
575 * We are doing 1:1 mapping for endpoints, meaning
576 * Physical Endpoints 2 maps to Logical Endpoint 2 and
577 * so on. We consider the direction bit as part of the physical
578 * endpoint number. So USB endpoint 0x81 is 0x03.
579 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300580 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581
582 /*
583 * We must use the lower 16 TX FIFOs even though
584 * HW might have more
585 */
586 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300587 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
589 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 dep->interval = 1 << (desc->bInterval - 1);
592 }
593
Felipe Balbi2cd47182016-04-12 16:42:43 +0300594 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300595}
596
597static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
598{
599 struct dwc3_gadget_ep_cmd_params params;
600
601 memset(&params, 0x00, sizeof(params));
602
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300603 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300604
Felipe Balbi2cd47182016-04-12 16:42:43 +0300605 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
606 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300607}
608
609/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300610 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 * @dep: endpoint to be initialized
Felipe Balbibfad65e2017-04-19 14:59:27 +0300612 * @modify: if true, modify existing endpoint configuration
613 * @restore: if true, restore endpoint configuration from scratch buffer
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300615 * Caller should take care of locking. Execute all necessary commands to
616 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 */
618static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300619 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300620{
John Youn39ebb052016-11-09 16:36:28 -0800621 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800623
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300625 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
628 ret = dwc3_gadget_start_config(dwc, dep);
629 if (ret)
630 return ret;
631 }
632
John Youn39ebb052016-11-09 16:36:28 -0800633 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 if (ret)
635 return ret;
636
637 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200638 struct dwc3_trb *trb_st_hw;
639 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300640
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 dep->type = usb_endpoint_type(desc);
642 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800643 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
645 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
646 reg |= DWC3_DALEPENA_EP(dep->number);
647 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
648
Baolin Wang76a638f2016-10-31 19:38:36 +0800649 init_waitqueue_head(&dep->wait_end_transfer);
650
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300651 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200652 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300653
John Youn0d257442016-05-19 17:26:08 -0700654 /* Initialize the TRB ring */
655 dep->trb_dequeue = 0;
656 dep->trb_enqueue = 0;
657 memset(dep->trb_pool, 0,
658 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
659
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300660 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 trb_st_hw = &dep->trb_pool[0];
662
Felipe Balbif6bafc62012-02-06 11:04:53 +0200663 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200664 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
665 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
666 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
667 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 }
669
Felipe Balbia97ea992016-09-29 16:28:56 +0300670 /*
671 * Issue StartTransfer here with no-op TRB so we can always rely on No
672 * Response Update Transfer command.
673 */
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300674 if (usb_endpoint_xfer_bulk(desc) ||
675 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300676 struct dwc3_gadget_ep_cmd_params params;
677 struct dwc3_trb *trb;
678 dma_addr_t trb_dma;
679 u32 cmd;
680
681 memset(&params, 0, sizeof(params));
682 trb = &dep->trb_pool[0];
683 trb_dma = dwc3_trb_dma_offset(dep, trb);
684
685 params.param0 = upper_32_bits(trb_dma);
686 params.param1 = lower_32_bits(trb_dma);
687
688 cmd = DWC3_DEPCMD_STARTTRANSFER;
689
690 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
691 if (ret < 0)
692 return ret;
693
Felipe Balbia97ea992016-09-29 16:28:56 +0300694 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 +0200698out:
699 trace_dwc3_gadget_ep_enable(dep);
700
Felipe Balbi72246da2011-08-19 18:10:58 +0300701 return 0;
702}
703
Felipe Balbi8f608e82018-03-27 10:53:29 +0300704static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200705static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300706{
707 struct dwc3_request *req;
708
Felipe Balbi8f608e82018-03-27 10:53:29 +0300709 dwc3_stop_active_transfer(dep, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300710
Felipe Balbi0e146022016-06-21 10:32:02 +0300711 /* - giveback all requests to gadget driver */
712 while (!list_empty(&dep->started_list)) {
713 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200714
Felipe Balbi0e146022016-06-21 10:32:02 +0300715 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200716 }
717
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200718 while (!list_empty(&dep->pending_list)) {
719 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300720
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200721 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300722 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300723}
724
725/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300726 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 * @dep: the endpoint to disable
728 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300729 * This function undoes what __dwc3_gadget_ep_enable did and also removes
730 * requests which are currently being processed by the hardware and those which
731 * are not yet scheduled.
732 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200733 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300734 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300735static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
736{
737 struct dwc3 *dwc = dep->dwc;
738 u32 reg;
739
Felipe Balbi2870e502016-11-03 13:53:29 +0200740 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500741
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200742 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
Felipe Balbi687ef982014-04-16 10:30:33 -0500744 /* make sure HW endpoint isn't stalled */
745 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500746 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500747
Felipe Balbi72246da2011-08-19 18:10:58 +0300748 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
749 reg &= ~DWC3_DALEPENA_EP(dep->number);
750 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
751
Felipe Balbi879631a2011-09-30 10:58:47 +0300752 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800754 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300755
John Youn39ebb052016-11-09 16:36:28 -0800756 /* Clear out the ep descriptors for non-ep0 */
757 if (dep->number > 1) {
758 dep->endpoint.comp_desc = NULL;
759 dep->endpoint.desc = NULL;
760 }
761
Felipe Balbi72246da2011-08-19 18:10:58 +0300762 return 0;
763}
764
765/* -------------------------------------------------------------------------- */
766
767static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
768 const struct usb_endpoint_descriptor *desc)
769{
770 return -EINVAL;
771}
772
773static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
774{
775 return -EINVAL;
776}
777
778/* -------------------------------------------------------------------------- */
779
780static int dwc3_gadget_ep_enable(struct usb_ep *ep,
781 const struct usb_endpoint_descriptor *desc)
782{
783 struct dwc3_ep *dep;
784 struct dwc3 *dwc;
785 unsigned long flags;
786 int ret;
787
788 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
789 pr_debug("dwc3: invalid parameters\n");
790 return -EINVAL;
791 }
792
793 if (!desc->wMaxPacketSize) {
794 pr_debug("dwc3: missing wMaxPacketSize\n");
795 return -EINVAL;
796 }
797
798 dep = to_dwc3_ep(ep);
799 dwc = dep->dwc;
800
Felipe Balbi95ca9612015-12-10 13:08:20 -0600801 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
802 "%s is already enabled\n",
803 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300804 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300805
Felipe Balbi72246da2011-08-19 18:10:58 +0300806 spin_lock_irqsave(&dwc->lock, flags);
John Youn39ebb052016-11-09 16:36:28 -0800807 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300808 spin_unlock_irqrestore(&dwc->lock, flags);
809
810 return ret;
811}
812
813static int dwc3_gadget_ep_disable(struct usb_ep *ep)
814{
815 struct dwc3_ep *dep;
816 struct dwc3 *dwc;
817 unsigned long flags;
818 int ret;
819
820 if (!ep) {
821 pr_debug("dwc3: invalid parameters\n");
822 return -EINVAL;
823 }
824
825 dep = to_dwc3_ep(ep);
826 dwc = dep->dwc;
827
Felipe Balbi95ca9612015-12-10 13:08:20 -0600828 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
829 "%s is already disabled\n",
830 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300831 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300832
Felipe Balbi72246da2011-08-19 18:10:58 +0300833 spin_lock_irqsave(&dwc->lock, flags);
834 ret = __dwc3_gadget_ep_disable(dep);
835 spin_unlock_irqrestore(&dwc->lock, flags);
836
837 return ret;
838}
839
840static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300841 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300842{
843 struct dwc3_request *req;
844 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300845
846 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900847 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300848 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300849
850 req->epnum = dep->number;
851 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300852
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500853 trace_dwc3_alloc_request(req);
854
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 return &req->request;
856}
857
858static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
859 struct usb_request *request)
860{
861 struct dwc3_request *req = to_dwc3_request(request);
862
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500863 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300864 kfree(req);
865}
866
Felipe Balbi2c78c022016-08-12 13:13:10 +0300867static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
868
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200869static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
870 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
871 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200872{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300873 struct dwc3 *dwc = dep->dwc;
874 struct usb_gadget *gadget = &dwc->gadget;
875 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200876
Felipe Balbief966b92016-04-05 13:09:51 +0300877 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530878
Felipe Balbif6bafc62012-02-06 11:04:53 +0200879 trb->size = DWC3_TRB_SIZE_LENGTH(length);
880 trb->bpl = lower_32_bits(dma);
881 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200882
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200883 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200884 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200885 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200886 break;
887
888 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300889 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530890 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300891
Manu Gautam40d829f2017-07-19 17:07:10 +0530892 /*
893 * USB Specification 2.0 Section 5.9.2 states that: "If
894 * there is only a single transaction in the microframe,
895 * only a DATA0 data packet PID is used. If there are
896 * two transactions per microframe, DATA1 is used for
897 * the first transaction data packet and DATA0 is used
898 * for the second transaction data packet. If there are
899 * three transactions per microframe, DATA2 is used for
900 * the first transaction data packet, DATA1 is used for
901 * the second, and DATA0 is used for the third."
902 *
903 * IOW, we should satisfy the following cases:
904 *
905 * 1) length <= maxpacket
906 * - DATA0
907 *
908 * 2) maxpacket < length <= (2 * maxpacket)
909 * - DATA1, DATA0
910 *
911 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
912 * - DATA2, DATA1, DATA0
913 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300914 if (speed == USB_SPEED_HIGH) {
915 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530916 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530917 unsigned int maxp = usb_endpoint_maxp(ep->desc);
918
919 if (length <= (2 * maxp))
920 mult--;
921
922 if (length <= maxp)
923 mult--;
924
925 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300926 }
927 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530928 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300929 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200930
931 /* always enable Interrupt on Missed ISOC */
932 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200933 break;
934
935 case USB_ENDPOINT_XFER_BULK:
936 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200937 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200938 break;
939 default:
940 /*
941 * This is only possible with faulty memory because we
942 * checked it already :)
943 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300944 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
945 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200946 }
947
Felipe Balbica4d44e2016-03-10 13:53:27 +0200948 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300949 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300950 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600951
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200952 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300953 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
954 }
955
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200956 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300957 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300958 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200959
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530960 if (chain)
961 trb->ctrl |= DWC3_TRB_CTRL_CHN;
962
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200963 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200964 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200965
966 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500967
968 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200969}
970
John Youn361572b2016-05-19 17:26:17 -0700971/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200972 * dwc3_prepare_one_trb - setup one TRB from one request
973 * @dep: endpoint for which this request is prepared
974 * @req: dwc3_request pointer
975 * @chain: should this TRB be chained to the next?
976 * @node: only for isochronous endpoints. First TRB needs different type.
977 */
978static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
979 struct dwc3_request *req, unsigned chain, unsigned node)
980{
981 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +0530982 unsigned int length;
983 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200984 unsigned stream_id = req->request.stream_id;
985 unsigned short_not_ok = req->request.short_not_ok;
986 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +0530987
988 if (req->request.num_sgs > 0) {
989 length = sg_dma_len(req->start_sg);
990 dma = sg_dma_address(req->start_sg);
991 } else {
992 length = req->request.length;
993 dma = req->request.dma;
994 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200995
996 trb = &dep->trb_pool[dep->trb_enqueue];
997
998 if (!req->trb) {
999 dwc3_gadget_move_started_request(req);
1000 req->trb = trb;
1001 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +02001002 }
1003
1004 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1005 stream_id, short_not_ok, no_interrupt);
1006}
1007
1008/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001009 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -07001010 * @dep: The endpoint with the TRB ring
1011 * @index: The index of the current TRB in the ring
1012 *
1013 * Returns the TRB prior to the one pointed to by the index. If the
1014 * index is 0, we will wrap backwards, skip the link TRB, and return
1015 * the one just before that.
1016 */
1017static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1018{
Felipe Balbi45438a02016-08-11 12:26:59 +03001019 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001020
Felipe Balbi45438a02016-08-11 12:26:59 +03001021 if (!tmp)
1022 tmp = DWC3_TRB_NUM - 1;
1023
1024 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001025}
1026
Felipe Balbic4233572016-05-12 14:08:34 +03001027static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1028{
1029 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001030 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001031
1032 /*
1033 * If enqueue & dequeue are equal than it is either full or empty.
1034 *
1035 * One way to know for sure is if the TRB right before us has HWO bit
1036 * set or not. If it has, then we're definitely full and can't fit any
1037 * more transfers in our ring.
1038 */
1039 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001040 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001041 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001042 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001043
1044 return DWC3_TRB_NUM - 1;
1045 }
1046
John Youn9d7aba72016-08-26 18:43:01 -07001047 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001048 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001049
John Youn9d7aba72016-08-26 18:43:01 -07001050 if (dep->trb_dequeue < dep->trb_enqueue)
1051 trbs_left--;
1052
John Youn32db3d92016-05-19 17:26:12 -07001053 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001054}
1055
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001056static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001057 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001058{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301059 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001060 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001061 int i;
1062
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301063 unsigned int remaining = req->request.num_mapped_sgs
1064 - req->num_queued_sgs;
1065
1066 for_each_sg(sg, s, remaining, i) {
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;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001070 unsigned chain = true;
1071
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001072 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001073 chain = false;
1074
Felipe Balbic6267a52017-01-05 14:58:46 +02001075 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1076 struct dwc3 *dwc = dep->dwc;
1077 struct dwc3_trb *trb;
1078
1079 req->unaligned = true;
1080
1081 /* prepare normal TRB */
1082 dwc3_prepare_one_trb(dep, req, true, i);
1083
1084 /* Now prepare one extra TRB to align transfer size */
1085 trb = &dep->trb_pool[dep->trb_enqueue];
1086 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1087 maxp - rem, false, 0,
1088 req->request.stream_id,
1089 req->request.short_not_ok,
1090 req->request.no_interrupt);
1091 } else {
1092 dwc3_prepare_one_trb(dep, req, chain, i);
1093 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001094
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301095 /*
1096 * There can be a situation where all sgs in sglist are not
1097 * queued because of insufficient trb number. To handle this
1098 * case, update start_sg to next sg to be queued, so that
1099 * we have free trbs we can continue queuing from where we
1100 * previously stopped
1101 */
1102 if (chain)
1103 req->start_sg = sg_next(s);
1104
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301105 req->num_queued_sgs++;
1106
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001107 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001108 break;
1109 }
1110}
1111
1112static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001113 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001114{
Felipe Balbic6267a52017-01-05 14:58:46 +02001115 unsigned int length = req->request.length;
1116 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1117 unsigned int rem = length % maxp;
1118
1119 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1120 struct dwc3 *dwc = dep->dwc;
1121 struct dwc3_trb *trb;
1122
1123 req->unaligned = true;
1124
1125 /* prepare normal TRB */
1126 dwc3_prepare_one_trb(dep, req, true, 0);
1127
1128 /* Now prepare one extra TRB to align transfer size */
1129 trb = &dep->trb_pool[dep->trb_enqueue];
1130 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1131 false, 0, req->request.stream_id,
1132 req->request.short_not_ok,
1133 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001134 } else if (req->request.zero && req->request.length &&
1135 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1136 struct dwc3 *dwc = dep->dwc;
1137 struct dwc3_trb *trb;
1138
1139 req->zero = true;
1140
1141 /* prepare normal TRB */
1142 dwc3_prepare_one_trb(dep, req, true, 0);
1143
1144 /* Now prepare one extra TRB to handle ZLP */
1145 trb = &dep->trb_pool[dep->trb_enqueue];
1146 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1147 false, 0, req->request.stream_id,
1148 req->request.short_not_ok,
1149 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001150 } else {
1151 dwc3_prepare_one_trb(dep, req, false, 0);
1152 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001153}
1154
Felipe Balbi72246da2011-08-19 18:10:58 +03001155/*
1156 * dwc3_prepare_trbs - setup TRBs from requests
1157 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001158 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001159 * The function goes through the requests list and sets up TRBs for the
1160 * transfers. The function returns once there are no more TRBs available or
1161 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001162 */
Felipe Balbic4233572016-05-12 14:08:34 +03001163static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001164{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001165 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001166
1167 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1168
Felipe Balbid86c5a62016-10-25 13:48:52 +03001169 /*
1170 * We can get in a situation where there's a request in the started list
1171 * but there weren't enough TRBs to fully kick it in the first time
1172 * around, so it has been waiting for more TRBs to be freed up.
1173 *
1174 * In that case, we should check if we have a request with pending_sgs
1175 * in the started list and prepare TRBs for that request first,
1176 * otherwise we will prepare TRBs completely out of order and that will
1177 * break things.
1178 */
1179 list_for_each_entry(req, &dep->started_list, list) {
1180 if (req->num_pending_sgs > 0)
1181 dwc3_prepare_one_trb_sg(dep, req);
1182
1183 if (!dwc3_calc_trbs_left(dep))
1184 return;
1185 }
1186
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001187 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001188 struct dwc3 *dwc = dep->dwc;
1189 int ret;
1190
1191 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1192 dep->direction);
1193 if (ret)
1194 return;
1195
1196 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301197 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301198 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001199 req->num_pending_sgs = req->request.num_mapped_sgs;
1200
Felipe Balbi1f512112016-08-12 13:17:27 +03001201 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001202 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001203 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001204 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001205
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001206 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001207 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001208 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001209}
1210
Felipe Balbi7fdca762017-09-05 14:41:34 +03001211static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001212{
1213 struct dwc3_gadget_ep_cmd_params params;
1214 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001215 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001216 int ret;
1217 u32 cmd;
1218
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001219 if (!dwc3_calc_trbs_left(dep))
1220 return 0;
1221
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001222 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001223
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001224 dwc3_prepare_trbs(dep);
1225 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001226 if (!req) {
1227 dep->flags |= DWC3_EP_PENDING_REQUEST;
1228 return 0;
1229 }
1230
1231 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001232
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001233 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301234 params.param0 = upper_32_bits(req->trb_dma);
1235 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001236 cmd = DWC3_DEPCMD_STARTTRANSFER;
1237
1238 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1239 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301240 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001241 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1242 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301243 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001244
Felipe Balbi2cd47182016-04-12 16:42:43 +03001245 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001246 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001247 /*
1248 * FIXME we need to iterate over the list of requests
1249 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001250 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001251 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001252 if (req->trb)
1253 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001254 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001255 return ret;
1256 }
1257
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001258 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001259 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001260 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001261 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001262
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 return 0;
1264}
1265
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001266static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1267{
1268 u32 reg;
1269
1270 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1271 return DWC3_DSTS_SOFFN(reg);
1272}
1273
Felipe Balbi5828cad2018-03-27 11:14:31 +03001274static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301275{
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001276 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001277 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001278 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301279 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301280 return;
1281 }
1282
John Younaf771d72017-01-26 11:58:40 -08001283 /*
1284 * Schedule the first trb for one interval in the future or at
1285 * least 4 microframes.
1286 */
Felipe Balbi5828cad2018-03-27 11:14:31 +03001287 dep->frame_number += max_t(u32, 4, dep->interval);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001288 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301289}
1290
Felipe Balbi72246da2011-08-19 18:10:58 +03001291static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1292{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001293 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001294
Felipe Balbibb423982015-11-16 15:31:21 -06001295 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001296 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1297 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001298 return -ESHUTDOWN;
1299 }
1300
Felipe Balbi04fb3652017-05-17 15:57:45 +03001301 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1302 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001303 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001304
Felipe Balbifc8bb912016-05-16 13:14:48 +03001305 pm_runtime_get(dwc->dev);
1306
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 req->request.actual = 0;
1308 req->request.status = -EINPROGRESS;
1309 req->direction = dep->direction;
1310 req->epnum = dep->number;
1311
Felipe Balbife84f522015-09-01 09:01:38 -05001312 trace_dwc3_ep_queue(req);
1313
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001314 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001315
Felipe Balbid889c232016-09-29 15:44:29 +03001316 /*
1317 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1318 * wait for a XferNotReady event so we will know what's the current
1319 * (micro-)frame number.
1320 *
1321 * Without this trick, we are very, very likely gonna get Bus Expiry
1322 * errors which will force us issue EndTransfer command.
1323 */
1324 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001325 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1326 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001327 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001328
1329 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1330 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1331 __dwc3_gadget_start_isoc(dep);
1332 return 0;
1333 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001334 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001335 }
1336
Felipe Balbi7fdca762017-09-05 14:41:34 +03001337 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001338}
1339
1340static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1341 gfp_t gfp_flags)
1342{
1343 struct dwc3_request *req = to_dwc3_request(request);
1344 struct dwc3_ep *dep = to_dwc3_ep(ep);
1345 struct dwc3 *dwc = dep->dwc;
1346
1347 unsigned long flags;
1348
1349 int ret;
1350
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001351 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 ret = __dwc3_gadget_ep_queue(dep, req);
1353 spin_unlock_irqrestore(&dwc->lock, flags);
1354
1355 return ret;
1356}
1357
1358static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1359 struct usb_request *request)
1360{
1361 struct dwc3_request *req = to_dwc3_request(request);
1362 struct dwc3_request *r = NULL;
1363
1364 struct dwc3_ep *dep = to_dwc3_ep(ep);
1365 struct dwc3 *dwc = dep->dwc;
1366
1367 unsigned long flags;
1368 int ret = 0;
1369
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001370 trace_dwc3_ep_dequeue(req);
1371
Felipe Balbi72246da2011-08-19 18:10:58 +03001372 spin_lock_irqsave(&dwc->lock, flags);
1373
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001374 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 if (r == req)
1376 break;
1377 }
1378
1379 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001380 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001381 if (r == req)
1382 break;
1383 }
1384 if (r == req) {
1385 /* wait until it is processed */
Felipe Balbi8f608e82018-03-27 10:53:29 +03001386 dwc3_stop_active_transfer(dep, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001387
1388 /*
1389 * If request was already started, this means we had to
1390 * stop the transfer. With that we also need to ignore
1391 * all TRBs used by the request, however TRBs can only
1392 * be modified after completion of END_TRANSFER
1393 * command. So what we do here is that we wait for
1394 * END_TRANSFER completion and only after that, we jump
1395 * over TRBs by clearing HWO and incrementing dequeue
1396 * pointer.
1397 *
1398 * Note that we have 2 possible types of transfers here:
1399 *
1400 * i) Linear buffer request
1401 * ii) SG-list based request
1402 *
1403 * SG-list based requests will have r->num_pending_sgs
1404 * set to a valid number (> 0). Linear requests,
1405 * normally use a single TRB.
1406 *
1407 * For each of these two cases, if r->unaligned flag is
1408 * set, one extra TRB has been used to align transfer
1409 * size to wMaxPacketSize.
1410 *
1411 * All of these cases need to be taken into
1412 * consideration so we don't mess up our TRB ring
1413 * pointers.
1414 */
1415 wait_event_lock_irq(dep->wait_end_transfer,
1416 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1417 dwc->lock);
1418
1419 if (!r->trb)
1420 goto out1;
1421
1422 if (r->num_pending_sgs) {
1423 struct dwc3_trb *trb;
1424 int i = 0;
1425
1426 for (i = 0; i < r->num_pending_sgs; i++) {
1427 trb = r->trb + i;
1428 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1429 dwc3_ep_inc_deq(dep);
1430 }
1431
Felipe Balbid6e5a542017-04-07 16:34:38 +03001432 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001433 trb = r->trb + r->num_pending_sgs + 1;
1434 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1435 dwc3_ep_inc_deq(dep);
1436 }
1437 } else {
1438 struct dwc3_trb *trb = r->trb;
1439
1440 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1441 dwc3_ep_inc_deq(dep);
1442
Felipe Balbid6e5a542017-04-07 16:34:38 +03001443 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001444 trb = r->trb + 1;
1445 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1446 dwc3_ep_inc_deq(dep);
1447 }
1448 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301449 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001451 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 request, ep->name);
1453 ret = -EINVAL;
1454 goto out0;
1455 }
1456
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301457out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001458 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001459
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1461
1462out0:
1463 spin_unlock_irqrestore(&dwc->lock, flags);
1464
1465 return ret;
1466}
1467
Felipe Balbi7a608552014-09-24 14:19:52 -05001468int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001469{
1470 struct dwc3_gadget_ep_cmd_params params;
1471 struct dwc3 *dwc = dep->dwc;
1472 int ret;
1473
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001474 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1475 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1476 return -EINVAL;
1477 }
1478
Felipe Balbi72246da2011-08-19 18:10:58 +03001479 memset(&params, 0x00, sizeof(params));
1480
1481 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001482 struct dwc3_trb *trb;
1483
1484 unsigned transfer_in_flight;
1485 unsigned started;
1486
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001487 if (dep->flags & DWC3_EP_STALL)
1488 return 0;
1489
Felipe Balbi69450c42016-05-30 13:37:02 +03001490 if (dep->number > 1)
1491 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1492 else
1493 trb = &dwc->ep0_trb[dep->trb_enqueue];
1494
1495 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1496 started = !list_empty(&dep->started_list);
1497
1498 if (!protocol && ((dep->direction && transfer_in_flight) ||
1499 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001500 return -EAGAIN;
1501 }
1502
Felipe Balbi2cd47182016-04-12 16:42:43 +03001503 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1504 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001505 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001506 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001507 dep->name);
1508 else
1509 dep->flags |= DWC3_EP_STALL;
1510 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001511 if (!(dep->flags & DWC3_EP_STALL))
1512 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001513
John Youn50c763f2016-05-31 17:49:56 -07001514 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001516 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001517 dep->name);
1518 else
Alan Sterna535d812013-11-01 12:05:12 -04001519 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001520 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001521
Felipe Balbi72246da2011-08-19 18:10:58 +03001522 return ret;
1523}
1524
1525static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1526{
1527 struct dwc3_ep *dep = to_dwc3_ep(ep);
1528 struct dwc3 *dwc = dep->dwc;
1529
1530 unsigned long flags;
1531
1532 int ret;
1533
1534 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001535 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 spin_unlock_irqrestore(&dwc->lock, flags);
1537
1538 return ret;
1539}
1540
1541static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1542{
1543 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001544 struct dwc3 *dwc = dep->dwc;
1545 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001546 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001547
Paul Zimmerman249a4562012-02-24 17:32:16 -08001548 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001549 dep->flags |= DWC3_EP_WEDGE;
1550
Pratyush Anand08f0d962012-06-25 22:40:43 +05301551 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001552 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301553 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001554 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001555 spin_unlock_irqrestore(&dwc->lock, flags);
1556
1557 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558}
1559
1560/* -------------------------------------------------------------------------- */
1561
1562static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1563 .bLength = USB_DT_ENDPOINT_SIZE,
1564 .bDescriptorType = USB_DT_ENDPOINT,
1565 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1566};
1567
1568static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1569 .enable = dwc3_gadget_ep0_enable,
1570 .disable = dwc3_gadget_ep0_disable,
1571 .alloc_request = dwc3_gadget_ep_alloc_request,
1572 .free_request = dwc3_gadget_ep_free_request,
1573 .queue = dwc3_gadget_ep0_queue,
1574 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301575 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001576 .set_wedge = dwc3_gadget_ep_set_wedge,
1577};
1578
1579static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1580 .enable = dwc3_gadget_ep_enable,
1581 .disable = dwc3_gadget_ep_disable,
1582 .alloc_request = dwc3_gadget_ep_alloc_request,
1583 .free_request = dwc3_gadget_ep_free_request,
1584 .queue = dwc3_gadget_ep_queue,
1585 .dequeue = dwc3_gadget_ep_dequeue,
1586 .set_halt = dwc3_gadget_ep_set_halt,
1587 .set_wedge = dwc3_gadget_ep_set_wedge,
1588};
1589
1590/* -------------------------------------------------------------------------- */
1591
1592static int dwc3_gadget_get_frame(struct usb_gadget *g)
1593{
1594 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001595
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001596 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001597}
1598
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001599static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001600{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001601 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001602
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001603 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001604 u32 reg;
1605
Felipe Balbi72246da2011-08-19 18:10:58 +03001606 u8 link_state;
1607 u8 speed;
1608
Felipe Balbi72246da2011-08-19 18:10:58 +03001609 /*
1610 * According to the Databook Remote wakeup request should
1611 * be issued only when the device is in early suspend state.
1612 *
1613 * We can check that via USB Link State bits in DSTS register.
1614 */
1615 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1616
1617 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001618 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001619 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001620 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001621
1622 link_state = DWC3_DSTS_USBLNKST(reg);
1623
1624 switch (link_state) {
1625 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1626 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1627 break;
1628 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001629 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 }
1631
Felipe Balbi8598bde2012-01-02 18:55:57 +02001632 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1633 if (ret < 0) {
1634 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001635 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001636 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001637
Paul Zimmerman802fde92012-04-27 13:10:52 +03001638 /* Recent versions do this automatically */
1639 if (dwc->revision < DWC3_REVISION_194A) {
1640 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001641 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001642 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1643 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1644 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001645
Paul Zimmerman1d046792012-02-15 18:56:56 -08001646 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001647 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001648
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001649 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1651
1652 /* in HS, means ON */
1653 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1654 break;
1655 }
1656
1657 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1658 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001659 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 }
1661
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001662 return 0;
1663}
1664
1665static int dwc3_gadget_wakeup(struct usb_gadget *g)
1666{
1667 struct dwc3 *dwc = gadget_to_dwc(g);
1668 unsigned long flags;
1669 int ret;
1670
1671 spin_lock_irqsave(&dwc->lock, flags);
1672 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001673 spin_unlock_irqrestore(&dwc->lock, flags);
1674
1675 return ret;
1676}
1677
1678static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1679 int is_selfpowered)
1680{
1681 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001682 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001683
Paul Zimmerman249a4562012-02-24 17:32:16 -08001684 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001685 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001686 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001687
1688 return 0;
1689}
1690
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001691static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001692{
1693 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001694 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001695
Felipe Balbifc8bb912016-05-16 13:14:48 +03001696 if (pm_runtime_suspended(dwc->dev))
1697 return 0;
1698
Felipe Balbi72246da2011-08-19 18:10:58 +03001699 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001700 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001701 if (dwc->revision <= DWC3_REVISION_187A) {
1702 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1703 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1704 }
1705
1706 if (dwc->revision >= DWC3_REVISION_194A)
1707 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1708 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001709
1710 if (dwc->has_hibernation)
1711 reg |= DWC3_DCTL_KEEP_CONNECT;
1712
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001713 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001714 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001716
1717 if (dwc->has_hibernation && !suspend)
1718 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1719
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001720 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001721 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001722
1723 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1724
1725 do {
1726 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001727 reg &= DWC3_DSTS_DEVCTRLHLT;
1728 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001729
1730 if (!timeout)
1731 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001732
Pratyush Anand6f17f742012-07-02 10:21:55 +05301733 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001734}
1735
1736static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1737{
1738 struct dwc3 *dwc = gadget_to_dwc(g);
1739 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301740 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741
1742 is_on = !!is_on;
1743
Baolin Wangbb014732016-10-14 17:11:33 +08001744 /*
1745 * Per databook, when we want to stop the gadget, if a control transfer
1746 * is still in process, complete it and get the core into setup phase.
1747 */
1748 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1749 reinit_completion(&dwc->ep0_in_setup);
1750
1751 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1752 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1753 if (ret == 0) {
1754 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1755 return -ETIMEDOUT;
1756 }
1757 }
1758
Felipe Balbi72246da2011-08-19 18:10:58 +03001759 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001760 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001761 spin_unlock_irqrestore(&dwc->lock, flags);
1762
Pratyush Anand6f17f742012-07-02 10:21:55 +05301763 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001764}
1765
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001766static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1767{
1768 u32 reg;
1769
1770 /* Enable all but Start and End of Frame IRQs */
1771 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1772 DWC3_DEVTEN_EVNTOVERFLOWEN |
1773 DWC3_DEVTEN_CMDCMPLTEN |
1774 DWC3_DEVTEN_ERRTICERREN |
1775 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001776 DWC3_DEVTEN_CONNECTDONEEN |
1777 DWC3_DEVTEN_USBRSTEN |
1778 DWC3_DEVTEN_DISCONNEVTEN);
1779
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001780 if (dwc->revision < DWC3_REVISION_250A)
1781 reg |= DWC3_DEVTEN_ULSTCNGEN;
1782
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001783 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1784}
1785
1786static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1787{
1788 /* mask all interrupts */
1789 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1790}
1791
1792static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001793static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001794
Felipe Balbi4e994722016-05-13 14:09:59 +03001795/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001796 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1797 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001798 *
1799 * The following looks like complex but it's actually very simple. In order to
1800 * calculate the number of packets we can burst at once on OUT transfers, we're
1801 * gonna use RxFIFO size.
1802 *
1803 * To calculate RxFIFO size we need two numbers:
1804 * MDWIDTH = size, in bits, of the internal memory bus
1805 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1806 *
1807 * Given these two numbers, the formula is simple:
1808 *
1809 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1810 *
1811 * 24 bytes is for 3x SETUP packets
1812 * 16 bytes is a clock domain crossing tolerance
1813 *
1814 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1815 */
1816static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1817{
1818 u32 ram2_depth;
1819 u32 mdwidth;
1820 u32 nump;
1821 u32 reg;
1822
1823 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1824 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1825
1826 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1827 nump = min_t(u32, nump, 16);
1828
1829 /* update NumP */
1830 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1831 reg &= ~DWC3_DCFG_NUMP_MASK;
1832 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1833 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1834}
1835
Felipe Balbid7be2952016-05-04 15:49:37 +03001836static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001837{
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001839 int ret = 0;
1840 u32 reg;
1841
John Youncf40b862016-11-14 12:32:43 -08001842 /*
1843 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1844 * the core supports IMOD, disable it.
1845 */
1846 if (dwc->imod_interval) {
1847 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1848 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1849 } else if (dwc3_has_imod(dwc)) {
1850 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1851 }
1852
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001853 /*
1854 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1855 * field instead of letting dwc3 itself calculate that automatically.
1856 *
1857 * This way, we maximize the chances that we'll be able to get several
1858 * bursts of data without going through any sort of endpoint throttling.
1859 */
1860 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001861 if (dwc3_is_usb31(dwc))
1862 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1863 else
1864 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1865
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001866 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1867
Felipe Balbi4e994722016-05-13 14:09:59 +03001868 dwc3_gadget_setup_nump(dwc);
1869
Felipe Balbi72246da2011-08-19 18:10:58 +03001870 /* Start with SuperSpeed Default */
1871 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1872
1873 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08001874 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001875 if (ret) {
1876 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001877 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 }
1879
1880 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08001881 ret = __dwc3_gadget_ep_enable(dep, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001882 if (ret) {
1883 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001884 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001885 }
1886
1887 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001888 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 dwc3_ep0_out_start(dwc);
1890
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001891 dwc3_gadget_enable_irq(dwc);
1892
Felipe Balbid7be2952016-05-04 15:49:37 +03001893 return 0;
1894
1895err1:
1896 __dwc3_gadget_ep_disable(dwc->eps[0]);
1897
1898err0:
1899 return ret;
1900}
1901
1902static int dwc3_gadget_start(struct usb_gadget *g,
1903 struct usb_gadget_driver *driver)
1904{
1905 struct dwc3 *dwc = gadget_to_dwc(g);
1906 unsigned long flags;
1907 int ret = 0;
1908 int irq;
1909
Roger Quadros9522def2016-06-10 14:48:38 +03001910 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001911 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1912 IRQF_SHARED, "dwc3", dwc->ev_buf);
1913 if (ret) {
1914 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1915 irq, ret);
1916 goto err0;
1917 }
1918
1919 spin_lock_irqsave(&dwc->lock, flags);
1920 if (dwc->gadget_driver) {
1921 dev_err(dwc->dev, "%s is already bound to %s\n",
1922 dwc->gadget.name,
1923 dwc->gadget_driver->driver.name);
1924 ret = -EBUSY;
1925 goto err1;
1926 }
1927
1928 dwc->gadget_driver = driver;
1929
Felipe Balbifc8bb912016-05-16 13:14:48 +03001930 if (pm_runtime_active(dwc->dev))
1931 __dwc3_gadget_start(dwc);
1932
Felipe Balbi72246da2011-08-19 18:10:58 +03001933 spin_unlock_irqrestore(&dwc->lock, flags);
1934
1935 return 0;
1936
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001937err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001938 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001939 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001940
1941err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001942 return ret;
1943}
1944
Felipe Balbid7be2952016-05-04 15:49:37 +03001945static void __dwc3_gadget_stop(struct dwc3 *dwc)
1946{
1947 dwc3_gadget_disable_irq(dwc);
1948 __dwc3_gadget_ep_disable(dwc->eps[0]);
1949 __dwc3_gadget_ep_disable(dwc->eps[1]);
1950}
1951
Felipe Balbi22835b82014-10-17 12:05:12 -05001952static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001953{
1954 struct dwc3 *dwc = gadget_to_dwc(g);
1955 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001956 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02001957 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001958
1959 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001960
1961 if (pm_runtime_suspended(dwc->dev))
1962 goto out;
1963
Felipe Balbid7be2952016-05-04 15:49:37 +03001964 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001965
1966 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1967 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02001968 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08001969
1970 if (!dep)
1971 continue;
1972
1973 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1974 continue;
1975
Roger Quadros498f0472018-03-09 14:47:04 +02001976 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
1977 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1978 dwc->lock, msecs_to_jiffies(5));
1979
1980 if (ret <= 0) {
1981 /* Timed out or interrupted! There's nothing much
1982 * we can do so we just log here and print which
1983 * endpoints timed out at the end.
1984 */
1985 tmo_eps |= 1 << epnum;
1986 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
1987 }
1988 }
1989
1990 if (tmo_eps) {
1991 dev_err(dwc->dev,
1992 "end transfer timed out on endpoints 0x%x [bitmap]\n",
1993 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08001994 }
1995
1996out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001997 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998 spin_unlock_irqrestore(&dwc->lock, flags);
1999
Felipe Balbi3f308d12016-05-16 14:17:06 +03002000 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002001
Felipe Balbi72246da2011-08-19 18:10:58 +03002002 return 0;
2003}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002004
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002005static void dwc3_gadget_set_speed(struct usb_gadget *g,
2006 enum usb_device_speed speed)
2007{
2008 struct dwc3 *dwc = gadget_to_dwc(g);
2009 unsigned long flags;
2010 u32 reg;
2011
2012 spin_lock_irqsave(&dwc->lock, flags);
2013 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2014 reg &= ~(DWC3_DCFG_SPEED_MASK);
2015
2016 /*
2017 * WORKAROUND: DWC3 revision < 2.20a have an issue
2018 * which would cause metastability state on Run/Stop
2019 * bit if we try to force the IP to USB2-only mode.
2020 *
2021 * Because of that, we cannot configure the IP to any
2022 * speed other than the SuperSpeed
2023 *
2024 * Refers to:
2025 *
2026 * STAR#9000525659: Clock Domain Crossing on DCTL in
2027 * USB 2.0 Mode
2028 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002029 if (dwc->revision < DWC3_REVISION_220A &&
2030 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002031 reg |= DWC3_DCFG_SUPERSPEED;
2032 } else {
2033 switch (speed) {
2034 case USB_SPEED_LOW:
2035 reg |= DWC3_DCFG_LOWSPEED;
2036 break;
2037 case USB_SPEED_FULL:
2038 reg |= DWC3_DCFG_FULLSPEED;
2039 break;
2040 case USB_SPEED_HIGH:
2041 reg |= DWC3_DCFG_HIGHSPEED;
2042 break;
2043 case USB_SPEED_SUPER:
2044 reg |= DWC3_DCFG_SUPERSPEED;
2045 break;
2046 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002047 if (dwc3_is_usb31(dwc))
2048 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2049 else
2050 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002051 break;
2052 default:
2053 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2054
2055 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2056 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2057 else
2058 reg |= DWC3_DCFG_SUPERSPEED;
2059 }
2060 }
2061 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2062
2063 spin_unlock_irqrestore(&dwc->lock, flags);
2064}
2065
Felipe Balbi72246da2011-08-19 18:10:58 +03002066static const struct usb_gadget_ops dwc3_gadget_ops = {
2067 .get_frame = dwc3_gadget_get_frame,
2068 .wakeup = dwc3_gadget_wakeup,
2069 .set_selfpowered = dwc3_gadget_set_selfpowered,
2070 .pullup = dwc3_gadget_pullup,
2071 .udc_start = dwc3_gadget_start,
2072 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002073 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002074};
2075
2076/* -------------------------------------------------------------------------- */
2077
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002078static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
Felipe Balbi72246da2011-08-19 18:10:58 +03002079{
2080 struct dwc3_ep *dep;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002081 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002082
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002083 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2084
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002085 for (epnum = 0; epnum < total; epnum++) {
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002086 bool direction = epnum & 1;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002087 u8 num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002088
Felipe Balbi72246da2011-08-19 18:10:58 +03002089 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002090 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002091 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002092
2093 dep->dwc = dwc;
2094 dep->number = epnum;
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002095 dep->direction = direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002096 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002097 dwc->eps[epnum] = dep;
2098
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002099 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002100 direction ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002101
Felipe Balbi72246da2011-08-19 18:10:58 +03002102 dep->endpoint.name = dep->name;
John Youn39ebb052016-11-09 16:36:28 -08002103
2104 if (!(dep->number > 1)) {
2105 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2106 dep->endpoint.comp_desc = NULL;
2107 }
2108
Felipe Balbi74674cb2016-04-13 16:44:39 +03002109 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002110
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002111 if (num == 0) {
Robert Baldygae117e742013-12-13 12:23:38 +01002112 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302113 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002115 if (!direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002116 dwc->gadget.ep0 = &dep->endpoint;
Felipe Balbi28781782017-01-23 18:01:59 +02002117 } else if (direction) {
2118 int mdwidth;
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002119 int kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002120 int size;
2121 int ret;
Felipe Balbi28781782017-01-23 18:01:59 +02002122
2123 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2124 /* MDWIDTH is represented in bits, we need it in bytes */
2125 mdwidth /= 8;
2126
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002127 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
Thinh Nguyend548a612018-03-16 15:34:00 -07002128 if (dwc3_is_usb31(dwc))
2129 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2130 else
2131 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
Felipe Balbi28781782017-01-23 18:01:59 +02002132
2133 /* FIFO Depth is in MDWDITH bytes. Multiply */
2134 size *= mdwidth;
2135
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002136 kbytes = size / 1024;
2137 if (kbytes == 0)
2138 kbytes = 1;
Felipe Balbi28781782017-01-23 18:01:59 +02002139
2140 /*
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002141 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
Felipe Balbi28781782017-01-23 18:01:59 +02002142 * internal overhead. We don't really know how these are used,
2143 * but documentation say it exists.
2144 */
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002145 size -= mdwidth * (kbytes + 1);
2146 size /= kbytes;
Felipe Balbi28781782017-01-23 18:01:59 +02002147
2148 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2149
2150 dep->endpoint.max_streams = 15;
2151 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2152 list_add_tail(&dep->endpoint.ep_list,
2153 &dwc->gadget.ep_list);
2154
2155 ret = dwc3_alloc_trb_pool(dep);
2156 if (ret)
2157 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002158 } else {
2159 int ret;
2160
Robert Baldygae117e742013-12-13 12:23:38 +01002161 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002162 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002163 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2164 list_add_tail(&dep->endpoint.ep_list,
2165 &dwc->gadget.ep_list);
2166
2167 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002168 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002169 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002170 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002171
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002172 if (num == 0) {
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002173 dep->endpoint.caps.type_control = true;
2174 } else {
2175 dep->endpoint.caps.type_iso = true;
2176 dep->endpoint.caps.type_bulk = true;
2177 dep->endpoint.caps.type_int = true;
2178 }
2179
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002180 dep->endpoint.caps.dir_in = direction;
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002181 dep->endpoint.caps.dir_out = !direction;
2182
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002183 INIT_LIST_HEAD(&dep->pending_list);
2184 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002185 }
2186
2187 return 0;
2188}
2189
2190static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2191{
2192 struct dwc3_ep *dep;
2193 u8 epnum;
2194
2195 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2196 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002197 if (!dep)
2198 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302199 /*
2200 * Physical endpoints 0 and 1 are special; they form the
2201 * bi-directional USB endpoint 0.
2202 *
2203 * For those two physical endpoints, we don't allocate a TRB
2204 * pool nor do we add them the endpoints list. Due to that, we
2205 * shouldn't do these two operations otherwise we would end up
2206 * with all sorts of bugs when removing dwc3.ko.
2207 */
2208 if (epnum != 0 && epnum != 1) {
2209 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002210 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302211 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002212
2213 kfree(dep);
2214 }
2215}
2216
Felipe Balbi72246da2011-08-19 18:10:58 +03002217/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002218
Felipe Balbi8f608e82018-03-27 10:53:29 +03002219static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2220 struct dwc3_request *req, struct dwc3_trb *trb,
2221 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302222{
2223 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302224
Felipe Balbidc55c672016-08-12 13:20:32 +03002225 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002226
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002227 trace_dwc3_complete_trb(dep, trb);
2228
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002229 /*
2230 * If we're in the middle of series of chained TRBs and we
2231 * receive a short transfer along the way, DWC3 will skip
2232 * through all TRBs including the last TRB in the chain (the
2233 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2234 * bit and SW has to do it manually.
2235 *
2236 * We're going to do that here to avoid problems of HW trying
2237 * to use bogus TRBs for transfers.
2238 */
2239 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2240 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2241
Felipe Balbic6267a52017-01-05 14:58:46 +02002242 /*
2243 * If we're dealing with unaligned size OUT transfer, we will be left
2244 * with one TRB pending in the ring. We need to manually clear HWO bit
2245 * from that TRB.
2246 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002247 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002248 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2249 return 1;
2250 }
2251
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302252 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002253 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302254
Felipe Balbi35b27192017-03-08 13:56:37 +02002255 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2256 return 1;
2257
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002258 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302259 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002260
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002261 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302262 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002263
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302264 return 0;
2265}
2266
Felipe Balbid3692952018-03-29 13:32:10 +03002267static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2268 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2269 int status)
2270{
2271 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2272 struct scatterlist *sg = req->sg;
2273 struct scatterlist *s;
2274 unsigned int pending = req->num_pending_sgs;
2275 unsigned int i;
2276 int ret = 0;
2277
2278 for_each_sg(sg, s, pending, i) {
2279 trb = &dep->trb_pool[dep->trb_dequeue];
2280
2281 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2282 break;
2283
2284 req->sg = sg_next(s);
2285 req->num_pending_sgs--;
2286
2287 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2288 trb, event, status, true);
2289 if (ret)
2290 break;
2291 }
2292
2293 return ret;
2294}
2295
2296static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2297 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2298 int status)
2299{
2300 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2301
2302 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2303 event, status, false);
2304}
2305
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002306static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2307{
2308 return req->request.actual == req->request.length;
2309}
2310
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002311static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002312 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002313{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002314 struct dwc3_request *req;
2315 struct dwc3_request *tmp;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002316 int ret = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002317
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002318 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002319 unsigned length;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002320
Felipe Balbi1f512112016-08-12 13:17:27 +03002321 length = req->request.length;
Felipe Balbicf35fc32018-03-29 14:49:36 +03002322 if (req->num_pending_sgs)
Felipe Balbid3692952018-03-29 13:32:10 +03002323 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2324 status);
2325 else
2326 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2327 status);
Ville Syrjäläd115d702015-08-31 19:48:28 +03002328
Felipe Balbid6e5a542017-04-07 16:34:38 +03002329 if (req->unaligned || req->zero) {
Felipe Balbi8b3b7b662018-03-29 15:30:44 +03002330 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2331 status);
Felipe Balbic6267a52017-01-05 14:58:46 +02002332 req->unaligned = false;
Felipe Balbid6e5a542017-04-07 16:34:38 +03002333 req->zero = false;
Felipe Balbic6267a52017-01-05 14:58:46 +02002334 }
2335
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002336 req->request.actual = length - req->remaining;
Felipe Balbi1f512112016-08-12 13:17:27 +03002337
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002338 if (!dwc3_gadget_ep_request_completed(req) ||
2339 req->num_pending_sgs) {
2340 __dwc3_gadget_kick_transfer(dep);
2341 break;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05302342 }
Felipe Balbi1f512112016-08-12 13:17:27 +03002343
Ville Syrjäläd115d702015-08-31 19:48:28 +03002344 dwc3_gadget_giveback(dep, req, status);
2345
Felipe Balbi58f02182018-03-29 12:10:31 +03002346 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002348 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002349}
2350
Felipe Balbiee3638b2018-03-27 11:26:53 +03002351static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2352 const struct dwc3_event_depevt *event)
2353{
2354 u32 cur_uf, mask;
2355
2356 mask = ~(dep->interval - 1);
2357 cur_uf = event->parameters & mask;
2358 dep->frame_number = cur_uf;
2359}
2360
Felipe Balbi8f608e82018-03-27 10:53:29 +03002361static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2362 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002363{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002364 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002365 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002366 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002367
Felipe Balbiee3638b2018-03-27 11:26:53 +03002368 dwc3_gadget_endpoint_frame_from_event(dep, event);
2369
Felipe Balbi72246da2011-08-19 18:10:58 +03002370 if (event->status & DEPEVT_STATUS_BUSERR)
2371 status = -ECONNRESET;
2372
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002373 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2374 status = -EXDEV;
2375 stop = true;
2376 }
2377
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002378 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002379
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002380 if (stop) {
2381 dwc3_stop_active_transfer(dep, true);
2382 dep->flags = DWC3_EP_ENABLED;
2383 }
2384
Felipe Balbifae2b902011-10-14 13:00:30 +03002385 /*
2386 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2387 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2388 */
2389 if (dwc->revision < DWC3_REVISION_183A) {
2390 u32 reg;
2391 int i;
2392
2393 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002394 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002395
2396 if (!(dep->flags & DWC3_EP_ENABLED))
2397 continue;
2398
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002399 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002400 return;
2401 }
2402
2403 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2404 reg |= dwc->u1u2;
2405 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2406
2407 dwc->u1u2 = 0;
2408 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002409}
2410
Felipe Balbi8f608e82018-03-27 10:53:29 +03002411static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2412 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002413{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002414 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002415 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002416}
2417
Felipe Balbi72246da2011-08-19 18:10:58 +03002418static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2419 const struct dwc3_event_depevt *event)
2420{
2421 struct dwc3_ep *dep;
2422 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002423 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002424
2425 dep = dwc->eps[epnum];
2426
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002427 if (!(dep->flags & DWC3_EP_ENABLED)) {
2428 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2429 return;
2430
2431 /* Handle only EPCMDCMPLT when EP disabled */
2432 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2433 return;
2434 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002435
Felipe Balbi72246da2011-08-19 18:10:58 +03002436 if (epnum == 0 || epnum == 1) {
2437 dwc3_ep0_interrupt(dwc, event);
2438 return;
2439 }
2440
2441 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002442 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002443 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002444 break;
2445 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002446 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002447 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002448 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002449 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2450
2451 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2452 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2453 wake_up(&dep->wait_end_transfer);
2454 }
2455 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002456 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002457 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002458 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 break;
2460 }
2461}
2462
2463static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2464{
2465 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2466 spin_unlock(&dwc->lock);
2467 dwc->gadget_driver->disconnect(&dwc->gadget);
2468 spin_lock(&dwc->lock);
2469 }
2470}
2471
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002472static void dwc3_suspend_gadget(struct dwc3 *dwc)
2473{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002474 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002475 spin_unlock(&dwc->lock);
2476 dwc->gadget_driver->suspend(&dwc->gadget);
2477 spin_lock(&dwc->lock);
2478 }
2479}
2480
2481static void dwc3_resume_gadget(struct dwc3 *dwc)
2482{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002483 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002484 spin_unlock(&dwc->lock);
2485 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002486 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002487 }
2488}
2489
2490static void dwc3_reset_gadget(struct dwc3 *dwc)
2491{
2492 if (!dwc->gadget_driver)
2493 return;
2494
2495 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2496 spin_unlock(&dwc->lock);
2497 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002498 spin_lock(&dwc->lock);
2499 }
2500}
2501
Felipe Balbi8f608e82018-03-27 10:53:29 +03002502static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002503{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002504 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002505 struct dwc3_gadget_ep_cmd_params params;
2506 u32 cmd;
2507 int ret;
2508
Baolin Wang76a638f2016-10-31 19:38:36 +08002509 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2510 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302511 return;
2512
Pratyush Anand57911502012-07-06 15:19:10 +05302513 /*
2514 * NOTICE: We are violating what the Databook says about the
2515 * EndTransfer command. Ideally we would _always_ wait for the
2516 * EndTransfer Command Completion IRQ, but that's causing too
2517 * much trouble synchronizing between us and gadget driver.
2518 *
2519 * We have discussed this with the IP Provider and it was
2520 * suggested to giveback all requests here, but give HW some
2521 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002522 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302523 *
2524 * Note also that a similar handling was tested by Synopsys
2525 * (thanks a lot Paul) and nothing bad has come out of it.
2526 * In short, what we're doing is:
2527 *
2528 * - Issue EndTransfer WITH CMDIOC bit set
2529 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002530 *
2531 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2532 * supports a mode to work around the above limitation. The
2533 * software can poll the CMDACT bit in the DEPCMD register
2534 * after issuing a EndTransfer command. This mode is enabled
2535 * by writing GUCTL2[14]. This polling is already done in the
2536 * dwc3_send_gadget_ep_cmd() function so if the mode is
2537 * enabled, the EndTransfer command will have completed upon
2538 * returning from this function and we don't need to delay for
2539 * 100us.
2540 *
2541 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302542 */
2543
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302544 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002545 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2546 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002547 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302548 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002549 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302550 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002551 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002552
Baolin Wang76a638f2016-10-31 19:38:36 +08002553 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2554 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002555 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002556 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002557}
2558
Felipe Balbi72246da2011-08-19 18:10:58 +03002559static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2560{
2561 u32 epnum;
2562
2563 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2564 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 int ret;
2566
2567 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002568 if (!dep)
2569 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002570
2571 if (!(dep->flags & DWC3_EP_STALL))
2572 continue;
2573
2574 dep->flags &= ~DWC3_EP_STALL;
2575
John Youn50c763f2016-05-31 17:49:56 -07002576 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002577 WARN_ON_ONCE(ret);
2578 }
2579}
2580
2581static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2582{
Felipe Balbic4430a22012-05-24 10:30:01 +03002583 int reg;
2584
Felipe Balbi72246da2011-08-19 18:10:58 +03002585 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2586 reg &= ~DWC3_DCTL_INITU1ENA;
2587 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2588
2589 reg &= ~DWC3_DCTL_INITU2ENA;
2590 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002591
Felipe Balbi72246da2011-08-19 18:10:58 +03002592 dwc3_disconnect_gadget(dwc);
2593
2594 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002595 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002596 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002597
2598 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002599}
2600
Felipe Balbi72246da2011-08-19 18:10:58 +03002601static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2602{
2603 u32 reg;
2604
Felipe Balbifc8bb912016-05-16 13:14:48 +03002605 dwc->connected = true;
2606
Felipe Balbidf62df52011-10-14 15:11:49 +03002607 /*
2608 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2609 * would cause a missing Disconnect Event if there's a
2610 * pending Setup Packet in the FIFO.
2611 *
2612 * There's no suggested workaround on the official Bug
2613 * report, which states that "unless the driver/application
2614 * is doing any special handling of a disconnect event,
2615 * there is no functional issue".
2616 *
2617 * Unfortunately, it turns out that we _do_ some special
2618 * handling of a disconnect event, namely complete all
2619 * pending transfers, notify gadget driver of the
2620 * disconnection, and so on.
2621 *
2622 * Our suggested workaround is to follow the Disconnect
2623 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002624 * flag. Such flag gets set whenever we have a SETUP_PENDING
2625 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002626 * same endpoint.
2627 *
2628 * Refers to:
2629 *
2630 * STAR#9000466709: RTL: Device : Disconnect event not
2631 * generated if setup packet pending in FIFO
2632 */
2633 if (dwc->revision < DWC3_REVISION_188A) {
2634 if (dwc->setup_packet_pending)
2635 dwc3_gadget_disconnect_interrupt(dwc);
2636 }
2637
Felipe Balbi8e744752014-11-06 14:27:53 +08002638 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002639
2640 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2641 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2642 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002643 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002644 dwc3_clear_stall_all_ep(dwc);
2645
2646 /* Reset device address to zero */
2647 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2648 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2649 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002650}
2651
Felipe Balbi72246da2011-08-19 18:10:58 +03002652static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2653{
Felipe Balbi72246da2011-08-19 18:10:58 +03002654 struct dwc3_ep *dep;
2655 int ret;
2656 u32 reg;
2657 u8 speed;
2658
Felipe Balbi72246da2011-08-19 18:10:58 +03002659 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2660 speed = reg & DWC3_DSTS_CONNECTSPD;
2661 dwc->speed = speed;
2662
John Youn5fb6fda2016-11-10 17:23:25 -08002663 /*
2664 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2665 * each time on Connect Done.
2666 *
2667 * Currently we always use the reset value. If any platform
2668 * wants to set this to a different value, we need to add a
2669 * setting and update GCTL.RAMCLKSEL here.
2670 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002671
2672 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002673 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002674 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2675 dwc->gadget.ep0->maxpacket = 512;
2676 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2677 break;
John Youn2da9ad72016-05-20 16:34:26 -07002678 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002679 /*
2680 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2681 * would cause a missing USB3 Reset event.
2682 *
2683 * In such situations, we should force a USB3 Reset
2684 * event by calling our dwc3_gadget_reset_interrupt()
2685 * routine.
2686 *
2687 * Refers to:
2688 *
2689 * STAR#9000483510: RTL: SS : USB3 reset event may
2690 * not be generated always when the link enters poll
2691 */
2692 if (dwc->revision < DWC3_REVISION_190A)
2693 dwc3_gadget_reset_interrupt(dwc);
2694
Felipe Balbi72246da2011-08-19 18:10:58 +03002695 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2696 dwc->gadget.ep0->maxpacket = 512;
2697 dwc->gadget.speed = USB_SPEED_SUPER;
2698 break;
John Youn2da9ad72016-05-20 16:34:26 -07002699 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002700 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2701 dwc->gadget.ep0->maxpacket = 64;
2702 dwc->gadget.speed = USB_SPEED_HIGH;
2703 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002704 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2706 dwc->gadget.ep0->maxpacket = 64;
2707 dwc->gadget.speed = USB_SPEED_FULL;
2708 break;
John Youn2da9ad72016-05-20 16:34:26 -07002709 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2711 dwc->gadget.ep0->maxpacket = 8;
2712 dwc->gadget.speed = USB_SPEED_LOW;
2713 break;
2714 }
2715
Thinh Nguyen61800262018-01-12 18:18:05 -08002716 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2717
Pratyush Anand2b758352013-01-14 15:59:31 +05302718 /* Enable USB2 LPM Capability */
2719
John Younee5cd412016-02-05 17:08:45 -08002720 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002721 (speed != DWC3_DSTS_SUPERSPEED) &&
2722 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302723 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2724 reg |= DWC3_DCFG_LPM_CAP;
2725 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2726
2727 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2728 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2729
Huang Rui460d0982014-10-31 11:11:18 +08002730 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302731
Huang Rui80caf7d2014-10-28 19:54:26 +08002732 /*
2733 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2734 * DCFG.LPMCap is set, core responses with an ACK and the
2735 * BESL value in the LPM token is less than or equal to LPM
2736 * NYET threshold.
2737 */
2738 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2739 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002740 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002741
2742 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2743 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2744
Pratyush Anand2b758352013-01-14 15:59:31 +05302745 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002746 } else {
2747 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2748 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2749 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302750 }
2751
Felipe Balbi72246da2011-08-19 18:10:58 +03002752 dep = dwc->eps[0];
John Youn39ebb052016-11-09 16:36:28 -08002753 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002754 if (ret) {
2755 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2756 return;
2757 }
2758
2759 dep = dwc->eps[1];
John Youn39ebb052016-11-09 16:36:28 -08002760 ret = __dwc3_gadget_ep_enable(dep, true, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002761 if (ret) {
2762 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2763 return;
2764 }
2765
2766 /*
2767 * Configure PHY via GUSB3PIPECTLn if required.
2768 *
2769 * Update GTXFIFOSIZn
2770 *
2771 * In both cases reset values should be sufficient.
2772 */
2773}
2774
2775static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2776{
Felipe Balbi72246da2011-08-19 18:10:58 +03002777 /*
2778 * TODO take core out of low power mode when that's
2779 * implemented.
2780 */
2781
Jiebing Liad14d4e2014-12-11 13:26:29 +08002782 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2783 spin_unlock(&dwc->lock);
2784 dwc->gadget_driver->resume(&dwc->gadget);
2785 spin_lock(&dwc->lock);
2786 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002787}
2788
2789static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2790 unsigned int evtinfo)
2791{
Felipe Balbifae2b902011-10-14 13:00:30 +03002792 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002793 unsigned int pwropt;
2794
2795 /*
2796 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2797 * Hibernation mode enabled which would show up when device detects
2798 * host-initiated U3 exit.
2799 *
2800 * In that case, device will generate a Link State Change Interrupt
2801 * from U3 to RESUME which is only necessary if Hibernation is
2802 * configured in.
2803 *
2804 * There are no functional changes due to such spurious event and we
2805 * just need to ignore it.
2806 *
2807 * Refers to:
2808 *
2809 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2810 * operational mode
2811 */
2812 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2813 if ((dwc->revision < DWC3_REVISION_250A) &&
2814 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2815 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2816 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002817 return;
2818 }
2819 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002820
2821 /*
2822 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2823 * on the link partner, the USB session might do multiple entry/exit
2824 * of low power states before a transfer takes place.
2825 *
2826 * Due to this problem, we might experience lower throughput. The
2827 * suggested workaround is to disable DCTL[12:9] bits if we're
2828 * transitioning from U1/U2 to U0 and enable those bits again
2829 * after a transfer completes and there are no pending transfers
2830 * on any of the enabled endpoints.
2831 *
2832 * This is the first half of that workaround.
2833 *
2834 * Refers to:
2835 *
2836 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2837 * core send LGO_Ux entering U0
2838 */
2839 if (dwc->revision < DWC3_REVISION_183A) {
2840 if (next == DWC3_LINK_STATE_U0) {
2841 u32 u1u2;
2842 u32 reg;
2843
2844 switch (dwc->link_state) {
2845 case DWC3_LINK_STATE_U1:
2846 case DWC3_LINK_STATE_U2:
2847 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2848 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2849 | DWC3_DCTL_ACCEPTU2ENA
2850 | DWC3_DCTL_INITU1ENA
2851 | DWC3_DCTL_ACCEPTU1ENA);
2852
2853 if (!dwc->u1u2)
2854 dwc->u1u2 = reg & u1u2;
2855
2856 reg &= ~u1u2;
2857
2858 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2859 break;
2860 default:
2861 /* do nothing */
2862 break;
2863 }
2864 }
2865 }
2866
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002867 switch (next) {
2868 case DWC3_LINK_STATE_U1:
2869 if (dwc->speed == USB_SPEED_SUPER)
2870 dwc3_suspend_gadget(dwc);
2871 break;
2872 case DWC3_LINK_STATE_U2:
2873 case DWC3_LINK_STATE_U3:
2874 dwc3_suspend_gadget(dwc);
2875 break;
2876 case DWC3_LINK_STATE_RESUME:
2877 dwc3_resume_gadget(dwc);
2878 break;
2879 default:
2880 /* do nothing */
2881 break;
2882 }
2883
Felipe Balbie57ebc12014-04-22 13:20:12 -05002884 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002885}
2886
Baolin Wang72704f82016-05-16 16:43:53 +08002887static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2888 unsigned int evtinfo)
2889{
2890 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2891
2892 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2893 dwc3_suspend_gadget(dwc);
2894
2895 dwc->link_state = next;
2896}
2897
Felipe Balbie1dadd32014-02-25 14:47:54 -06002898static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2899 unsigned int evtinfo)
2900{
2901 unsigned int is_ss = evtinfo & BIT(4);
2902
Felipe Balbibfad65e2017-04-19 14:59:27 +03002903 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002904 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2905 * have a known issue which can cause USB CV TD.9.23 to fail
2906 * randomly.
2907 *
2908 * Because of this issue, core could generate bogus hibernation
2909 * events which SW needs to ignore.
2910 *
2911 * Refers to:
2912 *
2913 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2914 * Device Fallback from SuperSpeed
2915 */
2916 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2917 return;
2918
2919 /* enter hibernation here */
2920}
2921
Felipe Balbi72246da2011-08-19 18:10:58 +03002922static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2923 const struct dwc3_event_devt *event)
2924{
2925 switch (event->type) {
2926 case DWC3_DEVICE_EVENT_DISCONNECT:
2927 dwc3_gadget_disconnect_interrupt(dwc);
2928 break;
2929 case DWC3_DEVICE_EVENT_RESET:
2930 dwc3_gadget_reset_interrupt(dwc);
2931 break;
2932 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2933 dwc3_gadget_conndone_interrupt(dwc);
2934 break;
2935 case DWC3_DEVICE_EVENT_WAKEUP:
2936 dwc3_gadget_wakeup_interrupt(dwc);
2937 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002938 case DWC3_DEVICE_EVENT_HIBER_REQ:
2939 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2940 "unexpected hibernation event\n"))
2941 break;
2942
2943 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2944 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002945 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2946 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2947 break;
2948 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002949 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002950 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002951 /*
2952 * Ignore suspend event until the gadget enters into
2953 * USB_STATE_CONFIGURED state.
2954 */
2955 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2956 dwc3_gadget_suspend_interrupt(dwc,
2957 event->event_info);
2958 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002959 break;
2960 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002961 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002962 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002963 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002964 break;
2965 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002966 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002967 }
2968}
2969
2970static void dwc3_process_event_entry(struct dwc3 *dwc,
2971 const union dwc3_event *event)
2972{
Felipe Balbi43c96be2016-09-26 13:23:34 +03002973 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002974
Felipe Balbidfc5e802017-04-26 13:44:51 +03002975 if (!event->type.is_devspec)
2976 dwc3_endpoint_interrupt(dwc, &event->depevt);
2977 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03002978 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03002979 else
Felipe Balbi72246da2011-08-19 18:10:58 +03002980 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03002981}
2982
Felipe Balbidea520a2016-03-30 09:39:34 +03002983static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002984{
Felipe Balbidea520a2016-03-30 09:39:34 +03002985 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002986 irqreturn_t ret = IRQ_NONE;
2987 int left;
2988 u32 reg;
2989
Felipe Balbif42f2442013-06-12 21:25:08 +03002990 left = evt->count;
2991
2992 if (!(evt->flags & DWC3_EVENT_PENDING))
2993 return IRQ_NONE;
2994
2995 while (left > 0) {
2996 union dwc3_event event;
2997
John Younebbb2d52016-11-15 13:07:02 +02002998 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03002999
3000 dwc3_process_event_entry(dwc, &event);
3001
3002 /*
3003 * FIXME we wrap around correctly to the next entry as
3004 * almost all entries are 4 bytes in size. There is one
3005 * entry which has 12 bytes which is a regular entry
3006 * followed by 8 bytes data. ATM I don't know how
3007 * things are organized if we get next to the a
3008 * boundary so I worry about that once we try to handle
3009 * that.
3010 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003011 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003012 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003013 }
3014
3015 evt->count = 0;
3016 evt->flags &= ~DWC3_EVENT_PENDING;
3017 ret = IRQ_HANDLED;
3018
3019 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003020 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003021 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003022 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003023
John Youncf40b862016-11-14 12:32:43 -08003024 if (dwc->imod_interval) {
3025 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3026 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3027 }
3028
Felipe Balbif42f2442013-06-12 21:25:08 +03003029 return ret;
3030}
3031
Felipe Balbidea520a2016-03-30 09:39:34 +03003032static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003033{
Felipe Balbidea520a2016-03-30 09:39:34 +03003034 struct dwc3_event_buffer *evt = _evt;
3035 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003036 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003037 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003038
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003039 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003040 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003041 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003042
3043 return ret;
3044}
3045
Felipe Balbidea520a2016-03-30 09:39:34 +03003046static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003047{
Felipe Balbidea520a2016-03-30 09:39:34 +03003048 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003049 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003050 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003051 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003052
Felipe Balbifc8bb912016-05-16 13:14:48 +03003053 if (pm_runtime_suspended(dwc->dev)) {
3054 pm_runtime_get(dwc->dev);
3055 disable_irq_nosync(dwc->irq_gadget);
3056 dwc->pending_events = true;
3057 return IRQ_HANDLED;
3058 }
3059
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003060 /*
3061 * With PCIe legacy interrupt, test shows that top-half irq handler can
3062 * be called again after HW interrupt deassertion. Check if bottom-half
3063 * irq event handler completes before caching new event to prevent
3064 * losing events.
3065 */
3066 if (evt->flags & DWC3_EVENT_PENDING)
3067 return IRQ_HANDLED;
3068
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003069 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003070 count &= DWC3_GEVNTCOUNT_MASK;
3071 if (!count)
3072 return IRQ_NONE;
3073
Felipe Balbib15a7622011-06-30 16:57:15 +03003074 evt->count = count;
3075 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003076
Felipe Balbie8adfc32013-06-12 21:11:14 +03003077 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003078 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003079 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003080 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003081
John Younebbb2d52016-11-15 13:07:02 +02003082 amount = min(count, evt->length - evt->lpos);
3083 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3084
3085 if (amount < count)
3086 memcpy(evt->cache, evt->buf, count - amount);
3087
John Youn65aca322016-11-15 13:08:59 +02003088 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3089
Felipe Balbib15a7622011-06-30 16:57:15 +03003090 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003091}
3092
Felipe Balbidea520a2016-03-30 09:39:34 +03003093static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003094{
Felipe Balbidea520a2016-03-30 09:39:34 +03003095 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003096
Felipe Balbidea520a2016-03-30 09:39:34 +03003097 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003098}
3099
Felipe Balbi6db38122016-10-03 11:27:01 +03003100static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3101{
3102 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3103 int irq;
3104
3105 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3106 if (irq > 0)
3107 goto out;
3108
3109 if (irq == -EPROBE_DEFER)
3110 goto out;
3111
3112 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3113 if (irq > 0)
3114 goto out;
3115
3116 if (irq == -EPROBE_DEFER)
3117 goto out;
3118
3119 irq = platform_get_irq(dwc3_pdev, 0);
3120 if (irq > 0)
3121 goto out;
3122
3123 if (irq != -EPROBE_DEFER)
3124 dev_err(dwc->dev, "missing peripheral IRQ\n");
3125
3126 if (!irq)
3127 irq = -EINVAL;
3128
3129out:
3130 return irq;
3131}
3132
Felipe Balbi72246da2011-08-19 18:10:58 +03003133/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003134 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003135 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003136 *
3137 * Returns 0 on success otherwise negative errno.
3138 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003139int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003140{
Felipe Balbi6db38122016-10-03 11:27:01 +03003141 int ret;
3142 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003143
Felipe Balbi6db38122016-10-03 11:27:01 +03003144 irq = dwc3_gadget_get_irq(dwc);
3145 if (irq < 0) {
3146 ret = irq;
3147 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003148 }
3149
3150 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003151
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303152 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3153 sizeof(*dwc->ep0_trb) * 2,
3154 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003155 if (!dwc->ep0_trb) {
3156 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3157 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003158 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003159 }
3160
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003161 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003162 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003163 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003164 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003165 }
3166
Felipe Balbi905dc042017-01-05 14:46:52 +02003167 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3168 &dwc->bounce_addr, GFP_KERNEL);
3169 if (!dwc->bounce) {
3170 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003171 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003172 }
3173
Baolin Wangbb014732016-10-14 17:11:33 +08003174 init_completion(&dwc->ep0_in_setup);
3175
Felipe Balbi72246da2011-08-19 18:10:58 +03003176 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003177 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003178 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003179 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003180 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003181
3182 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003183 * FIXME We might be setting max_speed to <SUPER, however versions
3184 * <2.20a of dwc3 have an issue with metastability (documented
3185 * elsewhere in this driver) which tells us we can't set max speed to
3186 * anything lower than SUPER.
3187 *
3188 * Because gadget.max_speed is only used by composite.c and function
3189 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3190 * to happen so we avoid sending SuperSpeed Capability descriptor
3191 * together with our BOS descriptor as that could confuse host into
3192 * thinking we can handle super speed.
3193 *
3194 * Note that, in fact, we won't even support GetBOS requests when speed
3195 * is less than super speed because we don't have means, yet, to tell
3196 * composite.c that we are USB 2.0 + LPM ECN.
3197 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003198 if (dwc->revision < DWC3_REVISION_220A &&
3199 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003200 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003201 dwc->revision);
3202
3203 dwc->gadget.max_speed = dwc->maximum_speed;
3204
3205 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003206 * REVISIT: Here we should clear all pending IRQs to be
3207 * sure we're starting from a well known location.
3208 */
3209
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003210 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003211 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003212 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003213
Felipe Balbi72246da2011-08-19 18:10:58 +03003214 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3215 if (ret) {
3216 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003217 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003218 }
3219
3220 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003221
3222err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003223 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003224
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003225err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003226 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3227 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003228
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003229err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003230 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003231
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003232err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303233 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003234 dwc->ep0_trb, dwc->ep0_trb_addr);
3235
Felipe Balbi72246da2011-08-19 18:10:58 +03003236err0:
3237 return ret;
3238}
3239
Felipe Balbi7415f172012-04-30 14:56:33 +03003240/* -------------------------------------------------------------------------- */
3241
Felipe Balbi72246da2011-08-19 18:10:58 +03003242void dwc3_gadget_exit(struct dwc3 *dwc)
3243{
Felipe Balbi72246da2011-08-19 18:10:58 +03003244 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003245 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003246 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003247 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003248 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303249 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003250 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003251}
Felipe Balbi7415f172012-04-30 14:56:33 +03003252
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003253int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003254{
Roger Quadros9772b472016-04-12 11:33:29 +03003255 if (!dwc->gadget_driver)
3256 return 0;
3257
Roger Quadros1551e352017-02-15 14:16:26 +02003258 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003259 dwc3_disconnect_gadget(dwc);
3260 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003261
3262 return 0;
3263}
3264
3265int dwc3_gadget_resume(struct dwc3 *dwc)
3266{
Felipe Balbi7415f172012-04-30 14:56:33 +03003267 int ret;
3268
Roger Quadros9772b472016-04-12 11:33:29 +03003269 if (!dwc->gadget_driver)
3270 return 0;
3271
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003272 ret = __dwc3_gadget_start(dwc);
3273 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003274 goto err0;
3275
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003276 ret = dwc3_gadget_run_stop(dwc, true, false);
3277 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003278 goto err1;
3279
Felipe Balbi7415f172012-04-30 14:56:33 +03003280 return 0;
3281
3282err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003283 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003284
3285err0:
3286 return ret;
3287}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003288
3289void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3290{
3291 if (dwc->pending_events) {
3292 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3293 dwc->pending_events = false;
3294 enable_irq(dwc->irq_gadget);
3295 }
3296}