blob: 9d570d1dae6d0d4e271622f3529377d4fa8adad8 [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>
Mayank Ranaa99689a2016-08-10 17:39:47 -070025#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030026#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/list.h>
29#include <linux/dma-mapping.h>
30
31#include <linux/usb/ch9.h>
Mayank Ranaa99689a2016-08-10 17:39:47 -070032#include <linux/usb/composite.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030033#include <linux/usb/gadget.h>
34
Felipe Balbi80977dc2014-08-19 16:37:22 -050035#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030036#include "core.h"
37#include "gadget.h"
38#include "io.h"
39
Mayank Ranaa99689a2016-08-10 17:39:47 -070040static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup);
41static int dwc3_gadget_wakeup_int(struct dwc3 *dwc);
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020042/**
43 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
44 * @dwc: pointer to our context structure
45 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
46 *
47 * Caller should take care of locking. This function will
48 * return 0 on success or -EINVAL if wrong Test Selector
49 * is passed
50 */
51int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
52{
53 u32 reg;
54
55 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
56 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
57
58 switch (mode) {
59 case TEST_J:
60 case TEST_K:
61 case TEST_SE0_NAK:
62 case TEST_PACKET:
63 case TEST_FORCE_EN:
64 reg |= mode << 1;
65 break;
66 default:
67 return -EINVAL;
68 }
69
70 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
71
72 return 0;
73}
74
Felipe Balbi8598bde2012-01-02 18:55:57 +020075/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030076 * dwc3_gadget_get_link_state - Gets current state of USB Link
77 * @dwc: pointer to our context structure
78 *
79 * Caller should take care of locking. This function will
80 * return the link state on success (>= 0) or -ETIMEDOUT.
81 */
82int dwc3_gadget_get_link_state(struct dwc3 *dwc)
83{
84 u32 reg;
85
86 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
87
88 return DWC3_DSTS_USBLNKST(reg);
89}
90
91/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020092 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
Paul Zimmerman802fde92012-04-27 13:10:52 +0300104 /*
105 * Wait until device controller is ready. Only applies to 1.94a and
106 * later RTL.
107 */
108 if (dwc->revision >= DWC3_REVISION_194A) {
109 while (--retries) {
110 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
111 if (reg & DWC3_DSTS_DCNRD)
112 udelay(5);
113 else
114 break;
115 }
116
117 if (retries <= 0)
118 return -ETIMEDOUT;
119 }
120
Felipe Balbi8598bde2012-01-02 18:55:57 +0200121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
122 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
123
124 /* set requested state */
125 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
126 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
127
Paul Zimmerman802fde92012-04-27 13:10:52 +0300128 /*
129 * The following code is racy when called from dwc3_gadget_wakeup,
130 * and is not needed, at least on newer versions
131 */
132 if (dwc->revision >= DWC3_REVISION_194A)
133 return 0;
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300136 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 while (--retries) {
138 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
139
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 if (DWC3_DSTS_USBLNKST(reg) == state)
141 return 0;
142
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800143 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144 }
145
Felipe Balbi73815282015-01-27 13:48:14 -0600146 dwc3_trace(trace_dwc3_gadget,
147 "link state change request timed out");
Felipe Balbi8598bde2012-01-02 18:55:57 +0200148
149 return -ETIMEDOUT;
150}
151
John Youndca01192016-05-19 17:26:05 -0700152/**
153 * dwc3_ep_inc_trb() - Increment a TRB index.
154 * @index - Pointer to the TRB index to increment.
155 *
156 * The index should never point to the link TRB. After incrementing,
157 * if it is point to the link TRB, wrap around to the beginning. The
158 * link TRB is always at the last TRB entry.
159 */
160static void dwc3_ep_inc_trb(u8 *index)
161{
162 (*index)++;
163 if (*index == (DWC3_TRB_NUM - 1))
164 *index = 0;
165}
166
Felipe Balbief966b92016-04-05 13:09:51 +0300167static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200168{
John Youndca01192016-05-19 17:26:05 -0700169 dwc3_ep_inc_trb(&dep->trb_enqueue);
Felipe Balbief966b92016-04-05 13:09:51 +0300170}
Felipe Balbi457e84b2012-01-18 18:04:09 +0200171
Felipe Balbief966b92016-04-05 13:09:51 +0300172static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
Felipe Balbi72246da2011-08-19 18:10:58 +0300177void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
178 int status)
179{
180 struct dwc3 *dwc = dep->dwc;
181
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300182 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300183 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200184 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300185
186 if (req->request.status == -EINPROGRESS)
187 req->request.status = status;
188
Pratyush Anand0416e492012-08-10 13:42:16 +0530189 if (dwc->ep0_bounced && dep->number == 0)
190 dwc->ep0_bounced = false;
191 else
192 usb_gadget_unmap_request(&dwc->gadget, &req->request,
193 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300194
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500195 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300196
197 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200198 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300199 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300200
201 if (dep->number > 1)
202 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +0300203}
204
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500205int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300206{
207 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300208 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300209 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300210 u32 reg;
211
212 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
213 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
214
215 do {
216 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
217 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300218 status = DWC3_DGCMD_STATUS(reg);
219 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300220 ret = -EINVAL;
221 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300222 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300223 } while (timeout--);
224
225 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300226 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300227 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300228 }
229
Felipe Balbi71f7e702016-05-23 14:16:19 +0300230 trace_dwc3_gadget_generic_cmd(cmd, param, status);
231
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300232 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300233}
234
Felipe Balbic36d8e92016-04-04 12:46:33 +0300235static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
236
Felipe Balbi2cd47182016-04-12 16:42:43 +0300237int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
238 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300239{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300240 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700241 u32 timeout = 1500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300242 u32 reg;
243
Felipe Balbi0933df12016-05-23 14:02:33 +0300244 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300245 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300246 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300247
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300248 /*
249 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
250 * we're issuing an endpoint command, we must check if
251 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
252 *
253 * We will also set SUSPHY bit to what it was before returning as stated
254 * by the same section on Synopsys databook.
255 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300256 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
257 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
258 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
259 susphy = true;
260 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
261 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
262 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300263 }
264
Felipe Balbic36d8e92016-04-04 12:46:33 +0300265 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
266 int needs_wakeup;
267
268 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
269 dwc->link_state == DWC3_LINK_STATE_U2 ||
270 dwc->link_state == DWC3_LINK_STATE_U3);
271
272 if (unlikely(needs_wakeup)) {
273 ret = __dwc3_gadget_wakeup(dwc);
274 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
275 ret);
276 }
277 }
278
Felipe Balbi2eb88012016-04-12 16:53:39 +0300279 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
280 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
281 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300282
Felipe Balbi2eb88012016-04-12 16:53:39 +0300283 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300285 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300286 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300287 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000288
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000289 switch (cmd_status) {
290 case 0:
291 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300292 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000293 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000294 ret = -EINVAL;
295 break;
296 case DEPEVT_TRANSFER_BUS_EXPIRY:
297 /*
298 * SW issues START TRANSFER command to
299 * isochronous ep with future frame interval. If
300 * future interval time has already passed when
301 * core receives the command, it will respond
302 * with an error status of 'Bus Expiry'.
303 *
304 * Instead of always returning -EINVAL, let's
305 * give a hint to the gadget driver that this is
306 * the case by returning -EAGAIN.
307 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000308 ret = -EAGAIN;
309 break;
310 default:
311 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
312 }
313
Felipe Balbic0ca3242016-04-04 09:11:51 +0300314 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300315 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300316 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300317
Felipe Balbif6bb2252016-05-23 13:53:34 +0300318 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300319 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700320 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
321 dev_err(dwc->dev, "%s command timeout for %s\n",
322 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Felipe Balbi0933df12016-05-23 14:02:33 +0300323 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300324 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300325
Felipe Balbi0933df12016-05-23 14:02:33 +0300326 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
327
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300328 if (unlikely(susphy)) {
329 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
330 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
331 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
332 }
333
Felipe Balbic0ca3242016-04-04 09:11:51 +0300334 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300335}
336
John Youn50c763f2016-05-31 17:49:56 -0700337static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
338{
339 struct dwc3 *dwc = dep->dwc;
340 struct dwc3_gadget_ep_cmd_params params;
341 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
342
343 /*
344 * As of core revision 2.60a the recommended programming model
345 * is to set the ClearPendIN bit when issuing a Clear Stall EP
346 * command for IN endpoints. This is to prevent an issue where
347 * some (non-compliant) hosts may not send ACK TPs for pending
348 * IN transfers due to a mishandled error condition. Synopsys
349 * STAR 9000614252.
350 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800351 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
352 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700353 cmd |= DWC3_DEPCMD_CLEARPENDIN;
354
355 memset(&params, 0, sizeof(params));
356
Felipe Balbi2cd47182016-04-12 16:42:43 +0300357 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700358}
359
Felipe Balbi72246da2011-08-19 18:10:58 +0300360static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
361{
362 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700363 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300364
365 if (dep->trb_pool)
366 return 0;
367
Mayank Ranaa99689a2016-08-10 17:39:47 -0700368 dep->trb_pool = dma_zalloc_coherent(dwc->dev,
369 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 &dep->trb_pool_dma, GFP_KERNEL);
371 if (!dep->trb_pool) {
372 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
373 dep->name);
374 return -ENOMEM;
375 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700376 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300377
378 return 0;
379}
380
381static void dwc3_free_trb_pool(struct dwc3_ep *dep)
382{
383 struct dwc3 *dwc = dep->dwc;
384
Mayank Ranaa99689a2016-08-10 17:39:47 -0700385 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
386 if (dep->endpoint.ep_type == EP_TYPE_GSI)
387 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300388
Mayank Rana4dd882c2016-10-05 09:43:05 -0700389 /*
390 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
391 * with HWO bit set from previous composition when update transfer cmd
392 * is issued.
393 */
394 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
395 memset(&dep->trb_pool[0], 0,
396 sizeof(struct dwc3_trb) * dep->num_trbs);
397 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
398
Mayank Ranaa99689a2016-08-10 17:39:47 -0700399 dma_free_coherent(dwc->dev,
400 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
401 dep->trb_pool, dep->trb_pool_dma);
402 dep->trb_pool = NULL;
403 dep->trb_pool_dma = 0;
404 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300405}
406
John Younc4509602016-02-16 20:10:53 -0800407static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
408
409/**
410 * dwc3_gadget_start_config - Configure EP resources
411 * @dwc: pointer to our controller context structure
412 * @dep: endpoint that is being enabled
413 *
414 * The assignment of transfer resources cannot perfectly follow the
415 * data book due to the fact that the controller driver does not have
416 * all knowledge of the configuration in advance. It is given this
417 * information piecemeal by the composite gadget framework after every
418 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
419 * programming model in this scenario can cause errors. For two
420 * reasons:
421 *
422 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
423 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
424 * multiple interfaces.
425 *
426 * 2) The databook does not mention doing more DEPXFERCFG for new
427 * endpoint on alt setting (8.1.6).
428 *
429 * The following simplified method is used instead:
430 *
431 * All hardware endpoints can be assigned a transfer resource and this
432 * setting will stay persistent until either a core reset or
433 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
434 * do DEPXFERCFG for every hardware endpoint as well. We are
435 * guaranteed that there are as many transfer resources as endpoints.
436 *
437 * This function is called for each endpoint when it is being enabled
438 * but is triggered only when called for EP0-out, which always happens
439 * first, and which should only happen in one of the above conditions.
440 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300441static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
442{
443 struct dwc3_gadget_ep_cmd_params params;
444 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800445 int i;
446 int ret;
447
448 if (dep->number)
449 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300450
451 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800452 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300453
Felipe Balbi2cd47182016-04-12 16:42:43 +0300454 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800455 if (ret)
456 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300457
John Younc4509602016-02-16 20:10:53 -0800458 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
459 struct dwc3_ep *dep = dwc->eps[i];
460
461 if (!dep)
462 continue;
463
464 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
465 if (ret)
466 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300467 }
468
469 return 0;
470}
471
472static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200473 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300474 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300475 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300476{
477 struct dwc3_gadget_ep_cmd_params params;
478
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300479 if (dev_WARN_ONCE(dwc->dev, modify && restore,
480 "Can't modify and restore\n"))
481 return -EINVAL;
482
Felipe Balbi72246da2011-08-19 18:10:58 +0300483 memset(&params, 0x00, sizeof(params));
484
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300485 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900486 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
487
488 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800489 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300490 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300491 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900492 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300493
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300494 if (modify) {
495 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
496 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600497 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
498 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300499 } else {
500 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600501 }
502
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300503 if (usb_endpoint_xfer_control(desc))
504 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300505
506 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
507 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300508
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200509 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300510 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
511 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300512 dep->stream_capable = true;
513 }
514
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500515 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300516 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
518 /*
519 * We are doing 1:1 mapping for endpoints, meaning
520 * Physical Endpoints 2 maps to Logical Endpoint 2 and
521 * so on. We consider the direction bit as part of the physical
522 * endpoint number. So USB endpoint 0x81 is 0x03.
523 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300524 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300525
526 /*
527 * We must use the lower 16 TX FIFOs even though
528 * HW might have more
529 */
530 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300531 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300532
533 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300534 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300535 dep->interval = 1 << (desc->bInterval - 1);
536 }
537
Felipe Balbi2cd47182016-04-12 16:42:43 +0300538 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300539}
540
541static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
542{
543 struct dwc3_gadget_ep_cmd_params params;
544
545 memset(&params, 0x00, sizeof(params));
546
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300547 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
Felipe Balbi2cd47182016-04-12 16:42:43 +0300549 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
550 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300551}
552
553/**
554 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
555 * @dep: endpoint to be initialized
556 * @desc: USB Endpoint Descriptor
557 *
558 * Caller should take care of locking
559 */
560static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200561 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300562 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300563 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300564{
565 struct dwc3 *dwc = dep->dwc;
566 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300567 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300568
Felipe Balbi73815282015-01-27 13:48:14 -0600569 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300570
Felipe Balbi72246da2011-08-19 18:10:58 +0300571 if (!(dep->flags & DWC3_EP_ENABLED)) {
572 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700573 if (ret) {
574 dev_err(dwc->dev, "start_config() failed for %s\n",
575 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700577 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300578 }
579
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300580 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600581 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700582 if (ret) {
583 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700585 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300586
587 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200588 struct dwc3_trb *trb_st_hw;
589 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200591 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200592 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 dep->type = usb_endpoint_type(desc);
594 dep->flags |= DWC3_EP_ENABLED;
595
596 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
597 reg |= DWC3_DALEPENA_EP(dep->number);
598 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
599
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300600 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300601 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300602
John Youn0d257442016-05-19 17:26:08 -0700603 /* Initialize the TRB ring */
604 dep->trb_dequeue = 0;
605 dep->trb_enqueue = 0;
606 memset(dep->trb_pool, 0,
607 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
608
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300609 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 trb_st_hw = &dep->trb_pool[0];
611
Felipe Balbif6bafc62012-02-06 11:04:53 +0200612 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200613 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
614 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
615 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
616 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300617 }
618
619 return 0;
620}
621
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200622static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300623{
624 struct dwc3_request *req;
625
Felipe Balbi0e146022016-06-21 10:32:02 +0300626 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300627
Felipe Balbi0e146022016-06-21 10:32:02 +0300628 /* - giveback all requests to gadget driver */
629 while (!list_empty(&dep->started_list)) {
630 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200631
Felipe Balbi0e146022016-06-21 10:32:02 +0300632 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200633 }
634
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200635 while (!list_empty(&dep->pending_list)) {
636 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300637
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200638 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300639 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300640}
641
642/**
643 * __dwc3_gadget_ep_disable - Disables a HW endpoint
644 * @dep: the endpoint to disable
645 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200646 * This function also removes requests which are currently processed ny the
647 * hardware and those which are not yet scheduled.
648 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300650static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
651{
652 struct dwc3 *dwc = dep->dwc;
653 u32 reg;
654
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500655 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
656
Mayank Ranaa99689a2016-08-10 17:39:47 -0700657 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
658 dwc3_remove_requests(dwc, dep);
659 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
660 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300661
Felipe Balbi687ef982014-04-16 10:30:33 -0500662 /* make sure HW endpoint isn't stalled */
663 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500664 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500665
Felipe Balbi72246da2011-08-19 18:10:58 +0300666 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
667 reg &= ~DWC3_DALEPENA_EP(dep->number);
668 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
669
Felipe Balbi879631a2011-09-30 10:58:47 +0300670 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200671 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200672 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300674 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300675
Mayank Ranaa99689a2016-08-10 17:39:47 -0700676 /* Keep GSI ep names with "-gsi" suffix */
677 if (!strnstr(dep->name, "gsi", 10)) {
678 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
679 dep->number >> 1,
680 (dep->number & 1) ? "in" : "out");
681 }
682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 return 0;
684}
685
686/* -------------------------------------------------------------------------- */
687
688static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
689 const struct usb_endpoint_descriptor *desc)
690{
691 return -EINVAL;
692}
693
694static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
695{
696 return -EINVAL;
697}
698
699/* -------------------------------------------------------------------------- */
700
701static int dwc3_gadget_ep_enable(struct usb_ep *ep,
702 const struct usb_endpoint_descriptor *desc)
703{
704 struct dwc3_ep *dep;
705 struct dwc3 *dwc;
706 unsigned long flags;
707 int ret;
708
709 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
710 pr_debug("dwc3: invalid parameters\n");
711 return -EINVAL;
712 }
713
714 if (!desc->wMaxPacketSize) {
715 pr_debug("dwc3: missing wMaxPacketSize\n");
716 return -EINVAL;
717 }
718
719 dep = to_dwc3_ep(ep);
720 dwc = dep->dwc;
721
Felipe Balbi95ca9612015-12-10 13:08:20 -0600722 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
723 "%s is already enabled\n",
724 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300725 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300726
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600728 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 spin_unlock_irqrestore(&dwc->lock, flags);
730
731 return ret;
732}
733
734static int dwc3_gadget_ep_disable(struct usb_ep *ep)
735{
736 struct dwc3_ep *dep;
737 struct dwc3 *dwc;
738 unsigned long flags;
739 int ret;
740
741 if (!ep) {
742 pr_debug("dwc3: invalid parameters\n");
743 return -EINVAL;
744 }
745
746 dep = to_dwc3_ep(ep);
747 dwc = dep->dwc;
748
Felipe Balbi95ca9612015-12-10 13:08:20 -0600749 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
750 "%s is already disabled\n",
751 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300753
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 spin_lock_irqsave(&dwc->lock, flags);
755 ret = __dwc3_gadget_ep_disable(dep);
756 spin_unlock_irqrestore(&dwc->lock, flags);
757
758 return ret;
759}
760
761static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
762 gfp_t gfp_flags)
763{
764 struct dwc3_request *req;
765 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300766
767 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900768 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300769 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300770
771 req->epnum = dep->number;
772 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300773
Felipe Balbi68d34c82016-05-30 13:34:58 +0300774 dep->allocated_requests++;
775
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500776 trace_dwc3_alloc_request(req);
777
Felipe Balbi72246da2011-08-19 18:10:58 +0300778 return &req->request;
779}
780
781static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
782 struct usb_request *request)
783{
784 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300785 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300786
Felipe Balbi68d34c82016-05-30 13:34:58 +0300787 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500788 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 kfree(req);
790}
791
Felipe Balbi2c78c022016-08-12 13:13:10 +0300792static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
793
Felipe Balbic71fc372011-11-22 11:37:34 +0200794/**
795 * dwc3_prepare_one_trb - setup one TRB from one request
796 * @dep: endpoint for which this request is prepared
797 * @req: dwc3_request pointer
798 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200799static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200800 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300801 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200802{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200803 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200804
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300805 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200806 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300807 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530808
Felipe Balbi4faf7552016-04-05 13:14:31 +0300809 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200810
Felipe Balbieeb720f2011-11-28 12:46:59 +0200811 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200812 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200813 req->trb = trb;
814 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300815 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300816 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200817 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200818
Felipe Balbief966b92016-04-05 13:09:51 +0300819 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530820
Felipe Balbif6bafc62012-02-06 11:04:53 +0200821 trb->size = DWC3_TRB_SIZE_LENGTH(length);
822 trb->bpl = lower_32_bits(dma);
823 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200824
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200825 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200826 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200827 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200828 break;
829
830 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530831 if (!node)
832 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
833 else
834 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbica4d44e2016-03-10 13:53:27 +0200835
836 /* always enable Interrupt on Missed ISOC */
837 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838 break;
839
840 case USB_ENDPOINT_XFER_BULK:
841 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200842 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200843 break;
844 default:
845 /*
846 * This is only possible with faulty memory because we
847 * checked it already :)
848 */
849 BUG();
850 }
851
Felipe Balbica4d44e2016-03-10 13:53:27 +0200852 /* always enable Continue on Short Packet */
853 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -0600854
Felipe Balbi2c78c022016-08-12 13:13:10 +0300855 if ((!req->request.no_interrupt && !chain) ||
856 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbica4d44e2016-03-10 13:53:27 +0200857 trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI;
858
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530859 if (chain)
860 trb->ctrl |= DWC3_TRB_CTRL_CHN;
861
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200862 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200863 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
864
865 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500866
867 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +0200868}
869
John Youn361572b2016-05-19 17:26:17 -0700870/**
871 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
872 * @dep: The endpoint with the TRB ring
873 * @index: The index of the current TRB in the ring
874 *
875 * Returns the TRB prior to the one pointed to by the index. If the
876 * index is 0, we will wrap backwards, skip the link TRB, and return
877 * the one just before that.
878 */
879static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
880{
Felipe Balbi45438a02016-08-11 12:26:59 +0300881 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -0700882
Felipe Balbi45438a02016-08-11 12:26:59 +0300883 if (!tmp)
884 tmp = DWC3_TRB_NUM - 1;
885
886 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -0700887}
888
Felipe Balbic4233572016-05-12 14:08:34 +0300889static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
890{
891 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -0700892 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300893
894 /*
895 * If enqueue & dequeue are equal than it is either full or empty.
896 *
897 * One way to know for sure is if the TRB right before us has HWO bit
898 * set or not. If it has, then we're definitely full and can't fit any
899 * more transfers in our ring.
900 */
901 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -0700902 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
903 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
904 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +0300905
906 return DWC3_TRB_NUM - 1;
907 }
908
John Youn9d7aba72016-08-26 18:43:01 -0700909 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -0700910 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -0700911
John Youn9d7aba72016-08-26 18:43:01 -0700912 if (dep->trb_dequeue < dep->trb_enqueue)
913 trbs_left--;
914
John Youn32db3d92016-05-19 17:26:12 -0700915 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +0300916}
917
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300918static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300919 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300920{
Felipe Balbi1f512112016-08-12 13:17:27 +0300921 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300922 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300923 unsigned int length;
924 dma_addr_t dma;
925 int i;
926
Felipe Balbi1f512112016-08-12 13:17:27 +0300927 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300928 unsigned chain = true;
929
930 length = sg_dma_len(s);
931 dma = sg_dma_address(s);
932
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300933 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300934 chain = false;
935
936 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300937 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300938
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300939 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300940 break;
941 }
942}
943
944static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300945 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300946{
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300947 unsigned int length;
948 dma_addr_t dma;
949
950 dma = req->request.dma;
951 length = req->request.length;
952
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300953 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300954 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300955}
956
Felipe Balbi72246da2011-08-19 18:10:58 +0300957/*
958 * dwc3_prepare_trbs - setup TRBs from requests
959 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +0300960 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800961 * The function goes through the requests list and sets up TRBs for the
962 * transfers. The function returns once there are no more TRBs available or
963 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 */
Felipe Balbic4233572016-05-12 14:08:34 +0300965static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300966{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200967 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300968
969 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
970
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300971 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -0700972 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300973
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200974 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +0300975 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300976 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300977 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300978 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300979
Felipe Balbi7ae7df42016-08-24 14:37:22 +0300980 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +0300981 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300982 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300983}
984
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300985static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +0300986{
987 struct dwc3_gadget_ep_cmd_params params;
988 struct dwc3_request *req;
989 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300990 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 int ret;
992 u32 cmd;
993
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300994 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995
Felipe Balbi4fae2e32016-05-12 16:53:59 +0300996 dwc3_prepare_trbs(dep);
997 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300998 if (!req) {
999 dep->flags |= DWC3_EP_PENDING_REQUEST;
1000 return 0;
1001 }
1002
1003 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001004
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001005 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301006 params.param0 = upper_32_bits(req->trb_dma);
1007 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001008 cmd = DWC3_DEPCMD_STARTTRANSFER |
1009 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301010 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001011 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1012 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301013 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001014
Felipe Balbi2cd47182016-04-12 16:42:43 +03001015 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001016 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001017 /*
1018 * FIXME we need to iterate over the list of requests
1019 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001020 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001021 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001022 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1023 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001024 list_del(&req->list);
1025 return ret;
1026 }
1027
1028 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001029
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001030 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001031 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001032 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001033 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001034
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 return 0;
1036}
1037
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301038static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1039 struct dwc3_ep *dep, u32 cur_uf)
1040{
1041 u32 uf;
1042
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001043 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001044 dwc3_trace(trace_dwc3_gadget,
1045 "ISOC ep %s run out for requests",
1046 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301047 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301048 return;
1049 }
1050
1051 /* 4 micro frames in the future */
1052 uf = cur_uf + dep->interval * 4;
1053
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001054 __dwc3_gadget_kick_transfer(dep, uf);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301055}
1056
1057static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1058 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1059{
1060 u32 cur_uf, mask;
1061
1062 mask = ~(dep->interval - 1);
1063 cur_uf = event->parameters & mask;
1064
1065 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1066}
1067
Felipe Balbi72246da2011-08-19 18:10:58 +03001068static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1069{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001070 struct dwc3 *dwc = dep->dwc;
1071 int ret;
1072
Felipe Balbibb423982015-11-16 15:31:21 -06001073 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001074 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001075 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001076 &req->request, dep->endpoint.name);
1077 return -ESHUTDOWN;
1078 }
1079
1080 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1081 &req->request, req->dep->name)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001082 dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001083 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001084 return -EINVAL;
1085 }
1086
Felipe Balbifc8bb912016-05-16 13:14:48 +03001087 pm_runtime_get(dwc->dev);
1088
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 req->request.actual = 0;
1090 req->request.status = -EINPROGRESS;
1091 req->direction = dep->direction;
1092 req->epnum = dep->number;
1093
Felipe Balbife84f522015-09-01 09:01:38 -05001094 trace_dwc3_ep_queue(req);
1095
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001096 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1097 dep->direction);
1098 if (ret)
1099 return ret;
1100
Felipe Balbi1f512112016-08-12 13:17:27 +03001101 req->sg = req->request.sg;
1102 req->num_pending_sgs = req->request.num_mapped_sgs;
1103
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001104 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001105
Felipe Balbid889c232016-09-29 15:44:29 +03001106 /*
1107 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1108 * wait for a XferNotReady event so we will know what's the current
1109 * (micro-)frame number.
1110 *
1111 * Without this trick, we are very, very likely gonna get Bus Expiry
1112 * errors which will force us issue EndTransfer command.
1113 */
1114 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1115 if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1116 list_empty(&dep->started_list)) {
Felipe Balbi08a36b52016-08-11 14:27:52 +03001117 dwc3_stop_active_transfer(dwc, dep->number, true);
1118 dep->flags = DWC3_EP_ENABLED;
1119 }
1120 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001121 }
1122
Felipe Balbi594e1212016-08-24 14:38:10 +03001123 if (!dwc3_calc_trbs_left(dep))
1124 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001125
Felipe Balbi08a36b52016-08-11 14:27:52 +03001126 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001127 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001128 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001129 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001130 dep->name);
1131 if (ret == -EBUSY)
1132 ret = 0;
1133
1134 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001135}
1136
Mayank Ranaa99689a2016-08-10 17:39:47 -07001137static int dwc3_gadget_wakeup(struct usb_gadget *g)
1138{
1139 struct dwc3 *dwc = gadget_to_dwc(g);
1140
1141 schedule_work(&dwc->wakeup_work);
1142 return 0;
1143}
1144
1145static inline enum dwc3_link_state dwc3_get_link_state(struct dwc3 *dwc)
1146{
1147 u32 reg;
1148
1149 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1150 return DWC3_DSTS_USBLNKST(reg);
1151}
1152
1153static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1154{
1155 if (atomic_read(&dwc->in_lpm) ||
1156 dwc->link_state == DWC3_LINK_STATE_U3)
1157 return true;
1158 return false;
1159}
1160
Felipe Balbi04c03d12015-12-02 10:06:45 -06001161static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1162 struct usb_request *request)
1163{
1164 dwc3_gadget_ep_free_request(ep, request);
1165}
1166
1167static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1168{
1169 struct dwc3_request *req;
1170 struct usb_request *request;
1171 struct usb_ep *ep = &dep->endpoint;
1172
Felipe Balbi60cfb372016-05-24 13:45:17 +03001173 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001174 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1175 if (!request)
1176 return -ENOMEM;
1177
1178 request->length = 0;
1179 request->buf = dwc->zlp_buf;
1180 request->complete = __dwc3_gadget_ep_zlp_complete;
1181
1182 req = to_dwc3_request(request);
1183
1184 return __dwc3_gadget_ep_queue(dep, req);
1185}
1186
Felipe Balbi72246da2011-08-19 18:10:58 +03001187static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1188 gfp_t gfp_flags)
1189{
1190 struct dwc3_request *req = to_dwc3_request(request);
1191 struct dwc3_ep *dep = to_dwc3_ep(ep);
1192 struct dwc3 *dwc = dep->dwc;
1193
1194 unsigned long flags;
1195
1196 int ret;
1197
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001198 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001199 if (!dep->endpoint.desc) {
1200 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1201 request, ep->name);
1202 ret = -ESHUTDOWN;
1203 goto out;
1204 }
1205
1206 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1207 request, req->dep->name)) {
1208 ret = -EINVAL;
1209 goto out;
1210 }
1211
1212 if (dwc3_gadget_is_suspended(dwc)) {
1213 if (dwc->gadget.remote_wakeup)
1214 dwc3_gadget_wakeup(&dwc->gadget);
1215 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1216 goto out;
1217 }
1218
Felipe Balbi72246da2011-08-19 18:10:58 +03001219 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001220
1221 /*
1222 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1223 * setting request->zero, instead of doing magic, we will just queue an
1224 * extra usb_request ourselves so that it gets handled the same way as
1225 * any other request.
1226 */
John Yound92618982015-12-22 12:23:20 -08001227 if (ret == 0 && request->zero && request->length &&
1228 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001229 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1230
Mayank Ranaa99689a2016-08-10 17:39:47 -07001231out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 spin_unlock_irqrestore(&dwc->lock, flags);
1233
1234 return ret;
1235}
1236
1237static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1238 struct usb_request *request)
1239{
1240 struct dwc3_request *req = to_dwc3_request(request);
1241 struct dwc3_request *r = NULL;
1242
1243 struct dwc3_ep *dep = to_dwc3_ep(ep);
1244 struct dwc3 *dwc = dep->dwc;
1245
1246 unsigned long flags;
1247 int ret = 0;
1248
Mayank Ranaa99689a2016-08-10 17:39:47 -07001249 if (atomic_read(&dwc->in_lpm)) {
1250 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1251 return -EAGAIN;
1252 }
1253
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001254 trace_dwc3_ep_dequeue(req);
1255
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 spin_lock_irqsave(&dwc->lock, flags);
1257
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001258 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001259 if (r == req)
1260 break;
1261 }
1262
1263 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001264 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001265 if (r == req)
1266 break;
1267 }
1268 if (r == req) {
1269 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001270 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301271 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 }
1273 dev_err(dwc->dev, "request %p was not queued to %s\n",
1274 request, ep->name);
1275 ret = -EINVAL;
1276 goto out0;
1277 }
1278
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301279out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001280 /* giveback the request */
1281 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1282
1283out0:
1284 spin_unlock_irqrestore(&dwc->lock, flags);
1285
1286 return ret;
1287}
1288
Felipe Balbi7a608552014-09-24 14:19:52 -05001289int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001290{
1291 struct dwc3_gadget_ep_cmd_params params;
1292 struct dwc3 *dwc = dep->dwc;
1293 int ret;
1294
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001295 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1296 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1297 return -EINVAL;
1298 }
1299
Felipe Balbi72246da2011-08-19 18:10:58 +03001300 memset(&params, 0x00, sizeof(params));
1301
1302 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001303 struct dwc3_trb *trb;
1304
1305 unsigned transfer_in_flight;
1306 unsigned started;
1307
1308 if (dep->number > 1)
1309 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1310 else
1311 trb = &dwc->ep0_trb[dep->trb_enqueue];
1312
1313 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1314 started = !list_empty(&dep->started_list);
1315
1316 if (!protocol && ((dep->direction && transfer_in_flight) ||
1317 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001318 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001319 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001320 dep->name);
1321 return -EAGAIN;
1322 }
1323
Felipe Balbi2cd47182016-04-12 16:42:43 +03001324 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1325 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001327 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001328 dep->name);
1329 else
1330 dep->flags |= DWC3_EP_STALL;
1331 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001332
John Youn50c763f2016-05-31 17:49:56 -07001333 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001335 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001336 dep->name);
1337 else
Alan Sterna535d812013-11-01 12:05:12 -04001338 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001340
Felipe Balbi72246da2011-08-19 18:10:58 +03001341 return ret;
1342}
1343
1344static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1345{
1346 struct dwc3_ep *dep = to_dwc3_ep(ep);
1347 struct dwc3 *dwc = dep->dwc;
1348
1349 unsigned long flags;
1350
1351 int ret;
1352
Mayank Ranaa99689a2016-08-10 17:39:47 -07001353 if (!ep->desc) {
1354 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1355 return -EINVAL;
1356 }
1357
Felipe Balbi72246da2011-08-19 18:10:58 +03001358 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001359 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001360 spin_unlock_irqrestore(&dwc->lock, flags);
1361
1362 return ret;
1363}
1364
1365static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1366{
1367 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001368 struct dwc3 *dwc = dep->dwc;
1369 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001370 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001371
Paul Zimmerman249a4562012-02-24 17:32:16 -08001372 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001373 dep->flags |= DWC3_EP_WEDGE;
1374
Pratyush Anand08f0d962012-06-25 22:40:43 +05301375 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001376 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301377 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001378 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001379 spin_unlock_irqrestore(&dwc->lock, flags);
1380
1381 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001382}
1383
1384/* -------------------------------------------------------------------------- */
1385
1386static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1387 .bLength = USB_DT_ENDPOINT_SIZE,
1388 .bDescriptorType = USB_DT_ENDPOINT,
1389 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1390};
1391
1392static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1393 .enable = dwc3_gadget_ep0_enable,
1394 .disable = dwc3_gadget_ep0_disable,
1395 .alloc_request = dwc3_gadget_ep_alloc_request,
1396 .free_request = dwc3_gadget_ep_free_request,
1397 .queue = dwc3_gadget_ep0_queue,
1398 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301399 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001400 .set_wedge = dwc3_gadget_ep_set_wedge,
1401};
1402
1403static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1404 .enable = dwc3_gadget_ep_enable,
1405 .disable = dwc3_gadget_ep_disable,
1406 .alloc_request = dwc3_gadget_ep_alloc_request,
1407 .free_request = dwc3_gadget_ep_free_request,
1408 .queue = dwc3_gadget_ep_queue,
1409 .dequeue = dwc3_gadget_ep_dequeue,
1410 .set_halt = dwc3_gadget_ep_set_halt,
1411 .set_wedge = dwc3_gadget_ep_set_wedge,
1412};
1413
1414/* -------------------------------------------------------------------------- */
1415
1416static int dwc3_gadget_get_frame(struct usb_gadget *g)
1417{
1418 struct dwc3 *dwc = gadget_to_dwc(g);
1419 u32 reg;
1420
1421 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1422 return DWC3_DSTS_SOFFN(reg);
1423}
1424
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001425static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001426{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001427 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001428
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001429 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 u32 reg;
1431
Felipe Balbi72246da2011-08-19 18:10:58 +03001432 u8 link_state;
1433 u8 speed;
1434
Felipe Balbi72246da2011-08-19 18:10:58 +03001435 /*
1436 * According to the Databook Remote wakeup request should
1437 * be issued only when the device is in early suspend state.
1438 *
1439 * We can check that via USB Link State bits in DSTS register.
1440 */
1441 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1442
1443 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001444 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1445 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001446 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001447 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001448 }
1449
1450 link_state = DWC3_DSTS_USBLNKST(reg);
1451
1452 switch (link_state) {
1453 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1454 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1455 break;
1456 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001457 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001458 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001459 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001460 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 }
1462
Felipe Balbi8598bde2012-01-02 18:55:57 +02001463 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1464 if (ret < 0) {
1465 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001466 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001467 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001468
Paul Zimmerman802fde92012-04-27 13:10:52 +03001469 /* Recent versions do this automatically */
1470 if (dwc->revision < DWC3_REVISION_194A) {
1471 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001472 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001473 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1474 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1475 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001476
Paul Zimmerman1d046792012-02-15 18:56:56 -08001477 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001478 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001479
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001480 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001481 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1482
1483 /* in HS, means ON */
1484 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1485 break;
1486 }
1487
1488 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1489 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001490 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001491 }
1492
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001493 return 0;
1494}
1495
Mayank Ranaa99689a2016-08-10 17:39:47 -07001496#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1497#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1498
1499static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001500{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001501 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001502 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001503 static int retry_count;
1504
1505 dwc = container_of(w, struct dwc3, wakeup_work);
1506
1507 ret = pm_runtime_get_sync(dwc->dev);
1508 if (ret) {
1509 /* pm_runtime_get_sync returns -EACCES error between
1510 * late_suspend and early_resume, wait for system resume to
1511 * finish and queue work again
1512 */
1513 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1514 if (ret == -EACCES) {
1515 pm_runtime_put_noidle(dwc->dev);
1516 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1517 retry_count = 0;
1518 pr_err("pm_runtime_get_sync timed out\n");
1519 return;
1520 }
1521 msleep(DWC3_PM_RESUME_DELAY);
1522 retry_count++;
1523 schedule_work(&dwc->wakeup_work);
1524 return;
1525 }
1526 }
1527 retry_count = 0;
1528
1529 ret = dwc3_gadget_wakeup_int(dwc);
1530
1531 if (ret)
1532 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1533 else
1534 pr_debug("Remote wakeup succeeded.\n");
1535
1536 pm_runtime_put_noidle(dwc->dev);
1537}
1538
1539static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1540{
1541 bool link_recover_only = false;
1542
1543 u32 reg;
1544 int ret = 0;
1545 u8 link_state;
1546 unsigned long flags;
1547
1548 pr_debug("%s(): Entry\n", __func__);
1549 disable_irq(dwc->irq);
1550 spin_lock_irqsave(&dwc->lock, flags);
1551 /*
1552 * According to the Databook Remote wakeup request should
1553 * be issued only when the device is in early suspend state.
1554 *
1555 * We can check that via USB Link State bits in DSTS register.
1556 */
1557 link_state = dwc3_get_link_state(dwc);
1558
1559 switch (link_state) {
1560 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1561 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1562 break;
1563 case DWC3_LINK_STATE_U1:
1564 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1565 link_recover_only = true;
1566 break;
1567 }
1568 /* Intentional fallthrough */
1569 default:
1570 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1571 link_state);
1572 ret = -EINVAL;
1573 goto out;
1574 }
1575
1576 /* Enable LINK STATUS change event */
1577 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1578 reg |= DWC3_DEVTEN_ULSTCNGEN;
1579 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1580 /*
1581 * memory barrier is required to make sure that required events
1582 * with core is enabled before performing RECOVERY mechnism.
1583 */
1584 mb();
1585
1586 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1587 if (ret < 0) {
1588 dev_err(dwc->dev, "failed to put link in Recovery\n");
1589 /* Disable LINK STATUS change */
1590 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1591 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1592 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1593 /* Required to complete this operation before returning */
1594 mb();
1595 goto out;
1596 }
1597
1598 /* Recent versions do this automatically */
1599 if (dwc->revision < DWC3_REVISION_194A) {
1600 /* write zeroes to Link Change Request */
1601 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1602 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1603 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1604 }
1605
1606 spin_unlock_irqrestore(&dwc->lock, flags);
1607 enable_irq(dwc->irq);
1608
1609 /*
1610 * Have bigger value (16 sec) for timeout since some host PCs driving
1611 * resume for very long time (e.g. 8 sec)
1612 */
1613 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1614 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1615 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1616 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001617
1618 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001619 /* Disable link status change event */
1620 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1621 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1622 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1623 /*
1624 * Complete this write before we go ahead and perform resume
1625 * as we don't need link status change notificaiton anymore.
1626 */
1627 mb();
1628
1629 if (!ret) {
1630 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1631 dwc->link_state);
1632 ret = -EINVAL;
1633 spin_unlock_irqrestore(&dwc->lock, flags);
1634 goto out1;
1635 } else {
1636 ret = 0;
1637 /*
1638 * If USB is disconnected OR received RESET from host,
1639 * don't perform resume
1640 */
1641 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1642 dwc->gadget.state == USB_STATE_DEFAULT)
1643 link_recover_only = true;
1644 }
1645
1646 /*
1647 * According to DWC3 databook, the controller does not
1648 * trigger a wakeup event when remote-wakeup is used.
1649 * Hence, after remote-wakeup sequence is complete, and
1650 * the device is back at U0 state, it is required that
1651 * the resume sequence is initiated by SW.
1652 */
1653 if (!link_recover_only)
1654 dwc3_gadget_wakeup_interrupt(dwc, true);
1655
Felipe Balbi72246da2011-08-19 18:10:58 +03001656 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001657 pr_debug("%s: Exit\n", __func__);
1658 return ret;
1659
1660out:
1661 spin_unlock_irqrestore(&dwc->lock, flags);
1662 enable_irq(dwc->irq);
1663
1664out1:
1665 return ret;
1666}
1667
1668static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1669{
1670 int ret = 0;
1671 struct dwc3 *dwc = gadget_to_dwc(g);
1672
1673 if (!g || (g->speed != USB_SPEED_SUPER))
1674 return -ENOTSUPP;
1675
1676 if (dwc3_gadget_is_suspended(dwc)) {
1677 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1678 dwc3_gadget_wakeup(&dwc->gadget);
1679 return -EAGAIN;
1680 }
1681
1682 if (dwc->revision < DWC3_REVISION_220A) {
1683 ret = dwc3_send_gadget_generic_command(dwc,
1684 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1685 } else {
1686 ret = dwc3_send_gadget_generic_command(dwc,
1687 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1688 }
1689
1690 if (ret)
1691 pr_err("Function wakeup HW command failed.\n");
1692 else
1693 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001694
1695 return ret;
1696}
1697
1698static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1699 int is_selfpowered)
1700{
1701 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001702 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001703
Paul Zimmerman249a4562012-02-24 17:32:16 -08001704 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001705 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001706 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001707
1708 return 0;
1709}
1710
Mayank Ranaa99689a2016-08-10 17:39:47 -07001711#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001712static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001713{
1714 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001715 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001716
Felipe Balbi72246da2011-08-19 18:10:58 +03001717 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001718 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001719 if (dwc->revision <= DWC3_REVISION_187A) {
1720 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1721 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1722 }
1723
1724 if (dwc->revision >= DWC3_REVISION_194A)
1725 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001726
1727 dwc3_event_buffers_setup(dwc);
1728 dwc3_gadget_restart(dwc);
1729 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001730 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001731
1732 if (dwc->has_hibernation)
1733 reg |= DWC3_DCTL_KEEP_CONNECT;
1734
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001735 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001736 } else {
Mayank Ranaa99689a2016-08-10 17:39:47 -07001737 dwc3_gadget_disable_irq(dwc);
1738 __dwc3_gadget_ep_disable(dwc->eps[0]);
1739 __dwc3_gadget_ep_disable(dwc->eps[1]);
1740
Felipe Balbi72246da2011-08-19 18:10:58 +03001741 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001742
1743 if (dwc->has_hibernation && !suspend)
1744 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1745
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001746 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001747 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001748
1749 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1750
1751 do {
1752 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001753 reg &= DWC3_DSTS_DEVCTRLHLT;
1754 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001755
Mayank Ranaa99689a2016-08-10 17:39:47 -07001756 if (!timeout) {
1757 dev_err(dwc->dev, "failed to %s controller\n",
1758 is_on ? "start" : "stop");
Felipe Balbif2df6792016-06-09 16:31:34 +03001759 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001760 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001761
Felipe Balbi73815282015-01-27 13:48:14 -06001762 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 dwc->gadget_driver
1764 ? dwc->gadget_driver->function : "no-function",
1765 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301766
1767 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001768}
1769
Mayank Ranaa99689a2016-08-10 17:39:47 -07001770static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
1771{
1772 struct dwc3 *dwc = gadget_to_dwc(g);
1773
1774 dwc->vbus_draw = mA;
1775 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
1776 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT);
1777 return 0;
1778}
1779
Felipe Balbi72246da2011-08-19 18:10:58 +03001780static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1781{
1782 struct dwc3 *dwc = gadget_to_dwc(g);
1783 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301784 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001785
1786 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001787 dwc->softconnect = is_on;
1788 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
1789 /*
1790 * Need to wait for vbus_session(on) from otg driver or to
1791 * the udc_start.
1792 */
1793 return 0;
1794 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001795
Mayank Ranaa99689a2016-08-10 17:39:47 -07001796 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001797 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001798 /*
1799 * If we are here after bus suspend notify otg state machine to
1800 * increment pm usage count of dwc to prevent pm_runtime_suspend
1801 * during enumeration.
1802 */
1803 dwc->b_suspend = false;
1804 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001805 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001806 spin_unlock_irqrestore(&dwc->lock, flags);
1807
Mayank Ranaa99689a2016-08-10 17:39:47 -07001808 pm_runtime_mark_last_busy(dwc->dev);
1809 pm_runtime_put_autosuspend(dwc->dev);
1810
Pratyush Anand6f17f742012-07-02 10:21:55 +05301811 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001812}
1813
Mayank Ranaa99689a2016-08-10 17:39:47 -07001814void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001815{
1816 u32 reg;
1817
1818 /* Enable all but Start and End of Frame IRQs */
1819 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1820 DWC3_DEVTEN_EVNTOVERFLOWEN |
1821 DWC3_DEVTEN_CMDCMPLTEN |
1822 DWC3_DEVTEN_ERRTICERREN |
1823 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001824 DWC3_DEVTEN_CONNECTDONEEN |
1825 DWC3_DEVTEN_USBRSTEN |
1826 DWC3_DEVTEN_DISCONNEVTEN);
1827
Mayank Ranaa99689a2016-08-10 17:39:47 -07001828 /*
1829 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
1830 * else enable USB Link change event (BIT:3) for older version
1831 */
1832 if (dwc->revision < DWC3_REVISION_230A)
1833 reg |= DWC3_DEVTEN_ULSTCNGEN;
1834 else
1835 reg |= DWC3_DEVTEN_SUSPEND;
1836
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001837 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1838}
1839
Mayank Ranaa99689a2016-08-10 17:39:47 -07001840void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001841{
1842 /* mask all interrupts */
1843 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1844}
1845
Felipe Balbib15a7622011-06-30 16:57:15 +03001846static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001847static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001848
Felipe Balbi4e994722016-05-13 14:09:59 +03001849/**
1850 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
1851 * dwc: pointer to our context structure
1852 *
1853 * The following looks like complex but it's actually very simple. In order to
1854 * calculate the number of packets we can burst at once on OUT transfers, we're
1855 * gonna use RxFIFO size.
1856 *
1857 * To calculate RxFIFO size we need two numbers:
1858 * MDWIDTH = size, in bits, of the internal memory bus
1859 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1860 *
1861 * Given these two numbers, the formula is simple:
1862 *
1863 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1864 *
1865 * 24 bytes is for 3x SETUP packets
1866 * 16 bytes is a clock domain crossing tolerance
1867 *
1868 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1869 */
1870static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1871{
1872 u32 ram2_depth;
1873 u32 mdwidth;
1874 u32 nump;
1875 u32 reg;
1876
1877 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1878 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1879
1880 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1881 nump = min_t(u32, nump, 16);
1882
1883 /* update NumP */
1884 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1885 reg &= ~DWC3_DCFG_NUMP_MASK;
1886 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1887 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1888}
1889
Mayank Ranaa99689a2016-08-10 17:39:47 -07001890static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1891{
1892 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1893 unsigned long flags;
1894
1895 if (!dwc->is_drd)
1896 return -EPERM;
1897
1898 is_active = !!is_active;
1899
1900 spin_lock_irqsave(&dwc->lock, flags);
1901
1902 /* Mark that the vbus was powered */
1903 dwc->vbus_active = is_active;
1904
1905 /*
1906 * Check if upper level usb_gadget_driver was already registered with
1907 * this udc controller driver (if dwc3_gadget_start was called)
1908 */
1909 if (dwc->gadget_driver && dwc->softconnect) {
1910 if (dwc->vbus_active) {
1911 /*
1912 * Both vbus was activated by otg and pullup was
1913 * signaled by the gadget driver.
1914 */
1915 dwc3_gadget_run_stop(dwc, 1, false);
1916 } else {
1917 dwc3_gadget_run_stop(dwc, 0, false);
1918 }
1919 }
1920
1921 /*
1922 * Clearing run/stop bit might occur before disconnect event is seen.
1923 * Make sure to let gadget driver know in that case.
1924 */
1925 if (!dwc->vbus_active) {
1926 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1927 dwc3_gadget_disconnect_interrupt(dwc);
1928 }
1929
1930 spin_unlock_irqrestore(&dwc->lock, flags);
1931 return 0;
1932}
1933
Felipe Balbid7be2952016-05-04 15:49:37 +03001934static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001935{
Felipe Balbi72246da2011-08-19 18:10:58 +03001936 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03001937 int ret = 0;
1938 u32 reg;
1939
Felipe Balbi72246da2011-08-19 18:10:58 +03001940 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1941 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001942
1943 /**
1944 * WORKAROUND: DWC3 revision < 2.20a have an issue
1945 * which would cause metastability state on Run/Stop
1946 * bit if we try to force the IP to USB2-only mode.
1947 *
1948 * Because of that, we cannot configure the IP to any
1949 * speed other than the SuperSpeed
1950 *
1951 * Refers to:
1952 *
1953 * STAR#9000525659: Clock Domain Crossing on DCTL in
1954 * USB 2.0 Mode
1955 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001956 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001957 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001958 } else {
1959 switch (dwc->maximum_speed) {
1960 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07001961 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001962 break;
1963 case USB_SPEED_FULL:
John Youn2da9ad72016-05-20 16:34:26 -07001964 reg |= DWC3_DCFG_FULLSPEED1;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001965 break;
1966 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07001967 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001968 break;
John Youn75808622016-02-05 17:09:13 -08001969 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07001970 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08001971 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001972 default:
John Youn77966eb2016-02-19 17:31:01 -08001973 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07001974 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08001975 /* fall through */
1976 case USB_SPEED_SUPER:
1977 reg |= DWC3_DCFG_SUPERSPEED;
1978 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001979 }
1980 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001981 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1982
Mayank Ranaa99689a2016-08-10 17:39:47 -07001983 /* Programs the number of outstanding pipelined transfer requests
1984 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001985 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07001986 if (dwc->revision >= DWC3_REVISION_270A) {
1987 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
1988 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
1989 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
1990 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
1991 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03001992
Felipe Balbi4e994722016-05-13 14:09:59 +03001993 dwc3_gadget_setup_nump(dwc);
1994
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 /* Start with SuperSpeed Default */
1996 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1997
Mayank Ranaa99689a2016-08-10 17:39:47 -07001998 dwc->delayed_status = false;
1999 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002000 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002001 dep->flags = 0;
2002 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002003 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2004 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002005 if (ret) {
2006 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002007 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002008 }
2009
2010 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002011 dep->flags = 0;
2012 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002013 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2014 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002015 if (ret) {
2016 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002017 __dwc3_gadget_ep_disable(dwc->eps[0]);
2018 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002019 }
2020
2021 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002022 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002023 dwc3_ep0_out_start(dwc);
2024
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002025 dwc3_gadget_enable_irq(dwc);
2026
Felipe Balbid7be2952016-05-04 15:49:37 +03002027 return ret;
2028}
2029
Mayank Ranaa99689a2016-08-10 17:39:47 -07002030/* Required gadget re-initialization before switching to gadget in OTG mode */
2031void dwc3_gadget_restart(struct dwc3 *dwc)
2032{
2033 __dwc3_gadget_start(dwc);
2034}
2035
Felipe Balbid7be2952016-05-04 15:49:37 +03002036static int dwc3_gadget_start(struct usb_gadget *g,
2037 struct usb_gadget_driver *driver)
2038{
2039 struct dwc3 *dwc = gadget_to_dwc(g);
2040 unsigned long flags;
2041 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002042
2043 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002044
Felipe Balbid7be2952016-05-04 15:49:37 +03002045 if (dwc->gadget_driver) {
2046 dev_err(dwc->dev, "%s is already bound to %s\n",
2047 dwc->gadget.name,
2048 dwc->gadget_driver->driver.name);
2049 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002050 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002051 }
2052
2053 dwc->gadget_driver = driver;
2054
Mayank Ranaa99689a2016-08-10 17:39:47 -07002055 /*
2056 * For DRD, this might get called by gadget driver during bootup
2057 * even though host mode might be active. Don't actually perform
2058 * device-specific initialization until device mode is activated.
2059 * In that case dwc3_gadget_restart() will handle it.
2060 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002061 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002062 return 0;
2063
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002064err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002065 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 return ret;
2067}
2068
Felipe Balbid7be2952016-05-04 15:49:37 +03002069static void __dwc3_gadget_stop(struct dwc3 *dwc)
2070{
2071 dwc3_gadget_disable_irq(dwc);
2072 __dwc3_gadget_ep_disable(dwc->eps[0]);
2073 __dwc3_gadget_ep_disable(dwc->eps[1]);
2074}
2075
Felipe Balbi22835b82014-10-17 12:05:12 -05002076static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002077{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002078 struct dwc3 *dwc = gadget_to_dwc(g);
2079 unsigned long flags;
2080
2081 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03002082
2083 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbid7be2952016-05-04 15:49:37 +03002084 __dwc3_gadget_stop(dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002085 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002086 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002087
Mayank Ranaa99689a2016-08-10 17:39:47 -07002088 pm_runtime_mark_last_busy(dwc->dev);
2089 pm_runtime_put_autosuspend(dwc->dev);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002090
Felipe Balbi72246da2011-08-19 18:10:58 +03002091 return 0;
2092}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002093
Mayank Ranaa99689a2016-08-10 17:39:47 -07002094static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2095{
2096 struct dwc3 *dwc = gadget_to_dwc(g);
2097
2098 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION);
2099}
2100
Felipe Balbi72246da2011-08-19 18:10:58 +03002101static const struct usb_gadget_ops dwc3_gadget_ops = {
2102 .get_frame = dwc3_gadget_get_frame,
2103 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002104 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002105 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002106 .vbus_session = dwc3_gadget_vbus_session,
2107 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002108 .pullup = dwc3_gadget_pullup,
2109 .udc_start = dwc3_gadget_start,
2110 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002111 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002112};
2113
2114/* -------------------------------------------------------------------------- */
2115
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002116static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2117 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002118{
2119 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002120 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03002121
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002122 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002123 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002124
Felipe Balbi72246da2011-08-19 18:10:58 +03002125 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002126 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002127 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002128
2129 dep->dwc = dwc;
2130 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002131 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002132 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002133 dwc->eps[epnum] = dep;
2134
2135 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
2136 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002137
Felipe Balbi72246da2011-08-19 18:10:58 +03002138 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002139 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002140
Felipe Balbi73815282015-01-27 13:48:14 -06002141 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002142
Felipe Balbi72246da2011-08-19 18:10:58 +03002143 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002144 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302145 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002146 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2147 if (!epnum)
2148 dwc->gadget.ep0 = &dep->endpoint;
2149 } else {
2150 int ret;
2151
Robert Baldygae117e742013-12-13 12:23:38 +01002152 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002153 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002154 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2155 list_add_tail(&dep->endpoint.ep_list,
2156 &dwc->gadget.ep_list);
2157
2158 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002159 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002161 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002162
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002163 if (epnum == 0 || epnum == 1) {
2164 dep->endpoint.caps.type_control = true;
2165 } else {
2166 dep->endpoint.caps.type_iso = true;
2167 dep->endpoint.caps.type_bulk = true;
2168 dep->endpoint.caps.type_int = true;
2169 }
2170
2171 dep->endpoint.caps.dir_in = !!direction;
2172 dep->endpoint.caps.dir_out = !direction;
2173
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002174 INIT_LIST_HEAD(&dep->pending_list);
2175 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002176 }
2177
2178 return 0;
2179}
2180
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002181static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2182{
2183 int ret;
2184
2185 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2186
2187 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2188 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002189 dwc3_trace(trace_dwc3_gadget,
2190 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002191 return ret;
2192 }
2193
2194 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2195 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002196 dwc3_trace(trace_dwc3_gadget,
2197 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002198 return ret;
2199 }
2200
2201 return 0;
2202}
2203
Felipe Balbi72246da2011-08-19 18:10:58 +03002204static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2205{
2206 struct dwc3_ep *dep;
2207 u8 epnum;
2208
2209 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2210 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002211 if (!dep)
2212 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302213 /*
2214 * Physical endpoints 0 and 1 are special; they form the
2215 * bi-directional USB endpoint 0.
2216 *
2217 * For those two physical endpoints, we don't allocate a TRB
2218 * pool nor do we add them the endpoints list. Due to that, we
2219 * shouldn't do these two operations otherwise we would end up
2220 * with all sorts of bugs when removing dwc3.ko.
2221 */
2222 if (epnum != 0 && epnum != 1) {
2223 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002224 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302225 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002226
2227 kfree(dep);
2228 }
2229}
2230
Felipe Balbi72246da2011-08-19 18:10:58 +03002231/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002232
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302233static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2234 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002235 const struct dwc3_event_depevt *event, int status,
2236 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302237{
2238 unsigned int count;
2239 unsigned int s_pkt = 0;
2240 unsigned int trb_status;
2241
Felipe Balbidc55c672016-08-12 13:20:32 +03002242 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002243
2244 if (req->trb == trb)
2245 dep->queued_requests--;
2246
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002247 trace_dwc3_complete_trb(dep, trb);
2248
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002249 /*
2250 * If we're in the middle of series of chained TRBs and we
2251 * receive a short transfer along the way, DWC3 will skip
2252 * through all TRBs including the last TRB in the chain (the
2253 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2254 * bit and SW has to do it manually.
2255 *
2256 * We're going to do that here to avoid problems of HW trying
2257 * to use bogus TRBs for transfers.
2258 */
2259 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2260 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2261
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302262 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002263 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002264
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302265 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002266 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302267
2268 if (dep->direction) {
2269 if (count) {
2270 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2271 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002272 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002273 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302274 dep->name);
2275 /*
2276 * If missed isoc occurred and there is
2277 * no request queued then issue END
2278 * TRANSFER, so that core generates
2279 * next xfernotready and we will issue
2280 * a fresh START TRANSFER.
2281 * If there are still queued request
2282 * then wait, do not issue either END
2283 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002284 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302285 * giveback.If any future queued request
2286 * is successfully transferred then we
2287 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002288 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302289 */
2290 dep->flags |= DWC3_EP_MISSED_ISOC;
2291 } else {
2292 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2293 dep->name);
2294 status = -ECONNRESET;
2295 }
2296 } else {
2297 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2298 }
2299 } else {
2300 if (count && (event->status & DEPEVT_STATUS_SHORT))
2301 s_pkt = 1;
2302 }
2303
Felipe Balbi7c705df2016-08-10 12:35:30 +03002304 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302305 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002306
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302307 if ((event->status & DEPEVT_STATUS_IOC) &&
2308 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2309 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002310
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302311 return 0;
2312}
2313
Felipe Balbi72246da2011-08-19 18:10:58 +03002314static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2315 const struct dwc3_event_depevt *event, int status)
2316{
Felipe Balbi31162af2016-08-11 14:38:37 +03002317 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002318 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002319 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302320 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002321
Felipe Balbi31162af2016-08-11 14:38:37 +03002322 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002323 unsigned length;
2324 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002325 int chain;
2326
Felipe Balbi1f512112016-08-12 13:17:27 +03002327 length = req->request.length;
2328 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002329 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002330 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002331 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002332 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002333 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002334
Felipe Balbi1f512112016-08-12 13:17:27 +03002335 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002336 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002337
Felipe Balbi1f512112016-08-12 13:17:27 +03002338 req->sg = sg_next(s);
2339 req->num_pending_sgs--;
2340
Felipe Balbi31162af2016-08-11 14:38:37 +03002341 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2342 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002343 if (ret)
2344 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002345 }
2346 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002347 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002348 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002349 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002350 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002351
Felipe Balbic7de5732016-07-29 03:17:58 +03002352 /*
2353 * We assume here we will always receive the entire data block
2354 * which we should receive. Meaning, if we program RX to
2355 * receive 4K but we receive only 2K, we assume that's all we
2356 * should receive and we simply bounce the request back to the
2357 * gadget driver for further processing.
2358 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002359 actual = length - req->request.actual;
2360 req->request.actual = actual;
2361
2362 if (ret && chain && (actual < length) && req->num_pending_sgs)
2363 return __dwc3_gadget_kick_transfer(dep, 0);
2364
Ville Syrjäläd115d702015-08-31 19:48:28 +03002365 dwc3_gadget_giveback(dep, req, status);
2366
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002367 if (ret) {
2368 if ((event->status & DEPEVT_STATUS_IOC) &&
2369 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2370 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002371 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002372 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002373 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002374
Felipe Balbi4cb42212016-05-18 12:37:21 +03002375 /*
2376 * Our endpoint might get disabled by another thread during
2377 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2378 * early on so DWC3_EP_BUSY flag gets cleared
2379 */
2380 if (!dep->endpoint.desc)
2381 return 1;
2382
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302383 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002384 list_empty(&dep->started_list)) {
2385 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302386 /*
2387 * If there is no entry in request list then do
2388 * not issue END TRANSFER now. Just set PENDING
2389 * flag, so that END TRANSFER is issued when an
2390 * entry is added into request list.
2391 */
2392 dep->flags = DWC3_EP_PENDING_REQUEST;
2393 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002394 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302395 dep->flags = DWC3_EP_ENABLED;
2396 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302397 return 1;
2398 }
2399
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002400 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2401 return 0;
2402
Felipe Balbi72246da2011-08-19 18:10:58 +03002403 return 1;
2404}
2405
2406static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002407 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002408{
2409 unsigned status = 0;
2410 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002411 u32 is_xfer_complete;
2412
2413 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002414
2415 if (event->status & DEPEVT_STATUS_BUSERR)
2416 status = -ECONNRESET;
2417
Paul Zimmerman1d046792012-02-15 18:56:56 -08002418 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002419 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002420 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002421 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002422
2423 /*
2424 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2425 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2426 */
2427 if (dwc->revision < DWC3_REVISION_183A) {
2428 u32 reg;
2429 int i;
2430
2431 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002432 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002433
2434 if (!(dep->flags & DWC3_EP_ENABLED))
2435 continue;
2436
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002437 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002438 return;
2439 }
2440
2441 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2442 reg |= dwc->u1u2;
2443 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2444
2445 dwc->u1u2 = 0;
2446 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002447
Felipe Balbi4cb42212016-05-18 12:37:21 +03002448 /*
2449 * Our endpoint might get disabled by another thread during
2450 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2451 * early on so DWC3_EP_BUSY flag gets cleared
2452 */
2453 if (!dep->endpoint.desc)
2454 return;
2455
Felipe Balbie6e709b2015-09-28 15:16:56 -05002456 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002457 int ret;
2458
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002459 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002460 if (!ret || ret == -EBUSY)
2461 return;
2462 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002463}
2464
Felipe Balbi72246da2011-08-19 18:10:58 +03002465static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2466 const struct dwc3_event_depevt *event)
2467{
2468 struct dwc3_ep *dep;
2469 u8 epnum = event->endpoint_number;
2470
2471 dep = dwc->eps[epnum];
2472
Felipe Balbi3336abb2012-06-06 09:19:35 +03002473 if (!(dep->flags & DWC3_EP_ENABLED))
2474 return;
2475
Felipe Balbi72246da2011-08-19 18:10:58 +03002476 if (epnum == 0 || epnum == 1) {
2477 dwc3_ep0_interrupt(dwc, event);
2478 return;
2479 }
2480
2481 switch (event->endpoint_event) {
2482 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002483 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002484
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002485 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002486 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002487 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002488 dep->name);
2489 return;
2490 }
2491
Jingoo Han029d97f2014-07-04 15:00:51 +09002492 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002493 break;
2494 case DWC3_DEPEVT_XFERINPROGRESS:
Jingoo Han029d97f2014-07-04 15:00:51 +09002495 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002496 break;
2497 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002498 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002499 dwc3_gadget_start_isoc(dwc, dep, event);
2500 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002501 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002502 int ret;
2503
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002504 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2505
Felipe Balbi73815282015-01-27 13:48:14 -06002506 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002507 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 : "Transfer Not Active");
2509
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002510 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002511 if (!ret || ret == -EBUSY)
2512 return;
2513
Felipe Balbiec5e7952015-11-16 16:04:13 -06002514 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002515 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002516 dep->name);
2517 }
2518
2519 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002520 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002521 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002522 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2523 dep->name);
2524 return;
2525 }
2526
2527 switch (event->status) {
2528 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002529 dwc3_trace(trace_dwc3_gadget,
2530 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002531 event->parameters);
2532
2533 break;
2534 case DEPEVT_STREAMEVT_NOTFOUND:
2535 /* FALLTHROUGH */
2536 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002537 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002538 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002539 }
2540 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002541 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002542 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +03002543 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002544 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002545 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03002546 break;
2547 }
2548}
2549
2550static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2551{
2552 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2553 spin_unlock(&dwc->lock);
2554 dwc->gadget_driver->disconnect(&dwc->gadget);
2555 spin_lock(&dwc->lock);
2556 }
2557}
2558
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002559static void dwc3_suspend_gadget(struct dwc3 *dwc)
2560{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002561 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002562 spin_unlock(&dwc->lock);
2563 dwc->gadget_driver->suspend(&dwc->gadget);
2564 spin_lock(&dwc->lock);
2565 }
2566}
2567
2568static void dwc3_resume_gadget(struct dwc3 *dwc)
2569{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002570 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002571 spin_unlock(&dwc->lock);
2572 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002573 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002574 }
2575}
2576
2577static void dwc3_reset_gadget(struct dwc3 *dwc)
2578{
2579 if (!dwc->gadget_driver)
2580 return;
2581
2582 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2583 spin_unlock(&dwc->lock);
2584 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002585 spin_lock(&dwc->lock);
2586 }
2587}
2588
Mayank Ranaa99689a2016-08-10 17:39:47 -07002589void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002590{
2591 struct dwc3_ep *dep;
2592 struct dwc3_gadget_ep_cmd_params params;
2593 u32 cmd;
2594 int ret;
2595
2596 dep = dwc->eps[epnum];
2597
Felipe Balbib4996a82012-06-06 12:04:13 +03002598 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302599 return;
2600
Pratyush Anand57911502012-07-06 15:19:10 +05302601 /*
2602 * NOTICE: We are violating what the Databook says about the
2603 * EndTransfer command. Ideally we would _always_ wait for the
2604 * EndTransfer Command Completion IRQ, but that's causing too
2605 * much trouble synchronizing between us and gadget driver.
2606 *
2607 * We have discussed this with the IP Provider and it was
2608 * suggested to giveback all requests here, but give HW some
2609 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002610 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302611 *
2612 * Note also that a similar handling was tested by Synopsys
2613 * (thanks a lot Paul) and nothing bad has come out of it.
2614 * In short, what we're doing is:
2615 *
2616 * - Issue EndTransfer WITH CMDIOC bit set
2617 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002618 *
2619 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2620 * supports a mode to work around the above limitation. The
2621 * software can poll the CMDACT bit in the DEPCMD register
2622 * after issuing a EndTransfer command. This mode is enabled
2623 * by writing GUCTL2[14]. This polling is already done in the
2624 * dwc3_send_gadget_ep_cmd() function so if the mode is
2625 * enabled, the EndTransfer command will have completed upon
2626 * returning from this function and we don't need to delay for
2627 * 100us.
2628 *
2629 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302630 */
2631
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302632 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002633 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2634 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002635 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302636 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002637 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302638 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002639 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002640 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002641
2642 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2643 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002644}
2645
2646static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2647{
2648 u32 epnum;
2649
2650 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2651 struct dwc3_ep *dep;
2652
2653 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002654 if (!dep)
2655 continue;
2656
Felipe Balbi72246da2011-08-19 18:10:58 +03002657 if (!(dep->flags & DWC3_EP_ENABLED))
2658 continue;
2659
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002660 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002661 }
2662}
2663
2664static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2665{
2666 u32 epnum;
2667
2668 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2669 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002670 int ret;
2671
2672 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002673 if (!dep)
2674 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002675
2676 if (!(dep->flags & DWC3_EP_STALL))
2677 continue;
2678
2679 dep->flags &= ~DWC3_EP_STALL;
2680
John Youn50c763f2016-05-31 17:49:56 -07002681 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 WARN_ON_ONCE(ret);
2683 }
2684}
2685
2686static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2687{
Felipe Balbic4430a22012-05-24 10:30:01 +03002688 int reg;
2689
Mayank Ranaa99689a2016-08-10 17:39:47 -07002690 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2691 dwc->b_suspend = false;
2692 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2693
Felipe Balbi72246da2011-08-19 18:10:58 +03002694 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2695 reg &= ~DWC3_DCTL_INITU1ENA;
2696 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2697
2698 reg &= ~DWC3_DCTL_INITU2ENA;
2699 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002700
Felipe Balbi72246da2011-08-19 18:10:58 +03002701 dwc3_disconnect_gadget(dwc);
2702
2703 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002704 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002705 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002706 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002707
2708 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002709 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002710}
2711
Felipe Balbi72246da2011-08-19 18:10:58 +03002712static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2713{
2714 u32 reg;
2715
Felipe Balbifc8bb912016-05-16 13:14:48 +03002716 dwc->connected = true;
2717
Felipe Balbidf62df52011-10-14 15:11:49 +03002718 /*
2719 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2720 * would cause a missing Disconnect Event if there's a
2721 * pending Setup Packet in the FIFO.
2722 *
2723 * There's no suggested workaround on the official Bug
2724 * report, which states that "unless the driver/application
2725 * is doing any special handling of a disconnect event,
2726 * there is no functional issue".
2727 *
2728 * Unfortunately, it turns out that we _do_ some special
2729 * handling of a disconnect event, namely complete all
2730 * pending transfers, notify gadget driver of the
2731 * disconnection, and so on.
2732 *
2733 * Our suggested workaround is to follow the Disconnect
2734 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002735 * flag. Such flag gets set whenever we have a SETUP_PENDING
2736 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002737 * same endpoint.
2738 *
2739 * Refers to:
2740 *
2741 * STAR#9000466709: RTL: Device : Disconnect event not
2742 * generated if setup packet pending in FIFO
2743 */
2744 if (dwc->revision < DWC3_REVISION_188A) {
2745 if (dwc->setup_packet_pending)
2746 dwc3_gadget_disconnect_interrupt(dwc);
2747 }
2748
Mayank Ranaa99689a2016-08-10 17:39:47 -07002749 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2750 dwc->b_suspend = false;
2751 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2752
2753 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07002754 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002755
Felipe Balbi8e744752014-11-06 14:27:53 +08002756 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002757
2758 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2759 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2760 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002761 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002762
2763 dwc3_stop_active_transfers(dwc);
2764 dwc3_clear_stall_all_ep(dwc);
2765
2766 /* Reset device address to zero */
2767 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2768 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2769 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002770
2771 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2772 dwc->link_state = DWC3_LINK_STATE_U0;
2773 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002774}
2775
2776static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2777{
2778 u32 reg;
2779 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2780
2781 /*
2782 * We change the clock only at SS but I dunno why I would want to do
2783 * this. Maybe it becomes part of the power saving plan.
2784 */
2785
John Younee5cd412016-02-05 17:08:45 -08002786 if ((speed != DWC3_DSTS_SUPERSPEED) &&
2787 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03002788 return;
2789
2790 /*
2791 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2792 * each time on Connect Done.
2793 */
2794 if (!usb30_clock)
2795 return;
2796
2797 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2798 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2799 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2800}
2801
Felipe Balbi72246da2011-08-19 18:10:58 +03002802static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2803{
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 struct dwc3_ep *dep;
2805 int ret;
2806 u32 reg;
2807 u8 speed;
2808
Felipe Balbi72246da2011-08-19 18:10:58 +03002809 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2810 speed = reg & DWC3_DSTS_CONNECTSPD;
2811 dwc->speed = speed;
2812
2813 dwc3_update_ram_clk_sel(dwc, speed);
2814
2815 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07002816 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08002817 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2818 dwc->gadget.ep0->maxpacket = 512;
2819 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2820 break;
John Youn2da9ad72016-05-20 16:34:26 -07002821 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002822 /*
2823 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2824 * would cause a missing USB3 Reset event.
2825 *
2826 * In such situations, we should force a USB3 Reset
2827 * event by calling our dwc3_gadget_reset_interrupt()
2828 * routine.
2829 *
2830 * Refers to:
2831 *
2832 * STAR#9000483510: RTL: SS : USB3 reset event may
2833 * not be generated always when the link enters poll
2834 */
2835 if (dwc->revision < DWC3_REVISION_190A)
2836 dwc3_gadget_reset_interrupt(dwc);
2837
Felipe Balbi72246da2011-08-19 18:10:58 +03002838 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2839 dwc->gadget.ep0->maxpacket = 512;
2840 dwc->gadget.speed = USB_SPEED_SUPER;
2841 break;
John Youn2da9ad72016-05-20 16:34:26 -07002842 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002843 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2844 dwc->gadget.ep0->maxpacket = 64;
2845 dwc->gadget.speed = USB_SPEED_HIGH;
2846 break;
John Youn2da9ad72016-05-20 16:34:26 -07002847 case DWC3_DSTS_FULLSPEED2:
2848 case DWC3_DSTS_FULLSPEED1:
Felipe Balbi72246da2011-08-19 18:10:58 +03002849 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2850 dwc->gadget.ep0->maxpacket = 64;
2851 dwc->gadget.speed = USB_SPEED_FULL;
2852 break;
John Youn2da9ad72016-05-20 16:34:26 -07002853 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2855 dwc->gadget.ep0->maxpacket = 8;
2856 dwc->gadget.speed = USB_SPEED_LOW;
2857 break;
2858 }
2859
Pratyush Anand2b758352013-01-14 15:59:31 +05302860 /* Enable USB2 LPM Capability */
2861
John Younee5cd412016-02-05 17:08:45 -08002862 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07002863 (speed != DWC3_DSTS_SUPERSPEED) &&
2864 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05302865 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2866 reg |= DWC3_DCFG_LPM_CAP;
2867 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2868
2869 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2870 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2871
Huang Rui460d0982014-10-31 11:11:18 +08002872 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05302873
Huang Rui80caf7d2014-10-28 19:54:26 +08002874 /*
2875 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2876 * DCFG.LPMCap is set, core responses with an ACK and the
2877 * BESL value in the LPM token is less than or equal to LPM
2878 * NYET threshold.
2879 */
2880 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2881 && dwc->has_lpm_erratum,
2882 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2883
2884 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2885 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2886
Pratyush Anand2b758352013-01-14 15:59:31 +05302887 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002888 } else {
2889 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2890 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2891 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302892 }
2893
Mayank Ranaa99689a2016-08-10 17:39:47 -07002894
2895 /*
2896 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
2897 * suspend in P3 state and generates IN_P3 power event irq.
2898 */
2899 dwc3_usb3_phy_suspend(dwc, true);
2900
Felipe Balbi72246da2011-08-19 18:10:58 +03002901 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002902 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2903 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002904 if (ret) {
2905 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2906 return;
2907 }
2908
2909 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002910 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2911 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002912 if (ret) {
2913 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2914 return;
2915 }
2916
Mayank Ranaa99689a2016-08-10 17:39:47 -07002917 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +03002918 /*
2919 * Configure PHY via GUSB3PIPECTLn if required.
2920 *
2921 * Update GTXFIFOSIZn
2922 *
2923 * In both cases reset values should be sufficient.
2924 */
2925}
2926
Mayank Ranaa99689a2016-08-10 17:39:47 -07002927static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03002928{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002929 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002930
Mayank Ranaa99689a2016-08-10 17:39:47 -07002931 dev_dbg(dwc->dev, "%s\n", __func__);
2932
2933 /*
2934 * Identify if it is called from wakeup_interrupt() context for bus
2935 * resume or as part of remote wakeup. And based on that check for
2936 * U3 state. as we need to handle case of L1 resume i.e. where we
2937 * don't want to perform resume.
2938 */
2939 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
2940 perform_resume = false;
2941
2942 /* Only perform resume from L2 or Early Suspend states */
2943 if (perform_resume) {
2944
2945 /*
2946 * In case of remote wake up dwc3_gadget_wakeup_work()
2947 * is doing pm_runtime_get_sync().
2948 */
2949 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2950 dwc->b_suspend = false;
2951 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2952
2953 /*
2954 * set state to U0 as function level resume is trying to queue
2955 * notification over USB interrupt endpoint which would fail
2956 * due to state is not being updated.
2957 */
2958 dwc->link_state = DWC3_LINK_STATE_U0;
2959 dwc3_resume_gadget(dwc);
2960 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08002961 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07002962
2963 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03002964}
2965
2966static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2967 unsigned int evtinfo)
2968{
Felipe Balbifae2b902011-10-14 13:00:30 +03002969 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002970 unsigned int pwropt;
2971
2972 /*
2973 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2974 * Hibernation mode enabled which would show up when device detects
2975 * host-initiated U3 exit.
2976 *
2977 * In that case, device will generate a Link State Change Interrupt
2978 * from U3 to RESUME which is only necessary if Hibernation is
2979 * configured in.
2980 *
2981 * There are no functional changes due to such spurious event and we
2982 * just need to ignore it.
2983 *
2984 * Refers to:
2985 *
2986 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2987 * operational mode
2988 */
2989 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2990 if ((dwc->revision < DWC3_REVISION_250A) &&
2991 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2992 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2993 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06002994 dwc3_trace(trace_dwc3_gadget,
2995 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002996 return;
2997 }
2998 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002999
3000 /*
3001 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3002 * on the link partner, the USB session might do multiple entry/exit
3003 * of low power states before a transfer takes place.
3004 *
3005 * Due to this problem, we might experience lower throughput. The
3006 * suggested workaround is to disable DCTL[12:9] bits if we're
3007 * transitioning from U1/U2 to U0 and enable those bits again
3008 * after a transfer completes and there are no pending transfers
3009 * on any of the enabled endpoints.
3010 *
3011 * This is the first half of that workaround.
3012 *
3013 * Refers to:
3014 *
3015 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3016 * core send LGO_Ux entering U0
3017 */
3018 if (dwc->revision < DWC3_REVISION_183A) {
3019 if (next == DWC3_LINK_STATE_U0) {
3020 u32 u1u2;
3021 u32 reg;
3022
3023 switch (dwc->link_state) {
3024 case DWC3_LINK_STATE_U1:
3025 case DWC3_LINK_STATE_U2:
3026 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3027 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3028 | DWC3_DCTL_ACCEPTU2ENA
3029 | DWC3_DCTL_INITU1ENA
3030 | DWC3_DCTL_ACCEPTU1ENA);
3031
3032 if (!dwc->u1u2)
3033 dwc->u1u2 = reg & u1u2;
3034
3035 reg &= ~u1u2;
3036
3037 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3038 break;
3039 default:
3040 /* do nothing */
3041 break;
3042 }
3043 }
3044 }
3045
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003046 switch (next) {
3047 case DWC3_LINK_STATE_U1:
3048 if (dwc->speed == USB_SPEED_SUPER)
3049 dwc3_suspend_gadget(dwc);
3050 break;
3051 case DWC3_LINK_STATE_U2:
3052 case DWC3_LINK_STATE_U3:
3053 dwc3_suspend_gadget(dwc);
3054 break;
3055 case DWC3_LINK_STATE_RESUME:
3056 dwc3_resume_gadget(dwc);
3057 break;
3058 default:
3059 /* do nothing */
3060 break;
3061 }
3062
Mayank Ranaa99689a2016-08-10 17:39:47 -07003063 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003064 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003065 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003066}
3067
Baolin Wang72704f82016-05-16 16:43:53 +08003068static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003069 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003070{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003071 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003072
Mayank Ranaa99689a2016-08-10 17:39:47 -07003073 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3074
3075 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3076 /*
3077 * When first connecting the cable, even before the initial
3078 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3079 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3080 * event. In such a case, ignore.
3081 * Ignore suspend event until device side usb is not into
3082 * CONFIGURED state.
3083 */
3084 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3085 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3086 __func__, dwc->gadget.state);
3087 return;
3088 }
3089
Baolin Wang72704f82016-05-16 16:43:53 +08003090 dwc3_suspend_gadget(dwc);
3091
Mayank Ranaa99689a2016-08-10 17:39:47 -07003092 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3093 dwc->b_suspend = true;
3094 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3095 }
3096
Baolin Wang72704f82016-05-16 16:43:53 +08003097 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003098 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003099}
3100
Felipe Balbie1dadd32014-02-25 14:47:54 -06003101static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3102 unsigned int evtinfo)
3103{
3104 unsigned int is_ss = evtinfo & BIT(4);
3105
3106 /**
3107 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3108 * have a known issue which can cause USB CV TD.9.23 to fail
3109 * randomly.
3110 *
3111 * Because of this issue, core could generate bogus hibernation
3112 * events which SW needs to ignore.
3113 *
3114 * Refers to:
3115 *
3116 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3117 * Device Fallback from SuperSpeed
3118 */
3119 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3120 return;
3121
3122 /* enter hibernation here */
3123}
3124
Felipe Balbi72246da2011-08-19 18:10:58 +03003125static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3126 const struct dwc3_event_devt *event)
3127{
3128 switch (event->type) {
3129 case DWC3_DEVICE_EVENT_DISCONNECT:
3130 dwc3_gadget_disconnect_interrupt(dwc);
3131 break;
3132 case DWC3_DEVICE_EVENT_RESET:
3133 dwc3_gadget_reset_interrupt(dwc);
3134 break;
3135 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3136 dwc3_gadget_conndone_interrupt(dwc);
3137 break;
3138 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003139 dwc3_gadget_wakeup_interrupt(dwc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003140 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003141 case DWC3_DEVICE_EVENT_HIBER_REQ:
3142 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3143 "unexpected hibernation event\n"))
3144 break;
3145
3146 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3147 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003148 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3149 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3150 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003151 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003152 if (dwc->revision < DWC3_REVISION_230A) {
3153 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
3154 } else {
3155 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
3156
3157 /*
3158 * Ignore suspend event until the gadget enters into
3159 * USB_STATE_CONFIGURED state.
3160 */
3161 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3162 dwc3_gadget_suspend_interrupt(dwc,
3163 event->event_info);
3164 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003165 break;
3166 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003167 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Felipe Balbi72246da2011-08-19 18:10:58 +03003168 break;
3169 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003170 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Felipe Balbi72246da2011-08-19 18:10:58 +03003171 break;
3172 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003173 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Felipe Balbi72246da2011-08-19 18:10:58 +03003174 break;
3175 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003176 dwc3_trace(trace_dwc3_gadget, "Overflow");
Felipe Balbi72246da2011-08-19 18:10:58 +03003177 break;
3178 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003179 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Felipe Balbi72246da2011-08-19 18:10:58 +03003180 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003181
3182 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003183}
3184
3185static void dwc3_process_event_entry(struct dwc3 *dwc,
3186 const union dwc3_event *event)
3187{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003188 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003189 /* skip event processing in absence of vbus */
3190 if (!dwc->vbus_active) {
3191 dev_err(dwc->dev, "SKIP EVT:%x", event->raw);
3192 return;
3193 }
3194
3195 /* If run/stop is cleared don't process any more events */
3196 if (!dwc->pullups_connected) {
3197 dev_err(dwc->dev, "SKIP_EVT_PULLUP:%x", event->raw);
3198 return;
3199 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003200
Felipe Balbi72246da2011-08-19 18:10:58 +03003201 /* Endpoint IRQ, handle it and return early */
3202 if (event->type.is_devspec == 0) {
3203 /* depevt */
3204 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3205 }
3206
3207 switch (event->type.type) {
3208 case DWC3_EVENT_TYPE_DEV:
3209 dwc3_gadget_interrupt(dwc, &event->devt);
3210 break;
3211 /* REVISIT what to do with Carkit and I2C events ? */
3212 default:
3213 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3214 }
3215}
3216
Mayank Ranaa99689a2016-08-10 17:39:47 -07003217static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003218{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003219 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003220 irqreturn_t ret = IRQ_NONE;
3221 int left;
3222 u32 reg;
3223
Mayank Ranaa99689a2016-08-10 17:39:47 -07003224 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003225 left = evt->count;
3226
3227 if (!(evt->flags & DWC3_EVENT_PENDING))
3228 return IRQ_NONE;
3229
3230 while (left > 0) {
3231 union dwc3_event event;
3232
3233 event.raw = *(u32 *) (evt->buf + evt->lpos);
3234
3235 dwc3_process_event_entry(dwc, &event);
3236
Mayank Ranaa99689a2016-08-10 17:39:47 -07003237 if (dwc->err_evt_seen) {
3238 /*
3239 * if erratic error, skip remaining events
3240 * while controller undergoes reset
3241 */
3242 evt->lpos = (evt->lpos + left) %
3243 DWC3_EVENT_BUFFERS_SIZE;
3244 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
3245 if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT))
3246 dwc->err_evt_seen = 0;
3247 break;
3248 }
3249
Felipe Balbif42f2442013-06-12 21:25:08 +03003250 /*
3251 * FIXME we wrap around correctly to the next entry as
3252 * almost all entries are 4 bytes in size. There is one
3253 * entry which has 12 bytes which is a regular entry
3254 * followed by 8 bytes data. ATM I don't know how
3255 * things are organized if we get next to the a
3256 * boundary so I worry about that once we try to handle
3257 * that.
3258 */
3259 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3260 left -= 4;
3261
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003262 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003263 }
3264
Mayank Ranaa99689a2016-08-10 17:39:47 -07003265 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003266 evt->count = 0;
3267 evt->flags &= ~DWC3_EVENT_PENDING;
3268 ret = IRQ_HANDLED;
3269
3270 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003271 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003272 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003273 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003274
3275 return ret;
3276}
3277
Mayank Ranaa99689a2016-08-10 17:39:47 -07003278static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3279{
3280 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003281 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003282 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003283 unsigned int temp_time;
3284 ktime_t start_time;
3285
3286 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003287
Felipe Balbie5f68b42015-10-12 13:25:44 -05003288 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003289 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3290
3291 ret = dwc3_process_event_buf(dwc);
3292
Felipe Balbie5f68b42015-10-12 13:25:44 -05003293 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003294
Mayank Ranaa99689a2016-08-10 17:39:47 -07003295 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3296 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3297 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3298
Felipe Balbib15a7622011-06-30 16:57:15 +03003299 return ret;
3300}
3301
Mayank Ranaa99689a2016-08-10 17:39:47 -07003302static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003303{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003304 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003305 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003306 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003307
Mayank Ranaa99689a2016-08-10 17:39:47 -07003308 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003309
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003310 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003311 count &= DWC3_GEVNTCOUNT_MASK;
3312 if (!count)
3313 return IRQ_NONE;
3314
Mayank Ranaa99689a2016-08-10 17:39:47 -07003315 if (count > evt->length) {
3316 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
3317 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3318 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3319 return IRQ_HANDLED;
3320 }
3321
Felipe Balbib15a7622011-06-30 16:57:15 +03003322 evt->count = count;
3323 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003324
Felipe Balbie8adfc32013-06-12 21:11:14 +03003325 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003326 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003327 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003328 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003329
Felipe Balbib15a7622011-06-30 16:57:15 +03003330 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003331}
3332
Mayank Ranaa99689a2016-08-10 17:39:47 -07003333irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003334{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003335 struct dwc3 *dwc = _dwc;
3336 irqreturn_t ret = IRQ_NONE;
3337 irqreturn_t status;
3338 unsigned int temp_cnt = 0;
3339 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003340
Mayank Ranaa99689a2016-08-10 17:39:47 -07003341 start_time = ktime_get();
3342 dwc->irq_cnt++;
3343
3344 /* controller reset is still pending */
3345 if (dwc->err_evt_seen)
3346 return IRQ_HANDLED;
3347
3348 status = dwc3_check_event_buf(dwc);
3349 if (status == IRQ_WAKE_THREAD)
3350 ret = status;
3351
3352 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3353 dwc->irq_completion_time[dwc->irq_dbg_index] =
3354 ktime_us_delta(ktime_get(), start_time);
3355 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3356 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3357
Hemant Kumar78c7c282016-08-09 12:28:55 -07003358 if (ret == IRQ_WAKE_THREAD)
3359 dwc3_thread_interrupt(dwc->irq, dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003360
3361 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003362}
3363
3364/**
3365 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003366 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003367 *
3368 * Returns 0 on success otherwise negative errno.
3369 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003370int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003371{
Roger Quadros9522def2016-06-10 14:48:38 +03003372 int ret, irq;
3373 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3374
3375 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3376 if (irq == -EPROBE_DEFER)
3377 return irq;
3378
3379 if (irq <= 0) {
3380 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3381 if (irq == -EPROBE_DEFER)
3382 return irq;
3383
3384 if (irq <= 0) {
3385 irq = platform_get_irq(dwc3_pdev, 0);
3386 if (irq <= 0) {
3387 if (irq != -EPROBE_DEFER) {
3388 dev_err(dwc->dev,
3389 "missing peripheral IRQ\n");
3390 }
3391 if (!irq)
3392 irq = -EINVAL;
3393 return irq;
3394 }
3395 }
3396 }
3397
3398 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003399
Mayank Ranaa99689a2016-08-10 17:39:47 -07003400 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3401
Felipe Balbi72246da2011-08-19 18:10:58 +03003402 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3403 &dwc->ctrl_req_addr, GFP_KERNEL);
3404 if (!dwc->ctrl_req) {
3405 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3406 ret = -ENOMEM;
3407 goto err0;
3408 }
3409
Kishon Vijay Abraham I2abd9d52015-07-27 12:25:31 +05303410 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003411 &dwc->ep0_trb_addr, GFP_KERNEL);
3412 if (!dwc->ep0_trb) {
3413 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3414 ret = -ENOMEM;
3415 goto err1;
3416 }
3417
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003418 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003419 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003420 ret = -ENOMEM;
3421 goto err2;
3422 }
3423
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003424 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003425 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3426 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003427 if (!dwc->ep0_bounce) {
3428 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3429 ret = -ENOMEM;
3430 goto err3;
3431 }
3432
Felipe Balbi04c03d12015-12-02 10:06:45 -06003433 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3434 if (!dwc->zlp_buf) {
3435 ret = -ENOMEM;
3436 goto err4;
3437 }
3438
Felipe Balbi72246da2011-08-19 18:10:58 +03003439 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003440 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003441 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003442 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003443 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003444
3445 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003446 * FIXME We might be setting max_speed to <SUPER, however versions
3447 * <2.20a of dwc3 have an issue with metastability (documented
3448 * elsewhere in this driver) which tells us we can't set max speed to
3449 * anything lower than SUPER.
3450 *
3451 * Because gadget.max_speed is only used by composite.c and function
3452 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3453 * to happen so we avoid sending SuperSpeed Capability descriptor
3454 * together with our BOS descriptor as that could confuse host into
3455 * thinking we can handle super speed.
3456 *
3457 * Note that, in fact, we won't even support GetBOS requests when speed
3458 * is less than super speed because we don't have means, yet, to tell
3459 * composite.c that we are USB 2.0 + LPM ECN.
3460 */
3461 if (dwc->revision < DWC3_REVISION_220A)
3462 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003463 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003464 dwc->revision);
3465
3466 dwc->gadget.max_speed = dwc->maximum_speed;
3467
3468 /*
David Cohena4b9d942013-12-09 15:55:38 -08003469 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3470 * on ep out.
3471 */
3472 dwc->gadget.quirk_ep_out_aligned_size = true;
3473
3474 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003475 * REVISIT: Here we should clear all pending IRQs to be
3476 * sure we're starting from a well known location.
3477 */
3478
3479 ret = dwc3_gadget_init_endpoints(dwc);
3480 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003481 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003482
Felipe Balbi72246da2011-08-19 18:10:58 +03003483 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3484 if (ret) {
3485 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003486 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003487 }
3488
Mayank Ranaa99689a2016-08-10 17:39:47 -07003489 if (!dwc->is_drd) {
3490 pm_runtime_no_callbacks(&dwc->gadget.dev);
3491 pm_runtime_set_active(&dwc->gadget.dev);
3492 pm_runtime_enable(&dwc->gadget.dev);
3493 pm_runtime_get(&dwc->gadget.dev);
3494 }
3495
Felipe Balbi72246da2011-08-19 18:10:58 +03003496 return 0;
3497
Felipe Balbi04c03d12015-12-02 10:06:45 -06003498err5:
3499 kfree(dwc->zlp_buf);
3500
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003501err4:
David Cohene1f80462013-09-11 17:42:47 -07003502 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003503 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3504 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003505
Felipe Balbi72246da2011-08-19 18:10:58 +03003506err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003507 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003508
3509err2:
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003510 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003511 dwc->ep0_trb, dwc->ep0_trb_addr);
3512
3513err1:
3514 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3515 dwc->ctrl_req, dwc->ctrl_req_addr);
3516
3517err0:
3518 return ret;
3519}
3520
Felipe Balbi7415f172012-04-30 14:56:33 +03003521/* -------------------------------------------------------------------------- */
3522
Felipe Balbi72246da2011-08-19 18:10:58 +03003523void dwc3_gadget_exit(struct dwc3 *dwc)
3524{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003525 if (dwc->is_drd) {
3526 pm_runtime_put(&dwc->gadget.dev);
3527 pm_runtime_disable(&dwc->gadget.dev);
3528 }
3529
Felipe Balbi72246da2011-08-19 18:10:58 +03003530 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003531
Felipe Balbi72246da2011-08-19 18:10:58 +03003532 dwc3_gadget_free_endpoints(dwc);
3533
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003534 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3535 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003536
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003537 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003538 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003539
Christophe JAILLET51fbc7c2016-10-07 22:12:39 +02003540 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003541 dwc->ep0_trb, dwc->ep0_trb_addr);
3542
3543 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3544 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003545}
Felipe Balbi7415f172012-04-30 14:56:33 +03003546
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003547int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003548{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003549 int ret;
3550
Roger Quadros9772b472016-04-12 11:33:29 +03003551 if (!dwc->gadget_driver)
3552 return 0;
3553
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003554 ret = dwc3_gadget_run_stop(dwc, false, false);
3555 if (ret < 0)
3556 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003557
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003558 dwc3_disconnect_gadget(dwc);
3559 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003560
3561 return 0;
3562}
3563
3564int dwc3_gadget_resume(struct dwc3 *dwc)
3565{
Felipe Balbi7415f172012-04-30 14:56:33 +03003566 int ret;
3567
Roger Quadros9772b472016-04-12 11:33:29 +03003568 if (!dwc->gadget_driver)
3569 return 0;
3570
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003571 ret = __dwc3_gadget_start(dwc);
3572 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003573 goto err0;
3574
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003575 ret = dwc3_gadget_run_stop(dwc, true, false);
3576 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003577 goto err1;
3578
Felipe Balbi7415f172012-04-30 14:56:33 +03003579 return 0;
3580
3581err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003582 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003583
3584err0:
3585 return ret;
3586}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003587
3588void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3589{
3590 if (dwc->pending_events) {
3591 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3592 dwc->pending_events = false;
3593 enable_irq(dwc->irq_gadget);
3594 }
3595}