blob: 378c14c45fcbb37993bc3ce5815323bb57568dd7 [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 */
261 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
262 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
263 susphy = true;
264 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
265 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
266 }
267
Felipe Balbic36d8e92016-04-04 12:46:33 +0300268 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
269 int needs_wakeup;
270
271 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
272 dwc->link_state == DWC3_LINK_STATE_U2 ||
273 dwc->link_state == DWC3_LINK_STATE_U3);
274
275 if (unlikely(needs_wakeup)) {
276 ret = __dwc3_gadget_wakeup(dwc);
277 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
278 ret);
279 }
280 }
281
Felipe Balbi2eb88012016-04-12 16:53:39 +0300282 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
283 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
284 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
Felipe Balbi2eb88012016-04-12 16:53:39 +0300286 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300288 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000290 int cmd_status = DWC3_DEPCMD_STATUS(reg);
291
Felipe Balbi73815282015-01-27 13:48:14 -0600292 dwc3_trace(trace_dwc3_gadget,
293 "Command Complete --> %d",
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294 cmd_status);
295
296 switch (cmd_status) {
297 case 0:
298 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300299 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000300 case DEPEVT_TRANSFER_NO_RESOURCE:
301 dwc3_trace(trace_dwc3_gadget, "%s: no resource available");
302 ret = -EINVAL;
303 break;
304 case DEPEVT_TRANSFER_BUS_EXPIRY:
305 /*
306 * SW issues START TRANSFER command to
307 * isochronous ep with future frame interval. If
308 * future interval time has already passed when
309 * core receives the command, it will respond
310 * with an error status of 'Bus Expiry'.
311 *
312 * Instead of always returning -EINVAL, let's
313 * give a hint to the gadget driver that this is
314 * the case by returning -EAGAIN.
315 */
316 dwc3_trace(trace_dwc3_gadget, "%s: bus expiry");
317 ret = -EAGAIN;
318 break;
319 default:
320 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
321 }
322
Felipe Balbic0ca3242016-04-04 09:11:51 +0300323 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 }
325
326 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300327 * We can't sleep here, because it is also called from
328 * interrupt context.
329 */
330 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600331 if (!timeout) {
332 dwc3_trace(trace_dwc3_gadget,
333 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 ret = -ETIMEDOUT;
335 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600336 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300338
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300339 if (unlikely(susphy)) {
340 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
341 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
342 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
343 }
344
Felipe Balbic0ca3242016-04-04 09:11:51 +0300345 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300346}
347
John Youn50c763f2016-05-31 17:49:56 -0700348static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
349{
350 struct dwc3 *dwc = dep->dwc;
351 struct dwc3_gadget_ep_cmd_params params;
352 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
353
354 /*
355 * As of core revision 2.60a the recommended programming model
356 * is to set the ClearPendIN bit when issuing a Clear Stall EP
357 * command for IN endpoints. This is to prevent an issue where
358 * some (non-compliant) hosts may not send ACK TPs for pending
359 * IN transfers due to a mishandled error condition. Synopsys
360 * STAR 9000614252.
361 */
362 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A))
363 cmd |= DWC3_DEPCMD_CLEARPENDIN;
364
365 memset(&params, 0, sizeof(params));
366
Felipe Balbi2cd47182016-04-12 16:42:43 +0300367 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700368}
369
Felipe Balbi72246da2011-08-19 18:10:58 +0300370static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200371 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300372{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300373 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
375 return dep->trb_pool_dma + offset;
376}
377
378static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
379{
380 struct dwc3 *dwc = dep->dwc;
381
382 if (dep->trb_pool)
383 return 0;
384
Felipe Balbi72246da2011-08-19 18:10:58 +0300385 dep->trb_pool = dma_alloc_coherent(dwc->dev,
386 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
387 &dep->trb_pool_dma, GFP_KERNEL);
388 if (!dep->trb_pool) {
389 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
390 dep->name);
391 return -ENOMEM;
392 }
393
394 return 0;
395}
396
397static void dwc3_free_trb_pool(struct dwc3_ep *dep)
398{
399 struct dwc3 *dwc = dep->dwc;
400
401 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
402 dep->trb_pool, dep->trb_pool_dma);
403
404 dep->trb_pool = NULL;
405 dep->trb_pool_dma = 0;
406}
407
John Younc4509602016-02-16 20:10:53 -0800408static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
409
410/**
411 * dwc3_gadget_start_config - Configure EP resources
412 * @dwc: pointer to our controller context structure
413 * @dep: endpoint that is being enabled
414 *
415 * The assignment of transfer resources cannot perfectly follow the
416 * data book due to the fact that the controller driver does not have
417 * all knowledge of the configuration in advance. It is given this
418 * information piecemeal by the composite gadget framework after every
419 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
420 * programming model in this scenario can cause errors. For two
421 * reasons:
422 *
423 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
424 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
425 * multiple interfaces.
426 *
427 * 2) The databook does not mention doing more DEPXFERCFG for new
428 * endpoint on alt setting (8.1.6).
429 *
430 * The following simplified method is used instead:
431 *
432 * All hardware endpoints can be assigned a transfer resource and this
433 * setting will stay persistent until either a core reset or
434 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
435 * do DEPXFERCFG for every hardware endpoint as well. We are
436 * guaranteed that there are as many transfer resources as endpoints.
437 *
438 * This function is called for each endpoint when it is being enabled
439 * but is triggered only when called for EP0-out, which always happens
440 * first, and which should only happen in one of the above conditions.
441 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300442static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
443{
444 struct dwc3_gadget_ep_cmd_params params;
445 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800446 int i;
447 int ret;
448
449 if (dep->number)
450 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
452 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800453 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300454
Felipe Balbi2cd47182016-04-12 16:42:43 +0300455 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800456 if (ret)
457 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300458
John Younc4509602016-02-16 20:10:53 -0800459 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
460 struct dwc3_ep *dep = dwc->eps[i];
461
462 if (!dep)
463 continue;
464
465 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
466 if (ret)
467 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 }
469
470 return 0;
471}
472
473static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200474 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300475 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600476 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300477{
478 struct dwc3_gadget_ep_cmd_params params;
479
480 memset(&params, 0x00, sizeof(params));
481
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300482 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900483 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
484
485 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800486 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300487 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300488 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900489 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300490
Felipe Balbi4b345c92012-07-16 14:08:16 +0300491 if (ignore)
492 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
493
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600494 if (restore) {
495 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
496 params.param2 |= dep->saved_state;
497 }
498
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300499 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
500 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300501
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200502 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300503 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
504 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300505 dep->stream_capable = true;
506 }
507
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500508 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300509 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300510
511 /*
512 * We are doing 1:1 mapping for endpoints, meaning
513 * Physical Endpoints 2 maps to Logical Endpoint 2 and
514 * so on. We consider the direction bit as part of the physical
515 * endpoint number. So USB endpoint 0x81 is 0x03.
516 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300517 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300518
519 /*
520 * We must use the lower 16 TX FIFOs even though
521 * HW might have more
522 */
523 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300524 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300525
526 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300527 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 dep->interval = 1 << (desc->bInterval - 1);
529 }
530
Felipe Balbi2cd47182016-04-12 16:42:43 +0300531 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300532}
533
534static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
535{
536 struct dwc3_gadget_ep_cmd_params params;
537
538 memset(&params, 0x00, sizeof(params));
539
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300540 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
Felipe Balbi2cd47182016-04-12 16:42:43 +0300542 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
543 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300544}
545
546/**
547 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
548 * @dep: endpoint to be initialized
549 * @desc: USB Endpoint Descriptor
550 *
551 * Caller should take care of locking
552 */
553static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200554 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300555 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600556 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300557{
558 struct dwc3 *dwc = dep->dwc;
559 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300560 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300561
Felipe Balbi73815282015-01-27 13:48:14 -0600562 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300563
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 if (!(dep->flags & DWC3_EP_ENABLED)) {
565 ret = dwc3_gadget_start_config(dwc, dep);
566 if (ret)
567 return ret;
568 }
569
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600570 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
571 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300572 if (ret)
573 return ret;
574
575 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200576 struct dwc3_trb *trb_st_hw;
577 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300578
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200579 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200580 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 dep->type = usb_endpoint_type(desc);
582 dep->flags |= DWC3_EP_ENABLED;
583
584 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
585 reg |= DWC3_DALEPENA_EP(dep->number);
586 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
587
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300588 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200589 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300591 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 trb_st_hw = &dep->trb_pool[0];
593
Felipe Balbif6bafc62012-02-06 11:04:53 +0200594 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700595 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300596
Felipe Balbif6bafc62012-02-06 11:04:53 +0200597 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
598 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
599 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
600 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 }
602
Felipe Balbie901aa12016-03-16 14:01:37 +0200603out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500604 switch (usb_endpoint_type(desc)) {
605 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200606 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500607 break;
608 case USB_ENDPOINT_XFER_ISOC:
609 strlcat(dep->name, "-isoc", sizeof(dep->name));
610 break;
611 case USB_ENDPOINT_XFER_BULK:
612 strlcat(dep->name, "-bulk", sizeof(dep->name));
613 break;
614 case USB_ENDPOINT_XFER_INT:
615 strlcat(dep->name, "-int", sizeof(dep->name));
616 break;
617 default:
618 dev_err(dwc->dev, "invalid endpoint transfer type\n");
619 }
620
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 return 0;
622}
623
Paul Zimmermanb992e682012-04-27 14:17:35 +0300624static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200625static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300626{
627 struct dwc3_request *req;
628
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200629 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300630 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200631
Pratyush Anand57911502012-07-06 15:19:10 +0530632 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200633 while (!list_empty(&dep->started_list)) {
634 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530635
636 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
637 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200638 }
639
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200640 while (!list_empty(&dep->pending_list)) {
641 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300642
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200643 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300645}
646
647/**
648 * __dwc3_gadget_ep_disable - Disables a HW endpoint
649 * @dep: the endpoint to disable
650 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200651 * This function also removes requests which are currently processed ny the
652 * hardware and those which are not yet scheduled.
653 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300654 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300655static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
656{
657 struct dwc3 *dwc = dep->dwc;
658 u32 reg;
659
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500660 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
661
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200662 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300663
Felipe Balbi687ef982014-04-16 10:30:33 -0500664 /* make sure HW endpoint isn't stalled */
665 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500666 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500667
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
669 reg &= ~DWC3_DALEPENA_EP(dep->number);
670 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
671
Felipe Balbi879631a2011-09-30 10:58:47 +0300672 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200673 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200674 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300676 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300677
Felipe Balbiaa739972015-07-20 14:48:13 -0500678 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
679 dep->number >> 1,
680 (dep->number & 1) ? "in" : "out");
681
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 return 0;
683}
684
685/* -------------------------------------------------------------------------- */
686
687static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
688 const struct usb_endpoint_descriptor *desc)
689{
690 return -EINVAL;
691}
692
693static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
694{
695 return -EINVAL;
696}
697
698/* -------------------------------------------------------------------------- */
699
700static int dwc3_gadget_ep_enable(struct usb_ep *ep,
701 const struct usb_endpoint_descriptor *desc)
702{
703 struct dwc3_ep *dep;
704 struct dwc3 *dwc;
705 unsigned long flags;
706 int ret;
707
708 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
709 pr_debug("dwc3: invalid parameters\n");
710 return -EINVAL;
711 }
712
713 if (!desc->wMaxPacketSize) {
714 pr_debug("dwc3: missing wMaxPacketSize\n");
715 return -EINVAL;
716 }
717
718 dep = to_dwc3_ep(ep);
719 dwc = dep->dwc;
720
Felipe Balbi95ca9612015-12-10 13:08:20 -0600721 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
722 "%s is already enabled\n",
723 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300724 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300725
Felipe Balbi72246da2011-08-19 18:10:58 +0300726 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600727 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300728 spin_unlock_irqrestore(&dwc->lock, flags);
729
730 return ret;
731}
732
733static int dwc3_gadget_ep_disable(struct usb_ep *ep)
734{
735 struct dwc3_ep *dep;
736 struct dwc3 *dwc;
737 unsigned long flags;
738 int ret;
739
740 if (!ep) {
741 pr_debug("dwc3: invalid parameters\n");
742 return -EINVAL;
743 }
744
745 dep = to_dwc3_ep(ep);
746 dwc = dep->dwc;
747
Felipe Balbi95ca9612015-12-10 13:08:20 -0600748 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
749 "%s is already disabled\n",
750 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300751 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300752
Felipe Balbi72246da2011-08-19 18:10:58 +0300753 spin_lock_irqsave(&dwc->lock, flags);
754 ret = __dwc3_gadget_ep_disable(dep);
755 spin_unlock_irqrestore(&dwc->lock, flags);
756
757 return ret;
758}
759
760static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
761 gfp_t gfp_flags)
762{
763 struct dwc3_request *req;
764 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300765
766 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900767 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300768 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300769
770 req->epnum = dep->number;
771 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300772
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500773 trace_dwc3_alloc_request(req);
774
Felipe Balbi72246da2011-08-19 18:10:58 +0300775 return &req->request;
776}
777
778static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
779 struct usb_request *request)
780{
781 struct dwc3_request *req = to_dwc3_request(request);
782
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500783 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300784 kfree(req);
785}
786
Felipe Balbic71fc372011-11-22 11:37:34 +0200787/**
788 * dwc3_prepare_one_trb - setup one TRB from one request
789 * @dep: endpoint for which this request is prepared
790 * @req: dwc3_request pointer
791 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200792static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200793 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530794 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200795{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200797
Felipe Balbi73815282015-01-27 13:48:14 -0600798 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200799 dep->name, req, (unsigned long long) dma,
800 length, last ? " last" : "",
801 chain ? " chain" : "");
802
Pratyush Anand915e2022013-01-14 15:59:35 +0530803
Felipe Balbi4faf7552016-04-05 13:14:31 +0300804 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200805
Felipe Balbieeb720f2011-11-28 12:46:59 +0200806 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200807 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200808 req->trb = trb;
809 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300810 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200811 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200812
Felipe Balbief966b92016-04-05 13:09:51 +0300813 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300814 /* Skip the LINK-TRB */
815 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300816 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530817
Felipe Balbif6bafc62012-02-06 11:04:53 +0200818 trb->size = DWC3_TRB_SIZE_LENGTH(length);
819 trb->bpl = lower_32_bits(dma);
820 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200821
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200822 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200823 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200824 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200825 break;
826
827 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530828 if (!node)
829 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
830 else
831 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200832
833 /* always enable Interrupt on Missed ISOC */
834 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200835 break;
836
837 case USB_ENDPOINT_XFER_BULK:
838 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200839 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200840 break;
841 default:
842 /*
843 * This is only possible with faulty memory because we
844 * checked it already :)
845 */
846 BUG();
847 }
848
Felipe Balbica4d44e2016-03-10 13:53:27 +0200849 /* always enable Continue on Short Packet */
850 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600851
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300852 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200853 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
854
855 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530856 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200857
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530858 if (chain)
859 trb->ctrl |= DWC3_TRB_CTRL_CHN;
860
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200861 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200862 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
863
864 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500865
866 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200867}
868
Felipe Balbic4233572016-05-12 14:08:34 +0300869static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
870{
871 struct dwc3_trb *tmp;
872
873 /*
874 * If enqueue & dequeue are equal than it is either full or empty.
875 *
876 * One way to know for sure is if the TRB right before us has HWO bit
877 * set or not. If it has, then we're definitely full and can't fit any
878 * more transfers in our ring.
879 */
880 if (dep->trb_enqueue == dep->trb_dequeue) {
881 /* If we're full, enqueue/dequeue are > 0 */
882 if (dep->trb_enqueue) {
883 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
884 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
885 return 0;
886 }
887
888 return DWC3_TRB_NUM - 1;
889 }
890
891 return dep->trb_dequeue - dep->trb_enqueue;
892}
893
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300894static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
895 struct dwc3_request *req, unsigned int trbs_left)
896{
897 struct usb_request *request = &req->request;
898 struct scatterlist *sg = request->sg;
899 struct scatterlist *s;
900 unsigned int last = false;
901 unsigned int length;
902 dma_addr_t dma;
903 int i;
904
905 for_each_sg(sg, s, request->num_mapped_sgs, i) {
906 unsigned chain = true;
907
908 length = sg_dma_len(s);
909 dma = sg_dma_address(s);
910
911 if (sg_is_last(s)) {
912 if (list_is_last(&req->list, &dep->pending_list))
913 last = true;
914
915 chain = false;
916 }
917
918 if (!trbs_left)
919 last = true;
920
921 if (last)
922 chain = false;
923
924 dwc3_prepare_one_trb(dep, req, dma, length,
925 last, chain, i);
926
927 if (last)
928 break;
929 }
930}
931
932static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
933 struct dwc3_request *req, unsigned int trbs_left)
934{
935 unsigned int last = false;
936 unsigned int length;
937 dma_addr_t dma;
938
939 dma = req->request.dma;
940 length = req->request.length;
941
942 if (!trbs_left)
943 last = true;
944
945 /* Is this the last request? */
946 if (list_is_last(&req->list, &dep->pending_list))
947 last = true;
948
949 dwc3_prepare_one_trb(dep, req, dma, length,
950 last, false, 0);
951}
952
Felipe Balbi72246da2011-08-19 18:10:58 +0300953/*
954 * dwc3_prepare_trbs - setup TRBs from requests
955 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300956 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800957 * The function goes through the requests list and sets up TRBs for the
958 * transfers. The function returns once there are no more TRBs available or
959 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300960 */
Felipe Balbic4233572016-05-12 14:08:34 +0300961static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300962{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200963 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 u32 trbs_left;
965
966 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
967
Felipe Balbic4233572016-05-12 14:08:34 +0300968 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300969
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200970 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300971 if (req->request.num_mapped_sgs > 0)
972 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
973 else
974 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300975
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300976 if (!trbs_left)
977 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300978 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300979}
980
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300981static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300982{
983 struct dwc3_gadget_ep_cmd_params params;
984 struct dwc3_request *req;
985 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300986 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 int ret;
988 u32 cmd;
989
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300990 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300991
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300992 dwc3_prepare_trbs(dep);
993 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300994 if (!req) {
995 dep->flags |= DWC3_EP_PENDING_REQUEST;
996 return 0;
997 }
998
999 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001000
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001001 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301002 params.param0 = upper_32_bits(req->trb_dma);
1003 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301005 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301007 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001008
1009 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
Felipe Balbi2cd47182016-04-12 16:42:43 +03001010 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001011 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001012 /*
1013 * FIXME we need to iterate over the list of requests
1014 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001015 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001017 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1018 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001019 list_del(&req->list);
1020 return ret;
1021 }
1022
1023 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001024
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001025 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001026 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001027 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001028 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001029
Felipe Balbi72246da2011-08-19 18:10:58 +03001030 return 0;
1031}
1032
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301033static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1034 struct dwc3_ep *dep, u32 cur_uf)
1035{
1036 u32 uf;
1037
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001038 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001039 dwc3_trace(trace_dwc3_gadget,
1040 "ISOC ep %s run out for requests",
1041 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301042 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301043 return;
1044 }
1045
1046 /* 4 micro frames in the future */
1047 uf = cur_uf + dep->interval * 4;
1048
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001049 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301050}
1051
1052static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1053 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1054{
1055 u32 cur_uf, mask;
1056
1057 mask = ~(dep->interval - 1);
1058 cur_uf = event->parameters & mask;
1059
1060 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1061}
1062
Felipe Balbi72246da2011-08-19 18:10:58 +03001063static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1064{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001065 struct dwc3 *dwc = dep->dwc;
1066 int ret;
1067
Felipe Balbibb423982015-11-16 15:31:21 -06001068 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001069 dwc3_trace(trace_dwc3_gadget,
1070 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001071 &req->request, dep->endpoint.name);
1072 return -ESHUTDOWN;
1073 }
1074
1075 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1076 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001077 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1078 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001079 return -EINVAL;
1080 }
1081
Felipe Balbi72246da2011-08-19 18:10:58 +03001082 req->request.actual = 0;
1083 req->request.status = -EINPROGRESS;
1084 req->direction = dep->direction;
1085 req->epnum = dep->number;
1086
Felipe Balbife84f522015-09-01 09:01:38 -05001087 trace_dwc3_ep_queue(req);
1088
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 /*
1090 * We only add to our list of requests now and
1091 * start consuming the list once we get XferNotReady
1092 * IRQ.
1093 *
1094 * That way, we avoid doing anything that we don't need
1095 * to do now and defer it until the point we receive a
1096 * particular token from the Host side.
1097 *
1098 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001099 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001100 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001101 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1102 dep->direction);
1103 if (ret)
1104 return ret;
1105
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001106 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001107
1108 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001109 * If there are no pending requests and the endpoint isn't already
1110 * busy, we will just start the request straight away.
1111 *
1112 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1113 * little bit faster.
1114 */
1115 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001116 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001117 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001118 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001119 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001120 }
1121
1122 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001123 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001124 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001125 * 1. XferNotReady with empty list of requests. We need to kick the
1126 * transfer here in that situation, otherwise we will be NAKing
1127 * forever. If we get XferNotReady before gadget driver has a
1128 * chance to queue a request, we will ACK the IRQ but won't be
1129 * able to receive the data until the next request is queued.
1130 * The following code is handling exactly that.
1131 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001132 */
1133 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301134 /*
1135 * If xfernotready is already elapsed and it is a case
1136 * of isoc transfer, then issue END TRANSFER, so that
1137 * you can receive xfernotready again and can have
1138 * notion of current microframe.
1139 */
1140 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001141 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001142 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301143 dep->flags = DWC3_EP_ENABLED;
1144 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301145 return 0;
1146 }
1147
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001148 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001149 if (!ret)
1150 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1151
Felipe Balbia8f32812015-09-16 10:40:07 -05001152 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001153 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001154
Felipe Balbib511e5e2012-06-06 12:00:50 +03001155 /*
1156 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1157 * kick the transfer here after queuing a request, otherwise the
1158 * core may not see the modified TRB(s).
1159 */
1160 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301161 (dep->flags & DWC3_EP_BUSY) &&
1162 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001163 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001164 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001165 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001166 }
1167
Felipe Balbib997ada2012-07-26 13:26:50 +03001168 /*
1169 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1170 * right away, otherwise host will not know we have streams to be
1171 * handled.
1172 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001173 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001174 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001175
Felipe Balbia8f32812015-09-16 10:40:07 -05001176out:
1177 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001178 dwc3_trace(trace_dwc3_gadget,
1179 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001180 dep->name);
1181 if (ret == -EBUSY)
1182 ret = 0;
1183
1184 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001185}
1186
Felipe Balbi04c03d12015-12-02 10:06:45 -06001187static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1188 struct usb_request *request)
1189{
1190 dwc3_gadget_ep_free_request(ep, request);
1191}
1192
1193static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1194{
1195 struct dwc3_request *req;
1196 struct usb_request *request;
1197 struct usb_ep *ep = &dep->endpoint;
1198
1199 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1200 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1201 if (!request)
1202 return -ENOMEM;
1203
1204 request->length = 0;
1205 request->buf = dwc->zlp_buf;
1206 request->complete = __dwc3_gadget_ep_zlp_complete;
1207
1208 req = to_dwc3_request(request);
1209
1210 return __dwc3_gadget_ep_queue(dep, req);
1211}
1212
Felipe Balbi72246da2011-08-19 18:10:58 +03001213static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1214 gfp_t gfp_flags)
1215{
1216 struct dwc3_request *req = to_dwc3_request(request);
1217 struct dwc3_ep *dep = to_dwc3_ep(ep);
1218 struct dwc3 *dwc = dep->dwc;
1219
1220 unsigned long flags;
1221
1222 int ret;
1223
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001224 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001225 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001226
1227 /*
1228 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1229 * setting request->zero, instead of doing magic, we will just queue an
1230 * extra usb_request ourselves so that it gets handled the same way as
1231 * any other request.
1232 */
John Yound92618982015-12-22 12:23:20 -08001233 if (ret == 0 && request->zero && request->length &&
1234 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001235 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1236
Felipe Balbi72246da2011-08-19 18:10:58 +03001237 spin_unlock_irqrestore(&dwc->lock, flags);
1238
1239 return ret;
1240}
1241
1242static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1243 struct usb_request *request)
1244{
1245 struct dwc3_request *req = to_dwc3_request(request);
1246 struct dwc3_request *r = NULL;
1247
1248 struct dwc3_ep *dep = to_dwc3_ep(ep);
1249 struct dwc3 *dwc = dep->dwc;
1250
1251 unsigned long flags;
1252 int ret = 0;
1253
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001254 trace_dwc3_ep_dequeue(req);
1255
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 spin_lock_irqsave(&dwc->lock, flags);
1257
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001258 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001259 if (r == req)
1260 break;
1261 }
1262
1263 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001264 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001265 if (r == req)
1266 break;
1267 }
1268 if (r == req) {
1269 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001270 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301271 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 }
1273 dev_err(dwc->dev, "request %p was not queued to %s\n",
1274 request, ep->name);
1275 ret = -EINVAL;
1276 goto out0;
1277 }
1278
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301279out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 /* giveback the request */
1281 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1282
1283out0:
1284 spin_unlock_irqrestore(&dwc->lock, flags);
1285
1286 return ret;
1287}
1288
Felipe Balbi7a608552014-09-24 14:19:52 -05001289int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001290{
1291 struct dwc3_gadget_ep_cmd_params params;
1292 struct dwc3 *dwc = dep->dwc;
1293 int ret;
1294
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001295 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1296 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1297 return -EINVAL;
1298 }
1299
Felipe Balbi72246da2011-08-19 18:10:58 +03001300 memset(&params, 0x00, sizeof(params));
1301
1302 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001303 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001304 (!list_empty(&dep->started_list) ||
1305 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001306 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001307 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001308 dep->name);
1309 return -EAGAIN;
1310 }
1311
Felipe Balbi2cd47182016-04-12 16:42:43 +03001312 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1313 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001315 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 dep->name);
1317 else
1318 dep->flags |= DWC3_EP_STALL;
1319 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001320
John Youn50c763f2016-05-31 17:49:56 -07001321 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001322 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001323 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001324 dep->name);
1325 else
Alan Sterna535d812013-11-01 12:05:12 -04001326 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001327 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001328
Felipe Balbi72246da2011-08-19 18:10:58 +03001329 return ret;
1330}
1331
1332static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1333{
1334 struct dwc3_ep *dep = to_dwc3_ep(ep);
1335 struct dwc3 *dwc = dep->dwc;
1336
1337 unsigned long flags;
1338
1339 int ret;
1340
1341 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001342 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001343 spin_unlock_irqrestore(&dwc->lock, flags);
1344
1345 return ret;
1346}
1347
1348static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1349{
1350 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001351 struct dwc3 *dwc = dep->dwc;
1352 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001353 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001354
Paul Zimmerman249a4562012-02-24 17:32:16 -08001355 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001356 dep->flags |= DWC3_EP_WEDGE;
1357
Pratyush Anand08f0d962012-06-25 22:40:43 +05301358 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001359 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301360 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001361 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001362 spin_unlock_irqrestore(&dwc->lock, flags);
1363
1364 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001365}
1366
1367/* -------------------------------------------------------------------------- */
1368
1369static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1370 .bLength = USB_DT_ENDPOINT_SIZE,
1371 .bDescriptorType = USB_DT_ENDPOINT,
1372 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1373};
1374
1375static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1376 .enable = dwc3_gadget_ep0_enable,
1377 .disable = dwc3_gadget_ep0_disable,
1378 .alloc_request = dwc3_gadget_ep_alloc_request,
1379 .free_request = dwc3_gadget_ep_free_request,
1380 .queue = dwc3_gadget_ep0_queue,
1381 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301382 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001383 .set_wedge = dwc3_gadget_ep_set_wedge,
1384};
1385
1386static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1387 .enable = dwc3_gadget_ep_enable,
1388 .disable = dwc3_gadget_ep_disable,
1389 .alloc_request = dwc3_gadget_ep_alloc_request,
1390 .free_request = dwc3_gadget_ep_free_request,
1391 .queue = dwc3_gadget_ep_queue,
1392 .dequeue = dwc3_gadget_ep_dequeue,
1393 .set_halt = dwc3_gadget_ep_set_halt,
1394 .set_wedge = dwc3_gadget_ep_set_wedge,
1395};
1396
1397/* -------------------------------------------------------------------------- */
1398
1399static int dwc3_gadget_get_frame(struct usb_gadget *g)
1400{
1401 struct dwc3 *dwc = gadget_to_dwc(g);
1402 u32 reg;
1403
1404 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1405 return DWC3_DSTS_SOFFN(reg);
1406}
1407
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001408static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001409{
Felipe Balbi72246da2011-08-19 18:10:58 +03001410 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001411
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001412 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001413 u32 reg;
1414
Felipe Balbi72246da2011-08-19 18:10:58 +03001415 u8 link_state;
1416 u8 speed;
1417
Felipe Balbi72246da2011-08-19 18:10:58 +03001418 /*
1419 * According to the Databook Remote wakeup request should
1420 * be issued only when the device is in early suspend state.
1421 *
1422 * We can check that via USB Link State bits in DSTS register.
1423 */
1424 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1425
1426 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001427 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1428 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001429 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001430 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001431 }
1432
1433 link_state = DWC3_DSTS_USBLNKST(reg);
1434
1435 switch (link_state) {
1436 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1437 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1438 break;
1439 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001440 dwc3_trace(trace_dwc3_gadget,
1441 "can't wakeup from '%s'\n",
1442 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001443 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001444 }
1445
Felipe Balbi8598bde2012-01-02 18:55:57 +02001446 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1447 if (ret < 0) {
1448 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001449 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001450 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001451
Paul Zimmerman802fde92012-04-27 13:10:52 +03001452 /* Recent versions do this automatically */
1453 if (dwc->revision < DWC3_REVISION_194A) {
1454 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001455 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001456 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1457 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1458 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001459
Paul Zimmerman1d046792012-02-15 18:56:56 -08001460 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 timeout = jiffies + msecs_to_jiffies(100);
1462
Paul Zimmerman1d046792012-02-15 18:56:56 -08001463 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001464 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1465
1466 /* in HS, means ON */
1467 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1468 break;
1469 }
1470
1471 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1472 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001473 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 }
1475
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001476 return 0;
1477}
1478
1479static int dwc3_gadget_wakeup(struct usb_gadget *g)
1480{
1481 struct dwc3 *dwc = gadget_to_dwc(g);
1482 unsigned long flags;
1483 int ret;
1484
1485 spin_lock_irqsave(&dwc->lock, flags);
1486 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 spin_unlock_irqrestore(&dwc->lock, flags);
1488
1489 return ret;
1490}
1491
1492static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1493 int is_selfpowered)
1494{
1495 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001496 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001497
Paul Zimmerman249a4562012-02-24 17:32:16 -08001498 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001499 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001500 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001501
1502 return 0;
1503}
1504
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001505static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001506{
1507 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001508 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001509
1510 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001511 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001512 if (dwc->revision <= DWC3_REVISION_187A) {
1513 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1514 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1515 }
1516
1517 if (dwc->revision >= DWC3_REVISION_194A)
1518 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1519 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001520
1521 if (dwc->has_hibernation)
1522 reg |= DWC3_DCTL_KEEP_CONNECT;
1523
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001524 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001525 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001527
1528 if (dwc->has_hibernation && !suspend)
1529 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1530
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001531 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001532 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001533
1534 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1535
1536 do {
1537 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1538 if (is_on) {
1539 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1540 break;
1541 } else {
1542 if (reg & DWC3_DSTS_DEVCTRLHLT)
1543 break;
1544 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001545 timeout--;
1546 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301547 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001548 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001549 } while (1);
1550
Felipe Balbi73815282015-01-27 13:48:14 -06001551 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001552 dwc->gadget_driver
1553 ? dwc->gadget_driver->function : "no-function",
1554 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301555
1556 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001557}
1558
1559static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1560{
1561 struct dwc3 *dwc = gadget_to_dwc(g);
1562 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301563 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001564
1565 is_on = !!is_on;
1566
1567 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001568 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001569 spin_unlock_irqrestore(&dwc->lock, flags);
1570
Pratyush Anand6f17f742012-07-02 10:21:55 +05301571 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001572}
1573
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001574static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1575{
1576 u32 reg;
1577
1578 /* Enable all but Start and End of Frame IRQs */
1579 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1580 DWC3_DEVTEN_EVNTOVERFLOWEN |
1581 DWC3_DEVTEN_CMDCMPLTEN |
1582 DWC3_DEVTEN_ERRTICERREN |
1583 DWC3_DEVTEN_WKUPEVTEN |
1584 DWC3_DEVTEN_ULSTCNGEN |
1585 DWC3_DEVTEN_CONNECTDONEEN |
1586 DWC3_DEVTEN_USBRSTEN |
1587 DWC3_DEVTEN_DISCONNEVTEN);
1588
1589 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1590}
1591
1592static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1593{
1594 /* mask all interrupts */
1595 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1596}
1597
1598static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001599static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001600
Felipe Balbi4e994722016-05-13 14:09:59 +03001601/**
1602 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1603 * dwc: pointer to our context structure
1604 *
1605 * The following looks like complex but it's actually very simple. In order to
1606 * calculate the number of packets we can burst at once on OUT transfers, we're
1607 * gonna use RxFIFO size.
1608 *
1609 * To calculate RxFIFO size we need two numbers:
1610 * MDWIDTH = size, in bits, of the internal memory bus
1611 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1612 *
1613 * Given these two numbers, the formula is simple:
1614 *
1615 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1616 *
1617 * 24 bytes is for 3x SETUP packets
1618 * 16 bytes is a clock domain crossing tolerance
1619 *
1620 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1621 */
1622static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1623{
1624 u32 ram2_depth;
1625 u32 mdwidth;
1626 u32 nump;
1627 u32 reg;
1628
1629 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1630 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1631
1632 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1633 nump = min_t(u32, nump, 16);
1634
1635 /* update NumP */
1636 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1637 reg &= ~DWC3_DCFG_NUMP_MASK;
1638 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1639 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1640}
1641
Felipe Balbid7be2952016-05-04 15:49:37 +03001642static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001643{
Felipe Balbi72246da2011-08-19 18:10:58 +03001644 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001645 int ret = 0;
1646 u32 reg;
1647
Felipe Balbi72246da2011-08-19 18:10:58 +03001648 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1649 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001650
1651 /**
1652 * WORKAROUND: DWC3 revision < 2.20a have an issue
1653 * which would cause metastability state on Run/Stop
1654 * bit if we try to force the IP to USB2-only mode.
1655 *
1656 * Because of that, we cannot configure the IP to any
1657 * speed other than the SuperSpeed
1658 *
1659 * Refers to:
1660 *
1661 * STAR#9000525659: Clock Domain Crossing on DCTL in
1662 * USB 2.0 Mode
1663 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001664 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001665 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001666 } else {
1667 switch (dwc->maximum_speed) {
1668 case USB_SPEED_LOW:
1669 reg |= DWC3_DSTS_LOWSPEED;
1670 break;
1671 case USB_SPEED_FULL:
1672 reg |= DWC3_DSTS_FULLSPEED1;
1673 break;
1674 case USB_SPEED_HIGH:
1675 reg |= DWC3_DSTS_HIGHSPEED;
1676 break;
John Youn75808622016-02-05 17:09:13 -08001677 case USB_SPEED_SUPER_PLUS:
1678 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1679 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001680 default:
John Youn77966eb2016-02-19 17:31:01 -08001681 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1682 dwc->maximum_speed);
1683 /* fall through */
1684 case USB_SPEED_SUPER:
1685 reg |= DWC3_DCFG_SUPERSPEED;
1686 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001687 }
1688 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001689 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1690
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001691 /*
1692 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1693 * field instead of letting dwc3 itself calculate that automatically.
1694 *
1695 * This way, we maximize the chances that we'll be able to get several
1696 * bursts of data without going through any sort of endpoint throttling.
1697 */
1698 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1699 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1700 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1701
Felipe Balbi4e994722016-05-13 14:09:59 +03001702 dwc3_gadget_setup_nump(dwc);
1703
Felipe Balbi72246da2011-08-19 18:10:58 +03001704 /* Start with SuperSpeed Default */
1705 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1706
1707 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001708 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1709 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001710 if (ret) {
1711 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001712 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001713 }
1714
1715 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001716 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1717 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001718 if (ret) {
1719 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001720 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 }
1722
1723 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001724 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001725 dwc3_ep0_out_start(dwc);
1726
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001727 dwc3_gadget_enable_irq(dwc);
1728
Felipe Balbid7be2952016-05-04 15:49:37 +03001729 return 0;
1730
1731err1:
1732 __dwc3_gadget_ep_disable(dwc->eps[0]);
1733
1734err0:
1735 return ret;
1736}
1737
1738static int dwc3_gadget_start(struct usb_gadget *g,
1739 struct usb_gadget_driver *driver)
1740{
1741 struct dwc3 *dwc = gadget_to_dwc(g);
1742 unsigned long flags;
1743 int ret = 0;
1744 int irq;
1745
1746 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1747 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1748 IRQF_SHARED, "dwc3", dwc->ev_buf);
1749 if (ret) {
1750 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1751 irq, ret);
1752 goto err0;
1753 }
Felipe Balbi3f308d12016-05-16 14:17:06 +03001754 dwc->irq_gadget = irq;
Felipe Balbid7be2952016-05-04 15:49:37 +03001755
1756 spin_lock_irqsave(&dwc->lock, flags);
1757 if (dwc->gadget_driver) {
1758 dev_err(dwc->dev, "%s is already bound to %s\n",
1759 dwc->gadget.name,
1760 dwc->gadget_driver->driver.name);
1761 ret = -EBUSY;
1762 goto err1;
1763 }
1764
1765 dwc->gadget_driver = driver;
1766
1767 __dwc3_gadget_start(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 spin_unlock_irqrestore(&dwc->lock, flags);
1769
1770 return 0;
1771
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001772err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001773 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001774 free_irq(irq, dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001775
1776err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001777 return ret;
1778}
1779
Felipe Balbid7be2952016-05-04 15:49:37 +03001780static void __dwc3_gadget_stop(struct dwc3 *dwc)
1781{
1782 dwc3_gadget_disable_irq(dwc);
1783 __dwc3_gadget_ep_disable(dwc->eps[0]);
1784 __dwc3_gadget_ep_disable(dwc->eps[1]);
1785}
1786
Felipe Balbi22835b82014-10-17 12:05:12 -05001787static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001788{
1789 struct dwc3 *dwc = gadget_to_dwc(g);
1790 unsigned long flags;
1791
1792 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001793 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001794 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 spin_unlock_irqrestore(&dwc->lock, flags);
1796
Felipe Balbi3f308d12016-05-16 14:17:06 +03001797 free_irq(dwc->irq_gadget, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001798
Felipe Balbi72246da2011-08-19 18:10:58 +03001799 return 0;
1800}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001801
Felipe Balbi72246da2011-08-19 18:10:58 +03001802static const struct usb_gadget_ops dwc3_gadget_ops = {
1803 .get_frame = dwc3_gadget_get_frame,
1804 .wakeup = dwc3_gadget_wakeup,
1805 .set_selfpowered = dwc3_gadget_set_selfpowered,
1806 .pullup = dwc3_gadget_pullup,
1807 .udc_start = dwc3_gadget_start,
1808 .udc_stop = dwc3_gadget_stop,
1809};
1810
1811/* -------------------------------------------------------------------------- */
1812
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001813static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1814 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001815{
1816 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001817 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001818
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001819 for (i = 0; i < num; i++) {
1820 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001821
Felipe Balbi72246da2011-08-19 18:10:58 +03001822 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001823 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001824 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001825
1826 dep->dwc = dwc;
1827 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001828 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03001829 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03001830 dwc->eps[epnum] = dep;
1831
1832 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1833 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001834
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001836
Felipe Balbi73815282015-01-27 13:48:14 -06001837 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001838
Felipe Balbi72246da2011-08-19 18:10:58 +03001839 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001840 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301841 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001842 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1843 if (!epnum)
1844 dwc->gadget.ep0 = &dep->endpoint;
1845 } else {
1846 int ret;
1847
Robert Baldygae117e742013-12-13 12:23:38 +01001848 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001849 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001850 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1851 list_add_tail(&dep->endpoint.ep_list,
1852 &dwc->gadget.ep_list);
1853
1854 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001855 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001856 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001858
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001859 if (epnum == 0 || epnum == 1) {
1860 dep->endpoint.caps.type_control = true;
1861 } else {
1862 dep->endpoint.caps.type_iso = true;
1863 dep->endpoint.caps.type_bulk = true;
1864 dep->endpoint.caps.type_int = true;
1865 }
1866
1867 dep->endpoint.caps.dir_in = !!direction;
1868 dep->endpoint.caps.dir_out = !direction;
1869
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001870 INIT_LIST_HEAD(&dep->pending_list);
1871 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001872 }
1873
1874 return 0;
1875}
1876
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001877static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1878{
1879 int ret;
1880
1881 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1882
1883 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1884 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001885 dwc3_trace(trace_dwc3_gadget,
1886 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001887 return ret;
1888 }
1889
1890 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1891 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001892 dwc3_trace(trace_dwc3_gadget,
1893 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001894 return ret;
1895 }
1896
1897 return 0;
1898}
1899
Felipe Balbi72246da2011-08-19 18:10:58 +03001900static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1901{
1902 struct dwc3_ep *dep;
1903 u8 epnum;
1904
1905 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1906 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001907 if (!dep)
1908 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301909 /*
1910 * Physical endpoints 0 and 1 are special; they form the
1911 * bi-directional USB endpoint 0.
1912 *
1913 * For those two physical endpoints, we don't allocate a TRB
1914 * pool nor do we add them the endpoints list. Due to that, we
1915 * shouldn't do these two operations otherwise we would end up
1916 * with all sorts of bugs when removing dwc3.ko.
1917 */
1918 if (epnum != 0 && epnum != 1) {
1919 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301921 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001922
1923 kfree(dep);
1924 }
1925}
1926
Felipe Balbi72246da2011-08-19 18:10:58 +03001927/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001928
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301929static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1930 struct dwc3_request *req, struct dwc3_trb *trb,
1931 const struct dwc3_event_depevt *event, int status)
1932{
1933 unsigned int count;
1934 unsigned int s_pkt = 0;
1935 unsigned int trb_status;
1936
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001937 trace_dwc3_complete_trb(dep, trb);
1938
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301939 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1940 /*
1941 * We continue despite the error. There is not much we
1942 * can do. If we don't clean it up we loop forever. If
1943 * we skip the TRB then it gets overwritten after a
1944 * while since we use them in a ring buffer. A BUG()
1945 * would help. Lets hope that if this occurs, someone
1946 * fixes the root cause instead of looking away :)
1947 */
1948 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1949 dep->name, trb);
1950 count = trb->size & DWC3_TRB_SIZE_MASK;
1951
1952 if (dep->direction) {
1953 if (count) {
1954 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1955 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001956 dwc3_trace(trace_dwc3_gadget,
1957 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301958 dep->name);
1959 /*
1960 * If missed isoc occurred and there is
1961 * no request queued then issue END
1962 * TRANSFER, so that core generates
1963 * next xfernotready and we will issue
1964 * a fresh START TRANSFER.
1965 * If there are still queued request
1966 * then wait, do not issue either END
1967 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001968 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301969 * giveback.If any future queued request
1970 * is successfully transferred then we
1971 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001972 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301973 */
1974 dep->flags |= DWC3_EP_MISSED_ISOC;
1975 } else {
1976 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1977 dep->name);
1978 status = -ECONNRESET;
1979 }
1980 } else {
1981 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1982 }
1983 } else {
1984 if (count && (event->status & DEPEVT_STATUS_SHORT))
1985 s_pkt = 1;
1986 }
1987
1988 /*
1989 * We assume here we will always receive the entire data block
1990 * which we should receive. Meaning, if we program RX to
1991 * receive 4K but we receive only 2K, we assume that's all we
1992 * should receive and we simply bounce the request back to the
1993 * gadget driver for further processing.
1994 */
1995 req->request.actual += req->request.length - count;
1996 if (s_pkt)
1997 return 1;
1998 if ((event->status & DEPEVT_STATUS_LST) &&
1999 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2000 DWC3_TRB_CTRL_HWO)))
2001 return 1;
2002 if ((event->status & DEPEVT_STATUS_IOC) &&
2003 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2004 return 1;
2005 return 0;
2006}
2007
Felipe Balbi72246da2011-08-19 18:10:58 +03002008static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2009 const struct dwc3_event_depevt *event, int status)
2010{
2011 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002012 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302013 unsigned int slot;
2014 unsigned int i;
2015 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002016
2017 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002018 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002019 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002020 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002021
Ville Syrjäläd115d702015-08-31 19:48:28 +03002022 i = 0;
2023 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002024 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002025 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002026 slot++;
2027 slot %= DWC3_TRB_NUM;
2028 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002029
Ville Syrjäläd115d702015-08-31 19:48:28 +03002030 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2031 event, status);
2032 if (ret)
2033 break;
2034 } while (++i < req->request.num_mapped_sgs);
2035
2036 dwc3_gadget_giveback(dep, req, status);
2037
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302038 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002039 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002040 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002041
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302042 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002043 list_empty(&dep->started_list)) {
2044 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302045 /*
2046 * If there is no entry in request list then do
2047 * not issue END TRANSFER now. Just set PENDING
2048 * flag, so that END TRANSFER is issued when an
2049 * entry is added into request list.
2050 */
2051 dep->flags = DWC3_EP_PENDING_REQUEST;
2052 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002053 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302054 dep->flags = DWC3_EP_ENABLED;
2055 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302056 return 1;
2057 }
2058
Felipe Balbi72246da2011-08-19 18:10:58 +03002059 return 1;
2060}
2061
2062static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002063 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002064{
2065 unsigned status = 0;
2066 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002067 u32 is_xfer_complete;
2068
2069 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002070
2071 if (event->status & DEPEVT_STATUS_BUSERR)
2072 status = -ECONNRESET;
2073
Paul Zimmerman1d046792012-02-15 18:56:56 -08002074 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002075 if (clean_busy && (is_xfer_complete ||
2076 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002077 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002078
2079 /*
2080 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2081 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2082 */
2083 if (dwc->revision < DWC3_REVISION_183A) {
2084 u32 reg;
2085 int i;
2086
2087 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002088 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002089
2090 if (!(dep->flags & DWC3_EP_ENABLED))
2091 continue;
2092
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002093 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002094 return;
2095 }
2096
2097 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2098 reg |= dwc->u1u2;
2099 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2100
2101 dwc->u1u2 = 0;
2102 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002103
Felipe Balbie6e709b2015-09-28 15:16:56 -05002104 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002105 int ret;
2106
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002107 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002108 if (!ret || ret == -EBUSY)
2109 return;
2110 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002111}
2112
Felipe Balbi72246da2011-08-19 18:10:58 +03002113static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2114 const struct dwc3_event_depevt *event)
2115{
2116 struct dwc3_ep *dep;
2117 u8 epnum = event->endpoint_number;
2118
2119 dep = dwc->eps[epnum];
2120
Felipe Balbi3336abb2012-06-06 09:19:35 +03002121 if (!(dep->flags & DWC3_EP_ENABLED))
2122 return;
2123
Felipe Balbi72246da2011-08-19 18:10:58 +03002124 if (epnum == 0 || epnum == 1) {
2125 dwc3_ep0_interrupt(dwc, event);
2126 return;
2127 }
2128
2129 switch (event->endpoint_event) {
2130 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002131 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002132
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002133 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002134 dwc3_trace(trace_dwc3_gadget,
2135 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002136 dep->name);
2137 return;
2138 }
2139
Jingoo Han029d97f2014-07-04 15:00:51 +09002140 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002141 break;
2142 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002143 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002144 break;
2145 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002146 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002147 dwc3_gadget_start_isoc(dwc, dep, event);
2148 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002149 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 int ret;
2151
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002152 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2153
Felipe Balbi73815282015-01-27 13:48:14 -06002154 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002155 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 : "Transfer Not Active");
2157
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002158 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002159 if (!ret || ret == -EBUSY)
2160 return;
2161
Felipe Balbiec5e7952015-11-16 16:04:13 -06002162 dwc3_trace(trace_dwc3_gadget,
2163 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002164 dep->name);
2165 }
2166
2167 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002168 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002169 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002170 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2171 dep->name);
2172 return;
2173 }
2174
2175 switch (event->status) {
2176 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002177 dwc3_trace(trace_dwc3_gadget,
2178 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002179 event->parameters);
2180
2181 break;
2182 case DEPEVT_STREAMEVT_NOTFOUND:
2183 /* FALLTHROUGH */
2184 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002185 dwc3_trace(trace_dwc3_gadget,
2186 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002187 }
2188 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002190 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002192 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002193 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002194 break;
2195 }
2196}
2197
2198static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2199{
2200 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2201 spin_unlock(&dwc->lock);
2202 dwc->gadget_driver->disconnect(&dwc->gadget);
2203 spin_lock(&dwc->lock);
2204 }
2205}
2206
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002207static void dwc3_suspend_gadget(struct dwc3 *dwc)
2208{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002209 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002210 spin_unlock(&dwc->lock);
2211 dwc->gadget_driver->suspend(&dwc->gadget);
2212 spin_lock(&dwc->lock);
2213 }
2214}
2215
2216static void dwc3_resume_gadget(struct dwc3 *dwc)
2217{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002218 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002219 spin_unlock(&dwc->lock);
2220 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002221 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002222 }
2223}
2224
2225static void dwc3_reset_gadget(struct dwc3 *dwc)
2226{
2227 if (!dwc->gadget_driver)
2228 return;
2229
2230 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2231 spin_unlock(&dwc->lock);
2232 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002233 spin_lock(&dwc->lock);
2234 }
2235}
2236
Paul Zimmermanb992e682012-04-27 14:17:35 +03002237static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002238{
2239 struct dwc3_ep *dep;
2240 struct dwc3_gadget_ep_cmd_params params;
2241 u32 cmd;
2242 int ret;
2243
2244 dep = dwc->eps[epnum];
2245
Felipe Balbib4996a82012-06-06 12:04:13 +03002246 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302247 return;
2248
Pratyush Anand57911502012-07-06 15:19:10 +05302249 /*
2250 * NOTICE: We are violating what the Databook says about the
2251 * EndTransfer command. Ideally we would _always_ wait for the
2252 * EndTransfer Command Completion IRQ, but that's causing too
2253 * much trouble synchronizing between us and gadget driver.
2254 *
2255 * We have discussed this with the IP Provider and it was
2256 * suggested to giveback all requests here, but give HW some
2257 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002258 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302259 *
2260 * Note also that a similar handling was tested by Synopsys
2261 * (thanks a lot Paul) and nothing bad has come out of it.
2262 * In short, what we're doing is:
2263 *
2264 * - Issue EndTransfer WITH CMDIOC bit set
2265 * - Wait 100us
2266 */
2267
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302268 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002269 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2270 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002271 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302272 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002273 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302274 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002275 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002276 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302277 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002278}
2279
2280static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2281{
2282 u32 epnum;
2283
2284 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2285 struct dwc3_ep *dep;
2286
2287 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002288 if (!dep)
2289 continue;
2290
Felipe Balbi72246da2011-08-19 18:10:58 +03002291 if (!(dep->flags & DWC3_EP_ENABLED))
2292 continue;
2293
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002294 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002295 }
2296}
2297
2298static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2299{
2300 u32 epnum;
2301
2302 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2303 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002304 int ret;
2305
2306 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002307 if (!dep)
2308 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002309
2310 if (!(dep->flags & DWC3_EP_STALL))
2311 continue;
2312
2313 dep->flags &= ~DWC3_EP_STALL;
2314
John Youn50c763f2016-05-31 17:49:56 -07002315 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002316 WARN_ON_ONCE(ret);
2317 }
2318}
2319
2320static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2321{
Felipe Balbic4430a22012-05-24 10:30:01 +03002322 int reg;
2323
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2325 reg &= ~DWC3_DCTL_INITU1ENA;
2326 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2327
2328 reg &= ~DWC3_DCTL_INITU2ENA;
2329 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002330
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 dwc3_disconnect_gadget(dwc);
2332
2333 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002334 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002335 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002336}
2337
Felipe Balbi72246da2011-08-19 18:10:58 +03002338static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2339{
2340 u32 reg;
2341
Felipe Balbidf62df52011-10-14 15:11:49 +03002342 /*
2343 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2344 * would cause a missing Disconnect Event if there's a
2345 * pending Setup Packet in the FIFO.
2346 *
2347 * There's no suggested workaround on the official Bug
2348 * report, which states that "unless the driver/application
2349 * is doing any special handling of a disconnect event,
2350 * there is no functional issue".
2351 *
2352 * Unfortunately, it turns out that we _do_ some special
2353 * handling of a disconnect event, namely complete all
2354 * pending transfers, notify gadget driver of the
2355 * disconnection, and so on.
2356 *
2357 * Our suggested workaround is to follow the Disconnect
2358 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002359 * flag. Such flag gets set whenever we have a SETUP_PENDING
2360 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002361 * same endpoint.
2362 *
2363 * Refers to:
2364 *
2365 * STAR#9000466709: RTL: Device : Disconnect event not
2366 * generated if setup packet pending in FIFO
2367 */
2368 if (dwc->revision < DWC3_REVISION_188A) {
2369 if (dwc->setup_packet_pending)
2370 dwc3_gadget_disconnect_interrupt(dwc);
2371 }
2372
Felipe Balbi8e744752014-11-06 14:27:53 +08002373 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002374
2375 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2376 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2377 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002378 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002379
2380 dwc3_stop_active_transfers(dwc);
2381 dwc3_clear_stall_all_ep(dwc);
2382
2383 /* Reset device address to zero */
2384 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2385 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2386 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002387}
2388
2389static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2390{
2391 u32 reg;
2392 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2393
2394 /*
2395 * We change the clock only at SS but I dunno why I would want to do
2396 * this. Maybe it becomes part of the power saving plan.
2397 */
2398
John Younee5cd412016-02-05 17:08:45 -08002399 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2400 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 return;
2402
2403 /*
2404 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2405 * each time on Connect Done.
2406 */
2407 if (!usb30_clock)
2408 return;
2409
2410 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2411 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2412 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2413}
2414
Felipe Balbi72246da2011-08-19 18:10:58 +03002415static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2416{
Felipe Balbi72246da2011-08-19 18:10:58 +03002417 struct dwc3_ep *dep;
2418 int ret;
2419 u32 reg;
2420 u8 speed;
2421
Felipe Balbi72246da2011-08-19 18:10:58 +03002422 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2423 speed = reg & DWC3_DSTS_CONNECTSPD;
2424 dwc->speed = speed;
2425
2426 dwc3_update_ram_clk_sel(dwc, speed);
2427
2428 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002429 case DWC3_DCFG_SUPERSPEED_PLUS:
2430 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2431 dwc->gadget.ep0->maxpacket = 512;
2432 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2433 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002434 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002435 /*
2436 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2437 * would cause a missing USB3 Reset event.
2438 *
2439 * In such situations, we should force a USB3 Reset
2440 * event by calling our dwc3_gadget_reset_interrupt()
2441 * routine.
2442 *
2443 * Refers to:
2444 *
2445 * STAR#9000483510: RTL: SS : USB3 reset event may
2446 * not be generated always when the link enters poll
2447 */
2448 if (dwc->revision < DWC3_REVISION_190A)
2449 dwc3_gadget_reset_interrupt(dwc);
2450
Felipe Balbi72246da2011-08-19 18:10:58 +03002451 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2452 dwc->gadget.ep0->maxpacket = 512;
2453 dwc->gadget.speed = USB_SPEED_SUPER;
2454 break;
2455 case DWC3_DCFG_HIGHSPEED:
2456 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2457 dwc->gadget.ep0->maxpacket = 64;
2458 dwc->gadget.speed = USB_SPEED_HIGH;
2459 break;
2460 case DWC3_DCFG_FULLSPEED2:
2461 case DWC3_DCFG_FULLSPEED1:
2462 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2463 dwc->gadget.ep0->maxpacket = 64;
2464 dwc->gadget.speed = USB_SPEED_FULL;
2465 break;
2466 case DWC3_DCFG_LOWSPEED:
2467 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2468 dwc->gadget.ep0->maxpacket = 8;
2469 dwc->gadget.speed = USB_SPEED_LOW;
2470 break;
2471 }
2472
Pratyush Anand2b758352013-01-14 15:59:31 +05302473 /* Enable USB2 LPM Capability */
2474
John Younee5cd412016-02-05 17:08:45 -08002475 if ((dwc->revision > DWC3_REVISION_194A) &&
2476 (speed != DWC3_DCFG_SUPERSPEED) &&
2477 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302478 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2479 reg |= DWC3_DCFG_LPM_CAP;
2480 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2481
2482 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2483 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2484
Huang Rui460d0982014-10-31 11:11:18 +08002485 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302486
Huang Rui80caf7d2014-10-28 19:54:26 +08002487 /*
2488 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2489 * DCFG.LPMCap is set, core responses with an ACK and the
2490 * BESL value in the LPM token is less than or equal to LPM
2491 * NYET threshold.
2492 */
2493 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2494 && dwc->has_lpm_erratum,
2495 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2496
2497 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2498 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2499
Pratyush Anand2b758352013-01-14 15:59:31 +05302500 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002501 } else {
2502 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2503 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2504 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302505 }
2506
Felipe Balbi72246da2011-08-19 18:10:58 +03002507 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002508 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2509 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002510 if (ret) {
2511 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2512 return;
2513 }
2514
2515 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002516 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2517 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002518 if (ret) {
2519 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2520 return;
2521 }
2522
2523 /*
2524 * Configure PHY via GUSB3PIPECTLn if required.
2525 *
2526 * Update GTXFIFOSIZn
2527 *
2528 * In both cases reset values should be sufficient.
2529 */
2530}
2531
2532static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2533{
Felipe Balbi72246da2011-08-19 18:10:58 +03002534 /*
2535 * TODO take core out of low power mode when that's
2536 * implemented.
2537 */
2538
Jiebing Liad14d4e2014-12-11 13:26:29 +08002539 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2540 spin_unlock(&dwc->lock);
2541 dwc->gadget_driver->resume(&dwc->gadget);
2542 spin_lock(&dwc->lock);
2543 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002544}
2545
2546static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2547 unsigned int evtinfo)
2548{
Felipe Balbifae2b902011-10-14 13:00:30 +03002549 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002550 unsigned int pwropt;
2551
2552 /*
2553 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2554 * Hibernation mode enabled which would show up when device detects
2555 * host-initiated U3 exit.
2556 *
2557 * In that case, device will generate a Link State Change Interrupt
2558 * from U3 to RESUME which is only necessary if Hibernation is
2559 * configured in.
2560 *
2561 * There are no functional changes due to such spurious event and we
2562 * just need to ignore it.
2563 *
2564 * Refers to:
2565 *
2566 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2567 * operational mode
2568 */
2569 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2570 if ((dwc->revision < DWC3_REVISION_250A) &&
2571 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2572 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2573 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002574 dwc3_trace(trace_dwc3_gadget,
2575 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002576 return;
2577 }
2578 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002579
2580 /*
2581 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2582 * on the link partner, the USB session might do multiple entry/exit
2583 * of low power states before a transfer takes place.
2584 *
2585 * Due to this problem, we might experience lower throughput. The
2586 * suggested workaround is to disable DCTL[12:9] bits if we're
2587 * transitioning from U1/U2 to U0 and enable those bits again
2588 * after a transfer completes and there are no pending transfers
2589 * on any of the enabled endpoints.
2590 *
2591 * This is the first half of that workaround.
2592 *
2593 * Refers to:
2594 *
2595 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2596 * core send LGO_Ux entering U0
2597 */
2598 if (dwc->revision < DWC3_REVISION_183A) {
2599 if (next == DWC3_LINK_STATE_U0) {
2600 u32 u1u2;
2601 u32 reg;
2602
2603 switch (dwc->link_state) {
2604 case DWC3_LINK_STATE_U1:
2605 case DWC3_LINK_STATE_U2:
2606 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2607 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2608 | DWC3_DCTL_ACCEPTU2ENA
2609 | DWC3_DCTL_INITU1ENA
2610 | DWC3_DCTL_ACCEPTU1ENA);
2611
2612 if (!dwc->u1u2)
2613 dwc->u1u2 = reg & u1u2;
2614
2615 reg &= ~u1u2;
2616
2617 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2618 break;
2619 default:
2620 /* do nothing */
2621 break;
2622 }
2623 }
2624 }
2625
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002626 switch (next) {
2627 case DWC3_LINK_STATE_U1:
2628 if (dwc->speed == USB_SPEED_SUPER)
2629 dwc3_suspend_gadget(dwc);
2630 break;
2631 case DWC3_LINK_STATE_U2:
2632 case DWC3_LINK_STATE_U3:
2633 dwc3_suspend_gadget(dwc);
2634 break;
2635 case DWC3_LINK_STATE_RESUME:
2636 dwc3_resume_gadget(dwc);
2637 break;
2638 default:
2639 /* do nothing */
2640 break;
2641 }
2642
Felipe Balbie57ebc12014-04-22 13:20:12 -05002643 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002644}
2645
Felipe Balbie1dadd32014-02-25 14:47:54 -06002646static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2647 unsigned int evtinfo)
2648{
2649 unsigned int is_ss = evtinfo & BIT(4);
2650
2651 /**
2652 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2653 * have a known issue which can cause USB CV TD.9.23 to fail
2654 * randomly.
2655 *
2656 * Because of this issue, core could generate bogus hibernation
2657 * events which SW needs to ignore.
2658 *
2659 * Refers to:
2660 *
2661 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2662 * Device Fallback from SuperSpeed
2663 */
2664 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2665 return;
2666
2667 /* enter hibernation here */
2668}
2669
Felipe Balbi72246da2011-08-19 18:10:58 +03002670static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2671 const struct dwc3_event_devt *event)
2672{
2673 switch (event->type) {
2674 case DWC3_DEVICE_EVENT_DISCONNECT:
2675 dwc3_gadget_disconnect_interrupt(dwc);
2676 break;
2677 case DWC3_DEVICE_EVENT_RESET:
2678 dwc3_gadget_reset_interrupt(dwc);
2679 break;
2680 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2681 dwc3_gadget_conndone_interrupt(dwc);
2682 break;
2683 case DWC3_DEVICE_EVENT_WAKEUP:
2684 dwc3_gadget_wakeup_interrupt(dwc);
2685 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002686 case DWC3_DEVICE_EVENT_HIBER_REQ:
2687 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2688 "unexpected hibernation event\n"))
2689 break;
2690
2691 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2692 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002693 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2694 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2695 break;
2696 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002697 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002698 break;
2699 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002700 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 break;
2702 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002703 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002704 break;
2705 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002706 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 break;
2708 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002709 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002710 break;
2711 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06002712 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 }
2714}
2715
2716static void dwc3_process_event_entry(struct dwc3 *dwc,
2717 const union dwc3_event *event)
2718{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002719 trace_dwc3_event(event->raw);
2720
Felipe Balbi72246da2011-08-19 18:10:58 +03002721 /* Endpoint IRQ, handle it and return early */
2722 if (event->type.is_devspec == 0) {
2723 /* depevt */
2724 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2725 }
2726
2727 switch (event->type.type) {
2728 case DWC3_EVENT_TYPE_DEV:
2729 dwc3_gadget_interrupt(dwc, &event->devt);
2730 break;
2731 /* REVISIT what to do with Carkit and I2C events ? */
2732 default:
2733 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2734 }
2735}
2736
Felipe Balbidea520a2016-03-30 09:39:34 +03002737static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002738{
Felipe Balbidea520a2016-03-30 09:39:34 +03002739 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002740 irqreturn_t ret = IRQ_NONE;
2741 int left;
2742 u32 reg;
2743
Felipe Balbif42f2442013-06-12 21:25:08 +03002744 left = evt->count;
2745
2746 if (!(evt->flags & DWC3_EVENT_PENDING))
2747 return IRQ_NONE;
2748
2749 while (left > 0) {
2750 union dwc3_event event;
2751
2752 event.raw = *(u32 *) (evt->buf + evt->lpos);
2753
2754 dwc3_process_event_entry(dwc, &event);
2755
2756 /*
2757 * FIXME we wrap around correctly to the next entry as
2758 * almost all entries are 4 bytes in size. There is one
2759 * entry which has 12 bytes which is a regular entry
2760 * followed by 8 bytes data. ATM I don't know how
2761 * things are organized if we get next to the a
2762 * boundary so I worry about that once we try to handle
2763 * that.
2764 */
2765 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2766 left -= 4;
2767
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002768 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002769 }
2770
2771 evt->count = 0;
2772 evt->flags &= ~DWC3_EVENT_PENDING;
2773 ret = IRQ_HANDLED;
2774
2775 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002776 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002777 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002778 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002779
2780 return ret;
2781}
2782
Felipe Balbidea520a2016-03-30 09:39:34 +03002783static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002784{
Felipe Balbidea520a2016-03-30 09:39:34 +03002785 struct dwc3_event_buffer *evt = _evt;
2786 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002787 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002788 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002789
Felipe Balbie5f68b42015-10-12 13:25:44 -05002790 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002791 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002792 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002793
2794 return ret;
2795}
2796
Felipe Balbidea520a2016-03-30 09:39:34 +03002797static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002798{
Felipe Balbidea520a2016-03-30 09:39:34 +03002799 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002800 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002801 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002802
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002803 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 count &= DWC3_GEVNTCOUNT_MASK;
2805 if (!count)
2806 return IRQ_NONE;
2807
Felipe Balbib15a7622011-06-30 16:57:15 +03002808 evt->count = count;
2809 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002810
Felipe Balbie8adfc32013-06-12 21:11:14 +03002811 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002812 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002813 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002814 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002815
Felipe Balbib15a7622011-06-30 16:57:15 +03002816 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002817}
2818
Felipe Balbidea520a2016-03-30 09:39:34 +03002819static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002820{
Felipe Balbidea520a2016-03-30 09:39:34 +03002821 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002822
Felipe Balbidea520a2016-03-30 09:39:34 +03002823 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002824}
2825
2826/**
2827 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002828 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002829 *
2830 * Returns 0 on success otherwise negative errno.
2831 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002832int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002833{
Felipe Balbi72246da2011-08-19 18:10:58 +03002834 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002835
2836 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2837 &dwc->ctrl_req_addr, GFP_KERNEL);
2838 if (!dwc->ctrl_req) {
2839 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2840 ret = -ENOMEM;
2841 goto err0;
2842 }
2843
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302844 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002845 &dwc->ep0_trb_addr, GFP_KERNEL);
2846 if (!dwc->ep0_trb) {
2847 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2848 ret = -ENOMEM;
2849 goto err1;
2850 }
2851
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002852 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002853 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 ret = -ENOMEM;
2855 goto err2;
2856 }
2857
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002858 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002859 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2860 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002861 if (!dwc->ep0_bounce) {
2862 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2863 ret = -ENOMEM;
2864 goto err3;
2865 }
2866
Felipe Balbi04c03d12015-12-02 10:06:45 -06002867 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2868 if (!dwc->zlp_buf) {
2869 ret = -ENOMEM;
2870 goto err4;
2871 }
2872
Felipe Balbi72246da2011-08-19 18:10:58 +03002873 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002874 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002875 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002876 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002877 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002878
2879 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002880 * FIXME We might be setting max_speed to <SUPER, however versions
2881 * <2.20a of dwc3 have an issue with metastability (documented
2882 * elsewhere in this driver) which tells us we can't set max speed to
2883 * anything lower than SUPER.
2884 *
2885 * Because gadget.max_speed is only used by composite.c and function
2886 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2887 * to happen so we avoid sending SuperSpeed Capability descriptor
2888 * together with our BOS descriptor as that could confuse host into
2889 * thinking we can handle super speed.
2890 *
2891 * Note that, in fact, we won't even support GetBOS requests when speed
2892 * is less than super speed because we don't have means, yet, to tell
2893 * composite.c that we are USB 2.0 + LPM ECN.
2894 */
2895 if (dwc->revision < DWC3_REVISION_220A)
2896 dwc3_trace(trace_dwc3_gadget,
2897 "Changing max_speed on rev %08x\n",
2898 dwc->revision);
2899
2900 dwc->gadget.max_speed = dwc->maximum_speed;
2901
2902 /*
David Cohena4b9d942013-12-09 15:55:38 -08002903 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2904 * on ep out.
2905 */
2906 dwc->gadget.quirk_ep_out_aligned_size = true;
2907
2908 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002909 * REVISIT: Here we should clear all pending IRQs to be
2910 * sure we're starting from a well known location.
2911 */
2912
2913 ret = dwc3_gadget_init_endpoints(dwc);
2914 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002915 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002916
Felipe Balbi72246da2011-08-19 18:10:58 +03002917 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2918 if (ret) {
2919 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002920 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002921 }
2922
2923 return 0;
2924
Felipe Balbi04c03d12015-12-02 10:06:45 -06002925err5:
2926 kfree(dwc->zlp_buf);
2927
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002928err4:
David Cohene1f80462013-09-11 17:42:47 -07002929 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002930 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2931 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002932
Felipe Balbi72246da2011-08-19 18:10:58 +03002933err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002934 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002935
2936err2:
2937 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2938 dwc->ep0_trb, dwc->ep0_trb_addr);
2939
2940err1:
2941 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2942 dwc->ctrl_req, dwc->ctrl_req_addr);
2943
2944err0:
2945 return ret;
2946}
2947
Felipe Balbi7415f172012-04-30 14:56:33 +03002948/* -------------------------------------------------------------------------- */
2949
Felipe Balbi72246da2011-08-19 18:10:58 +03002950void dwc3_gadget_exit(struct dwc3 *dwc)
2951{
Felipe Balbi72246da2011-08-19 18:10:58 +03002952 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002953
Felipe Balbi72246da2011-08-19 18:10:58 +03002954 dwc3_gadget_free_endpoints(dwc);
2955
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002956 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2957 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002958
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002959 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002960 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002961
2962 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2963 dwc->ep0_trb, dwc->ep0_trb_addr);
2964
2965 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2966 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002967}
Felipe Balbi7415f172012-04-30 14:56:33 +03002968
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002969int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002970{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002971 int ret;
2972
Roger Quadros9772b472016-04-12 11:33:29 +03002973 if (!dwc->gadget_driver)
2974 return 0;
2975
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002976 ret = dwc3_gadget_run_stop(dwc, false, false);
2977 if (ret < 0)
2978 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002979
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002980 dwc3_disconnect_gadget(dwc);
2981 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002982
2983 return 0;
2984}
2985
2986int dwc3_gadget_resume(struct dwc3 *dwc)
2987{
Felipe Balbi7415f172012-04-30 14:56:33 +03002988 int ret;
2989
Roger Quadros9772b472016-04-12 11:33:29 +03002990 if (!dwc->gadget_driver)
2991 return 0;
2992
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002993 ret = __dwc3_gadget_start(dwc);
2994 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002995 goto err0;
2996
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002997 ret = dwc3_gadget_run_stop(dwc, true, false);
2998 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002999 goto err1;
3000
Felipe Balbi7415f172012-04-30 14:56:33 +03003001 return 0;
3002
3003err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003004 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003005
3006err0:
3007 return ret;
3008}