blob: eb28326a633db055f9cef37213e551858af2d281 [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 Balbi72246da2011-08-19 18:10:58 +0300241int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
242 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
243{
244 struct dwc3_ep *dep = dwc->eps[ep];
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 Balbidc1c70a2011-09-30 10:58:51 +0300282 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
283 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
284 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
286 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
287 do {
288 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
289 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
367 return dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
368}
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
John Younc4509602016-02-16 20:10:53 -0800455 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
456 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
531 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
532 DWC3_DEPCMD_SETEPCONFIG, &params);
533}
534
535static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
536{
537 struct dwc3_gadget_ep_cmd_params params;
538
539 memset(&params, 0x00, sizeof(params));
540
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300541 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
543 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
544 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
545}
546
547/**
548 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
549 * @dep: endpoint to be initialized
550 * @desc: USB Endpoint Descriptor
551 *
552 * Caller should take care of locking
553 */
554static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200555 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300556 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600557 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300558{
559 struct dwc3 *dwc = dep->dwc;
560 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300561 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562
Felipe Balbi73815282015-01-27 13:48:14 -0600563 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300564
Felipe Balbi72246da2011-08-19 18:10:58 +0300565 if (!(dep->flags & DWC3_EP_ENABLED)) {
566 ret = dwc3_gadget_start_config(dwc, dep);
567 if (ret)
568 return ret;
569 }
570
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600571 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
572 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300573 if (ret)
574 return ret;
575
576 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200577 struct dwc3_trb *trb_st_hw;
578 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200580 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200581 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300582 dep->type = usb_endpoint_type(desc);
583 dep->flags |= DWC3_EP_ENABLED;
584
585 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
586 reg |= DWC3_DALEPENA_EP(dep->number);
587 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
588
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300589 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200590 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300592 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 trb_st_hw = &dep->trb_pool[0];
594
Felipe Balbif6bafc62012-02-06 11:04:53 +0200595 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700596 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300597
Felipe Balbif6bafc62012-02-06 11:04:53 +0200598 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
599 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
600 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
601 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300602 }
603
Felipe Balbie901aa12016-03-16 14:01:37 +0200604out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500605 switch (usb_endpoint_type(desc)) {
606 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200607 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500608 break;
609 case USB_ENDPOINT_XFER_ISOC:
610 strlcat(dep->name, "-isoc", sizeof(dep->name));
611 break;
612 case USB_ENDPOINT_XFER_BULK:
613 strlcat(dep->name, "-bulk", sizeof(dep->name));
614 break;
615 case USB_ENDPOINT_XFER_INT:
616 strlcat(dep->name, "-int", sizeof(dep->name));
617 break;
618 default:
619 dev_err(dwc->dev, "invalid endpoint transfer type\n");
620 }
621
Felipe Balbi72246da2011-08-19 18:10:58 +0300622 return 0;
623}
624
Paul Zimmermanb992e682012-04-27 14:17:35 +0300625static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200626static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300627{
628 struct dwc3_request *req;
629
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200630 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300631 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200632
Pratyush Anand57911502012-07-06 15:19:10 +0530633 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200634 while (!list_empty(&dep->started_list)) {
635 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530636
637 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
638 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200639 }
640
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200641 while (!list_empty(&dep->pending_list)) {
642 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300643
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200644 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300645 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300646}
647
648/**
649 * __dwc3_gadget_ep_disable - Disables a HW endpoint
650 * @dep: the endpoint to disable
651 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200652 * This function also removes requests which are currently processed ny the
653 * hardware and those which are not yet scheduled.
654 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300655 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300656static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
657{
658 struct dwc3 *dwc = dep->dwc;
659 u32 reg;
660
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500661 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
662
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200663 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300664
Felipe Balbi687ef982014-04-16 10:30:33 -0500665 /* make sure HW endpoint isn't stalled */
666 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500667 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500668
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
670 reg &= ~DWC3_DALEPENA_EP(dep->number);
671 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
672
Felipe Balbi879631a2011-09-30 10:58:47 +0300673 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200674 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200675 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300676 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300677 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300678
Felipe Balbiaa739972015-07-20 14:48:13 -0500679 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
680 dep->number >> 1,
681 (dep->number & 1) ? "in" : "out");
682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 return 0;
684}
685
686/* -------------------------------------------------------------------------- */
687
688static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
689 const struct usb_endpoint_descriptor *desc)
690{
691 return -EINVAL;
692}
693
694static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
695{
696 return -EINVAL;
697}
698
699/* -------------------------------------------------------------------------- */
700
701static int dwc3_gadget_ep_enable(struct usb_ep *ep,
702 const struct usb_endpoint_descriptor *desc)
703{
704 struct dwc3_ep *dep;
705 struct dwc3 *dwc;
706 unsigned long flags;
707 int ret;
708
709 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
710 pr_debug("dwc3: invalid parameters\n");
711 return -EINVAL;
712 }
713
714 if (!desc->wMaxPacketSize) {
715 pr_debug("dwc3: missing wMaxPacketSize\n");
716 return -EINVAL;
717 }
718
719 dep = to_dwc3_ep(ep);
720 dwc = dep->dwc;
721
Felipe Balbi95ca9612015-12-10 13:08:20 -0600722 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
723 "%s is already enabled\n",
724 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300725 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300726
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600728 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 spin_unlock_irqrestore(&dwc->lock, flags);
730
731 return ret;
732}
733
734static int dwc3_gadget_ep_disable(struct usb_ep *ep)
735{
736 struct dwc3_ep *dep;
737 struct dwc3 *dwc;
738 unsigned long flags;
739 int ret;
740
741 if (!ep) {
742 pr_debug("dwc3: invalid parameters\n");
743 return -EINVAL;
744 }
745
746 dep = to_dwc3_ep(ep);
747 dwc = dep->dwc;
748
Felipe Balbi95ca9612015-12-10 13:08:20 -0600749 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
750 "%s is already disabled\n",
751 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300753
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 spin_lock_irqsave(&dwc->lock, flags);
755 ret = __dwc3_gadget_ep_disable(dep);
756 spin_unlock_irqrestore(&dwc->lock, flags);
757
758 return ret;
759}
760
761static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
762 gfp_t gfp_flags)
763{
764 struct dwc3_request *req;
765 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300766
767 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900768 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300770
771 req->epnum = dep->number;
772 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300773
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500774 trace_dwc3_alloc_request(req);
775
Felipe Balbi72246da2011-08-19 18:10:58 +0300776 return &req->request;
777}
778
779static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
780 struct usb_request *request)
781{
782 struct dwc3_request *req = to_dwc3_request(request);
783
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500784 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300785 kfree(req);
786}
787
Felipe Balbic71fc372011-11-22 11:37:34 +0200788/**
789 * dwc3_prepare_one_trb - setup one TRB from one request
790 * @dep: endpoint for which this request is prepared
791 * @req: dwc3_request pointer
792 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200793static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200794 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530795 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200796{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200797 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200798
Felipe Balbi73815282015-01-27 13:48:14 -0600799 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200800 dep->name, req, (unsigned long long) dma,
801 length, last ? " last" : "",
802 chain ? " chain" : "");
803
Pratyush Anand915e2022013-01-14 15:59:35 +0530804
Felipe Balbi4faf7552016-04-05 13:14:31 +0300805 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200806
Felipe Balbieeb720f2011-11-28 12:46:59 +0200807 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200808 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200809 req->trb = trb;
810 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300811 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200812 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200813
Felipe Balbief966b92016-04-05 13:09:51 +0300814 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300815 /* Skip the LINK-TRB */
816 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300817 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530818
Felipe Balbif6bafc62012-02-06 11:04:53 +0200819 trb->size = DWC3_TRB_SIZE_LENGTH(length);
820 trb->bpl = lower_32_bits(dma);
821 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200822
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200823 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200824 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200825 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200826 break;
827
828 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530829 if (!node)
830 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
831 else
832 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200833
834 /* always enable Interrupt on Missed ISOC */
835 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200836 break;
837
838 case USB_ENDPOINT_XFER_BULK:
839 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200840 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200841 break;
842 default:
843 /*
844 * This is only possible with faulty memory because we
845 * checked it already :)
846 */
847 BUG();
848 }
849
Felipe Balbica4d44e2016-03-10 13:53:27 +0200850 /* always enable Continue on Short Packet */
851 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600852
Felipe Balbi8e7046b2016-04-06 10:01:14 +0300853 if (!req->request.no_interrupt && !chain)
Felipe Balbica4d44e2016-03-10 13:53:27 +0200854 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
855
856 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530857 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200858
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530859 if (chain)
860 trb->ctrl |= DWC3_TRB_CTRL_CHN;
861
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200862 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200863 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
864
865 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500866
867 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200868}
869
Felipe Balbic4233572016-05-12 14:08:34 +0300870static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
871{
872 struct dwc3_trb *tmp;
873
874 /*
875 * If enqueue & dequeue are equal than it is either full or empty.
876 *
877 * One way to know for sure is if the TRB right before us has HWO bit
878 * set or not. If it has, then we're definitely full and can't fit any
879 * more transfers in our ring.
880 */
881 if (dep->trb_enqueue == dep->trb_dequeue) {
882 /* If we're full, enqueue/dequeue are > 0 */
883 if (dep->trb_enqueue) {
884 tmp = &dep->trb_pool[dep->trb_enqueue - 1];
885 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
886 return 0;
887 }
888
889 return DWC3_TRB_NUM - 1;
890 }
891
892 return dep->trb_dequeue - dep->trb_enqueue;
893}
894
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300895static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
896 struct dwc3_request *req, unsigned int trbs_left)
897{
898 struct usb_request *request = &req->request;
899 struct scatterlist *sg = request->sg;
900 struct scatterlist *s;
901 unsigned int last = false;
902 unsigned int length;
903 dma_addr_t dma;
904 int i;
905
906 for_each_sg(sg, s, request->num_mapped_sgs, i) {
907 unsigned chain = true;
908
909 length = sg_dma_len(s);
910 dma = sg_dma_address(s);
911
912 if (sg_is_last(s)) {
913 if (list_is_last(&req->list, &dep->pending_list))
914 last = true;
915
916 chain = false;
917 }
918
919 if (!trbs_left)
920 last = true;
921
922 if (last)
923 chain = false;
924
925 dwc3_prepare_one_trb(dep, req, dma, length,
926 last, chain, i);
927
928 if (last)
929 break;
930 }
931}
932
933static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
934 struct dwc3_request *req, unsigned int trbs_left)
935{
936 unsigned int last = false;
937 unsigned int length;
938 dma_addr_t dma;
939
940 dma = req->request.dma;
941 length = req->request.length;
942
943 if (!trbs_left)
944 last = true;
945
946 /* Is this the last request? */
947 if (list_is_last(&req->list, &dep->pending_list))
948 last = true;
949
950 dwc3_prepare_one_trb(dep, req, dma, length,
951 last, false, 0);
952}
953
Felipe Balbi72246da2011-08-19 18:10:58 +0300954/*
955 * dwc3_prepare_trbs - setup TRBs from requests
956 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300957 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800958 * The function goes through the requests list and sets up TRBs for the
959 * transfers. The function returns once there are no more TRBs available or
960 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300961 */
Felipe Balbic4233572016-05-12 14:08:34 +0300962static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300963{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200964 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 u32 trbs_left;
966
967 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
968
Felipe Balbic4233572016-05-12 14:08:34 +0300969 trbs_left = dwc3_calc_trbs_left(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300970
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200971 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300972 if (req->request.num_mapped_sgs > 0)
973 dwc3_prepare_one_trb_sg(dep, req, trbs_left--);
974 else
975 dwc3_prepare_one_trb_linear(dep, req, trbs_left--);
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300977 if (!trbs_left)
978 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300979 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300980}
981
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300982static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300983{
984 struct dwc3_gadget_ep_cmd_params params;
985 struct dwc3_request *req;
986 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300987 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300988 int ret;
989 u32 cmd;
990
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300991 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300992
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300993 dwc3_prepare_trbs(dep);
994 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 if (!req) {
996 dep->flags |= DWC3_EP_PENDING_REQUEST;
997 return 0;
998 }
999
1000 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001001
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001002 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301003 params.param0 = upper_32_bits(req->trb_dma);
1004 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001005 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301006 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001007 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301008 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001009
1010 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1011 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1012 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001013 /*
1014 * FIXME we need to iterate over the list of requests
1015 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001016 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001017 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001018 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1019 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001020 list_del(&req->list);
1021 return ret;
1022 }
1023
1024 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001025
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001026 if (starting) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001027 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001028 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001029 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001030 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001031
Felipe Balbi72246da2011-08-19 18:10:58 +03001032 return 0;
1033}
1034
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301035static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1036 struct dwc3_ep *dep, u32 cur_uf)
1037{
1038 u32 uf;
1039
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001040 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001041 dwc3_trace(trace_dwc3_gadget,
1042 "ISOC ep %s run out for requests",
1043 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301044 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301045 return;
1046 }
1047
1048 /* 4 micro frames in the future */
1049 uf = cur_uf + dep->interval * 4;
1050
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001051 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301052}
1053
1054static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1055 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1056{
1057 u32 cur_uf, mask;
1058
1059 mask = ~(dep->interval - 1);
1060 cur_uf = event->parameters & mask;
1061
1062 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1063}
1064
Felipe Balbi72246da2011-08-19 18:10:58 +03001065static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1066{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001067 struct dwc3 *dwc = dep->dwc;
1068 int ret;
1069
Felipe Balbibb423982015-11-16 15:31:21 -06001070 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001071 dwc3_trace(trace_dwc3_gadget,
1072 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001073 &req->request, dep->endpoint.name);
1074 return -ESHUTDOWN;
1075 }
1076
1077 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1078 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001079 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1080 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001081 return -EINVAL;
1082 }
1083
Felipe Balbi72246da2011-08-19 18:10:58 +03001084 req->request.actual = 0;
1085 req->request.status = -EINPROGRESS;
1086 req->direction = dep->direction;
1087 req->epnum = dep->number;
1088
Felipe Balbife84f522015-09-01 09:01:38 -05001089 trace_dwc3_ep_queue(req);
1090
Felipe Balbi72246da2011-08-19 18:10:58 +03001091 /*
1092 * We only add to our list of requests now and
1093 * start consuming the list once we get XferNotReady
1094 * IRQ.
1095 *
1096 * That way, we avoid doing anything that we don't need
1097 * to do now and defer it until the point we receive a
1098 * particular token from the Host side.
1099 *
1100 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001101 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001102 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001103 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1104 dep->direction);
1105 if (ret)
1106 return ret;
1107
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001108 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001109
1110 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001111 * If there are no pending requests and the endpoint isn't already
1112 * busy, we will just start the request straight away.
1113 *
1114 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1115 * little bit faster.
1116 */
1117 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001118 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001119 !(dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001120 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001121 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001122 }
1123
1124 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001125 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001126 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001127 * 1. XferNotReady with empty list of requests. We need to kick the
1128 * transfer here in that situation, otherwise we will be NAKing
1129 * forever. If we get XferNotReady before gadget driver has a
1130 * chance to queue a request, we will ACK the IRQ but won't be
1131 * able to receive the data until the next request is queued.
1132 * The following code is handling exactly that.
1133 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001134 */
1135 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301136 /*
1137 * If xfernotready is already elapsed and it is a case
1138 * of isoc transfer, then issue END TRANSFER, so that
1139 * you can receive xfernotready again and can have
1140 * notion of current microframe.
1141 */
1142 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001143 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001144 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301145 dep->flags = DWC3_EP_ENABLED;
1146 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301147 return 0;
1148 }
1149
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001150 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi89185912015-09-15 09:49:14 -05001151 if (!ret)
1152 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1153
Felipe Balbia8f32812015-09-16 10:40:07 -05001154 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001155 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001156
Felipe Balbib511e5e2012-06-06 12:00:50 +03001157 /*
1158 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1159 * kick the transfer here after queuing a request, otherwise the
1160 * core may not see the modified TRB(s).
1161 */
1162 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301163 (dep->flags & DWC3_EP_BUSY) &&
1164 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001165 WARN_ON_ONCE(!dep->resource_index);
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001166 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index);
Felipe Balbia8f32812015-09-16 10:40:07 -05001167 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001168 }
1169
Felipe Balbib997ada2012-07-26 13:26:50 +03001170 /*
1171 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1172 * right away, otherwise host will not know we have streams to be
1173 * handled.
1174 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001175 if (dep->stream_capable)
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001176 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbib997ada2012-07-26 13:26:50 +03001177
Felipe Balbia8f32812015-09-16 10:40:07 -05001178out:
1179 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001180 dwc3_trace(trace_dwc3_gadget,
1181 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001182 dep->name);
1183 if (ret == -EBUSY)
1184 ret = 0;
1185
1186 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001187}
1188
Felipe Balbi04c03d12015-12-02 10:06:45 -06001189static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1190 struct usb_request *request)
1191{
1192 dwc3_gadget_ep_free_request(ep, request);
1193}
1194
1195static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1196{
1197 struct dwc3_request *req;
1198 struct usb_request *request;
1199 struct usb_ep *ep = &dep->endpoint;
1200
1201 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1202 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1203 if (!request)
1204 return -ENOMEM;
1205
1206 request->length = 0;
1207 request->buf = dwc->zlp_buf;
1208 request->complete = __dwc3_gadget_ep_zlp_complete;
1209
1210 req = to_dwc3_request(request);
1211
1212 return __dwc3_gadget_ep_queue(dep, req);
1213}
1214
Felipe Balbi72246da2011-08-19 18:10:58 +03001215static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1216 gfp_t gfp_flags)
1217{
1218 struct dwc3_request *req = to_dwc3_request(request);
1219 struct dwc3_ep *dep = to_dwc3_ep(ep);
1220 struct dwc3 *dwc = dep->dwc;
1221
1222 unsigned long flags;
1223
1224 int ret;
1225
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001226 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001227 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001228
1229 /*
1230 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1231 * setting request->zero, instead of doing magic, we will just queue an
1232 * extra usb_request ourselves so that it gets handled the same way as
1233 * any other request.
1234 */
John Yound92618982015-12-22 12:23:20 -08001235 if (ret == 0 && request->zero && request->length &&
1236 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001237 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1238
Felipe Balbi72246da2011-08-19 18:10:58 +03001239 spin_unlock_irqrestore(&dwc->lock, flags);
1240
1241 return ret;
1242}
1243
1244static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1245 struct usb_request *request)
1246{
1247 struct dwc3_request *req = to_dwc3_request(request);
1248 struct dwc3_request *r = NULL;
1249
1250 struct dwc3_ep *dep = to_dwc3_ep(ep);
1251 struct dwc3 *dwc = dep->dwc;
1252
1253 unsigned long flags;
1254 int ret = 0;
1255
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001256 trace_dwc3_ep_dequeue(req);
1257
Felipe Balbi72246da2011-08-19 18:10:58 +03001258 spin_lock_irqsave(&dwc->lock, flags);
1259
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001260 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001261 if (r == req)
1262 break;
1263 }
1264
1265 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001266 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001267 if (r == req)
1268 break;
1269 }
1270 if (r == req) {
1271 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001272 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301273 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001274 }
1275 dev_err(dwc->dev, "request %p was not queued to %s\n",
1276 request, ep->name);
1277 ret = -EINVAL;
1278 goto out0;
1279 }
1280
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301281out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001282 /* giveback the request */
1283 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1284
1285out0:
1286 spin_unlock_irqrestore(&dwc->lock, flags);
1287
1288 return ret;
1289}
1290
Felipe Balbi7a608552014-09-24 14:19:52 -05001291int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001292{
1293 struct dwc3_gadget_ep_cmd_params params;
1294 struct dwc3 *dwc = dep->dwc;
1295 int ret;
1296
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001297 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1298 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1299 return -EINVAL;
1300 }
1301
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 memset(&params, 0x00, sizeof(params));
1303
1304 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001305 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001306 (!list_empty(&dep->started_list) ||
1307 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001308 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001309 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001310 dep->name);
1311 return -EAGAIN;
1312 }
1313
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1315 DWC3_DEPCMD_SETSTALL, &params);
1316 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001317 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001318 dep->name);
1319 else
1320 dep->flags |= DWC3_EP_STALL;
1321 } else {
John Youn50c763f2016-05-31 17:49:56 -07001322 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001324 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001325 dep->name);
1326 else
Alan Sterna535d812013-11-01 12:05:12 -04001327 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001328 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001329
Felipe Balbi72246da2011-08-19 18:10:58 +03001330 return ret;
1331}
1332
1333static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1334{
1335 struct dwc3_ep *dep = to_dwc3_ep(ep);
1336 struct dwc3 *dwc = dep->dwc;
1337
1338 unsigned long flags;
1339
1340 int ret;
1341
1342 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001343 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001344 spin_unlock_irqrestore(&dwc->lock, flags);
1345
1346 return ret;
1347}
1348
1349static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1350{
1351 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001352 struct dwc3 *dwc = dep->dwc;
1353 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001354 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001355
Paul Zimmerman249a4562012-02-24 17:32:16 -08001356 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001357 dep->flags |= DWC3_EP_WEDGE;
1358
Pratyush Anand08f0d962012-06-25 22:40:43 +05301359 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001360 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301361 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001362 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001363 spin_unlock_irqrestore(&dwc->lock, flags);
1364
1365 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001366}
1367
1368/* -------------------------------------------------------------------------- */
1369
1370static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1371 .bLength = USB_DT_ENDPOINT_SIZE,
1372 .bDescriptorType = USB_DT_ENDPOINT,
1373 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1374};
1375
1376static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1377 .enable = dwc3_gadget_ep0_enable,
1378 .disable = dwc3_gadget_ep0_disable,
1379 .alloc_request = dwc3_gadget_ep_alloc_request,
1380 .free_request = dwc3_gadget_ep_free_request,
1381 .queue = dwc3_gadget_ep0_queue,
1382 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301383 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001384 .set_wedge = dwc3_gadget_ep_set_wedge,
1385};
1386
1387static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1388 .enable = dwc3_gadget_ep_enable,
1389 .disable = dwc3_gadget_ep_disable,
1390 .alloc_request = dwc3_gadget_ep_alloc_request,
1391 .free_request = dwc3_gadget_ep_free_request,
1392 .queue = dwc3_gadget_ep_queue,
1393 .dequeue = dwc3_gadget_ep_dequeue,
1394 .set_halt = dwc3_gadget_ep_set_halt,
1395 .set_wedge = dwc3_gadget_ep_set_wedge,
1396};
1397
1398/* -------------------------------------------------------------------------- */
1399
1400static int dwc3_gadget_get_frame(struct usb_gadget *g)
1401{
1402 struct dwc3 *dwc = gadget_to_dwc(g);
1403 u32 reg;
1404
1405 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1406 return DWC3_DSTS_SOFFN(reg);
1407}
1408
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001409static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001410{
Felipe Balbi72246da2011-08-19 18:10:58 +03001411 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001412
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001413 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001414 u32 reg;
1415
Felipe Balbi72246da2011-08-19 18:10:58 +03001416 u8 link_state;
1417 u8 speed;
1418
Felipe Balbi72246da2011-08-19 18:10:58 +03001419 /*
1420 * According to the Databook Remote wakeup request should
1421 * be issued only when the device is in early suspend state.
1422 *
1423 * We can check that via USB Link State bits in DSTS register.
1424 */
1425 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1426
1427 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001428 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1429 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001430 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi6b742892016-05-13 10:19:42 +03001431 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001432 }
1433
1434 link_state = DWC3_DSTS_USBLNKST(reg);
1435
1436 switch (link_state) {
1437 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1438 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1439 break;
1440 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001441 dwc3_trace(trace_dwc3_gadget,
1442 "can't wakeup from '%s'\n",
1443 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001444 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001445 }
1446
Felipe Balbi8598bde2012-01-02 18:55:57 +02001447 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1448 if (ret < 0) {
1449 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001450 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001451 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001452
Paul Zimmerman802fde92012-04-27 13:10:52 +03001453 /* Recent versions do this automatically */
1454 if (dwc->revision < DWC3_REVISION_194A) {
1455 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001456 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001457 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1458 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1459 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001460
Paul Zimmerman1d046792012-02-15 18:56:56 -08001461 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001462 timeout = jiffies + msecs_to_jiffies(100);
1463
Paul Zimmerman1d046792012-02-15 18:56:56 -08001464 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001465 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1466
1467 /* in HS, means ON */
1468 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1469 break;
1470 }
1471
1472 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1473 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001474 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 }
1476
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001477 return 0;
1478}
1479
1480static int dwc3_gadget_wakeup(struct usb_gadget *g)
1481{
1482 struct dwc3 *dwc = gadget_to_dwc(g);
1483 unsigned long flags;
1484 int ret;
1485
1486 spin_lock_irqsave(&dwc->lock, flags);
1487 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001488 spin_unlock_irqrestore(&dwc->lock, flags);
1489
1490 return ret;
1491}
1492
1493static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1494 int is_selfpowered)
1495{
1496 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001497 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001498
Paul Zimmerman249a4562012-02-24 17:32:16 -08001499 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001500 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001501 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001502
1503 return 0;
1504}
1505
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001506static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001507{
1508 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001509 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001510
1511 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001512 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001513 if (dwc->revision <= DWC3_REVISION_187A) {
1514 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1515 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1516 }
1517
1518 if (dwc->revision >= DWC3_REVISION_194A)
1519 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1520 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001521
1522 if (dwc->has_hibernation)
1523 reg |= DWC3_DCTL_KEEP_CONNECT;
1524
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001525 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001526 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001527 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001528
1529 if (dwc->has_hibernation && !suspend)
1530 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1531
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001532 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001533 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001534
1535 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1536
1537 do {
1538 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1539 if (is_on) {
1540 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1541 break;
1542 } else {
1543 if (reg & DWC3_DSTS_DEVCTRLHLT)
1544 break;
1545 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001546 timeout--;
1547 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301548 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001549 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001550 } while (1);
1551
Felipe Balbi73815282015-01-27 13:48:14 -06001552 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001553 dwc->gadget_driver
1554 ? dwc->gadget_driver->function : "no-function",
1555 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301556
1557 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558}
1559
1560static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1561{
1562 struct dwc3 *dwc = gadget_to_dwc(g);
1563 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301564 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001565
1566 is_on = !!is_on;
1567
1568 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001569 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001570 spin_unlock_irqrestore(&dwc->lock, flags);
1571
Pratyush Anand6f17f742012-07-02 10:21:55 +05301572 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001573}
1574
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001575static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1576{
1577 u32 reg;
1578
1579 /* Enable all but Start and End of Frame IRQs */
1580 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1581 DWC3_DEVTEN_EVNTOVERFLOWEN |
1582 DWC3_DEVTEN_CMDCMPLTEN |
1583 DWC3_DEVTEN_ERRTICERREN |
1584 DWC3_DEVTEN_WKUPEVTEN |
1585 DWC3_DEVTEN_ULSTCNGEN |
1586 DWC3_DEVTEN_CONNECTDONEEN |
1587 DWC3_DEVTEN_USBRSTEN |
1588 DWC3_DEVTEN_DISCONNEVTEN);
1589
1590 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1591}
1592
1593static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1594{
1595 /* mask all interrupts */
1596 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1597}
1598
1599static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001600static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001601
Felipe Balbi4e994722016-05-13 14:09:59 +03001602/**
1603 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1604 * dwc: pointer to our context structure
1605 *
1606 * The following looks like complex but it's actually very simple. In order to
1607 * calculate the number of packets we can burst at once on OUT transfers, we're
1608 * gonna use RxFIFO size.
1609 *
1610 * To calculate RxFIFO size we need two numbers:
1611 * MDWIDTH = size, in bits, of the internal memory bus
1612 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1613 *
1614 * Given these two numbers, the formula is simple:
1615 *
1616 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1617 *
1618 * 24 bytes is for 3x SETUP packets
1619 * 16 bytes is a clock domain crossing tolerance
1620 *
1621 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1622 */
1623static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1624{
1625 u32 ram2_depth;
1626 u32 mdwidth;
1627 u32 nump;
1628 u32 reg;
1629
1630 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1631 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1632
1633 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1634 nump = min_t(u32, nump, 16);
1635
1636 /* update NumP */
1637 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1638 reg &= ~DWC3_DCFG_NUMP_MASK;
1639 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1640 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1641}
1642
Felipe Balbid7be2952016-05-04 15:49:37 +03001643static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001644{
Felipe Balbi72246da2011-08-19 18:10:58 +03001645 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001646 int ret = 0;
1647 u32 reg;
1648
Felipe Balbi72246da2011-08-19 18:10:58 +03001649 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1650 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001651
1652 /**
1653 * WORKAROUND: DWC3 revision < 2.20a have an issue
1654 * which would cause metastability state on Run/Stop
1655 * bit if we try to force the IP to USB2-only mode.
1656 *
1657 * Because of that, we cannot configure the IP to any
1658 * speed other than the SuperSpeed
1659 *
1660 * Refers to:
1661 *
1662 * STAR#9000525659: Clock Domain Crossing on DCTL in
1663 * USB 2.0 Mode
1664 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001665 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001666 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001667 } else {
1668 switch (dwc->maximum_speed) {
1669 case USB_SPEED_LOW:
1670 reg |= DWC3_DSTS_LOWSPEED;
1671 break;
1672 case USB_SPEED_FULL:
1673 reg |= DWC3_DSTS_FULLSPEED1;
1674 break;
1675 case USB_SPEED_HIGH:
1676 reg |= DWC3_DSTS_HIGHSPEED;
1677 break;
John Youn75808622016-02-05 17:09:13 -08001678 case USB_SPEED_SUPER_PLUS:
1679 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1680 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001681 default:
John Youn77966eb2016-02-19 17:31:01 -08001682 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1683 dwc->maximum_speed);
1684 /* fall through */
1685 case USB_SPEED_SUPER:
1686 reg |= DWC3_DCFG_SUPERSPEED;
1687 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001688 }
1689 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001690 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1691
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001692 /*
1693 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1694 * field instead of letting dwc3 itself calculate that automatically.
1695 *
1696 * This way, we maximize the chances that we'll be able to get several
1697 * bursts of data without going through any sort of endpoint throttling.
1698 */
1699 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1700 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1701 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1702
Felipe Balbi4e994722016-05-13 14:09:59 +03001703 dwc3_gadget_setup_nump(dwc);
1704
Felipe Balbi72246da2011-08-19 18:10:58 +03001705 /* Start with SuperSpeed Default */
1706 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1707
1708 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001709 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1710 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001711 if (ret) {
1712 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001713 goto err0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 }
1715
1716 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001717 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1718 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001719 if (ret) {
1720 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbid7be2952016-05-04 15:49:37 +03001721 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 }
1723
1724 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001725 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001726 dwc3_ep0_out_start(dwc);
1727
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001728 dwc3_gadget_enable_irq(dwc);
1729
Felipe Balbid7be2952016-05-04 15:49:37 +03001730 return 0;
1731
1732err1:
1733 __dwc3_gadget_ep_disable(dwc->eps[0]);
1734
1735err0:
1736 return ret;
1737}
1738
1739static int dwc3_gadget_start(struct usb_gadget *g,
1740 struct usb_gadget_driver *driver)
1741{
1742 struct dwc3 *dwc = gadget_to_dwc(g);
1743 unsigned long flags;
1744 int ret = 0;
1745 int irq;
1746
1747 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1748 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1749 IRQF_SHARED, "dwc3", dwc->ev_buf);
1750 if (ret) {
1751 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1752 irq, ret);
1753 goto err0;
1754 }
1755
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;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001791 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001792
1793 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03001794 __dwc3_gadget_stop(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001796 spin_unlock_irqrestore(&dwc->lock, flags);
1797
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001798 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001799 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001800
Felipe Balbi72246da2011-08-19 18:10:58 +03001801 return 0;
1802}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001803
Felipe Balbi72246da2011-08-19 18:10:58 +03001804static const struct usb_gadget_ops dwc3_gadget_ops = {
1805 .get_frame = dwc3_gadget_get_frame,
1806 .wakeup = dwc3_gadget_wakeup,
1807 .set_selfpowered = dwc3_gadget_set_selfpowered,
1808 .pullup = dwc3_gadget_pullup,
1809 .udc_start = dwc3_gadget_start,
1810 .udc_stop = dwc3_gadget_stop,
1811};
1812
1813/* -------------------------------------------------------------------------- */
1814
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001815static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1816 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001817{
1818 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001819 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001820
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001821 for (i = 0; i < num; i++) {
1822 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001823
Felipe Balbi72246da2011-08-19 18:10:58 +03001824 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001825 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001826 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001827
1828 dep->dwc = dwc;
1829 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001830 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001831 dwc->eps[epnum] = dep;
1832
1833 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1834 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001835
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001837
Felipe Balbi73815282015-01-27 13:48:14 -06001838 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001839
Felipe Balbi72246da2011-08-19 18:10:58 +03001840 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001841 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301842 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001843 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1844 if (!epnum)
1845 dwc->gadget.ep0 = &dep->endpoint;
1846 } else {
1847 int ret;
1848
Robert Baldygae117e742013-12-13 12:23:38 +01001849 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001850 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001851 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1852 list_add_tail(&dep->endpoint.ep_list,
1853 &dwc->gadget.ep_list);
1854
1855 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001856 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001859
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001860 if (epnum == 0 || epnum == 1) {
1861 dep->endpoint.caps.type_control = true;
1862 } else {
1863 dep->endpoint.caps.type_iso = true;
1864 dep->endpoint.caps.type_bulk = true;
1865 dep->endpoint.caps.type_int = true;
1866 }
1867
1868 dep->endpoint.caps.dir_in = !!direction;
1869 dep->endpoint.caps.dir_out = !direction;
1870
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001871 INIT_LIST_HEAD(&dep->pending_list);
1872 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001873 }
1874
1875 return 0;
1876}
1877
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001878static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1879{
1880 int ret;
1881
1882 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1883
1884 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1885 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001886 dwc3_trace(trace_dwc3_gadget,
1887 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001888 return ret;
1889 }
1890
1891 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1892 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001893 dwc3_trace(trace_dwc3_gadget,
1894 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001895 return ret;
1896 }
1897
1898 return 0;
1899}
1900
Felipe Balbi72246da2011-08-19 18:10:58 +03001901static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1902{
1903 struct dwc3_ep *dep;
1904 u8 epnum;
1905
1906 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1907 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001908 if (!dep)
1909 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301910 /*
1911 * Physical endpoints 0 and 1 are special; they form the
1912 * bi-directional USB endpoint 0.
1913 *
1914 * For those two physical endpoints, we don't allocate a TRB
1915 * pool nor do we add them the endpoints list. Due to that, we
1916 * shouldn't do these two operations otherwise we would end up
1917 * with all sorts of bugs when removing dwc3.ko.
1918 */
1919 if (epnum != 0 && epnum != 1) {
1920 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001921 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301922 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001923
1924 kfree(dep);
1925 }
1926}
1927
Felipe Balbi72246da2011-08-19 18:10:58 +03001928/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001929
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301930static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1931 struct dwc3_request *req, struct dwc3_trb *trb,
1932 const struct dwc3_event_depevt *event, int status)
1933{
1934 unsigned int count;
1935 unsigned int s_pkt = 0;
1936 unsigned int trb_status;
1937
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001938 trace_dwc3_complete_trb(dep, trb);
1939
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301940 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1941 /*
1942 * We continue despite the error. There is not much we
1943 * can do. If we don't clean it up we loop forever. If
1944 * we skip the TRB then it gets overwritten after a
1945 * while since we use them in a ring buffer. A BUG()
1946 * would help. Lets hope that if this occurs, someone
1947 * fixes the root cause instead of looking away :)
1948 */
1949 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1950 dep->name, trb);
1951 count = trb->size & DWC3_TRB_SIZE_MASK;
1952
1953 if (dep->direction) {
1954 if (count) {
1955 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1956 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001957 dwc3_trace(trace_dwc3_gadget,
1958 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301959 dep->name);
1960 /*
1961 * If missed isoc occurred and there is
1962 * no request queued then issue END
1963 * TRANSFER, so that core generates
1964 * next xfernotready and we will issue
1965 * a fresh START TRANSFER.
1966 * If there are still queued request
1967 * then wait, do not issue either END
1968 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001969 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301970 * giveback.If any future queued request
1971 * is successfully transferred then we
1972 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001973 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301974 */
1975 dep->flags |= DWC3_EP_MISSED_ISOC;
1976 } else {
1977 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1978 dep->name);
1979 status = -ECONNRESET;
1980 }
1981 } else {
1982 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1983 }
1984 } else {
1985 if (count && (event->status & DEPEVT_STATUS_SHORT))
1986 s_pkt = 1;
1987 }
1988
1989 /*
1990 * We assume here we will always receive the entire data block
1991 * which we should receive. Meaning, if we program RX to
1992 * receive 4K but we receive only 2K, we assume that's all we
1993 * should receive and we simply bounce the request back to the
1994 * gadget driver for further processing.
1995 */
1996 req->request.actual += req->request.length - count;
1997 if (s_pkt)
1998 return 1;
1999 if ((event->status & DEPEVT_STATUS_LST) &&
2000 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2001 DWC3_TRB_CTRL_HWO)))
2002 return 1;
2003 if ((event->status & DEPEVT_STATUS_IOC) &&
2004 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2005 return 1;
2006 return 0;
2007}
2008
Felipe Balbi72246da2011-08-19 18:10:58 +03002009static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2010 const struct dwc3_event_depevt *event, int status)
2011{
2012 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002013 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302014 unsigned int slot;
2015 unsigned int i;
2016 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002017
2018 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002019 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002020 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03002021 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002022
Ville Syrjäläd115d702015-08-31 19:48:28 +03002023 i = 0;
2024 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03002025 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03002026 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03002027 slot++;
2028 slot %= DWC3_TRB_NUM;
2029 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03002030
Ville Syrjäläd115d702015-08-31 19:48:28 +03002031 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2032 event, status);
2033 if (ret)
2034 break;
2035 } while (++i < req->request.num_mapped_sgs);
2036
2037 dwc3_gadget_giveback(dep, req, status);
2038
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302039 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03002041 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03002042
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302043 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002044 list_empty(&dep->started_list)) {
2045 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302046 /*
2047 * If there is no entry in request list then do
2048 * not issue END TRANSFER now. Just set PENDING
2049 * flag, so that END TRANSFER is issued when an
2050 * entry is added into request list.
2051 */
2052 dep->flags = DWC3_EP_PENDING_REQUEST;
2053 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002054 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302055 dep->flags = DWC3_EP_ENABLED;
2056 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302057 return 1;
2058 }
2059
Felipe Balbi72246da2011-08-19 18:10:58 +03002060 return 1;
2061}
2062
2063static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002064 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002065{
2066 unsigned status = 0;
2067 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002068 u32 is_xfer_complete;
2069
2070 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002071
2072 if (event->status & DEPEVT_STATUS_BUSERR)
2073 status = -ECONNRESET;
2074
Paul Zimmerman1d046792012-02-15 18:56:56 -08002075 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05002076 if (clean_busy && (is_xfer_complete ||
2077 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002078 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002079
2080 /*
2081 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2082 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2083 */
2084 if (dwc->revision < DWC3_REVISION_183A) {
2085 u32 reg;
2086 int i;
2087
2088 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002089 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002090
2091 if (!(dep->flags & DWC3_EP_ENABLED))
2092 continue;
2093
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002094 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002095 return;
2096 }
2097
2098 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2099 reg |= dwc->u1u2;
2100 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2101
2102 dwc->u1u2 = 0;
2103 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002104
Felipe Balbie6e709b2015-09-28 15:16:56 -05002105 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002106 int ret;
2107
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002108 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002109 if (!ret || ret == -EBUSY)
2110 return;
2111 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002112}
2113
Felipe Balbi72246da2011-08-19 18:10:58 +03002114static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2115 const struct dwc3_event_depevt *event)
2116{
2117 struct dwc3_ep *dep;
2118 u8 epnum = event->endpoint_number;
2119
2120 dep = dwc->eps[epnum];
2121
Felipe Balbi3336abb2012-06-06 09:19:35 +03002122 if (!(dep->flags & DWC3_EP_ENABLED))
2123 return;
2124
Felipe Balbi72246da2011-08-19 18:10:58 +03002125 if (epnum == 0 || epnum == 1) {
2126 dwc3_ep0_interrupt(dwc, event);
2127 return;
2128 }
2129
2130 switch (event->endpoint_event) {
2131 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002132 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002133
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002134 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002135 dwc3_trace(trace_dwc3_gadget,
2136 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002137 dep->name);
2138 return;
2139 }
2140
Jingoo Han029d97f2014-07-04 15:00:51 +09002141 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002142 break;
2143 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002144 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002145 break;
2146 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002147 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002148 dwc3_gadget_start_isoc(dwc, dep, event);
2149 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002150 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002151 int ret;
2152
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002153 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2154
Felipe Balbi73815282015-01-27 13:48:14 -06002155 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002156 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 : "Transfer Not Active");
2158
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002159 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 if (!ret || ret == -EBUSY)
2161 return;
2162
Felipe Balbiec5e7952015-11-16 16:04:13 -06002163 dwc3_trace(trace_dwc3_gadget,
2164 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002165 dep->name);
2166 }
2167
2168 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002169 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002170 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002171 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2172 dep->name);
2173 return;
2174 }
2175
2176 switch (event->status) {
2177 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002178 dwc3_trace(trace_dwc3_gadget,
2179 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002180 event->parameters);
2181
2182 break;
2183 case DEPEVT_STREAMEVT_NOTFOUND:
2184 /* FALLTHROUGH */
2185 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002186 dwc3_trace(trace_dwc3_gadget,
2187 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002188 }
2189 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002190 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002191 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002192 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002194 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 break;
2196 }
2197}
2198
2199static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2200{
2201 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2202 spin_unlock(&dwc->lock);
2203 dwc->gadget_driver->disconnect(&dwc->gadget);
2204 spin_lock(&dwc->lock);
2205 }
2206}
2207
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002208static void dwc3_suspend_gadget(struct dwc3 *dwc)
2209{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002210 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002211 spin_unlock(&dwc->lock);
2212 dwc->gadget_driver->suspend(&dwc->gadget);
2213 spin_lock(&dwc->lock);
2214 }
2215}
2216
2217static void dwc3_resume_gadget(struct dwc3 *dwc)
2218{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002219 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002220 spin_unlock(&dwc->lock);
2221 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002222 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002223 }
2224}
2225
2226static void dwc3_reset_gadget(struct dwc3 *dwc)
2227{
2228 if (!dwc->gadget_driver)
2229 return;
2230
2231 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2232 spin_unlock(&dwc->lock);
2233 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002234 spin_lock(&dwc->lock);
2235 }
2236}
2237
Paul Zimmermanb992e682012-04-27 14:17:35 +03002238static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002239{
2240 struct dwc3_ep *dep;
2241 struct dwc3_gadget_ep_cmd_params params;
2242 u32 cmd;
2243 int ret;
2244
2245 dep = dwc->eps[epnum];
2246
Felipe Balbib4996a82012-06-06 12:04:13 +03002247 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302248 return;
2249
Pratyush Anand57911502012-07-06 15:19:10 +05302250 /*
2251 * NOTICE: We are violating what the Databook says about the
2252 * EndTransfer command. Ideally we would _always_ wait for the
2253 * EndTransfer Command Completion IRQ, but that's causing too
2254 * much trouble synchronizing between us and gadget driver.
2255 *
2256 * We have discussed this with the IP Provider and it was
2257 * suggested to giveback all requests here, but give HW some
2258 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002259 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302260 *
2261 * Note also that a similar handling was tested by Synopsys
2262 * (thanks a lot Paul) and nothing bad has come out of it.
2263 * In short, what we're doing is:
2264 *
2265 * - Issue EndTransfer WITH CMDIOC bit set
2266 * - Wait 100us
2267 */
2268
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302269 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002270 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2271 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002272 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302273 memset(&params, 0, sizeof(params));
2274 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2275 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002276 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002277 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302278 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002279}
2280
2281static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2282{
2283 u32 epnum;
2284
2285 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2286 struct dwc3_ep *dep;
2287
2288 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002289 if (!dep)
2290 continue;
2291
Felipe Balbi72246da2011-08-19 18:10:58 +03002292 if (!(dep->flags & DWC3_EP_ENABLED))
2293 continue;
2294
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002295 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002296 }
2297}
2298
2299static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2300{
2301 u32 epnum;
2302
2303 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2304 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002305 int ret;
2306
2307 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002308 if (!dep)
2309 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002310
2311 if (!(dep->flags & DWC3_EP_STALL))
2312 continue;
2313
2314 dep->flags &= ~DWC3_EP_STALL;
2315
John Youn50c763f2016-05-31 17:49:56 -07002316 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002317 WARN_ON_ONCE(ret);
2318 }
2319}
2320
2321static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2322{
Felipe Balbic4430a22012-05-24 10:30:01 +03002323 int reg;
2324
Felipe Balbi72246da2011-08-19 18:10:58 +03002325 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2326 reg &= ~DWC3_DCTL_INITU1ENA;
2327 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2328
2329 reg &= ~DWC3_DCTL_INITU2ENA;
2330 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002331
Felipe Balbi72246da2011-08-19 18:10:58 +03002332 dwc3_disconnect_gadget(dwc);
2333
2334 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002335 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002336 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002337}
2338
Felipe Balbi72246da2011-08-19 18:10:58 +03002339static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2340{
2341 u32 reg;
2342
Felipe Balbidf62df52011-10-14 15:11:49 +03002343 /*
2344 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2345 * would cause a missing Disconnect Event if there's a
2346 * pending Setup Packet in the FIFO.
2347 *
2348 * There's no suggested workaround on the official Bug
2349 * report, which states that "unless the driver/application
2350 * is doing any special handling of a disconnect event,
2351 * there is no functional issue".
2352 *
2353 * Unfortunately, it turns out that we _do_ some special
2354 * handling of a disconnect event, namely complete all
2355 * pending transfers, notify gadget driver of the
2356 * disconnection, and so on.
2357 *
2358 * Our suggested workaround is to follow the Disconnect
2359 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002360 * flag. Such flag gets set whenever we have a SETUP_PENDING
2361 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002362 * same endpoint.
2363 *
2364 * Refers to:
2365 *
2366 * STAR#9000466709: RTL: Device : Disconnect event not
2367 * generated if setup packet pending in FIFO
2368 */
2369 if (dwc->revision < DWC3_REVISION_188A) {
2370 if (dwc->setup_packet_pending)
2371 dwc3_gadget_disconnect_interrupt(dwc);
2372 }
2373
Felipe Balbi8e744752014-11-06 14:27:53 +08002374 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002375
2376 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2377 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2378 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002379 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002380
2381 dwc3_stop_active_transfers(dwc);
2382 dwc3_clear_stall_all_ep(dwc);
2383
2384 /* Reset device address to zero */
2385 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2386 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2387 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002388}
2389
2390static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2391{
2392 u32 reg;
2393 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2394
2395 /*
2396 * We change the clock only at SS but I dunno why I would want to do
2397 * this. Maybe it becomes part of the power saving plan.
2398 */
2399
John Younee5cd412016-02-05 17:08:45 -08002400 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2401 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002402 return;
2403
2404 /*
2405 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2406 * each time on Connect Done.
2407 */
2408 if (!usb30_clock)
2409 return;
2410
2411 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2412 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2413 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2414}
2415
Felipe Balbi72246da2011-08-19 18:10:58 +03002416static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2417{
Felipe Balbi72246da2011-08-19 18:10:58 +03002418 struct dwc3_ep *dep;
2419 int ret;
2420 u32 reg;
2421 u8 speed;
2422
Felipe Balbi72246da2011-08-19 18:10:58 +03002423 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2424 speed = reg & DWC3_DSTS_CONNECTSPD;
2425 dwc->speed = speed;
2426
2427 dwc3_update_ram_clk_sel(dwc, speed);
2428
2429 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002430 case DWC3_DCFG_SUPERSPEED_PLUS:
2431 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2432 dwc->gadget.ep0->maxpacket = 512;
2433 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2434 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002436 /*
2437 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2438 * would cause a missing USB3 Reset event.
2439 *
2440 * In such situations, we should force a USB3 Reset
2441 * event by calling our dwc3_gadget_reset_interrupt()
2442 * routine.
2443 *
2444 * Refers to:
2445 *
2446 * STAR#9000483510: RTL: SS : USB3 reset event may
2447 * not be generated always when the link enters poll
2448 */
2449 if (dwc->revision < DWC3_REVISION_190A)
2450 dwc3_gadget_reset_interrupt(dwc);
2451
Felipe Balbi72246da2011-08-19 18:10:58 +03002452 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2453 dwc->gadget.ep0->maxpacket = 512;
2454 dwc->gadget.speed = USB_SPEED_SUPER;
2455 break;
2456 case DWC3_DCFG_HIGHSPEED:
2457 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2458 dwc->gadget.ep0->maxpacket = 64;
2459 dwc->gadget.speed = USB_SPEED_HIGH;
2460 break;
2461 case DWC3_DCFG_FULLSPEED2:
2462 case DWC3_DCFG_FULLSPEED1:
2463 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2464 dwc->gadget.ep0->maxpacket = 64;
2465 dwc->gadget.speed = USB_SPEED_FULL;
2466 break;
2467 case DWC3_DCFG_LOWSPEED:
2468 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2469 dwc->gadget.ep0->maxpacket = 8;
2470 dwc->gadget.speed = USB_SPEED_LOW;
2471 break;
2472 }
2473
Pratyush Anand2b758352013-01-14 15:59:31 +05302474 /* Enable USB2 LPM Capability */
2475
John Younee5cd412016-02-05 17:08:45 -08002476 if ((dwc->revision > DWC3_REVISION_194A) &&
2477 (speed != DWC3_DCFG_SUPERSPEED) &&
2478 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302479 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2480 reg |= DWC3_DCFG_LPM_CAP;
2481 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2482
2483 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2484 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2485
Huang Rui460d0982014-10-31 11:11:18 +08002486 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302487
Huang Rui80caf7d2014-10-28 19:54:26 +08002488 /*
2489 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2490 * DCFG.LPMCap is set, core responses with an ACK and the
2491 * BESL value in the LPM token is less than or equal to LPM
2492 * NYET threshold.
2493 */
2494 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2495 && dwc->has_lpm_erratum,
2496 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2497
2498 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2499 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2500
Pratyush Anand2b758352013-01-14 15:59:31 +05302501 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002502 } else {
2503 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2504 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2505 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302506 }
2507
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002509 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2510 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002511 if (ret) {
2512 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2513 return;
2514 }
2515
2516 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002517 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2518 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002519 if (ret) {
2520 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2521 return;
2522 }
2523
2524 /*
2525 * Configure PHY via GUSB3PIPECTLn if required.
2526 *
2527 * Update GTXFIFOSIZn
2528 *
2529 * In both cases reset values should be sufficient.
2530 */
2531}
2532
2533static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2534{
Felipe Balbi72246da2011-08-19 18:10:58 +03002535 /*
2536 * TODO take core out of low power mode when that's
2537 * implemented.
2538 */
2539
Jiebing Liad14d4e2014-12-11 13:26:29 +08002540 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2541 spin_unlock(&dwc->lock);
2542 dwc->gadget_driver->resume(&dwc->gadget);
2543 spin_lock(&dwc->lock);
2544 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002545}
2546
2547static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2548 unsigned int evtinfo)
2549{
Felipe Balbifae2b902011-10-14 13:00:30 +03002550 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002551 unsigned int pwropt;
2552
2553 /*
2554 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2555 * Hibernation mode enabled which would show up when device detects
2556 * host-initiated U3 exit.
2557 *
2558 * In that case, device will generate a Link State Change Interrupt
2559 * from U3 to RESUME which is only necessary if Hibernation is
2560 * configured in.
2561 *
2562 * There are no functional changes due to such spurious event and we
2563 * just need to ignore it.
2564 *
2565 * Refers to:
2566 *
2567 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2568 * operational mode
2569 */
2570 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2571 if ((dwc->revision < DWC3_REVISION_250A) &&
2572 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2573 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2574 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002575 dwc3_trace(trace_dwc3_gadget,
2576 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002577 return;
2578 }
2579 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002580
2581 /*
2582 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2583 * on the link partner, the USB session might do multiple entry/exit
2584 * of low power states before a transfer takes place.
2585 *
2586 * Due to this problem, we might experience lower throughput. The
2587 * suggested workaround is to disable DCTL[12:9] bits if we're
2588 * transitioning from U1/U2 to U0 and enable those bits again
2589 * after a transfer completes and there are no pending transfers
2590 * on any of the enabled endpoints.
2591 *
2592 * This is the first half of that workaround.
2593 *
2594 * Refers to:
2595 *
2596 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2597 * core send LGO_Ux entering U0
2598 */
2599 if (dwc->revision < DWC3_REVISION_183A) {
2600 if (next == DWC3_LINK_STATE_U0) {
2601 u32 u1u2;
2602 u32 reg;
2603
2604 switch (dwc->link_state) {
2605 case DWC3_LINK_STATE_U1:
2606 case DWC3_LINK_STATE_U2:
2607 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2608 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2609 | DWC3_DCTL_ACCEPTU2ENA
2610 | DWC3_DCTL_INITU1ENA
2611 | DWC3_DCTL_ACCEPTU1ENA);
2612
2613 if (!dwc->u1u2)
2614 dwc->u1u2 = reg & u1u2;
2615
2616 reg &= ~u1u2;
2617
2618 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2619 break;
2620 default:
2621 /* do nothing */
2622 break;
2623 }
2624 }
2625 }
2626
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002627 switch (next) {
2628 case DWC3_LINK_STATE_U1:
2629 if (dwc->speed == USB_SPEED_SUPER)
2630 dwc3_suspend_gadget(dwc);
2631 break;
2632 case DWC3_LINK_STATE_U2:
2633 case DWC3_LINK_STATE_U3:
2634 dwc3_suspend_gadget(dwc);
2635 break;
2636 case DWC3_LINK_STATE_RESUME:
2637 dwc3_resume_gadget(dwc);
2638 break;
2639 default:
2640 /* do nothing */
2641 break;
2642 }
2643
Felipe Balbie57ebc12014-04-22 13:20:12 -05002644 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002645}
2646
Felipe Balbie1dadd32014-02-25 14:47:54 -06002647static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2648 unsigned int evtinfo)
2649{
2650 unsigned int is_ss = evtinfo & BIT(4);
2651
2652 /**
2653 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2654 * have a known issue which can cause USB CV TD.9.23 to fail
2655 * randomly.
2656 *
2657 * Because of this issue, core could generate bogus hibernation
2658 * events which SW needs to ignore.
2659 *
2660 * Refers to:
2661 *
2662 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2663 * Device Fallback from SuperSpeed
2664 */
2665 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2666 return;
2667
2668 /* enter hibernation here */
2669}
2670
Felipe Balbi72246da2011-08-19 18:10:58 +03002671static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2672 const struct dwc3_event_devt *event)
2673{
2674 switch (event->type) {
2675 case DWC3_DEVICE_EVENT_DISCONNECT:
2676 dwc3_gadget_disconnect_interrupt(dwc);
2677 break;
2678 case DWC3_DEVICE_EVENT_RESET:
2679 dwc3_gadget_reset_interrupt(dwc);
2680 break;
2681 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2682 dwc3_gadget_conndone_interrupt(dwc);
2683 break;
2684 case DWC3_DEVICE_EVENT_WAKEUP:
2685 dwc3_gadget_wakeup_interrupt(dwc);
2686 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002687 case DWC3_DEVICE_EVENT_HIBER_REQ:
2688 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2689 "unexpected hibernation event\n"))
2690 break;
2691
2692 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2693 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002694 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2695 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2696 break;
2697 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002698 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002699 break;
2700 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002701 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002702 break;
2703 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002704 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 break;
2706 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002707 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002708 break;
2709 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002710 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002711 break;
2712 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06002713 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002714 }
2715}
2716
2717static void dwc3_process_event_entry(struct dwc3 *dwc,
2718 const union dwc3_event *event)
2719{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002720 trace_dwc3_event(event->raw);
2721
Felipe Balbi72246da2011-08-19 18:10:58 +03002722 /* Endpoint IRQ, handle it and return early */
2723 if (event->type.is_devspec == 0) {
2724 /* depevt */
2725 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2726 }
2727
2728 switch (event->type.type) {
2729 case DWC3_EVENT_TYPE_DEV:
2730 dwc3_gadget_interrupt(dwc, &event->devt);
2731 break;
2732 /* REVISIT what to do with Carkit and I2C events ? */
2733 default:
2734 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2735 }
2736}
2737
Felipe Balbidea520a2016-03-30 09:39:34 +03002738static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002739{
Felipe Balbidea520a2016-03-30 09:39:34 +03002740 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002741 irqreturn_t ret = IRQ_NONE;
2742 int left;
2743 u32 reg;
2744
Felipe Balbif42f2442013-06-12 21:25:08 +03002745 left = evt->count;
2746
2747 if (!(evt->flags & DWC3_EVENT_PENDING))
2748 return IRQ_NONE;
2749
2750 while (left > 0) {
2751 union dwc3_event event;
2752
2753 event.raw = *(u32 *) (evt->buf + evt->lpos);
2754
2755 dwc3_process_event_entry(dwc, &event);
2756
2757 /*
2758 * FIXME we wrap around correctly to the next entry as
2759 * almost all entries are 4 bytes in size. There is one
2760 * entry which has 12 bytes which is a regular entry
2761 * followed by 8 bytes data. ATM I don't know how
2762 * things are organized if we get next to the a
2763 * boundary so I worry about that once we try to handle
2764 * that.
2765 */
2766 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2767 left -= 4;
2768
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002769 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002770 }
2771
2772 evt->count = 0;
2773 evt->flags &= ~DWC3_EVENT_PENDING;
2774 ret = IRQ_HANDLED;
2775
2776 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002777 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002778 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002779 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002780
2781 return ret;
2782}
2783
Felipe Balbidea520a2016-03-30 09:39:34 +03002784static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002785{
Felipe Balbidea520a2016-03-30 09:39:34 +03002786 struct dwc3_event_buffer *evt = _evt;
2787 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05002788 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002789 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002790
Felipe Balbie5f68b42015-10-12 13:25:44 -05002791 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002792 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b42015-10-12 13:25:44 -05002793 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002794
2795 return ret;
2796}
2797
Felipe Balbidea520a2016-03-30 09:39:34 +03002798static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002799{
Felipe Balbidea520a2016-03-30 09:39:34 +03002800 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002801 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002802 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002803
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002804 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002805 count &= DWC3_GEVNTCOUNT_MASK;
2806 if (!count)
2807 return IRQ_NONE;
2808
Felipe Balbib15a7622011-06-30 16:57:15 +03002809 evt->count = count;
2810 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002811
Felipe Balbie8adfc32013-06-12 21:11:14 +03002812 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002813 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002814 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002815 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002816
Felipe Balbib15a7622011-06-30 16:57:15 +03002817 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002818}
2819
Felipe Balbidea520a2016-03-30 09:39:34 +03002820static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002821{
Felipe Balbidea520a2016-03-30 09:39:34 +03002822 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002823
Felipe Balbidea520a2016-03-30 09:39:34 +03002824 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002825}
2826
2827/**
2828 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002829 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002830 *
2831 * Returns 0 on success otherwise negative errno.
2832 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002833int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002834{
Felipe Balbi72246da2011-08-19 18:10:58 +03002835 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002836
2837 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2838 &dwc->ctrl_req_addr, GFP_KERNEL);
2839 if (!dwc->ctrl_req) {
2840 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2841 ret = -ENOMEM;
2842 goto err0;
2843 }
2844
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302845 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002846 &dwc->ep0_trb_addr, GFP_KERNEL);
2847 if (!dwc->ep0_trb) {
2848 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2849 ret = -ENOMEM;
2850 goto err1;
2851 }
2852
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002853 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002855 ret = -ENOMEM;
2856 goto err2;
2857 }
2858
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002859 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002860 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2861 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002862 if (!dwc->ep0_bounce) {
2863 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2864 ret = -ENOMEM;
2865 goto err3;
2866 }
2867
Felipe Balbi04c03d12015-12-02 10:06:45 -06002868 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2869 if (!dwc->zlp_buf) {
2870 ret = -ENOMEM;
2871 goto err4;
2872 }
2873
Felipe Balbi72246da2011-08-19 18:10:58 +03002874 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002875 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002876 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002877 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002878 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002879
2880 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002881 * FIXME We might be setting max_speed to <SUPER, however versions
2882 * <2.20a of dwc3 have an issue with metastability (documented
2883 * elsewhere in this driver) which tells us we can't set max speed to
2884 * anything lower than SUPER.
2885 *
2886 * Because gadget.max_speed is only used by composite.c and function
2887 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2888 * to happen so we avoid sending SuperSpeed Capability descriptor
2889 * together with our BOS descriptor as that could confuse host into
2890 * thinking we can handle super speed.
2891 *
2892 * Note that, in fact, we won't even support GetBOS requests when speed
2893 * is less than super speed because we don't have means, yet, to tell
2894 * composite.c that we are USB 2.0 + LPM ECN.
2895 */
2896 if (dwc->revision < DWC3_REVISION_220A)
2897 dwc3_trace(trace_dwc3_gadget,
2898 "Changing max_speed on rev %08x\n",
2899 dwc->revision);
2900
2901 dwc->gadget.max_speed = dwc->maximum_speed;
2902
2903 /*
David Cohena4b9d942013-12-09 15:55:38 -08002904 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2905 * on ep out.
2906 */
2907 dwc->gadget.quirk_ep_out_aligned_size = true;
2908
2909 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002910 * REVISIT: Here we should clear all pending IRQs to be
2911 * sure we're starting from a well known location.
2912 */
2913
2914 ret = dwc3_gadget_init_endpoints(dwc);
2915 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002916 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002917
Felipe Balbi72246da2011-08-19 18:10:58 +03002918 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2919 if (ret) {
2920 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002921 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002922 }
2923
2924 return 0;
2925
Felipe Balbi04c03d12015-12-02 10:06:45 -06002926err5:
2927 kfree(dwc->zlp_buf);
2928
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002929err4:
David Cohene1f80462013-09-11 17:42:47 -07002930 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002931 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2932 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002933
Felipe Balbi72246da2011-08-19 18:10:58 +03002934err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002935 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002936
2937err2:
2938 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2939 dwc->ep0_trb, dwc->ep0_trb_addr);
2940
2941err1:
2942 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2943 dwc->ctrl_req, dwc->ctrl_req_addr);
2944
2945err0:
2946 return ret;
2947}
2948
Felipe Balbi7415f172012-04-30 14:56:33 +03002949/* -------------------------------------------------------------------------- */
2950
Felipe Balbi72246da2011-08-19 18:10:58 +03002951void dwc3_gadget_exit(struct dwc3 *dwc)
2952{
Felipe Balbi72246da2011-08-19 18:10:58 +03002953 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002954
Felipe Balbi72246da2011-08-19 18:10:58 +03002955 dwc3_gadget_free_endpoints(dwc);
2956
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002957 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2958 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002959
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002960 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002961 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002962
2963 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2964 dwc->ep0_trb, dwc->ep0_trb_addr);
2965
2966 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2967 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002968}
Felipe Balbi7415f172012-04-30 14:56:33 +03002969
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002970int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002971{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002972 int ret;
2973
Roger Quadros9772b472016-04-12 11:33:29 +03002974 if (!dwc->gadget_driver)
2975 return 0;
2976
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002977 ret = dwc3_gadget_run_stop(dwc, false, false);
2978 if (ret < 0)
2979 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03002980
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002981 dwc3_disconnect_gadget(dwc);
2982 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03002983
2984 return 0;
2985}
2986
2987int dwc3_gadget_resume(struct dwc3 *dwc)
2988{
Felipe Balbi7415f172012-04-30 14:56:33 +03002989 int ret;
2990
Roger Quadros9772b472016-04-12 11:33:29 +03002991 if (!dwc->gadget_driver)
2992 return 0;
2993
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002994 ret = __dwc3_gadget_start(dwc);
2995 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03002996 goto err0;
2997
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03002998 ret = dwc3_gadget_run_stop(dwc, true, false);
2999 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003000 goto err1;
3001
Felipe Balbi7415f172012-04-30 14:56:33 +03003002 return 0;
3003
3004err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003005 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003006
3007err0:
3008 return ret;
3009}