blob: c068a8f21f374decaec16453a15ae2c7ca0990ff [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)
149{
150 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}
153
154static 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}
159
160static 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 Balbief966b92016-04-05 13:09:51 +0300163}
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;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300247
248 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)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600290 dwc3_trace(trace_dwc3_gadget,
291 "Command Complete --> %d",
Felipe Balbi164f6e12011-08-27 20:29:58 +0300292 DWC3_DEPCMD_STATUS(reg));
Subbaraya Sundeep Bhatta76e838c2015-05-21 15:46:48 +0530293 if (DWC3_DEPCMD_STATUS(reg))
Felipe Balbic0ca3242016-04-04 09:11:51 +0300294 break;
295 ret = 0;
296 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300297 }
298
299 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300300 * We can't sleep here, because it is also called from
301 * interrupt context.
302 */
303 timeout--;
Felipe Balbi73815282015-01-27 13:48:14 -0600304 if (!timeout) {
305 dwc3_trace(trace_dwc3_gadget,
306 "Command Timed Out");
Felipe Balbic0ca3242016-04-04 09:11:51 +0300307 ret = -ETIMEDOUT;
308 break;
Felipe Balbi73815282015-01-27 13:48:14 -0600309 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300310
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200311 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300312 } while (1);
Felipe Balbic0ca3242016-04-04 09:11:51 +0300313
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300314 if (unlikely(susphy)) {
315 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
316 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
317 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
318 }
319
Felipe Balbic0ca3242016-04-04 09:11:51 +0300320 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300321}
322
323static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200324 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300325{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300326 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
328 return dep->trb_pool_dma + offset;
329}
330
331static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
332{
333 struct dwc3 *dwc = dep->dwc;
334
335 if (dep->trb_pool)
336 return 0;
337
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 dep->trb_pool = dma_alloc_coherent(dwc->dev,
339 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
340 &dep->trb_pool_dma, GFP_KERNEL);
341 if (!dep->trb_pool) {
342 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
343 dep->name);
344 return -ENOMEM;
345 }
346
347 return 0;
348}
349
350static void dwc3_free_trb_pool(struct dwc3_ep *dep)
351{
352 struct dwc3 *dwc = dep->dwc;
353
354 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
355 dep->trb_pool, dep->trb_pool_dma);
356
357 dep->trb_pool = NULL;
358 dep->trb_pool_dma = 0;
359}
360
John Younc4509602016-02-16 20:10:53 -0800361static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
362
363/**
364 * dwc3_gadget_start_config - Configure EP resources
365 * @dwc: pointer to our controller context structure
366 * @dep: endpoint that is being enabled
367 *
368 * The assignment of transfer resources cannot perfectly follow the
369 * data book due to the fact that the controller driver does not have
370 * all knowledge of the configuration in advance. It is given this
371 * information piecemeal by the composite gadget framework after every
372 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
373 * programming model in this scenario can cause errors. For two
374 * reasons:
375 *
376 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
377 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
378 * multiple interfaces.
379 *
380 * 2) The databook does not mention doing more DEPXFERCFG for new
381 * endpoint on alt setting (8.1.6).
382 *
383 * The following simplified method is used instead:
384 *
385 * All hardware endpoints can be assigned a transfer resource and this
386 * setting will stay persistent until either a core reset or
387 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
388 * do DEPXFERCFG for every hardware endpoint as well. We are
389 * guaranteed that there are as many transfer resources as endpoints.
390 *
391 * This function is called for each endpoint when it is being enabled
392 * but is triggered only when called for EP0-out, which always happens
393 * first, and which should only happen in one of the above conditions.
394 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300395static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
396{
397 struct dwc3_gadget_ep_cmd_params params;
398 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800399 int i;
400 int ret;
401
402 if (dep->number)
403 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300404
405 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800406 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407
John Younc4509602016-02-16 20:10:53 -0800408 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
409 if (ret)
410 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
John Younc4509602016-02-16 20:10:53 -0800412 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
413 struct dwc3_ep *dep = dwc->eps[i];
414
415 if (!dep)
416 continue;
417
418 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
419 if (ret)
420 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 }
422
423 return 0;
424}
425
426static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200427 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300428 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600429 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300430{
431 struct dwc3_gadget_ep_cmd_params params;
432
433 memset(&params, 0x00, sizeof(params));
434
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300435 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900436 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
437
438 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800439 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Chanho Parkd2e9a132012-08-31 16:54:07 +0900440 u32 burst = dep->endpoint.maxburst - 1;
441
442 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
443 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300444
Felipe Balbi4b345c92012-07-16 14:08:16 +0300445 if (ignore)
446 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
447
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600448 if (restore) {
449 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
450 params.param2 |= dep->saved_state;
451 }
452
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300453 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
454 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200456 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300457 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
458 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300459 dep->stream_capable = true;
460 }
461
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500462 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300463 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300464
465 /*
466 * We are doing 1:1 mapping for endpoints, meaning
467 * Physical Endpoints 2 maps to Logical Endpoint 2 and
468 * so on. We consider the direction bit as part of the physical
469 * endpoint number. So USB endpoint 0x81 is 0x03.
470 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300471 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300472
473 /*
474 * We must use the lower 16 TX FIFOs even though
475 * HW might have more
476 */
477 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300478 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300479
480 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300481 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300482 dep->interval = 1 << (desc->bInterval - 1);
483 }
484
485 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
486 DWC3_DEPCMD_SETEPCONFIG, &params);
487}
488
489static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
490{
491 struct dwc3_gadget_ep_cmd_params params;
492
493 memset(&params, 0x00, sizeof(params));
494
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300495 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300496
497 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
498 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
499}
500
501/**
502 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
503 * @dep: endpoint to be initialized
504 * @desc: USB Endpoint Descriptor
505 *
506 * Caller should take care of locking
507 */
508static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200509 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300510 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600511 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300512{
513 struct dwc3 *dwc = dep->dwc;
514 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300515 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300516
Felipe Balbi73815282015-01-27 13:48:14 -0600517 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300518
Felipe Balbi72246da2011-08-19 18:10:58 +0300519 if (!(dep->flags & DWC3_EP_ENABLED)) {
520 ret = dwc3_gadget_start_config(dwc, dep);
521 if (ret)
522 return ret;
523 }
524
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600525 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
526 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 if (ret)
528 return ret;
529
530 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200531 struct dwc3_trb *trb_st_hw;
532 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200534 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200535 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300536 dep->type = usb_endpoint_type(desc);
537 dep->flags |= DWC3_EP_ENABLED;
538
539 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
540 reg |= DWC3_DALEPENA_EP(dep->number);
541 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
542
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300543 if (usb_endpoint_xfer_control(desc))
Felipe Balbie901aa12016-03-16 14:01:37 +0200544 goto out;
Felipe Balbi72246da2011-08-19 18:10:58 +0300545
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300546 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 trb_st_hw = &dep->trb_pool[0];
548
Felipe Balbif6bafc62012-02-06 11:04:53 +0200549 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Jack Pham1200a822014-10-21 16:31:10 -0700550 memset(trb_link, 0, sizeof(*trb_link));
Felipe Balbi72246da2011-08-19 18:10:58 +0300551
Felipe Balbif6bafc62012-02-06 11:04:53 +0200552 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
553 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
554 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
555 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 }
557
Felipe Balbie901aa12016-03-16 14:01:37 +0200558out:
Felipe Balbiaa739972015-07-20 14:48:13 -0500559 switch (usb_endpoint_type(desc)) {
560 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbie901aa12016-03-16 14:01:37 +0200561 /* don't change name */
Felipe Balbiaa739972015-07-20 14:48:13 -0500562 break;
563 case USB_ENDPOINT_XFER_ISOC:
564 strlcat(dep->name, "-isoc", sizeof(dep->name));
565 break;
566 case USB_ENDPOINT_XFER_BULK:
567 strlcat(dep->name, "-bulk", sizeof(dep->name));
568 break;
569 case USB_ENDPOINT_XFER_INT:
570 strlcat(dep->name, "-int", sizeof(dep->name));
571 break;
572 default:
573 dev_err(dwc->dev, "invalid endpoint transfer type\n");
574 }
575
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 return 0;
577}
578
Paul Zimmermanb992e682012-04-27 14:17:35 +0300579static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200580static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300581{
582 struct dwc3_request *req;
583
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200584 if (!list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300585 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200586
Pratyush Anand57911502012-07-06 15:19:10 +0530587 /* - giveback all requests to gadget driver */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200588 while (!list_empty(&dep->started_list)) {
589 req = next_request(&dep->started_list);
Pratyush Anand15916332012-06-15 11:54:36 +0530590
591 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
592 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200593 }
594
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200595 while (!list_empty(&dep->pending_list)) {
596 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300597
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200598 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300600}
601
602/**
603 * __dwc3_gadget_ep_disable - Disables a HW endpoint
604 * @dep: the endpoint to disable
605 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200606 * This function also removes requests which are currently processed ny the
607 * hardware and those which are not yet scheduled.
608 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300610static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
611{
612 struct dwc3 *dwc = dep->dwc;
613 u32 reg;
614
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500615 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
616
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200617 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300618
Felipe Balbi687ef982014-04-16 10:30:33 -0500619 /* make sure HW endpoint isn't stalled */
620 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500621 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500622
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
624 reg &= ~DWC3_DALEPENA_EP(dep->number);
625 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
626
Felipe Balbi879631a2011-09-30 10:58:47 +0300627 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200628 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200629 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300630 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300631 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300632
Felipe Balbiaa739972015-07-20 14:48:13 -0500633 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
634 dep->number >> 1,
635 (dep->number & 1) ? "in" : "out");
636
Felipe Balbi72246da2011-08-19 18:10:58 +0300637 return 0;
638}
639
640/* -------------------------------------------------------------------------- */
641
642static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
643 const struct usb_endpoint_descriptor *desc)
644{
645 return -EINVAL;
646}
647
648static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
649{
650 return -EINVAL;
651}
652
653/* -------------------------------------------------------------------------- */
654
655static int dwc3_gadget_ep_enable(struct usb_ep *ep,
656 const struct usb_endpoint_descriptor *desc)
657{
658 struct dwc3_ep *dep;
659 struct dwc3 *dwc;
660 unsigned long flags;
661 int ret;
662
663 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
664 pr_debug("dwc3: invalid parameters\n");
665 return -EINVAL;
666 }
667
668 if (!desc->wMaxPacketSize) {
669 pr_debug("dwc3: missing wMaxPacketSize\n");
670 return -EINVAL;
671 }
672
673 dep = to_dwc3_ep(ep);
674 dwc = dep->dwc;
675
Felipe Balbi95ca9612015-12-10 13:08:20 -0600676 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
677 "%s is already enabled\n",
678 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300679 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300680
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600682 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 spin_unlock_irqrestore(&dwc->lock, flags);
684
685 return ret;
686}
687
688static int dwc3_gadget_ep_disable(struct usb_ep *ep)
689{
690 struct dwc3_ep *dep;
691 struct dwc3 *dwc;
692 unsigned long flags;
693 int ret;
694
695 if (!ep) {
696 pr_debug("dwc3: invalid parameters\n");
697 return -EINVAL;
698 }
699
700 dep = to_dwc3_ep(ep);
701 dwc = dep->dwc;
702
Felipe Balbi95ca9612015-12-10 13:08:20 -0600703 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
704 "%s is already disabled\n",
705 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300706 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300707
Felipe Balbi72246da2011-08-19 18:10:58 +0300708 spin_lock_irqsave(&dwc->lock, flags);
709 ret = __dwc3_gadget_ep_disable(dep);
710 spin_unlock_irqrestore(&dwc->lock, flags);
711
712 return ret;
713}
714
715static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
716 gfp_t gfp_flags)
717{
718 struct dwc3_request *req;
719 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300720
721 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900722 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300724
725 req->epnum = dep->number;
726 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300727
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500728 trace_dwc3_alloc_request(req);
729
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 return &req->request;
731}
732
733static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
734 struct usb_request *request)
735{
736 struct dwc3_request *req = to_dwc3_request(request);
737
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500738 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300739 kfree(req);
740}
741
Felipe Balbic71fc372011-11-22 11:37:34 +0200742/**
743 * dwc3_prepare_one_trb - setup one TRB from one request
744 * @dep: endpoint for which this request is prepared
745 * @req: dwc3_request pointer
746 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200747static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200748 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530749 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200750{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200751 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200752
Felipe Balbi73815282015-01-27 13:48:14 -0600753 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200754 dep->name, req, (unsigned long long) dma,
755 length, last ? " last" : "",
756 chain ? " chain" : "");
757
Pratyush Anand915e2022013-01-14 15:59:35 +0530758
Felipe Balbi4faf7552016-04-05 13:14:31 +0300759 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200760
Felipe Balbieeb720f2011-11-28 12:46:59 +0200761 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200762 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200763 req->trb = trb;
764 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300765 req->first_trb_index = dep->trb_enqueue;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200766 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200767
Felipe Balbief966b92016-04-05 13:09:51 +0300768 dwc3_ep_inc_enq(dep);
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300769 /* Skip the LINK-TRB */
770 if (dwc3_ep_is_last_trb(dep->trb_enqueue))
Felipe Balbief966b92016-04-05 13:09:51 +0300771 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530772
Felipe Balbif6bafc62012-02-06 11:04:53 +0200773 trb->size = DWC3_TRB_SIZE_LENGTH(length);
774 trb->bpl = lower_32_bits(dma);
775 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200776
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200777 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200778 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200779 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780 break;
781
782 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530783 if (!node)
784 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
785 else
786 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200787
788 /* always enable Interrupt on Missed ISOC */
789 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200790 break;
791
792 case USB_ENDPOINT_XFER_BULK:
793 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200794 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200795 break;
796 default:
797 /*
798 * This is only possible with faulty memory because we
799 * checked it already :)
800 */
801 BUG();
802 }
803
Felipe Balbica4d44e2016-03-10 13:53:27 +0200804 /* always enable Continue on Short Packet */
805 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600806
Felipe Balbica4d44e2016-03-10 13:53:27 +0200807 if (!req->request.no_interrupt)
808 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
809
810 if (last)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530811 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200812
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530813 if (chain)
814 trb->ctrl |= DWC3_TRB_CTRL_CHN;
815
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200816 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200817 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
818
819 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500820
821 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200822}
823
Felipe Balbi72246da2011-08-19 18:10:58 +0300824/*
825 * dwc3_prepare_trbs - setup TRBs from requests
826 * @dep: endpoint for which requests are being prepared
827 * @starting: true if the endpoint is idle and no requests are queued.
828 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800829 * The function goes through the requests list and sets up TRBs for the
830 * transfers. The function returns once there are no more TRBs available or
831 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300832 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200833static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300834{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200835 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 u32 trbs_left;
Felipe Balbic71fc372011-11-22 11:37:34 +0200837 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838
839 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
840
Felipe Balbi4faf7552016-04-05 13:14:31 +0300841 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
Felipe Balbic71fc372011-11-22 11:37:34 +0200842
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 /*
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300844 * If enqueue & dequeue are equal than it is either full or empty. If we
845 * are starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300846 * full and don't do anything
847 */
848 if (!trbs_left) {
849 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200850 return;
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300851
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 trbs_left = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300853 }
854
855 /* The last TRB is a link TRB, not used for xfer */
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300856 if (trbs_left <= 1)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200857 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300858
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200859 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200860 unsigned length;
861 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530862 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300863
Felipe Balbieeb720f2011-11-28 12:46:59 +0200864 if (req->request.num_mapped_sgs > 0) {
865 struct usb_request *request = &req->request;
866 struct scatterlist *sg = request->sg;
867 struct scatterlist *s;
868 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300869
Felipe Balbieeb720f2011-11-28 12:46:59 +0200870 for_each_sg(sg, s, request->num_mapped_sgs, i) {
871 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300872
Felipe Balbieeb720f2011-11-28 12:46:59 +0200873 length = sg_dma_len(s);
874 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300875
Paul Zimmerman1d046792012-02-15 18:56:56 -0800876 if (i == (request->num_mapped_sgs - 1) ||
877 sg_is_last(s)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200878 if (list_empty(&dep->pending_list))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530879 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200880 chain = false;
881 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300882
Felipe Balbieeb720f2011-11-28 12:46:59 +0200883 trbs_left--;
884 if (!trbs_left)
885 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300886
Felipe Balbieeb720f2011-11-28 12:46:59 +0200887 if (last_one)
888 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
Felipe Balbieeb720f2011-11-28 12:46:59 +0200890 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530891 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300892
Felipe Balbieeb720f2011-11-28 12:46:59 +0200893 if (last_one)
894 break;
895 }
Amit Virdi39e60632015-01-13 14:27:21 +0530896
897 if (last_one)
898 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300899 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200900 dma = req->request.dma;
901 length = req->request.length;
902 trbs_left--;
903
904 if (!trbs_left)
905 last_one = 1;
906
907 /* Is this the last request? */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200908 if (list_is_last(&req->list, &dep->pending_list))
Felipe Balbieeb720f2011-11-28 12:46:59 +0200909 last_one = 1;
910
911 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530912 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200913
914 if (last_one)
915 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300916 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300917 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300918}
919
920static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
921 int start_new)
922{
923 struct dwc3_gadget_ep_cmd_params params;
924 struct dwc3_request *req;
925 struct dwc3 *dwc = dep->dwc;
926 int ret;
927 u32 cmd;
928
929 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600930 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300931 return -EBUSY;
932 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300933
934 /*
935 * If we are getting here after a short-out-packet we don't enqueue any
936 * new requests as we try to set the IOC bit only on the last request.
937 */
938 if (start_new) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200939 if (list_empty(&dep->started_list))
Felipe Balbi72246da2011-08-19 18:10:58 +0300940 dwc3_prepare_trbs(dep, start_new);
941
942 /* req points to the first request which will be sent */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200943 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300944 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200945 dwc3_prepare_trbs(dep, start_new);
946
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800948 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300949 */
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200950 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300951 }
952 if (!req) {
953 dep->flags |= DWC3_EP_PENDING_REQUEST;
954 return 0;
955 }
956
957 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300958
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530959 if (start_new) {
960 params.param0 = upper_32_bits(req->trb_dma);
961 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300962 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530963 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530965 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300966
967 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
968 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
969 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 /*
971 * FIXME we need to iterate over the list of requests
972 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800973 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300974 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200975 usb_gadget_unmap_request(&dwc->gadget, &req->request,
976 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300977 list_del(&req->list);
978 return ret;
979 }
980
981 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200982
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000983 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +0300984 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000985 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +0300986 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000987 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200988
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 return 0;
990}
991
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530992static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
993 struct dwc3_ep *dep, u32 cur_uf)
994{
995 u32 uf;
996
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200997 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -0600998 dwc3_trace(trace_dwc3_gadget,
999 "ISOC ep %s run out for requests",
1000 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301001 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301002 return;
1003 }
1004
1005 /* 4 micro frames in the future */
1006 uf = cur_uf + dep->interval * 4;
1007
1008 __dwc3_gadget_kick_transfer(dep, uf, 1);
1009}
1010
1011static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1012 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1013{
1014 u32 cur_uf, mask;
1015
1016 mask = ~(dep->interval - 1);
1017 cur_uf = event->parameters & mask;
1018
1019 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1020}
1021
Felipe Balbi72246da2011-08-19 18:10:58 +03001022static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1023{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001024 struct dwc3 *dwc = dep->dwc;
1025 int ret;
1026
Felipe Balbibb423982015-11-16 15:31:21 -06001027 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001028 dwc3_trace(trace_dwc3_gadget,
1029 "trying to queue request %p to disabled %s\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001030 &req->request, dep->endpoint.name);
1031 return -ESHUTDOWN;
1032 }
1033
1034 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1035 &req->request, req->dep->name)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001036 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'\n",
1037 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001038 return -EINVAL;
1039 }
1040
Felipe Balbi72246da2011-08-19 18:10:58 +03001041 req->request.actual = 0;
1042 req->request.status = -EINPROGRESS;
1043 req->direction = dep->direction;
1044 req->epnum = dep->number;
1045
Felipe Balbife84f522015-09-01 09:01:38 -05001046 trace_dwc3_ep_queue(req);
1047
Felipe Balbi72246da2011-08-19 18:10:58 +03001048 /*
1049 * We only add to our list of requests now and
1050 * start consuming the list once we get XferNotReady
1051 * IRQ.
1052 *
1053 * That way, we avoid doing anything that we don't need
1054 * to do now and defer it until the point we receive a
1055 * particular token from the Host side.
1056 *
1057 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001058 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001059 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001060 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1061 dep->direction);
1062 if (ret)
1063 return ret;
1064
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001065 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001066
1067 /*
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001068 * If there are no pending requests and the endpoint isn't already
1069 * busy, we will just start the request straight away.
1070 *
1071 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1072 * little bit faster.
1073 */
1074 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbi62e345a2015-11-30 15:24:29 -06001075 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001076 !(dep->flags & DWC3_EP_BUSY)) {
1077 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbia8f32812015-09-16 10:40:07 -05001078 goto out;
Felipe Balbi1d6a3912015-09-14 11:27:46 -05001079 }
1080
1081 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001082 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001083 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001084 * 1. XferNotReady with empty list of requests. We need to kick the
1085 * transfer here in that situation, otherwise we will be NAKing
1086 * forever. If we get XferNotReady before gadget driver has a
1087 * chance to queue a request, we will ACK the IRQ but won't be
1088 * able to receive the data until the next request is queued.
1089 * The following code is handling exactly that.
1090 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001091 */
1092 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301093 /*
1094 * If xfernotready is already elapsed and it is a case
1095 * of isoc transfer, then issue END TRANSFER, so that
1096 * you can receive xfernotready again and can have
1097 * notion of current microframe.
1098 */
1099 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001100 if (list_empty(&dep->started_list)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001101 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301102 dep->flags = DWC3_EP_ENABLED;
1103 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301104 return 0;
1105 }
1106
Felipe Balbib511e5e2012-06-06 12:00:50 +03001107 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbi89185912015-09-15 09:49:14 -05001108 if (!ret)
1109 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1110
Felipe Balbia8f32812015-09-16 10:40:07 -05001111 goto out;
Felipe Balbia0925322012-05-22 10:24:11 +03001112 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001113
Felipe Balbib511e5e2012-06-06 12:00:50 +03001114 /*
1115 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1116 * kick the transfer here after queuing a request, otherwise the
1117 * core may not see the modified TRB(s).
1118 */
1119 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301120 (dep->flags & DWC3_EP_BUSY) &&
1121 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001122 WARN_ON_ONCE(!dep->resource_index);
1123 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001124 false);
Felipe Balbia8f32812015-09-16 10:40:07 -05001125 goto out;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001126 }
1127
Felipe Balbib997ada2012-07-26 13:26:50 +03001128 /*
1129 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1130 * right away, otherwise host will not know we have streams to be
1131 * handled.
1132 */
Felipe Balbia8f32812015-09-16 10:40:07 -05001133 if (dep->stream_capable)
Felipe Balbib997ada2012-07-26 13:26:50 +03001134 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbib997ada2012-07-26 13:26:50 +03001135
Felipe Balbia8f32812015-09-16 10:40:07 -05001136out:
1137 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001138 dwc3_trace(trace_dwc3_gadget,
1139 "%s: failed to kick transfers\n",
Felipe Balbia8f32812015-09-16 10:40:07 -05001140 dep->name);
1141 if (ret == -EBUSY)
1142 ret = 0;
1143
1144 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001145}
1146
Felipe Balbi04c03d12015-12-02 10:06:45 -06001147static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1148 struct usb_request *request)
1149{
1150 dwc3_gadget_ep_free_request(ep, request);
1151}
1152
1153static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1154{
1155 struct dwc3_request *req;
1156 struct usb_request *request;
1157 struct usb_ep *ep = &dep->endpoint;
1158
1159 dwc3_trace(trace_dwc3_gadget, "queueing ZLP\n");
1160 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1161 if (!request)
1162 return -ENOMEM;
1163
1164 request->length = 0;
1165 request->buf = dwc->zlp_buf;
1166 request->complete = __dwc3_gadget_ep_zlp_complete;
1167
1168 req = to_dwc3_request(request);
1169
1170 return __dwc3_gadget_ep_queue(dep, req);
1171}
1172
Felipe Balbi72246da2011-08-19 18:10:58 +03001173static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1174 gfp_t gfp_flags)
1175{
1176 struct dwc3_request *req = to_dwc3_request(request);
1177 struct dwc3_ep *dep = to_dwc3_ep(ep);
1178 struct dwc3 *dwc = dep->dwc;
1179
1180 unsigned long flags;
1181
1182 int ret;
1183
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001184 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001185 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001186
1187 /*
1188 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1189 * setting request->zero, instead of doing magic, we will just queue an
1190 * extra usb_request ourselves so that it gets handled the same way as
1191 * any other request.
1192 */
John Yound92618982015-12-22 12:23:20 -08001193 if (ret == 0 && request->zero && request->length &&
1194 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001195 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1196
Felipe Balbi72246da2011-08-19 18:10:58 +03001197 spin_unlock_irqrestore(&dwc->lock, flags);
1198
1199 return ret;
1200}
1201
1202static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1203 struct usb_request *request)
1204{
1205 struct dwc3_request *req = to_dwc3_request(request);
1206 struct dwc3_request *r = NULL;
1207
1208 struct dwc3_ep *dep = to_dwc3_ep(ep);
1209 struct dwc3 *dwc = dep->dwc;
1210
1211 unsigned long flags;
1212 int ret = 0;
1213
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001214 trace_dwc3_ep_dequeue(req);
1215
Felipe Balbi72246da2011-08-19 18:10:58 +03001216 spin_lock_irqsave(&dwc->lock, flags);
1217
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001218 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001219 if (r == req)
1220 break;
1221 }
1222
1223 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001224 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001225 if (r == req)
1226 break;
1227 }
1228 if (r == req) {
1229 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001230 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301231 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 }
1233 dev_err(dwc->dev, "request %p was not queued to %s\n",
1234 request, ep->name);
1235 ret = -EINVAL;
1236 goto out0;
1237 }
1238
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301239out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001240 /* giveback the request */
1241 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1242
1243out0:
1244 spin_unlock_irqrestore(&dwc->lock, flags);
1245
1246 return ret;
1247}
1248
Felipe Balbi7a608552014-09-24 14:19:52 -05001249int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001250{
1251 struct dwc3_gadget_ep_cmd_params params;
1252 struct dwc3 *dwc = dep->dwc;
1253 int ret;
1254
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001255 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1256 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1257 return -EINVAL;
1258 }
1259
Felipe Balbi72246da2011-08-19 18:10:58 +03001260 memset(&params, 0x00, sizeof(params));
1261
1262 if (value) {
Felipe Balbi7a608552014-09-24 14:19:52 -05001263 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001264 (!list_empty(&dep->started_list) ||
1265 !list_empty(&dep->pending_list)))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001266 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001267 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001268 dep->name);
1269 return -EAGAIN;
1270 }
1271
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1273 DWC3_DEPCMD_SETSTALL, &params);
1274 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001275 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001276 dep->name);
1277 else
1278 dep->flags |= DWC3_EP_STALL;
1279 } else {
1280 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1281 DWC3_DEPCMD_CLEARSTALL, &params);
1282 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001283 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 dep->name);
1285 else
Alan Sterna535d812013-11-01 12:05:12 -04001286 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001287 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001288
Felipe Balbi72246da2011-08-19 18:10:58 +03001289 return ret;
1290}
1291
1292static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1293{
1294 struct dwc3_ep *dep = to_dwc3_ep(ep);
1295 struct dwc3 *dwc = dep->dwc;
1296
1297 unsigned long flags;
1298
1299 int ret;
1300
1301 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001302 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001303 spin_unlock_irqrestore(&dwc->lock, flags);
1304
1305 return ret;
1306}
1307
1308static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1309{
1310 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001311 struct dwc3 *dwc = dep->dwc;
1312 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001313 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001314
Paul Zimmerman249a4562012-02-24 17:32:16 -08001315 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 dep->flags |= DWC3_EP_WEDGE;
1317
Pratyush Anand08f0d962012-06-25 22:40:43 +05301318 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001319 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301320 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001321 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001322 spin_unlock_irqrestore(&dwc->lock, flags);
1323
1324 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001325}
1326
1327/* -------------------------------------------------------------------------- */
1328
1329static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1330 .bLength = USB_DT_ENDPOINT_SIZE,
1331 .bDescriptorType = USB_DT_ENDPOINT,
1332 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1333};
1334
1335static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1336 .enable = dwc3_gadget_ep0_enable,
1337 .disable = dwc3_gadget_ep0_disable,
1338 .alloc_request = dwc3_gadget_ep_alloc_request,
1339 .free_request = dwc3_gadget_ep_free_request,
1340 .queue = dwc3_gadget_ep0_queue,
1341 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301342 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001343 .set_wedge = dwc3_gadget_ep_set_wedge,
1344};
1345
1346static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1347 .enable = dwc3_gadget_ep_enable,
1348 .disable = dwc3_gadget_ep_disable,
1349 .alloc_request = dwc3_gadget_ep_alloc_request,
1350 .free_request = dwc3_gadget_ep_free_request,
1351 .queue = dwc3_gadget_ep_queue,
1352 .dequeue = dwc3_gadget_ep_dequeue,
1353 .set_halt = dwc3_gadget_ep_set_halt,
1354 .set_wedge = dwc3_gadget_ep_set_wedge,
1355};
1356
1357/* -------------------------------------------------------------------------- */
1358
1359static int dwc3_gadget_get_frame(struct usb_gadget *g)
1360{
1361 struct dwc3 *dwc = gadget_to_dwc(g);
1362 u32 reg;
1363
1364 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1365 return DWC3_DSTS_SOFFN(reg);
1366}
1367
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001368static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001369{
Felipe Balbi72246da2011-08-19 18:10:58 +03001370 unsigned long timeout;
Felipe Balbi72246da2011-08-19 18:10:58 +03001371
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001372 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001373 u32 reg;
1374
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 u8 link_state;
1376 u8 speed;
1377
Felipe Balbi72246da2011-08-19 18:10:58 +03001378 /*
1379 * According to the Databook Remote wakeup request should
1380 * be issued only when the device is in early suspend state.
1381 *
1382 * We can check that via USB Link State bits in DSTS register.
1383 */
1384 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1385
1386 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001387 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1388 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001389 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001390 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001391 }
1392
1393 link_state = DWC3_DSTS_USBLNKST(reg);
1394
1395 switch (link_state) {
1396 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1397 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1398 break;
1399 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001400 dwc3_trace(trace_dwc3_gadget,
1401 "can't wakeup from '%s'\n",
1402 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001403 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001404 }
1405
Felipe Balbi8598bde2012-01-02 18:55:57 +02001406 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1407 if (ret < 0) {
1408 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001409 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001410 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001411
Paul Zimmerman802fde92012-04-27 13:10:52 +03001412 /* Recent versions do this automatically */
1413 if (dwc->revision < DWC3_REVISION_194A) {
1414 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001415 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001416 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1417 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1418 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001419
Paul Zimmerman1d046792012-02-15 18:56:56 -08001420 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001421 timeout = jiffies + msecs_to_jiffies(100);
1422
Paul Zimmerman1d046792012-02-15 18:56:56 -08001423 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1425
1426 /* in HS, means ON */
1427 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1428 break;
1429 }
1430
1431 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1432 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001433 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001434 }
1435
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001436 return 0;
1437}
1438
1439static int dwc3_gadget_wakeup(struct usb_gadget *g)
1440{
1441 struct dwc3 *dwc = gadget_to_dwc(g);
1442 unsigned long flags;
1443 int ret;
1444
1445 spin_lock_irqsave(&dwc->lock, flags);
1446 ret = __dwc3_gadget_wakeup(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001447 spin_unlock_irqrestore(&dwc->lock, flags);
1448
1449 return ret;
1450}
1451
1452static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1453 int is_selfpowered)
1454{
1455 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001456 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001457
Paul Zimmerman249a4562012-02-24 17:32:16 -08001458 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001459 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001460 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001461
1462 return 0;
1463}
1464
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001465static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001466{
1467 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001468 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001469
1470 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001471 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001472 if (dwc->revision <= DWC3_REVISION_187A) {
1473 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1474 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1475 }
1476
1477 if (dwc->revision >= DWC3_REVISION_194A)
1478 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1479 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001480
1481 if (dwc->has_hibernation)
1482 reg |= DWC3_DCTL_KEEP_CONNECT;
1483
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001484 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001485 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001486 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001487
1488 if (dwc->has_hibernation && !suspend)
1489 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1490
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001491 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001492 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001493
1494 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1495
1496 do {
1497 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1498 if (is_on) {
1499 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1500 break;
1501 } else {
1502 if (reg & DWC3_DSTS_DEVCTRLHLT)
1503 break;
1504 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001505 timeout--;
1506 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301507 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001508 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 } while (1);
1510
Felipe Balbi73815282015-01-27 13:48:14 -06001511 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001512 dwc->gadget_driver
1513 ? dwc->gadget_driver->function : "no-function",
1514 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301515
1516 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001517}
1518
1519static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1520{
1521 struct dwc3 *dwc = gadget_to_dwc(g);
1522 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301523 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001524
1525 is_on = !!is_on;
1526
1527 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001528 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 spin_unlock_irqrestore(&dwc->lock, flags);
1530
Pratyush Anand6f17f742012-07-02 10:21:55 +05301531 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001532}
1533
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001534static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1535{
1536 u32 reg;
1537
1538 /* Enable all but Start and End of Frame IRQs */
1539 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1540 DWC3_DEVTEN_EVNTOVERFLOWEN |
1541 DWC3_DEVTEN_CMDCMPLTEN |
1542 DWC3_DEVTEN_ERRTICERREN |
1543 DWC3_DEVTEN_WKUPEVTEN |
1544 DWC3_DEVTEN_ULSTCNGEN |
1545 DWC3_DEVTEN_CONNECTDONEEN |
1546 DWC3_DEVTEN_USBRSTEN |
1547 DWC3_DEVTEN_DISCONNEVTEN);
1548
1549 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1550}
1551
1552static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1553{
1554 /* mask all interrupts */
1555 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1556}
1557
1558static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001559static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001560
Felipe Balbi72246da2011-08-19 18:10:58 +03001561static int dwc3_gadget_start(struct usb_gadget *g,
1562 struct usb_gadget_driver *driver)
1563{
1564 struct dwc3 *dwc = gadget_to_dwc(g);
1565 struct dwc3_ep *dep;
1566 unsigned long flags;
1567 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001568 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001569 u32 reg;
1570
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001571 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1572 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbidea520a2016-03-30 09:39:34 +03001573 IRQF_SHARED, "dwc3", dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001574 if (ret) {
1575 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1576 irq, ret);
1577 goto err0;
1578 }
1579
Felipe Balbi72246da2011-08-19 18:10:58 +03001580 spin_lock_irqsave(&dwc->lock, flags);
1581
1582 if (dwc->gadget_driver) {
1583 dev_err(dwc->dev, "%s is already bound to %s\n",
1584 dwc->gadget.name,
1585 dwc->gadget_driver->driver.name);
1586 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001587 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 }
1589
1590 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001591
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1593 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001594
1595 /**
1596 * WORKAROUND: DWC3 revision < 2.20a have an issue
1597 * which would cause metastability state on Run/Stop
1598 * bit if we try to force the IP to USB2-only mode.
1599 *
1600 * Because of that, we cannot configure the IP to any
1601 * speed other than the SuperSpeed
1602 *
1603 * Refers to:
1604 *
1605 * STAR#9000525659: Clock Domain Crossing on DCTL in
1606 * USB 2.0 Mode
1607 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001608 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001609 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001610 } else {
1611 switch (dwc->maximum_speed) {
1612 case USB_SPEED_LOW:
1613 reg |= DWC3_DSTS_LOWSPEED;
1614 break;
1615 case USB_SPEED_FULL:
1616 reg |= DWC3_DSTS_FULLSPEED1;
1617 break;
1618 case USB_SPEED_HIGH:
1619 reg |= DWC3_DSTS_HIGHSPEED;
1620 break;
John Youn75808622016-02-05 17:09:13 -08001621 case USB_SPEED_SUPER_PLUS:
1622 reg |= DWC3_DSTS_SUPERSPEED_PLUS;
1623 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001624 default:
John Youn77966eb2016-02-19 17:31:01 -08001625 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
1626 dwc->maximum_speed);
1627 /* fall through */
1628 case USB_SPEED_SUPER:
1629 reg |= DWC3_DCFG_SUPERSPEED;
1630 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001631 }
1632 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001633 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1634
1635 /* Start with SuperSpeed Default */
1636 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1637
1638 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001639 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1640 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001641 if (ret) {
1642 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001643 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001644 }
1645
1646 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001647 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1648 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001649 if (ret) {
1650 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001651 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 }
1653
1654 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001655 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001656 dwc3_ep0_out_start(dwc);
1657
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001658 dwc3_gadget_enable_irq(dwc);
1659
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 spin_unlock_irqrestore(&dwc->lock, flags);
1661
1662 return 0;
1663
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001664err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001665 __dwc3_gadget_ep_disable(dwc->eps[0]);
1666
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001667err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001668 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001669
1670err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001671 spin_unlock_irqrestore(&dwc->lock, flags);
1672
Felipe Balbidea520a2016-03-30 09:39:34 +03001673 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001674
1675err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001676 return ret;
1677}
1678
Felipe Balbi22835b82014-10-17 12:05:12 -05001679static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03001680{
1681 struct dwc3 *dwc = gadget_to_dwc(g);
1682 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001683 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001684
1685 spin_lock_irqsave(&dwc->lock, flags);
1686
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001687 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001688 __dwc3_gadget_ep_disable(dwc->eps[0]);
1689 __dwc3_gadget_ep_disable(dwc->eps[1]);
1690
1691 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001692
1693 spin_unlock_irqrestore(&dwc->lock, flags);
1694
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001695 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
Felipe Balbidea520a2016-03-30 09:39:34 +03001696 free_irq(irq, dwc->ev_buf);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001697
Felipe Balbi72246da2011-08-19 18:10:58 +03001698 return 0;
1699}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001700
Felipe Balbi72246da2011-08-19 18:10:58 +03001701static const struct usb_gadget_ops dwc3_gadget_ops = {
1702 .get_frame = dwc3_gadget_get_frame,
1703 .wakeup = dwc3_gadget_wakeup,
1704 .set_selfpowered = dwc3_gadget_set_selfpowered,
1705 .pullup = dwc3_gadget_pullup,
1706 .udc_start = dwc3_gadget_start,
1707 .udc_stop = dwc3_gadget_stop,
1708};
1709
1710/* -------------------------------------------------------------------------- */
1711
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001712static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1713 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001714{
1715 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001716 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001717
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001718 for (i = 0; i < num; i++) {
1719 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001720
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09001722 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001723 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03001724
1725 dep->dwc = dwc;
1726 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001727 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001728 dwc->eps[epnum] = dep;
1729
1730 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1731 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001732
Felipe Balbi72246da2011-08-19 18:10:58 +03001733 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001734
Felipe Balbi73815282015-01-27 13:48:14 -06001735 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03001736
Felipe Balbi72246da2011-08-19 18:10:58 +03001737 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001738 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301739 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001740 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1741 if (!epnum)
1742 dwc->gadget.ep0 = &dep->endpoint;
1743 } else {
1744 int ret;
1745
Robert Baldygae117e742013-12-13 12:23:38 +01001746 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001747 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001748 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1749 list_add_tail(&dep->endpoint.ep_list,
1750 &dwc->gadget.ep_list);
1751
1752 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001753 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001754 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001755 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001756
Robert Baldygaa474d3b2015-07-31 16:00:19 +02001757 if (epnum == 0 || epnum == 1) {
1758 dep->endpoint.caps.type_control = true;
1759 } else {
1760 dep->endpoint.caps.type_iso = true;
1761 dep->endpoint.caps.type_bulk = true;
1762 dep->endpoint.caps.type_int = true;
1763 }
1764
1765 dep->endpoint.caps.dir_in = !!direction;
1766 dep->endpoint.caps.dir_out = !direction;
1767
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001768 INIT_LIST_HEAD(&dep->pending_list);
1769 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001770 }
1771
1772 return 0;
1773}
1774
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001775static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1776{
1777 int ret;
1778
1779 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1780
1781 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1782 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001783 dwc3_trace(trace_dwc3_gadget,
1784 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001785 return ret;
1786 }
1787
1788 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1789 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06001790 dwc3_trace(trace_dwc3_gadget,
1791 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001792 return ret;
1793 }
1794
1795 return 0;
1796}
1797
Felipe Balbi72246da2011-08-19 18:10:58 +03001798static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1799{
1800 struct dwc3_ep *dep;
1801 u8 epnum;
1802
1803 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1804 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001805 if (!dep)
1806 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301807 /*
1808 * Physical endpoints 0 and 1 are special; they form the
1809 * bi-directional USB endpoint 0.
1810 *
1811 * For those two physical endpoints, we don't allocate a TRB
1812 * pool nor do we add them the endpoints list. Due to that, we
1813 * shouldn't do these two operations otherwise we would end up
1814 * with all sorts of bugs when removing dwc3.ko.
1815 */
1816 if (epnum != 0 && epnum != 1) {
1817 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001818 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301819 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001820
1821 kfree(dep);
1822 }
1823}
1824
Felipe Balbi72246da2011-08-19 18:10:58 +03001825/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001826
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301827static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1828 struct dwc3_request *req, struct dwc3_trb *trb,
1829 const struct dwc3_event_depevt *event, int status)
1830{
1831 unsigned int count;
1832 unsigned int s_pkt = 0;
1833 unsigned int trb_status;
1834
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001835 trace_dwc3_complete_trb(dep, trb);
1836
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301837 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1838 /*
1839 * We continue despite the error. There is not much we
1840 * can do. If we don't clean it up we loop forever. If
1841 * we skip the TRB then it gets overwritten after a
1842 * while since we use them in a ring buffer. A BUG()
1843 * would help. Lets hope that if this occurs, someone
1844 * fixes the root cause instead of looking away :)
1845 */
1846 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1847 dep->name, trb);
1848 count = trb->size & DWC3_TRB_SIZE_MASK;
1849
1850 if (dep->direction) {
1851 if (count) {
1852 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1853 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001854 dwc3_trace(trace_dwc3_gadget,
1855 "%s: incomplete IN transfer\n",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301856 dep->name);
1857 /*
1858 * If missed isoc occurred and there is
1859 * no request queued then issue END
1860 * TRANSFER, so that core generates
1861 * next xfernotready and we will issue
1862 * a fresh START TRANSFER.
1863 * If there are still queued request
1864 * then wait, do not issue either END
1865 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001866 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301867 * giveback.If any future queued request
1868 * is successfully transferred then we
1869 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001870 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301871 */
1872 dep->flags |= DWC3_EP_MISSED_ISOC;
1873 } else {
1874 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1875 dep->name);
1876 status = -ECONNRESET;
1877 }
1878 } else {
1879 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1880 }
1881 } else {
1882 if (count && (event->status & DEPEVT_STATUS_SHORT))
1883 s_pkt = 1;
1884 }
1885
1886 /*
1887 * We assume here we will always receive the entire data block
1888 * which we should receive. Meaning, if we program RX to
1889 * receive 4K but we receive only 2K, we assume that's all we
1890 * should receive and we simply bounce the request back to the
1891 * gadget driver for further processing.
1892 */
1893 req->request.actual += req->request.length - count;
1894 if (s_pkt)
1895 return 1;
1896 if ((event->status & DEPEVT_STATUS_LST) &&
1897 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1898 DWC3_TRB_CTRL_HWO)))
1899 return 1;
1900 if ((event->status & DEPEVT_STATUS_IOC) &&
1901 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1902 return 1;
1903 return 0;
1904}
1905
Felipe Balbi72246da2011-08-19 18:10:58 +03001906static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1907 const struct dwc3_event_depevt *event, int status)
1908{
1909 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001910 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301911 unsigned int slot;
1912 unsigned int i;
1913 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001914
1915 do {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001916 req = next_request(&dep->started_list);
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001917 if (WARN_ON_ONCE(!req))
Ville Syrjäläd115d702015-08-31 19:48:28 +03001918 return 1;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06001919
Ville Syrjäläd115d702015-08-31 19:48:28 +03001920 i = 0;
1921 do {
Felipe Balbi53fd8812016-04-04 15:33:41 +03001922 slot = req->first_trb_index + i;
Felipe Balbi36b68aa2016-04-05 13:24:36 +03001923 if (slot == DWC3_TRB_NUM - 1)
Ville Syrjäläd115d702015-08-31 19:48:28 +03001924 slot++;
1925 slot %= DWC3_TRB_NUM;
1926 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001927
Ville Syrjäläd115d702015-08-31 19:48:28 +03001928 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1929 event, status);
1930 if (ret)
1931 break;
1932 } while (++i < req->request.num_mapped_sgs);
1933
1934 dwc3_gadget_giveback(dep, req, status);
1935
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301936 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001937 break;
Ville Syrjäläd115d702015-08-31 19:48:28 +03001938 } while (1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001939
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301940 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001941 list_empty(&dep->started_list)) {
1942 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301943 /*
1944 * If there is no entry in request list then do
1945 * not issue END TRANSFER now. Just set PENDING
1946 * flag, so that END TRANSFER is issued when an
1947 * entry is added into request list.
1948 */
1949 dep->flags = DWC3_EP_PENDING_REQUEST;
1950 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001951 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301952 dep->flags = DWC3_EP_ENABLED;
1953 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301954 return 1;
1955 }
1956
Felipe Balbi72246da2011-08-19 18:10:58 +03001957 return 1;
1958}
1959
1960static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09001961 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03001962{
1963 unsigned status = 0;
1964 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05001965 u32 is_xfer_complete;
1966
1967 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001968
1969 if (event->status & DEPEVT_STATUS_BUSERR)
1970 status = -ECONNRESET;
1971
Paul Zimmerman1d046792012-02-15 18:56:56 -08001972 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbie18b7972015-05-29 10:06:38 -05001973 if (clean_busy && (is_xfer_complete ||
1974 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001975 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001976
1977 /*
1978 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1979 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1980 */
1981 if (dwc->revision < DWC3_REVISION_183A) {
1982 u32 reg;
1983 int i;
1984
1985 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001986 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001987
1988 if (!(dep->flags & DWC3_EP_ENABLED))
1989 continue;
1990
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001991 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03001992 return;
1993 }
1994
1995 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1996 reg |= dwc->u1u2;
1997 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1998
1999 dwc->u1u2 = 0;
2000 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002001
Felipe Balbie6e709b2015-09-28 15:16:56 -05002002 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002003 int ret;
2004
Felipe Balbie6e709b2015-09-28 15:16:56 -05002005 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002006 if (!ret || ret == -EBUSY)
2007 return;
2008 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002009}
2010
Felipe Balbi72246da2011-08-19 18:10:58 +03002011static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2012 const struct dwc3_event_depevt *event)
2013{
2014 struct dwc3_ep *dep;
2015 u8 epnum = event->endpoint_number;
2016
2017 dep = dwc->eps[epnum];
2018
Felipe Balbi3336abb2012-06-06 09:19:35 +03002019 if (!(dep->flags & DWC3_EP_ENABLED))
2020 return;
2021
Felipe Balbi72246da2011-08-19 18:10:58 +03002022 if (epnum == 0 || epnum == 1) {
2023 dwc3_ep0_interrupt(dwc, event);
2024 return;
2025 }
2026
2027 switch (event->endpoint_event) {
2028 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002029 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002030
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002031 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002032 dwc3_trace(trace_dwc3_gadget,
2033 "%s is an Isochronous endpoint\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002034 dep->name);
2035 return;
2036 }
2037
Jingoo Han029d97f2014-07-04 15:00:51 +09002038 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002039 break;
2040 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002041 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002042 break;
2043 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002044 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002045 dwc3_gadget_start_isoc(dwc, dep, event);
2046 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002047 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002048 int ret;
2049
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002050 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2051
Felipe Balbi73815282015-01-27 13:48:14 -06002052 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002053 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002054 : "Transfer Not Active");
2055
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002056 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
Felipe Balbi72246da2011-08-19 18:10:58 +03002057 if (!ret || ret == -EBUSY)
2058 return;
2059
Felipe Balbiec5e7952015-11-16 16:04:13 -06002060 dwc3_trace(trace_dwc3_gadget,
2061 "%s: failed to kick transfers\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03002062 dep->name);
2063 }
2064
2065 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002066 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002067 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002068 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2069 dep->name);
2070 return;
2071 }
2072
2073 switch (event->status) {
2074 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002075 dwc3_trace(trace_dwc3_gadget,
2076 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002077 event->parameters);
2078
2079 break;
2080 case DEPEVT_STREAMEVT_NOTFOUND:
2081 /* FALLTHROUGH */
2082 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002083 dwc3_trace(trace_dwc3_gadget,
2084 "unable to find suitable stream\n");
Felipe Balbi879631a2011-09-30 10:58:47 +03002085 }
2086 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002087 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002088 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002089 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002090 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002091 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002092 break;
2093 }
2094}
2095
2096static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2097{
2098 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2099 spin_unlock(&dwc->lock);
2100 dwc->gadget_driver->disconnect(&dwc->gadget);
2101 spin_lock(&dwc->lock);
2102 }
2103}
2104
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002105static void dwc3_suspend_gadget(struct dwc3 *dwc)
2106{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002107 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002108 spin_unlock(&dwc->lock);
2109 dwc->gadget_driver->suspend(&dwc->gadget);
2110 spin_lock(&dwc->lock);
2111 }
2112}
2113
2114static void dwc3_resume_gadget(struct dwc3 *dwc)
2115{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002116 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002117 spin_unlock(&dwc->lock);
2118 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002119 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002120 }
2121}
2122
2123static void dwc3_reset_gadget(struct dwc3 *dwc)
2124{
2125 if (!dwc->gadget_driver)
2126 return;
2127
2128 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2129 spin_unlock(&dwc->lock);
2130 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002131 spin_lock(&dwc->lock);
2132 }
2133}
2134
Paul Zimmermanb992e682012-04-27 14:17:35 +03002135static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002136{
2137 struct dwc3_ep *dep;
2138 struct dwc3_gadget_ep_cmd_params params;
2139 u32 cmd;
2140 int ret;
2141
2142 dep = dwc->eps[epnum];
2143
Felipe Balbib4996a82012-06-06 12:04:13 +03002144 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302145 return;
2146
Pratyush Anand57911502012-07-06 15:19:10 +05302147 /*
2148 * NOTICE: We are violating what the Databook says about the
2149 * EndTransfer command. Ideally we would _always_ wait for the
2150 * EndTransfer Command Completion IRQ, but that's causing too
2151 * much trouble synchronizing between us and gadget driver.
2152 *
2153 * We have discussed this with the IP Provider and it was
2154 * suggested to giveback all requests here, but give HW some
2155 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002156 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302157 *
2158 * Note also that a similar handling was tested by Synopsys
2159 * (thanks a lot Paul) and nothing bad has come out of it.
2160 * In short, what we're doing is:
2161 *
2162 * - Issue EndTransfer WITH CMDIOC bit set
2163 * - Wait 100us
2164 */
2165
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302166 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002167 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2168 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002169 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302170 memset(&params, 0, sizeof(params));
2171 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2172 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002173 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002174 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302175 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002176}
2177
2178static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2179{
2180 u32 epnum;
2181
2182 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2183 struct dwc3_ep *dep;
2184
2185 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002186 if (!dep)
2187 continue;
2188
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 if (!(dep->flags & DWC3_EP_ENABLED))
2190 continue;
2191
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002192 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002193 }
2194}
2195
2196static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2197{
2198 u32 epnum;
2199
2200 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2201 struct dwc3_ep *dep;
2202 struct dwc3_gadget_ep_cmd_params params;
2203 int ret;
2204
2205 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002206 if (!dep)
2207 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002208
2209 if (!(dep->flags & DWC3_EP_STALL))
2210 continue;
2211
2212 dep->flags &= ~DWC3_EP_STALL;
2213
2214 memset(&params, 0, sizeof(params));
2215 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2216 DWC3_DEPCMD_CLEARSTALL, &params);
2217 WARN_ON_ONCE(ret);
2218 }
2219}
2220
2221static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2222{
Felipe Balbic4430a22012-05-24 10:30:01 +03002223 int reg;
2224
Felipe Balbi72246da2011-08-19 18:10:58 +03002225 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2226 reg &= ~DWC3_DCTL_INITU1ENA;
2227 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2228
2229 reg &= ~DWC3_DCTL_INITU2ENA;
2230 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002231
Felipe Balbi72246da2011-08-19 18:10:58 +03002232 dwc3_disconnect_gadget(dwc);
2233
2234 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002235 dwc->setup_packet_pending = false;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002236 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbi72246da2011-08-19 18:10:58 +03002237}
2238
Felipe Balbi72246da2011-08-19 18:10:58 +03002239static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2240{
2241 u32 reg;
2242
Felipe Balbidf62df52011-10-14 15:11:49 +03002243 /*
2244 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2245 * would cause a missing Disconnect Event if there's a
2246 * pending Setup Packet in the FIFO.
2247 *
2248 * There's no suggested workaround on the official Bug
2249 * report, which states that "unless the driver/application
2250 * is doing any special handling of a disconnect event,
2251 * there is no functional issue".
2252 *
2253 * Unfortunately, it turns out that we _do_ some special
2254 * handling of a disconnect event, namely complete all
2255 * pending transfers, notify gadget driver of the
2256 * disconnection, and so on.
2257 *
2258 * Our suggested workaround is to follow the Disconnect
2259 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002260 * flag. Such flag gets set whenever we have a SETUP_PENDING
2261 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002262 * same endpoint.
2263 *
2264 * Refers to:
2265 *
2266 * STAR#9000466709: RTL: Device : Disconnect event not
2267 * generated if setup packet pending in FIFO
2268 */
2269 if (dwc->revision < DWC3_REVISION_188A) {
2270 if (dwc->setup_packet_pending)
2271 dwc3_gadget_disconnect_interrupt(dwc);
2272 }
2273
Felipe Balbi8e744752014-11-06 14:27:53 +08002274 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002275
2276 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2277 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2278 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002279 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002280
2281 dwc3_stop_active_transfers(dwc);
2282 dwc3_clear_stall_all_ep(dwc);
2283
2284 /* Reset device address to zero */
2285 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2286 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2287 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002288}
2289
2290static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2291{
2292 u32 reg;
2293 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2294
2295 /*
2296 * We change the clock only at SS but I dunno why I would want to do
2297 * this. Maybe it becomes part of the power saving plan.
2298 */
2299
John Younee5cd412016-02-05 17:08:45 -08002300 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2301 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002302 return;
2303
2304 /*
2305 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2306 * each time on Connect Done.
2307 */
2308 if (!usb30_clock)
2309 return;
2310
2311 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2312 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2313 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2314}
2315
Felipe Balbi72246da2011-08-19 18:10:58 +03002316static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2317{
Felipe Balbi72246da2011-08-19 18:10:58 +03002318 struct dwc3_ep *dep;
2319 int ret;
2320 u32 reg;
2321 u8 speed;
2322
Felipe Balbi72246da2011-08-19 18:10:58 +03002323 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2324 speed = reg & DWC3_DSTS_CONNECTSPD;
2325 dwc->speed = speed;
2326
2327 dwc3_update_ram_clk_sel(dwc, speed);
2328
2329 switch (speed) {
John Youn75808622016-02-05 17:09:13 -08002330 case DWC3_DCFG_SUPERSPEED_PLUS:
2331 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2332 dwc->gadget.ep0->maxpacket = 512;
2333 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2334 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002335 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c5b2011-10-14 14:51:38 +03002336 /*
2337 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2338 * would cause a missing USB3 Reset event.
2339 *
2340 * In such situations, we should force a USB3 Reset
2341 * event by calling our dwc3_gadget_reset_interrupt()
2342 * routine.
2343 *
2344 * Refers to:
2345 *
2346 * STAR#9000483510: RTL: SS : USB3 reset event may
2347 * not be generated always when the link enters poll
2348 */
2349 if (dwc->revision < DWC3_REVISION_190A)
2350 dwc3_gadget_reset_interrupt(dwc);
2351
Felipe Balbi72246da2011-08-19 18:10:58 +03002352 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2353 dwc->gadget.ep0->maxpacket = 512;
2354 dwc->gadget.speed = USB_SPEED_SUPER;
2355 break;
2356 case DWC3_DCFG_HIGHSPEED:
2357 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2358 dwc->gadget.ep0->maxpacket = 64;
2359 dwc->gadget.speed = USB_SPEED_HIGH;
2360 break;
2361 case DWC3_DCFG_FULLSPEED2:
2362 case DWC3_DCFG_FULLSPEED1:
2363 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2364 dwc->gadget.ep0->maxpacket = 64;
2365 dwc->gadget.speed = USB_SPEED_FULL;
2366 break;
2367 case DWC3_DCFG_LOWSPEED:
2368 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2369 dwc->gadget.ep0->maxpacket = 8;
2370 dwc->gadget.speed = USB_SPEED_LOW;
2371 break;
2372 }
2373
Pratyush Anand2b758352013-01-14 15:59:31 +05302374 /* Enable USB2 LPM Capability */
2375
John Younee5cd412016-02-05 17:08:45 -08002376 if ((dwc->revision > DWC3_REVISION_194A) &&
2377 (speed != DWC3_DCFG_SUPERSPEED) &&
2378 (speed != DWC3_DCFG_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302379 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2380 reg |= DWC3_DCFG_LPM_CAP;
2381 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2382
2383 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2384 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2385
Huang Rui460d0982014-10-31 11:11:18 +08002386 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302387
Huang Rui80caf7d2014-10-28 19:54:26 +08002388 /*
2389 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2390 * DCFG.LPMCap is set, core responses with an ACK and the
2391 * BESL value in the LPM token is less than or equal to LPM
2392 * NYET threshold.
2393 */
2394 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2395 && dwc->has_lpm_erratum,
2396 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2397
2398 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2399 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2400
Pratyush Anand2b758352013-01-14 15:59:31 +05302401 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002402 } else {
2403 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2404 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2405 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302406 }
2407
Felipe Balbi72246da2011-08-19 18:10:58 +03002408 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002409 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2410 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 if (ret) {
2412 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2413 return;
2414 }
2415
2416 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002417 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2418 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002419 if (ret) {
2420 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2421 return;
2422 }
2423
2424 /*
2425 * Configure PHY via GUSB3PIPECTLn if required.
2426 *
2427 * Update GTXFIFOSIZn
2428 *
2429 * In both cases reset values should be sufficient.
2430 */
2431}
2432
2433static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2434{
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 /*
2436 * TODO take core out of low power mode when that's
2437 * implemented.
2438 */
2439
Jiebing Liad14d4e2014-12-11 13:26:29 +08002440 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2441 spin_unlock(&dwc->lock);
2442 dwc->gadget_driver->resume(&dwc->gadget);
2443 spin_lock(&dwc->lock);
2444 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002445}
2446
2447static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2448 unsigned int evtinfo)
2449{
Felipe Balbifae2b902011-10-14 13:00:30 +03002450 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002451 unsigned int pwropt;
2452
2453 /*
2454 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2455 * Hibernation mode enabled which would show up when device detects
2456 * host-initiated U3 exit.
2457 *
2458 * In that case, device will generate a Link State Change Interrupt
2459 * from U3 to RESUME which is only necessary if Hibernation is
2460 * configured in.
2461 *
2462 * There are no functional changes due to such spurious event and we
2463 * just need to ignore it.
2464 *
2465 * Refers to:
2466 *
2467 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2468 * operational mode
2469 */
2470 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2471 if ((dwc->revision < DWC3_REVISION_250A) &&
2472 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2473 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2474 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002475 dwc3_trace(trace_dwc3_gadget,
2476 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002477 return;
2478 }
2479 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002480
2481 /*
2482 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2483 * on the link partner, the USB session might do multiple entry/exit
2484 * of low power states before a transfer takes place.
2485 *
2486 * Due to this problem, we might experience lower throughput. The
2487 * suggested workaround is to disable DCTL[12:9] bits if we're
2488 * transitioning from U1/U2 to U0 and enable those bits again
2489 * after a transfer completes and there are no pending transfers
2490 * on any of the enabled endpoints.
2491 *
2492 * This is the first half of that workaround.
2493 *
2494 * Refers to:
2495 *
2496 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2497 * core send LGO_Ux entering U0
2498 */
2499 if (dwc->revision < DWC3_REVISION_183A) {
2500 if (next == DWC3_LINK_STATE_U0) {
2501 u32 u1u2;
2502 u32 reg;
2503
2504 switch (dwc->link_state) {
2505 case DWC3_LINK_STATE_U1:
2506 case DWC3_LINK_STATE_U2:
2507 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2508 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2509 | DWC3_DCTL_ACCEPTU2ENA
2510 | DWC3_DCTL_INITU1ENA
2511 | DWC3_DCTL_ACCEPTU1ENA);
2512
2513 if (!dwc->u1u2)
2514 dwc->u1u2 = reg & u1u2;
2515
2516 reg &= ~u1u2;
2517
2518 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2519 break;
2520 default:
2521 /* do nothing */
2522 break;
2523 }
2524 }
2525 }
2526
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002527 switch (next) {
2528 case DWC3_LINK_STATE_U1:
2529 if (dwc->speed == USB_SPEED_SUPER)
2530 dwc3_suspend_gadget(dwc);
2531 break;
2532 case DWC3_LINK_STATE_U2:
2533 case DWC3_LINK_STATE_U3:
2534 dwc3_suspend_gadget(dwc);
2535 break;
2536 case DWC3_LINK_STATE_RESUME:
2537 dwc3_resume_gadget(dwc);
2538 break;
2539 default:
2540 /* do nothing */
2541 break;
2542 }
2543
Felipe Balbie57ebc12014-04-22 13:20:12 -05002544 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002545}
2546
Felipe Balbie1dadd32014-02-25 14:47:54 -06002547static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2548 unsigned int evtinfo)
2549{
2550 unsigned int is_ss = evtinfo & BIT(4);
2551
2552 /**
2553 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2554 * have a known issue which can cause USB CV TD.9.23 to fail
2555 * randomly.
2556 *
2557 * Because of this issue, core could generate bogus hibernation
2558 * events which SW needs to ignore.
2559 *
2560 * Refers to:
2561 *
2562 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2563 * Device Fallback from SuperSpeed
2564 */
2565 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2566 return;
2567
2568 /* enter hibernation here */
2569}
2570
Felipe Balbi72246da2011-08-19 18:10:58 +03002571static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2572 const struct dwc3_event_devt *event)
2573{
2574 switch (event->type) {
2575 case DWC3_DEVICE_EVENT_DISCONNECT:
2576 dwc3_gadget_disconnect_interrupt(dwc);
2577 break;
2578 case DWC3_DEVICE_EVENT_RESET:
2579 dwc3_gadget_reset_interrupt(dwc);
2580 break;
2581 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2582 dwc3_gadget_conndone_interrupt(dwc);
2583 break;
2584 case DWC3_DEVICE_EVENT_WAKEUP:
2585 dwc3_gadget_wakeup_interrupt(dwc);
2586 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002587 case DWC3_DEVICE_EVENT_HIBER_REQ:
2588 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2589 "unexpected hibernation event\n"))
2590 break;
2591
2592 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2593 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002594 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2595 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2596 break;
2597 case DWC3_DEVICE_EVENT_EOPF:
Felipe Balbi73815282015-01-27 13:48:14 -06002598 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002599 break;
2600 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06002601 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03002602 break;
2603 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06002604 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03002605 break;
2606 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06002607 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002608 break;
2609 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06002610 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03002611 break;
2612 default:
Felipe Balbie9f2aa82015-01-27 13:49:28 -06002613 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03002614 }
2615}
2616
2617static void dwc3_process_event_entry(struct dwc3 *dwc,
2618 const union dwc3_event *event)
2619{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002620 trace_dwc3_event(event->raw);
2621
Felipe Balbi72246da2011-08-19 18:10:58 +03002622 /* Endpoint IRQ, handle it and return early */
2623 if (event->type.is_devspec == 0) {
2624 /* depevt */
2625 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2626 }
2627
2628 switch (event->type.type) {
2629 case DWC3_EVENT_TYPE_DEV:
2630 dwc3_gadget_interrupt(dwc, &event->devt);
2631 break;
2632 /* REVISIT what to do with Carkit and I2C events ? */
2633 default:
2634 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2635 }
2636}
2637
Felipe Balbidea520a2016-03-30 09:39:34 +03002638static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbif42f2442013-06-12 21:25:08 +03002639{
Felipe Balbidea520a2016-03-30 09:39:34 +03002640 struct dwc3 *dwc = evt->dwc;
Felipe Balbif42f2442013-06-12 21:25:08 +03002641 irqreturn_t ret = IRQ_NONE;
2642 int left;
2643 u32 reg;
2644
Felipe Balbif42f2442013-06-12 21:25:08 +03002645 left = evt->count;
2646
2647 if (!(evt->flags & DWC3_EVENT_PENDING))
2648 return IRQ_NONE;
2649
2650 while (left > 0) {
2651 union dwc3_event event;
2652
2653 event.raw = *(u32 *) (evt->buf + evt->lpos);
2654
2655 dwc3_process_event_entry(dwc, &event);
2656
2657 /*
2658 * FIXME we wrap around correctly to the next entry as
2659 * almost all entries are 4 bytes in size. There is one
2660 * entry which has 12 bytes which is a regular entry
2661 * followed by 8 bytes data. ATM I don't know how
2662 * things are organized if we get next to the a
2663 * boundary so I worry about that once we try to handle
2664 * that.
2665 */
2666 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2667 left -= 4;
2668
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002669 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03002670 }
2671
2672 evt->count = 0;
2673 evt->flags &= ~DWC3_EVENT_PENDING;
2674 ret = IRQ_HANDLED;
2675
2676 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002677 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03002678 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002679 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03002680
2681 return ret;
2682}
2683
Felipe Balbidea520a2016-03-30 09:39:34 +03002684static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
Felipe Balbib15a7622011-06-30 16:57:15 +03002685{
Felipe Balbidea520a2016-03-30 09:39:34 +03002686 struct dwc3_event_buffer *evt = _evt;
2687 struct dwc3 *dwc = evt->dwc;
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002688 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03002689 irqreturn_t ret = IRQ_NONE;
Felipe Balbib15a7622011-06-30 16:57:15 +03002690
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002691 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbidea520a2016-03-30 09:39:34 +03002692 ret = dwc3_process_event_buf(evt);
Felipe Balbie5f68b4a2015-10-12 13:25:44 -05002693 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03002694
2695 return ret;
2696}
2697
Felipe Balbidea520a2016-03-30 09:39:34 +03002698static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002699{
Felipe Balbidea520a2016-03-30 09:39:34 +03002700 struct dwc3 *dwc = evt->dwc;
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002702 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002703
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002704 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 count &= DWC3_GEVNTCOUNT_MASK;
2706 if (!count)
2707 return IRQ_NONE;
2708
Felipe Balbib15a7622011-06-30 16:57:15 +03002709 evt->count = count;
2710 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002711
Felipe Balbie8adfc32013-06-12 21:11:14 +03002712 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002713 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03002714 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03002715 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03002716
Felipe Balbib15a7622011-06-30 16:57:15 +03002717 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002718}
2719
Felipe Balbidea520a2016-03-30 09:39:34 +03002720static irqreturn_t dwc3_interrupt(int irq, void *_evt)
Felipe Balbi72246da2011-08-19 18:10:58 +03002721{
Felipe Balbidea520a2016-03-30 09:39:34 +03002722 struct dwc3_event_buffer *evt = _evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002723
Felipe Balbidea520a2016-03-30 09:39:34 +03002724 return dwc3_check_event_buf(evt);
Felipe Balbi72246da2011-08-19 18:10:58 +03002725}
2726
2727/**
2728 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002729 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 *
2731 * Returns 0 on success otherwise negative errno.
2732 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002733int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002734{
Felipe Balbi72246da2011-08-19 18:10:58 +03002735 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002736
2737 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2738 &dwc->ctrl_req_addr, GFP_KERNEL);
2739 if (!dwc->ctrl_req) {
2740 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2741 ret = -ENOMEM;
2742 goto err0;
2743 }
2744
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05302745 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03002746 &dwc->ep0_trb_addr, GFP_KERNEL);
2747 if (!dwc->ep0_trb) {
2748 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2749 ret = -ENOMEM;
2750 goto err1;
2751 }
2752
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002753 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002754 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002755 ret = -ENOMEM;
2756 goto err2;
2757 }
2758
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002759 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002760 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2761 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002762 if (!dwc->ep0_bounce) {
2763 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2764 ret = -ENOMEM;
2765 goto err3;
2766 }
2767
Felipe Balbi04c03d12015-12-02 10:06:45 -06002768 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
2769 if (!dwc->zlp_buf) {
2770 ret = -ENOMEM;
2771 goto err4;
2772 }
2773
Felipe Balbi72246da2011-08-19 18:10:58 +03002774 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03002775 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002776 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002777 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08002778 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03002779
2780 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06002781 * FIXME We might be setting max_speed to <SUPER, however versions
2782 * <2.20a of dwc3 have an issue with metastability (documented
2783 * elsewhere in this driver) which tells us we can't set max speed to
2784 * anything lower than SUPER.
2785 *
2786 * Because gadget.max_speed is only used by composite.c and function
2787 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2788 * to happen so we avoid sending SuperSpeed Capability descriptor
2789 * together with our BOS descriptor as that could confuse host into
2790 * thinking we can handle super speed.
2791 *
2792 * Note that, in fact, we won't even support GetBOS requests when speed
2793 * is less than super speed because we don't have means, yet, to tell
2794 * composite.c that we are USB 2.0 + LPM ECN.
2795 */
2796 if (dwc->revision < DWC3_REVISION_220A)
2797 dwc3_trace(trace_dwc3_gadget,
2798 "Changing max_speed on rev %08x\n",
2799 dwc->revision);
2800
2801 dwc->gadget.max_speed = dwc->maximum_speed;
2802
2803 /*
David Cohena4b9d942013-12-09 15:55:38 -08002804 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2805 * on ep out.
2806 */
2807 dwc->gadget.quirk_ep_out_aligned_size = true;
2808
2809 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002810 * REVISIT: Here we should clear all pending IRQs to be
2811 * sure we're starting from a well known location.
2812 */
2813
2814 ret = dwc3_gadget_init_endpoints(dwc);
2815 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06002816 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002817
Felipe Balbi72246da2011-08-19 18:10:58 +03002818 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2819 if (ret) {
2820 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06002821 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002822 }
2823
2824 return 0;
2825
Felipe Balbi04c03d12015-12-02 10:06:45 -06002826err5:
2827 kfree(dwc->zlp_buf);
2828
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002829err4:
David Cohene1f80462013-09-11 17:42:47 -07002830 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002831 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2832 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002833
Felipe Balbi72246da2011-08-19 18:10:58 +03002834err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002835 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002836
2837err2:
2838 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2839 dwc->ep0_trb, dwc->ep0_trb_addr);
2840
2841err1:
2842 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2843 dwc->ctrl_req, dwc->ctrl_req_addr);
2844
2845err0:
2846 return ret;
2847}
2848
Felipe Balbi7415f172012-04-30 14:56:33 +03002849/* -------------------------------------------------------------------------- */
2850
Felipe Balbi72246da2011-08-19 18:10:58 +03002851void dwc3_gadget_exit(struct dwc3 *dwc)
2852{
Felipe Balbi72246da2011-08-19 18:10:58 +03002853 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002854
Felipe Balbi72246da2011-08-19 18:10:58 +03002855 dwc3_gadget_free_endpoints(dwc);
2856
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002857 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2858 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002859
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002860 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06002861 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002862
2863 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2864 dwc->ep0_trb, dwc->ep0_trb_addr);
2865
2866 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2867 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002868}
Felipe Balbi7415f172012-04-30 14:56:33 +03002869
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002870int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03002871{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002872 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002873 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002874 dwc3_gadget_run_stop(dwc, true, true);
2875 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002876
Felipe Balbi7415f172012-04-30 14:56:33 +03002877 __dwc3_gadget_ep_disable(dwc->eps[0]);
2878 __dwc3_gadget_ep_disable(dwc->eps[1]);
2879
2880 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2881
2882 return 0;
2883}
2884
2885int dwc3_gadget_resume(struct dwc3 *dwc)
2886{
2887 struct dwc3_ep *dep;
2888 int ret;
2889
2890 /* Start with SuperSpeed Default */
2891 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2892
2893 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002894 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2895 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002896 if (ret)
2897 goto err0;
2898
2899 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002900 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2901 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002902 if (ret)
2903 goto err1;
2904
2905 /* begin to receive SETUP packets */
2906 dwc->ep0state = EP0_SETUP_PHASE;
2907 dwc3_ep0_out_start(dwc);
2908
2909 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2910
Felipe Balbi0b0231a2014-10-07 10:19:23 -05002911 if (dwc->pullups_connected) {
2912 dwc3_gadget_enable_irq(dwc);
2913 dwc3_gadget_run_stop(dwc, true, false);
2914 }
2915
Felipe Balbi7415f172012-04-30 14:56:33 +03002916 return 0;
2917
2918err1:
2919 __dwc3_gadget_ep_disable(dwc->eps[0]);
2920
2921err0:
2922 return ret;
2923}