blob: 867adc9a2496385fd9533b507c978934e1acfcd0 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
Felipe Balbi80977dc2014-08-19 16:37:22 -050033#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030034#include "core.h"
35#include "gadget.h"
36#include "io.h"
37
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020038/**
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
42 *
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
45 * is passed
46 */
47int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48{
49 u32 reg;
50
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
53
54 switch (mode) {
55 case TEST_J:
56 case TEST_K:
57 case TEST_SE0_NAK:
58 case TEST_PACKET:
59 case TEST_FORCE_EN:
60 reg |= mode << 1;
61 break;
62 default:
63 return -EINVAL;
64 }
65
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
67
68 return 0;
69}
70
Felipe Balbi8598bde2012-01-02 18:55:57 +020071/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030072 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
74 *
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
77 */
78int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
83
84 return DWC3_DSTS_USBLNKST(reg);
85}
86
87/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020088 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
91 *
92 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080093 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020094 */
95int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
96{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 u32 reg;
99
Paul Zimmerman802fde92012-04-27 13:10:52 +0300100 /*
101 * Wait until device controller is ready. Only applies to 1.94a and
102 * later RTL.
103 */
104 if (dwc->revision >= DWC3_REVISION_194A) {
105 while (--retries) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
108 udelay(5);
109 else
110 break;
111 }
112
113 if (retries <= 0)
114 return -ETIMEDOUT;
115 }
116
Felipe Balbi8598bde2012-01-02 18:55:57 +0200117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
119
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
123
Paul Zimmerman802fde92012-04-27 13:10:52 +0300124 /*
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
127 */
128 if (dwc->revision >= DWC3_REVISION_194A)
129 return 0;
130
Felipe Balbi8598bde2012-01-02 18:55:57 +0200131 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300132 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200133 while (--retries) {
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 if (DWC3_DSTS_USBLNKST(reg) == state)
137 return 0;
138
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800139 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 }
141
Felipe Balbi73815282015-01-27 13:48:14 -0600142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144
145 return -ETIMEDOUT;
146}
147
Felipe Balbief966b92016-04-05 13:09:51 +0300148static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200149{
Felipe Balbief966b92016-04-05 13:09:51 +0300150 dep->trb_enqueue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300151 dep->trb_enqueue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300152}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200153
Felipe Balbief966b92016-04-05 13:09:51 +0300154static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
155{
156 dep->trb_dequeue++;
Felipe Balbi4faf7552016-04-05 13:14:31 +0300157 dep->trb_dequeue %= DWC3_TRB_NUM;
Felipe Balbief966b92016-04-05 13:09:51 +0300158}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159
Felipe Balbief966b92016-04-05 13:09:51 +0300160static int dwc3_ep_is_last_trb(unsigned int index)
161{
Felipe Balbi4faf7552016-04-05 13:14:31 +0300162 return index == DWC3_TRB_NUM - 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200163}
164
Felipe Balbi72246da2011-08-19 18:10:58 +0300165void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
166 int status)
167{
168 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530169 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300170
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200171 if (req->started) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530172 i = 0;
173 do {
Felipe Balbief966b92016-04-05 13:09:51 +0300174 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530175 /*
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
179 */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300180 if (dwc3_ep_is_last_trb(dep->trb_dequeue))
Felipe Balbief966b92016-04-05 13:09:51 +0300181 dwc3_ep_inc_deq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530182 } while(++i < req->request.num_mapped_sgs);
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200183 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300184 }
185 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200186 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300187
188 if (req->request.status == -EINPROGRESS)
189 req->request.status = status;
190
Pratyush Anand0416e492012-08-10 13:42:16 +0530191 if (dwc->ep0_bounced && dep->number == 0)
192 dwc->ep0_bounced = false;
193 else
194 usb_gadget_unmap_request(&dwc->gadget, &req->request,
195 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500197 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300198
199 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200200 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300201 spin_lock(&dwc->lock);
202}
203
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500204int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300205{
206 u32 timeout = 500;
207 u32 reg;
208
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500209 trace_dwc3_gadget_generic_cmd(cmd, param);
Felipe Balbi427c3df2014-04-25 14:14:14 -0500210
Felipe Balbib09bb642012-04-24 16:19:11 +0300211 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
212 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
213
214 do {
215 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
216 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600217 dwc3_trace(trace_dwc3_gadget,
218 "Command Complete --> %d",
Felipe Balbib09bb642012-04-24 16:19:11 +0300219 DWC3_DGCMD_STATUS(reg));
Subbaraya Sundeep Bhatta891b1dc2015-05-21 15:46:47 +0530220 if (DWC3_DGCMD_STATUS(reg))
221 return -EINVAL;
Felipe Balbib09bb642012-04-24 16:19:11 +0300222 return 0;
223 }
224
225 /*
226 * We can't sleep here, because it's also called from
227 * interrupt context.
228 */
229 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600230 if (!timeout) {
231 dwc3_trace(trace_dwc3_gadget,
232 "Command Timed Out");
Felipe Balbib09bb642012-04-24 16:19:11 +0300233 return -ETIMEDOUT;
Felipe Balbi73815282015-01-27 13:48:14 -0600234 }
Felipe Balbib09bb642012-04-24 16:19:11 +0300235 udelay(1);
236 } while (1);
237}
238
Felipe Balbic36d8e92016-04-04 12:46:33 +0300239static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
240
Felipe Balbi2cd47182016-04-12 16:42:43 +0300241int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
242 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300243{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300244 struct dwc3 *dwc = dep->dwc;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200245 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300246 u32 reg;
247
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300249 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300250
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500251 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300252
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300253 /*
254 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
255 * we're issuing an endpoint command, we must check if
256 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
257 *
258 * We will also set SUSPHY bit to what it was before returning as stated
259 * by the same section on Synopsys databook.
260 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300261 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
262 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
263 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
264 susphy = true;
265 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
266 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
267 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300268 }
269
Felipe Balbic36d8e92016-04-04 12:46:33 +0300270 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
271 int needs_wakeup;
272
273 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
274 dwc->link_state == DWC3_LINK_STATE_U2 ||
275 dwc->link_state == DWC3_LINK_STATE_U3);
276
277 if (unlikely(needs_wakeup)) {
278 ret = __dwc3_gadget_wakeup(dwc);
279 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
280 ret);
281 }
282 }
283
Felipe Balbi2eb88012016-04-12 16:53:39 +0300284 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
285 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
286 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
Felipe Balbi2eb88012016-04-12 16:53:39 +0300288 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300290 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300291 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000292 int cmd_status = DWC3_DEPCMD_STATUS(reg);
293
Felipe Balbi73815282015-01-27 13:48:14 -0600294 dwc3_trace(trace_dwc3_gadget,
295 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000296 cmd_status);
297
298 switch (cmd_status) {
299 case 0:
300 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300301 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000302 case DEPEVT_TRANSFER_NO_RESOURCE:
303 dwc3_trace(trace_dwc3_gadget, "%s: no resource available");
304 ret = -EINVAL;
305 break;
306 case DEPEVT_TRANSFER_BUS_EXPIRY:
307 /*
308 * SW issues START TRANSFER command to
309 * isochronous ep with future frame interval. If
310 * future interval time has already passed when
311 * core receives the command, it will respond
312 * with an error status of 'Bus Expiry'.
313 *
314 * Instead of always returning -EINVAL, let's
315 * give a hint to the gadget driver that this is
316 * the case by returning -EAGAIN.
317 */
318 dwc3_trace(trace_dwc3_gadget, "%s: bus expiry");
319 ret = -EAGAIN;
320 break;
321 default:
322 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
323 }
324
Felipe Balbic0ca3242016-04-04 09:11:51 +0300325 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300326 }
327
328 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300329 * We can't sleep here, because it is also called from
330 * interrupt context.
331 */
332 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600333 if (!timeout) {
334 dwc3_trace(trace_dwc3_gadget,
335 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300336 ret = -ETIMEDOUT;
337 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600338 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300339 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300340
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300341 if (unlikely(susphy)) {
342 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
343 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
344 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
345 }
346
Felipe Balbic0ca3242016-04-04 09:11:51 +0300347 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300348}
349
John Youn50c763f2016-05-31 17:49:56 -0700350static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
351{
352 struct dwc3 *dwc = dep->dwc;
353 struct dwc3_gadget_ep_cmd_params params;
354 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
355
356 /*
357 * As of core revision 2.60a the recommended programming model
358 * is to set the ClearPendIN bit when issuing a Clear Stall EP
359 * command for IN endpoints. This is to prevent an issue where
360 * some (non-compliant) hosts may not send ACK TPs for pending
361 * IN transfers due to a mishandled error condition. Synopsys
362 * STAR 9000614252.
363 */
364 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
365 cmd |= DWC3_DEPCMD_CLEARPENDIN;
366
367 memset(&params, 0, sizeof(params));
368
Felipe Balbi2cd47182016-04-12 16:42:43 +0300369 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700370}
371
Felipe Balbi72246da2011-08-19 18:10:58 +0300372static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200373 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300374{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300375 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
377 return dep->trb_pool_dma + offset;
378}
379
380static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
381{
382 struct dwc3 *dwc = dep->dwc;
383
384 if (dep->trb_pool)
385 return 0;
386
Felipe Balbi72246da2011-08-19 18:10:58 +0300387 dep->trb_pool = dma_alloc_coherent(dwc->dev,
388 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
389 &dep->trb_pool_dma, GFP_KERNEL);
390 if (!dep->trb_pool) {
391 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
392 dep->name);
393 return -ENOMEM;
394 }
395
396 return 0;
397}
398
399static void dwc3_free_trb_pool(struct dwc3_ep *dep)
400{
401 struct dwc3 *dwc = dep->dwc;
402
403 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
404 dep->trb_pool, dep->trb_pool_dma);
405
406 dep->trb_pool = NULL;
407 dep->trb_pool_dma = 0;
408}
409
John Younc4509602016-02-16 20:10:53 -0800410static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
411
412/**
413 * dwc3_gadget_start_config - Configure EP resources
414 * @dwc: pointer to our controller context structure
415 * @dep: endpoint that is being enabled
416 *
417 * The assignment of transfer resources cannot perfectly follow the
418 * data book due to the fact that the controller driver does not have
419 * all knowledge of the configuration in advance. It is given this
420 * information piecemeal by the composite gadget framework after every
421 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
422 * programming model in this scenario can cause errors. For two
423 * reasons:
424 *
425 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
426 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
427 * multiple interfaces.
428 *
429 * 2) The databook does not mention doing more DEPXFERCFG for new
430 * endpoint on alt setting (8.1.6).
431 *
432 * The following simplified method is used instead:
433 *
434 * All hardware endpoints can be assigned a transfer resource and this
435 * setting will stay persistent until either a core reset or
436 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
437 * do DEPXFERCFG for every hardware endpoint as well. We are
438 * guaranteed that there are as many transfer resources as endpoints.
439 *
440 * This function is called for each endpoint when it is being enabled
441 * but is triggered only when called for EP0-out, which always happens
442 * first, and which should only happen in one of the above conditions.
443 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300444static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
445{
446 struct dwc3_gadget_ep_cmd_params params;
447 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800448 int i;
449 int ret;
450
451 if (dep->number)
452 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300453
454 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800455 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
Felipe Balbi2cd47182016-04-12 16:42:43 +0300457 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800458 if (ret)
459 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300460
John Younc4509602016-02-16 20:10:53 -0800461 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
462 struct dwc3_ep *dep = dwc->eps[i];
463
464 if (!dep)
465 continue;
466
467 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
468 if (ret)
469 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470 }
471
472 return 0;
473}
474
475static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200476 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300477 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600478 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300479{
480 struct dwc3_gadget_ep_cmd_params params;
481
482 memset(&params, 0x00, sizeof(params));
483
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300484 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900485 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
486
487 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800488 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300489 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300490 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900491 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300492
Felipe Balbi4b345c92012-07-16 14:08:16 +0300493 if (ignore)
494 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
495
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600496 if (restore) {
497 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
498 params.param2 |= dep->saved_state;
499 }
500
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300501 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
502 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300503
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200504 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300505 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
506 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300507 dep->stream_capable = true;
508 }
509
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500510 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300511 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300512
513 /*
514 * We are doing 1:1 mapping for endpoints, meaning
515 * Physical Endpoints 2 maps to Logical Endpoint 2 and
516 * so on. We consider the direction bit as part of the physical
517 * endpoint number. So USB endpoint 0x81 is 0x03.
518 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300519 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
521 /*
522 * We must use the lower 16 TX FIFOs even though
523 * HW might have more
524 */
525 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300526 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300527
528 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300529 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300530 dep->interval = 1 << (desc->bInterval - 1);
531 }
532
Felipe Balbi2cd47182016-04-12 16:42:43 +0300533 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300534}
535
536static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
537{
538 struct dwc3_gadget_ep_cmd_params params;
539
540 memset(&params, 0x00, sizeof(params));
541
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300542 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
Felipe Balbi2cd47182016-04-12 16:42:43 +0300544 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
545 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300546}
547
548/**
549 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
550 * @dep: endpoint to be initialized
551 * @desc: USB Endpoint Descriptor
552 *
553 * Caller should take care of locking
554 */
555static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200556 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300557 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600558 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300559{
560 struct dwc3 *dwc = dep->dwc;
561 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300562 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
Felipe Balbi73815282015-01-27 13:48:14 -0600564 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300565
Felipe Balbi72246da2011-08-19 18:10:58 +0300566 if (!(dep->flags & DWC3_EP_ENABLED)) {
567 ret = dwc3_gadget_start_config(dwc, dep);
568 if (ret)
569 return ret;
570 }
571
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600572 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
573 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300574 if (ret)
575 return ret;
576
577 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200578 struct dwc3_trb *trb_st_hw;
579 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200581 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200582 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 dep->type = usb_endpoint_type(desc);
584 dep->flags |= DWC3_EP_ENABLED;
585
586 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
587 reg |= DWC3_DALEPENA_EP(dep->number);
588 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
589
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300590 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200591 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300593 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 trb_st_hw = &dep->trb_pool[0];
595
Felipe Balbif6bafc62012-02-06 11:04:53 +0200596 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700597 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
Felipe Balbif6bafc62012-02-06 11:04:53 +0200599 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
600 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
601 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
602 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300603 }
604
Felipe Balbie901aa12016-03-16 14:01:37 +0200605out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500606 switch (usb_endpoint_type(desc)) {
607 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200608 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500609 break;
610 case USB_ENDPOINT_XFER_ISOC:
611 strlcat(dep->name, "-isoc", sizeof(dep->name));
612 break;
613 case USB_ENDPOINT_XFER_BULK:
614 strlcat(dep->name, "-bulk", sizeof(dep->name));
615 break;
616 case USB_ENDPOINT_XFER_INT:
617 strlcat(dep->name, "-int", sizeof(dep->name));
618 break;
619 default:
620 dev_err(dwc->dev, "invalid endpoint transfer type\n");
621 }
622
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 return 0;
624}
625
Paul Zimmermanb992e682012-04-27 14:17:35 +0300626static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200627static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300628{
629 struct dwc3_request *req;
630
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200631 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300632 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200633
Pratyush Anand57911502012-07-06 15:19:10 +0530634 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200635 while (!list_empty(&dep->started_list)) {
636 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530637
638 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
639 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200640 }
641
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200642 while (!list_empty(&dep->pending_list)) {
643 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300644
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200645 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300646 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300647}
648
649/**
650 * __dwc3_gadget_ep_disable - Disables a HW endpoint
651 * @dep: the endpoint to disable
652 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200653 * This function also removes requests which are currently processed ny the
654 * hardware and those which are not yet scheduled.
655 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300657static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
658{
659 struct dwc3 *dwc = dep->dwc;
660 u32 reg;
661
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500662 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
663
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200664 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300665
Felipe Balbi687ef982014-04-16 10:30:33 -0500666 /* make sure HW endpoint isn't stalled */
667 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500668 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500669
Felipe Balbi72246da2011-08-19 18:10:58 +0300670 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
671 reg &= ~DWC3_DALEPENA_EP(dep->number);
672 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
673
Felipe Balbi879631a2011-09-30 10:58:47 +0300674 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200675 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200676 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300677 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300678 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300679
Felipe Balbiaa739972015-07-20 14:48:13 -0500680 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
681 dep->number >> 1,
682 (dep->number & 1) ? "in" : "out");
683
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 return 0;
685}
686
687/* -------------------------------------------------------------------------- */
688
689static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
690 const struct usb_endpoint_descriptor *desc)
691{
692 return -EINVAL;
693}
694
695static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
696{
697 return -EINVAL;
698}
699
700/* -------------------------------------------------------------------------- */
701
702static int dwc3_gadget_ep_enable(struct usb_ep *ep,
703 const struct usb_endpoint_descriptor *desc)
704{
705 struct dwc3_ep *dep;
706 struct dwc3 *dwc;
707 unsigned long flags;
708 int ret;
709
710 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
711 pr_debug("dwc3: invalid parameters\n");
712 return -EINVAL;
713 }
714
715 if (!desc->wMaxPacketSize) {
716 pr_debug("dwc3: missing wMaxPacketSize\n");
717 return -EINVAL;
718 }
719
720 dep = to_dwc3_ep(ep);
721 dwc = dep->dwc;
722
Felipe Balbi95ca9612015-12-10 13:08:20 -0600723 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
724 "%s is already enabled\n",
725 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300726 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300727
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600729 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 spin_unlock_irqrestore(&dwc->lock, flags);
731
732 return ret;
733}
734
735static int dwc3_gadget_ep_disable(struct usb_ep *ep)
736{
737 struct dwc3_ep *dep;
738 struct dwc3 *dwc;
739 unsigned long flags;
740 int ret;
741
742 if (!ep) {
743 pr_debug("dwc3: invalid parameters\n");
744 return -EINVAL;
745 }
746
747 dep = to_dwc3_ep(ep);
748 dwc = dep->dwc;
749
Felipe Balbi95ca9612015-12-10 13:08:20 -0600750 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
751 "%s is already disabled\n",
752 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 spin_lock_irqsave(&dwc->lock, flags);
756 ret = __dwc3_gadget_ep_disable(dep);
757 spin_unlock_irqrestore(&dwc->lock, flags);
758
759 return ret;
760}
761
762static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
763 gfp_t gfp_flags)
764{
765 struct dwc3_request *req;
766 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300767
768 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900769 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300770 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300771
772 req->epnum = dep->number;
773 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300774
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500775 trace_dwc3_alloc_request(req);
776
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 return &req->request;
778}
779
780static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
781 struct usb_request *request)
782{
783 struct dwc3_request *req = to_dwc3_request(request);
784
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500785 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300786 kfree(req);
787}
788
Felipe Balbic71fc372011-11-22 11:37:34 +0200789/**
790 * dwc3_prepare_one_trb - setup one TRB from one request
791 * @dep: endpoint for which this request is prepared
792 * @req: dwc3_request pointer
793 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200794static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200795 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530796 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200797{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200798 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200799
Felipe Balbi73815282015-01-27 13:48:14 -0600800 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200801 dep->name, req, (unsigned long long) dma,
802 length, last ? " last" : "",
803 chain ? " chain" : "");
804
Pratyush Anand915e2022013-01-14 15:59:35 +0530805
Felipe Balbi4faf7552016-04-05 13:14:31 +0300806 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200807
Felipe Balbieeb720f2011-11-28 12:46:59 +0200808 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200809 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200810 req->trb = trb;
811 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300812 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200813 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200814
Felipe Balbief966b92016-04-05 13:09:51 +0300815 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300816 /* Skip the LINK-TRB */
817 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300818 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530819
Felipe Balbif6bafc62012-02-06 11:04:53 +0200820 trb->size = DWC3_TRB_SIZE_LENGTH(length);
821 trb->bpl = lower_32_bits(dma);
822 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200823
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200824 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200825 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200826 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200827 break;
828
829 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530830 if (!node)
831 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
832 else
833 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200834
835 /* always enable Interrupt on Missed ISOC */
836 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200837 break;
838
839 case USB_ENDPOINT_XFER_BULK:
840 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200841 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200842 break;
843 default:
844 /*
845 * This is only possible with faulty memory because we
846 * checked it already :)
847 */
848 BUG();
849 }
850
Felipe Balbica4d44e2016-03-10 13:53:27 +0200851 /* always enable Continue on Short Packet */
852 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600853
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300854 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200855 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
856
857 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530858 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200859
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530860 if (chain)
861 trb->ctrl |= DWC3_TRB_CTRL_CHN;
862
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200863 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200864 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
865
866 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500867
868 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200869}
870
Felipe Balbic4233572016-05-12 14:08:34 +0300871static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
872{
873 struct dwc3_trb *tmp;
874
875 /*
876 * If enqueue & dequeue are equal than it is either full or empty.
877 *
878 * One way to know for sure is if the TRB right before us has HWO bit
879 * set or not. If it has, then we're definitely full and can't fit any
880 * more transfers in our ring.
881 */
882 if (dep->trb_enqueue == dep->trb_dequeue) {
883 /* If we're full, enqueue/dequeue are > 0 */
884 if (dep->trb_enqueue) {
885 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
886 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
887 return 0;
888 }
889
890 return DWC3_TRB_NUM - 1;
891 }
892
893 return dep->trb_dequeue - dep->trb_enqueue;
894}
895
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300896static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
897 struct dwc3_request *req, unsigned int trbs_left)
898{
899 struct usb_request *request = &req->request;
900 struct scatterlist *sg = request->sg;
901 struct scatterlist *s;
902 unsigned int last = false;
903 unsigned int length;
904 dma_addr_t dma;
905 int i;
906
907 for_each_sg(sg, s, request->num_mapped_sgs, i) {
908 unsigned chain = true;
909
910 length = sg_dma_len(s);
911 dma = sg_dma_address(s);
912
913 if (sg_is_last(s)) {
914 if (list_is_last(&req->list, &dep->pending_list))
915 last = true;
916
917 chain = false;
918 }
919
920 if (!trbs_left)
921 last = true;
922
923 if (last)
924 chain = false;
925
926 dwc3_prepare_one_trb(dep, req, dma, length,
927 last, chain, i);
928
929 if (last)
930 break;
931 }
932}
933
934static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
935 struct dwc3_request *req, unsigned int trbs_left)
936{
937 unsigned int last = false;
938 unsigned int length;
939 dma_addr_t dma;
940
941 dma = req->request.dma;
942 length = req->request.length;
943
944 if (!trbs_left)
945 last = true;
946
947 /* Is this the last request? */
948 if (list_is_last(&req->list, &dep->pending_list))
949 last = true;
950
951 dwc3_prepare_one_trb(dep, req, dma, length,
952 last, false, 0);
953}
954
Felipe Balbi72246da2011-08-19 18:10:58 +0300955/*
956 * dwc3_prepare_trbs - setup TRBs from requests
957 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800959 * The function goes through the requests list and sets up TRBs for the
960 * transfers. The function returns once there are no more TRBs available or
961 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300962 */
Felipe Balbic4233572016-05-12 14:08:34 +0300963static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300964{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200965 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 u32 trbs_left;
967
968 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
969
Felipe Balbic4233572016-05-12 14:08:34 +0300970 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300971
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200972 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300973 if (req->request.num_mapped_sgs > 0)
974 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
975 else
976 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300977
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300978 if (!trbs_left)
979 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300981}
982
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300983static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300984{
985 struct dwc3_gadget_ep_cmd_params params;
986 struct dwc3_request *req;
987 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300988 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 int ret;
990 u32 cmd;
991
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300992 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300993
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300994 dwc3_prepare_trbs(dep);
995 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 if (!req) {
997 dep->flags |= DWC3_EP_PENDING_REQUEST;
998 return 0;
999 }
1000
1001 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001002
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001003 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301004 params.param0 = upper_32_bits(req->trb_dma);
1005 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301007 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001008 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301009 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001010
1011 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +03001012 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001013 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001014 /*
1015 * FIXME we need to iterate over the list of requests
1016 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001017 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001018 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001019 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1020 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001021 list_del(&req->list);
1022 return ret;
1023 }
1024
1025 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001026
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001027 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001028 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001029 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001030 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001031
Felipe Balbi72246da2011-08-19 18:10:58 +03001032 return 0;
1033}
1034
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301035static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1036 struct dwc3_ep *dep, u32 cur_uf)
1037{
1038 u32 uf;
1039
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001040 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001041 dwc3_trace(trace_dwc3_gadget,
1042 "ISOC ep %s run out for requests",
1043 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301044 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301045 return;
1046 }
1047
1048 /* 4 micro frames in the future */
1049 uf = cur_uf + dep->interval * 4;
1050
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001051 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301052}
1053
1054static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1055 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1056{
1057 u32 cur_uf, mask;
1058
1059 mask = ~(dep->interval - 1);
1060 cur_uf = event->parameters & mask;
1061
1062 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1063}
1064
Felipe Balbi72246da2011-08-19 18:10:58 +03001065static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1066{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001067 struct dwc3 *dwc = dep->dwc;
1068 int ret;
1069
Felipe Balbibb423982015-11-16 15:31:21 -06001070 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001071 dwc3_trace(trace_dwc3_gadget,
1072 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001073 &req->request, dep->endpoint.name);
1074 return -ESHUTDOWN;
1075 }
1076
1077 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1078 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001079 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1080 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001081 return -EINVAL;
1082 }
1083
Felipe Balbi72246da2011-08-19 18:10:58 +03001084 req->request.actual = 0;
1085 req->request.status = -EINPROGRESS;
1086 req->direction = dep->direction;
1087 req->epnum = dep->number;
1088
Felipe Balbife84f522015-09-01 09:01:38 -05001089 trace_dwc3_ep_queue(req);
1090
Felipe Balbi72246da2011-08-19 18:10:58 +03001091 /*
1092 * We only add to our list of requests now and
1093 * start consuming the list once we get XferNotReady
1094 * IRQ.
1095 *
1096 * That way, we avoid doing anything that we don't need
1097 * to do now and defer it until the point we receive a
1098 * particular token from the Host side.
1099 *
1100 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001101 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001102 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001103 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1104 dep->direction);
1105 if (ret)
1106 return ret;
1107
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001108 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001109
1110 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001111 * If there are no pending requests and the endpoint isn't already
1112 * busy, we will just start the request straight away.
1113 *
1114 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1115 * little bit faster.
1116 */
1117 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001118 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001119 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001120 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001121 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001122 }
1123
1124 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001125 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001126 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001127 * 1. XferNotReady with empty list of requests. We need to kick the
1128 * transfer here in that situation, otherwise we will be NAKing
1129 * forever. If we get XferNotReady before gadget driver has a
1130 * chance to queue a request, we will ACK the IRQ but won't be
1131 * able to receive the data until the next request is queued.
1132 * The following code is handling exactly that.
1133 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001134 */
1135 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301136 /*
1137 * If xfernotready is already elapsed and it is a case
1138 * of isoc transfer, then issue END TRANSFER, so that
1139 * you can receive xfernotready again and can have
1140 * notion of current microframe.
1141 */
1142 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001143 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001144 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301145 dep->flags = DWC3_EP_ENABLED;
1146 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301147 return 0;
1148 }
1149
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001150 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001151 if (!ret)
1152 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1153
Felipe Balbia8f32812015-09-16 10:40:07 -05001154 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001155 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001156
Felipe Balbib511e5e2012-06-06 12:00:50 +03001157 /*
1158 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1159 * kick the transfer here after queuing a request, otherwise the
1160 * core may not see the modified TRB(s).
1161 */
1162 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301163 (dep->flags & DWC3_EP_BUSY) &&
1164 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001165 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001166 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001167 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001168 }
1169
Felipe Balbib997ada2012-07-26 13:26:50 +03001170 /*
1171 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1172 * right away, otherwise host will not know we have streams to be
1173 * handled.
1174 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001175 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001176 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001177
Felipe Balbia8f32812015-09-16 10:40:07 -05001178out:
1179 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001180 dwc3_trace(trace_dwc3_gadget,
1181 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001182 dep->name);
1183 if (ret == -EBUSY)
1184 ret = 0;
1185
1186 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001187}
1188
Felipe Balbi04c03d12015-12-02 10:06:45 -06001189static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1190 struct usb_request *request)
1191{
1192 dwc3_gadget_ep_free_request(ep, request);
1193}
1194
1195static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1196{
1197 struct dwc3_request *req;
1198 struct usb_request *request;
1199 struct usb_ep *ep = &dep->endpoint;
1200
1201 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1202 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1203 if (!request)
1204 return -ENOMEM;
1205
1206 request->length = 0;
1207 request->buf = dwc->zlp_buf;
1208 request->complete = __dwc3_gadget_ep_zlp_complete;
1209
1210 req = to_dwc3_request(request);
1211
1212 return __dwc3_gadget_ep_queue(dep, req);
1213}
1214
Felipe Balbi72246da2011-08-19 18:10:58 +03001215static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1216 gfp_t gfp_flags)
1217{
1218 struct dwc3_request *req = to_dwc3_request(request);
1219 struct dwc3_ep *dep = to_dwc3_ep(ep);
1220 struct dwc3 *dwc = dep->dwc;
1221
1222 unsigned long flags;
1223
1224 int ret;
1225
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001226 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001227 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001228
1229 /*
1230 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1231 * setting request->zero, instead of doing magic, we will just queue an
1232 * extra usb_request ourselves so that it gets handled the same way as
1233 * any other request.
1234 */
John Yound92618982015-12-22 12:23:20 -08001235 if (ret == 0 && request->zero && request->length &&
1236 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001237 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1238
Felipe Balbi72246da2011-08-19 18:10:58 +03001239 spin_unlock_irqrestore(&dwc->lock, flags);
1240
1241 return ret;
1242}
1243
1244static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1245 struct usb_request *request)
1246{
1247 struct dwc3_request *req = to_dwc3_request(request);
1248 struct dwc3_request *r = NULL;
1249
1250 struct dwc3_ep *dep = to_dwc3_ep(ep);
1251 struct dwc3 *dwc = dep->dwc;
1252
1253 unsigned long flags;
1254 int ret = 0;
1255
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001256 trace_dwc3_ep_dequeue(req);
1257
Felipe Balbi72246da2011-08-19 18:10:58 +03001258 spin_lock_irqsave(&dwc->lock, flags);
1259
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001260 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001261 if (r == req)
1262 break;
1263 }
1264
1265 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001266 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001267 if (r == req)
1268 break;
1269 }
1270 if (r == req) {
1271 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001272 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301273 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001274 }
1275 dev_err(dwc->dev, "request %p was not queued to %s\n",
1276 request, ep->name);
1277 ret = -EINVAL;
1278 goto out0;
1279 }
1280
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301281out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001282 /* giveback the request */
1283 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1284
1285out0:
1286 spin_unlock_irqrestore(&dwc->lock, flags);
1287
1288 return ret;
1289}
1290
Felipe Balbi7a608552014-09-24 14:19:52 -05001291int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001292{
1293 struct dwc3_gadget_ep_cmd_params params;
1294 struct dwc3 *dwc = dep->dwc;
1295 int ret;
1296
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001297 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1298 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1299 return -EINVAL;
1300 }
1301
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 memset(&params, 0x00, sizeof(params));
1303
1304 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001305 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001306 (!list_empty(&dep->started_list) ||
1307 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001308 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001309 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001310 dep->name);
1311 return -EAGAIN;
1312 }
1313
Felipe Balbi2cd47182016-04-12 16:42:43 +03001314 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1315 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001317 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001318 dep->name);
1319 else
1320 dep->flags |= DWC3_EP_STALL;
1321 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001322
John Youn50c763f2016-05-31 17:49:56 -07001323 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001324 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001325 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 dep->name);
1327 else
Alan Sterna535d812013-11-01 12:05:12 -04001328 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001330
Felipe Balbi72246da2011-08-19 18:10:58 +03001331 return ret;
1332}
1333
1334static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1335{
1336 struct dwc3_ep *dep = to_dwc3_ep(ep);
1337 struct dwc3 *dwc = dep->dwc;
1338
1339 unsigned long flags;
1340
1341 int ret;
1342
1343 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001344 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001345 spin_unlock_irqrestore(&dwc->lock, flags);
1346
1347 return ret;
1348}
1349
1350static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1351{
1352 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001353 struct dwc3 *dwc = dep->dwc;
1354 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001355 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001356
Paul Zimmerman249a4562012-02-24 17:32:16 -08001357 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001358 dep->flags |= DWC3_EP_WEDGE;
1359
Pratyush Anand08f0d962012-06-25 22:40:43 +05301360 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001361 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301362 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001363 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001364 spin_unlock_irqrestore(&dwc->lock, flags);
1365
1366 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001367}
1368
1369/* -------------------------------------------------------------------------- */
1370
1371static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1372 .bLength = USB_DT_ENDPOINT_SIZE,
1373 .bDescriptorType = USB_DT_ENDPOINT,
1374 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1375};
1376
1377static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1378 .enable = dwc3_gadget_ep0_enable,
1379 .disable = dwc3_gadget_ep0_disable,
1380 .alloc_request = dwc3_gadget_ep_alloc_request,
1381 .free_request = dwc3_gadget_ep_free_request,
1382 .queue = dwc3_gadget_ep0_queue,
1383 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301384 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001385 .set_wedge = dwc3_gadget_ep_set_wedge,
1386};
1387
1388static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1389 .enable = dwc3_gadget_ep_enable,
1390 .disable = dwc3_gadget_ep_disable,
1391 .alloc_request = dwc3_gadget_ep_alloc_request,
1392 .free_request = dwc3_gadget_ep_free_request,
1393 .queue = dwc3_gadget_ep_queue,
1394 .dequeue = dwc3_gadget_ep_dequeue,
1395 .set_halt = dwc3_gadget_ep_set_halt,
1396 .set_wedge = dwc3_gadget_ep_set_wedge,
1397};
1398
1399/* -------------------------------------------------------------------------- */
1400
1401static int dwc3_gadget_get_frame(struct usb_gadget *g)
1402{
1403 struct dwc3 *dwc = gadget_to_dwc(g);
1404 u32 reg;
1405
1406 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1407 return DWC3_DSTS_SOFFN(reg);
1408}
1409
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001410static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001411{
Felipe Balbi72246da2011-08-19 18:10:58 +03001412 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001413
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001414 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001415 u32 reg;
1416
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 u8 link_state;
1418 u8 speed;
1419
Felipe Balbi72246da2011-08-19 18:10:58 +03001420 /*
1421 * According to the Databook Remote wakeup request should
1422 * be issued only when the device is in early suspend state.
1423 *
1424 * We can check that via USB Link State bits in DSTS register.
1425 */
1426 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1427
1428 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001429 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1430 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001431 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001432 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 }
1434
1435 link_state = DWC3_DSTS_USBLNKST(reg);
1436
1437 switch (link_state) {
1438 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1439 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1440 break;
1441 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001442 dwc3_trace(trace_dwc3_gadget,
1443 "can't wakeup from '%s'\n",
1444 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001445 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 }
1447
Felipe Balbi8598bde2012-01-02 18:55:57 +02001448 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1449 if (ret < 0) {
1450 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001451 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001452 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001453
Paul Zimmerman802fde92012-04-27 13:10:52 +03001454 /* Recent versions do this automatically */
1455 if (dwc->revision < DWC3_REVISION_194A) {
1456 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001457 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001458 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1459 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1460 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001461
Paul Zimmerman1d046792012-02-15 18:56:56 -08001462 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001463 timeout = jiffies + msecs_to_jiffies(100);
1464
Paul Zimmerman1d046792012-02-15 18:56:56 -08001465 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1467
1468 /* in HS, means ON */
1469 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1470 break;
1471 }
1472
1473 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1474 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001475 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 }
1477
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001478 return 0;
1479}
1480
1481static int dwc3_gadget_wakeup(struct usb_gadget *g)
1482{
1483 struct dwc3 *dwc = gadget_to_dwc(g);
1484 unsigned long flags;
1485 int ret;
1486
1487 spin_lock_irqsave(&dwc->lock, flags);
1488 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001489 spin_unlock_irqrestore(&dwc->lock, flags);
1490
1491 return ret;
1492}
1493
1494static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1495 int is_selfpowered)
1496{
1497 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001498 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001499
Paul Zimmerman249a4562012-02-24 17:32:16 -08001500 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001501 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001502 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001503
1504 return 0;
1505}
1506
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001507static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001508{
1509 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001510 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001511
1512 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001513 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001514 if (dwc->revision <= DWC3_REVISION_187A) {
1515 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1516 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1517 }
1518
1519 if (dwc->revision >= DWC3_REVISION_194A)
1520 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1521 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001522
1523 if (dwc->has_hibernation)
1524 reg |= DWC3_DCTL_KEEP_CONNECT;
1525
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001526 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001527 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001528 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001529
1530 if (dwc->has_hibernation && !suspend)
1531 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1532
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001533 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001534 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001535
1536 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1537
1538 do {
1539 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1540 if (is_on) {
1541 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1542 break;
1543 } else {
1544 if (reg & DWC3_DSTS_DEVCTRLHLT)
1545 break;
1546 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001547 timeout--;
1548 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301549 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001550 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001551 } while (1);
1552
Felipe Balbi73815282015-01-27 13:48:14 -06001553 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001554 dwc->gadget_driver
1555 ? dwc->gadget_driver->function : "no-function",
1556 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301557
1558 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001559}
1560
1561static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1562{
1563 struct dwc3 *dwc = gadget_to_dwc(g);
1564 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301565 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001566
1567 is_on = !!is_on;
1568
1569 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001570 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001571 spin_unlock_irqrestore(&dwc->lock, flags);
1572
Pratyush Anand6f17f742012-07-02 10:21:55 +05301573 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001574}
1575
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001576static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1577{
1578 u32 reg;
1579
1580 /* Enable all but Start and End of Frame IRQs */
1581 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1582 DWC3_DEVTEN_EVNTOVERFLOWEN |
1583 DWC3_DEVTEN_CMDCMPLTEN |
1584 DWC3_DEVTEN_ERRTICERREN |
1585 DWC3_DEVTEN_WKUPEVTEN |
1586 DWC3_DEVTEN_ULSTCNGEN |
1587 DWC3_DEVTEN_CONNECTDONEEN |
1588 DWC3_DEVTEN_USBRSTEN |
1589 DWC3_DEVTEN_DISCONNEVTEN);
1590
1591 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1592}
1593
1594static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1595{
1596 /* mask all interrupts */
1597 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1598}
1599
1600static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001601static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001602
Felipe Balbi4e994722016-05-13 14:09:59 +03001603/**
1604 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1605 * dwc: pointer to our context structure
1606 *
1607 * The following looks like complex but it's actually very simple. In order to
1608 * calculate the number of packets we can burst at once on OUT transfers, we're
1609 * gonna use RxFIFO size.
1610 *
1611 * To calculate RxFIFO size we need two numbers:
1612 * MDWIDTH = size, in bits, of the internal memory bus
1613 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1614 *
1615 * Given these two numbers, the formula is simple:
1616 *
1617 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1618 *
1619 * 24 bytes is for 3x SETUP packets
1620 * 16 bytes is a clock domain crossing tolerance
1621 *
1622 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1623 */
1624static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1625{
1626 u32 ram2_depth;
1627 u32 mdwidth;
1628 u32 nump;
1629 u32 reg;
1630
1631 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1632 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1633
1634 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1635 nump = min_t(u32, nump, 16);
1636
1637 /* update NumP */
1638 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1639 reg &= ~DWC3_DCFG_NUMP_MASK;
1640 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1641 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1642}
1643
Felipe Balbid7be2952016-05-04 15:49:37 +03001644static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001645{
Felipe Balbi72246da2011-08-19 18:10:58 +03001646 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001647 int ret = 0;
1648 u32 reg;
1649
Felipe Balbi72246da2011-08-19 18:10:58 +03001650 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1651 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001652
1653 /**
1654 * WORKAROUND: DWC3 revision < 2.20a have an issue
1655 * which would cause metastability state on Run/Stop
1656 * bit if we try to force the IP to USB2-only mode.
1657 *
1658 * Because of that, we cannot configure the IP to any
1659 * speed other than the SuperSpeed
1660 *
1661 * Refers to:
1662 *
1663 * STAR#9000525659: Clock Domain Crossing on DCTL in
1664 * USB 2.0 Mode
1665 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001666 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001667 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001668 } else {
1669 switch (dwc->maximum_speed) {
1670 case USB_SPEED_LOW:
1671 reg |= DWC3_DSTS_LOWSPEED;
1672 break;
1673 case USB_SPEED_FULL:
1674 reg |= DWC3_DSTS_FULLSPEED1;
1675 break;
1676 case USB_SPEED_HIGH:
1677 reg |= DWC3_DSTS_HIGHSPEED;
1678 break;
John Youn75808622016-02-05 17:09:13 -08001679 case USB_SPEED_SUPER_PLUS:
1680 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1681 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001682 default:
John Youn77966eb2016-02-19 17:31:01 -08001683 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1684 dwc->maximum_speed);
1685 /* fall through */
1686 case USB_SPEED_SUPER:
1687 reg |= DWC3_DCFG_SUPERSPEED;
1688 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001689 }
1690 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001691 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1692
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001693 /*
1694 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1695 * field instead of letting dwc3 itself calculate that automatically.
1696 *
1697 * This way, we maximize the chances that we'll be able to get several
1698 * bursts of data without going through any sort of endpoint throttling.
1699 */
1700 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1701 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1702 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1703
Felipe Balbi4e994722016-05-13 14:09:59 +03001704 dwc3_gadget_setup_nump(dwc);
1705
Felipe Balbi72246da2011-08-19 18:10:58 +03001706 /* Start with SuperSpeed Default */
1707 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1708
1709 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001710 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1711 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001712 if (ret) {
1713 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001714 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 }
1716
1717 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001718 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1719 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001720 if (ret) {
1721 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001722 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001723 }
1724
1725 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001726 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 dwc3_ep0_out_start(dwc);
1728
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001729 dwc3_gadget_enable_irq(dwc);
1730
Felipe Balbid7be2952016-05-04 15:49:37 +03001731 return 0;
1732
1733err1:
1734 __dwc3_gadget_ep_disable(dwc->eps[0]);
1735
1736err0:
1737 return ret;
1738}
1739
1740static int dwc3_gadget_start(struct usb_gadget *g,
1741 struct usb_gadget_driver *driver)
1742{
1743 struct dwc3 *dwc = gadget_to_dwc(g);
1744 unsigned long flags;
1745 int ret = 0;
1746 int irq;
1747
1748 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1749 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1750 IRQF_SHARED, "dwc3", dwc->ev_buf);
1751 if (ret) {
1752 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1753 irq, ret);
1754 goto err0;
1755 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001756 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001757
1758 spin_lock_irqsave(&dwc->lock, flags);
1759 if (dwc->gadget_driver) {
1760 dev_err(dwc->dev, "%s is already bound to %s\n",
1761 dwc->gadget.name,
1762 dwc->gadget_driver->driver.name);
1763 ret = -EBUSY;
1764 goto err1;
1765 }
1766
1767 dwc->gadget_driver = driver;
1768
1769 __dwc3_gadget_start(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001770 spin_unlock_irqrestore(&dwc->lock, flags);
1771
1772 return 0;
1773
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001774err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001776 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001777
1778err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001779 return ret;
1780}
1781
Felipe Balbid7be2952016-05-04 15:49:37 +03001782static void __dwc3_gadget_stop(struct dwc3 *dwc)
1783{
1784 dwc3_gadget_disable_irq(dwc);
1785 __dwc3_gadget_ep_disable(dwc->eps[0]);
1786 __dwc3_gadget_ep_disable(dwc->eps[1]);
1787}
1788
Felipe Balbi22835b82014-10-17 12:05:12 -05001789static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001790{
1791 struct dwc3 *dwc = gadget_to_dwc(g);
1792 unsigned long flags;
1793
1794 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001795 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001796 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001797 spin_unlock_irqrestore(&dwc->lock, flags);
1798
Felipe Balbi3f308d12016-05-16 14:17:06 +03001799 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001800
Felipe Balbi72246da2011-08-19 18:10:58 +03001801 return 0;
1802}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001803
Felipe Balbi72246da2011-08-19 18:10:58 +03001804static const struct usb_gadget_ops dwc3_gadget_ops = {
1805 .get_frame = dwc3_gadget_get_frame,
1806 .wakeup = dwc3_gadget_wakeup,
1807 .set_selfpowered = dwc3_gadget_set_selfpowered,
1808 .pullup = dwc3_gadget_pullup,
1809 .udc_start = dwc3_gadget_start,
1810 .udc_stop = dwc3_gadget_stop,
1811};
1812
1813/* -------------------------------------------------------------------------- */
1814
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001815static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1816 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001817{
1818 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001819 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001820
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001821 for (i = 0; i < num; i++) {
1822 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001823
Felipe Balbi72246da2011-08-19 18:10:58 +03001824 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001825 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001827
1828 dep->dwc = dwc;
1829 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001830 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001831 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001832 dwc->eps[epnum] = dep;
1833
1834 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1835 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001836
Felipe Balbi72246da2011-08-19 18:10:58 +03001837 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001838
Felipe Balbi73815282015-01-27 13:48:14 -06001839 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001840
Felipe Balbi72246da2011-08-19 18:10:58 +03001841 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001842 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301843 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001844 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1845 if (!epnum)
1846 dwc->gadget.ep0 = &dep->endpoint;
1847 } else {
1848 int ret;
1849
Robert Baldygae117e742013-12-13 12:23:38 +01001850 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001851 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001852 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1853 list_add_tail(&dep->endpoint.ep_list,
1854 &dwc->gadget.ep_list);
1855
1856 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001857 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001859 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001860
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001861 if (epnum == 0 || epnum == 1) {
1862 dep->endpoint.caps.type_control = true;
1863 } else {
1864 dep->endpoint.caps.type_iso = true;
1865 dep->endpoint.caps.type_bulk = true;
1866 dep->endpoint.caps.type_int = true;
1867 }
1868
1869 dep->endpoint.caps.dir_in = !!direction;
1870 dep->endpoint.caps.dir_out = !direction;
1871
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001872 INIT_LIST_HEAD(&dep->pending_list);
1873 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001874 }
1875
1876 return 0;
1877}
1878
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001879static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1880{
1881 int ret;
1882
1883 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1884
1885 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1886 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001887 dwc3_trace(trace_dwc3_gadget,
1888 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001889 return ret;
1890 }
1891
1892 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1893 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001894 dwc3_trace(trace_dwc3_gadget,
1895 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001896 return ret;
1897 }
1898
1899 return 0;
1900}
1901
Felipe Balbi72246da2011-08-19 18:10:58 +03001902static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1903{
1904 struct dwc3_ep *dep;
1905 u8 epnum;
1906
1907 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1908 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001909 if (!dep)
1910 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301911 /*
1912 * Physical endpoints 0 and 1 are special; they form the
1913 * bi-directional USB endpoint 0.
1914 *
1915 * For those two physical endpoints, we don't allocate a TRB
1916 * pool nor do we add them the endpoints list. Due to that, we
1917 * shouldn't do these two operations otherwise we would end up
1918 * with all sorts of bugs when removing dwc3.ko.
1919 */
1920 if (epnum != 0 && epnum != 1) {
1921 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001922 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301923 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001924
1925 kfree(dep);
1926 }
1927}
1928
Felipe Balbi72246da2011-08-19 18:10:58 +03001929/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001930
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301931static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1932 struct dwc3_request *req, struct dwc3_trb *trb,
1933 const struct dwc3_event_depevt *event, int status)
1934{
1935 unsigned int count;
1936 unsigned int s_pkt = 0;
1937 unsigned int trb_status;
1938
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001939 trace_dwc3_complete_trb(dep, trb);
1940
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301941 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1942 /*
1943 * We continue despite the error. There is not much we
1944 * can do. If we don't clean it up we loop forever. If
1945 * we skip the TRB then it gets overwritten after a
1946 * while since we use them in a ring buffer. A BUG()
1947 * would help. Lets hope that if this occurs, someone
1948 * fixes the root cause instead of looking away :)
1949 */
1950 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1951 dep->name, trb);
1952 count = trb->size & DWC3_TRB_SIZE_MASK;
1953
1954 if (dep->direction) {
1955 if (count) {
1956 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1957 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001958 dwc3_trace(trace_dwc3_gadget,
1959 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301960 dep->name);
1961 /*
1962 * If missed isoc occurred and there is
1963 * no request queued then issue END
1964 * TRANSFER, so that core generates
1965 * next xfernotready and we will issue
1966 * a fresh START TRANSFER.
1967 * If there are still queued request
1968 * then wait, do not issue either END
1969 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001970 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301971 * giveback.If any future queued request
1972 * is successfully transferred then we
1973 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001974 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301975 */
1976 dep->flags |= DWC3_EP_MISSED_ISOC;
1977 } else {
1978 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1979 dep->name);
1980 status = -ECONNRESET;
1981 }
1982 } else {
1983 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1984 }
1985 } else {
1986 if (count && (event->status & DEPEVT_STATUS_SHORT))
1987 s_pkt = 1;
1988 }
1989
1990 /*
1991 * We assume here we will always receive the entire data block
1992 * which we should receive. Meaning, if we program RX to
1993 * receive 4K but we receive only 2K, we assume that's all we
1994 * should receive and we simply bounce the request back to the
1995 * gadget driver for further processing.
1996 */
1997 req->request.actual += req->request.length - count;
1998 if (s_pkt)
1999 return 1;
2000 if ((event->status & DEPEVT_STATUS_LST) &&
2001 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2002 DWC3_TRB_CTRL_HWO)))
2003 return 1;
2004 if ((event->status & DEPEVT_STATUS_IOC) &&
2005 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2006 return 1;
2007 return 0;
2008}
2009
Felipe Balbi72246da2011-08-19 18:10:58 +03002010static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2011 const struct dwc3_event_depevt *event, int status)
2012{
2013 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002014 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302015 unsigned int slot;
2016 unsigned int i;
2017 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002018
2019 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002020 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002021 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002022 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002023
Ville Syrjäläd115d702015-08-31 19:48:28 +03002024 i = 0;
2025 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002026 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002027 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002028 slot++;
2029 slot %= DWC3_TRB_NUM;
2030 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002031
Ville Syrjäläd115d702015-08-31 19:48:28 +03002032 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2033 event, status);
2034 if (ret)
2035 break;
2036 } while (++i < req->request.num_mapped_sgs);
2037
2038 dwc3_gadget_giveback(dep, req, status);
2039
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302040 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002041 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002042 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002043
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302044 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002045 list_empty(&dep->started_list)) {
2046 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302047 /*
2048 * If there is no entry in request list then do
2049 * not issue END TRANSFER now. Just set PENDING
2050 * flag, so that END TRANSFER is issued when an
2051 * entry is added into request list.
2052 */
2053 dep->flags = DWC3_EP_PENDING_REQUEST;
2054 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002055 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302056 dep->flags = DWC3_EP_ENABLED;
2057 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302058 return 1;
2059 }
2060
Konrad Leszczynski9cad39f2016-02-08 16:13:12 +01002061 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2062 if ((event->status & DEPEVT_STATUS_IOC) &&
2063 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2064 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002065 return 1;
2066}
2067
2068static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002069 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002070{
2071 unsigned status = 0;
2072 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002073 u32 is_xfer_complete;
2074
2075 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002076
2077 if (event->status & DEPEVT_STATUS_BUSERR)
2078 status = -ECONNRESET;
2079
Paul Zimmerman1d046792012-02-15 18:56:56 -08002080 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002081 if (clean_busy && (is_xfer_complete ||
2082 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002083 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002084
2085 /*
2086 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2087 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2088 */
2089 if (dwc->revision < DWC3_REVISION_183A) {
2090 u32 reg;
2091 int i;
2092
2093 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002094 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002095
2096 if (!(dep->flags & DWC3_EP_ENABLED))
2097 continue;
2098
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002099 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002100 return;
2101 }
2102
2103 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2104 reg |= dwc->u1u2;
2105 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2106
2107 dwc->u1u2 = 0;
2108 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002109
Felipe Balbie6e709b2015-09-28 15:16:56 -05002110 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002111 int ret;
2112
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002113 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002114 if (!ret || ret == -EBUSY)
2115 return;
2116 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002117}
2118
Felipe Balbi72246da2011-08-19 18:10:58 +03002119static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2120 const struct dwc3_event_depevt *event)
2121{
2122 struct dwc3_ep *dep;
2123 u8 epnum = event->endpoint_number;
2124
2125 dep = dwc->eps[epnum];
2126
Felipe Balbi3336abb2012-06-06 09:19:35 +03002127 if (!(dep->flags & DWC3_EP_ENABLED))
2128 return;
2129
Felipe Balbi72246da2011-08-19 18:10:58 +03002130 if (epnum == 0 || epnum == 1) {
2131 dwc3_ep0_interrupt(dwc, event);
2132 return;
2133 }
2134
2135 switch (event->endpoint_event) {
2136 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002137 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002138
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002139 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002140 dwc3_trace(trace_dwc3_gadget,
2141 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002142 dep->name);
2143 return;
2144 }
2145
Jingoo Han029d97f2014-07-04 15:00:51 +09002146 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002147 break;
2148 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002149 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 break;
2151 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002152 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 dwc3_gadget_start_isoc(dwc, dep, event);
2154 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002155 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 int ret;
2157
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002158 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2159
Felipe Balbi73815282015-01-27 13:48:14 -06002160 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002161 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002162 : "Transfer Not Active");
2163
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002164 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002165 if (!ret || ret == -EBUSY)
2166 return;
2167
Felipe Balbiec5e7952015-11-16 16:04:13 -06002168 dwc3_trace(trace_dwc3_gadget,
2169 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002170 dep->name);
2171 }
2172
2173 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002174 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002175 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002176 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2177 dep->name);
2178 return;
2179 }
2180
2181 switch (event->status) {
2182 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002183 dwc3_trace(trace_dwc3_gadget,
2184 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002185 event->parameters);
2186
2187 break;
2188 case DEPEVT_STREAMEVT_NOTFOUND:
2189 /* FALLTHROUGH */
2190 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002191 dwc3_trace(trace_dwc3_gadget,
2192 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002193 }
2194 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002196 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002198 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002199 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002200 break;
2201 }
2202}
2203
2204static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2205{
2206 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2207 spin_unlock(&dwc->lock);
2208 dwc->gadget_driver->disconnect(&dwc->gadget);
2209 spin_lock(&dwc->lock);
2210 }
2211}
2212
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002213static void dwc3_suspend_gadget(struct dwc3 *dwc)
2214{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002215 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002216 spin_unlock(&dwc->lock);
2217 dwc->gadget_driver->suspend(&dwc->gadget);
2218 spin_lock(&dwc->lock);
2219 }
2220}
2221
2222static void dwc3_resume_gadget(struct dwc3 *dwc)
2223{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002224 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002225 spin_unlock(&dwc->lock);
2226 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002227 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002228 }
2229}
2230
2231static void dwc3_reset_gadget(struct dwc3 *dwc)
2232{
2233 if (!dwc->gadget_driver)
2234 return;
2235
2236 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2237 spin_unlock(&dwc->lock);
2238 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002239 spin_lock(&dwc->lock);
2240 }
2241}
2242
Paul Zimmermanb992e682012-04-27 14:17:35 +03002243static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002244{
2245 struct dwc3_ep *dep;
2246 struct dwc3_gadget_ep_cmd_params params;
2247 u32 cmd;
2248 int ret;
2249
2250 dep = dwc->eps[epnum];
2251
Felipe Balbib4996a82012-06-06 12:04:13 +03002252 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302253 return;
2254
Pratyush Anand57911502012-07-06 15:19:10 +05302255 /*
2256 * NOTICE: We are violating what the Databook says about the
2257 * EndTransfer command. Ideally we would _always_ wait for the
2258 * EndTransfer Command Completion IRQ, but that's causing too
2259 * much trouble synchronizing between us and gadget driver.
2260 *
2261 * We have discussed this with the IP Provider and it was
2262 * suggested to giveback all requests here, but give HW some
2263 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002264 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302265 *
2266 * Note also that a similar handling was tested by Synopsys
2267 * (thanks a lot Paul) and nothing bad has come out of it.
2268 * In short, what we're doing is:
2269 *
2270 * - Issue EndTransfer WITH CMDIOC bit set
2271 * - Wait 100us
2272 */
2273
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302274 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002275 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2276 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002277 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302278 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002279 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302280 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002281 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002282 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302283 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002284}
2285
2286static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2287{
2288 u32 epnum;
2289
2290 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2291 struct dwc3_ep *dep;
2292
2293 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002294 if (!dep)
2295 continue;
2296
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 if (!(dep->flags & DWC3_EP_ENABLED))
2298 continue;
2299
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002300 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002301 }
2302}
2303
2304static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2305{
2306 u32 epnum;
2307
2308 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2309 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002310 int ret;
2311
2312 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002313 if (!dep)
2314 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002315
2316 if (!(dep->flags & DWC3_EP_STALL))
2317 continue;
2318
2319 dep->flags &= ~DWC3_EP_STALL;
2320
John Youn50c763f2016-05-31 17:49:56 -07002321 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002322 WARN_ON_ONCE(ret);
2323 }
2324}
2325
2326static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2327{
Felipe Balbic4430a22012-05-24 10:30:01 +03002328 int reg;
2329
Felipe Balbi72246da2011-08-19 18:10:58 +03002330 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2331 reg &= ~DWC3_DCTL_INITU1ENA;
2332 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2333
2334 reg &= ~DWC3_DCTL_INITU2ENA;
2335 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002336
Felipe Balbi72246da2011-08-19 18:10:58 +03002337 dwc3_disconnect_gadget(dwc);
2338
2339 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002340 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002341 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002342}
2343
Felipe Balbi72246da2011-08-19 18:10:58 +03002344static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2345{
2346 u32 reg;
2347
Felipe Balbidf62df52011-10-14 15:11:49 +03002348 /*
2349 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2350 * would cause a missing Disconnect Event if there's a
2351 * pending Setup Packet in the FIFO.
2352 *
2353 * There's no suggested workaround on the official Bug
2354 * report, which states that "unless the driver/application
2355 * is doing any special handling of a disconnect event,
2356 * there is no functional issue".
2357 *
2358 * Unfortunately, it turns out that we _do_ some special
2359 * handling of a disconnect event, namely complete all
2360 * pending transfers, notify gadget driver of the
2361 * disconnection, and so on.
2362 *
2363 * Our suggested workaround is to follow the Disconnect
2364 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002365 * flag. Such flag gets set whenever we have a SETUP_PENDING
2366 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002367 * same endpoint.
2368 *
2369 * Refers to:
2370 *
2371 * STAR#9000466709: RTL: Device : Disconnect event not
2372 * generated if setup packet pending in FIFO
2373 */
2374 if (dwc->revision < DWC3_REVISION_188A) {
2375 if (dwc->setup_packet_pending)
2376 dwc3_gadget_disconnect_interrupt(dwc);
2377 }
2378
Felipe Balbi8e744752014-11-06 14:27:53 +08002379 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002380
2381 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2382 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2383 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002384 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002385
2386 dwc3_stop_active_transfers(dwc);
2387 dwc3_clear_stall_all_ep(dwc);
2388
2389 /* Reset device address to zero */
2390 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2391 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2392 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002393}
2394
2395static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2396{
2397 u32 reg;
2398 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2399
2400 /*
2401 * We change the clock only at SS but I dunno why I would want to do
2402 * this. Maybe it becomes part of the power saving plan.
2403 */
2404
John Younee5cd412016-02-05 17:08:45 -08002405 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2406 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002407 return;
2408
2409 /*
2410 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2411 * each time on Connect Done.
2412 */
2413 if (!usb30_clock)
2414 return;
2415
2416 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2417 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2418 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2419}
2420
Felipe Balbi72246da2011-08-19 18:10:58 +03002421static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2422{
Felipe Balbi72246da2011-08-19 18:10:58 +03002423 struct dwc3_ep *dep;
2424 int ret;
2425 u32 reg;
2426 u8 speed;
2427
Felipe Balbi72246da2011-08-19 18:10:58 +03002428 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2429 speed = reg & DWC3_DSTS_CONNECTSPD;
2430 dwc->speed = speed;
2431
2432 dwc3_update_ram_clk_sel(dwc, speed);
2433
2434 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002435 case DWC3_DCFG_SUPERSPEED_PLUS:
2436 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2437 dwc->gadget.ep0->maxpacket = 512;
2438 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2439 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002440 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002441 /*
2442 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2443 * would cause a missing USB3 Reset event.
2444 *
2445 * In such situations, we should force a USB3 Reset
2446 * event by calling our dwc3_gadget_reset_interrupt()
2447 * routine.
2448 *
2449 * Refers to:
2450 *
2451 * STAR#9000483510: RTL: SS : USB3 reset event may
2452 * not be generated always when the link enters poll
2453 */
2454 if (dwc->revision < DWC3_REVISION_190A)
2455 dwc3_gadget_reset_interrupt(dwc);
2456
Felipe Balbi72246da2011-08-19 18:10:58 +03002457 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2458 dwc->gadget.ep0->maxpacket = 512;
2459 dwc->gadget.speed = USB_SPEED_SUPER;
2460 break;
2461 case DWC3_DCFG_HIGHSPEED:
2462 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2463 dwc->gadget.ep0->maxpacket = 64;
2464 dwc->gadget.speed = USB_SPEED_HIGH;
2465 break;
2466 case DWC3_DCFG_FULLSPEED2:
2467 case DWC3_DCFG_FULLSPEED1:
2468 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2469 dwc->gadget.ep0->maxpacket = 64;
2470 dwc->gadget.speed = USB_SPEED_FULL;
2471 break;
2472 case DWC3_DCFG_LOWSPEED:
2473 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2474 dwc->gadget.ep0->maxpacket = 8;
2475 dwc->gadget.speed = USB_SPEED_LOW;
2476 break;
2477 }
2478
Pratyush Anand2b758352013-01-14 15:59:31 +05302479 /* Enable USB2 LPM Capability */
2480
John Younee5cd412016-02-05 17:08:45 -08002481 if ((dwc->revision > DWC3_REVISION_194A) &&
2482 (speed != DWC3_DCFG_SUPERSPEED) &&
2483 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302484 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2485 reg |= DWC3_DCFG_LPM_CAP;
2486 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2487
2488 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2489 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2490
Huang Rui460d0982014-10-31 11:11:18 +08002491 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302492
Huang Rui80caf7d2014-10-28 19:54:26 +08002493 /*
2494 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2495 * DCFG.LPMCap is set, core responses with an ACK and the
2496 * BESL value in the LPM token is less than or equal to LPM
2497 * NYET threshold.
2498 */
2499 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2500 && dwc->has_lpm_erratum,
2501 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2502
2503 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2504 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2505
Pratyush Anand2b758352013-01-14 15:59:31 +05302506 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002507 } else {
2508 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2509 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2510 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302511 }
2512
Felipe Balbi72246da2011-08-19 18:10:58 +03002513 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002514 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2515 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002516 if (ret) {
2517 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2518 return;
2519 }
2520
2521 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002522 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2523 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002524 if (ret) {
2525 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2526 return;
2527 }
2528
2529 /*
2530 * Configure PHY via GUSB3PIPECTLn if required.
2531 *
2532 * Update GTXFIFOSIZn
2533 *
2534 * In both cases reset values should be sufficient.
2535 */
2536}
2537
2538static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2539{
Felipe Balbi72246da2011-08-19 18:10:58 +03002540 /*
2541 * TODO take core out of low power mode when that's
2542 * implemented.
2543 */
2544
Jiebing Liad14d4e2014-12-11 13:26:29 +08002545 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2546 spin_unlock(&dwc->lock);
2547 dwc->gadget_driver->resume(&dwc->gadget);
2548 spin_lock(&dwc->lock);
2549 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002550}
2551
2552static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2553 unsigned int evtinfo)
2554{
Felipe Balbifae2b902011-10-14 13:00:30 +03002555 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002556 unsigned int pwropt;
2557
2558 /*
2559 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2560 * Hibernation mode enabled which would show up when device detects
2561 * host-initiated U3 exit.
2562 *
2563 * In that case, device will generate a Link State Change Interrupt
2564 * from U3 to RESUME which is only necessary if Hibernation is
2565 * configured in.
2566 *
2567 * There are no functional changes due to such spurious event and we
2568 * just need to ignore it.
2569 *
2570 * Refers to:
2571 *
2572 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2573 * operational mode
2574 */
2575 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2576 if ((dwc->revision < DWC3_REVISION_250A) &&
2577 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2578 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2579 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002580 dwc3_trace(trace_dwc3_gadget,
2581 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002582 return;
2583 }
2584 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002585
2586 /*
2587 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2588 * on the link partner, the USB session might do multiple entry/exit
2589 * of low power states before a transfer takes place.
2590 *
2591 * Due to this problem, we might experience lower throughput. The
2592 * suggested workaround is to disable DCTL[12:9] bits if we're
2593 * transitioning from U1/U2 to U0 and enable those bits again
2594 * after a transfer completes and there are no pending transfers
2595 * on any of the enabled endpoints.
2596 *
2597 * This is the first half of that workaround.
2598 *
2599 * Refers to:
2600 *
2601 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2602 * core send LGO_Ux entering U0
2603 */
2604 if (dwc->revision < DWC3_REVISION_183A) {
2605 if (next == DWC3_LINK_STATE_U0) {
2606 u32 u1u2;
2607 u32 reg;
2608
2609 switch (dwc->link_state) {
2610 case DWC3_LINK_STATE_U1:
2611 case DWC3_LINK_STATE_U2:
2612 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2613 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2614 | DWC3_DCTL_ACCEPTU2ENA
2615 | DWC3_DCTL_INITU1ENA
2616 | DWC3_DCTL_ACCEPTU1ENA);
2617
2618 if (!dwc->u1u2)
2619 dwc->u1u2 = reg & u1u2;
2620
2621 reg &= ~u1u2;
2622
2623 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2624 break;
2625 default:
2626 /* do nothing */
2627 break;
2628 }
2629 }
2630 }
2631
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002632 switch (next) {
2633 case DWC3_LINK_STATE_U1:
2634 if (dwc->speed == USB_SPEED_SUPER)
2635 dwc3_suspend_gadget(dwc);
2636 break;
2637 case DWC3_LINK_STATE_U2:
2638 case DWC3_LINK_STATE_U3:
2639 dwc3_suspend_gadget(dwc);
2640 break;
2641 case DWC3_LINK_STATE_RESUME:
2642 dwc3_resume_gadget(dwc);
2643 break;
2644 default:
2645 /* do nothing */
2646 break;
2647 }
2648
Felipe Balbie57ebc12014-04-22 13:20:12 -05002649 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002650}
2651
Felipe Balbie1dadd32014-02-25 14:47:54 -06002652static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2653 unsigned int evtinfo)
2654{
2655 unsigned int is_ss = evtinfo & BIT(4);
2656
2657 /**
2658 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2659 * have a known issue which can cause USB CV TD.9.23 to fail
2660 * randomly.
2661 *
2662 * Because of this issue, core could generate bogus hibernation
2663 * events which SW needs to ignore.
2664 *
2665 * Refers to:
2666 *
2667 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2668 * Device Fallback from SuperSpeed
2669 */
2670 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2671 return;
2672
2673 /* enter hibernation here */
2674}
2675
Felipe Balbi72246da2011-08-19 18:10:58 +03002676static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2677 const struct dwc3_event_devt *event)
2678{
2679 switch (event->type) {
2680 case DWC3_DEVICE_EVENT_DISCONNECT:
2681 dwc3_gadget_disconnect_interrupt(dwc);
2682 break;
2683 case DWC3_DEVICE_EVENT_RESET:
2684 dwc3_gadget_reset_interrupt(dwc);
2685 break;
2686 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2687 dwc3_gadget_conndone_interrupt(dwc);
2688 break;
2689 case DWC3_DEVICE_EVENT_WAKEUP:
2690 dwc3_gadget_wakeup_interrupt(dwc);
2691 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002692 case DWC3_DEVICE_EVENT_HIBER_REQ:
2693 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2694 "unexpected hibernation event\n"))
2695 break;
2696
2697 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2698 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002699 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2700 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2701 break;
2702 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002703 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002704 break;
2705 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002706 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 break;
2708 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002709 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 break;
2711 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002712 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 break;
2714 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002715 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002716 break;
2717 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002718 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002719 }
2720}
2721
2722static void dwc3_process_event_entry(struct dwc3 *dwc,
2723 const union dwc3_event *event)
2724{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002725 trace_dwc3_event(event->raw);
2726
Felipe Balbi72246da2011-08-19 18:10:58 +03002727 /* Endpoint IRQ, handle it and return early */
2728 if (event->type.is_devspec == 0) {
2729 /* depevt */
2730 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2731 }
2732
2733 switch (event->type.type) {
2734 case DWC3_EVENT_TYPE_DEV:
2735 dwc3_gadget_interrupt(dwc, &event->devt);
2736 break;
2737 /* REVISIT what to do with Carkit and I2C events ? */
2738 default:
2739 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2740 }
2741}
2742
Felipe Balbidea520a2016-03-30 09:39:34 +03002743static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002744{
Felipe Balbidea520a2016-03-30 09:39:34 +03002745 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002746 irqreturn_t ret = IRQ_NONE;
2747 int left;
2748 u32 reg;
2749
Felipe Balbif42f2442013-06-12 21:25:08 +03002750 left = evt->count;
2751
2752 if (!(evt->flags & DWC3_EVENT_PENDING))
2753 return IRQ_NONE;
2754
2755 while (left > 0) {
2756 union dwc3_event event;
2757
2758 event.raw = *(u32 *) (evt->buf + evt->lpos);
2759
2760 dwc3_process_event_entry(dwc, &event);
2761
2762 /*
2763 * FIXME we wrap around correctly to the next entry as
2764 * almost all entries are 4 bytes in size. There is one
2765 * entry which has 12 bytes which is a regular entry
2766 * followed by 8 bytes data. ATM I don't know how
2767 * things are organized if we get next to the a
2768 * boundary so I worry about that once we try to handle
2769 * that.
2770 */
2771 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2772 left -= 4;
2773
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002774 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002775 }
2776
2777 evt->count = 0;
2778 evt->flags &= ~DWC3_EVENT_PENDING;
2779 ret = IRQ_HANDLED;
2780
2781 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002782 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002783 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002784 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002785
2786 return ret;
2787}
2788
Felipe Balbidea520a2016-03-30 09:39:34 +03002789static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002790{
Felipe Balbidea520a2016-03-30 09:39:34 +03002791 struct dwc3_event_buffer *evt = _evt;
2792 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002793 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002794 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002795
Felipe Balbie5f68b42015-10-12 13:25:44 -05002796 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002797 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002798 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002799
2800 return ret;
2801}
2802
Felipe Balbidea520a2016-03-30 09:39:34 +03002803static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002804{
Felipe Balbidea520a2016-03-30 09:39:34 +03002805 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002806 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002807 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002808
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002809 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002810 count &= DWC3_GEVNTCOUNT_MASK;
2811 if (!count)
2812 return IRQ_NONE;
2813
Felipe Balbib15a7622011-06-30 16:57:15 +03002814 evt->count = count;
2815 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002816
Felipe Balbie8adfc32013-06-12 21:11:14 +03002817 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002818 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002819 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002820 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002821
Felipe Balbib15a7622011-06-30 16:57:15 +03002822 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002823}
2824
Felipe Balbidea520a2016-03-30 09:39:34 +03002825static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002826{
Felipe Balbidea520a2016-03-30 09:39:34 +03002827 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002828
Felipe Balbidea520a2016-03-30 09:39:34 +03002829 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002830}
2831
2832/**
2833 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002834 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002835 *
2836 * Returns 0 on success otherwise negative errno.
2837 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002838int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002839{
Felipe Balbi72246da2011-08-19 18:10:58 +03002840 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002841
2842 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2843 &dwc->ctrl_req_addr, GFP_KERNEL);
2844 if (!dwc->ctrl_req) {
2845 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2846 ret = -ENOMEM;
2847 goto err0;
2848 }
2849
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302850 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002851 &dwc->ep0_trb_addr, GFP_KERNEL);
2852 if (!dwc->ep0_trb) {
2853 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2854 ret = -ENOMEM;
2855 goto err1;
2856 }
2857
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002858 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002859 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002860 ret = -ENOMEM;
2861 goto err2;
2862 }
2863
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002864 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002865 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2866 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002867 if (!dwc->ep0_bounce) {
2868 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2869 ret = -ENOMEM;
2870 goto err3;
2871 }
2872
Felipe Balbi04c03d12015-12-02 10:06:45 -06002873 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2874 if (!dwc->zlp_buf) {
2875 ret = -ENOMEM;
2876 goto err4;
2877 }
2878
Felipe Balbi72246da2011-08-19 18:10:58 +03002879 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002880 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002881 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002882 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002883 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002884
2885 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002886 * FIXME We might be setting max_speed to <SUPER, however versions
2887 * <2.20a of dwc3 have an issue with metastability (documented
2888 * elsewhere in this driver) which tells us we can't set max speed to
2889 * anything lower than SUPER.
2890 *
2891 * Because gadget.max_speed is only used by composite.c and function
2892 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2893 * to happen so we avoid sending SuperSpeed Capability descriptor
2894 * together with our BOS descriptor as that could confuse host into
2895 * thinking we can handle super speed.
2896 *
2897 * Note that, in fact, we won't even support GetBOS requests when speed
2898 * is less than super speed because we don't have means, yet, to tell
2899 * composite.c that we are USB 2.0 + LPM ECN.
2900 */
2901 if (dwc->revision < DWC3_REVISION_220A)
2902 dwc3_trace(trace_dwc3_gadget,
2903 "Changing max_speed on rev %08x\n",
2904 dwc->revision);
2905
2906 dwc->gadget.max_speed = dwc->maximum_speed;
2907
2908 /*
David Cohena4b9d942013-12-09 15:55:38 -08002909 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2910 * on ep out.
2911 */
2912 dwc->gadget.quirk_ep_out_aligned_size = true;
2913
2914 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002915 * REVISIT: Here we should clear all pending IRQs to be
2916 * sure we're starting from a well known location.
2917 */
2918
2919 ret = dwc3_gadget_init_endpoints(dwc);
2920 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002921 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002922
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2924 if (ret) {
2925 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002926 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002927 }
2928
2929 return 0;
2930
Felipe Balbi04c03d12015-12-02 10:06:45 -06002931err5:
2932 kfree(dwc->zlp_buf);
2933
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002934err4:
David Cohene1f80462013-09-11 17:42:47 -07002935 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002936 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2937 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002938
Felipe Balbi72246da2011-08-19 18:10:58 +03002939err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002940 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002941
2942err2:
2943 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2944 dwc->ep0_trb, dwc->ep0_trb_addr);
2945
2946err1:
2947 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2948 dwc->ctrl_req, dwc->ctrl_req_addr);
2949
2950err0:
2951 return ret;
2952}
2953
Felipe Balbi7415f172012-04-30 14:56:33 +03002954/* -------------------------------------------------------------------------- */
2955
Felipe Balbi72246da2011-08-19 18:10:58 +03002956void dwc3_gadget_exit(struct dwc3 *dwc)
2957{
Felipe Balbi72246da2011-08-19 18:10:58 +03002958 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002959
Felipe Balbi72246da2011-08-19 18:10:58 +03002960 dwc3_gadget_free_endpoints(dwc);
2961
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002962 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2963 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002964
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002965 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002966 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002967
2968 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2969 dwc->ep0_trb, dwc->ep0_trb_addr);
2970
2971 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2972 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002973}
Felipe Balbi7415f172012-04-30 14:56:33 +03002974
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002975int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002976{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002977 int ret;
2978
Roger Quadros9772b472016-04-12 11:33:29 +03002979 if (!dwc->gadget_driver)
2980 return 0;
2981
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002982 ret = dwc3_gadget_run_stop(dwc, false, false);
2983 if (ret < 0)
2984 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002985
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002986 dwc3_disconnect_gadget(dwc);
2987 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002988
2989 return 0;
2990}
2991
2992int dwc3_gadget_resume(struct dwc3 *dwc)
2993{
Felipe Balbi7415f172012-04-30 14:56:33 +03002994 int ret;
2995
Roger Quadros9772b472016-04-12 11:33:29 +03002996 if (!dwc->gadget_driver)
2997 return 0;
2998
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002999 ret = __dwc3_gadget_start(dwc);
3000 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003001 goto err0;
3002
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003003 ret = dwc3_gadget_run_stop(dwc, true, false);
3004 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003005 goto err1;
3006
Felipe Balbi7415f172012-04-30 14:56:33 +03003007 return 0;
3008
3009err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003010 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003011
3012err0:
3013 return ret;
3014}