blob: a170c0739aaa58f2a7a44fb7f41dda04b0e7ed6b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbibfad65e2017-04-19 14:59:27 +03002/*
Felipe Balbi72246da2011-08-19 18:10:58 +03003 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03006 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Felipe Balbi72246da2011-08-19 18:10:58 +03009 */
10
11#include <linux/kernel.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/platform_device.h>
16#include <linux/pm_runtime.h>
17#include <linux/interrupt.h>
18#include <linux/io.h>
19#include <linux/list.h>
20#include <linux/dma-mapping.h>
21
22#include <linux/usb/ch9.h>
23#include <linux/usb/gadget.h>
24
Felipe Balbi80977dc2014-08-19 16:37:22 -050025#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include "core.h"
27#include "gadget.h"
28#include "io.h"
29
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020030/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030031 * dwc3_gadget_set_test_mode - enables usb2 test modes
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020032 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
34 *
Felipe Balbibfad65e2017-04-19 14:59:27 +030035 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037 */
38int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
39{
40 u32 reg;
41
42 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
43 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
44
45 switch (mode) {
46 case TEST_J:
47 case TEST_K:
48 case TEST_SE0_NAK:
49 case TEST_PACKET:
50 case TEST_FORCE_EN:
51 reg |= mode << 1;
52 break;
53 default:
54 return -EINVAL;
55 }
56
57 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
58
59 return 0;
60}
61
Felipe Balbi8598bde2012-01-02 18:55:57 +020062/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030063 * dwc3_gadget_get_link_state - gets current state of usb link
Paul Zimmerman911f1f82012-04-27 13:35:15 +030064 * @dwc: pointer to our context structure
65 *
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
68 */
69int dwc3_gadget_get_link_state(struct dwc3 *dwc)
70{
71 u32 reg;
72
73 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
74
75 return DWC3_DSTS_USBLNKST(reg);
76}
77
78/**
Felipe Balbibfad65e2017-04-19 14:59:27 +030079 * dwc3_gadget_set_link_state - sets usb link to a particular state
Felipe Balbi8598bde2012-01-02 18:55:57 +020080 * @dwc: pointer to our context structure
81 * @state: the state to put link into
82 *
83 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080084 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020085 */
86int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
87{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080088 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020089 u32 reg;
90
Paul Zimmerman802fde92012-04-27 13:10:52 +030091 /*
92 * Wait until device controller is ready. Only applies to 1.94a and
93 * later RTL.
94 */
95 if (dwc->revision >= DWC3_REVISION_194A) {
96 while (--retries) {
97 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
98 if (reg & DWC3_DSTS_DCNRD)
99 udelay(5);
100 else
101 break;
102 }
103
104 if (retries <= 0)
105 return -ETIMEDOUT;
106 }
107
Felipe Balbi8598bde2012-01-02 18:55:57 +0200108 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
109 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
110
111 /* set requested state */
112 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
113 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
114
Paul Zimmerman802fde92012-04-27 13:10:52 +0300115 /*
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
118 */
119 if (dwc->revision >= DWC3_REVISION_194A)
120 return 0;
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300123 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200124 while (--retries) {
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
126
Felipe Balbi8598bde2012-01-02 18:55:57 +0200127 if (DWC3_DSTS_USBLNKST(reg) == state)
128 return 0;
129
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800130 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 }
132
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 return -ETIMEDOUT;
134}
135
John Youndca01192016-05-19 17:26:05 -0700136/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
John Youndca01192016-05-19 17:26:05 -0700139 *
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
143 */
144static void dwc3_ep_inc_trb(u8 *index)
145{
146 (*index)++;
147 if (*index == (DWC3_TRB_NUM - 1))
148 *index = 0;
149}
150
Felipe Balbibfad65e2017-04-19 14:59:27 +0300151/**
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
154 */
Felipe Balbief966b92016-04-05 13:09:51 +0300155static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200156{
John Youndca01192016-05-19 17:26:05 -0700157 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbibfad65e2017-04-19 14:59:27 +0300160/**
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
163 */
Felipe Balbief966b92016-04-05 13:09:51 +0300164static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
165{
John Youndca01192016-05-19 17:26:05 -0700166 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200167}
168
Felipe Balbic91815b2018-03-26 13:14:47 +0300169void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
170 struct dwc3_request *req, int status)
171{
172 struct dwc3 *dwc = dep->dwc;
173
174 req->started = false;
175 list_del(&req->list);
176 req->remaining = 0;
177
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
180
181 if (req->trb)
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
183 &req->request, req->direction);
184
185 req->trb = NULL;
186 trace_dwc3_gadget_giveback(req);
187
188 if (dep->number > 1)
189 pm_runtime_put(dwc->dev);
190}
191
Felipe Balbibfad65e2017-04-19 14:59:27 +0300192/**
193 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
194 * @dep: The endpoint to whom the request belongs to
195 * @req: The request we're giving back
196 * @status: completion code for the request
197 *
198 * Must be called with controller's lock held and interrupts disabled. This
199 * function will unmap @req and call its ->complete() callback to notify upper
200 * layers that it has completed.
201 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300202void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
203 int status)
204{
205 struct dwc3 *dwc = dep->dwc;
206
Felipe Balbic91815b2018-03-26 13:14:47 +0300207 dwc3_gadget_del_and_unmap_request(dep, req, status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300208
209 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300211 spin_lock(&dwc->lock);
212}
213
Felipe Balbibfad65e2017-04-19 14:59:27 +0300214/**
215 * dwc3_send_gadget_generic_command - issue a generic command for the controller
216 * @dwc: pointer to the controller context
217 * @cmd: the command to be issued
218 * @param: command parameter
219 *
220 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
221 * and wait for its completion.
222 */
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500223int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300224{
225 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300226 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300227 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300228 u32 reg;
229
230 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
231 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
232
233 do {
234 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
235 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300236 status = DWC3_DGCMD_STATUS(reg);
237 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300238 ret = -EINVAL;
239 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300240 }
Janusz Dziedzice3aee482016-11-09 11:01:33 +0100241 } while (--timeout);
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300242
243 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300244 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300245 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300246 }
247
Felipe Balbi71f7e702016-05-23 14:16:19 +0300248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
249
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300250 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300251}
252
Felipe Balbic36d8e92016-04-04 12:46:33 +0300253static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
254
Felipe Balbibfad65e2017-04-19 14:59:27 +0300255/**
256 * dwc3_send_gadget_ep_cmd - issue an endpoint command
257 * @dep: the endpoint to which the command is going to be issued
258 * @cmd: the command to be issued
259 * @params: parameters to the command
260 *
261 * Caller should handle locking. This function will issue @cmd with given
262 * @params to @dep and wait for its completion.
263 */
Felipe Balbi2cd47182016-04-12 16:42:43 +0300264int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300266{
Felipe Balbi8897a762016-09-22 10:56:08 +0300267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi2cd47182016-04-12 16:42:43 +0300268 struct dwc3 *dwc = dep->dwc;
Vincent Pelletier8722e092017-11-30 15:31:06 +0000269 u32 timeout = 1000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 u32 reg;
271
Felipe Balbi0933df12016-05-23 14:02:33 +0300272 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300273 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300274 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300276 /*
277 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
278 * we're issuing an endpoint command, we must check if
279 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
280 *
281 * We will also set SUSPHY bit to what it was before returning as stated
282 * by the same section on Synopsys databook.
283 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300284 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
285 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
286 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
287 susphy = true;
288 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
289 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
290 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300291 }
292
Felipe Balbi59999142016-09-22 12:25:28 +0300293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
Felipe Balbic36d8e92016-04-04 12:46:33 +0300294 int needs_wakeup;
295
296 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
297 dwc->link_state == DWC3_LINK_STATE_U2 ||
298 dwc->link_state == DWC3_LINK_STATE_U3);
299
300 if (unlikely(needs_wakeup)) {
301 ret = __dwc3_gadget_wakeup(dwc);
302 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
303 ret);
304 }
305 }
306
Felipe Balbi2eb88012016-04-12 16:53:39 +0300307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300310
Felipe Balbi8897a762016-09-22 10:56:08 +0300311 /*
312 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
313 * not relying on XferNotReady, we can make use of a special "No
314 * Response Update Transfer" command where we should clear both CmdAct
315 * and CmdIOC bits.
316 *
317 * With this, we don't need to wait for command completion and can
318 * straight away issue further commands to the endpoint.
319 *
320 * NOTICE: We're making an assumption that control endpoints will never
321 * make use of Update Transfer command. This is a safe assumption
322 * because we can never have more than one request at a time with
323 * Control Endpoints. If anybody changes that assumption, this chunk
324 * needs to be updated accordingly.
325 */
326 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
327 !usb_endpoint_xfer_isoc(desc))
328 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
329 else
330 cmd |= DWC3_DEPCMD_CMDACT;
331
332 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
Felipe Balbi72246da2011-08-19 18:10:58 +0300333 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000337
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000338 switch (cmd_status) {
339 case 0:
340 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300341 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000342 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000343 ret = -EINVAL;
344 break;
345 case DEPEVT_TRANSFER_BUS_EXPIRY:
346 /*
347 * SW issues START TRANSFER command to
348 * isochronous ep with future frame interval. If
349 * future interval time has already passed when
350 * core receives the command, it will respond
351 * with an error status of 'Bus Expiry'.
352 *
353 * Instead of always returning -EINVAL, let's
354 * give a hint to the gadget driver that this is
355 * the case by returning -EAGAIN.
356 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000357 ret = -EAGAIN;
358 break;
359 default:
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
361 }
362
Felipe Balbic0ca3242016-04-04 09:11:51 +0300363 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300364 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300365 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366
Felipe Balbif6bb2252016-05-23 13:53:34 +0300367 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300368 ret = -ETIMEDOUT;
Felipe Balbi0933df12016-05-23 14:02:33 +0300369 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300370 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300371
Felipe Balbi0933df12016-05-23 14:02:33 +0300372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
373
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +0300374 if (ret == 0) {
375 switch (DWC3_DEPCMD_CMD(cmd)) {
376 case DWC3_DEPCMD_STARTTRANSFER:
377 dep->flags |= DWC3_EP_TRANSFER_STARTED;
378 break;
379 case DWC3_DEPCMD_ENDTRANSFER:
380 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
381 break;
382 default:
383 /* nothing */
384 break;
385 }
386 }
387
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300388 if (unlikely(susphy)) {
389 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
392 }
393
Felipe Balbic0ca3242016-04-04 09:11:51 +0300394 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395}
396
John Youn50c763f2016-05-31 17:49:56 -0700397static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400 struct dwc3_gadget_ep_cmd_params params;
401 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
402
403 /*
404 * As of core revision 2.60a the recommended programming model
405 * is to set the ClearPendIN bit when issuing a Clear Stall EP
406 * command for IN endpoints. This is to prevent an issue where
407 * some (non-compliant) hosts may not send ACK TPs for pending
408 * IN transfers due to a mishandled error condition. Synopsys
409 * STAR 9000614252.
410 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
414
415 memset(&params, 0, sizeof(params));
416
Felipe Balbi2cd47182016-04-12 16:42:43 +0300417 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700418}
419
Felipe Balbi72246da2011-08-19 18:10:58 +0300420static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200421 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300422{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300423 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
425 return dep->trb_pool_dma + offset;
426}
427
428static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
429{
430 struct dwc3 *dwc = dep->dwc;
431
432 if (dep->trb_pool)
433 return 0;
434
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi72246da2011-08-19 18:10:58 +0300436 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
437 &dep->trb_pool_dma, GFP_KERNEL);
438 if (!dep->trb_pool) {
439 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
440 dep->name);
441 return -ENOMEM;
442 }
443
444 return 0;
445}
446
447static void dwc3_free_trb_pool(struct dwc3_ep *dep)
448{
449 struct dwc3 *dwc = dep->dwc;
450
Arnd Bergmannd64ff402016-11-17 17:13:47 +0530451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
Felipe Balbi72246da2011-08-19 18:10:58 +0300452 dep->trb_pool, dep->trb_pool_dma);
453
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
456}
457
Felipe Balbi20d1d432018-04-09 12:49:02 +0300458static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
459{
460 struct dwc3_gadget_ep_cmd_params params;
461
462 memset(&params, 0x00, sizeof(params));
463
464 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
465
466 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
467 &params);
468}
John Younc4509602016-02-16 20:10:53 -0800469
470/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300471 * dwc3_gadget_start_config - configure ep resources
John Younc4509602016-02-16 20:10:53 -0800472 * @dwc: pointer to our controller context structure
473 * @dep: endpoint that is being enabled
474 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300475 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
476 * completion, it will set Transfer Resource for all available endpoints.
John Younc4509602016-02-16 20:10:53 -0800477 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300478 * The assignment of transfer resources cannot perfectly follow the data book
479 * due to the fact that the controller driver does not have all knowledge of the
480 * configuration in advance. It is given this information piecemeal by the
481 * composite gadget framework after every SET_CONFIGURATION and
482 * SET_INTERFACE. Trying to follow the databook programming model in this
483 * scenario can cause errors. For two reasons:
John Younc4509602016-02-16 20:10:53 -0800484 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300485 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
486 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
487 * incorrect in the scenario of multiple interfaces.
488 *
489 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
John Younc4509602016-02-16 20:10:53 -0800490 * endpoint on alt setting (8.1.6).
491 *
492 * The following simplified method is used instead:
493 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300494 * All hardware endpoints can be assigned a transfer resource and this setting
495 * will stay persistent until either a core reset or hibernation. So whenever we
496 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
497 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
John Younc4509602016-02-16 20:10:53 -0800498 * guaranteed that there are as many transfer resources as endpoints.
499 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300500 * This function is called for each endpoint when it is being enabled but is
501 * triggered only when called for EP0-out, which always happens first, and which
502 * should only happen in one of the above conditions.
John Younc4509602016-02-16 20:10:53 -0800503 */
Felipe Balbib07c2db2018-04-09 12:46:47 +0300504static int dwc3_gadget_start_config(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300505{
506 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300507 struct dwc3 *dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300508 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800509 int i;
510 int ret;
511
512 if (dep->number)
513 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
515 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800516 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300517 dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300518
Felipe Balbi2cd47182016-04-12 16:42:43 +0300519 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800520 if (ret)
521 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300522
John Younc4509602016-02-16 20:10:53 -0800523 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
524 struct dwc3_ep *dep = dwc->eps[i];
525
526 if (!dep)
527 continue;
528
Felipe Balbib07c2db2018-04-09 12:46:47 +0300529 ret = dwc3_gadget_set_xfer_resource(dep);
John Younc4509602016-02-16 20:10:53 -0800530 if (ret)
531 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532 }
533
534 return 0;
535}
536
Felipe Balbib07c2db2018-04-09 12:46:47 +0300537static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300538{
John Youn39ebb052016-11-09 16:36:28 -0800539 const struct usb_ss_ep_comp_descriptor *comp_desc;
540 const struct usb_endpoint_descriptor *desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 struct dwc3_gadget_ep_cmd_params params;
Felipe Balbib07c2db2018-04-09 12:46:47 +0300542 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
John Youn39ebb052016-11-09 16:36:28 -0800544 comp_desc = dep->endpoint.comp_desc;
545 desc = dep->endpoint.desc;
546
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 memset(&params, 0x00, sizeof(params));
548
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300549 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900550 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
551
552 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800553 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300554 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300555 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900556 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbia2d23f02018-04-09 12:40:48 +0300558 params.param0 |= action;
559 if (action == DWC3_DEPCFG_ACTION_RESTORE)
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600560 params.param2 |= dep->saved_state;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600561
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300562 if (usb_endpoint_xfer_control(desc))
563 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300564
565 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
566 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300567
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200568 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300569 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
570 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300571 dep->stream_capable = true;
572 }
573
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500574 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300575 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300576
577 /*
578 * We are doing 1:1 mapping for endpoints, meaning
579 * Physical Endpoints 2 maps to Logical Endpoint 2 and
580 * so on. We consider the direction bit as part of the physical
581 * endpoint number. So USB endpoint 0x81 is 0x03.
582 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584
585 /*
586 * We must use the lower 16 TX FIFOs even though
587 * HW might have more
588 */
589 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300590 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
592 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300593 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 dep->interval = 1 << (desc->bInterval - 1);
595 }
596
Felipe Balbi2cd47182016-04-12 16:42:43 +0300597 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300598}
599
Felipe Balbi72246da2011-08-19 18:10:58 +0300600/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300601 * __dwc3_gadget_ep_enable - initializes a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300602 * @dep: endpoint to be initialized
Felipe Balbia2d23f02018-04-09 12:40:48 +0300603 * @action: one of INIT, MODIFY or RESTORE
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300605 * Caller should take care of locking. Execute all necessary commands to
606 * initialize a HW endpoint so it can be used by a gadget driver.
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 */
Felipe Balbia2d23f02018-04-09 12:40:48 +0300608static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
Felipe Balbi72246da2011-08-19 18:10:58 +0300609{
John Youn39ebb052016-11-09 16:36:28 -0800610 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 struct dwc3 *dwc = dep->dwc;
John Youn39ebb052016-11-09 16:36:28 -0800612
Felipe Balbi72246da2011-08-19 18:10:58 +0300613 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300614 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
616 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbib07c2db2018-04-09 12:46:47 +0300617 ret = dwc3_gadget_start_config(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 if (ret)
619 return ret;
620 }
621
Felipe Balbib07c2db2018-04-09 12:46:47 +0300622 ret = dwc3_gadget_set_ep_config(dep, action);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 if (ret)
624 return ret;
625
626 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200627 struct dwc3_trb *trb_st_hw;
628 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629
Felipe Balbi72246da2011-08-19 18:10:58 +0300630 dep->type = usb_endpoint_type(desc);
631 dep->flags |= DWC3_EP_ENABLED;
Baolin Wang76a638f2016-10-31 19:38:36 +0800632 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633
634 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
635 reg |= DWC3_DALEPENA_EP(dep->number);
636 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
637
Baolin Wang76a638f2016-10-31 19:38:36 +0800638 init_waitqueue_head(&dep->wait_end_transfer);
639
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300640 if (usb_endpoint_xfer_control(desc))
Felipe Balbi2870e502016-11-03 13:53:29 +0200641 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300642
John Youn0d257442016-05-19 17:26:08 -0700643 /* Initialize the TRB ring */
644 dep->trb_dequeue = 0;
645 dep->trb_enqueue = 0;
646 memset(dep->trb_pool, 0,
647 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
648
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300649 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 trb_st_hw = &dep->trb_pool[0];
651
Felipe Balbif6bafc62012-02-06 11:04:53 +0200652 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200653 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 }
658
Felipe Balbia97ea992016-09-29 16:28:56 +0300659 /*
660 * Issue StartTransfer here with no-op TRB so we can always rely on No
661 * Response Update Transfer command.
662 */
Felipe Balbi52fcc0b2018-03-26 13:19:43 +0300663 if (usb_endpoint_xfer_bulk(desc) ||
664 usb_endpoint_xfer_int(desc)) {
Felipe Balbia97ea992016-09-29 16:28:56 +0300665 struct dwc3_gadget_ep_cmd_params params;
666 struct dwc3_trb *trb;
667 dma_addr_t trb_dma;
668 u32 cmd;
669
670 memset(&params, 0, sizeof(params));
671 trb = &dep->trb_pool[0];
672 trb_dma = dwc3_trb_dma_offset(dep, trb);
673
674 params.param0 = upper_32_bits(trb_dma);
675 params.param1 = lower_32_bits(trb_dma);
676
677 cmd = DWC3_DEPCMD_STARTTRANSFER;
678
679 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
680 if (ret < 0)
681 return ret;
682
Felipe Balbia97ea992016-09-29 16:28:56 +0300683 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
684 WARN_ON_ONCE(!dep->resource_index);
685 }
686
Felipe Balbi2870e502016-11-03 13:53:29 +0200687out:
688 trace_dwc3_gadget_ep_enable(dep);
689
Felipe Balbi72246da2011-08-19 18:10:58 +0300690 return 0;
691}
692
Felipe Balbi8f608e82018-03-27 10:53:29 +0300693static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200694static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300695{
696 struct dwc3_request *req;
697
Felipe Balbi8f608e82018-03-27 10:53:29 +0300698 dwc3_stop_active_transfer(dep, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300699
Felipe Balbi0e146022016-06-21 10:32:02 +0300700 /* - giveback all requests to gadget driver */
701 while (!list_empty(&dep->started_list)) {
702 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200703
Felipe Balbi0e146022016-06-21 10:32:02 +0300704 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200705 }
706
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200707 while (!list_empty(&dep->pending_list)) {
708 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300709
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200710 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300711 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300712}
713
714/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300715 * __dwc3_gadget_ep_disable - disables a hw endpoint
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 * @dep: the endpoint to disable
717 *
Felipe Balbibfad65e2017-04-19 14:59:27 +0300718 * This function undoes what __dwc3_gadget_ep_enable did and also removes
719 * requests which are currently being processed by the hardware and those which
720 * are not yet scheduled.
721 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200722 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300724static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
725{
726 struct dwc3 *dwc = dep->dwc;
727 u32 reg;
728
Felipe Balbi2870e502016-11-03 13:53:29 +0200729 trace_dwc3_gadget_ep_disable(dep);
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500730
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200731 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300732
Felipe Balbi687ef982014-04-16 10:30:33 -0500733 /* make sure HW endpoint isn't stalled */
734 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500735 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500736
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
738 reg &= ~DWC3_DALEPENA_EP(dep->number);
739 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
740
Felipe Balbi879631a2011-09-30 10:58:47 +0300741 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300742 dep->type = 0;
Baolin Wang76a638f2016-10-31 19:38:36 +0800743 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +0300744
John Youn39ebb052016-11-09 16:36:28 -0800745 /* Clear out the ep descriptors for non-ep0 */
746 if (dep->number > 1) {
747 dep->endpoint.comp_desc = NULL;
748 dep->endpoint.desc = NULL;
749 }
750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 return 0;
752}
753
754/* -------------------------------------------------------------------------- */
755
756static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
757 const struct usb_endpoint_descriptor *desc)
758{
759 return -EINVAL;
760}
761
762static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
763{
764 return -EINVAL;
765}
766
767/* -------------------------------------------------------------------------- */
768
769static int dwc3_gadget_ep_enable(struct usb_ep *ep,
770 const struct usb_endpoint_descriptor *desc)
771{
772 struct dwc3_ep *dep;
773 struct dwc3 *dwc;
774 unsigned long flags;
775 int ret;
776
777 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
778 pr_debug("dwc3: invalid parameters\n");
779 return -EINVAL;
780 }
781
782 if (!desc->wMaxPacketSize) {
783 pr_debug("dwc3: missing wMaxPacketSize\n");
784 return -EINVAL;
785 }
786
787 dep = to_dwc3_ep(ep);
788 dwc = dep->dwc;
789
Felipe Balbi95ca9612015-12-10 13:08:20 -0600790 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
791 "%s is already enabled\n",
792 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300793 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300794
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbia2d23f02018-04-09 12:40:48 +0300796 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 spin_unlock_irqrestore(&dwc->lock, flags);
798
799 return ret;
800}
801
802static int dwc3_gadget_ep_disable(struct usb_ep *ep)
803{
804 struct dwc3_ep *dep;
805 struct dwc3 *dwc;
806 unsigned long flags;
807 int ret;
808
809 if (!ep) {
810 pr_debug("dwc3: invalid parameters\n");
811 return -EINVAL;
812 }
813
814 dep = to_dwc3_ep(ep);
815 dwc = dep->dwc;
816
Felipe Balbi95ca9612015-12-10 13:08:20 -0600817 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
818 "%s is already disabled\n",
819 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300820 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300821
Felipe Balbi72246da2011-08-19 18:10:58 +0300822 spin_lock_irqsave(&dwc->lock, flags);
823 ret = __dwc3_gadget_ep_disable(dep);
824 spin_unlock_irqrestore(&dwc->lock, flags);
825
826 return ret;
827}
828
829static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +0300830 gfp_t gfp_flags)
Felipe Balbi72246da2011-08-19 18:10:58 +0300831{
832 struct dwc3_request *req;
833 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300834
835 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900836 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300837 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838
839 req->epnum = dep->number;
840 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300841
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500842 trace_dwc3_alloc_request(req);
843
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 return &req->request;
845}
846
847static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
848 struct usb_request *request)
849{
850 struct dwc3_request *req = to_dwc3_request(request);
851
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500852 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 kfree(req);
854}
855
Felipe Balbi2c78c022016-08-12 13:13:10 +0300856static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
857
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200858static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
859 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
860 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
Felipe Balbic71fc372011-11-22 11:37:34 +0200861{
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300862 struct dwc3 *dwc = dep->dwc;
863 struct usb_gadget *gadget = &dwc->gadget;
864 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200865
Felipe Balbief966b92016-04-05 13:09:51 +0300866 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530867
Felipe Balbif6bafc62012-02-06 11:04:53 +0200868 trb->size = DWC3_TRB_SIZE_LENGTH(length);
869 trb->bpl = lower_32_bits(dma);
870 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200871
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200872 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200873 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200874 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200875 break;
876
877 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300878 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530879 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300880
Manu Gautam40d829f2017-07-19 17:07:10 +0530881 /*
882 * USB Specification 2.0 Section 5.9.2 states that: "If
883 * there is only a single transaction in the microframe,
884 * only a DATA0 data packet PID is used. If there are
885 * two transactions per microframe, DATA1 is used for
886 * the first transaction data packet and DATA0 is used
887 * for the second transaction data packet. If there are
888 * three transactions per microframe, DATA2 is used for
889 * the first transaction data packet, DATA1 is used for
890 * the second, and DATA0 is used for the third."
891 *
892 * IOW, we should satisfy the following cases:
893 *
894 * 1) length <= maxpacket
895 * - DATA0
896 *
897 * 2) maxpacket < length <= (2 * maxpacket)
898 * - DATA1, DATA0
899 *
900 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
901 * - DATA2, DATA1, DATA0
902 */
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300903 if (speed == USB_SPEED_HIGH) {
904 struct usb_ep *ep = &dep->endpoint;
Manu Gautamec5bb872017-12-06 12:49:04 +0530905 unsigned int mult = 2;
Manu Gautam40d829f2017-07-19 17:07:10 +0530906 unsigned int maxp = usb_endpoint_maxp(ep->desc);
907
908 if (length <= (2 * maxp))
909 mult--;
910
911 if (length <= maxp)
912 mult--;
913
914 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300915 }
916 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530917 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi6b9018d2016-09-22 11:01:01 +0300918 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200919
920 /* always enable Interrupt on Missed ISOC */
921 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200922 break;
923
924 case USB_ENDPOINT_XFER_BULK:
925 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200926 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200927 break;
928 default:
929 /*
930 * This is only possible with faulty memory because we
931 * checked it already :)
932 */
Felipe Balbi0a695d42016-10-07 11:20:01 +0300933 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
934 usb_endpoint_type(dep->endpoint.desc));
Felipe Balbic71fc372011-11-22 11:37:34 +0200935 }
936
Felipe Balbica4d44e2016-03-10 13:53:27 +0200937 /* always enable Continue on Short Packet */
Felipe Balbic9508c82016-10-05 14:26:23 +0300938 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi58f29032016-10-06 17:10:39 +0300939 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600940
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200941 if (short_not_ok)
Felipe Balbic9508c82016-10-05 14:26:23 +0300942 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
943 }
944
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200945 if ((!no_interrupt && !chain) ||
Felipe Balbi2c78c022016-08-12 13:13:10 +0300946 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbic9508c82016-10-05 14:26:23 +0300947 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200948
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530949 if (chain)
950 trb->ctrl |= DWC3_TRB_CTRL_CHN;
951
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200952 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200953 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200954
955 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500956
957 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200958}
959
John Youn361572b2016-05-19 17:26:17 -0700960/**
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200961 * dwc3_prepare_one_trb - setup one TRB from one request
962 * @dep: endpoint for which this request is prepared
963 * @req: dwc3_request pointer
964 * @chain: should this TRB be chained to the next?
965 * @node: only for isochronous endpoints. First TRB needs different type.
966 */
967static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
968 struct dwc3_request *req, unsigned chain, unsigned node)
969{
970 struct dwc3_trb *trb;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +0530971 unsigned int length;
972 dma_addr_t dma;
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200973 unsigned stream_id = req->request.stream_id;
974 unsigned short_not_ok = req->request.short_not_ok;
975 unsigned no_interrupt = req->request.no_interrupt;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +0530976
977 if (req->request.num_sgs > 0) {
978 length = sg_dma_len(req->start_sg);
979 dma = sg_dma_address(req->start_sg);
980 } else {
981 length = req->request.length;
982 dma = req->request.dma;
983 }
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200984
985 trb = &dep->trb_pool[dep->trb_enqueue];
986
987 if (!req->trb) {
988 dwc3_gadget_move_started_request(req);
989 req->trb = trb;
990 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbie49d3cf2017-01-05 14:40:53 +0200991 }
992
993 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
994 stream_id, short_not_ok, no_interrupt);
995}
996
997/**
Felipe Balbibfad65e2017-04-19 14:59:27 +0300998 * dwc3_ep_prev_trb - returns the previous TRB in the ring
John Youn361572b2016-05-19 17:26:17 -0700999 * @dep: The endpoint with the TRB ring
1000 * @index: The index of the current TRB in the ring
1001 *
1002 * Returns the TRB prior to the one pointed to by the index. If the
1003 * index is 0, we will wrap backwards, skip the link TRB, and return
1004 * the one just before that.
1005 */
1006static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1007{
Felipe Balbi45438a02016-08-11 12:26:59 +03001008 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001009
Felipe Balbi45438a02016-08-11 12:26:59 +03001010 if (!tmp)
1011 tmp = DWC3_TRB_NUM - 1;
1012
1013 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001014}
1015
Felipe Balbic4233572016-05-12 14:08:34 +03001016static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1017{
1018 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001019 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001020
1021 /*
1022 * If enqueue & dequeue are equal than it is either full or empty.
1023 *
1024 * One way to know for sure is if the TRB right before us has HWO bit
1025 * set or not. If it has, then we're definitely full and can't fit any
1026 * more transfers in our ring.
1027 */
1028 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001029 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
Felipe Balbi202adaf2017-05-17 13:19:06 +03001030 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
John Youn361572b2016-05-19 17:26:17 -07001031 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001032
1033 return DWC3_TRB_NUM - 1;
1034 }
1035
John Youn9d7aba72016-08-26 18:43:01 -07001036 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001037 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001038
John Youn9d7aba72016-08-26 18:43:01 -07001039 if (dep->trb_dequeue < dep->trb_enqueue)
1040 trbs_left--;
1041
John Youn32db3d92016-05-19 17:26:12 -07001042 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001043}
1044
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001045static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001046 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001047{
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301048 struct scatterlist *sg = req->start_sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001049 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001050 int i;
1051
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301052 unsigned int remaining = req->request.num_mapped_sgs
1053 - req->num_queued_sgs;
1054
1055 for_each_sg(sg, s, remaining, i) {
Felipe Balbic6267a52017-01-05 14:58:46 +02001056 unsigned int length = req->request.length;
1057 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1058 unsigned int rem = length % maxp;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001059 unsigned chain = true;
1060
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001061 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001062 chain = false;
1063
Felipe Balbic6267a52017-01-05 14:58:46 +02001064 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1065 struct dwc3 *dwc = dep->dwc;
1066 struct dwc3_trb *trb;
1067
1068 req->unaligned = true;
1069
1070 /* prepare normal TRB */
1071 dwc3_prepare_one_trb(dep, req, true, i);
1072
1073 /* Now prepare one extra TRB to align transfer size */
1074 trb = &dep->trb_pool[dep->trb_enqueue];
1075 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1076 maxp - rem, false, 0,
1077 req->request.stream_id,
1078 req->request.short_not_ok,
1079 req->request.no_interrupt);
1080 } else {
1081 dwc3_prepare_one_trb(dep, req, chain, i);
1082 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001083
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301084 /*
1085 * There can be a situation where all sgs in sglist are not
1086 * queued because of insufficient trb number. To handle this
1087 * case, update start_sg to next sg to be queued, so that
1088 * we have free trbs we can continue queuing from where we
1089 * previously stopped
1090 */
1091 if (chain)
1092 req->start_sg = sg_next(s);
1093
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301094 req->num_queued_sgs++;
1095
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001096 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001097 break;
1098 }
1099}
1100
1101static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001102 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001103{
Felipe Balbic6267a52017-01-05 14:58:46 +02001104 unsigned int length = req->request.length;
1105 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1106 unsigned int rem = length % maxp;
1107
1108 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1109 struct dwc3 *dwc = dep->dwc;
1110 struct dwc3_trb *trb;
1111
1112 req->unaligned = true;
1113
1114 /* prepare normal TRB */
1115 dwc3_prepare_one_trb(dep, req, true, 0);
1116
1117 /* Now prepare one extra TRB to align transfer size */
1118 trb = &dep->trb_pool[dep->trb_enqueue];
1119 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1120 false, 0, req->request.stream_id,
1121 req->request.short_not_ok,
1122 req->request.no_interrupt);
Felipe Balbid6e5a542017-04-07 16:34:38 +03001123 } else if (req->request.zero && req->request.length &&
1124 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1125 struct dwc3 *dwc = dep->dwc;
1126 struct dwc3_trb *trb;
1127
1128 req->zero = true;
1129
1130 /* prepare normal TRB */
1131 dwc3_prepare_one_trb(dep, req, true, 0);
1132
1133 /* Now prepare one extra TRB to handle ZLP */
1134 trb = &dep->trb_pool[dep->trb_enqueue];
1135 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1136 false, 0, req->request.stream_id,
1137 req->request.short_not_ok,
1138 req->request.no_interrupt);
Felipe Balbic6267a52017-01-05 14:58:46 +02001139 } else {
1140 dwc3_prepare_one_trb(dep, req, false, 0);
1141 }
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001142}
1143
Felipe Balbi72246da2011-08-19 18:10:58 +03001144/*
1145 * dwc3_prepare_trbs - setup TRBs from requests
1146 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001148 * The function goes through the requests list and sets up TRBs for the
1149 * transfers. The function returns once there are no more TRBs available or
1150 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001151 */
Felipe Balbic4233572016-05-12 14:08:34 +03001152static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001153{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001154 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001155
1156 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1157
Felipe Balbid86c5a62016-10-25 13:48:52 +03001158 /*
1159 * We can get in a situation where there's a request in the started list
1160 * but there weren't enough TRBs to fully kick it in the first time
1161 * around, so it has been waiting for more TRBs to be freed up.
1162 *
1163 * In that case, we should check if we have a request with pending_sgs
1164 * in the started list and prepare TRBs for that request first,
1165 * otherwise we will prepare TRBs completely out of order and that will
1166 * break things.
1167 */
1168 list_for_each_entry(req, &dep->started_list, list) {
1169 if (req->num_pending_sgs > 0)
1170 dwc3_prepare_one_trb_sg(dep, req);
1171
1172 if (!dwc3_calc_trbs_left(dep))
1173 return;
1174 }
1175
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001176 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbicdb55b32017-05-17 13:21:14 +03001177 struct dwc3 *dwc = dep->dwc;
1178 int ret;
1179
1180 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1181 dep->direction);
1182 if (ret)
1183 return;
1184
1185 req->sg = req->request.sg;
Anurag Kumar Vulishaa31e63b2018-03-27 16:35:20 +05301186 req->start_sg = req->sg;
Anurag Kumar Vulishac96e6722018-03-27 16:35:21 +05301187 req->num_queued_sgs = 0;
Felipe Balbicdb55b32017-05-17 13:21:14 +03001188 req->num_pending_sgs = req->request.num_mapped_sgs;
1189
Felipe Balbi1f512112016-08-12 13:17:27 +03001190 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001191 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001192 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001193 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001194
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001195 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001196 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001197 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001198}
1199
Felipe Balbi7fdca762017-09-05 14:41:34 +03001200static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001201{
1202 struct dwc3_gadget_ep_cmd_params params;
1203 struct dwc3_request *req;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001204 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001205 int ret;
1206 u32 cmd;
1207
Felipe Balbiccb94eb2017-09-05 14:28:46 +03001208 if (!dwc3_calc_trbs_left(dep))
1209 return 0;
1210
Felipe Balbi1912cbc2018-03-29 11:08:46 +03001211 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
Felipe Balbi72246da2011-08-19 18:10:58 +03001212
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001213 dwc3_prepare_trbs(dep);
1214 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001215 if (!req) {
1216 dep->flags |= DWC3_EP_PENDING_REQUEST;
1217 return 0;
1218 }
1219
1220 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001221
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001222 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301223 params.param0 = upper_32_bits(req->trb_dma);
1224 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001225 cmd = DWC3_DEPCMD_STARTTRANSFER;
1226
1227 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1228 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301229 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001230 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1231 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301232 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001233
Felipe Balbi2cd47182016-04-12 16:42:43 +03001234 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001235 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 /*
1237 * FIXME we need to iterate over the list of requests
1238 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001239 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001240 */
Janusz Dziedzicce3fc8b2016-11-09 11:01:32 +01001241 if (req->trb)
1242 memset(req->trb, 0, sizeof(struct dwc3_trb));
Felipe Balbic91815b2018-03-26 13:14:47 +03001243 dwc3_gadget_del_and_unmap_request(dep, req, ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 return ret;
1245 }
1246
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001247 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001248 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001249 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001250 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001251
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 return 0;
1253}
1254
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001255static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1256{
1257 u32 reg;
1258
1259 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1260 return DWC3_DSTS_SOFFN(reg);
1261}
1262
Felipe Balbi5828cad2018-03-27 11:14:31 +03001263static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301264{
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001265 if (list_empty(&dep->pending_list)) {
Felipe Balbi8f608e82018-03-27 10:53:29 +03001266 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
Felipe Balbi73815282015-01-27 13:48:14 -06001267 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301268 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301269 return;
1270 }
1271
John Younaf771d72017-01-26 11:58:40 -08001272 /*
1273 * Schedule the first trb for one interval in the future or at
1274 * least 4 microframes.
1275 */
Felipe Balbi5828cad2018-03-27 11:14:31 +03001276 dep->frame_number += max_t(u32, 4, dep->interval);
Felipe Balbi7fdca762017-09-05 14:41:34 +03001277 __dwc3_gadget_kick_transfer(dep);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301278}
1279
Felipe Balbi72246da2011-08-19 18:10:58 +03001280static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1281{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001282 struct dwc3 *dwc = dep->dwc;
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001283
Felipe Balbibb423982015-11-16 15:31:21 -06001284 if (!dep->endpoint.desc) {
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001285 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1286 dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001287 return -ESHUTDOWN;
1288 }
1289
Felipe Balbi04fb3652017-05-17 15:57:45 +03001290 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1291 &req->request, req->dep->name))
Felipe Balbibb423982015-11-16 15:31:21 -06001292 return -EINVAL;
Felipe Balbibb423982015-11-16 15:31:21 -06001293
Felipe Balbifc8bb912016-05-16 13:14:48 +03001294 pm_runtime_get(dwc->dev);
1295
Felipe Balbi72246da2011-08-19 18:10:58 +03001296 req->request.actual = 0;
1297 req->request.status = -EINPROGRESS;
1298 req->direction = dep->direction;
1299 req->epnum = dep->number;
1300
Felipe Balbife84f522015-09-01 09:01:38 -05001301 trace_dwc3_ep_queue(req);
1302
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001303 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001304
Felipe Balbid889c232016-09-29 15:44:29 +03001305 /*
1306 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1307 * wait for a XferNotReady event so we will know what's the current
1308 * (micro-)frame number.
1309 *
1310 * Without this trick, we are very, very likely gonna get Bus Expiry
1311 * errors which will force us issue EndTransfer command.
1312 */
1313 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbife990ce2018-03-29 13:23:53 +03001314 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1315 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
Roger Quadrosf1d68262017-04-21 15:58:08 +03001316 return 0;
Felipe Balbife990ce2018-03-29 13:23:53 +03001317
1318 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1319 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1320 __dwc3_gadget_start_isoc(dep);
1321 return 0;
1322 }
Felipe Balbi08a36b52016-08-11 14:27:52 +03001323 }
Felipe Balbib511e5e2012-06-06 12:00:50 +03001324 }
1325
Felipe Balbi7fdca762017-09-05 14:41:34 +03001326 return __dwc3_gadget_kick_transfer(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001327}
1328
1329static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1330 gfp_t gfp_flags)
1331{
1332 struct dwc3_request *req = to_dwc3_request(request);
1333 struct dwc3_ep *dep = to_dwc3_ep(ep);
1334 struct dwc3 *dwc = dep->dwc;
1335
1336 unsigned long flags;
1337
1338 int ret;
1339
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001340 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001341 ret = __dwc3_gadget_ep_queue(dep, req);
1342 spin_unlock_irqrestore(&dwc->lock, flags);
1343
1344 return ret;
1345}
1346
1347static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1348 struct usb_request *request)
1349{
1350 struct dwc3_request *req = to_dwc3_request(request);
1351 struct dwc3_request *r = NULL;
1352
1353 struct dwc3_ep *dep = to_dwc3_ep(ep);
1354 struct dwc3 *dwc = dep->dwc;
1355
1356 unsigned long flags;
1357 int ret = 0;
1358
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001359 trace_dwc3_ep_dequeue(req);
1360
Felipe Balbi72246da2011-08-19 18:10:58 +03001361 spin_lock_irqsave(&dwc->lock, flags);
1362
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001363 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001364 if (r == req)
1365 break;
1366 }
1367
1368 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001369 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001370 if (r == req)
1371 break;
1372 }
1373 if (r == req) {
1374 /* wait until it is processed */
Felipe Balbi8f608e82018-03-27 10:53:29 +03001375 dwc3_stop_active_transfer(dep, true);
Felipe Balbicf3113d2017-02-17 11:12:44 +02001376
1377 /*
1378 * If request was already started, this means we had to
1379 * stop the transfer. With that we also need to ignore
1380 * all TRBs used by the request, however TRBs can only
1381 * be modified after completion of END_TRANSFER
1382 * command. So what we do here is that we wait for
1383 * END_TRANSFER completion and only after that, we jump
1384 * over TRBs by clearing HWO and incrementing dequeue
1385 * pointer.
1386 *
1387 * Note that we have 2 possible types of transfers here:
1388 *
1389 * i) Linear buffer request
1390 * ii) SG-list based request
1391 *
1392 * SG-list based requests will have r->num_pending_sgs
1393 * set to a valid number (> 0). Linear requests,
1394 * normally use a single TRB.
1395 *
1396 * For each of these two cases, if r->unaligned flag is
1397 * set, one extra TRB has been used to align transfer
1398 * size to wMaxPacketSize.
1399 *
1400 * All of these cases need to be taken into
1401 * consideration so we don't mess up our TRB ring
1402 * pointers.
1403 */
1404 wait_event_lock_irq(dep->wait_end_transfer,
1405 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1406 dwc->lock);
1407
1408 if (!r->trb)
1409 goto out1;
1410
1411 if (r->num_pending_sgs) {
1412 struct dwc3_trb *trb;
1413 int i = 0;
1414
1415 for (i = 0; i < r->num_pending_sgs; i++) {
1416 trb = r->trb + i;
1417 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1418 dwc3_ep_inc_deq(dep);
1419 }
1420
Felipe Balbid6e5a542017-04-07 16:34:38 +03001421 if (r->unaligned || r->zero) {
Felipe Balbicf3113d2017-02-17 11:12:44 +02001422 trb = r->trb + r->num_pending_sgs + 1;
1423 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1424 dwc3_ep_inc_deq(dep);
1425 }
1426 } else {
1427 struct dwc3_trb *trb = r->trb;
1428
1429 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1430 dwc3_ep_inc_deq(dep);
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 + 1;
1434 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1435 dwc3_ep_inc_deq(dep);
1436 }
1437 }
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301438 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001439 }
Felipe Balbi04fb3652017-05-17 15:57:45 +03001440 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001441 request, ep->name);
1442 ret = -EINVAL;
1443 goto out0;
1444 }
1445
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301446out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001447 /* giveback the request */
Felipe Balbi0bd0f6d2018-03-26 16:09:00 +03001448
Felipe Balbi72246da2011-08-19 18:10:58 +03001449 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1450
1451out0:
1452 spin_unlock_irqrestore(&dwc->lock, flags);
1453
1454 return ret;
1455}
1456
Felipe Balbi7a608552014-09-24 14:19:52 -05001457int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001458{
1459 struct dwc3_gadget_ep_cmd_params params;
1460 struct dwc3 *dwc = dep->dwc;
1461 int ret;
1462
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001463 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1464 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1465 return -EINVAL;
1466 }
1467
Felipe Balbi72246da2011-08-19 18:10:58 +03001468 memset(&params, 0x00, sizeof(params));
1469
1470 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001471 struct dwc3_trb *trb;
1472
1473 unsigned transfer_in_flight;
1474 unsigned started;
1475
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001476 if (dep->flags & DWC3_EP_STALL)
1477 return 0;
1478
Felipe Balbi69450c42016-05-30 13:37:02 +03001479 if (dep->number > 1)
1480 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1481 else
1482 trb = &dwc->ep0_trb[dep->trb_enqueue];
1483
1484 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1485 started = !list_empty(&dep->started_list);
1486
1487 if (!protocol && ((dep->direction && transfer_in_flight) ||
1488 (!dep->direction && started))) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001489 return -EAGAIN;
1490 }
1491
Felipe Balbi2cd47182016-04-12 16:42:43 +03001492 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1493 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001495 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001496 dep->name);
1497 else
1498 dep->flags |= DWC3_EP_STALL;
1499 } else {
Felipe Balbiffb80fc2017-01-19 13:38:42 +02001500 if (!(dep->flags & DWC3_EP_STALL))
1501 return 0;
Felipe Balbi2cd47182016-04-12 16:42:43 +03001502
John Youn50c763f2016-05-31 17:49:56 -07001503 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001504 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001505 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001506 dep->name);
1507 else
Alan Sterna535d812013-11-01 12:05:12 -04001508 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001510
Felipe Balbi72246da2011-08-19 18:10:58 +03001511 return ret;
1512}
1513
1514static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1515{
1516 struct dwc3_ep *dep = to_dwc3_ep(ep);
1517 struct dwc3 *dwc = dep->dwc;
1518
1519 unsigned long flags;
1520
1521 int ret;
1522
1523 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001524 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001525 spin_unlock_irqrestore(&dwc->lock, flags);
1526
1527 return ret;
1528}
1529
1530static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1531{
1532 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001533 struct dwc3 *dwc = dep->dwc;
1534 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001535 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001536
Paul Zimmerman249a4562012-02-24 17:32:16 -08001537 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001538 dep->flags |= DWC3_EP_WEDGE;
1539
Pratyush Anand08f0d962012-06-25 22:40:43 +05301540 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001541 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301542 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001543 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001544 spin_unlock_irqrestore(&dwc->lock, flags);
1545
1546 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001547}
1548
1549/* -------------------------------------------------------------------------- */
1550
1551static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1552 .bLength = USB_DT_ENDPOINT_SIZE,
1553 .bDescriptorType = USB_DT_ENDPOINT,
1554 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1555};
1556
1557static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1558 .enable = dwc3_gadget_ep0_enable,
1559 .disable = dwc3_gadget_ep0_disable,
1560 .alloc_request = dwc3_gadget_ep_alloc_request,
1561 .free_request = dwc3_gadget_ep_free_request,
1562 .queue = dwc3_gadget_ep0_queue,
1563 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301564 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001565 .set_wedge = dwc3_gadget_ep_set_wedge,
1566};
1567
1568static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1569 .enable = dwc3_gadget_ep_enable,
1570 .disable = dwc3_gadget_ep_disable,
1571 .alloc_request = dwc3_gadget_ep_alloc_request,
1572 .free_request = dwc3_gadget_ep_free_request,
1573 .queue = dwc3_gadget_ep_queue,
1574 .dequeue = dwc3_gadget_ep_dequeue,
1575 .set_halt = dwc3_gadget_ep_set_halt,
1576 .set_wedge = dwc3_gadget_ep_set_wedge,
1577};
1578
1579/* -------------------------------------------------------------------------- */
1580
1581static int dwc3_gadget_get_frame(struct usb_gadget *g)
1582{
1583 struct dwc3 *dwc = gadget_to_dwc(g);
Felipe Balbi72246da2011-08-19 18:10:58 +03001584
Felipe Balbi6cb2e4e32016-10-21 13:07:09 +03001585 return __dwc3_gadget_get_frame(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001586}
1587
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001588static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001589{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001590 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001591
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001592 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001593 u32 reg;
1594
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 u8 link_state;
1596 u8 speed;
1597
Felipe Balbi72246da2011-08-19 18:10:58 +03001598 /*
1599 * According to the Databook Remote wakeup request should
1600 * be issued only when the device is in early suspend state.
1601 *
1602 * We can check that via USB Link State bits in DSTS register.
1603 */
1604 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1605
1606 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001607 if ((speed == DWC3_DSTS_SUPERSPEED) ||
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02001608 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi6b742892016-05-13 10:19:42 +03001609 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001610
1611 link_state = DWC3_DSTS_USBLNKST(reg);
1612
1613 switch (link_state) {
1614 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1615 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1616 break;
1617 default:
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001618 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001619 }
1620
Felipe Balbi8598bde2012-01-02 18:55:57 +02001621 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1622 if (ret < 0) {
1623 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001624 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001625 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001626
Paul Zimmerman802fde92012-04-27 13:10:52 +03001627 /* Recent versions do this automatically */
1628 if (dwc->revision < DWC3_REVISION_194A) {
1629 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001630 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001631 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1632 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1633 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001634
Paul Zimmerman1d046792012-02-15 18:56:56 -08001635 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001636 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001637
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001638 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001639 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1640
1641 /* in HS, means ON */
1642 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1643 break;
1644 }
1645
1646 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1647 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001648 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001649 }
1650
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001651 return 0;
1652}
1653
1654static int dwc3_gadget_wakeup(struct usb_gadget *g)
1655{
1656 struct dwc3 *dwc = gadget_to_dwc(g);
1657 unsigned long flags;
1658 int ret;
1659
1660 spin_lock_irqsave(&dwc->lock, flags);
1661 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001662 spin_unlock_irqrestore(&dwc->lock, flags);
1663
1664 return ret;
1665}
1666
1667static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1668 int is_selfpowered)
1669{
1670 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001671 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001672
Paul Zimmerman249a4562012-02-24 17:32:16 -08001673 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001674 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001675 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001676
1677 return 0;
1678}
1679
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001680static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001681{
1682 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001683 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001684
Felipe Balbifc8bb912016-05-16 13:14:48 +03001685 if (pm_runtime_suspended(dwc->dev))
1686 return 0;
1687
Felipe Balbi72246da2011-08-19 18:10:58 +03001688 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001689 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001690 if (dwc->revision <= DWC3_REVISION_187A) {
1691 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1692 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1693 }
1694
1695 if (dwc->revision >= DWC3_REVISION_194A)
1696 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1697 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001698
1699 if (dwc->has_hibernation)
1700 reg |= DWC3_DCTL_KEEP_CONNECT;
1701
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001702 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001703 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001704 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001705
1706 if (dwc->has_hibernation && !suspend)
1707 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1708
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001709 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001710 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001711
1712 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1713
1714 do {
1715 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001716 reg &= DWC3_DSTS_DEVCTRLHLT;
1717 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001718
1719 if (!timeout)
1720 return -ETIMEDOUT;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721
Pratyush Anand6f17f742012-07-02 10:21:55 +05301722 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001723}
1724
1725static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1726{
1727 struct dwc3 *dwc = gadget_to_dwc(g);
1728 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301729 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001730
1731 is_on = !!is_on;
1732
Baolin Wangbb014732016-10-14 17:11:33 +08001733 /*
1734 * Per databook, when we want to stop the gadget, if a control transfer
1735 * is still in process, complete it and get the core into setup phase.
1736 */
1737 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1738 reinit_completion(&dwc->ep0_in_setup);
1739
1740 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1741 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1742 if (ret == 0) {
1743 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1744 return -ETIMEDOUT;
1745 }
1746 }
1747
Felipe Balbi72246da2011-08-19 18:10:58 +03001748 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001749 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001750 spin_unlock_irqrestore(&dwc->lock, flags);
1751
Pratyush Anand6f17f742012-07-02 10:21:55 +05301752 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001753}
1754
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001755static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1756{
1757 u32 reg;
1758
1759 /* Enable all but Start and End of Frame IRQs */
1760 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1761 DWC3_DEVTEN_EVNTOVERFLOWEN |
1762 DWC3_DEVTEN_CMDCMPLTEN |
1763 DWC3_DEVTEN_ERRTICERREN |
1764 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001765 DWC3_DEVTEN_CONNECTDONEEN |
1766 DWC3_DEVTEN_USBRSTEN |
1767 DWC3_DEVTEN_DISCONNEVTEN);
1768
Felipe Balbi799e9dc2016-09-23 11:20:40 +03001769 if (dwc->revision < DWC3_REVISION_250A)
1770 reg |= DWC3_DEVTEN_ULSTCNGEN;
1771
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001772 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1773}
1774
1775static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1776{
1777 /* mask all interrupts */
1778 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1779}
1780
1781static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001782static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001783
Felipe Balbi4e994722016-05-13 14:09:59 +03001784/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03001785 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1786 * @dwc: pointer to our context structure
Felipe Balbi4e994722016-05-13 14:09:59 +03001787 *
1788 * The following looks like complex but it's actually very simple. In order to
1789 * calculate the number of packets we can burst at once on OUT transfers, we're
1790 * gonna use RxFIFO size.
1791 *
1792 * To calculate RxFIFO size we need two numbers:
1793 * MDWIDTH = size, in bits, of the internal memory bus
1794 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1795 *
1796 * Given these two numbers, the formula is simple:
1797 *
1798 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1799 *
1800 * 24 bytes is for 3x SETUP packets
1801 * 16 bytes is a clock domain crossing tolerance
1802 *
1803 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1804 */
1805static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1806{
1807 u32 ram2_depth;
1808 u32 mdwidth;
1809 u32 nump;
1810 u32 reg;
1811
1812 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1813 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1814
1815 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1816 nump = min_t(u32, nump, 16);
1817
1818 /* update NumP */
1819 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1820 reg &= ~DWC3_DCFG_NUMP_MASK;
1821 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1822 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1823}
1824
Felipe Balbid7be2952016-05-04 15:49:37 +03001825static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001826{
Felipe Balbi72246da2011-08-19 18:10:58 +03001827 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001828 int ret = 0;
1829 u32 reg;
1830
John Youncf40b862016-11-14 12:32:43 -08001831 /*
1832 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1833 * the core supports IMOD, disable it.
1834 */
1835 if (dwc->imod_interval) {
1836 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1837 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1838 } else if (dwc3_has_imod(dwc)) {
1839 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1840 }
1841
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001842 /*
1843 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1844 * field instead of letting dwc3 itself calculate that automatically.
1845 *
1846 * This way, we maximize the chances that we'll be able to get several
1847 * bursts of data without going through any sort of endpoint throttling.
1848 */
1849 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
Thinh Nguyen01b0e2c2018-03-16 15:34:13 -07001850 if (dwc3_is_usb31(dwc))
1851 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1852 else
1853 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1854
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001855 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1856
Felipe Balbi4e994722016-05-13 14:09:59 +03001857 dwc3_gadget_setup_nump(dwc);
1858
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 /* Start with SuperSpeed Default */
1860 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1861
1862 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001863 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001864 if (ret) {
1865 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001866 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001867 }
1868
1869 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03001870 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
Felipe Balbi72246da2011-08-19 18:10:58 +03001871 if (ret) {
1872 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001873 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001874 }
1875
1876 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001877 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 dwc3_ep0_out_start(dwc);
1879
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001880 dwc3_gadget_enable_irq(dwc);
1881
Felipe Balbid7be2952016-05-04 15:49:37 +03001882 return 0;
1883
1884err1:
1885 __dwc3_gadget_ep_disable(dwc->eps[0]);
1886
1887err0:
1888 return ret;
1889}
1890
1891static int dwc3_gadget_start(struct usb_gadget *g,
1892 struct usb_gadget_driver *driver)
1893{
1894 struct dwc3 *dwc = gadget_to_dwc(g);
1895 unsigned long flags;
1896 int ret = 0;
1897 int irq;
1898
Roger Quadros9522def2016-06-10 14:48:38 +03001899 irq = dwc->irq_gadget;
Felipe Balbid7be2952016-05-04 15:49:37 +03001900 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1901 IRQF_SHARED, "dwc3", dwc->ev_buf);
1902 if (ret) {
1903 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1904 irq, ret);
1905 goto err0;
1906 }
1907
1908 spin_lock_irqsave(&dwc->lock, flags);
1909 if (dwc->gadget_driver) {
1910 dev_err(dwc->dev, "%s is already bound to %s\n",
1911 dwc->gadget.name,
1912 dwc->gadget_driver->driver.name);
1913 ret = -EBUSY;
1914 goto err1;
1915 }
1916
1917 dwc->gadget_driver = driver;
1918
Felipe Balbifc8bb912016-05-16 13:14:48 +03001919 if (pm_runtime_active(dwc->dev))
1920 __dwc3_gadget_start(dwc);
1921
Felipe Balbi72246da2011-08-19 18:10:58 +03001922 spin_unlock_irqrestore(&dwc->lock, flags);
1923
1924 return 0;
1925
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001926err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001927 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001928 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001929
1930err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001931 return ret;
1932}
1933
Felipe Balbid7be2952016-05-04 15:49:37 +03001934static void __dwc3_gadget_stop(struct dwc3 *dwc)
1935{
1936 dwc3_gadget_disable_irq(dwc);
1937 __dwc3_gadget_ep_disable(dwc->eps[0]);
1938 __dwc3_gadget_ep_disable(dwc->eps[1]);
1939}
1940
Felipe Balbi22835b82014-10-17 12:05:12 -05001941static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001942{
1943 struct dwc3 *dwc = gadget_to_dwc(g);
1944 unsigned long flags;
Baolin Wang76a638f2016-10-31 19:38:36 +08001945 int epnum;
Roger Quadros498f0472018-03-09 14:47:04 +02001946 u32 tmo_eps = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001947
1948 spin_lock_irqsave(&dwc->lock, flags);
Baolin Wang76a638f2016-10-31 19:38:36 +08001949
1950 if (pm_runtime_suspended(dwc->dev))
1951 goto out;
1952
Felipe Balbid7be2952016-05-04 15:49:37 +03001953 __dwc3_gadget_stop(dwc);
Baolin Wang76a638f2016-10-31 19:38:36 +08001954
1955 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1956 struct dwc3_ep *dep = dwc->eps[epnum];
Roger Quadros498f0472018-03-09 14:47:04 +02001957 int ret;
Baolin Wang76a638f2016-10-31 19:38:36 +08001958
1959 if (!dep)
1960 continue;
1961
1962 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1963 continue;
1964
Roger Quadros498f0472018-03-09 14:47:04 +02001965 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
1966 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1967 dwc->lock, msecs_to_jiffies(5));
1968
1969 if (ret <= 0) {
1970 /* Timed out or interrupted! There's nothing much
1971 * we can do so we just log here and print which
1972 * endpoints timed out at the end.
1973 */
1974 tmo_eps |= 1 << epnum;
1975 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
1976 }
1977 }
1978
1979 if (tmo_eps) {
1980 dev_err(dwc->dev,
1981 "end transfer timed out on endpoints 0x%x [bitmap]\n",
1982 tmo_eps);
Baolin Wang76a638f2016-10-31 19:38:36 +08001983 }
1984
1985out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 spin_unlock_irqrestore(&dwc->lock, flags);
1988
Felipe Balbi3f308d12016-05-16 14:17:06 +03001989 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001990
Felipe Balbi72246da2011-08-19 18:10:58 +03001991 return 0;
1992}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001993
Felipe Balbi7d8d0632017-06-06 16:05:23 +03001994static void dwc3_gadget_set_speed(struct usb_gadget *g,
1995 enum usb_device_speed speed)
1996{
1997 struct dwc3 *dwc = gadget_to_dwc(g);
1998 unsigned long flags;
1999 u32 reg;
2000
2001 spin_lock_irqsave(&dwc->lock, flags);
2002 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2003 reg &= ~(DWC3_DCFG_SPEED_MASK);
2004
2005 /*
2006 * WORKAROUND: DWC3 revision < 2.20a have an issue
2007 * which would cause metastability state on Run/Stop
2008 * bit if we try to force the IP to USB2-only mode.
2009 *
2010 * Because of that, we cannot configure the IP to any
2011 * speed other than the SuperSpeed
2012 *
2013 * Refers to:
2014 *
2015 * STAR#9000525659: Clock Domain Crossing on DCTL in
2016 * USB 2.0 Mode
2017 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02002018 if (dwc->revision < DWC3_REVISION_220A &&
2019 !dwc->dis_metastability_quirk) {
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002020 reg |= DWC3_DCFG_SUPERSPEED;
2021 } else {
2022 switch (speed) {
2023 case USB_SPEED_LOW:
2024 reg |= DWC3_DCFG_LOWSPEED;
2025 break;
2026 case USB_SPEED_FULL:
2027 reg |= DWC3_DCFG_FULLSPEED;
2028 break;
2029 case USB_SPEED_HIGH:
2030 reg |= DWC3_DCFG_HIGHSPEED;
2031 break;
2032 case USB_SPEED_SUPER:
2033 reg |= DWC3_DCFG_SUPERSPEED;
2034 break;
2035 case USB_SPEED_SUPER_PLUS:
Thinh Nguyen2f3090c2018-03-16 15:35:57 -07002036 if (dwc3_is_usb31(dwc))
2037 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2038 else
2039 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002040 break;
2041 default:
2042 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2043
2044 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2045 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2046 else
2047 reg |= DWC3_DCFG_SUPERSPEED;
2048 }
2049 }
2050 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2051
2052 spin_unlock_irqrestore(&dwc->lock, flags);
2053}
2054
Felipe Balbi72246da2011-08-19 18:10:58 +03002055static const struct usb_gadget_ops dwc3_gadget_ops = {
2056 .get_frame = dwc3_gadget_get_frame,
2057 .wakeup = dwc3_gadget_wakeup,
2058 .set_selfpowered = dwc3_gadget_set_selfpowered,
2059 .pullup = dwc3_gadget_pullup,
2060 .udc_start = dwc3_gadget_start,
2061 .udc_stop = dwc3_gadget_stop,
Felipe Balbi7d8d0632017-06-06 16:05:23 +03002062 .udc_set_speed = dwc3_gadget_set_speed,
Felipe Balbi72246da2011-08-19 18:10:58 +03002063};
2064
2065/* -------------------------------------------------------------------------- */
2066
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002067static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2068{
2069 struct dwc3 *dwc = dep->dwc;
2070
2071 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2072 dep->endpoint.maxburst = 1;
2073 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2074 if (!dep->direction)
2075 dwc->gadget.ep0 = &dep->endpoint;
2076
2077 dep->endpoint.caps.type_control = true;
2078
2079 return 0;
2080}
2081
2082static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2083{
2084 struct dwc3 *dwc = dep->dwc;
2085 int mdwidth;
2086 int kbytes;
2087 int size;
2088
2089 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2090 /* MDWIDTH is represented in bits, we need it in bytes */
2091 mdwidth /= 8;
2092
2093 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2094 if (dwc3_is_usb31(dwc))
2095 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2096 else
2097 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2098
2099 /* FIFO Depth is in MDWDITH bytes. Multiply */
2100 size *= mdwidth;
2101
2102 kbytes = size / 1024;
2103 if (kbytes == 0)
2104 kbytes = 1;
2105
2106 /*
2107 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2108 * internal overhead. We don't really know how these are used,
2109 * but documentation say it exists.
2110 */
2111 size -= mdwidth * (kbytes + 1);
2112 size /= kbytes;
2113
2114 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2115
2116 dep->endpoint.max_streams = 15;
2117 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2118 list_add_tail(&dep->endpoint.ep_list,
2119 &dwc->gadget.ep_list);
2120 dep->endpoint.caps.type_iso = true;
2121 dep->endpoint.caps.type_bulk = true;
2122 dep->endpoint.caps.type_int = true;
2123
2124 return dwc3_alloc_trb_pool(dep);
2125}
2126
2127static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2128{
2129 struct dwc3 *dwc = dep->dwc;
2130
2131 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2132 dep->endpoint.max_streams = 15;
2133 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2134 list_add_tail(&dep->endpoint.ep_list,
2135 &dwc->gadget.ep_list);
2136 dep->endpoint.caps.type_iso = true;
2137 dep->endpoint.caps.type_bulk = true;
2138 dep->endpoint.caps.type_int = true;
2139
2140 return dwc3_alloc_trb_pool(dep);
2141}
2142
2143static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
Felipe Balbi72246da2011-08-19 18:10:58 +03002144{
2145 struct dwc3_ep *dep;
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002146 bool direction = epnum & 1;
2147 int ret;
2148 u8 num = epnum >> 1;
2149
2150 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2151 if (!dep)
2152 return -ENOMEM;
2153
2154 dep->dwc = dwc;
2155 dep->number = epnum;
2156 dep->direction = direction;
2157 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2158 dwc->eps[epnum] = dep;
2159
2160 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2161 direction ? "in" : "out");
2162
2163 dep->endpoint.name = dep->name;
2164
2165 if (!(dep->number > 1)) {
2166 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2167 dep->endpoint.comp_desc = NULL;
2168 }
2169
2170 spin_lock_init(&dep->lock);
2171
2172 if (num == 0)
2173 ret = dwc3_gadget_init_control_endpoint(dep);
2174 else if (direction)
2175 ret = dwc3_gadget_init_in_endpoint(dep);
2176 else
2177 ret = dwc3_gadget_init_out_endpoint(dep);
2178
2179 if (ret)
2180 return ret;
2181
2182 dep->endpoint.caps.dir_in = direction;
2183 dep->endpoint.caps.dir_out = !direction;
2184
2185 INIT_LIST_HEAD(&dep->pending_list);
2186 INIT_LIST_HEAD(&dep->started_list);
2187
2188 return 0;
2189}
2190
2191static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2192{
Bryan O'Donoghue47d39462017-01-31 20:58:10 +00002193 u8 epnum;
Felipe Balbi72246da2011-08-19 18:10:58 +03002194
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00002195 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2196
Andy Shevchenko46b780d2017-06-12 15:11:25 +03002197 for (epnum = 0; epnum < total; epnum++) {
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002198 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002199
Felipe Balbi8f1c99c2018-04-09 11:06:09 +03002200 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2201 if (ret)
2202 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002203 }
2204
2205 return 0;
2206}
2207
2208static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2209{
2210 struct dwc3_ep *dep;
2211 u8 epnum;
2212
2213 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2214 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002215 if (!dep)
2216 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302217 /*
2218 * Physical endpoints 0 and 1 are special; they form the
2219 * bi-directional USB endpoint 0.
2220 *
2221 * For those two physical endpoints, we don't allocate a TRB
2222 * pool nor do we add them the endpoints list. Due to that, we
2223 * shouldn't do these two operations otherwise we would end up
2224 * with all sorts of bugs when removing dwc3.ko.
2225 */
2226 if (epnum != 0 && epnum != 1) {
2227 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002228 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302229 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002230
2231 kfree(dep);
2232 }
2233}
2234
Felipe Balbi72246da2011-08-19 18:10:58 +03002235/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002236
Felipe Balbi8f608e82018-03-27 10:53:29 +03002237static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2238 struct dwc3_request *req, struct dwc3_trb *trb,
2239 const struct dwc3_event_depevt *event, int status, int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302240{
2241 unsigned int count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302242
Felipe Balbidc55c672016-08-12 13:20:32 +03002243 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002244
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002245 trace_dwc3_complete_trb(dep, trb);
2246
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002247 /*
2248 * If we're in the middle of series of chained TRBs and we
2249 * receive a short transfer along the way, DWC3 will skip
2250 * through all TRBs including the last TRB in the chain (the
2251 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2252 * bit and SW has to do it manually.
2253 *
2254 * We're going to do that here to avoid problems of HW trying
2255 * to use bogus TRBs for transfers.
2256 */
2257 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2258 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2259
Felipe Balbic6267a52017-01-05 14:58:46 +02002260 /*
2261 * If we're dealing with unaligned size OUT transfer, we will be left
2262 * with one TRB pending in the ring. We need to manually clear HWO bit
2263 * from that TRB.
2264 */
Felipe Balbid6e5a542017-04-07 16:34:38 +03002265 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
Felipe Balbic6267a52017-01-05 14:58:46 +02002266 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2267 return 1;
2268 }
2269
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302270 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbie62c5bc52016-10-25 13:47:21 +03002271 req->remaining += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302272
Felipe Balbi35b27192017-03-08 13:56:37 +02002273 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2274 return 1;
2275
Felipe Balbid80fe1b2018-04-06 11:04:21 +03002276 if (event->status & DEPEVT_STATUS_SHORT && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302277 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002278
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002279 if (event->status & DEPEVT_STATUS_IOC)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302280 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002281
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302282 return 0;
2283}
2284
Felipe Balbid3692952018-03-29 13:32:10 +03002285static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2286 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2287 int status)
2288{
2289 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2290 struct scatterlist *sg = req->sg;
2291 struct scatterlist *s;
2292 unsigned int pending = req->num_pending_sgs;
2293 unsigned int i;
2294 int ret = 0;
2295
2296 for_each_sg(sg, s, pending, i) {
2297 trb = &dep->trb_pool[dep->trb_dequeue];
2298
2299 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2300 break;
2301
2302 req->sg = sg_next(s);
2303 req->num_pending_sgs--;
2304
2305 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2306 trb, event, status, true);
2307 if (ret)
2308 break;
2309 }
2310
2311 return ret;
2312}
2313
2314static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2315 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2316 int status)
2317{
2318 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2319
2320 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2321 event, status, false);
2322}
2323
Felipe Balbie0c42ce2018-04-06 15:37:30 +03002324static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2325{
2326 return req->request.actual == req->request.length;
2327}
2328
Felipe Balbif38e35d2018-04-06 15:56:35 +03002329static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2330 const struct dwc3_event_depevt *event,
2331 struct dwc3_request *req, int status)
2332{
2333 int ret;
2334
2335 if (req->num_pending_sgs)
2336 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2337 status);
2338 else
2339 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2340 status);
2341
2342 if (req->unaligned || req->zero) {
2343 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2344 status);
2345 req->unaligned = false;
2346 req->zero = false;
2347 }
2348
2349 req->request.actual = req->request.length - req->remaining;
2350
2351 if (!dwc3_gadget_ep_request_completed(req) &&
2352 req->num_pending_sgs) {
2353 __dwc3_gadget_kick_transfer(dep);
2354 goto out;
2355 }
2356
2357 dwc3_gadget_giveback(dep, req, status);
2358
2359out:
2360 return ret;
2361}
2362
Felipe Balbi12a3a4a2018-03-29 11:53:40 +03002363static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
Felipe Balbi8f608e82018-03-27 10:53:29 +03002364 const struct dwc3_event_depevt *event, int status)
Felipe Balbi72246da2011-08-19 18:10:58 +03002365{
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002366 struct dwc3_request *req;
2367 struct dwc3_request *tmp;
Felipe Balbi72246da2011-08-19 18:10:58 +03002368
Felipe Balbi6afbdb52018-04-06 15:49:49 +03002369 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
Felipe Balbifee73e62018-04-06 15:50:29 +03002370 int ret;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002371
Felipe Balbif38e35d2018-04-06 15:56:35 +03002372 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2373 req, status);
Felipe Balbi58f02182018-03-29 12:10:31 +03002374 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002375 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002376 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002377}
2378
Felipe Balbiee3638b2018-03-27 11:26:53 +03002379static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2380 const struct dwc3_event_depevt *event)
2381{
2382 u32 cur_uf, mask;
2383
2384 mask = ~(dep->interval - 1);
2385 cur_uf = event->parameters & mask;
2386 dep->frame_number = cur_uf;
2387}
2388
Felipe Balbi8f608e82018-03-27 10:53:29 +03002389static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2390 const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002391{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002392 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002393 unsigned status = 0;
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002394 bool stop = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002395
Felipe Balbiee3638b2018-03-27 11:26:53 +03002396 dwc3_gadget_endpoint_frame_from_event(dep, event);
2397
Felipe Balbi72246da2011-08-19 18:10:58 +03002398 if (event->status & DEPEVT_STATUS_BUSERR)
2399 status = -ECONNRESET;
2400
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002401 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2402 status = -EXDEV;
2403 stop = true;
2404 }
2405
Felipe Balbi5f2e7972018-03-29 11:10:45 +03002406 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
Felipe Balbifae2b902011-10-14 13:00:30 +03002407
Felipe Balbi6d8a0192018-03-29 12:49:28 +03002408 if (stop) {
2409 dwc3_stop_active_transfer(dep, true);
2410 dep->flags = DWC3_EP_ENABLED;
2411 }
2412
Felipe Balbifae2b902011-10-14 13:00:30 +03002413 /*
2414 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2415 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2416 */
2417 if (dwc->revision < DWC3_REVISION_183A) {
2418 u32 reg;
2419 int i;
2420
2421 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002422 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002423
2424 if (!(dep->flags & DWC3_EP_ENABLED))
2425 continue;
2426
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002427 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002428 return;
2429 }
2430
2431 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2432 reg |= dwc->u1u2;
2433 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2434
2435 dwc->u1u2 = 0;
2436 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002437}
2438
Felipe Balbi8f608e82018-03-27 10:53:29 +03002439static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2440 const struct dwc3_event_depevt *event)
Felipe Balbi32033862018-03-27 10:47:48 +03002441{
Felipe Balbiee3638b2018-03-27 11:26:53 +03002442 dwc3_gadget_endpoint_frame_from_event(dep, event);
Felipe Balbi5828cad2018-03-27 11:14:31 +03002443 __dwc3_gadget_start_isoc(dep);
Felipe Balbi32033862018-03-27 10:47:48 +03002444}
2445
Felipe Balbi72246da2011-08-19 18:10:58 +03002446static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2447 const struct dwc3_event_depevt *event)
2448{
2449 struct dwc3_ep *dep;
2450 u8 epnum = event->endpoint_number;
Baolin Wang76a638f2016-10-31 19:38:36 +08002451 u8 cmd;
Felipe Balbi72246da2011-08-19 18:10:58 +03002452
2453 dep = dwc->eps[epnum];
2454
Janusz Dziedzicd7fd41c2016-12-08 10:57:34 +01002455 if (!(dep->flags & DWC3_EP_ENABLED)) {
2456 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2457 return;
2458
2459 /* Handle only EPCMDCMPLT when EP disabled */
2460 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2461 return;
2462 }
Felipe Balbi3336abb2012-06-06 09:19:35 +03002463
Felipe Balbi72246da2011-08-19 18:10:58 +03002464 if (epnum == 0 || epnum == 1) {
2465 dwc3_ep0_interrupt(dwc, event);
2466 return;
2467 }
2468
2469 switch (event->endpoint_event) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002470 case DWC3_DEPEVT_XFERINPROGRESS:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002471 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002472 break;
2473 case DWC3_DEPEVT_XFERNOTREADY:
Felipe Balbi8f608e82018-03-27 10:53:29 +03002474 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002475 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002476 case DWC3_DEPEVT_EPCMDCMPLT:
Baolin Wang76a638f2016-10-31 19:38:36 +08002477 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2478
2479 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2480 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2481 wake_up(&dep->wait_end_transfer);
2482 }
2483 break;
Felipe Balbia24a6ab2018-03-27 10:41:39 +03002484 case DWC3_DEPEVT_STREAMEVT:
Felipe Balbi742a4ff2018-03-26 13:26:56 +03002485 case DWC3_DEPEVT_XFERCOMPLETE:
Baolin Wang76a638f2016-10-31 19:38:36 +08002486 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 break;
2488 }
2489}
2490
2491static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2492{
2493 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2494 spin_unlock(&dwc->lock);
2495 dwc->gadget_driver->disconnect(&dwc->gadget);
2496 spin_lock(&dwc->lock);
2497 }
2498}
2499
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002500static void dwc3_suspend_gadget(struct dwc3 *dwc)
2501{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002502 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002503 spin_unlock(&dwc->lock);
2504 dwc->gadget_driver->suspend(&dwc->gadget);
2505 spin_lock(&dwc->lock);
2506 }
2507}
2508
2509static void dwc3_resume_gadget(struct dwc3 *dwc)
2510{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002511 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002512 spin_unlock(&dwc->lock);
2513 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002514 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002515 }
2516}
2517
2518static void dwc3_reset_gadget(struct dwc3 *dwc)
2519{
2520 if (!dwc->gadget_driver)
2521 return;
2522
2523 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2524 spin_unlock(&dwc->lock);
2525 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002526 spin_lock(&dwc->lock);
2527 }
2528}
2529
Felipe Balbi8f608e82018-03-27 10:53:29 +03002530static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002531{
Felipe Balbi8f608e82018-03-27 10:53:29 +03002532 struct dwc3 *dwc = dep->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002533 struct dwc3_gadget_ep_cmd_params params;
2534 u32 cmd;
2535 int ret;
2536
Baolin Wang76a638f2016-10-31 19:38:36 +08002537 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2538 !dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302539 return;
2540
Pratyush Anand57911502012-07-06 15:19:10 +05302541 /*
2542 * NOTICE: We are violating what the Databook says about the
2543 * EndTransfer command. Ideally we would _always_ wait for the
2544 * EndTransfer Command Completion IRQ, but that's causing too
2545 * much trouble synchronizing between us and gadget driver.
2546 *
2547 * We have discussed this with the IP Provider and it was
2548 * suggested to giveback all requests here, but give HW some
2549 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002550 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302551 *
2552 * Note also that a similar handling was tested by Synopsys
2553 * (thanks a lot Paul) and nothing bad has come out of it.
2554 * In short, what we're doing is:
2555 *
2556 * - Issue EndTransfer WITH CMDIOC bit set
2557 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002558 *
2559 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2560 * supports a mode to work around the above limitation. The
2561 * software can poll the CMDACT bit in the DEPCMD register
2562 * after issuing a EndTransfer command. This mode is enabled
2563 * by writing GUCTL2[14]. This polling is already done in the
2564 * dwc3_send_gadget_ep_cmd() function so if the mode is
2565 * enabled, the EndTransfer command will have completed upon
2566 * returning from this function and we don't need to delay for
2567 * 100us.
2568 *
2569 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302570 */
2571
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302572 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002573 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2574 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002575 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302576 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002577 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302578 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002579 dep->resource_index = 0;
John Youn06281d42016-08-22 15:39:13 -07002580
Baolin Wang76a638f2016-10-31 19:38:36 +08002581 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2582 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
John Youn06281d42016-08-22 15:39:13 -07002583 udelay(100);
Baolin Wang76a638f2016-10-31 19:38:36 +08002584 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002585}
2586
Felipe Balbi72246da2011-08-19 18:10:58 +03002587static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2588{
2589 u32 epnum;
2590
2591 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2592 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002593 int ret;
2594
2595 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002596 if (!dep)
2597 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002598
2599 if (!(dep->flags & DWC3_EP_STALL))
2600 continue;
2601
2602 dep->flags &= ~DWC3_EP_STALL;
2603
John Youn50c763f2016-05-31 17:49:56 -07002604 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002605 WARN_ON_ONCE(ret);
2606 }
2607}
2608
2609static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2610{
Felipe Balbic4430a22012-05-24 10:30:01 +03002611 int reg;
2612
Felipe Balbi72246da2011-08-19 18:10:58 +03002613 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2614 reg &= ~DWC3_DCTL_INITU1ENA;
2615 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2616
2617 reg &= ~DWC3_DCTL_INITU2ENA;
2618 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002619
Felipe Balbi72246da2011-08-19 18:10:58 +03002620 dwc3_disconnect_gadget(dwc);
2621
2622 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002623 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002624 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002625
2626 dwc->connected = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002627}
2628
Felipe Balbi72246da2011-08-19 18:10:58 +03002629static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2630{
2631 u32 reg;
2632
Felipe Balbifc8bb912016-05-16 13:14:48 +03002633 dwc->connected = true;
2634
Felipe Balbidf62df52011-10-14 15:11:49 +03002635 /*
2636 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2637 * would cause a missing Disconnect Event if there's a
2638 * pending Setup Packet in the FIFO.
2639 *
2640 * There's no suggested workaround on the official Bug
2641 * report, which states that "unless the driver/application
2642 * is doing any special handling of a disconnect event,
2643 * there is no functional issue".
2644 *
2645 * Unfortunately, it turns out that we _do_ some special
2646 * handling of a disconnect event, namely complete all
2647 * pending transfers, notify gadget driver of the
2648 * disconnection, and so on.
2649 *
2650 * Our suggested workaround is to follow the Disconnect
2651 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002652 * flag. Such flag gets set whenever we have a SETUP_PENDING
2653 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002654 * same endpoint.
2655 *
2656 * Refers to:
2657 *
2658 * STAR#9000466709: RTL: Device : Disconnect event not
2659 * generated if setup packet pending in FIFO
2660 */
2661 if (dwc->revision < DWC3_REVISION_188A) {
2662 if (dwc->setup_packet_pending)
2663 dwc3_gadget_disconnect_interrupt(dwc);
2664 }
2665
Felipe Balbi8e744752014-11-06 14:27:53 +08002666 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002667
2668 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2669 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2670 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002671 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002672 dwc3_clear_stall_all_ep(dwc);
2673
2674 /* Reset device address to zero */
2675 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2676 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2677 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002678}
2679
Felipe Balbi72246da2011-08-19 18:10:58 +03002680static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2681{
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 struct dwc3_ep *dep;
2683 int ret;
2684 u32 reg;
2685 u8 speed;
2686
Felipe Balbi72246da2011-08-19 18:10:58 +03002687 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2688 speed = reg & DWC3_DSTS_CONNECTSPD;
2689 dwc->speed = speed;
2690
John Youn5fb6fda2016-11-10 17:23:25 -08002691 /*
2692 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2693 * each time on Connect Done.
2694 *
2695 * Currently we always use the reset value. If any platform
2696 * wants to set this to a different value, we need to add a
2697 * setting and update GCTL.RAMCLKSEL here.
2698 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002699
2700 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002701 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002702 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2703 dwc->gadget.ep0->maxpacket = 512;
2704 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2705 break;
John Youn2da9ad72016-05-20 16:34:26 -07002706 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002707 /*
2708 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2709 * would cause a missing USB3 Reset event.
2710 *
2711 * In such situations, we should force a USB3 Reset
2712 * event by calling our dwc3_gadget_reset_interrupt()
2713 * routine.
2714 *
2715 * Refers to:
2716 *
2717 * STAR#9000483510: RTL: SS : USB3 reset event may
2718 * not be generated always when the link enters poll
2719 */
2720 if (dwc->revision < DWC3_REVISION_190A)
2721 dwc3_gadget_reset_interrupt(dwc);
2722
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2724 dwc->gadget.ep0->maxpacket = 512;
2725 dwc->gadget.speed = USB_SPEED_SUPER;
2726 break;
John Youn2da9ad72016-05-20 16:34:26 -07002727 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002728 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2729 dwc->gadget.ep0->maxpacket = 64;
2730 dwc->gadget.speed = USB_SPEED_HIGH;
2731 break;
Roger Quadros9418ee12017-01-03 14:32:09 +02002732 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002733 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2734 dwc->gadget.ep0->maxpacket = 64;
2735 dwc->gadget.speed = USB_SPEED_FULL;
2736 break;
John Youn2da9ad72016-05-20 16:34:26 -07002737 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002738 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2739 dwc->gadget.ep0->maxpacket = 8;
2740 dwc->gadget.speed = USB_SPEED_LOW;
2741 break;
2742 }
2743
Thinh Nguyen61800262018-01-12 18:18:05 -08002744 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2745
Pratyush Anand2b758352013-01-14 15:59:31 +05302746 /* Enable USB2 LPM Capability */
2747
John Younee5cd412016-02-05 17:08:45 -08002748 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002749 (speed != DWC3_DSTS_SUPERSPEED) &&
2750 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302751 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2752 reg |= DWC3_DCFG_LPM_CAP;
2753 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2754
2755 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2756 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2757
Huang Rui460d0982014-10-31 11:11:18 +08002758 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302759
Huang Rui80caf7d2014-10-28 19:54:26 +08002760 /*
2761 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2762 * DCFG.LPMCap is set, core responses with an ACK and the
2763 * BESL value in the LPM token is less than or equal to LPM
2764 * NYET threshold.
2765 */
2766 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2767 && dwc->has_lpm_erratum,
Masanari Iida9165dab2016-09-17 23:44:17 +09002768 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
Huang Rui80caf7d2014-10-28 19:54:26 +08002769
2770 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2771 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2772
Pratyush Anand2b758352013-01-14 15:59:31 +05302773 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002774 } else {
2775 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2776 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2777 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302778 }
2779
Felipe Balbi72246da2011-08-19 18:10:58 +03002780 dep = dwc->eps[0];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002781 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 if (ret) {
2783 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2784 return;
2785 }
2786
2787 dep = dwc->eps[1];
Felipe Balbia2d23f02018-04-09 12:40:48 +03002788 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
Felipe Balbi72246da2011-08-19 18:10:58 +03002789 if (ret) {
2790 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2791 return;
2792 }
2793
2794 /*
2795 * Configure PHY via GUSB3PIPECTLn if required.
2796 *
2797 * Update GTXFIFOSIZn
2798 *
2799 * In both cases reset values should be sufficient.
2800 */
2801}
2802
2803static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2804{
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 /*
2806 * TODO take core out of low power mode when that's
2807 * implemented.
2808 */
2809
Jiebing Liad14d4e2014-12-11 13:26:29 +08002810 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2811 spin_unlock(&dwc->lock);
2812 dwc->gadget_driver->resume(&dwc->gadget);
2813 spin_lock(&dwc->lock);
2814 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002815}
2816
2817static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2818 unsigned int evtinfo)
2819{
Felipe Balbifae2b902011-10-14 13:00:30 +03002820 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002821 unsigned int pwropt;
2822
2823 /*
2824 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2825 * Hibernation mode enabled which would show up when device detects
2826 * host-initiated U3 exit.
2827 *
2828 * In that case, device will generate a Link State Change Interrupt
2829 * from U3 to RESUME which is only necessary if Hibernation is
2830 * configured in.
2831 *
2832 * There are no functional changes due to such spurious event and we
2833 * just need to ignore it.
2834 *
2835 * Refers to:
2836 *
2837 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2838 * operational mode
2839 */
2840 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2841 if ((dwc->revision < DWC3_REVISION_250A) &&
2842 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2843 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2844 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002845 return;
2846 }
2847 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002848
2849 /*
2850 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2851 * on the link partner, the USB session might do multiple entry/exit
2852 * of low power states before a transfer takes place.
2853 *
2854 * Due to this problem, we might experience lower throughput. The
2855 * suggested workaround is to disable DCTL[12:9] bits if we're
2856 * transitioning from U1/U2 to U0 and enable those bits again
2857 * after a transfer completes and there are no pending transfers
2858 * on any of the enabled endpoints.
2859 *
2860 * This is the first half of that workaround.
2861 *
2862 * Refers to:
2863 *
2864 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2865 * core send LGO_Ux entering U0
2866 */
2867 if (dwc->revision < DWC3_REVISION_183A) {
2868 if (next == DWC3_LINK_STATE_U0) {
2869 u32 u1u2;
2870 u32 reg;
2871
2872 switch (dwc->link_state) {
2873 case DWC3_LINK_STATE_U1:
2874 case DWC3_LINK_STATE_U2:
2875 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2876 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2877 | DWC3_DCTL_ACCEPTU2ENA
2878 | DWC3_DCTL_INITU1ENA
2879 | DWC3_DCTL_ACCEPTU1ENA);
2880
2881 if (!dwc->u1u2)
2882 dwc->u1u2 = reg & u1u2;
2883
2884 reg &= ~u1u2;
2885
2886 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2887 break;
2888 default:
2889 /* do nothing */
2890 break;
2891 }
2892 }
2893 }
2894
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002895 switch (next) {
2896 case DWC3_LINK_STATE_U1:
2897 if (dwc->speed == USB_SPEED_SUPER)
2898 dwc3_suspend_gadget(dwc);
2899 break;
2900 case DWC3_LINK_STATE_U2:
2901 case DWC3_LINK_STATE_U3:
2902 dwc3_suspend_gadget(dwc);
2903 break;
2904 case DWC3_LINK_STATE_RESUME:
2905 dwc3_resume_gadget(dwc);
2906 break;
2907 default:
2908 /* do nothing */
2909 break;
2910 }
2911
Felipe Balbie57ebc12014-04-22 13:20:12 -05002912 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002913}
2914
Baolin Wang72704f82016-05-16 16:43:53 +08002915static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2916 unsigned int evtinfo)
2917{
2918 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2919
2920 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2921 dwc3_suspend_gadget(dwc);
2922
2923 dwc->link_state = next;
2924}
2925
Felipe Balbie1dadd32014-02-25 14:47:54 -06002926static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2927 unsigned int evtinfo)
2928{
2929 unsigned int is_ss = evtinfo & BIT(4);
2930
Felipe Balbibfad65e2017-04-19 14:59:27 +03002931 /*
Felipe Balbie1dadd32014-02-25 14:47:54 -06002932 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2933 * have a known issue which can cause USB CV TD.9.23 to fail
2934 * randomly.
2935 *
2936 * Because of this issue, core could generate bogus hibernation
2937 * events which SW needs to ignore.
2938 *
2939 * Refers to:
2940 *
2941 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2942 * Device Fallback from SuperSpeed
2943 */
2944 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2945 return;
2946
2947 /* enter hibernation here */
2948}
2949
Felipe Balbi72246da2011-08-19 18:10:58 +03002950static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2951 const struct dwc3_event_devt *event)
2952{
2953 switch (event->type) {
2954 case DWC3_DEVICE_EVENT_DISCONNECT:
2955 dwc3_gadget_disconnect_interrupt(dwc);
2956 break;
2957 case DWC3_DEVICE_EVENT_RESET:
2958 dwc3_gadget_reset_interrupt(dwc);
2959 break;
2960 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2961 dwc3_gadget_conndone_interrupt(dwc);
2962 break;
2963 case DWC3_DEVICE_EVENT_WAKEUP:
2964 dwc3_gadget_wakeup_interrupt(dwc);
2965 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002966 case DWC3_DEVICE_EVENT_HIBER_REQ:
2967 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2968 "unexpected hibernation event\n"))
2969 break;
2970
2971 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2972 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002973 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2974 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2975 break;
2976 case DWC3_DEVICE_EVENT_EOPF:
Baolin Wang72704f82016-05-16 16:43:53 +08002977 /* It changed to be suspend event for version 2.30a and above */
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02002978 if (dwc->revision >= DWC3_REVISION_230A) {
Baolin Wang72704f82016-05-16 16:43:53 +08002979 /*
2980 * Ignore suspend event until the gadget enters into
2981 * USB_STATE_CONFIGURED state.
2982 */
2983 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
2984 dwc3_gadget_suspend_interrupt(dwc,
2985 event->event_info);
2986 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002987 break;
2988 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi72246da2011-08-19 18:10:58 +03002989 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi72246da2011-08-19 18:10:58 +03002990 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi72246da2011-08-19 18:10:58 +03002991 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi72246da2011-08-19 18:10:58 +03002992 break;
2993 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002994 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002995 }
2996}
2997
2998static void dwc3_process_event_entry(struct dwc3 *dwc,
2999 const union dwc3_event *event)
3000{
Felipe Balbi43c96be2016-09-26 13:23:34 +03003001 trace_dwc3_event(event->raw, dwc);
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003002
Felipe Balbidfc5e802017-04-26 13:44:51 +03003003 if (!event->type.is_devspec)
3004 dwc3_endpoint_interrupt(dwc, &event->depevt);
3005 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 dwc3_gadget_interrupt(dwc, &event->devt);
Felipe Balbidfc5e802017-04-26 13:44:51 +03003007 else
Felipe Balbi72246da2011-08-19 18:10:58 +03003008 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
Felipe Balbi72246da2011-08-19 18:10:58 +03003009}
3010
Felipe Balbidea520a2016-03-30 09:39:34 +03003011static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03003012{
Felipe Balbidea520a2016-03-30 09:39:34 +03003013 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03003014 irqreturn_t ret = IRQ_NONE;
3015 int left;
3016 u32 reg;
3017
Felipe Balbif42f2442013-06-12 21:25:08 +03003018 left = evt->count;
3019
3020 if (!(evt->flags & DWC3_EVENT_PENDING))
3021 return IRQ_NONE;
3022
3023 while (left > 0) {
3024 union dwc3_event event;
3025
John Younebbb2d52016-11-15 13:07:02 +02003026 event.raw = *(u32 *) (evt->cache + evt->lpos);
Felipe Balbif42f2442013-06-12 21:25:08 +03003027
3028 dwc3_process_event_entry(dwc, &event);
3029
3030 /*
3031 * FIXME we wrap around correctly to the next entry as
3032 * almost all entries are 4 bytes in size. There is one
3033 * entry which has 12 bytes which is a regular entry
3034 * followed by 8 bytes data. ATM I don't know how
3035 * things are organized if we get next to the a
3036 * boundary so I worry about that once we try to handle
3037 * that.
3038 */
Felipe Balbicaefe6c2016-11-15 13:05:23 +02003039 evt->lpos = (evt->lpos + 4) % evt->length;
Felipe Balbif42f2442013-06-12 21:25:08 +03003040 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003041 }
3042
3043 evt->count = 0;
3044 evt->flags &= ~DWC3_EVENT_PENDING;
3045 ret = IRQ_HANDLED;
3046
3047 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003048 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003049 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003050 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003051
John Youncf40b862016-11-14 12:32:43 -08003052 if (dwc->imod_interval) {
3053 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3054 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3055 }
3056
Felipe Balbif42f2442013-06-12 21:25:08 +03003057 return ret;
3058}
3059
Felipe Balbidea520a2016-03-30 09:39:34 +03003060static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03003061{
Felipe Balbidea520a2016-03-30 09:39:34 +03003062 struct dwc3_event_buffer *evt = _evt;
3063 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003064 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003065 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03003066
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003067 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03003068 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05003069 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003070
3071 return ret;
3072}
3073
Felipe Balbidea520a2016-03-30 09:39:34 +03003074static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003075{
Felipe Balbidea520a2016-03-30 09:39:34 +03003076 struct dwc3 *dwc = evt->dwc;
John Younebbb2d52016-11-15 13:07:02 +02003077 u32 amount;
Felipe Balbi72246da2011-08-19 18:10:58 +03003078 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003079 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003080
Felipe Balbifc8bb912016-05-16 13:14:48 +03003081 if (pm_runtime_suspended(dwc->dev)) {
3082 pm_runtime_get(dwc->dev);
3083 disable_irq_nosync(dwc->irq_gadget);
3084 dwc->pending_events = true;
3085 return IRQ_HANDLED;
3086 }
3087
Thinh Nguyend325a1d2017-05-11 17:26:47 -07003088 /*
3089 * With PCIe legacy interrupt, test shows that top-half irq handler can
3090 * be called again after HW interrupt deassertion. Check if bottom-half
3091 * irq event handler completes before caching new event to prevent
3092 * losing events.
3093 */
3094 if (evt->flags & DWC3_EVENT_PENDING)
3095 return IRQ_HANDLED;
3096
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003097 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003098 count &= DWC3_GEVNTCOUNT_MASK;
3099 if (!count)
3100 return IRQ_NONE;
3101
Felipe Balbib15a7622011-06-30 16:57:15 +03003102 evt->count = count;
3103 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003104
Felipe Balbie8adfc32013-06-12 21:11:14 +03003105 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003106 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003107 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003108 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003109
John Younebbb2d52016-11-15 13:07:02 +02003110 amount = min(count, evt->length - evt->lpos);
3111 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3112
3113 if (amount < count)
3114 memcpy(evt->cache, evt->buf, count - amount);
3115
John Youn65aca322016-11-15 13:08:59 +02003116 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3117
Felipe Balbib15a7622011-06-30 16:57:15 +03003118 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003119}
3120
Felipe Balbidea520a2016-03-30 09:39:34 +03003121static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03003122{
Felipe Balbidea520a2016-03-30 09:39:34 +03003123 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003124
Felipe Balbidea520a2016-03-30 09:39:34 +03003125 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03003126}
3127
Felipe Balbi6db38122016-10-03 11:27:01 +03003128static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3129{
3130 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3131 int irq;
3132
3133 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3134 if (irq > 0)
3135 goto out;
3136
3137 if (irq == -EPROBE_DEFER)
3138 goto out;
3139
3140 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3141 if (irq > 0)
3142 goto out;
3143
3144 if (irq == -EPROBE_DEFER)
3145 goto out;
3146
3147 irq = platform_get_irq(dwc3_pdev, 0);
3148 if (irq > 0)
3149 goto out;
3150
3151 if (irq != -EPROBE_DEFER)
3152 dev_err(dwc->dev, "missing peripheral IRQ\n");
3153
3154 if (!irq)
3155 irq = -EINVAL;
3156
3157out:
3158 return irq;
3159}
3160
Felipe Balbi72246da2011-08-19 18:10:58 +03003161/**
Felipe Balbibfad65e2017-04-19 14:59:27 +03003162 * dwc3_gadget_init - initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003163 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003164 *
3165 * Returns 0 on success otherwise negative errno.
3166 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003167int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003168{
Felipe Balbi6db38122016-10-03 11:27:01 +03003169 int ret;
3170 int irq;
Roger Quadros9522def2016-06-10 14:48:38 +03003171
Felipe Balbi6db38122016-10-03 11:27:01 +03003172 irq = dwc3_gadget_get_irq(dwc);
3173 if (irq < 0) {
3174 ret = irq;
3175 goto err0;
Roger Quadros9522def2016-06-10 14:48:38 +03003176 }
3177
3178 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003179
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303180 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3181 sizeof(*dwc->ep0_trb) * 2,
3182 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003183 if (!dwc->ep0_trb) {
3184 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3185 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003186 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003187 }
3188
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003189 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003190 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003191 ret = -ENOMEM;
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003192 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03003193 }
3194
Felipe Balbi905dc042017-01-05 14:46:52 +02003195 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3196 &dwc->bounce_addr, GFP_KERNEL);
3197 if (!dwc->bounce) {
3198 ret = -ENOMEM;
Felipe Balbid6e5a542017-04-07 16:34:38 +03003199 goto err2;
Felipe Balbi905dc042017-01-05 14:46:52 +02003200 }
3201
Baolin Wangbb014732016-10-14 17:11:33 +08003202 init_completion(&dwc->ep0_in_setup);
3203
Felipe Balbi72246da2011-08-19 18:10:58 +03003204 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003205 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003206 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003207 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003208 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003209
3210 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003211 * FIXME We might be setting max_speed to <SUPER, however versions
3212 * <2.20a of dwc3 have an issue with metastability (documented
3213 * elsewhere in this driver) which tells us we can't set max speed to
3214 * anything lower than SUPER.
3215 *
3216 * Because gadget.max_speed is only used by composite.c and function
3217 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3218 * to happen so we avoid sending SuperSpeed Capability descriptor
3219 * together with our BOS descriptor as that could confuse host into
3220 * thinking we can handle super speed.
3221 *
3222 * Note that, in fact, we won't even support GetBOS requests when speed
3223 * is less than super speed because we don't have means, yet, to tell
3224 * composite.c that we are USB 2.0 + LPM ECN.
3225 */
Roger Quadros42bf02e2017-10-31 15:11:55 +02003226 if (dwc->revision < DWC3_REVISION_220A &&
3227 !dwc->dis_metastability_quirk)
Felipe Balbi5eb30ce2016-11-03 14:07:51 +02003228 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003229 dwc->revision);
3230
3231 dwc->gadget.max_speed = dwc->maximum_speed;
3232
3233 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003234 * REVISIT: Here we should clear all pending IRQs to be
3235 * sure we're starting from a well known location.
3236 */
3237
Bryan O'Donoghuef3bcfc72017-01-31 20:58:11 +00003238 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
Felipe Balbi72246da2011-08-19 18:10:58 +03003239 if (ret)
Felipe Balbid6e5a542017-04-07 16:34:38 +03003240 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03003241
Felipe Balbi72246da2011-08-19 18:10:58 +03003242 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3243 if (ret) {
3244 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbid6e5a542017-04-07 16:34:38 +03003245 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03003246 }
3247
3248 return 0;
Felipe Balbi4199c5f2017-04-07 14:09:13 +03003249
3250err4:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003251 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03003252
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003253err3:
Felipe Balbid6e5a542017-04-07 16:34:38 +03003254 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3255 dwc->bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003256
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003257err2:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003258 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003259
Felipe Balbi7d5e6502017-04-07 13:34:21 +03003260err1:
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303261 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003262 dwc->ep0_trb, dwc->ep0_trb_addr);
3263
Felipe Balbi72246da2011-08-19 18:10:58 +03003264err0:
3265 return ret;
3266}
3267
Felipe Balbi7415f172012-04-30 14:56:33 +03003268/* -------------------------------------------------------------------------- */
3269
Felipe Balbi72246da2011-08-19 18:10:58 +03003270void dwc3_gadget_exit(struct dwc3 *dwc)
3271{
Felipe Balbi72246da2011-08-19 18:10:58 +03003272 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003273 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi905dc042017-01-05 14:46:52 +02003274 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003275 dwc->bounce_addr);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003276 kfree(dwc->setup_buf);
Arnd Bergmannd64ff402016-11-17 17:13:47 +05303277 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbid6e5a542017-04-07 16:34:38 +03003278 dwc->ep0_trb, dwc->ep0_trb_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003279}
Felipe Balbi7415f172012-04-30 14:56:33 +03003280
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003281int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003282{
Roger Quadros9772b472016-04-12 11:33:29 +03003283 if (!dwc->gadget_driver)
3284 return 0;
3285
Roger Quadros1551e352017-02-15 14:16:26 +02003286 dwc3_gadget_run_stop(dwc, false, false);
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003287 dwc3_disconnect_gadget(dwc);
3288 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003289
3290 return 0;
3291}
3292
3293int dwc3_gadget_resume(struct dwc3 *dwc)
3294{
Felipe Balbi7415f172012-04-30 14:56:33 +03003295 int ret;
3296
Roger Quadros9772b472016-04-12 11:33:29 +03003297 if (!dwc->gadget_driver)
3298 return 0;
3299
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003300 ret = __dwc3_gadget_start(dwc);
3301 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003302 goto err0;
3303
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003304 ret = dwc3_gadget_run_stop(dwc, true, false);
3305 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003306 goto err1;
3307
Felipe Balbi7415f172012-04-30 14:56:33 +03003308 return 0;
3309
3310err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003311 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003312
3313err0:
3314 return ret;
3315}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003316
3317void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3318{
3319 if (dwc->pending_events) {
3320 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3321 dwc->pending_events = false;
3322 enable_irq(dwc->irq_gadget);
3323 }
3324}