blob: f4f378c8fe0b90627a09cbb03b68e739e590927a [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
Mayank Rana9ca186c2017-06-19 17:57:21 -0700167void 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
Mayank Rana9ca186c2017-06-19 17:57:21 -0700172void dwc3_ep_inc_deq(struct dwc3_ep *dep)
Felipe Balbief966b92016-04-05 13:09:51 +0300173{
John Youndca01192016-05-19 17:26:05 -0700174 dwc3_ep_inc_trb(&dep->trb_dequeue);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200175}
176
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800177/*
178 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
179 * @dwc: pointer to our context structure
180 *
181 * This function will a best effort FIFO allocation in order
182 * to improve FIFO usage and throughput, while still allowing
183 * us to enable as many endpoints as possible.
184 *
185 * Keep in mind that this operation will be highly dependent
186 * on the configured size for RAM1 - which contains TxFifo -,
187 * the amount of endpoints enabled on coreConsultant tool, and
188 * the width of the Master Bus.
189 *
190 * In the ideal world, we would always be able to satisfy the
191 * following equation:
192 *
193 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
194 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
195 *
196 * Unfortunately, due to many variables that's not always the case.
197 */
Mayank Ranaac1200c2017-04-25 13:48:46 -0700198int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc, struct dwc3_ep *dep)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800199{
Mayank Ranaac1200c2017-04-25 13:48:46 -0700200 int fifo_size, mdwidth, max_packet = 1024;
201 int tmp, mult = 1;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800202
Mayank Ranaac1200c2017-04-25 13:48:46 -0700203 if (!dwc->needs_fifo_resize)
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800204 return 0;
205
Mayank Ranaac1200c2017-04-25 13:48:46 -0700206 /* resize IN endpoints excepts ep0 */
207 if (!usb_endpoint_dir_in(dep->endpoint.desc) ||
208 dep->endpoint.ep_num == 0)
209 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800210
Mayank Ranaac1200c2017-04-25 13:48:46 -0700211 /* Don't resize already resized IN endpoint */
212 if (dep->fifo_depth) {
213 dev_dbg(dwc->dev, "%s fifo_depth:%d is already set\n",
214 dep->endpoint.name, dep->fifo_depth);
215 return 0;
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800216 }
217
Mayank Ranaac1200c2017-04-25 13:48:46 -0700218 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
219 /* MDWIDTH is represented in bits, we need it in bytes */
220 mdwidth >>= 3;
221
222 if (dep->endpoint.ep_type == EP_TYPE_GSI || dep->endpoint.endless)
223 mult = 3;
224
225 if (((dep->endpoint.maxburst > 1) &&
226 usb_endpoint_xfer_bulk(dep->endpoint.desc))
227 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
228 mult = 3;
229
230 tmp = ((max_packet + mdwidth) * mult) + mdwidth;
231 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
232 dep->fifo_depth = fifo_size;
233 fifo_size |= (dwc->last_fifo_depth << 16);
234 dwc->last_fifo_depth += (fifo_size & 0xffff);
235
236 dev_dbg(dwc->dev, "%s ep_num:%d last_fifo_depth:%04x fifo_depth:%d\n",
237 dep->endpoint.name, dep->endpoint.ep_num, dwc->last_fifo_depth,
238 dep->fifo_depth);
239
240 dbg_event(0xFF, "resize_fifo", dep->number);
241 dbg_event(0xFF, "fifo_depth", dep->fifo_depth);
242 /* Check fifo size allocation doesn't exceed available RAM size. */
243 if (dwc->tx_fifo_size &&
244 ((dwc->last_fifo_depth * mdwidth) >= dwc->tx_fifo_size)) {
245 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
246 (dwc->last_fifo_depth * mdwidth), dwc->tx_fifo_size,
247 dep->endpoint.name, fifo_size);
248 dwc->last_fifo_depth -= (fifo_size & 0xffff);
249 dep->fifo_depth = 0;
250 WARN_ON(1);
251 return -ENOMEM;
252 }
253
254 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->endpoint.ep_num),
255 fifo_size);
Mayank Ranaa8e4de62016-12-13 17:11:15 -0800256 return 0;
257}
258
Felipe Balbi72246da2011-08-19 18:10:58 +0300259void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
260 int status)
261{
262 struct dwc3 *dwc = dep->dwc;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200263 unsigned int unmap_after_complete = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300264
Felipe Balbi737f1ae2016-08-11 12:24:27 +0300265 req->started = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300266 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200267 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300268
269 if (req->request.status == -EINPROGRESS)
270 req->request.status = status;
271
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200272 /*
273 * NOTICE we don't want to unmap before calling ->complete() if we're
274 * dealing with a bounced ep0 request. If we unmap it here, we would end
275 * up overwritting the contents of req->buf and this could confuse the
276 * gadget driver.
277 */
278 if (dwc->ep0_bounced && dep->number <= 1) {
Pratyush Anand0416e492012-08-10 13:42:16 +0530279 dwc->ep0_bounced = false;
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200280 unmap_after_complete = true;
281 } else {
Mayank Ranabd17b852017-08-25 10:38:30 -0700282 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200283 &req->request, req->direction);
284 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300285
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500286 trace_dwc3_gadget_giveback(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
288 spin_unlock(&dwc->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200289 usb_gadget_giveback_request(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 spin_lock(&dwc->lock);
Felipe Balbifc8bb912016-05-16 13:14:48 +0300291
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200292 if (unmap_after_complete)
Mayank Ranabd17b852017-08-25 10:38:30 -0700293 usb_gadget_unmap_request_by_dev(dwc->sysdev,
Janusz Dziedzicd9a97dc2017-03-13 14:11:32 +0200294 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300295}
296
Felipe Balbi3ece0ec2014-09-05 09:47:44 -0500297int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
Felipe Balbib09bb642012-04-24 16:19:11 +0300298{
299 u32 timeout = 500;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300300 int status = 0;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300301 int ret = 0;
Felipe Balbib09bb642012-04-24 16:19:11 +0300302 u32 reg;
303
304 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
305 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
306
307 do {
308 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
309 if (!(reg & DWC3_DGCMD_CMDACT)) {
Felipe Balbi71f7e702016-05-23 14:16:19 +0300310 status = DWC3_DGCMD_STATUS(reg);
311 if (status)
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300312 ret = -EINVAL;
313 break;
Felipe Balbib09bb642012-04-24 16:19:11 +0300314 }
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300315 } while (timeout--);
316
317 if (!timeout) {
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300318 ret = -ETIMEDOUT;
Felipe Balbi71f7e702016-05-23 14:16:19 +0300319 status = -ETIMEDOUT;
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300320 }
321
Felipe Balbi71f7e702016-05-23 14:16:19 +0300322 trace_dwc3_gadget_generic_cmd(cmd, param, status);
323
Felipe Balbi0fe886c2016-05-23 14:06:07 +0300324 return ret;
Felipe Balbib09bb642012-04-24 16:19:11 +0300325}
326
Felipe Balbic36d8e92016-04-04 12:46:33 +0300327static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
328
Felipe Balbi2cd47182016-04-12 16:42:43 +0300329int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
330 struct dwc3_gadget_ep_cmd_params *params)
Felipe Balbi72246da2011-08-19 18:10:58 +0300331{
Felipe Balbi2cd47182016-04-12 16:42:43 +0300332 struct dwc3 *dwc = dep->dwc;
Hemant Kumar43874172016-08-25 16:17:48 -0700333 u32 timeout = 3000;
Felipe Balbi72246da2011-08-19 18:10:58 +0300334 u32 reg;
335
Felipe Balbi0933df12016-05-23 14:02:33 +0300336 int cmd_status = 0;
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300337 int susphy = false;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300338 int ret = -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300339
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300340 /*
341 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
342 * we're issuing an endpoint command, we must check if
343 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
344 *
345 * We will also set SUSPHY bit to what it was before returning as stated
346 * by the same section on Synopsys databook.
347 */
Felipe Balbiab2a92e2016-05-17 14:55:58 +0300348 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
349 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
350 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
351 susphy = true;
352 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
353 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
354 }
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300355 }
356
Felipe Balbic36d8e92016-04-04 12:46:33 +0300357 if (cmd == DWC3_DEPCMD_STARTTRANSFER) {
358 int needs_wakeup;
359
360 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
361 dwc->link_state == DWC3_LINK_STATE_U2 ||
362 dwc->link_state == DWC3_LINK_STATE_U3);
363
364 if (unlikely(needs_wakeup)) {
365 ret = __dwc3_gadget_wakeup(dwc);
366 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
367 ret);
368 }
369 }
370
Felipe Balbi2eb88012016-04-12 16:53:39 +0300371 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
372 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
373 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
Felipe Balbi2eb88012016-04-12 16:53:39 +0300375 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT);
Felipe Balbi72246da2011-08-19 18:10:58 +0300376 do {
Felipe Balbi2eb88012016-04-12 16:53:39 +0300377 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
Felipe Balbi72246da2011-08-19 18:10:58 +0300378 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi0933df12016-05-23 14:02:33 +0300379 cmd_status = DWC3_DEPCMD_STATUS(reg);
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000380
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000381 switch (cmd_status) {
382 case 0:
383 ret = 0;
Felipe Balbic0ca3242016-04-04 09:11:51 +0300384 break;
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000385 case DEPEVT_TRANSFER_NO_RESOURCE:
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000386 ret = -EINVAL;
387 break;
388 case DEPEVT_TRANSFER_BUS_EXPIRY:
389 /*
390 * SW issues START TRANSFER command to
391 * isochronous ep with future frame interval. If
392 * future interval time has already passed when
393 * core receives the command, it will respond
394 * with an error status of 'Bus Expiry'.
395 *
396 * Instead of always returning -EINVAL, let's
397 * give a hint to the gadget driver that this is
398 * the case by returning -EAGAIN.
399 */
Konrad Leszczynski7b9cc7a2016-02-12 15:21:46 +0000400 ret = -EAGAIN;
401 break;
402 default:
403 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
404 }
405
Felipe Balbic0ca3242016-04-04 09:11:51 +0300406 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407 }
Felipe Balbif6bb2252016-05-23 13:53:34 +0300408 } while (--timeout);
Felipe Balbi72246da2011-08-19 18:10:58 +0300409
Felipe Balbif6bb2252016-05-23 13:53:34 +0300410 if (timeout == 0) {
Felipe Balbif6bb2252016-05-23 13:53:34 +0300411 ret = -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700412 dwc3_trace(trace_dwc3_gadget, "Command Timed Out");
413 dev_err(dwc->dev, "%s command timeout for %s\n",
414 dwc3_gadget_ep_cmd_string(cmd), dep->name);
Hemant Kumar43874172016-08-25 16:17:48 -0700415 if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) {
416 dwc->ep_cmd_timeout_cnt++;
417 dwc3_notify_event(dwc,
418 DWC3_CONTROLLER_RESTART_USB_SESSION);
419 }
Felipe Balbi0933df12016-05-23 14:02:33 +0300420 cmd_status = -ETIMEDOUT;
Felipe Balbif6bb2252016-05-23 13:53:34 +0300421 }
Felipe Balbic0ca3242016-04-04 09:11:51 +0300422
Felipe Balbi0933df12016-05-23 14:02:33 +0300423 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
424
Felipe Balbi2b0f11d2016-04-04 09:19:17 +0300425 if (unlikely(susphy)) {
426 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
427 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
428 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
429 }
430
Felipe Balbic0ca3242016-04-04 09:11:51 +0300431 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300432}
433
John Youn50c763f2016-05-31 17:49:56 -0700434static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
435{
436 struct dwc3 *dwc = dep->dwc;
437 struct dwc3_gadget_ep_cmd_params params;
438 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
439
440 /*
441 * As of core revision 2.60a the recommended programming model
442 * is to set the ClearPendIN bit when issuing a Clear Stall EP
443 * command for IN endpoints. This is to prevent an issue where
444 * some (non-compliant) hosts may not send ACK TPs for pending
445 * IN transfers due to a mishandled error condition. Synopsys
446 * STAR 9000614252.
447 */
Lu Baolu5e6c88d2016-09-09 12:51:27 +0800448 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
449 (dwc->gadget.speed >= USB_SPEED_SUPER))
John Youn50c763f2016-05-31 17:49:56 -0700450 cmd |= DWC3_DEPCMD_CLEARPENDIN;
451
452 memset(&params, 0, sizeof(params));
453
Felipe Balbi2cd47182016-04-12 16:42:43 +0300454 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Youn50c763f2016-05-31 17:49:56 -0700455}
456
Felipe Balbi72246da2011-08-19 18:10:58 +0300457static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
458{
459 struct dwc3 *dwc = dep->dwc;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700460 u32 num_trbs = DWC3_TRB_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300461
462 if (dep->trb_pool)
463 return 0;
464
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530465 dep->trb_pool = dma_zalloc_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700466 sizeof(struct dwc3_trb) * num_trbs,
Felipe Balbi72246da2011-08-19 18:10:58 +0300467 &dep->trb_pool_dma, GFP_KERNEL);
468 if (!dep->trb_pool) {
469 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
470 dep->name);
471 return -ENOMEM;
472 }
Mayank Ranaa99689a2016-08-10 17:39:47 -0700473 dep->num_trbs = num_trbs;
Felipe Balbi72246da2011-08-19 18:10:58 +0300474
475 return 0;
476}
477
478static void dwc3_free_trb_pool(struct dwc3_ep *dep)
479{
480 struct dwc3 *dwc = dep->dwc;
481
Mayank Ranaa99689a2016-08-10 17:39:47 -0700482 /* Freeing of GSI EP TRBs are handled by GSI EP ops. */
483 if (dep->endpoint.ep_type == EP_TYPE_GSI)
484 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300485
Mayank Rana4dd882c2016-10-05 09:43:05 -0700486 /*
487 * Clean up ep ring to avoid getting xferInProgress due to stale trbs
488 * with HWO bit set from previous composition when update transfer cmd
489 * is issued.
490 */
491 if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) {
492 memset(&dep->trb_pool[0], 0,
493 sizeof(struct dwc3_trb) * dep->num_trbs);
Mayank Rana558baca2017-02-17 11:46:38 -0800494 dbg_event(dep->number, "Clr_TRB", 0);
Mayank Rana4dd882c2016-10-05 09:43:05 -0700495 dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name);
496
Arnd Bergmann42695fc2016-11-17 17:13:47 +0530497 dma_free_coherent(dwc->sysdev,
Mayank Ranaa99689a2016-08-10 17:39:47 -0700498 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
499 dep->trb_pool, dep->trb_pool_dma);
500 dep->trb_pool = NULL;
501 dep->trb_pool_dma = 0;
502 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300503}
504
John Younc4509602016-02-16 20:10:53 -0800505static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
506
507/**
508 * dwc3_gadget_start_config - Configure EP resources
509 * @dwc: pointer to our controller context structure
510 * @dep: endpoint that is being enabled
511 *
512 * The assignment of transfer resources cannot perfectly follow the
513 * data book due to the fact that the controller driver does not have
514 * all knowledge of the configuration in advance. It is given this
515 * information piecemeal by the composite gadget framework after every
516 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
517 * programming model in this scenario can cause errors. For two
518 * reasons:
519 *
520 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
521 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
522 * multiple interfaces.
523 *
524 * 2) The databook does not mention doing more DEPXFERCFG for new
525 * endpoint on alt setting (8.1.6).
526 *
527 * The following simplified method is used instead:
528 *
529 * All hardware endpoints can be assigned a transfer resource and this
530 * setting will stay persistent until either a core reset or
531 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
532 * do DEPXFERCFG for every hardware endpoint as well. We are
533 * guaranteed that there are as many transfer resources as endpoints.
534 *
535 * This function is called for each endpoint when it is being enabled
536 * but is triggered only when called for EP0-out, which always happens
537 * first, and which should only happen in one of the above conditions.
538 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300539static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
540{
541 struct dwc3_gadget_ep_cmd_params params;
542 u32 cmd;
John Younc4509602016-02-16 20:10:53 -0800543 int i;
544 int ret;
545
546 if (dep->number)
547 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
549 memset(&params, 0x00, sizeof(params));
John Younc4509602016-02-16 20:10:53 -0800550 cmd = DWC3_DEPCMD_DEPSTARTCFG;
Felipe Balbi72246da2011-08-19 18:10:58 +0300551
Felipe Balbi2cd47182016-04-12 16:42:43 +0300552 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
John Younc4509602016-02-16 20:10:53 -0800553 if (ret)
554 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555
John Younc4509602016-02-16 20:10:53 -0800556 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
557 struct dwc3_ep *dep = dwc->eps[i];
558
559 if (!dep)
560 continue;
561
562 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
563 if (ret)
564 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300565 }
566
567 return 0;
568}
569
570static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200571 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300572 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300573 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300574{
575 struct dwc3_gadget_ep_cmd_params params;
576
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300577 if (dev_WARN_ONCE(dwc->dev, modify && restore,
578 "Can't modify and restore\n"))
579 return -EINVAL;
580
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 memset(&params, 0x00, sizeof(params));
582
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300583 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900584 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
585
586 /* Burst size is only needed in SuperSpeed mode */
John Younee5cd412016-02-05 17:08:45 -0800587 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
Felipe Balbi676e3492016-04-26 10:49:07 +0300588 u32 burst = dep->endpoint.maxburst;
Felipe Balbi676e3492016-04-26 10:49:07 +0300589 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
Chanho Parkd2e9a132012-08-31 16:54:07 +0900590 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300591
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300592 if (modify) {
593 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
594 } else if (restore) {
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600595 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
596 params.param2 |= dep->saved_state;
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300597 } else {
598 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600599 }
600
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300601 if (usb_endpoint_xfer_control(desc))
602 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
Felipe Balbi13fa2e62016-05-30 13:40:00 +0300603
604 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
605 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200607 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300608 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
609 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300610 dep->stream_capable = true;
611 }
612
Felipe Balbi0b93a4c2014-09-04 10:28:10 -0500613 if (!usb_endpoint_xfer_control(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300614 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300615
616 /*
617 * We are doing 1:1 mapping for endpoints, meaning
618 * Physical Endpoints 2 maps to Logical Endpoint 2 and
619 * so on. We consider the direction bit as part of the physical
620 * endpoint number. So USB endpoint 0x81 is 0x03.
621 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300622 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623
624 /*
625 * We must use the lower 16 TX FIFOs even though
626 * HW might have more
627 */
628 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300629 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300630
631 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300632 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 dep->interval = 1 << (desc->bInterval - 1);
634 }
635
Felipe Balbi2cd47182016-04-12 16:42:43 +0300636 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300637}
638
639static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
640{
641 struct dwc3_gadget_ep_cmd_params params;
642
643 memset(&params, 0x00, sizeof(params));
644
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300645 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300646
Felipe Balbi2cd47182016-04-12 16:42:43 +0300647 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
648 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +0300649}
650
651/**
652 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
653 * @dep: endpoint to be initialized
654 * @desc: USB Endpoint Descriptor
655 *
656 * Caller should take care of locking
657 */
658static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200659 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300660 const struct usb_ss_ep_comp_descriptor *comp_desc,
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300661 bool modify, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300662{
663 struct dwc3 *dwc = dep->dwc;
664 u32 reg;
Andy Shevchenkob09e99e2014-05-15 15:53:32 +0300665 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300666
Felipe Balbi73815282015-01-27 13:48:14 -0600667 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300668
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 if (!(dep->flags & DWC3_EP_ENABLED)) {
Mayank Ranaac1200c2017-04-25 13:48:46 -0700670 dep->endpoint.desc = desc;
671 dep->comp_desc = comp_desc;
672 dep->type = usb_endpoint_type(desc);
673 ret = dwc3_gadget_resize_tx_fifos(dwc, dep);
674 if (ret) {
675 dep->endpoint.desc = NULL;
676 dep->comp_desc = NULL;
677 dep->type = 0;
678 return ret;
679 }
680
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 ret = dwc3_gadget_start_config(dwc, dep);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700682 if (ret) {
683 dev_err(dwc->dev, "start_config() failed for %s\n",
684 dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700686 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 }
688
Felipe Balbi21e64bf2016-06-02 12:37:31 +0300689 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600690 restore);
Mayank Ranaa99689a2016-08-10 17:39:47 -0700691 if (ret) {
692 dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name);
Felipe Balbi72246da2011-08-19 18:10:58 +0300693 return ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -0700694 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300695
696 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200697 struct dwc3_trb *trb_st_hw;
698 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300699
Felipe Balbi72246da2011-08-19 18:10:58 +0300700 dep->flags |= DWC3_EP_ENABLED;
701
702 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
703 reg |= DWC3_DALEPENA_EP(dep->number);
704 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
705
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300706 if (usb_endpoint_xfer_control(desc))
Felipe Balbi7ab373a2016-05-23 11:27:26 +0300707 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300708
John Youn0d257442016-05-19 17:26:08 -0700709 /* Initialize the TRB ring */
710 dep->trb_dequeue = 0;
711 dep->trb_enqueue = 0;
712 memset(dep->trb_pool, 0,
713 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
714
Felipe Balbi36b68aa2016-04-05 13:24:36 +0300715 /* Link TRB. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300716 trb_st_hw = &dep->trb_pool[0];
717
Felipe Balbif6bafc62012-02-06 11:04:53 +0200718 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbif6bafc62012-02-06 11:04:53 +0200719 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
720 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
721 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
722 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300723 }
724
725 return 0;
726}
727
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200728static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300729{
730 struct dwc3_request *req;
731
Felipe Balbi0e146022016-06-21 10:32:02 +0300732 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi69450c42016-05-30 13:37:02 +0300733
Felipe Balbi0e146022016-06-21 10:32:02 +0300734 /* - giveback all requests to gadget driver */
735 while (!list_empty(&dep->started_list)) {
736 req = next_request(&dep->started_list);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200737
Felipe Balbi0e146022016-06-21 10:32:02 +0300738 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbiea53b882012-02-17 12:10:04 +0200739 }
740
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200741 while (!list_empty(&dep->pending_list)) {
742 req = next_request(&dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +0300743
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200744 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300745 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300746}
747
748/**
749 * __dwc3_gadget_ep_disable - Disables a HW endpoint
750 * @dep: the endpoint to disable
751 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200752 * This function also removes requests which are currently processed ny the
753 * hardware and those which are not yet scheduled.
754 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300756static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
757{
758 struct dwc3 *dwc = dep->dwc;
759 u32 reg;
760
Felipe Balbi7eaeac52015-07-20 14:46:15 -0500761 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
762
Mayank Ranaa99689a2016-08-10 17:39:47 -0700763 if (dep->endpoint.ep_type == EP_TYPE_NORMAL)
764 dwc3_remove_requests(dwc, dep);
765 else if (dep->endpoint.ep_type == EP_TYPE_GSI)
766 dwc3_stop_active_transfer(dwc, dep->number, true);
Felipe Balbi72246da2011-08-19 18:10:58 +0300767
Felipe Balbi687ef982014-04-16 10:30:33 -0500768 /* make sure HW endpoint isn't stalled */
769 if (dep->flags & DWC3_EP_STALL)
Felipe Balbi7a608552014-09-24 14:19:52 -0500770 __dwc3_gadget_ep_set_halt(dep, 0, false);
Felipe Balbi687ef982014-04-16 10:30:33 -0500771
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
773 reg &= ~DWC3_DALEPENA_EP(dep->number);
774 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
775
Felipe Balbi879631a2011-09-30 10:58:47 +0300776 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200777 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200778 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300779 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300780 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300781
Mayank Ranaa99689a2016-08-10 17:39:47 -0700782 /* Keep GSI ep names with "-gsi" suffix */
783 if (!strnstr(dep->name, "gsi", 10)) {
784 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
785 dep->number >> 1,
786 (dep->number & 1) ? "in" : "out");
787 }
788
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 return 0;
790}
791
792/* -------------------------------------------------------------------------- */
793
794static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
795 const struct usb_endpoint_descriptor *desc)
796{
797 return -EINVAL;
798}
799
800static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
801{
802 return -EINVAL;
803}
804
805/* -------------------------------------------------------------------------- */
806
807static int dwc3_gadget_ep_enable(struct usb_ep *ep,
808 const struct usb_endpoint_descriptor *desc)
809{
810 struct dwc3_ep *dep;
811 struct dwc3 *dwc;
812 unsigned long flags;
813 int ret;
814
815 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
816 pr_debug("dwc3: invalid parameters\n");
817 return -EINVAL;
818 }
819
820 if (!desc->wMaxPacketSize) {
821 pr_debug("dwc3: missing wMaxPacketSize\n");
822 return -EINVAL;
823 }
824
825 dep = to_dwc3_ep(ep);
826 dwc = dep->dwc;
827
Felipe Balbi95ca9612015-12-10 13:08:20 -0600828 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
829 "%s is already enabled\n",
830 dep->name))
Felipe Balbic6f83f32012-08-15 12:28:29 +0300831 return 0;
Felipe Balbic6f83f32012-08-15 12:28:29 +0300832
Felipe Balbi72246da2011-08-19 18:10:58 +0300833 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600834 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Mayank Rana558baca2017-02-17 11:46:38 -0800835 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 spin_unlock_irqrestore(&dwc->lock, flags);
837
838 return ret;
839}
840
841static int dwc3_gadget_ep_disable(struct usb_ep *ep)
842{
843 struct dwc3_ep *dep;
844 struct dwc3 *dwc;
845 unsigned long flags;
846 int ret;
847
848 if (!ep) {
849 pr_debug("dwc3: invalid parameters\n");
850 return -EINVAL;
851 }
852
853 dep = to_dwc3_ep(ep);
854 dwc = dep->dwc;
855
Felipe Balbi95ca9612015-12-10 13:08:20 -0600856 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
857 "%s is already disabled\n",
858 dep->name))
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300860
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -0800861 /* Keep GSI ep names with "-gsi" suffix */
862 if (!strnstr(dep->name, "gsi", 10)) {
863 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
864 dep->number >> 1,
865 (dep->number & 1) ? "in" : "out");
866 }
867
Felipe Balbi72246da2011-08-19 18:10:58 +0300868 spin_lock_irqsave(&dwc->lock, flags);
869 ret = __dwc3_gadget_ep_disable(dep);
Mayank Rana558baca2017-02-17 11:46:38 -0800870 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 spin_unlock_irqrestore(&dwc->lock, flags);
872
873 return ret;
874}
875
876static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
877 gfp_t gfp_flags)
878{
879 struct dwc3_request *req;
880 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300881
882 req = kzalloc(sizeof(*req), gfp_flags);
Jingoo Han734d5a52014-07-17 12:45:11 +0900883 if (!req)
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 return NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300885
886 req->epnum = dep->number;
887 req->dep = dep;
Mayank Rana7877b272017-06-19 18:03:22 -0700888 req->request.dma = DMA_ERROR_CODE;
Felipe Balbi72246da2011-08-19 18:10:58 +0300889
Felipe Balbi68d34c82016-05-30 13:34:58 +0300890 dep->allocated_requests++;
891
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500892 trace_dwc3_alloc_request(req);
893
Felipe Balbi72246da2011-08-19 18:10:58 +0300894 return &req->request;
895}
896
897static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
898 struct usb_request *request)
899{
900 struct dwc3_request *req = to_dwc3_request(request);
Felipe Balbi68d34c82016-05-30 13:34:58 +0300901 struct dwc3_ep *dep = to_dwc3_ep(ep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300902
Felipe Balbi68d34c82016-05-30 13:34:58 +0300903 dep->allocated_requests--;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -0500904 trace_dwc3_free_request(req);
Felipe Balbi72246da2011-08-19 18:10:58 +0300905 kfree(req);
906}
907
Felipe Balbi2c78c022016-08-12 13:13:10 +0300908static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
909
Felipe Balbic71fc372011-11-22 11:37:34 +0200910/**
911 * dwc3_prepare_one_trb - setup one TRB from one request
912 * @dep: endpoint for which this request is prepared
913 * @req: dwc3_request pointer
914 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200915static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200916 struct dwc3_request *req, dma_addr_t dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300917 unsigned length, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200918{
Felipe Balbif6bafc62012-02-06 11:04:53 +0200919 struct dwc3_trb *trb;
Felipe Balbi3666b622016-09-22 11:01:01 +0300920 struct dwc3 *dwc = dep->dwc;
921 struct usb_gadget *gadget = &dwc->gadget;
922 enum usb_device_speed speed = gadget->speed;
Felipe Balbic71fc372011-11-22 11:37:34 +0200923
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300924 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s",
Felipe Balbieeb720f2011-11-28 12:46:59 +0200925 dep->name, req, (unsigned long long) dma,
Felipe Balbi4bc48c92016-08-10 16:04:33 +0300926 length, chain ? " chain" : "");
Pratyush Anand915e2022013-01-14 15:59:35 +0530927
Felipe Balbi4faf7552016-04-05 13:14:31 +0300928 trb = &dep->trb_pool[dep->trb_enqueue];
Felipe Balbic71fc372011-11-22 11:37:34 +0200929
Felipe Balbieeb720f2011-11-28 12:46:59 +0200930 if (!req->trb) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +0200931 dwc3_gadget_move_started_request(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200932 req->trb = trb;
933 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbi4faf7552016-04-05 13:14:31 +0300934 req->first_trb_index = dep->trb_enqueue;
Felipe Balbia9c3ca52016-10-05 14:24:37 +0300935 dep->queued_requests++;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200936 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200937
Felipe Balbief966b92016-04-05 13:09:51 +0300938 dwc3_ep_inc_enq(dep);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530939
Felipe Balbif6bafc62012-02-06 11:04:53 +0200940 trb->size = DWC3_TRB_SIZE_LENGTH(length);
941 trb->bpl = lower_32_bits(dma);
942 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200943
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200944 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200945 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200946 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200947 break;
948
949 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbi3666b622016-09-22 11:01:01 +0300950 if (!node) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530951 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi3666b622016-09-22 11:01:01 +0300952
Manu Gautam480fd4f2017-07-19 17:07:10 +0530953 /*
954 * USB Specification 2.0 Section 5.9.2 states that: "If
955 * there is only a single transaction in the microframe,
956 * only a DATA0 data packet PID is used. If there are
957 * two transactions per microframe, DATA1 is used for
958 * the first transaction data packet and DATA0 is used
959 * for the second transaction data packet. If there are
960 * three transactions per microframe, DATA2 is used for
961 * the first transaction data packet, DATA1 is used for
962 * the second, and DATA0 is used for the third."
963 *
964 * IOW, we should satisfy the following cases:
965 *
966 * 1) length <= maxpacket
967 * - DATA0
968 *
969 * 2) maxpacket < length <= (2 * maxpacket)
970 * - DATA1, DATA0
971 *
972 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
973 * - DATA2, DATA1, DATA0
974 */
Felipe Balbi3666b622016-09-22 11:01:01 +0300975 if (speed == USB_SPEED_HIGH) {
976 struct usb_ep *ep = &dep->endpoint;
Manu Gautam480fd4f2017-07-19 17:07:10 +0530977 unsigned int mult = ep->mult - 1;
978 unsigned int maxp;
979
980 maxp = usb_endpoint_maxp(ep->desc) & 0x07ff;
981
982 if (length <= (2 * maxp))
983 mult--;
984
985 if (length <= maxp)
986 mult--;
987
988 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
Felipe Balbi3666b622016-09-22 11:01:01 +0300989 }
990 } else {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530991 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbi3666b622016-09-22 11:01:01 +0300992 }
Felipe Balbica4d44e2016-03-10 13:53:27 +0200993
994 /* always enable Interrupt on Missed ISOC */
995 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
Felipe Balbic71fc372011-11-22 11:37:34 +0200996 break;
997
998 case USB_ENDPOINT_XFER_BULK:
999 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +02001000 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +02001001 break;
1002 default:
1003 /*
1004 * This is only possible with faulty memory because we
1005 * checked it already :)
1006 */
1007 BUG();
1008 }
1009
Felipe Balbica4d44e2016-03-10 13:53:27 +02001010 /* always enable Continue on Short Packet */
Felipe Balbi66f37a92016-10-05 14:26:23 +03001011 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
Felipe Balbi2e37cdd2016-10-06 17:10:39 +03001012 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Felipe Balbif3af3652013-12-13 14:19:33 -06001013
Felipe Balbi66f37a92016-10-05 14:26:23 +03001014 if (req->request.short_not_ok)
1015 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1016 }
1017
Felipe Balbi2c78c022016-08-12 13:13:10 +03001018 if ((!req->request.no_interrupt && !chain) ||
1019 (dwc3_calc_trbs_left(dep) == 0))
Felipe Balbi66f37a92016-10-05 14:26:23 +03001020 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbica4d44e2016-03-10 13:53:27 +02001021
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301022 if (chain)
1023 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1024
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001025 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +02001026 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
1027
1028 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001029
1030 trace_dwc3_prepare_trb(dep, trb);
Felipe Balbic71fc372011-11-22 11:37:34 +02001031}
1032
John Youn361572b2016-05-19 17:26:17 -07001033/**
1034 * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
1035 * @dep: The endpoint with the TRB ring
1036 * @index: The index of the current TRB in the ring
1037 *
1038 * Returns the TRB prior to the one pointed to by the index. If the
1039 * index is 0, we will wrap backwards, skip the link TRB, and return
1040 * the one just before that.
1041 */
1042static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1043{
Felipe Balbi45438a02016-08-11 12:26:59 +03001044 u8 tmp = index;
John Youn361572b2016-05-19 17:26:17 -07001045
Felipe Balbi45438a02016-08-11 12:26:59 +03001046 if (!tmp)
1047 tmp = DWC3_TRB_NUM - 1;
1048
1049 return &dep->trb_pool[tmp - 1];
John Youn361572b2016-05-19 17:26:17 -07001050}
1051
Felipe Balbic4233572016-05-12 14:08:34 +03001052static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1053{
1054 struct dwc3_trb *tmp;
John Youn32db3d92016-05-19 17:26:12 -07001055 u8 trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001056
1057 /*
1058 * If enqueue & dequeue are equal than it is either full or empty.
1059 *
1060 * One way to know for sure is if the TRB right before us has HWO bit
1061 * set or not. If it has, then we're definitely full and can't fit any
1062 * more transfers in our ring.
1063 */
1064 if (dep->trb_enqueue == dep->trb_dequeue) {
John Youn361572b2016-05-19 17:26:17 -07001065 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1066 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
1067 return 0;
Felipe Balbic4233572016-05-12 14:08:34 +03001068
1069 return DWC3_TRB_NUM - 1;
1070 }
1071
John Youn9d7aba72016-08-26 18:43:01 -07001072 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
John Youn3de2685f2016-05-23 11:32:45 -07001073 trbs_left &= (DWC3_TRB_NUM - 1);
John Youn32db3d92016-05-19 17:26:12 -07001074
John Youn9d7aba72016-08-26 18:43:01 -07001075 if (dep->trb_dequeue < dep->trb_enqueue)
1076 trbs_left--;
1077
John Youn32db3d92016-05-19 17:26:12 -07001078 return trbs_left;
Felipe Balbic4233572016-05-12 14:08:34 +03001079}
1080
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001081static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001082 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001083{
Felipe Balbi1f512112016-08-12 13:17:27 +03001084 struct scatterlist *sg = req->sg;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001085 struct scatterlist *s;
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001086 unsigned int length;
1087 dma_addr_t dma;
1088 int i;
1089
Felipe Balbi1f512112016-08-12 13:17:27 +03001090 for_each_sg(sg, s, req->num_pending_sgs, i) {
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001091 unsigned chain = true;
1092
1093 length = sg_dma_len(s);
1094 dma = sg_dma_address(s);
1095
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001096 if (sg_is_last(s))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001097 chain = false;
1098
1099 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001100 chain, i);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001101
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001102 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001103 break;
1104 }
1105}
1106
1107static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001108 struct dwc3_request *req)
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001109{
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001110 unsigned int length;
1111 dma_addr_t dma;
1112
1113 dma = req->request.dma;
1114 length = req->request.length;
1115
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001116 dwc3_prepare_one_trb(dep, req, dma, length,
Felipe Balbi4bc48c92016-08-10 16:04:33 +03001117 false, 0);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001118}
1119
Felipe Balbi72246da2011-08-19 18:10:58 +03001120/*
1121 * dwc3_prepare_trbs - setup TRBs from requests
1122 * @dep: endpoint for which requests are being prepared
Felipe Balbi72246da2011-08-19 18:10:58 +03001123 *
Paul Zimmerman1d046792012-02-15 18:56:56 -08001124 * The function goes through the requests list and sets up TRBs for the
1125 * transfers. The function returns once there are no more TRBs available or
1126 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +03001127 */
Felipe Balbic4233572016-05-12 14:08:34 +03001128static void dwc3_prepare_trbs(struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03001129{
Felipe Balbi68e823e2011-11-28 12:25:01 +02001130 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001131
1132 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1133
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001134 if (!dwc3_calc_trbs_left(dep))
John Youn89bc8562016-05-19 17:26:10 -07001135 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001136
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001137 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03001138 if (req->num_pending_sgs > 0)
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001139 dwc3_prepare_one_trb_sg(dep, req);
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001140 else
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001141 dwc3_prepare_one_trb_linear(dep, req);
Felipe Balbi72246da2011-08-19 18:10:58 +03001142
Felipe Balbi7ae7df42016-08-24 14:37:22 +03001143 if (!dwc3_calc_trbs_left(dep))
Felipe Balbi5ee85d82016-05-13 12:42:44 +03001144 return;
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001146}
1147
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001148static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param)
Felipe Balbi72246da2011-08-19 18:10:58 +03001149{
1150 struct dwc3_gadget_ep_cmd_params params;
1151 struct dwc3_request *req;
1152 struct dwc3 *dwc = dep->dwc;
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001153 int starting;
Felipe Balbi72246da2011-08-19 18:10:58 +03001154 int ret;
1155 u32 cmd;
1156
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001157 starting = !(dep->flags & DWC3_EP_BUSY);
Felipe Balbi72246da2011-08-19 18:10:58 +03001158
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001159 dwc3_prepare_trbs(dep);
1160 req = next_request(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001161 if (!req) {
1162 dep->flags |= DWC3_EP_PENDING_REQUEST;
Mayank Rana558baca2017-02-17 11:46:38 -08001163 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001164 return 0;
1165 }
1166
1167 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001168
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001169 if (starting) {
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301170 params.param0 = upper_32_bits(req->trb_dma);
1171 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001172 cmd = DWC3_DEPCMD_STARTTRANSFER |
1173 DWC3_DEPCMD_PARAM(cmd_param);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301174 } else {
Felipe Balbib6b1c6d2016-05-30 13:29:35 +03001175 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1176 DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301177 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001178
Felipe Balbi2cd47182016-04-12 16:42:43 +03001179 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001180 if (ret < 0) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001181 /*
1182 * FIXME we need to iterate over the list of requests
1183 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001184 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001185 */
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301186 usb_gadget_unmap_request_by_dev(dwc->sysdev,
1187 &req->request, req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001188 list_del(&req->list);
1189 return ret;
1190 }
1191
1192 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001193
Felipe Balbi4fae2e32016-05-12 16:53:59 +03001194 if (starting) {
Felipe Balbi2eb88012016-04-12 16:53:39 +03001195 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
Felipe Balbib4996a82012-06-06 12:04:13 +03001196 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001197 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001198
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 return 0;
1200}
1201
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301202static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1203 struct dwc3_ep *dep, u32 cur_uf)
1204{
1205 u32 uf;
Mayank Rana558baca2017-02-17 11:46:38 -08001206 int ret;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301207
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001208 if (list_empty(&dep->pending_list)) {
Felipe Balbi73815282015-01-27 13:48:14 -06001209 dwc3_trace(trace_dwc3_gadget,
1210 "ISOC ep %s run out for requests",
1211 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301212 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301213 return;
1214 }
1215
1216 /* 4 micro frames in the future */
1217 uf = cur_uf + dep->interval * 4;
1218
Mayank Rana558baca2017-02-17 11:46:38 -08001219 ret = __dwc3_gadget_kick_transfer(dep, uf);
1220 if (ret < 0)
1221 dbg_event(dep->number, "ISOC QUEUE", ret);
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301222}
1223
1224static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1225 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1226{
1227 u32 cur_uf, mask;
1228
1229 mask = ~(dep->interval - 1);
1230 cur_uf = event->parameters & mask;
1231
1232 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1233}
1234
Felipe Balbi72246da2011-08-19 18:10:58 +03001235static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1236{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001237 struct dwc3 *dwc = dep->dwc;
1238 int ret;
1239
Felipe Balbibb423982015-11-16 15:31:21 -06001240 if (!dep->endpoint.desc) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001241 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001242 "trying to queue request %p to disabled %s",
Felipe Balbibb423982015-11-16 15:31:21 -06001243 &req->request, dep->endpoint.name);
1244 return -ESHUTDOWN;
1245 }
1246
Felipe Balbi3272bad2017-05-17 15:57:45 +03001247 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
Felipe Balbibb423982015-11-16 15:31:21 -06001248 &req->request, req->dep->name)) {
Felipe Balbi3272bad2017-05-17 15:57:45 +03001249 dwc3_trace(trace_dwc3_gadget, "request %pK belongs to '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001250 &req->request, req->dep->name);
Felipe Balbibb423982015-11-16 15:31:21 -06001251 return -EINVAL;
1252 }
1253
Manu Gautamb40ef612013-02-11 15:53:34 +05301254 if (req->request.status == -EINPROGRESS) {
1255 ret = -EBUSY;
1256 dev_err(dwc->dev, "%s: %p request already in queue",
1257 dep->name, req);
1258 return ret;
1259 }
Felipe Balbifc8bb912016-05-16 13:14:48 +03001260
Felipe Balbi72246da2011-08-19 18:10:58 +03001261 req->request.actual = 0;
1262 req->request.status = -EINPROGRESS;
1263 req->direction = dep->direction;
1264 req->epnum = dep->number;
1265
Felipe Balbife84f522015-09-01 09:01:38 -05001266 trace_dwc3_ep_queue(req);
1267
Arnd Bergmann42695fc2016-11-17 17:13:47 +05301268 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1269 dep->direction);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001270 if (ret)
1271 return ret;
1272
Felipe Balbi1f512112016-08-12 13:17:27 +03001273 req->sg = req->request.sg;
1274 req->num_pending_sgs = req->request.num_mapped_sgs;
1275
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001276 list_add_tail(&req->list, &dep->pending_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03001277
Felipe Balbid889c232016-09-29 15:44:29 +03001278 /*
1279 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1280 * wait for a XferNotReady event so we will know what's the current
1281 * (micro-)frame number.
1282 *
1283 * Without this trick, we are very, very likely gonna get Bus Expiry
1284 * errors which will force us issue EndTransfer command.
1285 */
1286 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1287 if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1288 list_empty(&dep->started_list)) {
Felipe Balbi08a36b52016-08-11 14:27:52 +03001289 dwc3_stop_active_transfer(dwc, dep->number, true);
1290 dep->flags = DWC3_EP_ENABLED;
1291 }
1292 return 0;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001293 }
1294
Felipe Balbi594e1212016-08-24 14:38:10 +03001295 if (!dwc3_calc_trbs_left(dep))
1296 return 0;
Felipe Balbib997ada2012-07-26 13:26:50 +03001297
Felipe Balbi08a36b52016-08-11 14:27:52 +03001298 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbia8f32812015-09-16 10:40:07 -05001299 if (ret && ret != -EBUSY)
Felipe Balbiec5e7952015-11-16 16:04:13 -06001300 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001301 "%s: failed to kick transfers",
Felipe Balbia8f32812015-09-16 10:40:07 -05001302 dep->name);
1303 if (ret == -EBUSY)
1304 ret = 0;
1305
1306 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001307}
1308
Mayank Ranaa99689a2016-08-10 17:39:47 -07001309static int dwc3_gadget_wakeup(struct usb_gadget *g)
1310{
1311 struct dwc3 *dwc = gadget_to_dwc(g);
1312
1313 schedule_work(&dwc->wakeup_work);
1314 return 0;
1315}
1316
Mayank Ranaa99689a2016-08-10 17:39:47 -07001317static bool dwc3_gadget_is_suspended(struct dwc3 *dwc)
1318{
1319 if (atomic_read(&dwc->in_lpm) ||
1320 dwc->link_state == DWC3_LINK_STATE_U3)
1321 return true;
1322 return false;
1323}
1324
Felipe Balbi04c03d12015-12-02 10:06:45 -06001325static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep,
1326 struct usb_request *request)
1327{
1328 dwc3_gadget_ep_free_request(ep, request);
1329}
1330
1331static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep)
1332{
1333 struct dwc3_request *req;
1334 struct usb_request *request;
1335 struct usb_ep *ep = &dep->endpoint;
1336
Felipe Balbi60cfb372016-05-24 13:45:17 +03001337 dwc3_trace(trace_dwc3_gadget, "queueing ZLP");
Felipe Balbi04c03d12015-12-02 10:06:45 -06001338 request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC);
1339 if (!request)
1340 return -ENOMEM;
1341
1342 request->length = 0;
1343 request->buf = dwc->zlp_buf;
1344 request->complete = __dwc3_gadget_ep_zlp_complete;
1345
1346 req = to_dwc3_request(request);
1347
1348 return __dwc3_gadget_ep_queue(dep, req);
1349}
1350
Felipe Balbi72246da2011-08-19 18:10:58 +03001351static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1352 gfp_t gfp_flags)
1353{
1354 struct dwc3_request *req = to_dwc3_request(request);
1355 struct dwc3_ep *dep = to_dwc3_ep(ep);
1356 struct dwc3 *dwc = dep->dwc;
1357
1358 unsigned long flags;
1359
1360 int ret;
1361
Zhuang Jin Canfdee4eb2014-09-03 14:26:34 +08001362 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001363 if (!dep->endpoint.desc) {
1364 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1365 request, ep->name);
1366 ret = -ESHUTDOWN;
1367 goto out;
1368 }
1369
1370 if (WARN(req->dep != dep, "request %p belongs to '%s'\n",
1371 request, req->dep->name)) {
1372 ret = -EINVAL;
1373 goto out;
1374 }
1375
1376 if (dwc3_gadget_is_suspended(dwc)) {
1377 if (dwc->gadget.remote_wakeup)
1378 dwc3_gadget_wakeup(&dwc->gadget);
1379 ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP;
1380 goto out;
1381 }
1382
Manu Gautam3df6a322017-03-06 16:24:59 -08001383 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1384 "trying to queue unaligned request (%d) with %s\n",
1385 request->length, ep->name);
1386
Felipe Balbi72246da2011-08-19 18:10:58 +03001387 ret = __dwc3_gadget_ep_queue(dep, req);
Felipe Balbi04c03d12015-12-02 10:06:45 -06001388
1389 /*
1390 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1391 * setting request->zero, instead of doing magic, we will just queue an
1392 * extra usb_request ourselves so that it gets handled the same way as
1393 * any other request.
1394 */
John Yound92618982015-12-22 12:23:20 -08001395 if (ret == 0 && request->zero && request->length &&
1396 (request->length % ep->maxpacket == 0))
Felipe Balbi04c03d12015-12-02 10:06:45 -06001397 ret = __dwc3_gadget_ep_queue_zlp(dwc, dep);
1398
Mayank Ranaa99689a2016-08-10 17:39:47 -07001399out:
Felipe Balbi72246da2011-08-19 18:10:58 +03001400 spin_unlock_irqrestore(&dwc->lock, flags);
1401
1402 return ret;
1403}
1404
1405static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1406 struct usb_request *request)
1407{
1408 struct dwc3_request *req = to_dwc3_request(request);
1409 struct dwc3_request *r = NULL;
1410
1411 struct dwc3_ep *dep = to_dwc3_ep(ep);
1412 struct dwc3 *dwc = dep->dwc;
1413
1414 unsigned long flags;
1415 int ret = 0;
1416
Mayank Ranaa99689a2016-08-10 17:39:47 -07001417 if (atomic_read(&dwc->in_lpm)) {
1418 dev_err(dwc->dev, "Unable to dequeue while in LPM\n");
1419 return -EAGAIN;
1420 }
1421
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05001422 trace_dwc3_ep_dequeue(req);
1423
Felipe Balbi72246da2011-08-19 18:10:58 +03001424 spin_lock_irqsave(&dwc->lock, flags);
1425
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001426 list_for_each_entry(r, &dep->pending_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001427 if (r == req)
1428 break;
1429 }
1430
1431 if (r != req) {
Felipe Balbiaa3342c2016-03-14 11:01:31 +02001432 list_for_each_entry(r, &dep->started_list, list) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 if (r == req)
1434 break;
1435 }
1436 if (r == req) {
1437 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001438 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301439 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001440 }
Felipe Balbi3272bad2017-05-17 15:57:45 +03001441 dev_err(dwc->dev, "request %pK was not queued to %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001442 request, ep->name);
1443 ret = -EINVAL;
1444 goto out0;
1445 }
1446
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301447out1:
Mayank Rana558baca2017-02-17 11:46:38 -08001448 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001449 /* giveback the request */
1450 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1451
1452out0:
1453 spin_unlock_irqrestore(&dwc->lock, flags);
1454
1455 return ret;
1456}
1457
Felipe Balbi7a608552014-09-24 14:19:52 -05001458int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
Felipe Balbi72246da2011-08-19 18:10:58 +03001459{
1460 struct dwc3_gadget_ep_cmd_params params;
1461 struct dwc3 *dwc = dep->dwc;
1462 int ret;
1463
Mayank Ranad223be42017-06-07 11:54:08 -07001464 if (!dep->endpoint.desc) {
1465 dev_dbg(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1466 return -EINVAL;
1467 }
1468
Felipe Balbi5ad02fb2014-09-24 10:48:26 -05001469 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1470 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1471 return -EINVAL;
1472 }
1473
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 memset(&params, 0x00, sizeof(params));
Mayank Rana558baca2017-02-17 11:46:38 -08001475 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 if (value) {
Felipe Balbi69450c42016-05-30 13:37:02 +03001477 struct dwc3_trb *trb;
1478
1479 unsigned transfer_in_flight;
1480 unsigned started;
1481
1482 if (dep->number > 1)
1483 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1484 else
1485 trb = &dwc->ep0_trb[dep->trb_enqueue];
1486
1487 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1488 started = !list_empty(&dep->started_list);
1489
1490 if (!protocol && ((dep->direction && transfer_in_flight) ||
1491 (!dep->direction && started))) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06001492 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi052ba52ef2016-04-05 15:05:05 +03001493 "%s: pending request, cannot halt",
Felipe Balbi7a608552014-09-24 14:19:52 -05001494 dep->name);
1495 return -EAGAIN;
1496 }
1497
Felipe Balbi2cd47182016-04-12 16:42:43 +03001498 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1499 &params);
Felipe Balbi72246da2011-08-19 18:10:58 +03001500 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001501 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001502 dep->name);
1503 else
1504 dep->flags |= DWC3_EP_STALL;
1505 } else {
Felipe Balbi2cd47182016-04-12 16:42:43 +03001506
John Youn50c763f2016-05-31 17:49:56 -07001507 ret = dwc3_send_clear_stall_ep_cmd(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001508 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001509 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001510 dep->name);
1511 else
Alan Sterna535d812013-11-01 12:05:12 -04001512 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001513 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001514
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 return ret;
1516}
1517
1518static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1519{
1520 struct dwc3_ep *dep = to_dwc3_ep(ep);
1521 struct dwc3 *dwc = dep->dwc;
1522
1523 unsigned long flags;
1524
1525 int ret;
1526
Mayank Ranaa99689a2016-08-10 17:39:47 -07001527 if (!ep->desc) {
1528 dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name);
1529 return -EINVAL;
1530 }
1531
Felipe Balbi72246da2011-08-19 18:10:58 +03001532 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7a608552014-09-24 14:19:52 -05001533 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001534 spin_unlock_irqrestore(&dwc->lock, flags);
1535
1536 return ret;
1537}
1538
1539static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1540{
1541 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001542 struct dwc3 *dwc = dep->dwc;
1543 unsigned long flags;
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001544 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001545
Paul Zimmerman249a4562012-02-24 17:32:16 -08001546 spin_lock_irqsave(&dwc->lock, flags);
Mayank Rana558baca2017-02-17 11:46:38 -08001547 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001548 dep->flags |= DWC3_EP_WEDGE;
1549
Pratyush Anand08f0d962012-06-25 22:40:43 +05301550 if (dep->number == 0 || dep->number == 1)
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001551 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
Pratyush Anand08f0d962012-06-25 22:40:43 +05301552 else
Felipe Balbi7a608552014-09-24 14:19:52 -05001553 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
Felipe Balbi95aa4e82014-09-24 10:50:14 -05001554 spin_unlock_irqrestore(&dwc->lock, flags);
1555
1556 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001557}
1558
1559/* -------------------------------------------------------------------------- */
1560
1561static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1562 .bLength = USB_DT_ENDPOINT_SIZE,
1563 .bDescriptorType = USB_DT_ENDPOINT,
1564 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1565};
1566
1567static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1568 .enable = dwc3_gadget_ep0_enable,
1569 .disable = dwc3_gadget_ep0_disable,
1570 .alloc_request = dwc3_gadget_ep_alloc_request,
1571 .free_request = dwc3_gadget_ep_free_request,
1572 .queue = dwc3_gadget_ep0_queue,
1573 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301574 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001575 .set_wedge = dwc3_gadget_ep_set_wedge,
1576};
1577
1578static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1579 .enable = dwc3_gadget_ep_enable,
1580 .disable = dwc3_gadget_ep_disable,
1581 .alloc_request = dwc3_gadget_ep_alloc_request,
1582 .free_request = dwc3_gadget_ep_free_request,
1583 .queue = dwc3_gadget_ep_queue,
1584 .dequeue = dwc3_gadget_ep_dequeue,
1585 .set_halt = dwc3_gadget_ep_set_halt,
1586 .set_wedge = dwc3_gadget_ep_set_wedge,
1587};
1588
1589/* -------------------------------------------------------------------------- */
1590
1591static int dwc3_gadget_get_frame(struct usb_gadget *g)
1592{
1593 struct dwc3 *dwc = gadget_to_dwc(g);
1594 u32 reg;
1595
1596 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1597 return DWC3_DSTS_SOFFN(reg);
1598}
1599
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001600static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001601{
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001602 int retries;
Felipe Balbi72246da2011-08-19 18:10:58 +03001603
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001604 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001605 u32 reg;
1606
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 u8 link_state;
1608 u8 speed;
1609
Felipe Balbi72246da2011-08-19 18:10:58 +03001610 /*
1611 * According to the Databook Remote wakeup request should
1612 * be issued only when the device is in early suspend state.
1613 *
1614 * We can check that via USB Link State bits in DSTS register.
1615 */
1616 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1617
1618 speed = reg & DWC3_DSTS_CONNECTSPD;
John Younee5cd412016-02-05 17:08:45 -08001619 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1620 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) {
Felipe Balbi60cfb372016-05-24 13:45:17 +03001621 dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed");
Felipe Balbi6b742892016-05-13 10:19:42 +03001622 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 }
1624
1625 link_state = DWC3_DSTS_USBLNKST(reg);
1626
1627 switch (link_state) {
1628 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1629 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1630 break;
1631 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06001632 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03001633 "can't wakeup from '%s'",
Felipe Balbiec5e7952015-11-16 16:04:13 -06001634 dwc3_gadget_link_string(link_state));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001635 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001636 }
1637
Felipe Balbi8598bde2012-01-02 18:55:57 +02001638 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1639 if (ret < 0) {
1640 dev_err(dwc->dev, "failed to put link in Recovery\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001641 return ret;
Felipe Balbi8598bde2012-01-02 18:55:57 +02001642 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001643
Paul Zimmerman802fde92012-04-27 13:10:52 +03001644 /* Recent versions do this automatically */
1645 if (dwc->revision < DWC3_REVISION_194A) {
1646 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001647 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001648 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1649 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1650 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001651
Paul Zimmerman1d046792012-02-15 18:56:56 -08001652 /* poll until Link State changes to ON */
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001653 retries = 20000;
Felipe Balbi72246da2011-08-19 18:10:58 +03001654
Nicolas Saenz Julienned6011f62016-08-16 10:22:38 +01001655 while (retries--) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001656 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1657
1658 /* in HS, means ON */
1659 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1660 break;
1661 }
1662
1663 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1664 dev_err(dwc->dev, "failed to send remote wakeup\n");
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001665 return -EINVAL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001666 }
1667
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001668 return 0;
1669}
1670
Mayank Ranaa99689a2016-08-10 17:39:47 -07001671#define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */
1672#define DWC3_PM_RESUME_DELAY 100 /* 100 msec */
1673
1674static void dwc3_gadget_wakeup_work(struct work_struct *w)
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001675{
Mayank Ranaa99689a2016-08-10 17:39:47 -07001676 struct dwc3 *dwc;
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001677 int ret;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001678 static int retry_count;
1679
1680 dwc = container_of(w, struct dwc3, wakeup_work);
1681
1682 ret = pm_runtime_get_sync(dwc->dev);
1683 if (ret) {
1684 /* pm_runtime_get_sync returns -EACCES error between
1685 * late_suspend and early_resume, wait for system resume to
1686 * finish and queue work again
1687 */
1688 pr_debug("PM runtime get sync failed, ret %d\n", ret);
1689 if (ret == -EACCES) {
1690 pm_runtime_put_noidle(dwc->dev);
1691 if (retry_count == DWC3_PM_RESUME_RETRIES) {
1692 retry_count = 0;
1693 pr_err("pm_runtime_get_sync timed out\n");
1694 return;
1695 }
1696 msleep(DWC3_PM_RESUME_DELAY);
1697 retry_count++;
1698 schedule_work(&dwc->wakeup_work);
1699 return;
1700 }
1701 }
1702 retry_count = 0;
Mayank Rana558baca2017-02-17 11:46:38 -08001703 dbg_event(0xFF, "Gdgwake gsyn",
1704 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001705
1706 ret = dwc3_gadget_wakeup_int(dwc);
1707
1708 if (ret)
1709 pr_err("Remote wakeup failed. ret = %d.\n", ret);
1710 else
1711 pr_debug("Remote wakeup succeeded.\n");
1712
1713 pm_runtime_put_noidle(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001714 dbg_event(0xFF, "Gdgwake put",
1715 atomic_read(&dwc->dev->power.usage_count));
Mayank Ranaa99689a2016-08-10 17:39:47 -07001716}
1717
1718static int dwc3_gadget_wakeup_int(struct dwc3 *dwc)
1719{
1720 bool link_recover_only = false;
1721
1722 u32 reg;
1723 int ret = 0;
1724 u8 link_state;
1725 unsigned long flags;
1726
1727 pr_debug("%s(): Entry\n", __func__);
1728 disable_irq(dwc->irq);
1729 spin_lock_irqsave(&dwc->lock, flags);
1730 /*
1731 * According to the Databook Remote wakeup request should
1732 * be issued only when the device is in early suspend state.
1733 *
1734 * We can check that via USB Link State bits in DSTS register.
1735 */
1736 link_state = dwc3_get_link_state(dwc);
1737
1738 switch (link_state) {
1739 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1740 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1741 break;
1742 case DWC3_LINK_STATE_U1:
1743 if (dwc->gadget.speed != USB_SPEED_SUPER) {
1744 link_recover_only = true;
1745 break;
1746 }
1747 /* Intentional fallthrough */
1748 default:
1749 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1750 link_state);
1751 ret = -EINVAL;
1752 goto out;
1753 }
1754
1755 /* Enable LINK STATUS change event */
1756 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1757 reg |= DWC3_DEVTEN_ULSTCNGEN;
1758 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1759 /*
1760 * memory barrier is required to make sure that required events
1761 * with core is enabled before performing RECOVERY mechnism.
1762 */
1763 mb();
1764
1765 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1766 if (ret < 0) {
1767 dev_err(dwc->dev, "failed to put link in Recovery\n");
1768 /* Disable LINK STATUS change */
1769 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1770 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1771 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1772 /* Required to complete this operation before returning */
1773 mb();
1774 goto out;
1775 }
1776
1777 /* Recent versions do this automatically */
1778 if (dwc->revision < DWC3_REVISION_194A) {
1779 /* write zeroes to Link Change Request */
1780 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1781 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1782 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1783 }
1784
1785 spin_unlock_irqrestore(&dwc->lock, flags);
1786 enable_irq(dwc->irq);
1787
1788 /*
1789 * Have bigger value (16 sec) for timeout since some host PCs driving
1790 * resume for very long time (e.g. 8 sec)
1791 */
1792 ret = wait_event_interruptible_timeout(dwc->wait_linkstate,
1793 (dwc->link_state < DWC3_LINK_STATE_U3) ||
1794 (dwc->link_state == DWC3_LINK_STATE_SS_DIS),
1795 msecs_to_jiffies(16000));
Felipe Balbi218ef7b2016-04-04 11:24:04 +03001796
1797 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001798 /* Disable link status change event */
1799 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
1800 reg &= ~DWC3_DEVTEN_ULSTCNGEN;
1801 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1802 /*
1803 * Complete this write before we go ahead and perform resume
1804 * as we don't need link status change notificaiton anymore.
1805 */
1806 mb();
1807
1808 if (!ret) {
1809 dev_dbg(dwc->dev, "Timeout moving into state(%d)\n",
1810 dwc->link_state);
1811 ret = -EINVAL;
1812 spin_unlock_irqrestore(&dwc->lock, flags);
1813 goto out1;
1814 } else {
1815 ret = 0;
1816 /*
1817 * If USB is disconnected OR received RESET from host,
1818 * don't perform resume
1819 */
1820 if (dwc->link_state == DWC3_LINK_STATE_SS_DIS ||
1821 dwc->gadget.state == USB_STATE_DEFAULT)
1822 link_recover_only = true;
1823 }
1824
1825 /*
1826 * According to DWC3 databook, the controller does not
1827 * trigger a wakeup event when remote-wakeup is used.
1828 * Hence, after remote-wakeup sequence is complete, and
1829 * the device is back at U0 state, it is required that
1830 * the resume sequence is initiated by SW.
1831 */
1832 if (!link_recover_only)
1833 dwc3_gadget_wakeup_interrupt(dwc, true);
1834
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 spin_unlock_irqrestore(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001836 pr_debug("%s: Exit\n", __func__);
1837 return ret;
1838
1839out:
1840 spin_unlock_irqrestore(&dwc->lock, flags);
1841 enable_irq(dwc->irq);
1842
1843out1:
1844 return ret;
1845}
1846
1847static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id)
1848{
1849 int ret = 0;
1850 struct dwc3 *dwc = gadget_to_dwc(g);
1851
1852 if (!g || (g->speed != USB_SPEED_SUPER))
1853 return -ENOTSUPP;
1854
1855 if (dwc3_gadget_is_suspended(dwc)) {
1856 pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n");
1857 dwc3_gadget_wakeup(&dwc->gadget);
1858 return -EAGAIN;
1859 }
1860
1861 if (dwc->revision < DWC3_REVISION_220A) {
1862 ret = dwc3_send_gadget_generic_command(dwc,
1863 DWC3_DGCMD_XMIT_FUNCTION, interface_id);
1864 } else {
1865 ret = dwc3_send_gadget_generic_command(dwc,
1866 DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4));
1867 }
1868
1869 if (ret)
1870 pr_err("Function wakeup HW command failed.\n");
1871 else
1872 pr_debug("Function wakeup HW command succeeded.\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001873
1874 return ret;
1875}
1876
1877static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1878 int is_selfpowered)
1879{
1880 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001881 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001882
Paul Zimmerman249a4562012-02-24 17:32:16 -08001883 spin_lock_irqsave(&dwc->lock, flags);
Peter Chenbcdea502015-01-28 16:32:40 +08001884 g->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001885 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001886
1887 return 0;
1888}
1889
Mayank Ranaa99689a2016-08-10 17:39:47 -07001890#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001891static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001892{
1893 u32 reg;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001894 u32 timeout = 1500;
Felipe Balbifc8bb912016-05-16 13:14:48 +03001895
Felipe Balbi72246da2011-08-19 18:10:58 +03001896 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001897 if (is_on) {
Mayank Rana558baca2017-02-17 11:46:38 -08001898 dbg_event(0xFF, "Pullup_enable", is_on);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001899 if (dwc->revision <= DWC3_REVISION_187A) {
1900 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1901 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1902 }
1903
1904 if (dwc->revision >= DWC3_REVISION_194A)
1905 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001906
1907 dwc3_event_buffers_setup(dwc);
1908 dwc3_gadget_restart(dwc);
1909 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001910 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001911
1912 if (dwc->has_hibernation)
1913 reg |= DWC3_DCTL_KEEP_CONNECT;
1914
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001915 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001916 } else {
Mayank Rana558baca2017-02-17 11:46:38 -08001917 dbg_event(0xFF, "Pullup_disable", is_on);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001918 dwc3_gadget_disable_irq(dwc);
1919 __dwc3_gadget_ep_disable(dwc->eps[0]);
1920 __dwc3_gadget_ep_disable(dwc->eps[1]);
1921
Felipe Balbi72246da2011-08-19 18:10:58 +03001922 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001923
1924 if (dwc->has_hibernation && !suspend)
1925 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1926
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001927 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001928 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001929
1930 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1931
1932 do {
1933 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
Felipe Balbib6d4e162016-06-09 16:47:05 +03001934 reg &= DWC3_DSTS_DEVCTRLHLT;
1935 } while (--timeout && !(!is_on ^ !reg));
Felipe Balbif2df6792016-06-09 16:31:34 +03001936
Mayank Ranaa99689a2016-08-10 17:39:47 -07001937 if (!timeout) {
1938 dev_err(dwc->dev, "failed to %s controller\n",
1939 is_on ? "start" : "stop");
Mayank Rana558baca2017-02-17 11:46:38 -08001940 if (is_on)
1941 dbg_event(0xFF, "STARTTOUT", reg);
1942 else
1943 dbg_event(0xFF, "STOPTOUT", reg);
Felipe Balbif2df6792016-06-09 16:31:34 +03001944 return -ETIMEDOUT;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001945 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001946
Felipe Balbi73815282015-01-27 13:48:14 -06001947 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
Felipe Balbi72246da2011-08-19 18:10:58 +03001948 dwc->gadget_driver
1949 ? dwc->gadget_driver->function : "no-function",
1950 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301951
1952 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001953}
1954
Mayank Ranaa99689a2016-08-10 17:39:47 -07001955static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
1956{
1957 struct dwc3 *dwc = gadget_to_dwc(g);
1958
1959 dwc->vbus_draw = mA;
1960 dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA);
Mayank Rana558baca2017-02-17 11:46:38 -08001961 dbg_event(0xFF, "currentDraw", mA);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001962 dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT);
1963 return 0;
1964}
1965
Felipe Balbi72246da2011-08-19 18:10:58 +03001966static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1967{
1968 struct dwc3 *dwc = gadget_to_dwc(g);
1969 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301970 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001971
1972 is_on = !!is_on;
Mayank Ranaa99689a2016-08-10 17:39:47 -07001973 dwc->softconnect = is_on;
1974 if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) {
1975 /*
1976 * Need to wait for vbus_session(on) from otg driver or to
1977 * the udc_start.
1978 */
Mayank Rana558baca2017-02-17 11:46:38 -08001979 dbg_event(0xFF, "WaitPullup", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001980 return 0;
1981 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001982
Mayank Ranaa99689a2016-08-10 17:39:47 -07001983 pm_runtime_get_sync(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001984 dbg_event(0xFF, "Pullup gsync",
1985 atomic_read(&dwc->dev->power.usage_count));
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07001987 /*
1988 * If we are here after bus suspend notify otg state machine to
1989 * increment pm usage count of dwc to prevent pm_runtime_suspend
1990 * during enumeration.
1991 */
1992 dwc->b_suspend = false;
1993 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001994 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 spin_unlock_irqrestore(&dwc->lock, flags);
1996
Mayank Ranaa99689a2016-08-10 17:39:47 -07001997 pm_runtime_mark_last_busy(dwc->dev);
1998 pm_runtime_put_autosuspend(dwc->dev);
Mayank Rana558baca2017-02-17 11:46:38 -08001999 dbg_event(0xFF, "Pullup put",
2000 atomic_read(&dwc->dev->power.usage_count));
Pratyush Anand6f17f742012-07-02 10:21:55 +05302001 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002002}
2003
Mayank Ranaa99689a2016-08-10 17:39:47 -07002004void dwc3_gadget_enable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002005{
2006 u32 reg;
2007
Mayank Rana558baca2017-02-17 11:46:38 -08002008 dbg_event(0xFF, "UnmaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002009 /* Enable all but Start and End of Frame IRQs */
2010 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2011 DWC3_DEVTEN_EVNTOVERFLOWEN |
2012 DWC3_DEVTEN_CMDCMPLTEN |
2013 DWC3_DEVTEN_ERRTICERREN |
2014 DWC3_DEVTEN_WKUPEVTEN |
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002015 DWC3_DEVTEN_CONNECTDONEEN |
2016 DWC3_DEVTEN_USBRSTEN |
2017 DWC3_DEVTEN_DISCONNEVTEN);
2018
Mayank Ranaa99689a2016-08-10 17:39:47 -07002019 /*
2020 * Enable SUSPENDEVENT(BIT:6) for version 230A and above
2021 * else enable USB Link change event (BIT:3) for older version
2022 */
2023 if (dwc->revision < DWC3_REVISION_230A)
2024 reg |= DWC3_DEVTEN_ULSTCNGEN;
2025 else
2026 reg |= DWC3_DEVTEN_SUSPEND;
2027
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002028 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2029}
2030
Mayank Ranaa99689a2016-08-10 17:39:47 -07002031void dwc3_gadget_disable_irq(struct dwc3 *dwc)
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002032{
Mayank Rana558baca2017-02-17 11:46:38 -08002033 dbg_event(0xFF, "MaskINT", 0);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002034 /* mask all interrupts */
2035 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2036}
2037
Felipe Balbib15a7622011-06-30 16:57:15 +03002038static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002039static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002040
Felipe Balbi4e994722016-05-13 14:09:59 +03002041/**
2042 * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG
2043 * dwc: pointer to our context structure
2044 *
2045 * The following looks like complex but it's actually very simple. In order to
2046 * calculate the number of packets we can burst at once on OUT transfers, we're
2047 * gonna use RxFIFO size.
2048 *
2049 * To calculate RxFIFO size we need two numbers:
2050 * MDWIDTH = size, in bits, of the internal memory bus
2051 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2052 *
2053 * Given these two numbers, the formula is simple:
2054 *
2055 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2056 *
2057 * 24 bytes is for 3x SETUP packets
2058 * 16 bytes is a clock domain crossing tolerance
2059 *
2060 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2061 */
2062static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2063{
2064 u32 ram2_depth;
2065 u32 mdwidth;
2066 u32 nump;
2067 u32 reg;
2068
2069 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2070 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
2071
2072 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2073 nump = min_t(u32, nump, 16);
2074
2075 /* update NumP */
2076 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2077 reg &= ~DWC3_DCFG_NUMP_MASK;
2078 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2079 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2080}
2081
Mayank Ranaa99689a2016-08-10 17:39:47 -07002082static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
2083{
2084 struct dwc3 *dwc = gadget_to_dwc(_gadget);
2085 unsigned long flags;
2086
2087 if (!dwc->is_drd)
2088 return -EPERM;
2089
2090 is_active = !!is_active;
2091
Mayank Rana558baca2017-02-17 11:46:38 -08002092 dbg_event(0xFF, "VbusSess", is_active);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002093 spin_lock_irqsave(&dwc->lock, flags);
2094
2095 /* Mark that the vbus was powered */
2096 dwc->vbus_active = is_active;
2097
2098 /*
2099 * Check if upper level usb_gadget_driver was already registered with
2100 * this udc controller driver (if dwc3_gadget_start was called)
2101 */
2102 if (dwc->gadget_driver && dwc->softconnect) {
2103 if (dwc->vbus_active) {
2104 /*
2105 * Both vbus was activated by otg and pullup was
2106 * signaled by the gadget driver.
2107 */
2108 dwc3_gadget_run_stop(dwc, 1, false);
2109 } else {
2110 dwc3_gadget_run_stop(dwc, 0, false);
2111 }
2112 }
2113
2114 /*
2115 * Clearing run/stop bit might occur before disconnect event is seen.
2116 * Make sure to let gadget driver know in that case.
2117 */
2118 if (!dwc->vbus_active) {
2119 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
2120 dwc3_gadget_disconnect_interrupt(dwc);
2121 }
2122
2123 spin_unlock_irqrestore(&dwc->lock, flags);
2124 return 0;
2125}
2126
Felipe Balbid7be2952016-05-04 15:49:37 +03002127static int __dwc3_gadget_start(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002128{
Felipe Balbi72246da2011-08-19 18:10:58 +03002129 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002130 int ret = 0;
2131 u32 reg;
2132
Mayank Rana558baca2017-02-17 11:46:38 -08002133 dbg_event(0xFF, "__Gadgetstart", 0);
John Youn26cac202016-11-14 12:32:43 -08002134
2135 /*
2136 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2137 * the core supports IMOD, disable it.
2138 */
2139 if (dwc->imod_interval) {
2140 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2141 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2142 } else if (dwc3_has_imod(dwc)) {
2143 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2144 }
2145
Felipe Balbi72246da2011-08-19 18:10:58 +03002146 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2147 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02002148
2149 /**
2150 * WORKAROUND: DWC3 revision < 2.20a have an issue
2151 * which would cause metastability state on Run/Stop
2152 * bit if we try to force the IP to USB2-only mode.
2153 *
2154 * Because of that, we cannot configure the IP to any
2155 * speed other than the SuperSpeed
2156 *
2157 * Refers to:
2158 *
2159 * STAR#9000525659: Clock Domain Crossing on DCTL in
2160 * USB 2.0 Mode
2161 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03002162 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02002163 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002164 } else {
2165 switch (dwc->maximum_speed) {
2166 case USB_SPEED_LOW:
John Youn2da9ad72016-05-20 16:34:26 -07002167 reg |= DWC3_DCFG_LOWSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002168 break;
2169 case USB_SPEED_FULL:
Roger Quadros5e3c2922017-01-03 14:32:09 +02002170 reg |= DWC3_DCFG_FULLSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002171 break;
2172 case USB_SPEED_HIGH:
John Youn2da9ad72016-05-20 16:34:26 -07002173 reg |= DWC3_DCFG_HIGHSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002174 break;
John Youn75808622016-02-05 17:09:13 -08002175 case USB_SPEED_SUPER_PLUS:
John Youn2da9ad72016-05-20 16:34:26 -07002176 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
John Youn75808622016-02-05 17:09:13 -08002177 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002178 default:
John Youn77966eb2016-02-19 17:31:01 -08002179 dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n",
Mayank Ranaa99689a2016-08-10 17:39:47 -07002180 dwc->maximum_speed);
John Youn77966eb2016-02-19 17:31:01 -08002181 /* fall through */
2182 case USB_SPEED_SUPER:
2183 reg |= DWC3_DCFG_SUPERSPEED;
2184 break;
Felipe Balbif7e846f2013-06-30 14:29:51 +03002185 }
2186 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002187 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2188
Mayank Ranaa99689a2016-08-10 17:39:47 -07002189 /* Programs the number of outstanding pipelined transfer requests
2190 * the AXI master pushes to the AXI slave.
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002191 */
Mayank Ranaa99689a2016-08-10 17:39:47 -07002192 if (dwc->revision >= DWC3_REVISION_270A) {
2193 reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1);
2194 reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK;
2195 reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe);
2196 dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg);
2197 }
Felipe Balbi2a58f9c2016-04-28 10:56:28 +03002198
Felipe Balbi4e994722016-05-13 14:09:59 +03002199 dwc3_gadget_setup_nump(dwc);
2200
Felipe Balbi72246da2011-08-19 18:10:58 +03002201 /* Start with SuperSpeed Default */
2202 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2203
Mayank Ranaa99689a2016-08-10 17:39:47 -07002204 dwc->delayed_status = false;
2205 /* reinitialize physical ep0-1 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002206 dep = dwc->eps[0];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002207 dep->flags = 0;
2208 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002209 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2210 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002211 if (ret) {
2212 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002213 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002214 }
2215
2216 dep = dwc->eps[1];
Mayank Ranaa99689a2016-08-10 17:39:47 -07002217 dep->flags = 0;
2218 dep->endpoint.maxburst = 1;
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002219 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2220 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002221 if (ret) {
2222 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002223 __dwc3_gadget_ep_disable(dwc->eps[0]);
2224 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002225 }
2226
2227 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03002228 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03002229 dwc3_ep0_out_start(dwc);
2230
Felipe Balbi8698e2a2013-02-08 15:24:04 +02002231 dwc3_gadget_enable_irq(dwc);
2232
Felipe Balbid7be2952016-05-04 15:49:37 +03002233 return ret;
2234}
2235
Mayank Ranaa99689a2016-08-10 17:39:47 -07002236/* Required gadget re-initialization before switching to gadget in OTG mode */
2237void dwc3_gadget_restart(struct dwc3 *dwc)
2238{
2239 __dwc3_gadget_start(dwc);
2240}
2241
Felipe Balbid7be2952016-05-04 15:49:37 +03002242static int dwc3_gadget_start(struct usb_gadget *g,
2243 struct usb_gadget_driver *driver)
2244{
2245 struct dwc3 *dwc = gadget_to_dwc(g);
2246 unsigned long flags;
2247 int ret = 0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002248
2249 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002250
Felipe Balbid7be2952016-05-04 15:49:37 +03002251 if (dwc->gadget_driver) {
2252 dev_err(dwc->dev, "%s is already bound to %s\n",
2253 dwc->gadget.name,
2254 dwc->gadget_driver->driver.name);
2255 ret = -EBUSY;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002256 goto err0;
Felipe Balbid7be2952016-05-04 15:49:37 +03002257 }
2258
2259 dwc->gadget_driver = driver;
2260
Mayank Ranaa99689a2016-08-10 17:39:47 -07002261 /*
2262 * For DRD, this might get called by gadget driver during bootup
2263 * even though host mode might be active. Don't actually perform
2264 * device-specific initialization until device mode is activated.
2265 * In that case dwc3_gadget_restart() will handle it.
2266 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002267 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002268 return 0;
2269
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03002270err0:
Mayank Ranaa99689a2016-08-10 17:39:47 -07002271 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002272 return ret;
2273}
2274
Felipe Balbid7be2952016-05-04 15:49:37 +03002275static void __dwc3_gadget_stop(struct dwc3 *dwc)
2276{
Mayank Rana558baca2017-02-17 11:46:38 -08002277 dbg_event(0xFF, "__Gadgetstop", 0);
Felipe Balbid7be2952016-05-04 15:49:37 +03002278 dwc3_gadget_disable_irq(dwc);
2279 __dwc3_gadget_ep_disable(dwc->eps[0]);
2280 __dwc3_gadget_ep_disable(dwc->eps[1]);
2281}
2282
Felipe Balbi22835b82014-10-17 12:05:12 -05002283static int dwc3_gadget_stop(struct usb_gadget *g)
Felipe Balbi72246da2011-08-19 18:10:58 +03002284{
Mayank Ranaa99689a2016-08-10 17:39:47 -07002285 struct dwc3 *dwc = gadget_to_dwc(g);
2286 unsigned long flags;
2287
Felipe Balbi72246da2011-08-19 18:10:58 +03002288 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002289 dwc->gadget_driver = NULL;
Mayank Ranabb7c0d52016-11-10 10:15:44 -08002290 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03002291
2292 return 0;
2293}
Paul Zimmerman802fde92012-04-27 13:10:52 +03002294
Mayank Ranaa99689a2016-08-10 17:39:47 -07002295static int dwc3_gadget_restart_usb_session(struct usb_gadget *g)
2296{
2297 struct dwc3 *dwc = gadget_to_dwc(g);
2298
Mayank Rana558baca2017-02-17 11:46:38 -08002299 dbg_event(0xFF, "RestartUSBSession", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002300 return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION);
2301}
2302
Felipe Balbi72246da2011-08-19 18:10:58 +03002303static const struct usb_gadget_ops dwc3_gadget_ops = {
2304 .get_frame = dwc3_gadget_get_frame,
2305 .wakeup = dwc3_gadget_wakeup,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002306 .func_wakeup = dwc_gadget_func_wakeup,
Felipe Balbi72246da2011-08-19 18:10:58 +03002307 .set_selfpowered = dwc3_gadget_set_selfpowered,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002308 .vbus_session = dwc3_gadget_vbus_session,
2309 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03002310 .pullup = dwc3_gadget_pullup,
2311 .udc_start = dwc3_gadget_start,
2312 .udc_stop = dwc3_gadget_stop,
Mayank Ranaa99689a2016-08-10 17:39:47 -07002313 .restart = dwc3_gadget_restart_usb_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03002314};
2315
2316/* -------------------------------------------------------------------------- */
2317
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002318#define NUM_GSI_OUT_EPS 1
2319#define NUM_GSI_IN_EPS 2
2320
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002321static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
2322 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03002323{
2324 struct dwc3_ep *dep;
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002325 u8 i, gsi_ep_count, gsi_ep_index = 0;
2326
2327 gsi_ep_count = NUM_GSI_OUT_EPS + NUM_GSI_IN_EPS;
2328 /* OUT GSI EPs based on direction field */
2329 if (gsi_ep_count && !direction)
2330 gsi_ep_count = NUM_GSI_OUT_EPS;
2331 /* IN GSI EPs */
2332 else if (gsi_ep_count && direction)
2333 gsi_ep_count = NUM_GSI_IN_EPS;
Felipe Balbi72246da2011-08-19 18:10:58 +03002334
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002335 for (i = 0; i < num; i++) {
John Yound07fa662016-05-23 11:32:43 -07002336 u8 epnum = (i << 1) | (direction ? 1 : 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002337
Felipe Balbi72246da2011-08-19 18:10:58 +03002338 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
Jingoo Han734d5a52014-07-17 12:45:11 +09002339 if (!dep)
Felipe Balbi72246da2011-08-19 18:10:58 +03002340 return -ENOMEM;
Felipe Balbi72246da2011-08-19 18:10:58 +03002341
2342 dep->dwc = dwc;
2343 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03002344 dep->direction = !!direction;
Felipe Balbi2eb88012016-04-12 16:53:39 +03002345 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
Felipe Balbi72246da2011-08-19 18:10:58 +03002346 dwc->eps[epnum] = dep;
2347
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002348 /* Reserve EPs at the end for GSI based on gsi_ep_count */
2349 if ((gsi_ep_index < gsi_ep_count) &&
2350 (i > (num - 1 - gsi_ep_count))) {
2351 gsi_ep_index++;
2352 /* For GSI EPs, name eps as "gsi-epin" or "gsi-epout" */
2353 snprintf(dep->name, sizeof(dep->name), "%s",
2354 (epnum & 1) ? "gsi-epin" : "gsi-epout");
2355 /* Set ep type as GSI */
2356 dep->endpoint.ep_type = EP_TYPE_GSI;
2357 } else {
2358 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
2359 epnum >> 1, (epnum & 1) ? "in" : "out");
2360 }
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002361
Devdutt Patnaik9b7ea1b2016-01-25 12:50:22 -08002362 dep->endpoint.ep_num = epnum >> 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 dep->endpoint.name = dep->name;
Felipe Balbi74674cb2016-04-13 16:44:39 +03002364 spin_lock_init(&dep->lock);
Felipe Balbi72246da2011-08-19 18:10:58 +03002365
Felipe Balbi73815282015-01-27 13:48:14 -06002366 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
Felipe Balbi653df352013-07-12 19:11:57 +03002367
Felipe Balbi72246da2011-08-19 18:10:58 +03002368 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01002369 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05302370 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03002371 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2372 if (!epnum)
2373 dwc->gadget.ep0 = &dep->endpoint;
2374 } else {
2375 int ret;
2376
Robert Baldygae117e742013-12-13 12:23:38 +01002377 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01002378 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2380 list_add_tail(&dep->endpoint.ep_list,
2381 &dwc->gadget.ep_list);
2382
2383 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002384 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02002387
Robert Baldygaa474d3b2015-07-31 16:00:19 +02002388 if (epnum == 0 || epnum == 1) {
2389 dep->endpoint.caps.type_control = true;
2390 } else {
2391 dep->endpoint.caps.type_iso = true;
2392 dep->endpoint.caps.type_bulk = true;
2393 dep->endpoint.caps.type_int = true;
2394 }
2395
2396 dep->endpoint.caps.dir_in = !!direction;
2397 dep->endpoint.caps.dir_out = !direction;
2398
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002399 INIT_LIST_HEAD(&dep->pending_list);
2400 INIT_LIST_HEAD(&dep->started_list);
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 }
2402
2403 return 0;
2404}
2405
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002406static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
2407{
2408 int ret;
2409
2410 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2411
2412 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
2413 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002414 dwc3_trace(trace_dwc3_gadget,
2415 "failed to allocate OUT endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002416 return ret;
2417 }
2418
2419 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
2420 if (ret < 0) {
Felipe Balbi73815282015-01-27 13:48:14 -06002421 dwc3_trace(trace_dwc3_gadget,
2422 "failed to allocate IN endpoints");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002423 return ret;
2424 }
2425
2426 return 0;
2427}
2428
Felipe Balbi72246da2011-08-19 18:10:58 +03002429static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2430{
2431 struct dwc3_ep *dep;
2432 u8 epnum;
2433
2434 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2435 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002436 if (!dep)
2437 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05302438 /*
2439 * Physical endpoints 0 and 1 are special; they form the
2440 * bi-directional USB endpoint 0.
2441 *
2442 * For those two physical endpoints, we don't allocate a TRB
2443 * pool nor do we add them the endpoints list. Due to that, we
2444 * shouldn't do these two operations otherwise we would end up
2445 * with all sorts of bugs when removing dwc3.ko.
2446 */
2447 if (epnum != 0 && epnum != 1) {
2448 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002449 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05302450 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002451
2452 kfree(dep);
2453 }
2454}
2455
Felipe Balbi72246da2011-08-19 18:10:58 +03002456/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02002457
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302458static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
2459 struct dwc3_request *req, struct dwc3_trb *trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002460 const struct dwc3_event_depevt *event, int status,
2461 int chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302462{
2463 unsigned int count;
2464 unsigned int s_pkt = 0;
2465 unsigned int trb_status;
2466
Felipe Balbidc55c672016-08-12 13:20:32 +03002467 dwc3_ep_inc_deq(dep);
Felipe Balbia9c3ca52016-10-05 14:24:37 +03002468
2469 if (req->trb == trb)
2470 dep->queued_requests--;
2471
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05002472 trace_dwc3_complete_trb(dep, trb);
2473
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002474 /*
2475 * If we're in the middle of series of chained TRBs and we
2476 * receive a short transfer along the way, DWC3 will skip
2477 * through all TRBs including the last TRB in the chain (the
2478 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2479 * bit and SW has to do it manually.
2480 *
2481 * We're going to do that here to avoid problems of HW trying
2482 * to use bogus TRBs for transfers.
2483 */
2484 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2485 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2486
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302487 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Felipe Balbia0ad85a2016-08-10 18:07:46 +03002488 return 1;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002489
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302490 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbidc55c672016-08-12 13:20:32 +03002491 req->request.actual += count;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302492
2493 if (dep->direction) {
2494 if (count) {
2495 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2496 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002497 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002498 "%s: incomplete IN transfer",
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302499 dep->name);
2500 /*
2501 * If missed isoc occurred and there is
2502 * no request queued then issue END
2503 * TRANSFER, so that core generates
2504 * next xfernotready and we will issue
2505 * a fresh START TRANSFER.
2506 * If there are still queued request
2507 * then wait, do not issue either END
2508 * or UPDATE TRANSFER, just attach next
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002509 * request in pending_list during
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302510 * giveback.If any future queued request
2511 * is successfully transferred then we
2512 * will issue UPDATE TRANSFER for all
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002513 * request in the pending_list.
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302514 */
2515 dep->flags |= DWC3_EP_MISSED_ISOC;
Mayank Rana558baca2017-02-17 11:46:38 -08002516 dbg_event(dep->number, "MISSED ISOC", status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302517 } else {
2518 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2519 dep->name);
2520 status = -ECONNRESET;
2521 }
2522 } else {
2523 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2524 }
2525 } else {
2526 if (count && (event->status & DEPEVT_STATUS_SHORT))
2527 s_pkt = 1;
2528 }
2529
Felipe Balbi7c705df2016-08-10 12:35:30 +03002530 if (s_pkt && !chain)
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302531 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002532
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302533 if ((event->status & DEPEVT_STATUS_IOC) &&
2534 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2535 return 1;
Felipe Balbif99f53f2016-08-12 13:19:20 +03002536
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302537 return 0;
2538}
2539
Felipe Balbi72246da2011-08-19 18:10:58 +03002540static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2541 const struct dwc3_event_depevt *event, int status)
2542{
Felipe Balbi31162af2016-08-11 14:38:37 +03002543 struct dwc3_request *req, *n;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002544 struct dwc3_trb *trb;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002545 bool ioc = false;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05302546 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002547
Felipe Balbi31162af2016-08-11 14:38:37 +03002548 list_for_each_entry_safe(req, n, &dep->started_list, list) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002549 unsigned length;
2550 unsigned actual;
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002551 int chain;
2552
Felipe Balbi1f512112016-08-12 13:17:27 +03002553 length = req->request.length;
2554 chain = req->num_pending_sgs > 0;
Felipe Balbi31162af2016-08-11 14:38:37 +03002555 if (chain) {
Felipe Balbi1f512112016-08-12 13:17:27 +03002556 struct scatterlist *sg = req->sg;
Felipe Balbi31162af2016-08-11 14:38:37 +03002557 struct scatterlist *s;
Felipe Balbi1f512112016-08-12 13:17:27 +03002558 unsigned int pending = req->num_pending_sgs;
Felipe Balbi31162af2016-08-11 14:38:37 +03002559 unsigned int i;
Felipe Balbiac7bdcc2015-11-16 16:13:57 -06002560
Felipe Balbi1f512112016-08-12 13:17:27 +03002561 for_each_sg(sg, s, pending, i) {
Felipe Balbi31162af2016-08-11 14:38:37 +03002562 trb = &dep->trb_pool[dep->trb_dequeue];
Felipe Balbic7de5732016-07-29 03:17:58 +03002563
Felipe Balbi1f512112016-08-12 13:17:27 +03002564 req->sg = sg_next(s);
2565 req->num_pending_sgs--;
2566
Felipe Balbi31162af2016-08-11 14:38:37 +03002567 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
2568 event, status, chain);
Felipe Balbi1f512112016-08-12 13:17:27 +03002569 if (ret)
2570 break;
Felipe Balbi31162af2016-08-11 14:38:37 +03002571 }
2572 } else {
Felipe Balbi737f1ae2016-08-11 12:24:27 +03002573 trb = &dep->trb_pool[dep->trb_dequeue];
Ville Syrjäläd115d702015-08-31 19:48:28 +03002574 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
Felipe Balbie5b36ae2016-08-10 11:13:26 +03002575 event, status, chain);
Felipe Balbi31162af2016-08-11 14:38:37 +03002576 }
Ville Syrjäläd115d702015-08-31 19:48:28 +03002577
Felipe Balbic7de5732016-07-29 03:17:58 +03002578 /*
2579 * We assume here we will always receive the entire data block
2580 * which we should receive. Meaning, if we program RX to
2581 * receive 4K but we receive only 2K, we assume that's all we
2582 * should receive and we simply bounce the request back to the
2583 * gadget driver for further processing.
2584 */
Felipe Balbi1f512112016-08-12 13:17:27 +03002585 actual = length - req->request.actual;
2586 req->request.actual = actual;
2587
2588 if (ret && chain && (actual < length) && req->num_pending_sgs)
2589 return __dwc3_gadget_kick_transfer(dep, 0);
2590
Ville Syrjäläd115d702015-08-31 19:48:28 +03002591 dwc3_gadget_giveback(dep, req, status);
2592
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002593 if (ret) {
2594 if ((event->status & DEPEVT_STATUS_IOC) &&
2595 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2596 ioc = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002597 break;
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002598 }
Felipe Balbi31162af2016-08-11 14:38:37 +03002599 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002600
Felipe Balbi4cb42212016-05-18 12:37:21 +03002601 /*
2602 * Our endpoint might get disabled by another thread during
2603 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2604 * early on so DWC3_EP_BUSY flag gets cleared
2605 */
2606 if (!dep->endpoint.desc)
2607 return 1;
2608
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302609 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002610 list_empty(&dep->started_list)) {
2611 if (list_empty(&dep->pending_list)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302612 /*
2613 * If there is no entry in request list then do
2614 * not issue END TRANSFER now. Just set PENDING
2615 * flag, so that END TRANSFER is issued when an
2616 * entry is added into request list.
2617 */
2618 dep->flags = DWC3_EP_PENDING_REQUEST;
2619 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03002620 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05302621 dep->flags = DWC3_EP_ENABLED;
2622 }
Pratyush Anand7efea862013-01-14 15:59:32 +05302623 return 1;
2624 }
2625
Arnd Bergmannd6e10bf2016-09-09 12:01:51 +02002626 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2627 return 0;
2628
Felipe Balbi72246da2011-08-19 18:10:58 +03002629 return 1;
2630}
2631
2632static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
Jingoo Han029d97f2014-07-04 15:00:51 +09002633 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
Felipe Balbi72246da2011-08-19 18:10:58 +03002634{
2635 unsigned status = 0;
2636 int clean_busy;
Felipe Balbie18b7972015-05-29 10:06:38 -05002637 u32 is_xfer_complete;
2638
2639 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
Felipe Balbi72246da2011-08-19 18:10:58 +03002640
2641 if (event->status & DEPEVT_STATUS_BUSERR)
2642 status = -ECONNRESET;
2643
Paul Zimmerman1d046792012-02-15 18:56:56 -08002644 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Felipe Balbi4cb42212016-05-18 12:37:21 +03002645 if (clean_busy && (!dep->endpoint.desc || is_xfer_complete ||
Felipe Balbie18b7972015-05-29 10:06:38 -05002646 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002647 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002648
2649 /*
2650 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2651 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2652 */
2653 if (dwc->revision < DWC3_REVISION_183A) {
2654 u32 reg;
2655 int i;
2656
2657 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002658 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002659
2660 if (!(dep->flags & DWC3_EP_ENABLED))
2661 continue;
2662
Felipe Balbiaa3342c2016-03-14 11:01:31 +02002663 if (!list_empty(&dep->started_list))
Felipe Balbifae2b902011-10-14 13:00:30 +03002664 return;
2665 }
2666
2667 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2668 reg |= dwc->u1u2;
2669 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2670
2671 dwc->u1u2 = 0;
2672 }
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002673
Felipe Balbi4cb42212016-05-18 12:37:21 +03002674 /*
2675 * Our endpoint might get disabled by another thread during
2676 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2677 * early on so DWC3_EP_BUSY flag gets cleared
2678 */
2679 if (!dep->endpoint.desc)
2680 return;
2681
Felipe Balbie6e709b2015-09-28 15:16:56 -05002682 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002683 int ret;
2684
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002685 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi8a1a9c92015-09-21 14:32:00 -05002686 if (!ret || ret == -EBUSY)
2687 return;
2688 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002689}
2690
Felipe Balbi72246da2011-08-19 18:10:58 +03002691static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2692 const struct dwc3_event_depevt *event)
2693{
2694 struct dwc3_ep *dep;
2695 u8 epnum = event->endpoint_number;
2696
2697 dep = dwc->eps[epnum];
2698
Felipe Balbi3336abb2012-06-06 09:19:35 +03002699 if (!(dep->flags & DWC3_EP_ENABLED))
2700 return;
2701
Felipe Balbi72246da2011-08-19 18:10:58 +03002702 if (epnum == 0 || epnum == 1) {
2703 dwc3_ep0_interrupt(dwc, event);
2704 return;
2705 }
2706
Mayank Rana0c667b42017-02-09 11:56:51 -08002707 dep->dbg_ep_events.total++;
2708
Felipe Balbi72246da2011-08-19 18:10:58 +03002709 switch (event->endpoint_event) {
2710 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002711 dep->resource_index = 0;
Mayank Rana0c667b42017-02-09 11:56:51 -08002712 dep->dbg_ep_events.xfercomplete++;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002713
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002714 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbiec5e7952015-11-16 16:04:13 -06002715 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002716 "%s is an Isochronous endpoint",
Felipe Balbi72246da2011-08-19 18:10:58 +03002717 dep->name);
2718 return;
2719 }
2720
Jingoo Han029d97f2014-07-04 15:00:51 +09002721 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002722 break;
2723 case DWC3_DEPEVT_XFERINPROGRESS:
Mayank Rana0c667b42017-02-09 11:56:51 -08002724 dep->dbg_ep_events.xferinprogress++;
Jingoo Han029d97f2014-07-04 15:00:51 +09002725 dwc3_endpoint_transfer_complete(dwc, dep, event);
Felipe Balbi72246da2011-08-19 18:10:58 +03002726 break;
2727 case DWC3_DEPEVT_XFERNOTREADY:
Mayank Rana0c667b42017-02-09 11:56:51 -08002728 dep->dbg_ep_events.xfernotready++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002729 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 dwc3_gadget_start_isoc(dwc, dep, event);
2731 } else {
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002732 int active;
Felipe Balbi72246da2011-08-19 18:10:58 +03002733 int ret;
2734
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002735 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2736
Felipe Balbi73815282015-01-27 13:48:14 -06002737 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
Felipe Balbi6bb4fe12015-09-28 14:49:02 -05002738 dep->name, active ? "Transfer Active"
Felipe Balbi72246da2011-08-19 18:10:58 +03002739 : "Transfer Not Active");
2740
Felipe Balbi4fae2e32016-05-12 16:53:59 +03002741 ret = __dwc3_gadget_kick_transfer(dep, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002742 if (!ret || ret == -EBUSY)
2743 return;
2744
Felipe Balbiec5e7952015-11-16 16:04:13 -06002745 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002746 "%s: failed to kick transfers",
Felipe Balbi72246da2011-08-19 18:10:58 +03002747 dep->name);
2748 }
2749
2750 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002751 case DWC3_DEPEVT_STREAMEVT:
Mayank Rana0c667b42017-02-09 11:56:51 -08002752 dep->dbg_ep_events.streamevent++;
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002753 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002754 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2755 dep->name);
2756 return;
2757 }
2758
2759 switch (event->status) {
2760 case DEPEVT_STREAMEVT_FOUND:
Felipe Balbi73815282015-01-27 13:48:14 -06002761 dwc3_trace(trace_dwc3_gadget,
2762 "Stream %d found and started",
Felipe Balbi879631a2011-09-30 10:58:47 +03002763 event->parameters);
2764
2765 break;
2766 case DEPEVT_STREAMEVT_NOTFOUND:
2767 /* FALLTHROUGH */
2768 default:
Felipe Balbiec5e7952015-11-16 16:04:13 -06002769 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03002770 "unable to find suitable stream");
Felipe Balbi879631a2011-09-30 10:58:47 +03002771 }
2772 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002773 case DWC3_DEPEVT_RXTXFIFOEVT:
Felipe Balbi60cfb372016-05-24 13:45:17 +03002774 dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name);
Mayank Rana0c667b42017-02-09 11:56:51 -08002775 dep->dbg_ep_events.rxtxfifoevent++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002776 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002777 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbi73815282015-01-27 13:48:14 -06002778 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08002779 dep->dbg_ep_events.epcmdcomplete++;
Felipe Balbi72246da2011-08-19 18:10:58 +03002780 break;
2781 }
2782}
2783
2784static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2785{
2786 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2787 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002788 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002789 dwc->gadget_driver->disconnect(&dwc->gadget);
2790 spin_lock(&dwc->lock);
2791 }
2792}
2793
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002794static void dwc3_suspend_gadget(struct dwc3 *dwc)
2795{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002796 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002797 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002798 dbg_event(0xFF, "SUSPEND", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002799 dwc->gadget_driver->suspend(&dwc->gadget);
2800 spin_lock(&dwc->lock);
2801 }
2802}
2803
2804static void dwc3_resume_gadget(struct dwc3 *dwc)
2805{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002806 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002807 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002808 dbg_event(0xFF, "RESUME", 0);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002809 dwc->gadget_driver->resume(&dwc->gadget);
Felipe Balbi5c7b3b02015-01-29 10:29:18 -06002810 spin_lock(&dwc->lock);
Felipe Balbi8e744752014-11-06 14:27:53 +08002811 }
2812}
2813
2814static void dwc3_reset_gadget(struct dwc3 *dwc)
2815{
2816 if (!dwc->gadget_driver)
2817 return;
2818
2819 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2820 spin_unlock(&dwc->lock);
Mayank Rana558baca2017-02-17 11:46:38 -08002821 dbg_event(0xFF, "UDC RESET", 0);
Felipe Balbi8e744752014-11-06 14:27:53 +08002822 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002823 spin_lock(&dwc->lock);
2824 }
2825}
2826
Mayank Ranaa99689a2016-08-10 17:39:47 -07002827void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002828{
2829 struct dwc3_ep *dep;
2830 struct dwc3_gadget_ep_cmd_params params;
2831 u32 cmd;
2832 int ret;
2833
2834 dep = dwc->eps[epnum];
2835
Felipe Balbib4996a82012-06-06 12:04:13 +03002836 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302837 return;
2838
Pratyush Anand57911502012-07-06 15:19:10 +05302839 /*
2840 * NOTICE: We are violating what the Databook says about the
2841 * EndTransfer command. Ideally we would _always_ wait for the
2842 * EndTransfer Command Completion IRQ, but that's causing too
2843 * much trouble synchronizing between us and gadget driver.
2844 *
2845 * We have discussed this with the IP Provider and it was
2846 * suggested to giveback all requests here, but give HW some
2847 * extra time to synchronize with the interconnect. We're using
Mickael Maisondc93b412014-12-23 17:34:43 +01002848 * an arbitrary 100us delay for that.
Pratyush Anand57911502012-07-06 15:19:10 +05302849 *
2850 * Note also that a similar handling was tested by Synopsys
2851 * (thanks a lot Paul) and nothing bad has come out of it.
2852 * In short, what we're doing is:
2853 *
2854 * - Issue EndTransfer WITH CMDIOC bit set
2855 * - Wait 100us
John Youn06281d42016-08-22 15:39:13 -07002856 *
2857 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2858 * supports a mode to work around the above limitation. The
2859 * software can poll the CMDACT bit in the DEPCMD register
2860 * after issuing a EndTransfer command. This mode is enabled
2861 * by writing GUCTL2[14]. This polling is already done in the
2862 * dwc3_send_gadget_ep_cmd() function so if the mode is
2863 * enabled, the EndTransfer command will have completed upon
2864 * returning from this function and we don't need to delay for
2865 * 100us.
2866 *
2867 * This mode is NOT available on the DWC_usb31 IP.
Pratyush Anand57911502012-07-06 15:19:10 +05302868 */
2869
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302870 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002871 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2872 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002873 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302874 memset(&params, 0, sizeof(params));
Felipe Balbi2cd47182016-04-12 16:42:43 +03002875 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302876 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002877 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002878 dep->flags &= ~DWC3_EP_BUSY;
John Youn06281d42016-08-22 15:39:13 -07002879
2880 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A)
2881 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002882}
2883
2884static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2885{
2886 u32 epnum;
2887
2888 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2889 struct dwc3_ep *dep;
2890
2891 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002892 if (!dep)
2893 continue;
2894
Felipe Balbi72246da2011-08-19 18:10:58 +03002895 if (!(dep->flags & DWC3_EP_ENABLED))
2896 continue;
2897
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002898 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002899 }
2900}
2901
2902static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2903{
2904 u32 epnum;
2905
2906 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2907 struct dwc3_ep *dep;
Felipe Balbi72246da2011-08-19 18:10:58 +03002908 int ret;
2909
2910 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002911 if (!dep)
2912 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002913
2914 if (!(dep->flags & DWC3_EP_STALL))
2915 continue;
2916
2917 dep->flags &= ~DWC3_EP_STALL;
2918
John Youn50c763f2016-05-31 17:49:56 -07002919 ret = dwc3_send_clear_stall_ep_cmd(dep);
Mayank Rana558baca2017-02-17 11:46:38 -08002920 dbg_event(dep->number, "ECLRSTALL", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002921 WARN_ON_ONCE(ret);
2922 }
2923}
2924
2925static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2926{
Felipe Balbic4430a22012-05-24 10:30:01 +03002927 int reg;
2928
Mayank Rana558baca2017-02-17 11:46:38 -08002929 dbg_event(0xFF, "DISCONNECT INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002930 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2931 dwc->b_suspend = false;
2932 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2933
Felipe Balbi72246da2011-08-19 18:10:58 +03002934 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2935 reg &= ~DWC3_DCTL_INITU1ENA;
2936 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2937
2938 reg &= ~DWC3_DCTL_INITU2ENA;
2939 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002940
Felipe Balbi72246da2011-08-19 18:10:58 +03002941 dwc3_disconnect_gadget(dwc);
2942
2943 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002944 dwc->setup_packet_pending = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002945 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
Felipe Balbi06a374e2014-10-10 15:24:00 -05002946 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
Felipe Balbifc8bb912016-05-16 13:14:48 +03002947
2948 dwc->connected = false;
Mayank Ranaa99689a2016-08-10 17:39:47 -07002949 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03002950}
2951
Felipe Balbi72246da2011-08-19 18:10:58 +03002952static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2953{
2954 u32 reg;
2955
Felipe Balbifc8bb912016-05-16 13:14:48 +03002956 dwc->connected = true;
2957
Felipe Balbidf62df52011-10-14 15:11:49 +03002958 /*
2959 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2960 * would cause a missing Disconnect Event if there's a
2961 * pending Setup Packet in the FIFO.
2962 *
2963 * There's no suggested workaround on the official Bug
2964 * report, which states that "unless the driver/application
2965 * is doing any special handling of a disconnect event,
2966 * there is no functional issue".
2967 *
2968 * Unfortunately, it turns out that we _do_ some special
2969 * handling of a disconnect event, namely complete all
2970 * pending transfers, notify gadget driver of the
2971 * disconnection, and so on.
2972 *
2973 * Our suggested workaround is to follow the Disconnect
2974 * Event steps here, instead, based on a setup_packet_pending
Felipe Balbib5d335e2015-11-16 16:20:34 -06002975 * flag. Such flag gets set whenever we have a SETUP_PENDING
2976 * status for EP0 TRBs and gets cleared on XferComplete for the
Felipe Balbidf62df52011-10-14 15:11:49 +03002977 * same endpoint.
2978 *
2979 * Refers to:
2980 *
2981 * STAR#9000466709: RTL: Device : Disconnect event not
2982 * generated if setup packet pending in FIFO
2983 */
2984 if (dwc->revision < DWC3_REVISION_188A) {
2985 if (dwc->setup_packet_pending)
2986 dwc3_gadget_disconnect_interrupt(dwc);
2987 }
2988
Mayank Rana558baca2017-02-17 11:46:38 -08002989 dbg_event(0xFF, "BUS RESET", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002990 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
2991 dwc->b_suspend = false;
2992 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
2993
2994 dwc3_usb3_phy_suspend(dwc, false);
Hemant Kumard55fe952016-10-31 10:26:41 -07002995 usb_gadget_vbus_draw(&dwc->gadget, 100);
Mayank Ranaa99689a2016-08-10 17:39:47 -07002996
Felipe Balbi8e744752014-11-06 14:27:53 +08002997 dwc3_reset_gadget(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002998
2999 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3000 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
3001 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02003002 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03003003
3004 dwc3_stop_active_transfers(dwc);
3005 dwc3_clear_stall_all_ep(dwc);
3006
3007 /* Reset device address to zero */
3008 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3009 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
3010 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003011
3012 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3013 dwc->link_state = DWC3_LINK_STATE_U0;
3014 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003015}
3016
3017static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
3018{
3019 u32 reg;
3020 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
3021
3022 /*
3023 * We change the clock only at SS but I dunno why I would want to do
3024 * this. Maybe it becomes part of the power saving plan.
3025 */
3026
John Younee5cd412016-02-05 17:08:45 -08003027 if ((speed != DWC3_DSTS_SUPERSPEED) &&
3028 (speed != DWC3_DSTS_SUPERSPEED_PLUS))
Felipe Balbi72246da2011-08-19 18:10:58 +03003029 return;
3030
3031 /*
3032 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
3033 * each time on Connect Done.
3034 */
3035 if (!usb30_clock)
3036 return;
3037
3038 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
3039 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
3040 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
3041}
3042
Felipe Balbi72246da2011-08-19 18:10:58 +03003043static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
3044{
Felipe Balbi72246da2011-08-19 18:10:58 +03003045 struct dwc3_ep *dep;
3046 int ret;
3047 u32 reg;
3048 u8 speed;
3049
Mayank Rana558baca2017-02-17 11:46:38 -08003050 dbg_event(0xFF, "CONNECT DONE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03003051 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
3052 speed = reg & DWC3_DSTS_CONNECTSPD;
3053 dwc->speed = speed;
3054
3055 dwc3_update_ram_clk_sel(dwc, speed);
3056
3057 switch (speed) {
John Youn2da9ad72016-05-20 16:34:26 -07003058 case DWC3_DSTS_SUPERSPEED_PLUS:
John Youn75808622016-02-05 17:09:13 -08003059 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3060 dwc->gadget.ep0->maxpacket = 512;
3061 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
3062 break;
John Youn2da9ad72016-05-20 16:34:26 -07003063 case DWC3_DSTS_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03003064 /*
3065 * WORKAROUND: DWC3 revisions <1.90a have an issue which
3066 * would cause a missing USB3 Reset event.
3067 *
3068 * In such situations, we should force a USB3 Reset
3069 * event by calling our dwc3_gadget_reset_interrupt()
3070 * routine.
3071 *
3072 * Refers to:
3073 *
3074 * STAR#9000483510: RTL: SS : USB3 reset event may
3075 * not be generated always when the link enters poll
3076 */
3077 if (dwc->revision < DWC3_REVISION_190A)
3078 dwc3_gadget_reset_interrupt(dwc);
3079
Felipe Balbi72246da2011-08-19 18:10:58 +03003080 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
3081 dwc->gadget.ep0->maxpacket = 512;
3082 dwc->gadget.speed = USB_SPEED_SUPER;
3083 break;
John Youn2da9ad72016-05-20 16:34:26 -07003084 case DWC3_DSTS_HIGHSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003085 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3086 dwc->gadget.ep0->maxpacket = 64;
3087 dwc->gadget.speed = USB_SPEED_HIGH;
3088 break;
Roger Quadros5e3c2922017-01-03 14:32:09 +02003089 case DWC3_DSTS_FULLSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003090 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
3091 dwc->gadget.ep0->maxpacket = 64;
3092 dwc->gadget.speed = USB_SPEED_FULL;
3093 break;
John Youn2da9ad72016-05-20 16:34:26 -07003094 case DWC3_DSTS_LOWSPEED:
Felipe Balbi72246da2011-08-19 18:10:58 +03003095 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
3096 dwc->gadget.ep0->maxpacket = 8;
3097 dwc->gadget.speed = USB_SPEED_LOW;
3098 break;
3099 }
3100
Pratyush Anand2b758352013-01-14 15:59:31 +05303101 /* Enable USB2 LPM Capability */
3102
John Younee5cd412016-02-05 17:08:45 -08003103 if ((dwc->revision > DWC3_REVISION_194A) &&
John Youn2da9ad72016-05-20 16:34:26 -07003104 (speed != DWC3_DSTS_SUPERSPEED) &&
3105 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
Pratyush Anand2b758352013-01-14 15:59:31 +05303106 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
3107 reg |= DWC3_DCFG_LPM_CAP;
3108 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
3109
3110 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3111 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
3112
Huang Rui460d0982014-10-31 11:11:18 +08003113 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
Pratyush Anand2b758352013-01-14 15:59:31 +05303114
Huang Rui80caf7d2014-10-28 19:54:26 +08003115 /*
3116 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
3117 * DCFG.LPMCap is set, core responses with an ACK and the
3118 * BESL value in the LPM token is less than or equal to LPM
3119 * NYET threshold.
3120 */
3121 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
3122 && dwc->has_lpm_erratum,
3123 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
3124
3125 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
3126 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
3127
Pratyush Anand2b758352013-01-14 15:59:31 +05303128 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06003129 } else {
3130 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3131 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
3132 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05303133 }
3134
Mayank Ranaa99689a2016-08-10 17:39:47 -07003135
3136 /*
3137 * In HS mode this allows SS phy suspend. In SS mode this allows ss phy
3138 * suspend in P3 state and generates IN_P3 power event irq.
3139 */
3140 dwc3_usb3_phy_suspend(dwc, true);
3141
Felipe Balbi72246da2011-08-19 18:10:58 +03003142 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003143 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3144 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003145 if (ret) {
3146 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3147 return;
3148 }
3149
3150 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06003151 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
3152 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03003153 if (ret) {
3154 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
3155 return;
3156 }
3157
Mayank Ranaa99689a2016-08-10 17:39:47 -07003158 dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT);
Felipe Balbi72246da2011-08-19 18:10:58 +03003159 /*
3160 * Configure PHY via GUSB3PIPECTLn if required.
3161 *
3162 * Update GTXFIFOSIZn
3163 *
3164 * In both cases reset values should be sufficient.
3165 */
3166}
3167
Mayank Ranaa99689a2016-08-10 17:39:47 -07003168static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup)
Felipe Balbi72246da2011-08-19 18:10:58 +03003169{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003170 bool perform_resume = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003171
Mayank Ranaa99689a2016-08-10 17:39:47 -07003172 dev_dbg(dwc->dev, "%s\n", __func__);
3173
Mayank Rana558baca2017-02-17 11:46:38 -08003174 dbg_event(0xFF, "WAKEUP", remote_wakeup);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003175 /*
3176 * Identify if it is called from wakeup_interrupt() context for bus
3177 * resume or as part of remote wakeup. And based on that check for
3178 * U3 state. as we need to handle case of L1 resume i.e. where we
3179 * don't want to perform resume.
3180 */
3181 if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3)
3182 perform_resume = false;
3183
3184 /* Only perform resume from L2 or Early Suspend states */
3185 if (perform_resume) {
3186
3187 /*
3188 * In case of remote wake up dwc3_gadget_wakeup_work()
3189 * is doing pm_runtime_get_sync().
3190 */
3191 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3192 dwc->b_suspend = false;
3193 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3194
3195 /*
3196 * set state to U0 as function level resume is trying to queue
3197 * notification over USB interrupt endpoint which would fail
3198 * due to state is not being updated.
3199 */
3200 dwc->link_state = DWC3_LINK_STATE_U0;
3201 dwc3_resume_gadget(dwc);
3202 return;
Jiebing Liad14d4e2014-12-11 13:26:29 +08003203 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003204
3205 dwc->link_state = DWC3_LINK_STATE_U0;
Felipe Balbi72246da2011-08-19 18:10:58 +03003206}
3207
3208static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
3209 unsigned int evtinfo)
3210{
Felipe Balbifae2b902011-10-14 13:00:30 +03003211 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003212 unsigned int pwropt;
3213
3214 /*
3215 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
3216 * Hibernation mode enabled which would show up when device detects
3217 * host-initiated U3 exit.
3218 *
3219 * In that case, device will generate a Link State Change Interrupt
3220 * from U3 to RESUME which is only necessary if Hibernation is
3221 * configured in.
3222 *
3223 * There are no functional changes due to such spurious event and we
3224 * just need to ignore it.
3225 *
3226 * Refers to:
3227 *
3228 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3229 * operational mode
3230 */
3231 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3232 if ((dwc->revision < DWC3_REVISION_250A) &&
3233 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3234 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3235 (next == DWC3_LINK_STATE_RESUME)) {
Felipe Balbi73815282015-01-27 13:48:14 -06003236 dwc3_trace(trace_dwc3_gadget,
3237 "ignoring transition U3 -> Resume");
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03003238 return;
3239 }
3240 }
Felipe Balbifae2b902011-10-14 13:00:30 +03003241
3242 /*
3243 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3244 * on the link partner, the USB session might do multiple entry/exit
3245 * of low power states before a transfer takes place.
3246 *
3247 * Due to this problem, we might experience lower throughput. The
3248 * suggested workaround is to disable DCTL[12:9] bits if we're
3249 * transitioning from U1/U2 to U0 and enable those bits again
3250 * after a transfer completes and there are no pending transfers
3251 * on any of the enabled endpoints.
3252 *
3253 * This is the first half of that workaround.
3254 *
3255 * Refers to:
3256 *
3257 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3258 * core send LGO_Ux entering U0
3259 */
3260 if (dwc->revision < DWC3_REVISION_183A) {
3261 if (next == DWC3_LINK_STATE_U0) {
3262 u32 u1u2;
3263 u32 reg;
3264
3265 switch (dwc->link_state) {
3266 case DWC3_LINK_STATE_U1:
3267 case DWC3_LINK_STATE_U2:
3268 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3269 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3270 | DWC3_DCTL_ACCEPTU2ENA
3271 | DWC3_DCTL_INITU1ENA
3272 | DWC3_DCTL_ACCEPTU1ENA);
3273
3274 if (!dwc->u1u2)
3275 dwc->u1u2 = reg & u1u2;
3276
3277 reg &= ~u1u2;
3278
3279 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3280 break;
3281 default:
3282 /* do nothing */
3283 break;
3284 }
3285 }
3286 }
3287
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06003288 switch (next) {
3289 case DWC3_LINK_STATE_U1:
3290 if (dwc->speed == USB_SPEED_SUPER)
3291 dwc3_suspend_gadget(dwc);
3292 break;
3293 case DWC3_LINK_STATE_U2:
3294 case DWC3_LINK_STATE_U3:
3295 dwc3_suspend_gadget(dwc);
3296 break;
3297 case DWC3_LINK_STATE_RESUME:
3298 dwc3_resume_gadget(dwc);
3299 break;
3300 default:
3301 /* do nothing */
3302 break;
3303 }
3304
Mayank Ranaa99689a2016-08-10 17:39:47 -07003305 dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next);
Felipe Balbie57ebc12014-04-22 13:20:12 -05003306 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003307 wake_up_interruptible(&dwc->wait_linkstate);
Felipe Balbi72246da2011-08-19 18:10:58 +03003308}
3309
Baolin Wang72704f82016-05-16 16:43:53 +08003310static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
Mayank Ranaa99689a2016-08-10 17:39:47 -07003311 unsigned int evtinfo)
Baolin Wang72704f82016-05-16 16:43:53 +08003312{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003313 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Baolin Wang72704f82016-05-16 16:43:53 +08003314
Mayank Rana558baca2017-02-17 11:46:38 -08003315 dbg_event(0xFF, "SUSPEND INT", 0);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003316 dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next);
3317
3318 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) {
3319 /*
3320 * When first connecting the cable, even before the initial
3321 * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE
3322 * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND
3323 * event. In such a case, ignore.
3324 * Ignore suspend event until device side usb is not into
3325 * CONFIGURED state.
3326 */
3327 if (dwc->gadget.state != USB_STATE_CONFIGURED) {
3328 pr_err("%s(): state:%d. Ignore SUSPEND.\n",
3329 __func__, dwc->gadget.state);
3330 return;
3331 }
3332
Baolin Wang72704f82016-05-16 16:43:53 +08003333 dwc3_suspend_gadget(dwc);
3334
Mayank Ranaa99689a2016-08-10 17:39:47 -07003335 dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__);
3336 dwc->b_suspend = true;
3337 dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT);
3338 }
3339
Baolin Wang72704f82016-05-16 16:43:53 +08003340 dwc->link_state = next;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003341 dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state);
Baolin Wang72704f82016-05-16 16:43:53 +08003342}
3343
Felipe Balbie1dadd32014-02-25 14:47:54 -06003344static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3345 unsigned int evtinfo)
3346{
3347 unsigned int is_ss = evtinfo & BIT(4);
3348
3349 /**
3350 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3351 * have a known issue which can cause USB CV TD.9.23 to fail
3352 * randomly.
3353 *
3354 * Because of this issue, core could generate bogus hibernation
3355 * events which SW needs to ignore.
3356 *
3357 * Refers to:
3358 *
3359 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3360 * Device Fallback from SuperSpeed
3361 */
3362 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3363 return;
3364
3365 /* enter hibernation here */
3366}
3367
Felipe Balbi72246da2011-08-19 18:10:58 +03003368static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3369 const struct dwc3_event_devt *event)
3370{
3371 switch (event->type) {
3372 case DWC3_DEVICE_EVENT_DISCONNECT:
3373 dwc3_gadget_disconnect_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003374 dwc->dbg_gadget_events.disconnect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003375 break;
3376 case DWC3_DEVICE_EVENT_RESET:
3377 dwc3_gadget_reset_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003378 dwc->dbg_gadget_events.reset++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003379 break;
3380 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3381 dwc3_gadget_conndone_interrupt(dwc);
Mayank Rana0c667b42017-02-09 11:56:51 -08003382 dwc->dbg_gadget_events.connect++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003383 break;
3384 case DWC3_DEVICE_EVENT_WAKEUP:
Mayank Ranaa99689a2016-08-10 17:39:47 -07003385 dwc3_gadget_wakeup_interrupt(dwc, false);
Mayank Rana0c667b42017-02-09 11:56:51 -08003386 dwc->dbg_gadget_events.wakeup++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003387 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06003388 case DWC3_DEVICE_EVENT_HIBER_REQ:
3389 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3390 "unexpected hibernation event\n"))
3391 break;
3392
3393 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3394 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03003395 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3396 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
Mayank Rana0c667b42017-02-09 11:56:51 -08003397 dwc->dbg_gadget_events.link_status_change++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003398 break;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003399 case DWC3_DEVICE_EVENT_SUSPEND:
Baolin Wang72704f82016-05-16 16:43:53 +08003400 if (dwc->revision < DWC3_REVISION_230A) {
3401 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003402 dwc->dbg_gadget_events.eopf++;
Baolin Wang72704f82016-05-16 16:43:53 +08003403 } else {
3404 dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event");
Mayank Rana558baca2017-02-17 11:46:38 -08003405 dbg_event(0xFF, "GAD SUS", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003406 dwc->dbg_gadget_events.suspend++;
Baolin Wang72704f82016-05-16 16:43:53 +08003407 /*
3408 * Ignore suspend event until the gadget enters into
3409 * USB_STATE_CONFIGURED state.
3410 */
3411 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3412 dwc3_gadget_suspend_interrupt(dwc,
3413 event->event_info);
3414 }
Felipe Balbi72246da2011-08-19 18:10:58 +03003415 break;
3416 case DWC3_DEVICE_EVENT_SOF:
Felipe Balbi73815282015-01-27 13:48:14 -06003417 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
Mayank Rana0c667b42017-02-09 11:56:51 -08003418 dwc->dbg_gadget_events.sof++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003419 break;
3420 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Felipe Balbi73815282015-01-27 13:48:14 -06003421 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
Mayank Rana558baca2017-02-17 11:46:38 -08003422 dbg_event(0xFF, "ERROR", 0);
Mayank Rana0c667b42017-02-09 11:56:51 -08003423 dwc->dbg_gadget_events.erratic_error++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003424 break;
3425 case DWC3_DEVICE_EVENT_CMD_CMPL:
Felipe Balbi73815282015-01-27 13:48:14 -06003426 dwc3_trace(trace_dwc3_gadget, "Command Complete");
Mayank Rana0c667b42017-02-09 11:56:51 -08003427 dwc->dbg_gadget_events.cmdcmplt++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003428 break;
3429 case DWC3_DEVICE_EVENT_OVERFLOW:
Felipe Balbi73815282015-01-27 13:48:14 -06003430 dwc3_trace(trace_dwc3_gadget, "Overflow");
Mayank Rana0c667b42017-02-09 11:56:51 -08003431 dwc->dbg_gadget_events.overflow++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003432 break;
3433 default:
Felipe Balbie9f2aa872015-01-27 13:49:28 -06003434 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
Mayank Rana0c667b42017-02-09 11:56:51 -08003435 dwc->dbg_gadget_events.unknown_event++;
Felipe Balbi72246da2011-08-19 18:10:58 +03003436 }
Mayank Ranaa99689a2016-08-10 17:39:47 -07003437
3438 dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR);
Felipe Balbi72246da2011-08-19 18:10:58 +03003439}
3440
3441static void dwc3_process_event_entry(struct dwc3 *dwc,
3442 const union dwc3_event *event)
3443{
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003444 trace_dwc3_event(event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003445 /* skip event processing in absence of vbus */
3446 if (!dwc->vbus_active) {
Mayank Rana6300b452017-05-24 09:33:22 -07003447 dbg_event(0xFF, "SKIP_EVT", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003448 return;
3449 }
3450
3451 /* If run/stop is cleared don't process any more events */
3452 if (!dwc->pullups_connected) {
Mayank Rana6300b452017-05-24 09:33:22 -07003453 dbg_event(0xFF, "SKIP_EVT_PULLUP", event->raw);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003454 return;
3455 }
Felipe Balbi2c4cbe6e52014-04-30 17:45:10 -05003456
Felipe Balbi72246da2011-08-19 18:10:58 +03003457 /* Endpoint IRQ, handle it and return early */
3458 if (event->type.is_devspec == 0) {
3459 /* depevt */
3460 return dwc3_endpoint_interrupt(dwc, &event->depevt);
3461 }
3462
3463 switch (event->type.type) {
3464 case DWC3_EVENT_TYPE_DEV:
3465 dwc3_gadget_interrupt(dwc, &event->devt);
3466 break;
3467 /* REVISIT what to do with Carkit and I2C events ? */
3468 default:
3469 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3470 }
3471}
3472
Mayank Ranaa99689a2016-08-10 17:39:47 -07003473static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc)
Felipe Balbif42f2442013-06-12 21:25:08 +03003474{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003475 struct dwc3_event_buffer *evt;
Felipe Balbif42f2442013-06-12 21:25:08 +03003476 irqreturn_t ret = IRQ_NONE;
3477 int left;
3478 u32 reg;
3479
Mayank Ranaa99689a2016-08-10 17:39:47 -07003480 evt = dwc->ev_buf;
Felipe Balbif42f2442013-06-12 21:25:08 +03003481 left = evt->count;
3482
3483 if (!(evt->flags & DWC3_EVENT_PENDING))
3484 return IRQ_NONE;
3485
3486 while (left > 0) {
3487 union dwc3_event event;
3488
3489 event.raw = *(u32 *) (evt->buf + evt->lpos);
3490
3491 dwc3_process_event_entry(dwc, &event);
3492
Mayank Ranaa99689a2016-08-10 17:39:47 -07003493 if (dwc->err_evt_seen) {
3494 /*
3495 * if erratic error, skip remaining events
3496 * while controller undergoes reset
3497 */
3498 evt->lpos = (evt->lpos + left) %
3499 DWC3_EVENT_BUFFERS_SIZE;
3500 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left);
3501 if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT))
3502 dwc->err_evt_seen = 0;
3503 break;
3504 }
3505
Felipe Balbif42f2442013-06-12 21:25:08 +03003506 /*
3507 * FIXME we wrap around correctly to the next entry as
3508 * almost all entries are 4 bytes in size. There is one
3509 * entry which has 12 bytes which is a regular entry
3510 * followed by 8 bytes data. ATM I don't know how
3511 * things are organized if we get next to the a
3512 * boundary so I worry about that once we try to handle
3513 * that.
3514 */
3515 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
3516 left -= 4;
Felipe Balbif42f2442013-06-12 21:25:08 +03003517 }
3518
Mayank Ranaa99689a2016-08-10 17:39:47 -07003519 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4);
Felipe Balbif42f2442013-06-12 21:25:08 +03003520 evt->count = 0;
3521 evt->flags &= ~DWC3_EVENT_PENDING;
3522 ret = IRQ_HANDLED;
3523
3524 /* Unmask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003525 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbif42f2442013-06-12 21:25:08 +03003526 reg &= ~DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003527 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbif42f2442013-06-12 21:25:08 +03003528
John Youn26cac202016-11-14 12:32:43 -08003529 if (dwc->imod_interval)
3530 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0),
3531 DWC3_GEVNTCOUNT_EHB);
3532
Felipe Balbif42f2442013-06-12 21:25:08 +03003533 return ret;
3534}
3535
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003536void dwc3_bh_work(struct work_struct *w)
3537{
3538 struct dwc3 *dwc = container_of(w, struct dwc3, bh_work);
3539
3540 pm_runtime_get_sync(dwc->dev);
3541 dwc3_thread_interrupt(dwc->irq, dwc);
3542 pm_runtime_put(dwc->dev);
3543}
3544
Mayank Ranaa99689a2016-08-10 17:39:47 -07003545static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
3546{
3547 struct dwc3 *dwc = _dwc;
Felipe Balbie5f68b42015-10-12 13:25:44 -05003548 unsigned long flags;
Felipe Balbib15a7622011-06-30 16:57:15 +03003549 irqreturn_t ret = IRQ_NONE;
Mayank Ranaa99689a2016-08-10 17:39:47 -07003550 unsigned int temp_time;
3551 ktime_t start_time;
3552
3553 start_time = ktime_get();
Felipe Balbib15a7622011-06-30 16:57:15 +03003554
Felipe Balbie5f68b42015-10-12 13:25:44 -05003555 spin_lock_irqsave(&dwc->lock, flags);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003556 dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0;
3557
3558 ret = dwc3_process_event_buf(dwc);
3559
Felipe Balbie5f68b42015-10-12 13:25:44 -05003560 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbib15a7622011-06-30 16:57:15 +03003561
Mayank Ranaa99689a2016-08-10 17:39:47 -07003562 temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time));
3563 dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time;
3564 dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10;
3565
Felipe Balbib15a7622011-06-30 16:57:15 +03003566 return ret;
3567}
3568
Mayank Ranaa99689a2016-08-10 17:39:47 -07003569static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003570{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003571 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03003572 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03003573 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03003574
Mayank Ranaa99689a2016-08-10 17:39:47 -07003575 evt = dwc->ev_buf;
Felipe Balbifc8bb912016-05-16 13:14:48 +03003576
Thinh Nguyenff9177b2017-05-11 17:26:47 -07003577 /*
3578 * With PCIe legacy interrupt, test shows that top-half irq handler can
3579 * be called again after HW interrupt deassertion. Check if bottom-half
3580 * irq event handler completes before caching new event to prevent
3581 * losing events.
3582 */
3583 if (evt->flags & DWC3_EVENT_PENDING)
3584 return IRQ_HANDLED;
3585
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003586 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
Felipe Balbi72246da2011-08-19 18:10:58 +03003587 count &= DWC3_GEVNTCOUNT_MASK;
3588 if (!count)
3589 return IRQ_NONE;
3590
Mayank Ranaa99689a2016-08-10 17:39:47 -07003591 if (count > evt->length) {
3592 dev_err(dwc->dev, "HUGE_EVCNT(%d)", count);
Mayank Rana558baca2017-02-17 11:46:38 -08003593 dbg_event(0xFF, "HUGE_EVCNT", count);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003594 evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE;
3595 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3596 return IRQ_HANDLED;
3597 }
3598
Felipe Balbib15a7622011-06-30 16:57:15 +03003599 evt->count = count;
3600 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03003601
Felipe Balbie8adfc32013-06-12 21:11:14 +03003602 /* Mask interrupt */
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003603 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
Felipe Balbie8adfc32013-06-12 21:11:14 +03003604 reg |= DWC3_GEVNTSIZ_INTMASK;
Felipe Balbi660e9bd2016-03-30 09:26:24 +03003605 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
Felipe Balbie8adfc32013-06-12 21:11:14 +03003606
John Youn551d2902016-11-15 13:08:59 +02003607 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3608
Felipe Balbib15a7622011-06-30 16:57:15 +03003609 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03003610}
3611
Mayank Ranaa99689a2016-08-10 17:39:47 -07003612irqreturn_t dwc3_interrupt(int irq, void *_dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003613{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003614 struct dwc3 *dwc = _dwc;
3615 irqreturn_t ret = IRQ_NONE;
3616 irqreturn_t status;
3617 unsigned int temp_cnt = 0;
3618 ktime_t start_time;
Felipe Balbi72246da2011-08-19 18:10:58 +03003619
Mayank Ranaa99689a2016-08-10 17:39:47 -07003620 start_time = ktime_get();
3621 dwc->irq_cnt++;
3622
3623 /* controller reset is still pending */
3624 if (dwc->err_evt_seen)
3625 return IRQ_HANDLED;
3626
3627 status = dwc3_check_event_buf(dwc);
3628 if (status == IRQ_WAKE_THREAD)
3629 ret = status;
3630
3631 dwc->irq_start_time[dwc->irq_dbg_index] = start_time;
3632 dwc->irq_completion_time[dwc->irq_dbg_index] =
3633 ktime_us_delta(ktime_get(), start_time);
3634 dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4;
3635 dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS;
3636
Hemant Kumar78c7c282016-08-09 12:28:55 -07003637 if (ret == IRQ_WAKE_THREAD)
Mayank Ranaf616a7f2017-03-20 16:10:39 -07003638 queue_work(dwc->dwc_wq, &dwc->bh_work);
Mayank Ranaa99689a2016-08-10 17:39:47 -07003639
3640 return IRQ_HANDLED;
Felipe Balbi72246da2011-08-19 18:10:58 +03003641}
3642
3643/**
3644 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08003645 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03003646 *
3647 * Returns 0 on success otherwise negative errno.
3648 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003649int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03003650{
Roger Quadros9522def2016-06-10 14:48:38 +03003651 int ret, irq;
3652 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3653
3654 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3655 if (irq == -EPROBE_DEFER)
3656 return irq;
3657
3658 if (irq <= 0) {
3659 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3660 if (irq == -EPROBE_DEFER)
3661 return irq;
3662
3663 if (irq <= 0) {
3664 irq = platform_get_irq(dwc3_pdev, 0);
3665 if (irq <= 0) {
3666 if (irq != -EPROBE_DEFER) {
3667 dev_err(dwc->dev,
3668 "missing peripheral IRQ\n");
3669 }
3670 if (!irq)
3671 irq = -EINVAL;
3672 return irq;
3673 }
3674 }
3675 }
3676
3677 dwc->irq_gadget = irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003678
Mayank Ranaa99689a2016-08-10 17:39:47 -07003679 INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work);
3680
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303681 dwc->ctrl_req = dma_alloc_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003682 &dwc->ctrl_req_addr, GFP_KERNEL);
3683 if (!dwc->ctrl_req) {
3684 dev_err(dwc->dev, "failed to allocate ctrl request\n");
3685 ret = -ENOMEM;
3686 goto err0;
3687 }
3688
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303689 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3690 sizeof(*dwc->ep0_trb) * 2,
3691 &dwc->ep0_trb_addr, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003692 if (!dwc->ep0_trb) {
3693 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3694 ret = -ENOMEM;
3695 goto err1;
3696 }
3697
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003698 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03003699 if (!dwc->setup_buf) {
Felipe Balbi72246da2011-08-19 18:10:58 +03003700 ret = -ENOMEM;
3701 goto err2;
3702 }
3703
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303704 dwc->ep0_bounce = dma_alloc_coherent(dwc->sysdev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003705 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
3706 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003707 if (!dwc->ep0_bounce) {
3708 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
3709 ret = -ENOMEM;
3710 goto err3;
3711 }
3712
Felipe Balbi04c03d12015-12-02 10:06:45 -06003713 dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL);
3714 if (!dwc->zlp_buf) {
3715 ret = -ENOMEM;
3716 goto err4;
3717 }
3718
Felipe Balbi72246da2011-08-19 18:10:58 +03003719 dwc->gadget.ops = &dwc3_gadget_ops;
Felipe Balbi72246da2011-08-19 18:10:58 +03003720 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02003721 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03003722 dwc->gadget.name = "dwc3-gadget";
Jianqiang Tang6a4290c2016-01-20 14:09:39 +08003723 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
Felipe Balbi72246da2011-08-19 18:10:58 +03003724
3725 /*
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003726 * FIXME We might be setting max_speed to <SUPER, however versions
3727 * <2.20a of dwc3 have an issue with metastability (documented
3728 * elsewhere in this driver) which tells us we can't set max speed to
3729 * anything lower than SUPER.
3730 *
3731 * Because gadget.max_speed is only used by composite.c and function
3732 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3733 * to happen so we avoid sending SuperSpeed Capability descriptor
3734 * together with our BOS descriptor as that could confuse host into
3735 * thinking we can handle super speed.
3736 *
3737 * Note that, in fact, we won't even support GetBOS requests when speed
3738 * is less than super speed because we don't have means, yet, to tell
3739 * composite.c that we are USB 2.0 + LPM ECN.
3740 */
3741 if (dwc->revision < DWC3_REVISION_220A)
3742 dwc3_trace(trace_dwc3_gadget,
Felipe Balbi60cfb372016-05-24 13:45:17 +03003743 "Changing max_speed on rev %08x",
Ben McCauleyb9e51b22015-11-16 10:47:24 -06003744 dwc->revision);
3745
3746 dwc->gadget.max_speed = dwc->maximum_speed;
3747
3748 /*
David Cohena4b9d942013-12-09 15:55:38 -08003749 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
3750 * on ep out.
3751 */
3752 dwc->gadget.quirk_ep_out_aligned_size = true;
3753
3754 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03003755 * REVISIT: Here we should clear all pending IRQs to be
3756 * sure we're starting from a well known location.
3757 */
3758
3759 ret = dwc3_gadget_init_endpoints(dwc);
3760 if (ret)
Felipe Balbi04c03d12015-12-02 10:06:45 -06003761 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003762
Felipe Balbi72246da2011-08-19 18:10:58 +03003763 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3764 if (ret) {
3765 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi04c03d12015-12-02 10:06:45 -06003766 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03003767 }
3768
Mayank Ranaa99689a2016-08-10 17:39:47 -07003769 if (!dwc->is_drd) {
3770 pm_runtime_no_callbacks(&dwc->gadget.dev);
3771 pm_runtime_set_active(&dwc->gadget.dev);
3772 pm_runtime_enable(&dwc->gadget.dev);
3773 pm_runtime_get(&dwc->gadget.dev);
3774 }
3775
Felipe Balbi72246da2011-08-19 18:10:58 +03003776 return 0;
3777
Felipe Balbi04c03d12015-12-02 10:06:45 -06003778err5:
3779 kfree(dwc->zlp_buf);
3780
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003781err4:
David Cohene1f80462013-09-11 17:42:47 -07003782 dwc3_gadget_free_endpoints(dwc);
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303783 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003784 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003785
Felipe Balbi72246da2011-08-19 18:10:58 +03003786err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003787 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003788
3789err2:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303790 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003791 dwc->ep0_trb, dwc->ep0_trb_addr);
3792
3793err1:
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303794 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003795 dwc->ctrl_req, dwc->ctrl_req_addr);
3796
3797err0:
3798 return ret;
3799}
3800
Felipe Balbi7415f172012-04-30 14:56:33 +03003801/* -------------------------------------------------------------------------- */
3802
Felipe Balbi72246da2011-08-19 18:10:58 +03003803void dwc3_gadget_exit(struct dwc3 *dwc)
3804{
Mayank Ranaa99689a2016-08-10 17:39:47 -07003805 if (dwc->is_drd) {
3806 pm_runtime_put(&dwc->gadget.dev);
3807 pm_runtime_disable(&dwc->gadget.dev);
3808 }
3809
Felipe Balbi72246da2011-08-19 18:10:58 +03003810 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03003811
Felipe Balbi72246da2011-08-19 18:10:58 +03003812 dwc3_gadget_free_endpoints(dwc);
3813
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303814 dma_free_coherent(dwc->sysdev, DWC3_EP0_BOUNCE_SIZE,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03003815 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003816
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003817 kfree(dwc->setup_buf);
Felipe Balbi04c03d12015-12-02 10:06:45 -06003818 kfree(dwc->zlp_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003819
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303820 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
Felipe Balbi72246da2011-08-19 18:10:58 +03003821 dwc->ep0_trb, dwc->ep0_trb_addr);
3822
Arnd Bergmann42695fc2016-11-17 17:13:47 +05303823 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ctrl_req),
Felipe Balbi72246da2011-08-19 18:10:58 +03003824 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03003825}
Felipe Balbi7415f172012-04-30 14:56:33 +03003826
Felipe Balbi0b0231a2014-10-07 10:19:23 -05003827int dwc3_gadget_suspend(struct dwc3 *dwc)
Felipe Balbi7415f172012-04-30 14:56:33 +03003828{
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003829 int ret;
3830
Roger Quadros9772b472016-04-12 11:33:29 +03003831 if (!dwc->gadget_driver)
3832 return 0;
3833
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003834 ret = dwc3_gadget_run_stop(dwc, false, false);
3835 if (ret < 0)
3836 return ret;
Felipe Balbi7415f172012-04-30 14:56:33 +03003837
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003838 dwc3_disconnect_gadget(dwc);
3839 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003840
3841 return 0;
3842}
3843
3844int dwc3_gadget_resume(struct dwc3 *dwc)
3845{
Felipe Balbi7415f172012-04-30 14:56:33 +03003846 int ret;
3847
Roger Quadros9772b472016-04-12 11:33:29 +03003848 if (!dwc->gadget_driver)
3849 return 0;
3850
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003851 ret = __dwc3_gadget_start(dwc);
3852 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003853 goto err0;
3854
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003855 ret = dwc3_gadget_run_stop(dwc, true, false);
3856 if (ret < 0)
Felipe Balbi7415f172012-04-30 14:56:33 +03003857 goto err1;
3858
Felipe Balbi7415f172012-04-30 14:56:33 +03003859 return 0;
3860
3861err1:
Felipe Balbi9f8a67b2016-05-04 15:50:27 +03003862 __dwc3_gadget_stop(dwc);
Felipe Balbi7415f172012-04-30 14:56:33 +03003863
3864err0:
3865 return ret;
3866}
Felipe Balbifc8bb912016-05-16 13:14:48 +03003867
3868void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3869{
3870 if (dwc->pending_events) {
3871 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3872 dwc->pending_events = false;
3873 enable_irq(dwc->irq_gadget);
3874 }
3875}